Mercurial > hg > dml-open-vis
comparison src/DML/MainVisBundle/Resources/assets/marionette/modules/RepresentationModule/RepresentationModule.22-Master.entity.collection.pair.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 RepresentationModule.addInitializer(function(options){ | |
6 | |
7 RepresentationModule.registerMaster({ | |
8 id: "entity.collection.pair", | |
9 inherit: "entity._pair", | |
10 | |
11 | |
12 // ================================================================= | |
13 // config grid header | |
14 | |
15 _generateHeaderLabel1: function(entityHeaderView) { | |
16 return _.str.sprintf("collection comparison (%s)", this.getConfigParameterValueOrDefaultValue(entityHeaderView.options.config, "comparisonMode")); | |
17 }, | |
18 | |
19 | |
20 _generateHeaderLabel2: function(viewHeader) { | |
21 var configOnTheLeft = viewHeader.dynamicDerivedConfigData.attributes.collectionConfigOnTheLeft; | |
22 var configOnTheRight = viewHeader.dynamicDerivedConfigData.attributes.collectionConfigOnTheRight; | |
23 | |
24 var labelParts = []; | |
25 | |
26 if (configOnTheLeft) { | |
27 labelParts.push(this._generateCollectionConfigTitle(configOnTheLeft)); | |
28 } else { | |
29 labelParts.push("×"); | |
30 } | |
31 | |
32 labelParts.push(" ↔ "); | |
33 | |
34 if (configOnTheRight) { | |
35 labelParts.push(this._generateCollectionConfigTitle(configOnTheRight)); | |
36 } else { | |
37 labelParts.push("×"); | |
38 } | |
39 | |
40 return labelParts.join(""); | |
41 }, | |
42 | |
43 | |
44 // ================================================================= | |
45 // dynamic derived config data | |
46 | |
47 __optionsOfDynamicDerivedConfigData: { | |
48 attributesToExcludeFromHash: ["collectionConfigOnTheLeft", "collectionConfigOnTheRight"], | |
49 customHashSuffixGenerator: function (attributes) { | |
50 return (attributes.collectionConfigOnTheLeft ? attributes.collectionConfigOnTheLeft. getClientId() : "x") | |
51 + (attributes.collectionConfigOnTheRight ? attributes.collectionConfigOnTheRight.getClientId() : "x"); | |
52 // return (attributes.collectionConfigOnTheLeft ? attributes.collectionConfigOnTheLeft. getHashForParameters() : "x") | |
53 // + (attributes.collectionConfigOnTheRight ? attributes.collectionConfigOnTheRight.getHashForParameters() : "x"); | |
54 } | |
55 }, | |
56 | |
57 | |
58 __updateMethodOfDynamicDerivedConfigData: function (force) { | |
59 this._doUpdate(force); | |
60 if (this.attributes.collectionConfigOnTheLeft && this.attributes.dynamicDefinitionForCollectionOnTheLeft) { | |
61 return; | |
62 } | |
63 if (this.attributes.collectionConfigOnTheRight && this.attributes.dynamicDefinitionForCollectionOnTheRight) { | |
64 return; | |
65 } | |
66 var _this = this; | |
67 var interval = setInterval(function() { | |
68 _this._doUpdate(force); | |
69 if (_this.attributes.collectionConfigOnTheLeft && !_this.attributes.dynamicDefinitionForCollectionOnTheLeft) { | |
70 return; | |
71 } | |
72 if (_this.attributes.collectionConfigOnTheRight && !_this.attributes.dynamicDefinitionForCollectionOnTheRight) { | |
73 return; | |
74 } | |
75 clearInterval(interval); | |
76 }, 50); | |
77 }, | |
78 | |
79 __doUpdateMethodOfDynamicDerivedConfigData: function (force) { | |
80 var entityConfig = this.options.entityConfig; | |
81 var configGrid = this.options.configGrid; | |
82 | |
83 // do nothing with orphans (configs just before they are deleted) | |
84 if (!entityConfig.getDimension()) { | |
85 return; | |
86 } | |
87 | |
88 // find the nearest collections on the left and on the right | |
89 var newCollectionConfigOnTheLeft = entityConfig; | |
90 var newCollectionConfigOnTheRight = entityConfig; | |
91 | |
92 do { | |
93 newCollectionConfigOnTheLeft = configGrid.getPrevEntityNeighbour(newCollectionConfigOnTheLeft); | |
94 } while (newCollectionConfigOnTheLeft && newCollectionConfigOnTheLeft.getParameterValue("kind") == "pair"); | |
95 | |
96 do { | |
97 newCollectionConfigOnTheRight = configGrid.getNextEntityNeighbour(newCollectionConfigOnTheRight); | |
98 } while (newCollectionConfigOnTheRight && newCollectionConfigOnTheRight.getParameterValue("kind") == "pair"); | |
99 | |
100 var newDynamicDerivedConfigDataOnTheLeft = App.dynamicDerivedConfigDataProvider.get(newCollectionConfigOnTheLeft); | |
101 var newDynamicDerivedConfigDataOnTheRight = App.dynamicDerivedConfigDataProvider.get(newCollectionConfigOnTheRight); | |
102 | |
103 var newDynamicDefinitionForCollectionOnTheLeft = newDynamicDerivedConfigDataOnTheLeft ? newDynamicDerivedConfigDataOnTheLeft .attributes.dynamicDefinitionForCollection : null; | |
104 var newDynamicDefinitionForCollectionOnTheRight = newDynamicDerivedConfigDataOnTheRight ? newDynamicDerivedConfigDataOnTheRight.attributes.dynamicDefinitionForCollection : null; | |
105 | |
106 var attributesToSet = {}; | |
107 | |
108 var arrayOfShortcuts = [ | |
109 [newCollectionConfigOnTheLeft, "collectionConfigOnTheLeft", "change:parameters", this.triggerChange], | |
110 [newCollectionConfigOnTheRight, "collectionConfigOnTheRight", "change:parameters", this.triggerChange], | |
111 [newDynamicDefinitionForCollectionOnTheLeft, "dynamicDefinitionForCollectionOnTheLeft", "change", this.triggerChange], | |
112 [newDynamicDefinitionForCollectionOnTheRight, "dynamicDefinitionForCollectionOnTheRight", "change", this.triggerChange], | |
113 ]; | |
114 | |
115 for (var i = arrayOfShortcuts.length - 1; i >= 0; --i) { | |
116 var shortcuts = arrayOfShortcuts[i]; | |
117 var oldAttributeValue = this.attributes[shortcuts[1]]; | |
118 var newAttributeValue = shortcuts[0]; | |
119 if (newAttributeValue != oldAttributeValue) { | |
120 attributesToSet[shortcuts[1]] = newAttributeValue; | |
121 if (oldAttributeValue) { | |
122 this.stopListening(oldAttributeValue, shortcuts[2]); | |
123 } | |
124 if (newAttributeValue) { | |
125 this.listenTo(newAttributeValue, shortcuts[2], shortcuts[3]); | |
126 } | |
127 } | |
128 } | |
129 this.set(attributesToSet); | |
130 }, | |
131 | |
132 | |
133 __triggerChangeMethodOfDynamicDerivedConfigData: function() { | |
134 this.dropCachedHash(); | |
135 this.trigger("change"); | |
136 }, | |
137 | |
138 | |
139 generateDynamicDerivedConfigData: function(entityConfig, configGrid) { | |
140 var optionsOfThisDynamicDerivedConfigData = _.clone(this.__optionsOfDynamicDerivedConfigData); | |
141 optionsOfThisDynamicDerivedConfigData.entityConfig = entityConfig; | |
142 optionsOfThisDynamicDerivedConfigData.configGrid = configGrid; | |
143 | |
144 var dynamicDerivedConfigData = new RepresentationModule.DynamicDerivedConfigData({ | |
145 collectionConfigOnTheLeft: null, | |
146 collectionConfigOnTheRight: null, | |
147 dynamicDefinitionForCollectionOnTheLeft: null, | |
148 dynamicDefinitionForCollectionOnTheRight: null | |
149 }, optionsOfThisDynamicDerivedConfigData); | |
150 | |
151 dynamicDerivedConfigData.update = this.__updateMethodOfDynamicDerivedConfigData; | |
152 dynamicDerivedConfigData._doUpdate = this.__doUpdateMethodOfDynamicDerivedConfigData; | |
153 dynamicDerivedConfigData.triggerChange = this.__triggerChangeMethodOfDynamicDerivedConfigData; | |
154 | |
155 dynamicDerivedConfigData.listenTo(configGrid, "change_layout", dynamicDerivedConfigData.update); | |
156 dynamicDerivedConfigData.update(); | |
157 | |
158 return dynamicDerivedConfigData; | |
159 }, | |
160 | |
161 | |
162 // ================================================================= | |
163 // dynamic derived vis instance data | |
164 | |
165 | |
166 // ----------------------------------------------------------------- | |
167 // dynamic derived vis instance data - base | |
168 | |
169 __optionsOfDynamicDerivedVisInstanceDataForBase: { | |
170 attributesToExcludeFromHash: ["apiResponseOnTheLeft", "apiResponseOnTheRight"], | |
171 customHashSuffixGenerator: function (attributes) { | |
172 if (attributes.apiResponseOnTheLeft) { | |
173 return JSON.stringify(attributes.apiResponseOnTheLeft.errors); | |
174 } else { | |
175 return typeof attributes.apiResponseOnTheLeft; | |
176 } | |
177 if (attributes.apiResponseOnTheRight) { | |
178 return JSON.stringify(attributes.apiResponseOnTheRight.errors); | |
179 } else { | |
180 return typeof attributes.apiResponseOnTheRight; | |
181 } | |
182 } | |
183 }, | |
184 | |
185 | |
186 __upateMethodOfDynamicDerivedVisInstanceDataForBase: function(force) { | |
187 var visInstanceView = this.options.visInstanceView; | |
188 var _this = this; | |
189 _.each(["Right", "Left"], function(side) { | |
190 var dynamicDefinitionForCollection = visInstanceView.dynamicDerivedConfigDataForEntity.attributes["dynamicDefinitionForCollectionOnThe" + side]; | |
191 if (!dynamicDefinitionForCollection) { // entity kind has changed (e.g. a grid was reset) | |
192 return; | |
193 } | |
194 | |
195 var collectionId = dynamicDefinitionForCollection.attributes.id; | |
196 var requestParams = _.clone(visInstanceView.dynamicDerivedConfigDataForView.attributes.basePerspectiveRequestParams); | |
197 | |
198 if (!requestParams || !collectionId) { | |
199 var attrs = {}; | |
200 attrs["apiRequestURIOnThe" + side] = undefined; | |
201 attrs["apiRequestParamsHashOnThe" + side] = undefined; | |
202 attrs["apiResponseOnThe" + side] = undefined; | |
203 _this.set(attrs); | |
204 return; | |
205 } | |
206 requestParams.cid = collectionId; | |
207 | |
208 var apiRequestParamsHash = JSON.stringify(requestParams); | |
209 | |
210 if (!force && apiRequestParamsHash == _this.attributes["apiRequestParamsHashOnThe" + side]) { | |
211 return; | |
212 } | |
213 | |
214 var apiRequestURI = App.DataModule.CliopatriaAPI.request("getCollectionPerspective", requestParams, function(data){ | |
215 if (JSON.stringify(requestParams) != _this.attributes["apiRequestParamsHashOnThe" + side]) { | |
216 return; | |
217 } | |
218 var attrs = {}; | |
219 attrs["apiRequestParamsHashOnThe" + side] = undefined; | |
220 attrs["apiResponseOnThe" + side] = data; | |
221 _this.set(attrs); | |
222 }); | |
223 | |
224 var attrs = {}; | |
225 attrs["apiRequestURIOnThe" + side] = apiRequestURI; | |
226 attrs["apiRequestParamsHashOnThe" + side] = apiRequestParamsHash; | |
227 attrs["apiResponseOnThe" + side] = null; | |
228 _this.set(attrs); | |
229 }); | |
230 }, | |
231 | |
232 | |
233 generateDynamicDerivedVisInstanceDataForBase: function(visInstanceView) { | |
234 // An empty object if comparison is not supported | |
235 var viewMaster = visInstanceView._cachedViewMaster; | |
236 if (!viewMaster.options.visInstanceSupportedComparisonModes.length) { | |
237 return new RepresentationModule.DynamicDerivedVisInstanceData({}); | |
238 } | |
239 | |
240 var optionsForThisDynamicDerivedVisInstanceDataForBase = _.clone(this.__optionsOfDynamicDerivedVisInstanceDataForBase); | |
241 optionsForThisDynamicDerivedVisInstanceDataForBase.visInstanceView = visInstanceView; | |
242 | |
243 var dynamicDerivedVisInstanceDataForBase = new RepresentationModule.DynamicDerivedVisInstanceData({ | |
244 apiRequestURIOnTheLeft: undefined, | |
245 apiRequestParamsHashOnTheLeft: undefined, | |
246 apiResponseOnTheLeft: undefined, | |
247 apiRequestURIOnTheRight: undefined, | |
248 apiRequestParamsHashOnTheRight: undefined, | |
249 apiResponseOnTheRight: undefined | |
250 }, optionsForThisDynamicDerivedVisInstanceDataForBase); | |
251 | |
252 dynamicDerivedVisInstanceDataForBase.update = this.__upateMethodOfDynamicDerivedVisInstanceDataForBase; | |
253 | |
254 dynamicDerivedVisInstanceDataForBase.listenTo(visInstanceView.dynamicDerivedConfigDataForEntity, "change", dynamicDerivedVisInstanceDataForBase.update); | |
255 dynamicDerivedVisInstanceDataForBase.listenTo(visInstanceView.dynamicDerivedConfigDataForView, "change:basePerspectiveRequestParams", dynamicDerivedVisInstanceDataForBase.update); | |
256 | |
257 dynamicDerivedVisInstanceDataForBase.update(); | |
258 | |
259 return dynamicDerivedVisInstanceDataForBase; | |
260 }, | |
261 | |
262 | |
263 verifyAllDataForVisInstanceBase: function(visInstanceView) { | |
264 this._verifyThatViewIsNotEmptyOrUnknown(visInstanceView); | |
265 | |
266 // if (visInstanceView.options.entityConfig.getParameterValue("kind") | |
267 // && visInstanceView.options.viewConfig.getParameterValue("kind") == "key-relative-chord-seq") { | |
268 // throw new RepresentationModule.Error({type: "drawing", derivedDataToUpdate: "base"}); | |
269 // } | |
270 | |
271 var viewMaster = visInstanceView._cachedViewMaster; | |
272 if (!viewMaster.options.canHaveBase) { | |
273 return; | |
274 } | |
275 var supportedComparisonModes = viewMaster.options.visInstanceSupportedComparisonModes; | |
276 if (!supportedComparisonModes.length) { | |
277 throw new RepresentationModule.Error({type: "comparison_not-supported"}); | |
278 } | |
279 if (!_.contains(supportedComparisonModes, viewMaster._getVisInstanceViewComparisonMode(visInstanceView))) { | |
280 throw new RepresentationModule.Error({type: "comparison_wrong-type", supportedTypes: supportedComparisonModes}); | |
281 } | |
282 | |
283 var attributesOfDerivedConfigDataForEntityOnTheLeft = visInstanceView.dynamicDerivedConfigDataForEntity.attributes; | |
284 var attributesOfDerivedConfigDataForEntityOnTheRight = visInstanceView.dynamicDerivedConfigDataForEntity.attributes; | |
285 var dynamicDefinitionForCollectionOnTheLeft = attributesOfDerivedConfigDataForEntityOnTheLeft .dynamicDefinitionForCollectionOnTheLeft; | |
286 var dynamicDefinitionForCollectionOnTheRight = attributesOfDerivedConfigDataForEntityOnTheRight.dynamicDefinitionForCollectionOnTheRight; | |
287 var attributesOfCollectionOnTheLeft = dynamicDefinitionForCollectionOnTheLeft ? dynamicDefinitionForCollectionOnTheLeft.attributes : {}; | |
288 var attributesOfCollectionOnTheRight = dynamicDefinitionForCollectionOnTheRight ? dynamicDefinitionForCollectionOnTheRight.attributes : {}; | |
289 | |
290 if (attributesOfCollectionOnTheLeft.id === null || attributesOfCollectionOnTheRight.id === null) { | |
291 throw new RepresentationModule.Error({type: "data-preparing_entity-derived"}); | |
292 } | |
293 if (attributesOfCollectionOnTheLeft.id === false || attributesOfCollectionOnTheRight.id === false) { | |
294 var apiErrorsOnTheLeft = attributesOfCollectionOnTheLeft .errors || []; | |
295 var apiErrorsOnTheRight = attributesOfCollectionOnTheRight.errors || []; | |
296 apiErrors = apiErrorsOnTheLeft.concat(apiErrorsOnTheRight); | |
297 throw new RepresentationModule.Error({type: "api-error_entity-derived", apiErrors: apiErrors, coverTapAction: this.__coverTapActionThatUpdatesDynamicDerivedData, derivedDataToUpdate: "entity"}); | |
298 } | |
299 if ((attributesOfCollectionOnTheLeft .id === "" || (attributesOfCollectionOnTheLeft .hasOwnProperty("id") && attributesOfCollectionOnTheLeft .id === undefined)) | |
300 || (attributesOfCollectionOnTheRight.id === "" || (attributesOfCollectionOnTheRight.hasOwnProperty("id") && attributesOfCollectionOnTheRight.id === undefined))) { | |
301 throw new RepresentationModule.Error({type: "collection_no-recordings"}); | |
302 } | |
303 if (attributesOfCollectionOnTheLeft.id === undefined || attributesOfCollectionOnTheRight.id === undefined) { | |
304 throw new RepresentationModule.Error({type: "pair_incomplete"}); | |
305 } | |
306 | |
307 var attribytesOfDerivedVisInstanceDataForBase = visInstanceView.dynamicDerivedVisInstanceDataForBase.attributes; | |
308 if (!attribytesOfDerivedVisInstanceDataForBase.apiResponseOnTheLeft || !attribytesOfDerivedVisInstanceDataForBase.apiResponseOnTheRight) { | |
309 throw new RepresentationModule.Error({type: "data-preparing_base"}); | |
310 } | |
311 if (attribytesOfDerivedVisInstanceDataForBase.apiResponseOnTheLeft.errors || attribytesOfDerivedVisInstanceDataForBase.apiResponseOnTheRight.errors) { | |
312 var apiErrorsOnTheLeft = attribytesOfDerivedVisInstanceDataForBase.apiResponseOnTheLeft .errors || []; | |
313 var apiErrorsOnTheRight = attribytesOfDerivedVisInstanceDataForBase.apiResponseOnTheRight.errors || []; | |
314 var apiErrors = apiErrorsOnTheLeft.concat(apiErrorsOnTheRight); | |
315 | |
316 if (apiErrors[0]) { | |
317 var error0 = apiErrors[0]; | |
318 if (((error0.code == 11 || error0.code == 12) && !apiErrors[1]) || (apiErrors[1] && (apiErrors[1].code == 11 || apiErrors[1].code == 12))) { | |
319 throw new RepresentationModule.Error({type: "api-message_progress_base", apiErrors: apiErrors, coverTapAction: this.__coverTapActionThatUpdatesDynamicDerivedData, derivedDataToUpdate: "base"}); | |
320 | |
321 // FIXME errors like this should probably go to Master.view.xxx | |
322 } else if ((error0.code == 20 && !apiErrors[1]) || (apiErrors[1] && (apiErrors[1].code == 20))) { | |
323 return; | |
324 } | |
325 | |
326 } | |
327 | |
328 throw new RepresentationModule.Error({type: "api-error_base", apiErrors: apiErrors, coverTapAction: this.__coverTapActionThatUpdatesDynamicDerivedData, derivedDataToUpdate: "base"}); | |
329 } | |
330 }, | |
331 }); | |
332 }); | |
333 }, Logger); |