ebc515ca184a9569580f7090bc137d14f269a097
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / mwtnCommons / mwtnCommons-module / src / main / resources / mwtnCommons / mwtnCommons.module.js
1 /*
2  * Copyright (c) 2016 highstreet technologies GmbH and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 define(['angularAMD',
10         'app/routingConfig',
11         'app/core/core.services',
12         'common/config/env.module',
13         'app/mwtnCommons/bower_components/angular-ui-grid/ui-grid.min',
14         'app/mwtnCommons/bower_components/chart.js/dist/Chart',
15         'app/mwtnCommons/bower_components/angular-chart.js/dist/angular-chart',
16         'app/mwtnCommons/bower_components/angular-clipboard/angular-clipboard',
17         'ui-bootstrap', 
18         'app/mwtnCommons/bower_components/json-formatter/dist/json-formatter.min'],
19         function(ng) {
20   var mwtnCommonsApp = angular.module('app.mwtnCommons', ['app.core','chart.js', 'ui.router.state', 'ui.grid', 'ui.bootstrap', 'jsonFormatter', 'config', 'pascalprecht.translate', 'angular-clipboard']);
21
22   mwtnCommonsApp.config(function($stateProvider, $compileProvider, $controllerProvider, $provide, NavHelperProvider, $httpProvider, $translateProvider, $translatePartialLoaderProvider) {
23     mwtnCommonsApp.register = {
24       controller : $controllerProvider.register,
25       directive : $compileProvider.directive,
26       factory : $provide.factory,
27       service : $provide.service
28     };
29
30     $translatePartialLoaderProvider.addPart('app/mwtnCommons/locale/locale');
31
32     var access = routingConfig.accessLevels;
33
34   });
35
36   mwtnCommonsApp.directive('htHeader', [ '$injector' ,function ($injector) {
37     return {
38        template: '<div class="ht-header"><div class="ht-header-right"><alarm-status ng-if="mwtnFaultExists"></alarm-status><help  ng-if="helpExists" link="{{helpLink}}" ></help></div></div>',
39       controller: ['$scope', function ($scope) {
40         $scope.helpExists = $injector.has('helpDirective');
41         $scope.mwtnFaultExists = $injector.has('alarmStatusDirective');
42       }],
43       restict: "EA",
44       replace: true,
45       scope: {
46         helpLink: "@"
47       }
48     };
49   }]);
50
51
52   return mwtnCommonsApp;
53 });
54
55
56 /*
57  Copyright 2011-2013 Abdulla Abdurakhmanov
58  Original sources are available at https://code.google.com/p/x2js/
59
60  Licensed under the Apache License, Version 2.0 (the "License");
61  you may not use this file except in compliance with the License.
62  You may obtain a copy of the License at
63
64  http://www.apache.org/licenses/LICENSE-2.0
65
66  Unless required by applicable law or agreed to in writing, software
67  distributed under the License is distributed on an "AS IS" BASIS,
68  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
69  See the License for the specific language governing permissions and
70  limitations under the License.
71  */
72
73 function X2JS(config) {
74         'use strict';
75                 
76         var VERSION = "1.1.5";
77         
78         config = config || {};
79         initConfigDefaults();
80         initRequiredPolyfills();
81         
82         function initConfigDefaults() {
83                 if(config.escapeMode === undefined) {
84                         config.escapeMode = true;
85                 }
86                 config.attributePrefix = config.attributePrefix || "_";
87                 config.arrayAccessForm = config.arrayAccessForm || "none";
88                 config.emptyNodeForm = config.emptyNodeForm || "text";
89                 if(config.enableToStringFunc === undefined) {
90                         config.enableToStringFunc = true; 
91                 }
92                 config.arrayAccessFormPaths = config.arrayAccessFormPaths || []; 
93                 if(config.skipEmptyTextNodesForObj === undefined) {
94                         config.skipEmptyTextNodesForObj = true;
95                 }
96                 if(config.stripWhitespaces === undefined) {
97                         config.stripWhitespaces = true;
98                 }
99                 config.datetimeAccessFormPaths = config.datetimeAccessFormPaths || [];
100         }
101
102         var DOMNodeTypes = {
103                 ELEMENT_NODE       : 1,
104                 TEXT_NODE          : 3,
105                 CDATA_SECTION_NODE : 4,
106                 COMMENT_NODE       : 8,
107                 DOCUMENT_NODE      : 9
108         };
109         
110         function initRequiredPolyfills() {
111                 function pad(number) {
112               var r = String(number);
113               if ( r.length === 1 ) {
114                 r = '0' + r;
115               }
116               return r;
117             }
118                 // Hello IE8-
119                 if(typeof String.prototype.trim !== 'function') {                       
120                         String.prototype.trim = function() {
121                                 return this.replace(/^\s+|^\n+|(\s|\n)+$/g, '');
122                         }
123                 }
124                 if(typeof Date.prototype.toISOString !== 'function') {
125                         // Implementation from http://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript
126                         Date.prototype.toISOString = function() {
127                       return this.getUTCFullYear()
128                         + '-' + pad( this.getUTCMonth() + 1 )
129                         + '-' + pad( this.getUTCDate() )
130                         + 'T' + pad( this.getUTCHours() )
131                         + ':' + pad( this.getUTCMinutes() )
132                         + ':' + pad( this.getUTCSeconds() )
133                         + '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
134                         + 'Z';
135                     };
136                 }
137         }
138         
139         function getNodeLocalName( node ) {
140                 var nodeLocalName = node.localName;                     
141                 if(nodeLocalName == null) // Yeah, this is IE!! 
142                         nodeLocalName = node.baseName;
143                 if(nodeLocalName == null || nodeLocalName=="") // =="" is IE too
144                         nodeLocalName = node.nodeName;
145                 return nodeLocalName;
146         }
147         
148         function getNodePrefix(node) {
149                 return node.prefix;
150         }
151                 
152         function escapeXmlChars(str) {
153                 if(typeof(str) == "string")
154                         return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g, '&#x2F;');
155                 else
156                         return str;
157         }
158
159         function unescapeXmlChars(str) {
160                 return str.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#x27;/g, "'").replace(/&#x2F;/g, '\/');
161         }
162         
163         function toArrayAccessForm(obj, childName, path) {
164                 switch(config.arrayAccessForm) {
165                 case "property":
166                         if(!(obj[childName] instanceof Array))
167                                 obj[childName+"_asArray"] = [obj[childName]];
168                         else
169                                 obj[childName+"_asArray"] = obj[childName];
170                         break;          
171                 /*case "none":
172                         break;*/
173                 }
174                 
175                 if(!(obj[childName] instanceof Array) && config.arrayAccessFormPaths.length > 0) {
176                         var idx = 0;
177                         for(; idx < config.arrayAccessFormPaths.length; idx++) {
178                                 var arrayPath = config.arrayAccessFormPaths[idx];
179                                 if( typeof arrayPath === "string" ) {
180                                         if(arrayPath == path)
181                                                 break;
182                                 }
183                                 else
184                                 if( arrayPath instanceof RegExp) {
185                                         if(arrayPath.test(path))
186                                                 break;
187                                 }                               
188                                 else
189                                 if( typeof arrayPath === "function") {
190                                         if(arrayPath(obj, childName, path))
191                                                 break;
192                                 }
193                         }
194                         if(idx!=config.arrayAccessFormPaths.length) {
195                                 obj[childName] = [obj[childName]];
196                         }
197                 }
198         }
199         
200         function fromXmlDateTime(prop) {
201                 // Implementation based up on http://stackoverflow.com/questions/8178598/xml-datetime-to-javascript-date-object
202                 // Improved to support full spec and optional parts
203                 var bits = prop.split(/[-T:+Z]/g);
204                 
205                 var d = new Date(bits[0], bits[1]-1, bits[2]);                  
206                 var secondBits = bits[5].split("\.");
207                 d.setHours(bits[3], bits[4], secondBits[0]);
208                 if(secondBits.length>1)
209                         d.setMilliseconds(secondBits[1]);
210
211                 // Get supplied time zone offset in minutes
212                 if(bits[6] && bits[7]) {
213                         var offsetMinutes = bits[6] * 60 + Number(bits[7]);
214                         var sign = /\d\d-\d\d:\d\d$/.test(prop)? '-' : '+';
215
216                         // Apply the sign
217                         offsetMinutes = 0 + (sign == '-'? -1 * offsetMinutes : offsetMinutes);
218
219                         // Apply offset and local timezone
220                         d.setMinutes(d.getMinutes() - offsetMinutes - d.getTimezoneOffset())
221                 }
222                 else
223                         if(prop.indexOf("Z", prop.length - 1) !== -1) {
224                                 d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()));                                  
225                         }
226
227                 // d is now a local time equivalent to the supplied time
228                 return d;
229         }
230         
231         function checkFromXmlDateTimePaths(value, childName, fullPath) {
232                 if(config.datetimeAccessFormPaths.length > 0) {
233                         var path = fullPath.split("\.#")[0];
234                         var idx = 0;
235                         for(; idx < config.datetimeAccessFormPaths.length; idx++) {
236                                 var dtPath = config.datetimeAccessFormPaths[idx];
237                                 if( typeof dtPath === "string" ) {
238                                         if(dtPath == path)
239                                                 break;
240                                 }
241                                 else
242                                 if( dtPath instanceof RegExp) {
243                                         if(dtPath.test(path))
244                                                 break;
245                                 }                               
246                                 else
247                                 if( typeof dtPath === "function") {
248                                         if(dtPath(obj, childName, path))
249                                                 break;
250                                 }
251                         }
252                         if(idx!=config.datetimeAccessFormPaths.length) {
253                                 return fromXmlDateTime(value);
254                         }
255                         else
256                                 return value;
257                 }
258                 else
259                         return value;
260         }
261
262         function parseDOMChildren( node, path ) {
263                 if(node.nodeType == DOMNodeTypes.DOCUMENT_NODE) {
264                         var result = new Object;
265                         var nodeChildren = node.childNodes;
266                         // Alternative for firstElementChild which is not supported in some environments
267                         for(var cidx=0; cidx <nodeChildren.length; cidx++) {
268                                 var child = nodeChildren.item(cidx);
269                                 if(child.nodeType == DOMNodeTypes.ELEMENT_NODE) {
270                                         var childName = getNodeLocalName(child);
271                                         result[childName] = parseDOMChildren(child, childName);
272                                 }
273                         }
274                         return result;
275                 }
276                 else
277                 if(node.nodeType == DOMNodeTypes.ELEMENT_NODE) {
278                         var result = new Object;
279                         result.__cnt=0;
280                         
281                         var nodeChildren = node.childNodes;
282                         
283                         // Children nodes
284                         for(var cidx=0; cidx <nodeChildren.length; cidx++) {
285                                 var child = nodeChildren.item(cidx); // nodeChildren[cidx];
286                                 var childName = getNodeLocalName(child);
287                                 
288                                 if(child.nodeType!= DOMNodeTypes.COMMENT_NODE) {
289                                         result.__cnt++;
290                                         if(result[childName] == null) {
291                                                 result[childName] = parseDOMChildren(child, path+"."+childName);
292                                                 toArrayAccessForm(result, childName, path+"."+childName);                                       
293                                         }
294                                         else {
295                                                 if(result[childName] != null) {
296                                                         if( !(result[childName] instanceof Array)) {
297                                                                 result[childName] = [result[childName]];
298                                                                 toArrayAccessForm(result, childName, path+"."+childName);
299                                                         }
300                                                 }
301                                                 (result[childName])[result[childName].length] = parseDOMChildren(child, path+"."+childName);
302                                         }
303                                 }                                                               
304                         }
305                         
306                         // Attributes
307                         for(var aidx=0; aidx <node.attributes.length; aidx++) {
308                                 var attr = node.attributes.item(aidx); // [aidx];
309                                 result.__cnt++;
310                                 result[config.attributePrefix+attr.name]=attr.value;
311                         }
312                         
313                         // Node namespace prefix
314                         var nodePrefix = getNodePrefix(node);
315                         if(nodePrefix!=null && nodePrefix!="") {
316                                 result.__cnt++;
317                                 result.__prefix=nodePrefix;
318                         }
319                         
320                         if(result["#text"]!=null) {                             
321                                 result.__text = result["#text"];
322                                 if(result.__text instanceof Array) {
323                                         result.__text = result.__text.join("\n");
324                                 }
325                                 if(config.escapeMode)
326                                         result.__text = unescapeXmlChars(result.__text);
327                                 if(config.stripWhitespaces)
328                                         result.__text = result.__text.trim();
329                                 delete result["#text"];
330                                 if(config.arrayAccessForm=="property")
331                                         delete result["#text_asArray"];
332                                 result.__text = checkFromXmlDateTimePaths(result.__text, childName, path+"."+childName);
333                         }
334                         if(result["#cdata-section"]!=null) {
335                                 result.__cdata = result["#cdata-section"];
336                                 delete result["#cdata-section"];
337                                 if(config.arrayAccessForm=="property")
338                                         delete result["#cdata-section_asArray"];
339                         }
340                         
341                         if( result.__cnt == 1 && result.__text!=null  ) {
342                                 result = result.__text;
343                         }
344                         else
345                         if( result.__cnt == 0 && config.emptyNodeForm=="text" ) {
346                                 result = '';
347                         }
348                         else
349                         if ( result.__cnt > 1 && result.__text!=null && config.skipEmptyTextNodesForObj) {
350                                 if( (config.stripWhitespaces && result.__text=="") || (result.__text.trim()=="")) {
351                                         delete result.__text;
352                                 }
353                         }
354                         delete result.__cnt;                    
355                         
356                         if( config.enableToStringFunc && (result.__text!=null || result.__cdata!=null )) {
357                                 result.toString = function() {
358                                         return (this.__text!=null? this.__text:'')+( this.__cdata!=null ? this.__cdata:'');
359                                 };
360                         }
361                         
362                         return result;
363                 }
364                 else
365                 if(node.nodeType == DOMNodeTypes.TEXT_NODE || node.nodeType == DOMNodeTypes.CDATA_SECTION_NODE) {
366                         return node.nodeValue;
367                 }       
368         }
369         
370         function startTag(jsonObj, element, attrList, closed) {
371                 var resultStr = "<"+ ( (jsonObj!=null && jsonObj.__prefix!=null)? (jsonObj.__prefix+":"):"") + element;
372                 if(attrList!=null) {
373                         for(var aidx = 0; aidx < attrList.length; aidx++) {
374                                 var attrName = attrList[aidx];
375                                 var attrVal = jsonObj[attrName];
376                                 if(config.escapeMode)
377                                         attrVal=escapeXmlChars(attrVal);
378                                 resultStr+=" "+attrName.substr(config.attributePrefix.length)+"='"+attrVal+"'";
379                         }
380                 }
381                 if(!closed)
382                         resultStr+=">";
383                 else
384                         resultStr+="/>";
385                 return resultStr;
386         }
387         
388         function endTag(jsonObj,elementName) {
389                 return "</"+ (jsonObj.__prefix!=null? (jsonObj.__prefix+":"):"")+elementName+">";
390         }
391         
392         function endsWith(str, suffix) {
393             return str.indexOf(suffix, str.length - suffix.length) !== -1;
394         }
395         
396         function jsonXmlSpecialElem ( jsonObj, jsonObjField ) {
397                 if((config.arrayAccessForm=="property" && endsWith(jsonObjField.toString(),("_asArray"))) 
398                                 || jsonObjField.toString().indexOf(config.attributePrefix)==0 
399                                 || jsonObjField.toString().indexOf("__")==0
400                                 || (jsonObj[jsonObjField] instanceof Function) )
401                         return true;
402                 else
403                         return false;
404         }
405         
406         function jsonXmlElemCount ( jsonObj ) {
407                 var elementsCnt = 0;
408                 if(jsonObj instanceof Object ) {
409                         for( var it in jsonObj  ) {
410                                 if(jsonXmlSpecialElem ( jsonObj, it) )
411                                         continue;                       
412                                 elementsCnt++;
413                         }
414                 }
415                 return elementsCnt;
416         }
417         
418         function parseJSONAttributes ( jsonObj ) {
419                 var attrList = [];
420                 if(jsonObj instanceof Object ) {
421                         for( var ait in jsonObj  ) {
422                                 if(ait.toString().indexOf("__")== -1 && ait.toString().indexOf(config.attributePrefix)==0) {
423                                         attrList.push(ait);
424                                 }
425                         }
426                 }
427                 return attrList;
428         }
429         
430         function parseJSONTextAttrs ( jsonTxtObj ) {
431                 var result ="";
432                 
433                 if(jsonTxtObj.__cdata!=null) {                                                                          
434                         result+="<![CDATA["+jsonTxtObj.__cdata+"]]>";                                   
435                 }
436                 
437                 if(jsonTxtObj.__text!=null) {                   
438                         if(config.escapeMode)
439                                 result+=escapeXmlChars(jsonTxtObj.__text);
440                         else
441                                 result+=jsonTxtObj.__text;
442                 }
443                 return result;
444         }
445         
446         function parseJSONTextObject ( jsonTxtObj ) {
447                 var result ="";
448
449                 if( jsonTxtObj instanceof Object ) {
450                         result+=parseJSONTextAttrs ( jsonTxtObj );
451                 }
452                 else
453                         if(jsonTxtObj!=null) {
454                                 if(config.escapeMode)
455                                         result+=escapeXmlChars(jsonTxtObj);
456                                 else
457                                         result+=jsonTxtObj;
458                         }
459                 
460                 return result;
461         }
462         
463         function parseJSONArray ( jsonArrRoot, jsonArrObj, attrList ) {
464                 var result = ""; 
465                 if(jsonArrRoot.length == 0) {
466                         result+=startTag(jsonArrRoot, jsonArrObj, attrList, true);
467                 }
468                 else {
469                         for(var arIdx = 0; arIdx < jsonArrRoot.length; arIdx++) {
470                                 result+=startTag(jsonArrRoot[arIdx], jsonArrObj, parseJSONAttributes(jsonArrRoot[arIdx]), false);
471                                 result+=parseJSONObject(jsonArrRoot[arIdx]);
472                                 result+=endTag(jsonArrRoot[arIdx],jsonArrObj);                                          
473                         }
474                 }
475                 return result;
476         }
477         
478         function parseJSONObject ( jsonObj ) {
479                 var result = "";        
480
481                 var elementsCnt = jsonXmlElemCount ( jsonObj );
482                 
483                 if(elementsCnt > 0) {
484                         for( var it in jsonObj ) {
485                                 
486                                 if(jsonXmlSpecialElem ( jsonObj, it) )
487                                         continue;                       
488                                 
489                                 var subObj = jsonObj[it];                                               
490                                 
491                                 var attrList = parseJSONAttributes( subObj )
492                                 
493                                 if(subObj == null || subObj == undefined) {
494                                         result+=startTag(subObj, it, attrList, true);
495                                 }
496                                 else
497                                 if(subObj instanceof Object) {
498                                         
499                                         if(subObj instanceof Array) {                                   
500                                                 result+=parseJSONArray( subObj, it, attrList );                                 
501                                         }
502                                         else if(subObj instanceof Date) {
503                                                 result+=startTag(subObj, it, attrList, false);
504                                                 result+=subObj.toISOString();
505                                                 result+=endTag(subObj,it);
506                                         }
507                                         else {
508                                                 var subObjElementsCnt = jsonXmlElemCount ( subObj );
509                                                 if(subObjElementsCnt > 0 || subObj.__text!=null || subObj.__cdata!=null) {
510                                                         result+=startTag(subObj, it, attrList, false);
511                                                         result+=parseJSONObject(subObj);
512                                                         result+=endTag(subObj,it);
513                                                 }
514                                                 else {
515                                                         result+=startTag(subObj, it, attrList, true);
516                                                 }
517                                         }
518                                 }
519                                 else {
520                                         result+=startTag(subObj, it, attrList, false);
521                                         result+=parseJSONTextObject(subObj);
522                                         result+=endTag(subObj,it);
523                                 }
524                         }
525                 }
526                 result+=parseJSONTextObject(jsonObj);
527                 
528                 return result;
529         }
530         
531         this.parseXmlString = function(xmlDocStr) {
532                 var isIEParser = window.ActiveXObject || "ActiveXObject" in window;
533                 if (xmlDocStr === undefined) {
534                         return null;
535                 }
536                 var xmlDoc;
537                 if (window.DOMParser) {
538                         var parser=new window.DOMParser();                      
539                         var parsererrorNS = null;
540                         // IE9+ now is here
541                         if(!isIEParser) {
542                                 try {
543                                         parsererrorNS = parser.parseFromString("INVALID", "text/xml").childNodes[0].namespaceURI;
544                                 }
545                                 catch(err) {                                    
546                                         parsererrorNS = null;
547                                 }
548                         }
549                         try {
550                                 xmlDoc = parser.parseFromString( xmlDocStr, "text/xml" );
551                                 if( parsererrorNS!= null && xmlDoc.getElementsByTagNameNS(parsererrorNS, "parsererror").length > 0) {
552                                         //throw new Error('Error parsing XML: '+xmlDocStr);
553                                         xmlDoc = null;
554                                 }
555                         }
556                         catch(err) {
557                                 xmlDoc = null;
558                         }
559                 }
560                 else {
561                         // IE :(
562                         if(xmlDocStr.indexOf("<?")==0) {
563                                 xmlDocStr = xmlDocStr.substr( xmlDocStr.indexOf("?>") + 2 );
564                         }
565                         xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
566                         xmlDoc.async="false";
567                         xmlDoc.loadXML(xmlDocStr);
568                 }
569                 return xmlDoc;
570         };
571         
572         this.asArray = function(prop) {
573                 if(prop instanceof Array)
574                         return prop;
575                 else
576                         return [prop];
577         };
578         
579         this.toXmlDateTime = function(dt) {
580                 if(dt instanceof Date)
581                         return dt.toISOString();
582                 else
583                 if(typeof(dt) === 'number' )
584                         return new Date(dt).toISOString();
585                 else    
586                         return null;
587         };
588         
589         this.asDateTime = function(prop) {
590                 if(typeof(prop) == "string") {
591                         return fromXmlDateTime(prop);
592                 }
593                 else
594                         return prop;
595         };
596
597         this.xml2json = function (xmlDoc) {
598                 return parseDOMChildren ( xmlDoc );
599         };
600         
601         this.xml_str2json = function (xmlDocStr) {
602                 var xmlDoc = this.parseXmlString(xmlDocStr);
603                 if(xmlDoc!=null)
604                         return this.xml2json(xmlDoc);
605                 else
606                         return null;
607         };
608
609         this.json2xml_str = function (jsonObj) {
610                 return parseJSONObject ( jsonObj );
611         };
612
613         this.json2xml = function (jsonObj) {
614                 var xmlDocStr = this.json2xml_str (jsonObj);
615                 return this.parseXmlString(xmlDocStr);
616         };
617         
618         this.getVersion = function () {
619                 return VERSION;
620         };
621         
622 }