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