1 define([], function () {
4 function NodeWrapperService(constants, RequestBuilderService, RestrictionsService, TypeWrapperService,
5 ListFilteringService, EventDispatcherService, FilterNodeWrapperService){
6 var comparePropToElemByName = function comparePropToElemByName(propName, elemName) {
8 // return propName === elemName; //TODO also check by namespace - redundancy?
10 return (propName.indexOf(':') > -1 ? propName.split(':')[1] : propName) === elemName; //TODO also check by namespace - redundancy?
13 var equalArrays = function (arrA, arrB) {
14 var match = (arrA.length === arrB.length) && arrA.length > 0;
18 while (i < arrA.length && match) {
19 var valMatch = arrA[i] === arrB[i];
20 match = match && valMatch;
27 var equalListElems = function (listElemA, listElemB, refKey) {
28 var getKeyValue = function (data, label, module) {
29 if (data && data.hasOwnProperty(label)) {
31 } else if (data && data.hasOwnProperty(module + ':' + label)) {
32 return data[module + ':' + label];
37 getKeyArrayValues = function (data, refKey) {
38 return refKey.map(function (key) {
39 return getKeyValue(data, key.label, key.module);
40 }).filter(function (item) {
44 keyValuesA = getKeyArrayValues(listElemA, refKey);
45 var keyValuesB = getKeyArrayValues(listElemB, refKey);
47 return equalArrays(keyValuesA, keyValuesB);
50 var checkListElemKeys = function (listData, refKey) {
51 var doubleKeyIndexes = [],
54 listData.forEach(function (item, index) {
55 var duplitactes = checkedElems.filter(function (checked) {
56 var isDuplicate = equalListElems(item, checked.item, refKey);
57 if (isDuplicate && doubleKeyIndexes.indexOf(checked.index) === -1) {
58 doubleKeyIndexes.push(checked.index);
63 if (duplitactes.length) {
64 //item is already in checkedElems so we don't need to push it again
65 doubleKeyIndexes.push(index);
67 checkedElems.push({index: index, item: item});
71 return doubleKeyIndexes;
74 var parseRestrictText = function (text) {
75 return text.split('|').map(function (elem) {
76 var subElems = elem.split('..');
77 return subElems.length === 1 ? RestrictionsService.getEqualsFnc(subElems[0]) :
78 RestrictionsService.getMinMaxFnc(subElems[0], subElems[1]);
83 var getTypes = function (node) {
86 var getTypesRecursive = function (node, types) {
89 node.getChildren('type').forEach(function (child) {
90 getTypesRecursive(child, types);
94 node.getChildren('type').forEach(function (child) {
95 //console.info('child', child);
96 getTypesRecursive(child, types);
103 wrap: function (node) {
104 if (this.hasOwnProperty(node.type)) {
105 this[node.type](node);
108 wrapAll: function (node) {
111 node.children.forEach(function (child) {
115 checkKeyDuplicity: function (listData, refKey) {
116 return checkListElemKeys(listData, refKey);
118 leaf: function (node) {
120 node.valueIsValid = true;
121 node.typeChild = node.getChildren('type')[0];
123 node.buildRequest = function (builder, req, module) {
124 var value = node.typeChild.getValue(),
125 labelWithModule = (module !== node.module ? node.module + ':' : '') + node.label;
128 EventDispatcherService.dispatch(constants.EV_FILL_PATH, node, value);
132 builder.insertPropertyToObj(req, labelWithModule, value);
139 node.fill = function (name, data) {
142 match = comparePropToElemByName(name, node.label);
144 node.value = data.toString();
145 if (node.typeChild) {
146 node.typeChild.fill(node.value);
152 node.clear = function () {
155 if (node.typeChild) {
156 node.typeChild.clear();
160 node.isFilled = function () {
161 var filled = node.typeChild.getValue() ? true : false;
165 node.checkValueType = function () {
166 node.valueIsValid = node.typeChild ? node.typeChild.check(node.value) : true;
169 node.isKey = function() {
170 return node.parent && node.parent.type === 'list' && node.parent.refKey && node.parent.refKey.indexOf(node) !== -1;
173 type: function (node) {
174 TypeWrapperService.wrapAll(node);
176 length: function (node) {
177 node.restrictions = parseRestrictText(node.label);
179 range: function (node) {
180 node.restrictions = parseRestrictText(node.label);
182 pattern: function (node) {
183 node.restrictions = [RestrictionsService.getReqexpValidationFnc(node.label)];
185 // enum: function (node) {
187 // bit: function (node) {
189 // position: function (node) {
191 container: function (node) {
192 node.expanded = false;
194 node.toggleExpand = function () {
195 node.expanded = !node.expanded;
198 node.buildRequest = function (builder, req, module) {
201 objToAdd = builder.createObj(),
202 builderNodes = node.getChildren(null, null, constants.NODE_UI_DISPLAY),
203 checkEmptyContainer = function(type, obj) { //TODO: absolete after when statement is implemented
204 return !!(type === 'case' || !$.isEmptyObject(objToAdd));
206 checkPresence = function(containerNode) {
207 return containerNode.children.some(function(ch) {
208 return ch.type === 'presence';
211 labelWithModule = (module !== node.module ? node.module + ':' : '') + node.label;
213 if (builderNodes.length) {
214 builderNodes.forEach(function (child) {
215 var childAdded = child.buildRequest(builder, objToAdd, node.module);
216 added = added || childAdded;
224 if (added && (checkEmptyContainer(node.parent ? node.parent.type : 'blanktype', objToAdd) || checkPresence(node))) {
225 builder.insertPropertyToObj(req, labelWithModule, objToAdd);
231 node.fill = function (name, data) {
232 var match = comparePropToElemByName(name, node.label),
233 nodesToFill = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
235 if (match && nodesToFill.length) {
236 nodesToFill.forEach(function (child) {
237 for (var prop in data) {
238 child.fill(prop, data[prop]);
242 node.expanded = match;
248 node.clear = function () {
249 var nodesToClear = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
250 node.nodeType = constants.NODE_UI_DISPLAY;
252 if (nodesToClear.length) {
253 nodesToClear.forEach(function (child) {
259 node.isFilled = function () {
260 return node.getChildren(null, null, constants.NODE_UI_DISPLAY).some(function (child) {
261 return child.isFilled();
265 rpc: function (node) {
266 node.expanded = true;
267 node.buildRequest = function (builder, req, module) {
270 objToAdd = builder.createObj(),
271 builderNodes = node.getChildren(null, null, constants.NODE_UI_DISPLAY),
272 labelWithModule = (module !== node.module ? node.module + ':' : '') + node.label;
274 if (builderNodes.length) {
275 builderNodes.forEach(function (child) {
276 var childAdded = child.buildRequest(builder, objToAdd, node.module);
277 added = added || childAdded;
281 objToAdd = constants.NULL_DATA;
285 builder.insertPropertyToObj(req, labelWithModule, objToAdd);
291 node.fill = function (name, data) {
293 nodesToFill = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
295 nodesToFill.forEach(function (child) {
296 var childFilled = child.fill(name, data);
297 filled = filled || childFilled;
300 node.expanded = filled;
305 node.clear = function () {
306 var nodesToClear = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
308 if (nodesToClear.length) {
309 nodesToClear.forEach(function (child) {
315 node.isFilled = function () {
316 return node.getChildren(null, null, constants.NODE_UI_DISPLAY).some(function (child) {
317 return child.isFilled();
322 input: function (node) {
323 node.expanded = true;
325 node.buildRequest = function (builder, req, module) {
328 objToAdd = builder.createObj(),
329 builderNodes = node.getChildren(null, null, constants.NODE_UI_DISPLAY),
330 labelWithModule = (module !== node.module ? node.module + ':' : '') + node.label;
332 if (builderNodes.length) {
334 builderNodes.forEach(function (child) {
335 var childAdded = child.buildRequest(builder, objToAdd, node.module);
336 added = added || childAdded;
343 builder.insertPropertyToObj(req, labelWithModule, objToAdd);
349 node.fill = function (name, data) {
350 var match = comparePropToElemByName(name, node.label),
351 nodesToFill = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
353 if (match && nodesToFill.length) {
354 nodesToFill.forEach(function (child) {
355 for (var prop in data) {
356 child.fill(prop, data[prop]);
359 node.expanded = match;
365 node.clear = function () {
366 var nodesToClear = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
368 if (nodesToClear.length) {
369 nodesToClear.forEach(function (child) {
375 node.isFilled = function () {
376 return node.getChildren(null, null, constants.NODE_UI_DISPLAY).some(function (child) {
377 return child.isFilled();
382 output: function (node) {
383 node.expanded = true;
385 node.buildRequest = function (builder, req) {
386 // var added = false,
387 // name = node.label,
388 // objToAdd = builder.createObj(),
389 // builderNodes = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
391 // if (builderNodes.length) {
392 // builderNodes.forEach(function (child) {
393 // var childAdded = child.buildRequest(builder, objToAdd);
394 // added = added || childAdded;
401 // builder.insertPropertyToObj(req, name, objToAdd);
407 node.fill = function (name, data) {
408 var match = comparePropToElemByName(name, node.label),
409 nodesToFill = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
411 if (match && nodesToFill.length) {
412 nodesToFill.forEach(function (child) {
413 for (var prop in data) {
414 child.fill(prop, data[prop]);
417 node.expanded = match;
423 node.clear = function () {
424 var nodesToClear = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
426 if (nodesToClear.length) {
427 nodesToClear.forEach(function (child) {
433 node.isFilled = function () {
434 return node.getChildren(null, null, constants.NODE_UI_DISPLAY).some(function (child) {
435 return child.isFilled();
440 case: function (node) {
442 node.buildRequest = function (builder, req, module) {
445 node.getChildren(null, null, constants.NODE_UI_DISPLAY).forEach(function (child) {
446 var childAdded = child.buildRequest(builder, req, module);
447 added = added || childAdded;
453 node.fill = function (name, data) {
455 nodesToFill = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
457 nodesToFill.forEach(function (child) {
458 var childFilled = child.fill(name, data);
459 filled = filled || childFilled;
465 node.clear = function () {
466 var nodesToClear = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
468 nodesToClear.forEach(function (child) {
473 node.isFilled = function () {
474 return node.getChildren(null, null, constants.NODE_UI_DISPLAY).some(function (child) {
475 return child.isFilled();
479 choice: function (node) {
481 node.expanded = true;
483 node.buildRequest = function (builder, req, module) {
487 added = node.choice.buildRequest(builder, req, module);
493 node.fill = function (name, data) {
495 nodesToFill = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
497 nodesToFill.forEach(function (child) {
498 var childFilled = child.fill(name, data);
504 filled = filled || childFilled;
513 node.clear = function () {
514 node.nodeType = constants.NODE_UI_DISPLAY;
522 node.isFilled = function () {
523 return node.choice !== null;
526 'leaf-list': function (node) {
528 node.expanded = true;
530 node.toggleExpand = function () {
531 node.expanded = !node.expanded;
534 node.addListElem = function () {
538 node.value.push(newElement);
541 node.removeListElem = function (elem) {
542 node.value.splice(node.value.indexOf(elem), 1);
545 node.buildRequest = function (builder, req, module) {
547 labelWithModule = (module !== node.module ? node.module + ':' : '') + node.label;
549 for (var i = 0; i < node.value.length; i++) {
550 valueArray.push(node.value[i].value);
553 if (valueArray.length > 0) {
554 builder.insertPropertyToObj(req, labelWithModule, valueArray);
563 node.fill = function (name, array) {
564 var match = comparePropToElemByName(name, node.label),
569 for (var i = 0; i < array.length; i++) {
573 node.value.push(newLeafListItem);
580 node.clear = function () {
581 node.nodeType = constants.NODE_UI_DISPLAY;
585 node.isFilled = function () {
586 return node.value.length > 0;
590 key: function (node) {
591 // do this only on list, not on listElem because deepCopy on list doesn't copy property keys to listElem => don't do this when button for add new list is clicked
592 if (node.parent.hasOwnProperty('refKey')) {
593 var keyLabels = node.label.split(' '),
594 keyNodes = node.parent.getChildren(null, null, constants.NODE_UI_DISPLAY).filter(function (child) {
595 return keyLabels.indexOf(child.label) > -1;
597 getRefKeyArray = function(keys){
598 var refKeyArray = [];
599 keyLabels.forEach(function(keyLabel){
600 var nk = keys.filter(function(k){
601 return keyLabel === k.label;
605 refKeyArray.push(nk[0]);
611 node.parent.refKey = getRefKeyArray(keyNodes);
614 config: function (node) {
615 node.parent.isConfigStm = (node.label === 'true');
617 list: function (node) {
619 node.doubleKeyIndexes = [];
620 node.actElemStructure = null;
621 node.actElemIndex = -1;
623 node.expanded = true;
625 node.filterNodes = [];
626 node.searchedPath = [];
627 node.referenceNode = null;
628 node.filteredListData = [];
629 node.currentFilter = 0;
631 node.toggleExpand = function () {
632 node.expanded = !node.expanded;
635 node.createStructure = function () {
636 if (node.actElemStructure === null) {
637 var copy = node.deepCopy(['augmentionGroups', 'augmentationId']);
638 wrapper._listElem(copy);
639 node.actElemStructure = copy;
640 node.actElemStructure.getActElemIndex = node.getActElemIndex;
644 node.getActElemIndex = function() {
645 return node.actElemIndex;
648 node.addListElem = function () {
649 node.createStructure();
650 var newElemData = {};
651 node.listData.push(newElemData);
652 node.changeActElementData(node.listData.length - 1,true);
655 node.buildActElemData = function () {
658 if (node.actElemStructure) {
659 node.actElemStructure.listElemBuildRequest(RequestBuilderService, list, node.module);
660 result = list[0] ? list[0] : {};
665 node.changeActElementData = function (index,fromAdd) {
666 var storeData = node.buildActElemData();
667 node.expanded = true;
669 if (node.actElemIndex > -1) { //we are changing already existing data
670 if(node.filteredListData && node.filteredListData.length){
671 node.listData[node.listData.indexOf(node.filteredListData[node.actElemIndex])] = storeData;
672 node.filteredListData[node.actElemIndex] = storeData;
674 ListFilteringService.clearFilterData(node, true, false);
677 node.listData[node.actElemIndex] = storeData;
680 node.actElemIndex = index;
683 if(!(node.filteredListData && node.filteredListData.length)){
684 actData = node.listData[node.actElemIndex];
686 actData = node.listData[node.listData.indexOf(node.filteredListData[node.actElemIndex])];
689 node.actElemStructure.clear();
690 for (var prop in actData) {
691 node.actElemStructure.fillListElement(prop, actData[prop]);
694 EventDispatcherService.dispatch(constants.EV_LIST_CHANGED, node.actElemStructure);
697 node.removeListElem = function (elemIndex,fromFilter) {
700 elemIndex = node.listData.indexOf(node.filteredListData[elemIndex]);
703 node.listData.splice(elemIndex, 1);
704 node.actElemIndex = node.listData.length - 1;
707 ListFilteringService.clearFilterData(node,true,false);
710 if (node.actElemIndex === -1) {
711 node.actElemStructure = null;
713 var actData = node.listData[node.actElemIndex];
715 node.actElemStructure.clear();
716 for (var prop in actData) {
717 node.actElemStructure.fillListElement(prop, actData[prop]);
721 EventDispatcherService.dispatch(constants.EV_LIST_CHANGED, node.actElemStructure);
724 node.buildRequest = function (builder, req, module) {
727 var storeData = node.buildActElemData(),
728 labelWithModule = (module !== node.module ? node.module + ':' : '') + node.label;
730 if (node.actElemIndex > -1) {
731 if(node.filteredListData && node.filteredListData.length){
732 node.listData[node.listData.indexOf(node.filteredListData[node.actElemIndex])] = storeData;
733 node.filteredListData[node.actElemIndex] = storeData;
735 node.listData[node.actElemIndex] = storeData;
739 added = node.listData.filter(function (data) {
740 return $.isEmptyObject(data) === false;
743 var buildedDataCopy = node.listData.slice().map(function (item) {
745 for(var prop in item){
746 if(prop != '$$hashKey'){
747 newItem[prop] = item[prop];
751 }).filter(function(item){
752 return Object.keys(item).length !== 0;
755 // check of listElems keyValues duplicity
756 if(node.filteredListData && node.filteredListData.length){
757 node.doubleKeyIndexes = wrapper.checkKeyDuplicity(node.filteredListData, node.refKey);
759 node.doubleKeyIndexes = wrapper.checkKeyDuplicity(node.listData, node.refKey);
763 builder.insertPropertyToObj(req, labelWithModule, buildedDataCopy);
769 node.fill = function (name, array) { //data is array
771 var match = comparePropToElemByName(name, node.label);
773 if (match && array.length) {
774 node.createStructure();
775 node.listData = array.slice();
776 node.actElemIndex = node.listData.length - 1;
777 for (var prop in node.listData[node.actElemIndex]) {
778 node.actElemStructure.fillListElement(prop, node.listData[node.actElemIndex][prop]);
782 return (match && array.length > 0);
785 node.clear = function () {
786 while (node.listData.length > 0) {
789 while (node.filteredListData.length > 0) {
790 node.filteredListData.pop();
793 node.actElemIndex = -1;
794 node.actElemStructure = null;
795 node.nodeType = constants.NODE_UI_DISPLAY;
798 node.isFilled = function () {
799 return node.listData.length > 0;
802 node.createListName = function (index) {
807 if(node.filteredListData && node.filteredListData.length){
808 currentList = node.filteredListData;
810 currentList = node.listData;
814 node.actElemStructure.refKey.forEach(function (key) {
816 if(index === node.getActElemIndex()) {
817 val = key.value !== '' ? key.label + ':' + key.value : '';
820 if (!($.isEmptyObject(currentList[index]))) {
821 if(currentList[index][key.label]) {
823 } else if(currentList[index][key.module + ':' + key.label]) {
824 prop = key.module + ':' + key.label;
826 val = prop ? key.label + ':' + currentList[index][prop] : prop;
830 name = name ? (name + (val ? (' ' + val) : '')) : (name + (val ? (' <' + val) : ''));
841 node.getNewFilterElement = function (){
842 return node.getChildrenForFilter().map(function(element){
843 FilterNodeWrapperService.init(element);
844 var copy = element.deepCopyForFilter();
845 wrapper.wrapAll(copy);
846 FilterNodeWrapperService.wrapForFilter(copy);
851 _listElem: function (node) {
854 node.listElemBuildRequest = function (builder, req, module) {
856 objToAdd = builder.createObj();
858 node.getChildren(null, null, constants.NODE_UI_DISPLAY).forEach(function (child) {
859 var childAdded = child.buildRequest(builder, objToAdd, node.module);
860 added = added || childAdded;
864 builder.insertObjToList(req, objToAdd);
870 node.fillListElement = function (name, data) {
873 node.getChildren(null, null, constants.NODE_UI_DISPLAY).forEach(function (child) {
874 var childFilled = child.fill(name, data);
875 filled = filled || childFilled;
881 node.isFilled = function () {
882 return node.getChildren(null, null, constants.NODE_UI_DISPLAY).some(function (child) {
883 return child.isFilled();
887 node.clear = function () {
888 var nodesToClear = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
890 if (nodesToClear.length) {
891 nodesToClear.forEach(function (child) {
897 node.children.forEach(function (child) {
898 wrapper.wrapAll(child);
904 comparePropToElemByName: comparePropToElemByName,
905 equalArrays: equalArrays,
906 equalListElems: equalListElems,
907 parseRestrictText: parseRestrictText,
909 checkListElemKeys: checkListElemKeys
915 NodeWrapperService.$inject=['constants', 'RequestBuilderService', 'RestrictionsService', 'TypeWrapperService',
916 'ListFilteringService', 'EventDispatcherService', 'FilterNodeWrapperService'];
918 return NodeWrapperService;