CLIENT GUI Framework
[vnfsdk/refrepo.git] / openo-portal / portal-catalog / src / main / webapp / catalog / js / component / commonUtil.js
1 /*\r
2  * Copyright 2016-2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 var commonUtil = {};\r
17 commonUtil.arrayRemove = function( aryInstance , index ){\r
18     if(aryInstance == undefined || aryInstance == null){\r
19         return;\r
20     }\r
21     for(var i=0,n=0;i<aryInstance.length;i++) {\r
22         if(aryInstance[i]!=aryInstance[dx]) {\r
23             aryInstance[n++]=aryInstance[i];\r
24         }\r
25     }\r
26     aryInstance.length-=1;\r
27 };\r
28 \r
29 //For the expansion of the Date, convert the Date to specify the format String\r
30 // examples:\r
31 // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423\r
32 // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18\r
33 commonUtil.parseDate = function( dateObj , format){\r
34     var o = {\r
35         "M+" : dateObj.getMonth()+1, //month\r
36         "d+" : dateObj.getDate(),    //day\r
37         "h+" : dateObj.getHours(),   //hour\r
38         "m+" : dateObj.getMinutes(), //minute\r
39         "s+" : dateObj.getSeconds(), //second\r
40         "q+" : Math.floor((dateObj.getMonth()+3)/3),  //quarter\r
41         "S" : dateObj.getMilliseconds() //millisecond\r
42     }\r
43     if(/(y+)/.test(format)) format=format.replace(RegExp.$1,\r
44         (dateObj.getFullYear()+"").substr(4 - RegExp.$1.length));\r
45     for(var k in o)\r
46         if(new RegExp("("+ k +")").test(format))\r
47             format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] :\r
48                     ("00"+ o[k]).substr((""+ o[k]).length));\r
49     return format;\r
50 };\r
51 \r
52 //tooltip\r
53 commonUtil.showMessage = function(message, type) {\r
54     $.growl({\r
55         icon: "fa fa-envelope-o fa-lg",\r
56         title: "&nbsp;&nbsp;" + $.i18n.prop("nfv-nso-iui-common-tip"),\r
57         message: message\r
58     },{\r
59         type: type\r
60     });\r
61 };\r
62 \r
63 commonUtil.registerCometdMessage = function(url, channel, callback) {\r
64     var cometd = new $.Cometd();\r
65     var cometdURL = location.protocol + "//" + location.host + url;\r
66     cometd.configure({\r
67         url : cometdURL,\r
68         logLevel : "info"\r
69     });\r
70     // unregister websocket transport, use long-polling transport\r
71     cometd.unregisterTransport('websocket');\r
72     // store channel object parameters(this object include channel and callback function), start from arguments[1]\r
73     var _args = arguments;  \r
74 \r
75     cometd.addListener("/meta/handshake", function(handshake){\r
76         if(handshake.successful === true) {\r
77             cometd.batch(function() {\r
78                 //subscribe channel\r
79                 cometd.subscribe(channel, function(message){\r
80                     callback.call(this, message.data);\r
81                 });\r
82             });\r
83         }\r
84     });\r
85     cometd.handshake();\r
86 }\r
87 \r
88 commonUtil.format = function() {\r
89     if(arguments.length == 0) {\r
90         return null;\r
91     }\r
92     var str = arguments[0];\r
93     for(var i=0; i<arguments.length; i++) {\r
94         var reg = new RegExp("\\{" + (i - 1) + "\\}" , "gm");\r
95         str = str.replace(reg, arguments[i]);\r
96     }\r
97     return str;\r
98 }\r
99 \r
100 commonUtil.get = function(url, params, callback) {\r
101     $.ajax({\r
102         type : "GET",\r
103         url : url,\r
104         //contentType : contentType || "application/x-www-form-urlencoded; charset=UTF-8",\r
105         dataType : "json",\r
106         data : params || {},\r
107         success : callback\r
108     });\r
109 }\r
110 \r
111 commonUtil.post = function(url, params, callback, contentType) {\r
112     $.ajax({\r
113         type : "POST",\r
114         url : url,\r
115         contentType : contentType || "application/x-www-form-urlencoded; charset=UTF-8",\r
116         data : params || {},\r
117         success : callback\r
118     });\r
119 }\r
120 \r
121 commonUtil.delete = function(url, callback, contentType) {\r
122     $.ajax({\r
123         type : "DELETE",\r
124         url : url,\r
125         contentType : contentType || "application/x-www-form-urlencoded; charset=UTF-8",\r
126         success : callback\r
127     });\r
128 }