Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("RepresentationModule", function(RepresentationModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: Daniel@0: /** Daniel@0: * Masters are "extensions" that know how to represent particular views and entities Daniel@0: * They determine: Daniel@0: * - which controls are located in the panels Daniel@0: * - what labels do columns and rows have Daniel@0: * - how high the rows are Daniel@0: * - which data sit behind vis instances Daniel@0: * - how vis instances render themselves Daniel@0: */ Daniel@0: RepresentationModule.addInitializer(function(options){ Daniel@0: Daniel@0: RepresentationModule.MasterPrototypes = {}; Daniel@0: RepresentationModule.masters = {}; Daniel@0: Daniel@0: /** Daniel@0: * Registers a new master Daniel@0: */ Daniel@0: RepresentationModule.registerMaster = function(masterDefinition) { Daniel@0: var MasterPrototype; Daniel@0: var inheritId = masterDefinition.inherit ? masterDefinition.inherit : ""; Daniel@0: var id = masterDefinition.id; Daniel@0: if (!id) { Daniel@0: throw _.str.sprintf("Master definition does not contain an id"); Daniel@0: } Daniel@0: var masterWithThisId = null; Daniel@0: try { Daniel@0: masterWithThisId = RepresentationModule.getMasterById(id); Daniel@0: } catch (e) { Daniel@0: } Daniel@0: if (masterWithThisId) { Daniel@0: throw _.str.sprintf("Master with id \"%s\" already exists", id); Daniel@0: } Daniel@0: Daniel@0: var prototypeOptions = {}; Daniel@0: var prototypeDefaultConfigParameterValues = {}; Daniel@0: //var prototypeErrorsToVisInstanceCoverParams = {}; Daniel@0: if (!inheritId) { Daniel@0: MasterPrototype = Marionette.Object; Daniel@0: } else { Daniel@0: try { Daniel@0: MasterPrototype = RepresentationModule.getMasterPrototypeById(inheritId); Daniel@0: var parentMaster = RepresentationModule.getMasterById(inheritId); Daniel@0: prototypeOptions = parentMaster.options; Daniel@0: prototypeDefaultConfigParameterValues = parentMaster.defaultConfigParameterValues; Daniel@0: //prototypeErrorsToVisInstanceCoverParams = parentMaster.errorsToVisInstanceCoverParams; Daniel@0: } catch (e) { Daniel@0: throw _.str.sprintf("Master with id \"%s\" could not be inherited from master with id \"%s\" (it does not exist)", id, inheritId); Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: var combinedOptions = masterDefinition.options Daniel@0: ? _.defaults({}, masterDefinition.options, prototypeOptions) Daniel@0: : _.clone(prototypeOptions); Daniel@0: for (var key in masterDefinition.options) { Daniel@0: if (masterDefinition.options.hasOwnProperty(key) && masterDefinition.options[key] === undefined) { Daniel@0: delete combinedOptions[key]; Daniel@0: } Daniel@0: } Daniel@0: masterDefinition.options = combinedOptions; Daniel@0: Daniel@0: var combinedDefaultConfigParameterValues = masterDefinition.defaultConfigParameterValues Daniel@0: ? _.defaults({}, masterDefinition.defaultConfigParameterValues, prototypeDefaultConfigParameterValues) Daniel@0: : _.clone(prototypeDefaultConfigParameterValues); Daniel@0: for (var key in masterDefinition.defaultConfigParameterValues) { Daniel@0: if (masterDefinition.defaultConfigParameterValues.hasOwnProperty(key) && masterDefinition.defaultConfigParameterValues[key] === undefined) { Daniel@0: delete combinedDefaultConfigParameterValues[key]; Daniel@0: } Daniel@0: } Daniel@0: masterDefinition.defaultConfigParameterValues = combinedDefaultConfigParameterValues; Daniel@0: Daniel@0: // masterDefinition.errorsToVisInstanceCoverParams = masterDefinition.errorsToVisInstanceCoverParams Daniel@0: // ? _.defaults(masterDefinition.errorsToVisInstanceCoverParams, prototypeErrorsToVisInstanceCoverParams) Daniel@0: // : prototypeErrorsToVisInstanceCoverParams; Daniel@0: Daniel@0: var Master = MasterPrototype.extend(masterDefinition); Daniel@0: RepresentationModule.MasterPrototypes[id] = Master; Daniel@0: RepresentationModule.masters[id] = new Master(); Daniel@0: }; Daniel@0: Daniel@0: RepresentationModule.getMasterPrototypeById = function(id) { Daniel@0: var MasterPrototype = RepresentationModule.MasterPrototypes[id]; Daniel@0: if (!MasterPrototype) { Daniel@0: throw _.str.sprintf("MasterPrototype with id \"%s\" not found.", id); Daniel@0: } Daniel@0: return MasterPrototype; Daniel@0: }; Daniel@0: Daniel@0: RepresentationModule.getMasterById = function(id) { Daniel@0: var master = RepresentationModule.masters[id]; Daniel@0: if (!master) { Daniel@0: throw _.str.sprintf("Master with id \"%s\" not found.", id); Daniel@0: } Daniel@0: return master; Daniel@0: }; Daniel@0: Daniel@0: RepresentationModule.getMasterForConfig = function(config, lookAtPlanned) { Daniel@0: var dimension = config.getDimension(); Daniel@0: var configGridType = config.getConfigGridType(); Daniel@0: var configKind = lookAtPlanned ? config.getPlannedParameterValue("kind") : config.getParameterValue("kind"); Daniel@0: configKind = _.str.trim(configKind); Daniel@0: if (!configKind) { Daniel@0: configKind = "default"; Daniel@0: } Daniel@0: var id = [dimension, configGridType, configKind].join("."); Daniel@0: var master = RepresentationModule.masters[id]; Daniel@0: if (!master) { Daniel@0: try { Daniel@0: master = RepresentationModule.getMasterById([dimension, "_unknown"].join(".")); Daniel@0: } catch (e) { Daniel@0: throw _.str.sprintf("Master for config \"%s\" not found (master with id \"%s\" does not exist).", JSON.stringify(config.serialize()), id); Daniel@0: } Daniel@0: } Daniel@0: return master; Daniel@0: }; Daniel@0: }); Daniel@0: }, Logger);