Removal of useless files & bugfix
[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);
108   }, 100);
109
110
111
112 }
113
114 function setMultiSelect() {
115     $("select").each(function(index, mySelect) {
116         if ($(mySelect).parents(".multiselect-native-select") &&
117             $(mySelect).parents(".multiselect-native-select").length < 1) {
118             if (!$(mySelect).attr("multiple")) {
119                 if ($(mySelect).attr("enableFilter")) {
120                     $(mySelect).multiselect({
121                         numberDisplayed: 1,
122                         maxHeight: 200
123                     });
124                 } else {
125                     $(mySelect).multiselect({
126                         numberDisplayed: 1,
127                         enableFiltering: true,
128                         maxHeight: 200
129                     });
130                 }
131                 
132             } else {
133                 $(mySelect).multiselect({
134                     numberDisplayed: 1,
135                     includeSelectAllOption: true,
136                     enableFiltering: true,
137                     maxHeight: 200,
138                     enableCaseInsensitiveFiltering: true
139                 });
140             }
141
142         } else if ($(mySelect).parents(".multiselect-native-select") &&
143             $(mySelect).parents(".multiselect-native-select").length > 0) {
144             var selectDrop = $(mySelect).parent(".multiselect-native-select").find("select");
145             $(mySelect).parent(".multiselect-native-select").parent().html(selectDrop);
146             if (!$(mySelect).attr("multiple")) {
147                 if ($(mySelect).attr("enableFilter")) {
148                     $(mySelect).multiselect({
149                         numberDisplayed: 1,
150                         maxHeight: 200
151                     });
152                 } else {
153                     $(mySelect).multiselect({
154                         numberDisplayed: 1,
155                         enableFiltering: true,
156                         maxHeight: 200
157                     });
158                 }
159             } else {
160                 $(mySelect).multiselect({
161                     numberDisplayed: 1,
162                     includeSelectAllOption: true,
163                     enableFiltering: true,
164                     maxHeight: 200,
165                     enableCaseInsensitiveFiltering: true
166                 });
167             }
168         }
169     });
170     //refeshMultiSelect();
171 }
172
173 function loadSharedPropertyByService(onChangeUUID, refresh, callBack) {
174   var uuid = onChangeUUID;
175   if (uuid === undefined) {
176     uuid = elementMap["global"] && elementMap["global"].length > 0 ?
177       elementMap["global"][0].value : "";
178   } else if (uuid === "") {
179     vf_Services = null
180     if ($("#vf").length > 0)
181       $("#vf").empty().multiselect("refresh");
182     if ($("#location").length > 0)
183       $("#location").empty().multiselect("refresh");
184     if ($("#alarmCondition").length > 0)
185       $("#alarmCondition").empty().multiselect("refresh");
186     return true;
187   }
188   var share = null,
189     serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid;
190   if (refresh) {
191     serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid + '?refresh=true';
192   }
193
194   $.ajax({
195     async: false,
196     dataType: "json",
197     url: serviceUrl,
198     success: function(data) {
199       vf_Services = data;
200       setASDCFields()
201       if (refresh) {
202         $("#paramsWarnrefresh").hide();
203       }
204       if ($("#paramsWarn")) {
205         $("#paramsWarn").hide();
206       }
207       if (callBack && _.isFunction(callBack)) {
208         callBack(true);
209       }
210     },
211     error: function(s, a, err) {
212       if (refresh) {
213         $("#paramsWarnrefresh").show();
214       }
215       if ($("#paramsWarn")) {
216         $("#paramsWarn").show();
217       }
218
219       $('#servName').text($("#service option:selected").text());
220       if (callBack && _.isFunction(callBack)) {
221         callBack(false);
222       }
223       console.log(err)
224       console.log(s)
225       console.log(a)
226     },
227     timeout: 100000
228
229   });
230
231   //vf_Services=share['shared']['byService'][uuid];
232   //location_values = share['global']['location'];
233 }
234
235 function loadSharedPropertyByServiceProperties(callBack) {
236   $.ajax({
237     async: false,
238     dataType: "json",
239     url: '/restservices/clds/v1/clds/properties/',
240     success: function(data) {
241       vf_Services = data;
242       setASDCFields();
243       if (callBack && _.isFunction(callBack)) {
244         callBack(true);
245       }
246     },
247     error: function(s, a, err) {
248       $('#servName').text($("#service option:selected").text());
249       if (callBack && _.isFunction(callBack)) {
250         callBack(false);
251       }
252     },
253     timeout: 100000
254
255   });
256 }
257
258 function setASDCFields() {
259   if (vf_Services === null || vf_Services === undefined) {
260     loadSharedPropertyByService()
261   } else {
262     try {
263       $("#vf").empty().multiselect("refresh");
264       $("#location").empty().multiselect("refresh");
265       $("#actionSet").empty().multiselect("refresh");
266       $("#vfc").empty().multiselect("refresh");
267       $("#paramsWarn").hide();
268       var uuid = Object.keys(vf_Services['shared']['byService'])[0];
269
270       var vf_values = vf_Services['shared']['byService'][uuid] &&
271         vf_Services['shared']['byService'][uuid]['vf'] &&
272         _.keys(vf_Services['shared']['byService'][uuid]['vf']).length > 0 ?
273         vf_Services['shared']['byService'][uuid]['vf'] : null;
274
275       var selectedVF = {}
276       for (let e in elementMap["global"]) {
277         if (elementMap['global'][e].name === "vf") {
278           selectedVF = elementMap['global'][e].value[0]
279         }
280       }
281
282       var vfc_values2 = selectedVF &&
283         vf_Services['shared']['byVf'][selectedVF] &&
284         vf_Services['shared']['byVf'][selectedVF]['vfc'] &&
285         _.keys(vf_Services['shared']['byVf'][selectedVF]['vfc']).length > 0 ?
286         vf_Services['shared']['byVf'][selectedVF]['vfc'] : null;
287
288       if (vf_values) {
289         for (key in vf_values) {
290           if ($("#vf").length > 0) {
291             $("#vf").append("<option value=\"" + key + "\">" + vf_values[key] + "</opton>")
292           }
293         }
294         $("#vf").multiselect("rebuild");
295       }
296
297       var location_values = vf_Services['global']['location'];
298       if (location_values) {
299         for (key in location_values) {
300           if ($("#location").length > 0) {
301             $("#location").append("<option value=\"" + key + "\">" + location_values[key] + "</opton>")
302           }
303         }
304         $("#location").multiselect("rebuild");
305       }
306
307       var actionSet_values = vf_Services['global']['actionSet'];
308       if (actionSet_values) {
309         for (key in actionSet_values) {
310           if ($("#actionSet").length > 0) {
311             $("#actionSet").append("<option value=\"" + key + "\">" + actionSet_values[key] + "</opton>")
312           }
313         }
314         $("#actionSet").multiselect("rebuild");
315       }
316
317       if (vfc_values2) {
318         $("#vfc").append("<option value=\"\"></opton>");
319         for (key in vfc_values2) {
320           if ($("#vfc").length > 0) {
321             $("#vfc").append("<option value=\"" + key.split("\"").join("") + "\">" + vfc_values2[key] + "</opton>")
322           }
323         }
324         $("#vfc").multiselect("rebuild");
325       }
326       if ($("#vfc").length > 0 && !vfc_values2) {
327         showWarn();
328       }
329       if ($("#vf").length > 0 && !vf_values) {
330         showWarn();
331       }
332       if ($("#location").length > 0 && !location_values) {
333         showWarn();
334       }
335
336       function showWarn() {
337         $("#paramsWarn").show();
338         $('#servName').text($("#service option:selected").text());
339       }
340     } catch (e) {
341       console.log(e)
342     }
343   }
344 }
345
346 //Typically used when opening a new model/template
347 function reloadDefaultVariables(isTemp) {
348   isTemplate = isTemp;
349   vf_Services = null;
350   readOnly = false;
351 }
352
353 $(window).load(function() {
354   $.ajax({
355     dataType: "json",
356     url: '/restservices/clds/v1/clds/properties',
357     success: function(data) {
358
359       defaults_props = data;
360     },
361     error: function(s, a, err) {
362       console.log(err)
363       console.log(s)
364       console.log(a)
365     },
366     timeout: 100000
367   });
368 })