/
document_helper.js
692 lines (639 loc) · 26 KB
/
document_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// ==========================================================================
// DG.DocumentHelper
//
// Author: Kirk Swenson
//
// Copyright (c) 2014 by The Concord Consortium, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ==========================================================================
///* global Promise */
/**
* @class DocumentHelper
*
* Coordinates streaming of documents and document elements. Deals with documents
* as streamable entities in contrast to the Document Controller that
* provides operational coordination of document elements. This class addresses
* how to get documents in and out of CODAP predictably and the Document
* Controller deals with Documents while they are being presented and
* interacted with within CODAP.
*
* This class, at this point, has no state. It
* functions, more or less, as a mixin to the App Controller, providing
* a logically related collection of utility methods.
*
* @extends SC.Object
*/
DG.DocumentHelper = SC.Object.extend(
/** @scope DG.DocumentHelper.prototype */ {
/**
* Tests the JSON text for validity as a possible document.
*
* The following assertions are tested:
*
* (1) The document is valid JSON. that is: it parses correctly
* (2) The document looks like a valid CODAP document. It has all mandatory
* top level elements and no unexpected top level elements.
* (3) all internal links can be resolved.
*
* @param {String} iDocument -- The JSON-formatted document text
* @returns {[String]} An array of error messages, zero length, if none.
*/
isValidJsonDocument: function (iDocument) {
function visit(key, value, fn) {
var rtn = fn(key, value);
if (rtn !== false) {
if (Array.isArray(value)) {
value.forEach(function (item) {
visit(key, item, fn);
});
} else if (typeof value === 'object') {
DG.ObjectMap.forEach(value, function (key, item) {
visit(key, item, fn);
});
}
}
}
function validateInternalRefs(iDocument) {
var parts = subDocs.copy(false);
var symbols = [];
var references = [];
parts.unshift(iDocument);
parts.forEach(function (documentPart) {
visit('doc', documentPart, function (key, value) {
if (key === 'guid') {
// store guids in symbol table
symbols.push(Number(value));
return true;
} else if (key === '_links_') {
// store links as references
visit(key, value, function (k, v) {
if (k === 'id') {
references.push(Number(v));
}
return true;
});
return false;
} else if (key === 'parent') {
// store parents as references
references.push(Number(value));
return true;
} else if (key === 'contextStorage') {
// context storage is private to data interactive
return false;
}
});
});
references.forEach(function (ref) {
// verify link is resolved
if (symbols.indexOf(Number(ref)) < 0) {
errors.push('DG.AppController.validateDocument.unresolvedID'.loc(ref));
}
});
}
var expectedProperties = [
'appBuildNum',
'appName',
'appVersion',
'changeCount',
'components',
'contexts',
'globalValues',
'guid',
'metadata',
'name',
'_permissions',
'_openedFromSharedDocument' // this is an annotation we may create in
// CodapCommonStorage
];
var requiredProperties = [
'name'
];
var errors = [];
var doc;
var subDocs = [];
if (typeof iDocument === 'string') {
try {
doc = JSON.parse(iDocument);
} catch (ex) {
errors.push('DG.AppController.validateDocument.parseError'.loc(ex));
}
} else {
doc = iDocument;
}
if (doc) {
requiredProperties.forEach(function (prop) {
if (!doc.hasOwnProperty(prop)) {
errors.push('DG.AppController.validateDocument.missingRequiredProperty'.loc(prop));
}
}
);
DG.ObjectMap.keys(doc).forEach(function (prop) {
if (expectedProperties.indexOf(prop) < 0) {
// log unexpected properties but don't fail to open
DG.log('DG.AppController.validateDocument.unexpectedProperty'.loc(prop));
}
}
);
validateInternalRefs(doc);
}
DG.log('Document validation: ' + (errors.length ? JSON.stringify(errors) : 'No Errors'));
return errors;
},
/**
* We create a very simple document as a wrapper on the provided Data
* Interactive URL and open it.
*
* @param iURL - URL of data interactive
* @returns {DG.Document}
*/
createNewDocumentWithDataInteractives: function (iURLs) {
var tComponents = (iURLs || [])
.map(function (url) {
return {
"type": "DG.GameView",
"componentStorage": {
"currentGameName": "",
"currentGameUrl": url
}
};
});
var tDoc = {
name: 'DG.Document.defaultDocumentName'.loc(),
guid: 1,
isNewDocument: true, // mark this document as not having been saved and restored.
// this property will not be persisted with the document.
components: tComponents,
appName: DG.APPNAME,
appVersion: DG.VERSION,
appBuildNum: DG.BUILD_NUM,
globalValues: []
};
return DG.Document.createDocument(tDoc);
},
updateDataContext: function (iResources, iValues) {
var context = iResources.dataContext;
if (context) {
['managingController', 'title', 'description', 'preventReorg', 'metadata', 'rerandomize', 'sort']
.forEach(function (prop) {
var existingMetadata;
if (!SC.none(iValues[prop])) {
if (prop === 'metadata') {
if (typeof iValues[prop] === 'object') {
existingMetadata = context.getPath('model.metadata') || {};
context.setPath('model.metadata', Object.assign(existingMetadata, iValues[prop]));
}
} else if (prop === 'rerandomize' && !!iValues[prop]) {
context.doRerandomizeAll();
} else if (prop === 'sort') {
var controllersMap = DG.currDocumentController().get('componentControllersMap');
var tableAdapters = [];
for (var key in controllersMap) {
if (controllersMap[key].hasOwnProperty('caseTableAdapters')) {
tableAdapters.push(controllersMap[key]);
break;
}
}
tableAdapters.forEach(function (tableController) {
var tableContext = tableController.get('dataContext');
var attribute = typeof(iValues[prop].attr) === "string"
? context.getAttributeByName(iValues[prop].attr).id
: iValues[prop].attr;
if (tableContext.get('id') === context.get('id')) {
var isDescending = iValues[prop].isDescending;
tableController.sortAttribute(attribute, isDescending);
}
});
}
else {
context.set(prop, iValues[prop]);
}
}
});
}
return {
success: true
};
},
deleteCollection: function( iResources, iRequesterID) {
var context = iResources.dataContext;
var collection = iResources.collection;
if (!collection) {
return {success: false, values: {error: 'Collection not found'}};
}
return context.applyChange({
operation: 'deleteCollection',
collection: collection,
requester: iRequesterID
});
},
createCollection: function(iResources, iValues, iID) {
// returns a collection for the appropriate parent name, if any.
function mapParent (context, parentName) {
var parentKey;
var collections;
var collection;
if (SC.none(parentName)) {
collections = context.get('collections');
if (collections && collections.length > 0) {
parentKey = collections[collections.length - 1].get('id');
}
} else if (typeof parentName === 'number') {
parentKey = parentName;
} else if (parentName === '_root_') {
parentKey = null;
} else {
collection = context.getCollectionByName(parentName);
parentKey = collection ? collection.get('id') : null;
}
return (!SC.none(parentKey))? context.getCollectionByID(parentKey).collection: null;
}
// returns a success indicator and ids.
function createOneCollection(iContext, iCollectionSpec, iContextID) {
var change = {
operation: 'createCollection',
properties: iCollectionSpec,
attributes: ( iCollectionSpec && iCollectionSpec.attributes ),
requester: iContextID
};
iCollectionSpec.parent = mapParent(iContext, iCollectionSpec.parent);
var changeResult = iContext.applyChange(change);
var success = (changeResult && changeResult.success);
var ids = changeResult.collection && {
id: changeResult.collection.get('id'),
name: changeResult.collection.get('name')
};
return {
success: success,
values: ids
};
}
var context = iResources.dataContext;
var success = true;
var collectionIdentifiers = [];
if (!context) {
return {success: false, values: {error: "no context"}};
}
if (!Array.isArray(iValues)) {
iValues = [iValues];
}
iValues.every(function (iCollectionSpec) {
var rslt = createOneCollection(context, iCollectionSpec, iID);
success = success && rslt.success;
if (success) {
collectionIdentifiers.push(rslt.values);
}
return success;
});
return {success: success, values: collectionIdentifiers};
},
applyChangeAndProcessResult: function(context, change, metadata) {
if (metadata && metadata.dirtyDocument === false)
change.dirtyDocument = false;
var changeResult = context.applyChange(change),
resultAttrs = changeResult && changeResult.attrs,
returnAttrs = DG.DataInteractiveUtils.mapAttributeProperties(resultAttrs, change.attrPropsArray),
returnResult = {success: changeResult && changeResult.success};
if (returnAttrs)
returnResult.values = { attrs: returnAttrs };
return returnResult;
},
/**
*
* @param iResources
* @param iValues
* @param iMetadata
*/
createAttribute: function (iResources, iValues, iMetadata, iRequesterID) {
if (!iResources.dataContext) {
return {success: false, values: {error: "no context"}};
}
if (!iResources.collection) {
return {success: false, values: {error: 'Collection not found'}};
}
var context = iResources.dataContext;
var attrSpecs = SC.clone(Array.isArray(iValues) ? iValues : [iValues]);
if (attrSpecs.some(function(spec) { return !spec.name; })) {
return {success: false, values: {error: "Create attribute: name required"}};
}
attrSpecs.forEach(function(attrSpec) {
attrSpec.clientName = attrSpec.name;
attrSpec.name = context.canonicalizeName(attrSpec.name + ' ');
});
var change = {
operation: 'createAttributes',
collection: context.getCollectionByID( iResources.collection.get('id')),
attrPropsArray: attrSpecs,
requester: iRequesterID,
position: iResources.position
};
return this.applyChangeAndProcessResult(context, change, iMetadata);
},
/**
*
* @param iResources
* @param iValues
* @param iMetadata
*/
updateAttribute: function (iResources, iValues, iMetadata) {
var context = iResources.dataContext;
if (!iResources.collection) {
return {success: false, values: {error: 'Collection not found'}};
}
if (!iResources.attribute) {
return {success: false, values: {error: 'Attribute not found'}};
}
if (!iValues.id && iResources.attribute.id)
iValues.id = iResources.attribute.id;
if (!iValues.name && iResources.attribute.name)
iValues.name = iResources.attribute.name;
else if (iValues.name) {
iValues.clientName = iValues.name;
iValues.name = context.canonicalizeName(iValues.name + ' ');
}
var change = {
operation: 'updateAttributes',
collection: iResources.collection,
attrPropsArray: [iValues],
requester: this.get('id')
};
return this.applyChangeAndProcessResult(context, change, iMetadata);
},
updateAttributeLocation: function(iResources, iValues, iMetadata) {
var context = iResources.dataContext;
if (!context) {
return {
success: false,
values: {error: 'context not found'}
};
}
var change = {
operation: 'moveAttribute',
requester: this.get('id'),
attr: iResources.attributeLocation
};
if (iValues && !SC.none(iValues.collection)) {
change.toCollection = context.getCollectionByName(iValues.collection) ||
context.getCollectionByID(iValues.collection);
if (!change.toCollection) {
return {
success: false,
values: {error: 'Target collection not found'}
};
}
}
if (iValues && !SC.none(iValues.position)) {
change.position = iValues.position;
}
return context.applyChange(change);
},
createCases: function( iResources, iValues, iRequesterID) {
function convertDate( iValue) {
if (iValue instanceof Date) {
iValue.valueOf = function () {
return Date.prototype.valueOf.apply(this) / 1000;
};
}
}
function fixDates(iCase) {
if( Array.isArray( iCase.values)) {
iCase.values.forEach(function (iValue) {
convertDate( iValue);
});
}
else if ( typeof iCase.values === 'object') {
DG.ObjectMap.forEach( iCase.values, function( iKey, iValue) {
convertDate( iValue);
});
}
}
function createOrAppendRequest(iCase) {
fixDates(iCase);
var parent = iCase.parent;
var values = iCase.values;
var req = requests.find(function (request) {
return request.properties.parent === parent;
});
if (!req) {
req = {
operation: 'createCases',
collection: collection,
properties: {
parent: parent
},
values: [],
requester: requester
};
requests.push(req);
}
req.values.push(values);
}
if (!iResources.collection) {
return {success: false, values: {error: 'Collection not found'}};
}
// ------------------createCases-----------------
var success = true;
var context = iResources.dataContext;
var collection = iResources.collection;
var cases = Array.isArray(iValues)?iValues: [iValues];
var IDs = [];
var requester = iRequesterID;
var requests = [];
// We wish to minimize the number of change requests submitted,
// but create case change requests are not structured like cases.
// we must reformat the Plugin API create/case to some number of
// change requests, one for each parent referred to in the create/case
// object.
cases.forEach(createOrAppendRequest);
requests.forEach(function (req) {
var changeResult = context.applyChange(req);
var success = success && (changeResult && changeResult.success);
var index;
if (changeResult.success) {
for (index = 0; index < changeResult.caseIDs.length; index++) {
var caseid = changeResult.caseIDs[index];
var itemid = (index <= changeResult.itemIDs.length) ? changeResult.itemIDs[index] : null;
IDs.push({id: caseid, itemID: itemid});
}
}
});
return {success: success, values: IDs};
},
/**
* The current document will be made to conform to the information in the given JSON object.
* @param iDocObject {Object}
* @return {boolean}
*/
updateDocument: function (iDocObject, updateDataContextFunc) {
var tDocController = DG.currDocumentController(),
tComponentControllers = tDocController.get('componentControllersMap') || [],
tComponentsStorage = iDocObject.components || [];
function notifyDocumentChange(operation) {
tDocController.notificationManager.sendNotification({
action: 'notify',
resource: 'documentChangeNotice',
values: {
operation: operation
}
}, function (response) {
DG.log('Sent notification for document change: ' + operation);
DG.log('Response: ' + JSON.stringify(response));
});
}
function deleteComponentsNotInDocObject() {
var tIDsOfStoredComponents = tComponentsStorage.map(
function (iCompStorage) {
return Number(iCompStorage.guid);
});
DG.ObjectMap.forEach(tComponentControllers, function (iGuid, iController) {
var tFoundStorage = tIDsOfStoredComponents.indexOf(Number(iGuid)) >= 0;
if (!tFoundStorage) {
DG.closeComponent(iGuid);
}
});
}
function deleteDataContextsNotInDocObject() {
var tExistingContexts = tDocController.get('contextRecords') || [],
tIDsOfStoredContexts = iDocObject.contexts ? iDocObject.contexts.map(
function (iContext) {
return Number(iContext.guid);
}) : [];
DG.ObjectMap.forEach(tExistingContexts, function (iGuid, iContext) {
var tFoundStorage = tIDsOfStoredContexts.indexOf(Number(iGuid)) >= 0;
if (!tFoundStorage) {
tDocController.destroyDataContext(iGuid);
}
});
}
function createOrUpdateDataContexts() {
var tExistingContexts = tDocController.get('contextRecords'),
tNewDocContexts = iDocObject.contexts || []; // The ones we're moving toward
tNewDocContexts.forEach(function (iNewDocContext) {
var tDocContextID = iNewDocContext.guid,
tExistingDataContextRecord;
DG.ObjectMap.forEach(tExistingContexts, function (iGuid, iContext) {
if (Number(iGuid) === tDocContextID)
tExistingDataContextRecord = iContext;
});
if (tExistingDataContextRecord) { // We found an existing context with the same ID
// We don't want a difference in selected cases (contained in contextStorage) to count as a difference
var tArchiveOfFoundExistingDocContext = tExistingDataContextRecord.toArchive(true /* fullData */),
tNewContextStorage = iNewDocContext.contextStorage;
delete iNewDocContext.contextStorage;
delete tArchiveOfFoundExistingDocContext.contextStorage;
// Is the one we found identical to the one we're moving toward?
if (JSON.stringify(iNewDocContext) !== JSON.stringify(tArchiveOfFoundExistingDocContext)) {
// Something's different.
iNewDocContext.contextStorage = tNewContextStorage; // Restore this
tDocController.destroyDataContext(tDocContextID);
tDocController.createNewDataContext(iNewDocContext);
}
else
iNewDocContext.contextStorage = tNewContextStorage; // So selection will be restored
}
else {
tDocController.createNewDataContext(iNewDocContext);
}
});
}
function createOrUpdateComponents() {
tComponentsStorage.forEach(function (iCompStorage) {
var tComponentController = tComponentControllers[iCompStorage.guid];
if (tComponentController) {
var tComponentView = tComponentController.get('view'),
tViewIsMinimized = tComponentView.get('isMinimized');
tComponentController.restoreComponentStorage(iCompStorage.componentStorage);
if((tViewIsMinimized && SC.none(iCompStorage.savedHeight))||
(!tViewIsMinimized && !SC.none(iCompStorage.savedHeight))) {
tComponentView.toggleMinimization();
}
else if(iCompStorage.layout.isVisible) {
if( !tComponentView.get('isVisible')) {
tComponentView.adjust({left: 0, top: 0, width: 10, height: 10});
tComponentView.set('isVisible', true);
}
tComponentView.adjust({zIndex: iCompStorage.layout.zIndex,
isVisible: iCompStorage.layout.isVisible });
delete iCompStorage.layout.zIndex;
delete iCompStorage.layout.isVisible;
tComponentView.animate(iCompStorage.layout, {duration: 0.4, timing: 'ease-in-out'});
}
else {
tComponentView.set('isVisible', false);
}
}
else {
// Sometimes the controller no longer exits by the component does. This prevents an assert
DG.store.deregister(iCompStorage.id);
iCompStorage.document = DG.activeDocument;
iCompStorage.allowMoreThanOne = true; // defaults to false and not stored
var tComponent = DG.Component.createComponent(iCompStorage);
tDocController.createComponentAndView(tComponent);
}
});
}
// Global values are not stored in slider components so we have to reinstate them separately
function reinstateGlobalValues() {
if( iDocObject.globalValues)
iDocObject.globalValues.forEach(function (iValue) {
var tValueInDoc = DG.globalsController.getGlobalValueByID(iValue.guid);
if (tValueInDoc) {
tValueInDoc.set('value', iValue.value);
tValueInDoc.set('name', iValue.name);
}
else {
DG.globalsController.createGlobalValue( {name: iValue.name, value: iValue.value, guid: iValue.guid});
}
});
}
function reinstateSelection() {
var tDocContexts = tDocController.get('contexts');
if( iDocObject.contexts)
iDocObject.contexts.forEach(function (iContext) {
var tDocContext = tDocContexts.find(function (iDocContext) {
return iDocContext.getPath('model.id') === iContext.guid;
});
if (tDocContext) {
var tSelectedCases = iContext.contextStorage._links_.selectedCases.map(function (iLink) {
return tDocContext.getCaseByID(iLink.id);
});
// allow for a render cycle
tDocContext.invokeOnceLater(function () {
tDocContext.applyChange({
operation: 'selectCases',
select: true,
cases: tSelectedCases
});
}, 200);
}
});
}
// Begin updateDocument
var tResult = true;
notifyDocumentChange('updateDocumentBegun');
deleteComponentsNotInDocObject();
deleteDataContextsNotInDocObject();
reinstateGlobalValues();
createOrUpdateDataContexts();
createOrUpdateComponents();
reinstateSelection();
// The 500ms timeout here gives the document time to settle down.
// Especially any changes to center and zoom of a map
setTimeout( function() {
DG.UndoHistory.clearUndoRedoHistory();
notifyDocumentChange('updateDocumentEnded');
}, 500);
return tResult;
}
}
);