-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathUtilsService.js
More file actions
1433 lines (1376 loc) · 55.2 KB
/
UtilsService.js
File metadata and controls
1433 lines (1376 loc) · 55.2 KB
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
angular.module('mms')
.factory('UtilsService', ['$q', '$http', 'CacheService', 'URLService', 'ApplicationService', '_', UtilsService]);
/**
* @ngdoc service
* @name mms.UtilsService
* @requires $q
* @requires $http
* @requires CacheService
* @requires URLService
* @requires ApplicationService
* @requires _
*
* @description
* Utilities
*/
function UtilsService($q, $http, CacheService, URLService, ApplicationService, _) {
var PROJECT_URL_PREFIX = 'mms.html#/projects/';
var VIEW_SID = '_11_5EAPbeta_be00301_1147420760998_43940_227';
var OTHER_VIEW_SID = ['_17_0_1_407019f_1332453225141_893756_11936',
'_17_0_1_232f03dc_1325612611695_581988_21583', '_18_0beta_9150291_1392290067481_33752_4359'];
var DOCUMENT_SID = '_17_0_2_3_87b0275_1371477871400_792964_43374';
var BLOCK_SID = '_11_5EAPbeta_be00301_1147424179914_458922_958';
var REQUIREMENT_SID = ['_project-bundle_mission_PackageableElement-mission_u003aRequirement_PackageableElement',
'_18_0_5_f560360_1476403587924_687681_736366','_18_0_5_f560360_1476403587924_687681_736366',
'_11_5EAPbeta_be00301_1147873190330_159934_2220'];
var editKeys = ['name', 'documentation', 'defaultValue', 'value', 'specification', 'id', '_projectId', '_refId', 'type'];
var CLASS_ELEMENT_TEMPLATE = {
_appliedStereotypeIds: [],
appliedStereotypeInstanceId: null,
classifierBehaviorId: null,
clientDependencyIds: [],
collaborationUseIds: [],
documentation: "",
elementImportIds: [],
generalizationIds: [],
interfaceRealizationIds: [],
isAbstract: false,
isActive: false,
isFinalSpecialization: false,
isLeaf: false,
mdExtensionsIds: [],
name: "",
nameExpression: null,
ownedAttributeIds: [],
ownedOperationIds: [],
ownerId: null,
packageImportIds: [],
powertypeExtentIds: [],
redefinedClassifierIds: [],
representationId: null,
substitutionIds: [],
supplierDependencyIds: [],
syncElementId: null,
templateBindingIds: [],
templateParameterId: null,
type: "Class",
useCaseIds: [],
visibility: 'public'
};
var INSTANCE_ELEMENT_TEMPLATE = {
appliedStereotypeInstanceId: null,
classifierIds: [],
clientDependencyIds: [],
deploymentIds: [],
documentation: '',
mdExtensionsIds: [],
name: '',
nameExpression: null,
ownerId: null,
slotIds: [],
specification: null,
stereotypedElementId: null,
supplierDependencyIds: [],
syncElementId: null,
templateParameterId: null,
type: "InstanceSpecification",
visibility: "public",
_appliedStereotypeIds: [],
};
var VALUESPEC_ELEMENT_TEMPLATE = {
appliedStereotypeInstanceId: null,
clientDependencyIds: [ ],
documentation: "",
mdExtensionsIds: [ ],
name: "",
nameExpression: null,
supplierDependencyIds: [ ],
syncElementId: null,
templateParameterId: null,
typeId: null,
visibility: "public",
_appliedStereotypeIds: [ ],
};
var PACKAGE_ELEMENT_TEMPLATE = {
_appliedStereotypeIds : [ ],
documentation : "",
type : "Package",
mdExtensionsIds : [ ],
syncElementId : null,
appliedStereotypeInstanceId : null,
clientDependencyIds : [ ],
supplierDependencyIds : [ ],
name : "",
nameExpression : null,
visibility : null,
templateParameterId : null,
elementImportIds : [ ],
packageImportIds : [ ],
templateBindingIds : [ ],
URI : "",
packageMergeIds : [ ],
profileApplicationIds : [ ]
};
var GENERALIZATION_ELEMENT_TEMPLATE = {
appliedStereotypeInstanceId : null,
documentation : "",
generalizationSetIds : [ ],
isSubstitutable : true,
mdExtensionsIds : [ ],
syncElementId : null,
type : "Generalization",
_appliedStereotypeIds : [ ],
};
var DEPENDENCY_ELEMENT_TEMPLATE = {
_appliedStereotypeIds : [ ],
appliedStereotypeInstanceId : null,
clientDependencyIds : [ ],
documentation : "",
mdExtensionsIds : [ ],
name : "",
nameExpression : null,
supplierDependencyIds : [ ],
syncElementId : null,
templateParameterId : null,
type : "Dependency",
visibility : null,
};
/**
* @ngdoc method
* @name mms.UtilsService#hasCircularReference
* @methodOf mms.UtilsService
*
* @description
* Tells whether or not there exists a circular reference
*
* @param {Object} scope scope
* @param {string} curId current id
* @param {string} curType current type
* @returns {boolean} true or false
*/
var hasCircularReference = function(scope, curId, curType) {
var curscope = scope;
while (curscope.$parent) {
var parent = curscope.$parent;
if (parent.mmsElementId === curId && parent.cfType === curType)
return true;
curscope = parent;
}
return false;
};
/**
* @ngdoc method
* @name mms.UtilsService#cleanValueSpec
* @methodOf mms.UtilsService
*
* @description
* Cleans value specification
*
* @param {Object} vs value spec object
* @returns {void} nothing
*/
var cleanValueSpec = function(vs) {
if (vs.hasOwnProperty('valueExpression'))
delete vs.valueExpression;
if (vs.operand) {
for (var i = 0; i < vs.operand.length; i++) {
cleanValueSpec(vs.operand[i]);
}
}
};
/**
* @ngdoc method
* @name mms.UtilsService#cleanElement
* @methodOf mms.UtilsService
*
* @description
* Cleans
*
* @param {Object} elem the element object to be cleaned
* @param {boolean} [forEdit=false] (optional) forEdit.
* @returns {Object} clean elem
*/
var cleanElement = function(elem, forEdit) {
var i = 0;
if (elem.type === 'Property' || elem.type === 'Port') {
if (!elem.defaultValue) {
elem.defaultValue = null;
}
}
if (elem.type === 'Slot') {
if (!_.isArray(elem.value))
elem.value = [];
}
if (elem.value) {
for (i = 0; i < elem.value.length; i++) {
cleanValueSpec(elem.value[i]);
}
}
if (elem._contents) {
cleanValueSpec(elem._contents);
}
if (elem.specification) {
cleanValueSpec(elem.specification);
}
if (elem.type === 'Class') {
if (elem._contents && elem.contains) {
delete elem.contains;
}
if (Array.isArray(elem._displayedElementIds)) {
elem._displayedElementIds = JSON.stringify(elem._displayedElementIds);
}
if (elem._allowedElementIds) {
delete elem._allowedElementIds;
}
}
if (elem.hasOwnProperty('specialization')) {
delete elem.specialization;
}
if (forEdit) { //only keep editable or needed keys in edit object instead of everything
var keys = Object.keys(elem);
for (i in keys) {
if (editKeys.indexOf(keys[i]) >= 0) {
continue;
}
delete elem[keys[i]];
}
}
return elem;
};
/**
* @ngdoc method
* @name mms.UtilsService#buildTreeHierarchy
* @methodOf mms.UtilsService
*
* @description
* builds hierarchy of tree branch objects
*
* @param {array} array array of objects
* @param {string} id key of id field
* @param {string} type type of object
* @param {object} parent key of parent field
* @param {callback} level2_Func function to get childen objects
* @returns {void} root node
*/
var buildTreeHierarchy = function (array, id, type, parent, level2_Func) {
var rootNodes = [];
var data2Node = {};
var i = 0;
var data = null;
// make first pass to create all nodes
for (i = 0; i < array.length; i++) {
data = array[i];
data2Node[data[id]] = {
label : data.name,
type : type,
data : data,
children : []
};
}
// make second pass to associate data to parent nodes
for (i = 0; i < array.length; i++) {
data = array[i];
// If theres an element in data2Node whose key matches the 'parent' value in the array element
// add the array element to the children array of the matched data2Node element
if (data[parent] && data2Node[data[parent]]) {//bad data!
data2Node[data[parent]].children.push(data2Node[data[id]]);
} else {
// If theres not an element in data2Node whose key matches the 'parent' value in the array element
// it's a "root node" and so it should be pushed to the root nodes array along with its children
rootNodes.push(data2Node[data[id]]);
}
}
//apply level2 function if available
if (level2_Func) {
for (i = 0; i < array.length; i++) {
data = array[i];
var level1_parentNode = data2Node[data[id]];
level2_Func(data, level1_parentNode);
}
}
var sortFunction = function(a, b) {
if (a.children.length > 1) {
a.children.sort(sortFunction);
}
if (b.children.length > 1) {
b.children.sort(sortFunction);
}
if (a.label.toLowerCase() < b.label.toLowerCase()) {
return -1;
}
if (a.label.toLowerCase() > b.label.toLowerCase()) {
return 1;
}
return 0;
};
rootNodes.sort(sortFunction);
return rootNodes;
};
/**
* @ngdoc method
* @name mms.UtilsService#normalize
* @methodOf mms.UtilsService
*
* @description
* Normalize common arguments
*
* @param {Object} ob Object with update, workspace, version keys
* @returns {Object} object with update, ws, ver keys based on the input.
* default values: {update: false, ws: 'master', ver: 'latest'}
*/
var normalize = function(reqOb) {
reqOb.extended = !reqOb.extended ? false : true;
reqOb.refId = !reqOb.refId ? 'master' : reqOb.refId;
reqOb.commitId = !reqOb.commitId ? 'latest' : reqOb.commitId;
return reqOb;
};
/**
* @ngdoc method
* @name mms.UtilsService#makeElementKey
* @methodOf mms.UtilsService
*
* @description
* Make key for element for use in CacheService
*
* @param {string} elementOb element object
* @param {boolean} [edited=false] element is to be edited
* @returns {Array} key to be used in CacheService
*/
var makeElementKey = function(elementOb, edit) {
var refId = !elementOb._refId ? 'master' : elementOb._refId;
var commitId = !elementOb._commitId ? 'latest' : elementOb._commitId;
var key = ['element', elementOb._projectId, refId, elementOb.id, commitId];
if (edit)
key.push('edit');
return key;
};
/**
* @ngdoc method
* @name mms.UtilsService#makeArtifactKey
* @methodOf mms.UtilsService
*
* @description
* Make key for element for use in CacheService
*
* @param {string} elementOb element object
* @param {boolean} [edited=false] element is to be edited
* @returns {Array} key to be used in CacheService
*/
var makeArtifactKey = function(elementOb, edit) {
var refId = !elementOb._refId ? 'master' : elementOb._refId;
var commitId = !elementOb._commitId ? 'latest' : elementOb._commitId;
var key = ['artifact', elementOb._projectId, refId, elementOb.id, commitId];
if (edit)
key.push('edit');
return key;
};
/**
* @ngdoc method
* @name mms.UtilsService#mergeElement
* @methodOf mms.UtilsService
*
* @description
* Make key for element for use in CacheService
*
* @param {object} source the element object to merge in
* @param {boolean} [updateEdit=false] updateEdit
* @returns {void} nothing
*/
var mergeElement = function(source, updateEdit) {
//TODO remove calls to this, shoudl use ElementService.cacheElement
};
/**
* @ngdoc method
* @name mms.UtilsService#filterProperties
* @methodOf mms.UtilsService
*
* @description
* given element object a and element object b,
* returns new object with b data minus keys not in a
* (set notation A intersect B)
*
* @param {Object} a Element Object
* @param {Object} b Element Object
* @returns {Object} new object
*/
var filterProperties = function(a, b) {
var res = {};
for (var key in a) {
if (a.hasOwnProperty(key) && b.hasOwnProperty(key)) {
res[key] = b[key];
}
}
return res;
};
/**
* @ngdoc method
* @name mms.UtilsService#hasConflict
* @methodOf mms.UtilsService
*
* @description
* Checks if sever and cache version of the element are
* the same so that the user is aware that they are overriding
* changes to the element that they have not seen in the cache element.
* Given edit object with only keys that were edited,
* 'orig' object and 'server' object, should only return true
* if key is in edit object and value in orig object is different
* from value in server object.
*
* @param {Object} edit An object that contains element id and any property changes to be saved.
* @param {Object} orig version of elem object in cache.
* @param {Object} server version of elem object from server.
* @returns {Boolean} true if conflict, false if not
*/
var hasConflict = function(edit, orig, server) {
for (var i in edit) {
if (i === '_read' || i === '_modified' || i === '_modifier' ||
i === '_creator' || i === '_created' || i === '_commitId') {
continue;
}
if (edit.hasOwnProperty(i) && orig.hasOwnProperty(i) && server.hasOwnProperty(i)) {
if (!angular.equals(orig[i], server[i])) {
return true;
}
}
}
return false;
};
/**
* @ngdoc method
* @name mms.UtilsService#isRestrictedValue
* @methodOf mms.UtilsService
*
* @description
* deprecated
*
* @param {string} table table content
* @returns {boolean} boolean
*/
function isRestrictedValue(values) {
if (values.length > 0 && values[0].type === 'Expression' &&
values[0].operand.length === 3 && values[0].operand[0].value === 'RestrictedValue' &&
values[0].operand[2].type === 'Expression' && values[0].operand[2].operand.length > 0 &&
values[0].operand[1].type === 'ElementValue') {
return true;
}
return false;
}
/**
* @ngdoc method
* @name mms.UtilsService#makeHtmlTable
* @methodOf mms.UtilsService
*
* @description
* make html table based on table spec object
*
* @param {object} table table content
* @param {boolean} isFilterable table content
* @param {boolean} isSortable table content
* @returns {string} generated html string
*/
var makeHtmlTable = function(table, isFilterable, isSortable, pe) {
var result = ['<table class="table-bordered table-condensed">'];
if (ApplicationService.getState().inDoc && !table.excludeFromList) {
result.push('<caption>Table {{mmsPe._veNumber}}. <span ng-bind-html="table.title || mmsPe.name"></span></caption>');
} else if (table.title) {
result.push('<caption>' + table.title + '</caption>');
}
if (table.colwidths && table.colwidths.length > 0) {
result.push('<colgroup>');
for (var i = 0; i < table.colwidths.length; i++) {
if (table.colwidths[i]) {
result.push('<col style="width: ' + table.colwidths[i] + '">');
} else {
result.push('<col>');
}
}
result.push('</colgroup>');
}
if (table.header.length) {
// only add styling to the filterable or sortable header
if ( isFilterable || isSortable ) {
result.push('<thead class="doc-table-header" >');
} else {
result.push('<thead>');
}
result.push(makeTableBody(table.header, true, isFilterable, isSortable));
result.push('</thead>');
}
result.push('<tbody>');
result.push(makeTableBody(table.body, false));
result.push('</tbody>');
result.push('</table>');
return result.join('');
};
var tableConfig = {
sortByColumnFn: 'sortByColumnFn',
showBindingForSortIcon: 'sortColumnNum',
filterDebounceRate: 200,
filterTermColumnPrefixBinding: 'filterTermForColumn'
};
/** Include row and column number for table's header data object **/
var _generateRowColNumber = function(header) {
header.forEach(function (row, rowIndex) {
var startCol = 0;
var colCounter = 0;
row.forEach(function (cell, cellIndex) {
// startCol is always 0 except when row > 0th and on cell === 0th && rowSpan of the previous row's first element is larger than 1
// This is the only time when we need to offset the starting colNumber for cells under merged column(s)
if ( rowIndex !== 0 && cellIndex === 0 && Number(header[rowIndex - 1][0].rowspan) > 1 ) {
startCol = Number(header[rowIndex - 1][0].colspan);
}
var colSpan = Number(cell.colspan);
cell.startRow = rowIndex;
cell.endRow = cell.startRow + Number(cell.rowspan) - 1;
cell.startCol = startCol + colCounter;
cell.endCol = cell.startCol + colSpan - 1;
colCounter += colSpan;
});
startCol = 0;
colCounter = 0;
});
};
/**
* @ngdoc method
* @name mms.UtilsService#makeTableBody
* @methodOf mms.UtilsService
*
* @description
* make html table body based on body spec object
*
* @param {object} body body content
* @param {boolean} isHeader is header
* @param {boolean} isFilterable is filterable
* @param {boolean} isSortable is sortable
* @returns {string} generated html string
*/
var makeTableBody = function(body, isHeader, isFilterable, isSortable) {
if ( isHeader && (isFilterable || isSortable ) ) {
_generateRowColNumber(body);
}
var result = [], i, j, k, row, cell, thing;
var dtag = (isHeader ? 'th' : 'td');
for (i = 0; i < body.length; i++) {
result.push('<tr>');
row = body[i];
for (j = 0; j < row.length; j++) {
cell = row[j];
result.push('<' + dtag + ' colspan="' + cell.colspan + '" rowspan="' + cell.rowspan + '">');
for (k = 0; k < cell.content.length; k++) {
thing = cell.content[k];
if ( isFilterable || isSortable ) {
result.push('<div ng-style="{display: \'inline\'}">');
} else {
result.push('<div>');
}
if (thing.type === 'Paragraph') {
var para = makeHtmlPara(thing);
// add special styling for header's title
if ( ( isFilterable || isSortable ) && thing.sourceType === 'text' ) {
para = para.replace('<p>', '<p ng-style="{display: \'inline\'}">' );
}
result.push(para);
} else if (thing.type === 'Table') {
result.push(makeHtmlTable(thing));
} else if (thing.type === 'List') {
result.push(makeHtmlList(thing));
} else if (thing.type === 'Image') {
//todo use mmsCf
result.push('<mms-cf mms-cf-type="img" mms-element-id="' + thing.id + '"></mms-cf>');
}
result.push('</div>');
if ( isHeader ) {
if ( isSortable && Number(cell.colspan) === 1 ) {
result.push('<span' + ' ng-click=\"'+ tableConfig.sortByColumnFn + "(" + cell.startCol + ")" + '\"' + ' ng-class=\"'+ 'getSortIconClass('+ cell.startCol + ')' + '\"></span>');
}
if ( isFilterable ) {
result.push('<input class="no-print ve-plain-input filter-input" type="text" placeholder="Filter column"' + ' ng-show="showFilter" ng-model-options=\"{debounce: '+ tableConfig.filterDebounceRate + '}\"' + ' ng-model=\"' + tableConfig.filterTermColumnPrefixBinding + cell.startCol + cell.endCol + '\">');
}
}
}
result.push('</' + dtag + '>');
}
result.push('</tr>');
}
return result.join('');
};
/**
* @ngdoc method
* @name mms.UtilsService#makeHtmlList
* @methodOf mms.UtilsService
*
* @description
* make html list string based on list spec object
*
* @param {object} list list specification object
* @returns {string} generated html string
*/
var makeHtmlList = function(list) {
var result = [], i, j, item, thing;
if (list.ordered)
result.push('<ol>');
else
result.push('<ul>');
for (i = 0; i < list.list.length; i++) {
item = list.list[i];
result.push('<li>');
for (j = 0; j < item.length; j++) {
thing = item[j];
result.push('<div>');
if (thing.type === 'Paragraph') {
result.push(makeHtmlPara(thing));
} else if (thing.type === 'Table') {
result.push(makeHtmlTable(thing));
} else if (thing.type === 'List') {
result.push(makeHtmlList(thing));
} else if (thing.type === 'Image') {
result.push('<mms-cf mms-cf-type="img" mms-element-id="' + thing.id + '"></mms-cf>');
}
result.push('</div>');
}
result.push('</li>');
}
if (list.ordered)
result.push('</ol>');
else
result.push('</ul>');
return result.join('');
};
/**
* @ngdoc method
* @name mms.UtilsService#makeHtmlPara
* @methodOf mms.UtilsService
*
* @description
* make html para string based on para spec object
*
* @param {object} para paragraph spec object
* @returns {string} generated html string
*/
var makeHtmlPara = function(para) {
if (para.sourceType === 'text')
return para.text;
var t = 'doc';
var attr = '';
if (para.sourceProperty === 'name') {
t = 'name';
}
if (para.sourceProperty === 'value') {
t = 'val';
}
if (para.nonEditable) {
attr = ' non-editable="' + para.nonEditable + '"';
}
//TODO update these to match mmsCF
return '<mms-cf mms-cf-type="' + t + '" mms-element-id="' + para.source + '"' + attr + '></mms-cf>';
};
/**
* @ngdoc method
* @name mms.UtilsService#makeHtmlTOCChild
* @methodOf mms.UtilsService
*
* @description
* Generates table of contents for the document/views.
*
* @param {string} tree the root element (document or view)
* @returns {string} toc string
*/
var makeHtmlTOC = function (tree) {
var result = '<div class="toc"><h1 class="header">Table of Contents</h1>';
var root_branch = tree[0].branch;
result += makeHtmlTOCChild(root_branch, true);
result += '</div>';
return result;
};
/**
* @ngdoc method
* @name mms.UtilsService#makeHtmlTOCChild
* @methodOf mms.UtilsService
*
* @description
* Generates table of contents for the document/views.
*
* @param {string} child the view to be referenced in the table of content
* @param {boolean} skip skip adding li for this branch
* @returns {string} toc string
*/
var makeHtmlTOCChild = function(branch, skip) {
var result = '';
var child;
if (!skip) {
var anchor = '<a href=#' + branch.data.id + '>';
result += ' <li>' + anchor + branch.data._veNumber + ' ' + branch.data.name + '</a>';
}
var ulAdded = false;
for (var i = 0; i < branch.children.length; i++) {
child = branch.children[i];
if (child.type !== 'view' && child.type !== 'section') {
continue;
}
if (!ulAdded) {
result += '<ul>';
ulAdded = true;
}
result += makeHtmlTOCChild(child);
}
if (ulAdded) {
result += '</ul>';
}
if (!skip) {
result += '</li>';
}
return result;
};
/**
* @ngdoc method
* @name mms.UtilsService#makeTablesAndFiguresTOC
* @methodOf mms.UtilsService
*
* @description
* Generates a list of tables, figures, and equations. Default uses presentation elements.
* `html` param provides option to use html content to generate list. It also appends the
* captions to the figures and tables.
*
* @param {string} tree the document/view to be printed (what is on the left pane)
* @param {string} printElement contents to be printed (what is displayed in the center pane)
* @param {boolean} live true only if a specific sorting is required
* @param {boolean} html whether to generated list of tables and figures using html content, outside of the corresponding PE or not
* @returns {object} results
*/
var makeTablesAndFiguresTOC = function(tree, printElement, live, html) {
var ob = {
tables: '',
figures: '',
equations: '',
tableCount: 0,
figureCount: 0,
equationCount: 0
};
var root_branch = tree[0].branch;
// If both "Generate List of Tables and Figures" && "Use HTML for List of Tables and Figures " options are checked...
if (html) {
ob = generateTOCHtmlOption(ob, tree, printElement);
// return obHTML;
} else {
for (var i = 0; i < root_branch.children.length; i++) {
makeTablesAndFiguresTOCChild(root_branch.children[i], printElement, ob, live, false);
}
}
ob.tables = ob.tables.length ? '<div class="tot"><h1 class="header">List of Tables</h1><ul>' + ob.tables + '</ul></div>' : '';
ob.figures = ob.figures.length ? '<div class="tof"><h1 class="header">List of Figures</h1><ul>' + ob.figures + '</ul></div>' : '';
ob.equations = ob.equations.length ? '<div class="tof"><h1 class="header">List of Equations</h1><ul>' + ob.equations + '</ul></div>' : '';
return ob;
};
/**
* @ngdoc method
* @name mms.UtilsService#makeTablesAndFiguresTOCChild
* @methodOf mms.UtilsService
*
* @description
* Generates a list of tables, figures, and equations of the none root node of he tree (containment tree on the left pane). It also appends the captions to the figures and tables.
*
* @param {string} child presentation element
* @param {string} printElement contents to be printed (what is displayed in the center pane)
* @param {string} ob an object that stores the html list of tables, figures, and equations as well as the counts of those
* @param {boolean} live true when user would like to preview numbering in the app
* @param {boolean} showRefName the tree hierarchy of the document or view (what is displayed in the left pane)
* @returns {void} nothing
*/
var makeTablesAndFiguresTOCChild = function(child, printElement, ob, live, showRefName) {
var pe = child.data;
var sysmlId = pe.id;
var veNumber = pe._veNumber;
var prefix = '';
var el = printElement.find('#' + sysmlId);
var refs = printElement.find('mms-view-link[mms-pe-id="' + sysmlId + '"], mms-view-link[data-mms-pe-id="' + sysmlId + '"]');
var cap = '';
var name = '';
if (child.type === 'table') {
//ob.tableCount++;
prefix = 'Table ' + veNumber + '. ';
var capTbl = el.find('table > caption');
name = capTbl.html();
if (name && name.indexOf('Table') === 0 && name.split('. ').length > 0) {
name = name.substring(name.indexOf(prefix) + prefix.length);
} else if (name === "") {
name = pe.name;
}
cap = veNumber + '. ' + name;
ob.tables += '<li><a href="#' + sysmlId + '">' + cap + '</a></li>';
capTbl.html('Table ' + cap);
// If caption does not exist, add to html
if (capTbl.length === 0) {
el.find('table').prepend('<caption>Table ' + cap + '</caption>');
}
// Change cap value based on showRefName true/false
if (!showRefName) {
cap = veNumber;
}
if (!live) {
refs.find('a').attr('href', '#' + sysmlId);
}
refs.filter('[suppress-numbering!="true"]').filter(':not([link-text])').find('a').html('Table ' + cap);
}
if (child.type === 'figure') {
//ob.figureCount++;
prefix = 'Figure ' + veNumber + '. ';
var capFig = el.find('figure > figcaption');
name = capFig.html();
if (name && name.indexOf('Figure') === 0 && name.split('. ').length > 0) {
name = name.substring(name.indexOf(prefix) + prefix.length);
} else if (name === "") {
name = pe.name;
}
cap = veNumber + '. ' + name;
ob.figures += '<li><a href="#' + sysmlId + '">' + cap + '</a></li>';
capFig.html('Figure ' + cap);
// If caption does not exist, add to html
if (capFig.length === 0) {
el.find('img').wrap('<figure></figure>').after('<figcaption>Figure ' + cap + '</figcaption>');
}
// Change cap value based on showRefName true/false
if (!showRefName) {
cap = veNumber;
}
if (!live) {
refs.find('a').attr('href', '#' + sysmlId);
}
refs.filter('[suppress-numbering!="true"]').filter(':not([link-text])').find('a').html('Fig. ' + cap);
}
if (child.type === 'equation') {
//ob.equationCount++;
cap = veNumber + '. ' + pe.name;
ob.equations += '<li><a href="#' + sysmlId + '">' + cap + '</a></li>';
var equationCap = '(' + veNumber + ')';
var capEq = el.find('.mms-equation-caption');
capEq.html(equationCap);
// If caption does not exist, add to html
if (capEq.length === 0) {
el.find('mms-view-equation > mms-cf > mms-transclude-doc > p').last().append('<span class="mms-equation-caption pull-right">' + equationCap + '</span>');
}
if (!live) {
refs.find('a').attr('href', '#' + sysmlId);
}
refs.filter('[suppress-numbering!="true"]').filter(':not([link-text])').find('a').html('Eq. ' + equationCap);
}
for (var i = 0; i < child.children.length; i++) {
makeTablesAndFiguresTOCChild(child.children[i], printElement, ob, live, showRefName);
}
};
var addLiveNumbering = function(pe, el, type) {
var veNumber = pe._veNumber;
if (!veNumber) {
return;
}
var prefix = '';
var name = '';
var cap = '';
if (type === 'table') {
prefix = 'Table ' + veNumber + '. ';
var capTbl = el.find('table > caption');
name = capTbl.html();
if (name && name.indexOf('Table') === 0 && name.split('. ').length > 0) {
name = name.substring(name.indexOf(prefix) + prefix.length);
} else if (name === "") {
name = pe.name;
}
cap = veNumber + '. ' + name;
capTbl.html('Table ' + cap);
// If caption does not exist, add to html
if (capTbl.length === 0) {
el.find('table').prepend('<caption>Table ' + cap + '</caption>');
}
}
if (type === 'figure') {
prefix = 'Figure ' + veNumber + '. ';
var capFig = el.find('figure > figcaption');
name = capFig.html();
if (name && name.indexOf('Figure') === 0 && name.split('. ').length > 0) {
name = name.substring(name.indexOf(prefix) + prefix.length);
} else if (name === "") {
name = pe.name;
}
cap = veNumber + '. ' + name;
capFig.html('Figure ' + cap);
// If caption does not exist, add to html
if (capFig.length === 0) {
el.find('img').wrap('<figure></figure>').after('<figcaption>Figure ' + cap + '</figcaption>');
}
}
if (type === 'equation') {
var equationCap = '(' + veNumber + ')';
var capEq = el.find('.mms-equation-caption');
capEq.html(equationCap);
// If caption does not exist, add to html
if (capEq.length === 0) {
el.find('mms-view-equation > mms-cf > mms-transclude-doc > p').last().append('<span class="mms-equation-caption pull-right">' + equationCap + '</span>');
}
}
};
/**
* @ngdoc method
* @name mms.UtilsService#generateAnchorId
* @methodOf mms.UtilsService
*
* @description
* Generates a unique ID to be used in TOC anchor tags (e.g. <a name='tbl_xxxxx...x'>, <a href='#tbl_xxxxx...x'>)
*
* @param {string} prefix "tbl_" when creating an id for a table, "fig_" when creating an id for a figuer
* @returns {string} unique ID wit prefix, tbl_ or fig_
*/
var generateAnchorId = function(prefix){
return prefix + ApplicationService.createUniqueId();
};
/**
* @ngdoc method
* @name mms.UtilsService#generateTOCHtmlOption
* @methodOf mms.UtilsService
*
* @description
* Generates a list of tables, figures, and equations. It also appends the captions to the figures and tables.
*
* @param {string} ob an object that stores the html list of tables, figures, and equations as well as the counts of those
* @param {string} tree the tree hierarchy of the document or view (what is displayed in the left pane)
* @param {string} printElement contents to be printed (what is displayed in the center pane)
* @returns {string} populates the object fed to the function (the first argument) and return
*/
var generateTOCHtmlOption = function(ob, tree, printElement){
// Grab all existing tables and figures inside the center pane, and assign them to tables and figures
var tables = printElement.find('table'),
figures = printElement.find('figure');
// equations = printElement.find('.math-tex');
var anchorId = '', thisCap='', tblCap, tbl, fig, j;
ob.tableCount = tables.length;
ob.figureCount = figures.length;
// Tables
for ( j = 0; j < tables.length; j++) {
tbl = $(tables[j]);
tblCap = $('caption', tbl);
// Set the link from the List of Tables to the actual tables
anchorId = generateAnchorId('tbl_');
tbl.attr('id', anchorId);
// Append li to the List of Tables
thisCap = (tblCap && tblCap.text() !== '') ? (j+1) + ". " + tblCap.text() : (j+1) + ". ";
ob.tables += '<li><a href="#' + anchorId + '">' + thisCap + '</a></li>';
// If no caption exists, add empty caption for numbering
if (tblCap.length === 0) {
tbl.prepend('<caption> </caption>');
}
}
// Figures
for ( j = 0; j < figures.length; j++) {
fig = $(figures[j]);