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