Rework the javascript file of the UI (part3)
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / aOnBoot.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 //When element is first created it should have a red box because it hasn't been edited
25 function newElementProcessor(id) {
26     if ($('g[data-element-id="' + id + '"]').length > 0) {
27
28         var _idNode = $('g[data-element-id="' + id + '"]')
29         _idNode.children("rect").each(function() {
30             if ($(this).attr('class') === 'djs-outline') {
31                 $(this).attr('class', "djs-outline-no-property-saved")
32                 $(this).attr('fill', 'red')
33             }
34         });
35
36     }
37 }
38
39 function saveProperties(form) {
40     elementMap[lastElementSelected] = form;
41     totalJsonProperties = JSON.stringify(elementMap);
42     
43     //Take off the red border because the element has been edited
44     if ($('g[data-element-id="' + lastElementSelected + '"]').length > 0) {
45         var _idNode = $('g[data-element-id="' + lastElementSelected + '"]')
46         _idNode.children("rect").each(function() {
47             if ($(this).attr('class') === 'djs-outline-no-property-saved') {
48                 $(this).attr('class', "djs-outline")
49                 $(this).attr('fill', 'none')
50             }
51         });
52     }
53 }
54
55 function saveGlobalProperties(form) {
56     elementMap["global"] = form;
57 }
58 var isObject = function(a) {
59     return (!!a) && (a.constructor === Object);
60 };
61
62 function loadPropertyWindow(type) {
63     if (readOnly||readMOnly) {
64         if ($("#add_one_more").length == 1) {
65             $("#add_one_more").off();
66             $("#add_one_more").click(function(event) {
67                 event.preventDefault();
68             })
69         }
70         $("input,#savePropsBtn").attr("disabled", "");
71         $(".modal-body button").attr("disabled", "");
72                 ($("select:not([multiple])")).multiselect("disable");
73     }
74     
75     if (readTOnly){
76         $("textarea").attr("disabled","");
77         $("#savePropsBtn").attr("disabled", "");
78     }
79
80     var props = defaults_props[type];
81     
82     for (p in props) {
83         if (isObject(props[p])) {
84             var mySelect = $('#' + p);
85             if (p=="operator"){
86                 $.each(props[p], function(val, text) {
87                     mySelect.append(
88                         $('<option></option>').val(val).html(val)
89                     );
90                 });
91             } else {
92                 $.each(props[p], function(val, text) {
93                     mySelect.append(
94                         $('<option></option>').val(val).html(text)
95                     );
96                 });
97             }
98         } else {
99                 if (p=="pname"){
100                         var ms = new Date().getTime();
101                         props[p] = "Policy" + ms;
102                 }
103             $("#" + p).val(props[p])
104         }
105     }
106     setTimeout(function(){
107         setMultiSelect(type); }, 100);
108     
109     
110      
111 }
112
113 function setMultiSelect() {
114     $("select").each(function(index, mySelect) {
115         if ($(mySelect).parents(".multiselect-native-select") &&
116             $(mySelect).parents(".multiselect-native-select").length < 1) {
117             if (!$(mySelect).attr("multiple")) {
118                 if ($(mySelect).attr("enableFilter")) {
119                     $(mySelect).multiselect({
120                         numberDisplayed: 1,
121                         maxHeight: 200
122                     });
123                 } else {
124                     $(mySelect).multiselect({
125                         numberDisplayed: 1,
126                         enableFiltering: true,
127                         maxHeight: 200
128                     });
129                 }
130                 
131             } else {
132                 $(mySelect).multiselect({
133                     numberDisplayed: 1,
134                     includeSelectAllOption: true,
135                     enableFiltering: true,
136                     maxHeight: 200,
137                     enableCaseInsensitiveFiltering: true
138                 });
139             }
140
141         } else if ($(mySelect).parents(".multiselect-native-select") &&
142             $(mySelect).parents(".multiselect-native-select").length > 0) {
143             var selectDrop = $(mySelect).parent(".multiselect-native-select").find("select");
144             $(mySelect).parent(".multiselect-native-select").parent().html(selectDrop);
145             if (!$(mySelect).attr("multiple")) {
146                 if ($(mySelect).attr("enableFilter")) {
147                     $(mySelect).multiselect({
148                         numberDisplayed: 1,
149                         maxHeight: 200
150                     });
151                 } else {
152                     $(mySelect).multiselect({
153                         numberDisplayed: 1,
154                         enableFiltering: true,
155                         maxHeight: 200
156                     });
157                 }
158             } else {
159                 $(mySelect).multiselect({
160                     numberDisplayed: 1,
161                     includeSelectAllOption: true,
162                     enableFiltering: true,
163                     maxHeight: 200,
164                     enableCaseInsensitiveFiltering: true
165                 });
166             }
167         }
168     });
169     //refeshMultiSelect();
170 }
171 function loadSharedPropertyByService(onChangeUUID,refresh, callBack) {
172     var uuid = onChangeUUID;
173     if (uuid === undefined){
174         uuid = elementMap["global"] && elementMap["global"].length>0 ? 
175         elementMap["global"][0].value : "";
176     } else if (uuid === "") {
177         vf_Services = null
178         if ($("#vf").length > 0)
179             $("#vf").empty().multiselect("refresh");
180         if ($("#location").length > 0)
181             $("#location").empty().multiselect("refresh");
182         if ($("#alarmCondition").length > 0)
183             $("#alarmCondition").empty().multiselect("refresh");
184         return true;
185     }
186     var share = null,
187     serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid;
188     if(refresh){
189         serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid +'?refresh=true';
190     }
191
192     $.ajax({
193         async: false,
194         dataType: "json",
195         url: serviceUrl,
196         success: function(data) {
197             vf_Services = data;
198             setASDCFields()
199             if(refresh){
200              $("#paramsWarnrefresh").hide();   
201             }
202             if($("#paramsWarn")){
203                 $("#paramsWarn").hide();
204             }
205             if(callBack && _.isFunction(callBack)){
206                 callBack(true);
207             }
208         },
209         error: function(s, a, err) {
210             if(refresh){
211              $("#paramsWarnrefresh").show();   
212             }
213             if($("#paramsWarn")){
214                 $("#paramsWarn").show();    
215             }
216             
217             $('#servName').text($("#service option:selected").text());
218             if(callBack && _.isFunction(callBack)){
219                 callBack(false);
220             }
221             console.log(err)
222             console.log(s)
223             console.log(a)
224         },
225         timeout: 100000
226
227     });
228     
229     //vf_Services=share['shared']['byService'][uuid];
230     //location_values = share['global']['location'];
231 }
232
233 function loadSharedPropertyByServiceProperties(callBack) {
234     $.ajax({
235         async: false,
236         dataType: "json",
237         url: '/restservices/clds/v1/clds/properties/',
238         success: function(data) {
239             vf_Services = data;
240             setASDCFields();
241             if(callBack && _.isFunction(callBack)){
242                 callBack(true);
243             }
244         },
245         error: function(s, a, err) {
246             $('#servName').text($("#service option:selected").text());
247             if(callBack && _.isFunction(callBack)){
248                 callBack(false);
249             }
250         },
251         timeout: 100000
252
253     });
254 }
255
256 function setASDCFields() {
257     if (vf_Services === null || vf_Services === undefined) {
258         loadSharedPropertyByService()
259     } else {
260         try {
261             $("#vf").empty().multiselect("refresh");
262             $("#location").empty().multiselect("refresh");
263             $("#actionSet").empty().multiselect("refresh");
264             $("#vfc").empty().multiselect("refresh");
265             $("#paramsWarn").hide();
266             var uuid = Object.keys(vf_Services['shared']['byService'])[0];
267             
268             var vf_values = vf_Services['shared']['byService'][uuid] &&
269                 vf_Services['shared']['byService'][uuid]['vf'] &&
270                                 _.keys(vf_Services['shared']['byService'][uuid]['vf']).length > 0  ?
271                 vf_Services['shared']['byService'][uuid]['vf'] : null;
272                 
273             var selectedVF = {}
274             for (let e in elementMap["global"]) {
275                 if (elementMap['global'][e].name === "vf") {
276                     selectedVF = elementMap['global'][e].value[0]
277                 }
278             }
279             
280             var vfc_values2 = selectedVF &&
281                 vf_Services['shared']['byVf'][selectedVF] &&
282                 vf_Services['shared']['byVf'][selectedVF]['vfc'] &&
283                  _.keys(vf_Services['shared']['byVf'][selectedVF]['vfc']).length > 0 ?
284                 vf_Services['shared']['byVf'][selectedVF]['vfc'] : null;
285             
286             if (vf_values) {
287                 for (key in vf_values) {
288                     if ($("#vf").length > 0) {
289                         $("#vf").append("<option value=\"" + key + "\">" + vf_values[key] + "</opton>")
290                     }
291                 }
292                 $("#vf").multiselect("rebuild");
293             } 
294             
295             var location_values = vf_Services['global']['location'];
296             if (location_values) {
297                 for (key in location_values) {
298                     if ($("#location").length > 0) {
299                         $("#location").append("<option value=\"" + key + "\">" + location_values[key] + "</opton>")
300                     }
301                 }
302                 $("#location").multiselect("rebuild");
303             }    
304             
305             var actionSet_values = vf_Services['global']['actionSet'];
306             if (actionSet_values) {
307                 for (key in actionSet_values) {
308                     if ($("#actionSet").length > 0) {
309                         $("#actionSet").append("<option value=\"" + key + "\">" + actionSet_values[key] + "</opton>")
310                     }
311                 }
312                 $("#actionSet").multiselect("rebuild");
313             }  
314             
315             if (vfc_values2) {
316                 $("#vfc").append("<option value=\"\"></opton>");
317                 for (key in vfc_values2) {
318                     if ($("#vfc").length > 0) {
319                         $("#vfc").append("<option value=\"" + key.split("\"").join("") + "\">" + vfc_values2[key] + "</opton>")
320                     }
321                 }
322                 $("#vfc").multiselect("rebuild");
323             }
324             if($("#vfc").length > 0 && !vfc_values2){
325                                 showWarn();
326             }
327             if($("#vf").length > 0 && !vf_values){
328                         showWarn();     
329             }
330             if($("#location").length > 0 && !location_values){
331                 showWarn();
332             }
333
334             function showWarn() {
335                 $("#paramsWarn").show();
336                 $('#servName').text($("#service option:selected").text());
337             }
338         } catch (e) {
339             console.log(e)
340         }
341     }
342 }
343
344 //Typically used when opening a new model/template
345 function reloadDefaultVariables(isTemp) {
346     isTemplate = isTemp;
347     vf_Services = null;
348     readOnly = false;
349 }
350
351 $(window).load(function() {
352     $.ajax({
353         dataType: "json",
354         url: '/restservices/clds/v1/clds/properties',
355         success: function(data) {
356            
357             defaults_props = data;
358         },
359         error: function(s, a, err) {
360             console.log(err)
361             console.log(s)
362             console.log(a)
363         },
364         timeout: 100000
365     });
366 })