annotate src/DML/MainVisBundle/Resources/assets/marionette/modules/ContextModule/ContextModule.10-ConfigGrid.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("ContextModule", function(ContextModule, App, Backbone, Marionette, $, _, Logger) {
Daniel@0 4
Daniel@0 5 // Define private variables
Daniel@0 6 var logger = null;
Daniel@0 7
Daniel@0 8 ContextModule.addInitializer(function(options){
Daniel@0 9
Daniel@0 10 logger = Logger.get("ContextModule.ConfigGrid");
Daniel@0 11 logger.setLevel(Logger.WARN);
Daniel@0 12
Daniel@0 13 /**
Daniel@0 14 * ConfigGrid stores the configuration of a grid that consists of entityConfigs and viewConfigs
Daniel@0 15 * (both are Backbone.Collection of Config)
Daniel@0 16 *
Daniel@0 17 * In real situation entityConfigs are music collection configs and music recording configs
Daniel@0 18 *
Daniel@0 19 * two sub-collections can be interacted directly (without proxy methods)
Daniel@0 20 *
Daniel@0 21 * The grid can be given a read-only type on creation in order to easily to distinguish between collection and recording grid later
Daniel@0 22 * getType() method is available for this purpose. The type of the grid is not being serialized or unserialized
Daniel@0 23 *
Daniel@0 24 * The following events are triggered:
Daniel@0 25 *
Daniel@0 26 * change_layout
Daniel@0 27 * when both or any of the two collections of Config gets new objects, looses objects or shuffles
Daniel@0 28 * (but not when parameters in individual parameter bags change)
Daniel@0 29 *
Daniel@0 30 * change_entity:c123
Daniel@0 31 * change_view:c123
Daniel@0 32 * when a particular parameter bag changes (c123 is replaced with a corresponding client id of an entity or a view)
Daniel@0 33 *
Daniel@0 34 * change_entity_neighbours:c123
Daniel@0 35 * change_view_neighbours:c123
Daniel@0 36 * when a Config, which is right before or right after the given parameter bag, changes
Daniel@0 37 * This includes cases when neighbours are added or removed
Daniel@0 38 *
Daniel@0 39 * change_selection
Daniel@0 40 * when selectedEntityConfigClientId or (and) selectedViewConfigClientId change
Daniel@0 41 *
Daniel@0 42 * change
Daniel@0 43 * this event is triggered together with any of the above ones, but
Daniel@0 44 * maximum once during a complex operation such as unserialize
Daniel@0 45 */
Daniel@0 46 ContextModule.ConfigGrid = Backbone.Model.extend({
Daniel@0 47 defaults: {
Daniel@0 48 entityConfigs: null,
Daniel@0 49 viewConfigs: null,
Daniel@0 50 selectedEntityConfigClientId: null,
Daniel@0 51 selectedViewConfigClientId: null
Daniel@0 52 },
Daniel@0 53
Daniel@0 54 /**
Daniel@0 55 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 56 */
Daniel@0 57 initialize: function(type) {
Daniel@0 58
Daniel@0 59 this.type = type;
Daniel@0 60
Daniel@0 61 this.attributes.entityConfigs = new ContextModule.ConfigCollection(null, {
Daniel@0 62 comparator: false,
Daniel@0 63 configGridType: type,
Daniel@0 64 dimension: "entity",
Daniel@0 65 });
Daniel@0 66 this.attributes.viewConfigs = new ContextModule.ConfigCollection(null, {
Daniel@0 67 comparator: false,
Daniel@0 68 configGridType: type,
Daniel@0 69 dimension: "view",
Daniel@0 70 });
Daniel@0 71
Daniel@0 72 // Shortcuts for entity configs and view configs (for quicker access)
Daniel@0 73 this.entityConfigs = this.attributes.entityConfigs;
Daniel@0 74 this.viewConfigs = this.attributes.viewConfigs;
Daniel@0 75
Daniel@0 76 this._modificationPropagationEnabled = true;
Daniel@0 77 this._configCollectionsWereModified = false;
Daniel@0 78 this._configsWereModified = false;
Daniel@0 79 this._modifiedEntityConfigClientIds = [];
Daniel@0 80 this._modifiedViewConfigClientIds = [];
Daniel@0 81 this._lastSavedOrderedEntityClientIds = _.pluck(this.attributes.entityConfigs.models, "cid");
Daniel@0 82 this._lastSavedOrderedViewClientIds = _.pluck(this.attributes.viewConfigs.models, "cid");
Daniel@0 83 this._lastSavedSelectedEntityConfigClientId = this.attributes.selectedEntityConfigClientId;
Daniel@0 84 this._lastSavedSelectedViewConfigClientId = this.attributes.selectedViewConfigClientId;
Daniel@0 85
Daniel@0 86 this.entityConfigs.bind("add remove reset sort", this._registerModificationOfConfigCollectionForEntities, this);
Daniel@0 87 this.viewConfigs.bind("add remove reset sort", this._registerModificationOfConfigCollectionForViews, this);
Daniel@0 88
Daniel@0 89 this.entityConfigs.bind("change", this._registerModificationOfConfig, this);
Daniel@0 90 this.viewConfigs.bind("change", this._registerModificationOfConfig, this);
Daniel@0 91
Daniel@0 92 this.bind("change:selectedEntityConfigClientId", this._registerModificationOfAtomicProperty);
Daniel@0 93 this.bind("change:selectedViewConfigClientId", this._registerModificationOfAtomicProperty);
Daniel@0 94 this.bind("change:entityWidth", this._registerModificationOfStandardAtomicProperty);
Daniel@0 95 },
Daniel@0 96
Daniel@0 97 /**
Daniel@0 98 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 99 */
Daniel@0 100 getType: function() {
Daniel@0 101 return this.type;
Daniel@0 102 },
Daniel@0 103
Daniel@0 104 /**
Daniel@0 105 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 106 */
Daniel@0 107 getPrevEntityNeighbour: function(entityConfig) {
Daniel@0 108 return this._getNeighbour(this.attributes.entityConfigs, entityConfig, -1);
Daniel@0 109 },
Daniel@0 110
Daniel@0 111 /**
Daniel@0 112 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 113 */
Daniel@0 114 getNextEntityNeighbour: function(entityConfig) {
Daniel@0 115 return this._getNeighbour(this.attributes.entityConfigs, entityConfig, 1);
Daniel@0 116 },
Daniel@0 117
Daniel@0 118 /**
Daniel@0 119 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 120 */
Daniel@0 121 getPrevViewNeighbour: function(viewConfig) {
Daniel@0 122 return this._getNeighbour(this.attributes.viewConfigs, viewConfig, -1);
Daniel@0 123 },
Daniel@0 124
Daniel@0 125 /**
Daniel@0 126 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 127 */
Daniel@0 128 getNextViewNeighbour: function(viewConfig) {
Daniel@0 129 return this._getNeighbour(this.attributes.viewConfigs, viewConfig, 1);
Daniel@0 130 },
Daniel@0 131
Daniel@0 132 /**
Daniel@0 133 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 134 */
Daniel@0 135 relocateEntityConfig: function(entityConfig, indexOrNextConfigOrNextConfigClientId) {
Daniel@0 136 return this._relocate(this.attributes.entityConfigs, entityConfig, indexOrNextConfigOrNextConfigClientId);
Daniel@0 137 },
Daniel@0 138
Daniel@0 139 /**
Daniel@0 140 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 141 */
Daniel@0 142 relocateViewConfig: function(viewConfig, indexOrNextConfigOrNextConfigClientId) {
Daniel@0 143 return this._relocate(this.attributes.viewConfigs, viewConfig, indexOrNextConfigOrNextConfigClientId);
Daniel@0 144 },
Daniel@0 145
Daniel@0 146 /**
Daniel@0 147 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 148 */
Daniel@0 149 serialize: function() {
Daniel@0 150 logger.debug("method called: ConfigGrid::serialize");
Daniel@0 151 var _this = this;
Daniel@0 152
Daniel@0 153 var result = {
Daniel@0 154 entityConfigs: this.attributes.entityConfigs.map(function(config){ return config.serialize(); }),
Daniel@0 155 viewConfigs: this.attributes.viewConfigs.map(function(config){ return config.serialize(); })
Daniel@0 156 };
Daniel@0 157
Daniel@0 158 if (this.attributes.selectedEntityConfigClientId) {
Daniel@0 159 result.selectedEntityConfigClientId = this.attributes.selectedEntityConfigClientId;
Daniel@0 160 }
Daniel@0 161 if (this.attributes.selectedViewConfigClientId) {
Daniel@0 162 result.selectedViewConfigClientId = this.attributes.selectedViewConfigClientId;
Daniel@0 163 }
Daniel@0 164 if (this.attributes.entityWidth) {
Daniel@0 165 result.entityWidth = this.attributes.entityWidth;
Daniel@0 166 }
Daniel@0 167
Daniel@0 168 return result;
Daniel@0 169 },
Daniel@0 170
Daniel@0 171 getSelectedEntityConfig: function() {
Daniel@0 172 return this.attributes.entityConfigs.get(this.attributes.selectedEntityConfigClientId);
Daniel@0 173 },
Daniel@0 174
Daniel@0 175 getSelectedViewConfig: function() {
Daniel@0 176 return this.attributes.viewConfigs.get(this.attributes.selectedViewConfigClientId);
Daniel@0 177 },
Daniel@0 178
Daniel@0 179 // getSelectedConfigAtGivenDimension: function(dimension) {
Daniel@0 180 // if (dimension == "entity") {
Daniel@0 181 // return this.getSelectedEntityConfig();
Daniel@0 182 // } else if (dimension == "view") {
Daniel@0 183 // return this.getSelectedViewConfig();
Daniel@0 184 // }
Daniel@0 185 // },
Daniel@0 186 //
Daniel@0 187 addEntityAndSelectIt: function(entityConfig, indexOrNextConfigOrNextConfigClientId) {
Daniel@0 188 this._addAndSelect(this.attributes.entityConfigs, entityConfig, _.isUndefined(indexOrNextConfigOrNextConfigClientId) ? null : indexOrNextConfigOrNextConfigClientId);
Daniel@0 189 },
Daniel@0 190
Daniel@0 191 addViewAndSelectIt: function(viewConfig, indexOrNextConfigOrNextConfigClientId) {
Daniel@0 192 this._addAndSelect(this.attributes.viewConfigs, viewConfig, _.isUndefined(indexOrNextConfigOrNextConfigClientId) ? null : indexOrNextConfigOrNextConfigClientId);
Daniel@0 193 },
Daniel@0 194
Daniel@0 195 removeEntityAndSelectNeighbour: function(entityConfig) {
Daniel@0 196 this._removeAndSelectNeighbour(this.attributes.entityConfigs, entityConfig);
Daniel@0 197 },
Daniel@0 198
Daniel@0 199 removeViewAndSelectNeighbour: function(viewConfig) {
Daniel@0 200 this._removeAndSelectNeighbour(this.attributes.viewConfigs, viewConfig);
Daniel@0 201 },
Daniel@0 202
Daniel@0 203 _addAndSelect: function(configCollection, config, indexOrNextConfigOrNextConfigClientId) {
Daniel@0 204 this._modificationPropagationEnabled = false;
Daniel@0 205
Daniel@0 206 configCollection.add(config);
Daniel@0 207 if (configCollection == this.attributes.entityConfigs) {
Daniel@0 208 this.attributes.selectedEntityConfigClientId = config.getClientId();
Daniel@0 209 } else {
Daniel@0 210 this.attributes.selectedViewConfigClientId = config.getClientId();
Daniel@0 211 }
Daniel@0 212 this._relocate(configCollection, config, indexOrNextConfigOrNextConfigClientId);
Daniel@0 213
Daniel@0 214 this._triggerModificationEventsIfNeeded();
Daniel@0 215 this._modificationPropagationEnabled = true;
Daniel@0 216
Daniel@0 217 },
Daniel@0 218
Daniel@0 219 _removeAndSelectNeighbour: function(configCollection, config) {
Daniel@0 220 this._modificationPropagationEnabled = false;
Daniel@0 221
Daniel@0 222 var neighbourToSelect = this._getNeighbour(configCollection, config, 1);
Daniel@0 223 if (!neighbourToSelect) {
Daniel@0 224 neighbourToSelect = this._getNeighbour(configCollection, config, -1);
Daniel@0 225 }
Daniel@0 226 configCollection.remove(config);
Daniel@0 227 if (configCollection == this.attributes.entityConfigs) {
Daniel@0 228 this.attributes.selectedEntityConfigClientId = neighbourToSelect ? neighbourToSelect.getClientId() : null;
Daniel@0 229 } else {
Daniel@0 230 this.attributes.selectedViewConfigClientId = neighbourToSelect ? neighbourToSelect.getClientId() : null;
Daniel@0 231 }
Daniel@0 232
Daniel@0 233 this._triggerModificationEventsIfNeeded();
Daniel@0 234 this._modificationPropagationEnabled = true;
Daniel@0 235
Daniel@0 236 },
Daniel@0 237
Daniel@0 238 /**
Daniel@0 239 * @memberOf App.ContextModule.ConfigGrid
Daniel@0 240 */
Daniel@0 241 unserialize: function(serializedAttributes) {
Daniel@0 242 logger.debug("method called: ConfigGrid::unserialize");
Daniel@0 243
Daniel@0 244 this._modificationPropagationEnabled = false;
Daniel@0 245
Daniel@0 246 var fixedSerializedAttributes = serializedAttributes;
Daniel@0 247 if (!_.isSimpleObject(serializedAttributes)) {
Daniel@0 248 logger.warn("ConfigGrid::unserialize called for not an object: ", serializedAttributes);
Daniel@0 249 fixedSerializedAttributes = {};
Daniel@0 250 }
Daniel@0 251
Daniel@0 252 // entityConfigs
Daniel@0 253 var newConfigs = [];
Daniel@0 254 var fixedSerializedConfigs = fixedSerializedAttributes.entityConfigs;
Daniel@0 255 if (!_.isArray(fixedSerializedConfigs)) {
Daniel@0 256 if (_.isSimpleObject(serializedAttributes)) {
Daniel@0 257 logger.warn("ConfigGrid::unserialize called for an object with faulty entityConfigs: ", fixedSerializedConfigs);
Daniel@0 258 }
Daniel@0 259 fixedSerializedConfigs = [];
Daniel@0 260 };
Daniel@0 261 for (var i = 0; i < fixedSerializedConfigs.length; i++) {
Daniel@0 262 var serializedConfig = fixedSerializedConfigs[i];
Daniel@0 263 var config = this.attributes.entityConfigs.get(serializedConfig.clientId);
Daniel@0 264 if (!config) {
Daniel@0 265 config = new App.ContextModule.Config(serializedConfig);
Daniel@0 266 } else {
Daniel@0 267 config.unserialize(serializedConfig);
Daniel@0 268 }
Daniel@0 269 newConfigs.push(config);
Daniel@0 270 }
Daniel@0 271 this.attributes.entityConfigs.reset(newConfigs);
Daniel@0 272
Daniel@0 273 // viewConfigs
Daniel@0 274 var newConfigs = [];
Daniel@0 275 var fixedSerializedConfigs = fixedSerializedAttributes.viewConfigs;
Daniel@0 276 if (!_.isArray(fixedSerializedConfigs)) {
Daniel@0 277 if (_.isSimpleObject(serializedAttributes)) {
Daniel@0 278 logger.warn("ConfigGrid::unserialize called for an object with faulty viewConfigs: ", fixedSerializedConfigs);
Daniel@0 279 }
Daniel@0 280 fixedSerializedConfigs = [];
Daniel@0 281 };
Daniel@0 282 for (var i = 0; i < fixedSerializedConfigs.length; i++) {
Daniel@0 283 var serializedConfig = fixedSerializedConfigs[i];
Daniel@0 284 var config = this.attributes.viewConfigs.get(serializedConfig.clientId);
Daniel@0 285 if (!config) {
Daniel@0 286 config = new App.ContextModule.Config(serializedConfig);
Daniel@0 287 } else {
Daniel@0 288 config.unserialize(serializedConfig);
Daniel@0 289 }
Daniel@0 290 newConfigs.push(config);
Daniel@0 291 }
Daniel@0 292 this.attributes.viewConfigs.reset(newConfigs);
Daniel@0 293
Daniel@0 294 // selectedEntityConfigClientId, selectedViewConfigClientId
Daniel@0 295 this.attributes.selectedEntityConfigClientId = fixedSerializedAttributes.selectedEntityConfigClientId;
Daniel@0 296 this.attributes.selectedViewConfigClientId = fixedSerializedAttributes.selectedViewConfigClientId;
Daniel@0 297
Daniel@0 298 this.attributes.entityWidth = fixedSerializedAttributes.entityWidth;
Daniel@0 299
Daniel@0 300 this._triggerModificationEventsIfNeeded();
Daniel@0 301 this._modificationPropagationEnabled = true;
Daniel@0 302 },
Daniel@0 303
Daniel@0 304 _getNeighbour: function(configCollection, config, offset) {
Daniel@0 305 var index = configCollection.indexOf(config);
Daniel@0 306 if (index === -1) {
Daniel@0 307 throw _.str.sprintf("Can't find config %s", JSON.stringify(config.serialize()));
Daniel@0 308 }
Daniel@0 309 return configCollection.at(index + offset);
Daniel@0 310 },
Daniel@0 311
Daniel@0 312 _relocate: function(configCollection, config, indexOrNextConfigOrNextConfigClientId) {
Daniel@0 313 var clientIds = _.pluck(configCollection.models, "cid");
Daniel@0 314 var nextConfigClientId = null;
Daniel@0 315
Daniel@0 316 if (_.isNumber(indexOrNextConfigOrNextConfigClientId) && indexOrNextConfigOrNextConfigClientId != clientIds.length) {
Daniel@0 317 nextConfigClientId = clientIds[indexOrNextConfigOrNextConfigClientId];
Daniel@0 318 }
Daniel@0 319 if (_.isObject(indexOrNextConfigOrNextConfigClientId)) {
Daniel@0 320 nextConfigClientId = indexOrNextConfigOrNextConfigClientId.getClientId();
Daniel@0 321 }
Daniel@0 322 if (_.isString(indexOrNextConfigOrNextConfigClientId)) {
Daniel@0 323 if (clientIds.indexOf(indexOrNextConfigOrNextConfigClientId) !== -1) {
Daniel@0 324 nextConfigClientId = indexOrNextConfigOrNextConfigClientId;
Daniel@0 325 }
Daniel@0 326 }
Daniel@0 327 if (!nextConfigClientId && !_.isNull(indexOrNextConfigOrNextConfigClientId) && indexOrNextConfigOrNextConfigClientId != configCollection.size()) {
Daniel@0 328 throw _.str.sprintf("Wrong value for indexOrNextConfigOrNextConfigClientId %s", indexOrNextConfigOrNextConfigClientId);
Daniel@0 329 }
Daniel@0 330
Daniel@0 331 var configClientId = config.getClientId();
Daniel@0 332 if (!configClientId || clientIds.indexOf(configClientId) == -1) {
Daniel@0 333 var flattenedConfig = config;
Daniel@0 334 if (_.isObject(flattenedConfig)) {
Daniel@0 335 flattenedConfig = JSON.stringify(flattenedConfig);
Daniel@0 336 }
Daniel@0 337 throw _.str.sprintf("Config %s with cid %s is either not a Config or does not belong to a corresponding configCollection with cids [%s]", flattenedConfig, _.isObject(config ) ? config.getClientId() : undefined, clientIds.join(", "));
Daniel@0 338 }
Daniel@0 339 if (configCollection.get(configClientId) !== config) {
Daniel@0 340 throw _.str.sprintf("Config %s with cid %s is is a clone of what is stored in the grid. Relocation is not possible.", JSON.stringify(config), config.cid);
Daniel@0 341 }
Daniel@0 342 var newClientIds = _.without(clientIds, configClientId);
Daniel@0 343 if (_.isNull(nextConfigClientId)) {
Daniel@0 344 newClientIds.push(configClientId);
Daniel@0 345 } else {
Daniel@0 346 var nextConfigIndex = newClientIds.indexOf(nextConfigClientId);
Daniel@0 347 //if (nextConfigIndex)
Daniel@0 348 var tempClientIds = newClientIds.slice(0, nextConfigIndex);
Daniel@0 349 tempClientIds.push(configClientId);
Daniel@0 350 newClientIds = tempClientIds.concat(newClientIds.slice(nextConfigIndex));
Daniel@0 351 }
Daniel@0 352
Daniel@0 353 var oldComparator = configCollection.comparator;
Daniel@0 354 configCollection.comparator = function(model) {
Daniel@0 355 return newClientIds.indexOf(model.getClientId());
Daniel@0 356 };
Daniel@0 357 configCollection.sort();
Daniel@0 358 configCollection.comparator = oldComparator;
Daniel@0 359 },
Daniel@0 360
Daniel@0 361 _registerModificationOfConfigCollectionForEntities: function(modelOrModels, options) {
Daniel@0 362 if (!_.isEqual(this._lastSavedOrderedEntityClientIds, _.pluck(this.attributes.entityConfigs.models, "cid"))) {
Daniel@0 363 this._configCollectionsWereModified = true;
Daniel@0 364 if (this._modificationPropagationEnabled) {
Daniel@0 365 this._triggerModificationEventsIfNeeded();
Daniel@0 366 };
Daniel@0 367 }
Daniel@0 368 },
Daniel@0 369
Daniel@0 370 _registerModificationOfConfigCollectionForViews: function(modelOrModels, options) {
Daniel@0 371 if (!_.isEqual(this._lastSavedOrderedViewClientIds, _.pluck(this.attributes.viewConfigs.models, "cid"))) {
Daniel@0 372 this._configCollectionsWereModified = true;
Daniel@0 373 if (this._modificationPropagationEnabled) {
Daniel@0 374 this._triggerModificationEventsIfNeeded();
Daniel@0 375 };
Daniel@0 376 }
Daniel@0 377 },
Daniel@0 378
Daniel@0 379 _registerModificationOfConfig: function() {
Daniel@0 380 for (var i = 0; i < this.attributes.entityConfigs.length; i++) {
Daniel@0 381 if(this.attributes.entityConfigs.at(i).hasChanged()) {
Daniel@0 382 this._configsWereModified = true;
Daniel@0 383 this._modifiedEntityConfigClientIds.push(this.attributes.entityConfigs.at(i).getClientId());
Daniel@0 384 }
Daniel@0 385 }
Daniel@0 386 for (var i = 0; i < this.attributes.viewConfigs.length; i++) {
Daniel@0 387 if(this.attributes.viewConfigs.at(i).hasChanged()) {
Daniel@0 388 this._configsWereModified = true;
Daniel@0 389 this._modifiedViewConfigClientIds.push(this.attributes.viewConfigs.at(i).getClientId());
Daniel@0 390 }
Daniel@0 391 }
Daniel@0 392 if (this._modificationPropagationEnabled) {
Daniel@0 393 this._triggerModificationEventsIfNeeded();
Daniel@0 394 };
Daniel@0 395 },
Daniel@0 396
Daniel@0 397 _registerModificationOfAtomicProperty: function() {
Daniel@0 398 if (this._modificationPropagationEnabled) {
Daniel@0 399 this._triggerModificationEventsIfNeeded(true);
Daniel@0 400 };
Daniel@0 401 },
Daniel@0 402
Daniel@0 403 _registerModificationOfStandardAtomicProperty: function() {
Daniel@0 404 if (this._modificationPropagationEnabled) {
Daniel@0 405 this._triggerModificationEventsIfNeeded();
Daniel@0 406 };
Daniel@0 407 },
Daniel@0 408
Daniel@0 409 _triggerModificationEventsIfNeeded: function(specialCaseForRegisterModificationOfSelection) {
Daniel@0 410
Daniel@0 411 var triggeredAtLeastSomething = false;
Daniel@0 412 if (this._configCollectionsWereModified) {
Daniel@0 413 triggeredAtLeastSomething = true;
Daniel@0 414 this.trigger("change_layout");
Daniel@0 415 }
Daniel@0 416
Daniel@0 417 var newOrderedEntityClientIds = null;
Daniel@0 418 var newOrderedViewClientIds = null;
Daniel@0 419 if (this._configCollectionsWereModified || this._modifiedEntityConfigClientIds.length || this._modifiedViewConfigClientIds.length) {
Daniel@0 420 newOrderedEntityClientIds = _.pluck(this.attributes.entityConfigs.models, "cid");
Daniel@0 421 newOrderedViewClientIds = _.pluck(this.attributes.viewConfigs.models, "cid");
Daniel@0 422
Daniel@0 423 // change_entity:c123
Daniel@0 424 if (this._modifiedEntityConfigClientIds.length) {
Daniel@0 425 for (var i = 0; i < newOrderedEntityClientIds.length; i++) {
Daniel@0 426 if (_.indexOf(this._modifiedEntityConfigClientIds, newOrderedEntityClientIds[i]) !== -1) {
Daniel@0 427 triggeredAtLeastSomething = true;
Daniel@0 428 this.trigger("change_entity:" + newOrderedEntityClientIds[i]);
Daniel@0 429 }
Daniel@0 430 }
Daniel@0 431 }
Daniel@0 432 // change_view:c123
Daniel@0 433 if (this._modifiedViewConfigClientIds.length) {
Daniel@0 434 for (var i = 0; i < newOrderedViewClientIds.length; i++) {
Daniel@0 435 if (_.indexOf(this._modifiedViewConfigClientIds, newOrderedViewClientIds[i]) !== -1) {
Daniel@0 436 triggeredAtLeastSomething = true;
Daniel@0 437 this.trigger("change_view:" + newOrderedViewClientIds[i]);
Daniel@0 438 }
Daniel@0 439 }
Daniel@0 440 }
Daniel@0 441
Daniel@0 442 // change_entity_neighbours:c123
Daniel@0 443 for (var i = 0; i < newOrderedEntityClientIds.length; i++) {
Daniel@0 444 var entityClientId = newOrderedEntityClientIds[i];
Daniel@0 445 var oldEntityIndex = this._lastSavedOrderedEntityClientIds.indexOf(entityClientId);
Daniel@0 446 if (oldEntityIndex == -1) {
Daniel@0 447 continue;
Daniel@0 448 }
Daniel@0 449 var newPrevEntityClientId = newOrderedEntityClientIds[i - 1];
Daniel@0 450 var newNextEntityClientId = newOrderedEntityClientIds[i + 1];
Daniel@0 451 var oldPrevEntityClientId = this._lastSavedOrderedEntityClientIds[oldEntityIndex - 1];
Daniel@0 452 var oldNextEntityClientId = this._lastSavedOrderedEntityClientIds[oldEntityIndex + 1];
Daniel@0 453
Daniel@0 454 if (newPrevEntityClientId != oldPrevEntityClientId
Daniel@0 455 || newNextEntityClientId != oldNextEntityClientId
Daniel@0 456 || this._modifiedEntityConfigClientIds.indexOf(newPrevEntityClientId) !== -1
Daniel@0 457 || this._modifiedEntityConfigClientIds.indexOf(newNextEntityClientId) !== -1
Daniel@0 458 ) {
Daniel@0 459 triggeredAtLeastSomething = true;
Daniel@0 460 this.trigger("change_entity_neighbours:" + entityClientId);
Daniel@0 461 };
Daniel@0 462 }
Daniel@0 463
Daniel@0 464 // change_view_neighbours:c123
Daniel@0 465 for (var i = 0; i < newOrderedViewClientIds.length; i++) {
Daniel@0 466 var viewClientId = newOrderedViewClientIds[i];
Daniel@0 467 var oldViewIndex = this._lastSavedOrderedViewClientIds.indexOf(viewClientId);
Daniel@0 468 if (oldViewIndex == -1) {
Daniel@0 469 continue;
Daniel@0 470 }
Daniel@0 471 var newPrevViewClientId = newOrderedViewClientIds[i - 1];
Daniel@0 472 var newNextViewClientId = newOrderedViewClientIds[i + 1];
Daniel@0 473 var oldPrevViewClientId = this._lastSavedOrderedViewClientIds[oldViewIndex - 1];
Daniel@0 474 var oldNextViewClientId = this._lastSavedOrderedViewClientIds[oldViewIndex + 1];
Daniel@0 475
Daniel@0 476 if (newPrevViewClientId != oldPrevViewClientId
Daniel@0 477 || newNextViewClientId != oldNextViewClientId
Daniel@0 478 || this._modifiedViewConfigClientIds.indexOf(newPrevViewClientId) !== -1
Daniel@0 479 || this._modifiedViewConfigClientIds.indexOf(newNextViewClientId) !== -1
Daniel@0 480 ) {
Daniel@0 481 triggeredAtLeastSomething = true;
Daniel@0 482 this.trigger("change_view_neighbours:" + viewClientId);
Daniel@0 483 };
Daniel@0 484 };
Daniel@0 485 }
Daniel@0 486
Daniel@0 487 this._lastSavedOrderedEntityClientIds = _.pluck(this.attributes.entityConfigs.models, "cid");
Daniel@0 488 this._lastSavedOrderedViewClientIds = _.pluck(this.attributes.viewConfigs.models, "cid");
Daniel@0 489
Daniel@0 490 // Fix selection
Daniel@0 491 if (!this.attributes.entityConfigs.get(this.attributes.selectedEntityConfigClientId)) {
Daniel@0 492 this.attributes.selectedEntityConfigClientId = null;
Daniel@0 493 }
Daniel@0 494 if (!this.attributes.viewConfigs.get(this.attributes.selectedViewConfigClientId)) {
Daniel@0 495 this.attributes.selectedViewConfigClientId = null;
Daniel@0 496 }
Daniel@0 497 if (this._lastSavedSelectedEntityConfigClientId != this.attributes.selectedEntityConfigClientId
Daniel@0 498 || this._lastSavedSelectedViewConfigClientId != this.attributes.selectedViewConfigClientId
Daniel@0 499 ) {
Daniel@0 500 if (!specialCaseForRegisterModificationOfSelection) {
Daniel@0 501 triggeredAtLeastSomething = true;
Daniel@0 502 }
Daniel@0 503 this.trigger("change_selection");
Daniel@0 504 }
Daniel@0 505
Daniel@0 506 if (this._lastSavedEntityWidth != this.attributes.entityWidth) {
Daniel@0 507 triggeredAtLeastSomething = true;
Daniel@0 508 }
Daniel@0 509
Daniel@0 510 this._configCollectionsWereModified = false;
Daniel@0 511 this._configsWereModified = false;
Daniel@0 512 this._selectionWasModified = false;
Daniel@0 513 this._modifiedEntityConfigClientIds = [];
Daniel@0 514 this._modifiedViewConfigClientIds = [];
Daniel@0 515 this._lastSavedSelectedEntityConfigClientId = this.attributes.selectedEntityConfigClientId;
Daniel@0 516 this._lastSavedSelectedViewConfigClientId = this.attributes.selectedViewConfigClientId;
Daniel@0 517 this._lastSavedEntityWidth = this.attributes.entityWidth;
Daniel@0 518
Daniel@0 519 if (triggeredAtLeastSomething) {
Daniel@0 520 this.trigger("change");
Daniel@0 521 }
Daniel@0 522 },
Daniel@0 523 });
Daniel@0 524 });
Daniel@0 525 }, Logger);