diff --git a/src/core/Remixer.ts b/src/core/Remixer.ts index 0f906b4..e64cb23 100644 --- a/src/core/Remixer.ts +++ b/src/core/Remixer.ts @@ -14,11 +14,15 @@ * under the License. */ -import * as vars from "./variables/variableTypes"; -import { Constants as CONST } from "../lib/Constants"; +import { BooleanVariable } from "./variables/BooleanVariable"; +import { ColorVariable } from "./variables/ColorVariable"; +import { KeyCode, KeyEvent, CSS } from "../lib/Constants"; import { LocalStorage } from "../lib/LocalStorage"; import { Messaging } from "../lib/Messaging"; -import { VariableCallback, VariableKeyMap } from "./variables/Variable"; +import { NumberVariable } from "./variables/NumberVariable"; +import { RangeVariable } from "./variables/RangeVariable"; +import { StringVariable } from "./variables/StringVariable"; +import { Variable, VariableCallback, VariableKeyMap } from "./variables/Variable"; /** * The Remixer class is a singleton class that keeps track of all the Variables @@ -61,7 +65,7 @@ class Remixer { private appendFrameToBody(): void { if (!this._frameElement) { let frame = document.createElement("IFRAME") as HTMLFrameElement; - frame.id = CONST.ID_OVERLAY_FRAME; + frame.id = CSS.RMX_OVERLAY_FRAME; frame.setAttribute("src", "../dist/overlay.html"); frame.setAttribute("style", "position:fixed; border:0; width:400px; height:100%; top:0; right:0; z-index:999999;"); frame.setAttribute("sandbox", "allow-scripts allow-same-origin"); @@ -75,8 +79,8 @@ class Remixer { * @private */ private addKeyListener(): void { - document.addEventListener(CONST.KEY_EVENT_DOWN, (e: KeyboardEvent) => { - if (e.keyCode === CONST.KEY_CODE_ESC) { + document.addEventListener(KeyEvent.DOWN, (e: KeyboardEvent) => { + if (e.keyCode === KeyCode.ESC) { Messaging.postToFrame(Messaging.type.ToggleVisibility); } }); @@ -108,8 +112,8 @@ class Remixer { * when the Variable is updated. * @return {BooleanVariable} */ - static addBooleanVariable(key: string, defaultValue: boolean, callback?: VariableCallback): vars.BooleanVariable { - let variable = new vars.BooleanVariable(key, defaultValue, callback); + static addBooleanVariable(key: string, defaultValue: boolean, callback?: VariableCallback): BooleanVariable { + let variable = new BooleanVariable(key, defaultValue, callback); this.addVariable(variable); return variable; } @@ -125,8 +129,8 @@ class Remixer { * when the Variable is updated. * @return {RangeVariable} */ - static addRangeVariable(key: string, defaultValue: number, minValue: number, maxValue: number, increment: number, callback?: VariableCallback): vars.RangeVariable { - let variable = new vars.RangeVariable(key, defaultValue, minValue, maxValue, increment, callback); + static addRangeVariable(key: string, defaultValue: number, minValue: number, maxValue: number, increment: number, callback?: VariableCallback): RangeVariable { + let variable = new RangeVariable(key, defaultValue, minValue, maxValue, increment, callback); this.addVariable(variable); return variable; } @@ -140,8 +144,8 @@ class Remixer { * when the Variable is updated. * @return {StringVariable} */ - static addStringVariable(key: string, defaultValue: string, possibleValues?: Array, callback?: VariableCallback): vars.StringVariable { - let variable = new vars.StringVariable(key, defaultValue, possibleValues, callback); + static addStringVariable(key: string, defaultValue: string, possibleValues?: Array, callback?: VariableCallback): StringVariable { + let variable = new StringVariable(key, defaultValue, possibleValues, callback); this.addVariable(variable); return variable; } @@ -155,8 +159,8 @@ class Remixer { * when the Variable is updated. * @return {NumberVariable} */ - static addNumberVariable(key: string, defaultValue: number, possibleValues?: Array, callback?: VariableCallback): vars.NumberVariable { - let variable = new vars.NumberVariable(key, defaultValue, possibleValues, callback); + static addNumberVariable(key: string, defaultValue: number, possibleValues?: Array, callback?: VariableCallback): NumberVariable { + let variable = new NumberVariable(key, defaultValue, possibleValues, callback); this.addVariable(variable); return variable; } @@ -170,8 +174,8 @@ class Remixer { * when the Variable is updated. * @return {ColorVariable} */ - static addColorVariable(key: string, defaultValue: string, possibleValues?: Array, callback?: VariableCallback): vars.ColorVariable { - let variable = new vars.ColorVariable(key, defaultValue, possibleValues, callback); + static addColorVariable(key: string, defaultValue: string, possibleValues?: Array, callback?: VariableCallback): ColorVariable { + let variable = new ColorVariable(key, defaultValue, possibleValues, callback); this.addVariable(variable); return variable; } @@ -183,7 +187,7 @@ class Remixer { * @static * @param {Variable} variable The variable to add. */ - private static addVariable(variable: vars.Variable): void { + private static addVariable(variable: Variable): void { let key: string = variable.key; let existingVariable = this.getVariable(key); if (existingVariable) { @@ -217,16 +221,16 @@ class Remixer { * Returns an array of Variables from the Remixer shared instance. * @return {Array} Array of Variables. */ - get variablesArray(): Array { + get variablesArray(): Array { return Object.keys(this._variables).map(key => this._variables[key]); } /** * Returns an Variable for a given key from the Remixer shared instance. * @static - * @return {Array} Array of Variables. + * @return {Array} Array of Variables. */ - static getVariable(key: string): vars.Variable { + static getVariable(key: string): Variable { return this._sharedInstance._variables[key]; } @@ -236,7 +240,7 @@ class Remixer { * @param {Variable} variable The Variable to update. * @param {any} selectedValue The new selected value. */ - static updateVariable(variable: vars.Variable, selectedValue: any): void { + static updateVariable(variable: Variable, selectedValue: any): void { variable.selectedValue = selectedValue; } @@ -244,7 +248,7 @@ class Remixer { * Saves the Variable to local storage. * @param {Variable} variable The Variable to save. */ - static saveVariable(variable: vars.Variable): void { + static saveVariable(variable: Variable): void { LocalStorage.saveVariable(variable); } } diff --git a/src/core/variables/BooleanVariable.ts b/src/core/variables/BooleanVariable.ts index a52ec9a..7159f5b 100644 --- a/src/core/variables/BooleanVariable.ts +++ b/src/core/variables/BooleanVariable.ts @@ -14,16 +14,16 @@ * under the License. */ -import { Constants as CONST } from "../../lib/Constants"; import { SerializableData } from "../../lib/LocalStorage"; -import { Variable, VariableType, VariableCallback } from "./Variable"; +import { Variable, VariableParams, VariableCallback } from "./Variable"; +import { VariableType } from "../../lib/Constants"; /** * Interface for a class that represents a type of Variable for boolean values. * @interface - * @extends VariableType + * @extends VariableParams */ -interface BooleanVariableType extends VariableType { +interface BooleanVariableParams extends VariableParams { defaultValue: boolean; selectedValue: boolean; } @@ -32,9 +32,9 @@ interface BooleanVariableType extends VariableType { * A class representing a type of Variable for boolean values. * @class * @extends Variable - * @implements {BooleanVariableType} + * @implements {BooleanVariableParams} */ -export class BooleanVariable extends Variable implements BooleanVariableType { +export class BooleanVariable extends Variable implements BooleanVariableParams { /** * Creates an instance of a BooleanVariable. @@ -45,7 +45,7 @@ export class BooleanVariable extends Variable implements BooleanVariableType { * @return {BooleanVariable} */ constructor(key: string, defaultValue: boolean, callback?: VariableCallback) { - super(key, CONST.VARIABLE_TYPE_BOOLEAN, defaultValue, callback); + super(key, VariableType.BOOLEAN, defaultValue, callback); } /** diff --git a/src/core/variables/ColorVariable.ts b/src/core/variables/ColorVariable.ts index 4700729..da238fb 100644 --- a/src/core/variables/ColorVariable.ts +++ b/src/core/variables/ColorVariable.ts @@ -14,16 +14,16 @@ * under the License. */ -import { Constants as CONST } from "../../lib/Constants"; import { SerializableData } from "../../lib/LocalStorage"; -import { Variable, VariableListType, VariableCallback } from "./Variable"; +import { Variable, VariableListParams, VariableCallback } from "./Variable"; +import { VariableType } from "../../lib/Constants"; /** * Interface for a class that represents a type of Variable for color values. * @interface - * @extends VariableListType + * @extends VariableListParams */ -interface ColorVariableType extends VariableListType { +interface ColorVariableParams extends VariableListParams { defaultValue: string; selectedValue: string; possibleValues?: Array; @@ -33,9 +33,9 @@ interface ColorVariableType extends VariableListType { * A class representing a type of Variable for color values. * @class * @extends Variable - * @implements {ColorVariableType} + * @implements {ColorVariableParams} */ -export class ColorVariable extends Variable implements ColorVariableType { +export class ColorVariable extends Variable implements ColorVariableParams { /** * Creates an instance of a ColorVariable. @@ -47,7 +47,7 @@ export class ColorVariable extends Variable implements ColorVariableType { * @return {ColorVariable} */ constructor(key: string, defaultValue: string, possibleValues?: Array, callback?: VariableCallback) { - super(key, CONST.VARIABLE_TYPE_COLOR, defaultValue, callback); + super(key, VariableType.COLOR, defaultValue, callback); this.possibleValues = possibleValues; } diff --git a/src/core/variables/NumberVariable.ts b/src/core/variables/NumberVariable.ts index bab96d2..e0d44fa 100644 --- a/src/core/variables/NumberVariable.ts +++ b/src/core/variables/NumberVariable.ts @@ -14,16 +14,16 @@ * under the License. */ -import { Constants as CONST } from "../../lib/Constants"; import { SerializableData } from "../../lib/LocalStorage"; -import { Variable, VariableListType, VariableCallback } from "./Variable"; +import { Variable, VariableListParams, VariableCallback } from "./Variable"; +import { VariableType } from "../../lib/Constants"; /** * Interface for a class that represents a type of Variable for number values. * @interface - * @extends VariableListType + * @extends VariableListParams */ -interface NumberVariableType extends VariableListType { +interface NumberVariableParams extends VariableListParams { defaultValue: number; selectedValue: number; possibleValues?: Array; @@ -33,9 +33,9 @@ interface NumberVariableType extends VariableListType { * A class representing a type of Variable for number values. * @class * @extends Variable - * @implements {NumberVariableType} + * @implements {NumberVariableParams} */ -export class NumberVariable extends Variable implements NumberVariableType { +export class NumberVariable extends Variable implements NumberVariableParams { /** * Creates an instance of a ColorVariable. @@ -47,7 +47,7 @@ export class NumberVariable extends Variable implements NumberVariableType { * @return {[NumberVariable]} */ constructor(key: string, defaultValue: number, possibleValues?: Array, callback?: VariableCallback) { - super(key, CONST.VARIABLE_TYPE_NUMBER, defaultValue, callback); + super(key, VariableType.NUMBER, defaultValue, callback); this.possibleValues = possibleValues; } diff --git a/src/core/variables/RangeVariable.ts b/src/core/variables/RangeVariable.ts index 375bfdc..d6a7928 100644 --- a/src/core/variables/RangeVariable.ts +++ b/src/core/variables/RangeVariable.ts @@ -14,16 +14,16 @@ * under the License. */ -import { Constants as CONST } from "../../lib/Constants"; import { SerializableData } from "../../lib/LocalStorage"; -import { Variable, VariableType, VariableCallback } from "./Variable"; +import { Variable, VariableParams, VariableCallback } from "./Variable"; +import { VariableType } from "../../lib/Constants"; /** * Interface for a class that represents a type of Variable for a range of values. * @interface - * @extends VariableType + * @extends VariableParams */ -interface RangeVariableType extends VariableType { +interface RangeVariableParams extends VariableParams { defaultValue: number; selectedValue: number; minValue: number; @@ -35,9 +35,9 @@ interface RangeVariableType extends VariableType { * A class representing a type of Variable for a range of values. * @class * @extends Variable - * @implements {RangeVariableType} + * @implements {RangeVariableParams} */ -export class RangeVariable extends Variable implements RangeVariableType { +export class RangeVariable extends Variable implements RangeVariableParams { /** * Creates an instance of a RangeVariable. @@ -50,7 +50,7 @@ export class RangeVariable extends Variable implements RangeVariableType { * @return {RangeVariable} */ constructor(key: string, defaultValue: number, minValue: number, maxValue: number, increment: number, callback?: VariableCallback) { - super(key, CONST.VARIABLE_TYPE_RANGE, defaultValue, callback); + super(key, VariableType.RANGE, defaultValue, callback); this.minValue = minValue; this.maxValue = maxValue; this.increment = increment; diff --git a/src/core/variables/StringVariable.ts b/src/core/variables/StringVariable.ts index c217491..812550f 100644 --- a/src/core/variables/StringVariable.ts +++ b/src/core/variables/StringVariable.ts @@ -14,16 +14,16 @@ * under the License. */ -import { Constants as CONST } from "../../lib/Constants"; import { SerializableData } from "../../lib/LocalStorage"; -import { Variable, VariableListType, VariableCallback } from "./Variable"; +import { Variable, VariableListParams, VariableCallback } from "./Variable"; +import { VariableType } from "../../lib/Constants"; /** * Interface for a class that represents a type of Variable for string values. * @interface - * @extends VariableListType + * @extends VariableListParams */ -interface StringVariableType extends VariableListType { +interface StringVariableParams extends VariableListParams { defaultValue: string; selectedValue: string; possibleValues?: Array; @@ -33,9 +33,9 @@ interface StringVariableType extends VariableListType { * A class representing a type of Variable for string values. * @class * @extends Variable - * @implements {StringVariableType} + * @implements {StringVariableParams} */ -export class StringVariable extends Variable implements StringVariableType { +export class StringVariable extends Variable implements StringVariableParams { /** * Creates an instance of a StringVariable. @@ -47,7 +47,7 @@ export class StringVariable extends Variable implements StringVariableType { * @return {StringVariable} */ constructor(key: string, defaultValue: string, possibleValues?: Array, callback?: VariableCallback) { - super(key, CONST.VARIABLE_TYPE_STRING, defaultValue, callback); + super(key, VariableType.STRING, defaultValue, callback); this.possibleValues = possibleValues; } diff --git a/src/core/variables/Variable.ts b/src/core/variables/Variable.ts index e1079c4..b4bb281 100644 --- a/src/core/variables/Variable.ts +++ b/src/core/variables/Variable.ts @@ -14,7 +14,6 @@ * under the License. */ -import { Constants as CONST } from "../../lib/Constants"; import { SerializableData } from "../../lib/LocalStorage"; import { remixer } from "../Remixer"; @@ -22,7 +21,7 @@ import { remixer } from "../Remixer"; * Interface for a class that represents a type a Variable. * @interface */ -export interface VariableType { +export interface VariableParams { key: string; title: string; dataType: string; @@ -34,8 +33,9 @@ export interface VariableType { /** * Interface for a class that represents a type a Variable with possible values. * @interface + * @extends VariableParams */ -export interface VariableListType extends VariableType { +export interface VariableListParams extends VariableParams { possibleValues?: Array; } @@ -59,9 +59,9 @@ export interface VariableKeyMap { /** * A class representing a type a Variable. * @class - * @implements {VariableType} + * @implements {VariableParams} */ -export class Variable implements VariableType { +export class Variable implements VariableParams { /** * Creates an instance of a Variable. diff --git a/src/core/variables/variableTypes.ts b/src/core/variables/variableTypes.ts deleted file mode 100644 index 5173f4c..0000000 --- a/src/core/variables/variableTypes.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** @license - * Copyright 2016 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy - * of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -/** Convenience export of all variable classes. */ -export * from "./BooleanVariable" -export * from "./ColorVariable"; -export * from "./NumberVariable"; -export * from "./RangeVariable"; -export * from "./StringVariable"; -export * from "./Variable" diff --git a/src/lib/LocalStorage.ts b/src/lib/LocalStorage.ts index 9f03e09..85b6de1 100644 --- a/src/lib/LocalStorage.ts +++ b/src/lib/LocalStorage.ts @@ -14,9 +14,14 @@ * under the License. */ -import * as vars from "../core/variables/variableTypes"; -import { Constants as CONST } from "../lib/Constants"; import { remixer } from "../core/Remixer"; +import { BooleanVariable } from "../core/variables/BooleanVariable"; +import { ColorVariable } from "../core/variables/ColorVariable"; +import { NumberVariable } from "../core/variables/NumberVariable"; +import { RangeVariable } from "../core/variables/RangeVariable"; +import { StorageKey, VariableType } from "../lib/Constants"; +import { StringVariable } from "../core/variables/StringVariable"; +import { Variable } from "../core/variables/Variable"; /** * Interface for a class that represents serialized data. @@ -51,10 +56,10 @@ export class LocalStorage { /** * Retrieves a single Variable from local storage. * @static - * @param {string} key The key if the Variable to retrieve. + * @param {string} key The key if the Variable to retrieve. * @return {Variable} */ - static getVariable(key: string): vars.Variable { + static getVariable(key: string): Variable { let remixerData = this.getRawData(); let variableData = remixerData[key] as SerializableData; if (variableData) { @@ -68,7 +73,7 @@ export class LocalStorage { * @static * @param {Variable} variable The variable to save. */ - static saveVariable(variable: vars.Variable): void { + static saveVariable(variable: Variable): void { let remixerData = this.getRawData(); remixerData[variable.key] = variable.serialize(); this.saveRawData(remixerData); @@ -81,18 +86,18 @@ export class LocalStorage { * @param {SerializableData} data The serialized data. * @return {Variable} */ - private static deserialize(data: SerializableData): vars.Variable { + private static deserialize(data: SerializableData): Variable { switch (data.dataType) { - case CONST.VARIABLE_TYPE_BOOLEAN: - return vars.BooleanVariable.deserialize(data); - case CONST.VARIABLE_TYPE_COLOR: - return vars.ColorVariable.deserialize(data); - case CONST.VARIABLE_TYPE_NUMBER: - return vars.NumberVariable.deserialize(data); - case CONST.VARIABLE_TYPE_RANGE: - return vars.RangeVariable.deserialize(data); - case CONST.VARIABLE_TYPE_STRING: - return vars.StringVariable.deserialize(data); + case VariableType.BOOLEAN: + return BooleanVariable.deserialize(data); + case VariableType.COLOR: + return ColorVariable.deserialize(data); + case VariableType.NUMBER: + return NumberVariable.deserialize(data); + case VariableType.RANGE: + return RangeVariable.deserialize(data); + case VariableType.STRING: + return StringVariable.deserialize(data); default: return null; } @@ -105,7 +110,7 @@ export class LocalStorage { * @return {SerializableDataMap} The json data from local storage. */ private static getRawData(): SerializableDataMap { - let data: SerializableDataMap = JSON.parse(localStorage.getItem(CONST.STORAGE_KEY_REMIXER)); + let data: SerializableDataMap = JSON.parse(localStorage.getItem(StorageKey.REMIXER)); return data || {}; } @@ -116,6 +121,6 @@ export class LocalStorage { * @param {SerializableDataMap} data The serialized data to save. */ private static saveRawData(data: SerializableDataMap): void { - localStorage.setItem(CONST.STORAGE_KEY_REMIXER, JSON.stringify(data)); + localStorage.setItem(StorageKey.REMIXER, JSON.stringify(data)); } } diff --git a/src/lib/Messaging.ts b/src/lib/Messaging.ts index bb3847d..7201668 100644 --- a/src/lib/Messaging.ts +++ b/src/lib/Messaging.ts @@ -14,7 +14,7 @@ * under the License. */ -import { Constants as CONST } from "../lib/Constants"; +import { CSS } from "../lib/Constants"; /** * Available messaging types. @@ -72,7 +72,7 @@ export class Messaging { * @param {MessagingType} message The message type to send. */ static postToFrame(message: MessagingType): void { - const frame = document.getElementById(CONST.ID_OVERLAY_FRAME) as HTMLFrameElement; + const frame = document.getElementById(CSS.RMX_OVERLAY_FRAME) as HTMLFrameElement; frame.contentWindow.postMessage(message, "*"); } }