annotate src/DML/MainVisBundle/Resources/assets/marionette/modules/RepresentationModule/RepresentationModule.10-Master.js @ 0:493bcb69166c

added public content
author Daniel Wolff
date Tue, 09 Feb 2016 20:54:02 +0100
parents
children
rev   line source
Daniel@0 1 "use strict";
Daniel@0 2
Daniel@0 3 App.module("RepresentationModule", function(RepresentationModule, App, Backbone, Marionette, $, _, Logger) {
Daniel@0 4
Daniel@0 5 /**
Daniel@0 6 * Masters are "extensions" that know how to represent particular views and entities
Daniel@0 7 * They determine:
Daniel@0 8 * - which controls are located in the panels
Daniel@0 9 * - what labels do columns and rows have
Daniel@0 10 * - how high the rows are
Daniel@0 11 * - which data sit behind vis instances
Daniel@0 12 * - how vis instances render themselves
Daniel@0 13 */
Daniel@0 14 RepresentationModule.addInitializer(function(options){
Daniel@0 15
Daniel@0 16 RepresentationModule.MasterPrototypes = {};
Daniel@0 17 RepresentationModule.masters = {};
Daniel@0 18
Daniel@0 19 /**
Daniel@0 20 * Registers a new master
Daniel@0 21 */
Daniel@0 22 RepresentationModule.registerMaster = function(masterDefinition) {
Daniel@0 23 var MasterPrototype;
Daniel@0 24 var inheritId = masterDefinition.inherit ? masterDefinition.inherit : "";
Daniel@0 25 var id = masterDefinition.id;
Daniel@0 26 if (!id) {
Daniel@0 27 throw _.str.sprintf("Master definition does not contain an id");
Daniel@0 28 }
Daniel@0 29 var masterWithThisId = null;
Daniel@0 30 try {
Daniel@0 31 masterWithThisId = RepresentationModule.getMasterById(id);
Daniel@0 32 } catch (e) {
Daniel@0 33 }
Daniel@0 34 if (masterWithThisId) {
Daniel@0 35 throw _.str.sprintf("Master with id \"%s\" already exists", id);
Daniel@0 36 }
Daniel@0 37
Daniel@0 38 var prototypeOptions = {};
Daniel@0 39 var prototypeDefaultConfigParameterValues = {};
Daniel@0 40 //var prototypeErrorsToVisInstanceCoverParams = {};
Daniel@0 41 if (!inheritId) {
Daniel@0 42 MasterPrototype = Marionette.Object;
Daniel@0 43 } else {
Daniel@0 44 try {
Daniel@0 45 MasterPrototype = RepresentationModule.getMasterPrototypeById(inheritId);
Daniel@0 46 var parentMaster = RepresentationModule.getMasterById(inheritId);
Daniel@0 47 prototypeOptions = parentMaster.options;
Daniel@0 48 prototypeDefaultConfigParameterValues = parentMaster.defaultConfigParameterValues;
Daniel@0 49 //prototypeErrorsToVisInstanceCoverParams = parentMaster.errorsToVisInstanceCoverParams;
Daniel@0 50 } catch (e) {
Daniel@0 51 throw _.str.sprintf("Master with id \"%s\" could not be inherited from master with id \"%s\" (it does not exist)", id, inheritId);
Daniel@0 52 }
Daniel@0 53 }
Daniel@0 54
Daniel@0 55 var combinedOptions = masterDefinition.options
Daniel@0 56 ? _.defaults({}, masterDefinition.options, prototypeOptions)
Daniel@0 57 : _.clone(prototypeOptions);
Daniel@0 58 for (var key in masterDefinition.options) {
Daniel@0 59 if (masterDefinition.options.hasOwnProperty(key) && masterDefinition.options[key] === undefined) {
Daniel@0 60 delete combinedOptions[key];
Daniel@0 61 }
Daniel@0 62 }
Daniel@0 63 masterDefinition.options = combinedOptions;
Daniel@0 64
Daniel@0 65 var combinedDefaultConfigParameterValues = masterDefinition.defaultConfigParameterValues
Daniel@0 66 ? _.defaults({}, masterDefinition.defaultConfigParameterValues, prototypeDefaultConfigParameterValues)
Daniel@0 67 : _.clone(prototypeDefaultConfigParameterValues);
Daniel@0 68 for (var key in masterDefinition.defaultConfigParameterValues) {
Daniel@0 69 if (masterDefinition.defaultConfigParameterValues.hasOwnProperty(key) && masterDefinition.defaultConfigParameterValues[key] === undefined) {
Daniel@0 70 delete combinedDefaultConfigParameterValues[key];
Daniel@0 71 }
Daniel@0 72 }
Daniel@0 73 masterDefinition.defaultConfigParameterValues = combinedDefaultConfigParameterValues;
Daniel@0 74
Daniel@0 75 // masterDefinition.errorsToVisInstanceCoverParams = masterDefinition.errorsToVisInstanceCoverParams
Daniel@0 76 // ? _.defaults(masterDefinition.errorsToVisInstanceCoverParams, prototypeErrorsToVisInstanceCoverParams)
Daniel@0 77 // : prototypeErrorsToVisInstanceCoverParams;
Daniel@0 78
Daniel@0 79 var Master = MasterPrototype.extend(masterDefinition);
Daniel@0 80 RepresentationModule.MasterPrototypes[id] = Master;
Daniel@0 81 RepresentationModule.masters[id] = new Master();
Daniel@0 82 };
Daniel@0 83
Daniel@0 84 RepresentationModule.getMasterPrototypeById = function(id) {
Daniel@0 85 var MasterPrototype = RepresentationModule.MasterPrototypes[id];
Daniel@0 86 if (!MasterPrototype) {
Daniel@0 87 throw _.str.sprintf("MasterPrototype with id \"%s\" not found.", id);
Daniel@0 88 }
Daniel@0 89 return MasterPrototype;
Daniel@0 90 };
Daniel@0 91
Daniel@0 92 RepresentationModule.getMasterById = function(id) {
Daniel@0 93 var master = RepresentationModule.masters[id];
Daniel@0 94 if (!master) {
Daniel@0 95 throw _.str.sprintf("Master with id \"%s\" not found.", id);
Daniel@0 96 }
Daniel@0 97 return master;
Daniel@0 98 };
Daniel@0 99
Daniel@0 100 RepresentationModule.getMasterForConfig = function(config, lookAtPlanned) {
Daniel@0 101 var dimension = config.getDimension();
Daniel@0 102 var configGridType = config.getConfigGridType();
Daniel@0 103 var configKind = lookAtPlanned ? config.getPlannedParameterValue("kind") : config.getParameterValue("kind");
Daniel@0 104 configKind = _.str.trim(configKind);
Daniel@0 105 if (!configKind) {
Daniel@0 106 configKind = "default";
Daniel@0 107 }
Daniel@0 108 var id = [dimension, configGridType, configKind].join(".");
Daniel@0 109 var master = RepresentationModule.masters[id];
Daniel@0 110 if (!master) {
Daniel@0 111 try {
Daniel@0 112 master = RepresentationModule.getMasterById([dimension, "_unknown"].join("."));
Daniel@0 113 } catch (e) {
Daniel@0 114 throw _.str.sprintf("Master for config \"%s\" not found (master with id \"%s\" does not exist).", JSON.stringify(config.serialize()), id);
Daniel@0 115 }
Daniel@0 116 }
Daniel@0 117 return master;
Daniel@0 118 };
Daniel@0 119 });
Daniel@0 120 }, Logger);