b69cfea08e2a7e50195d7e17e0bb47d0a05ace1b
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / aOnBoot.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * 
22  */
23
24
25 //When element is first created it should have a red box because it hasn't been edited
26 function newElementProcessor(id) {
27   if ($('g[data-element-id="' + id + '"]').length > 0) {
28
29     var _idNode = $('g[data-element-id="' + id + '"]')
30     _idNode.children("rect").each(function() {
31       if ($(this).attr('class') === 'djs-outline') {
32         $(this).attr('class', "djs-outline-no-property-saved")
33         $(this).attr('fill', 'red')
34       }
35     });
36
37   }
38 }
39
40 function saveProperties(form) {
41   elementMap[lastElementSelected] = form;
42   totalJsonProperties = JSON.stringify(elementMap);
43
44   //Take off the red border because the element has been edited
45   if ($('g[data-element-id="' + lastElementSelected + '"]').length > 0) {
46     var _idNode = $('g[data-element-id="' + lastElementSelected + '"]')
47     _idNode.children("rect").each(function() {
48       if ($(this).attr('class') === 'djs-outline-no-property-saved') {
49         $(this).attr('class', "djs-outline")
50         $(this).attr('fill', 'none')
51       }
52     });
53   }
54 }
55
56 function saveGlobalProperties(form) {
57   elementMap["global"] = form;
58 }
59 var isObject = function(a) {
60   return (!!a) && (a.constructor === Object);
61 };
62
63 function loadPropertyWindow(type) {
64   if (readMOnly) {
65     if ($("#add_one_more").length == 1) {
66       $("#add_one_more").off();
67       $("#add_one_more").click(function(event) {
68         event.preventDefault();
69       })
70     }
71     $("input,#savePropsBtn").attr("disabled", "");
72     $(".modal-body button").attr("disabled", "");
73     ($("select:not([multiple])")).multiselect("disable");
74   }
75
76   var props = defaults_props[type];
77
78   for (p in props) {
79     if (isObject(props[p])) {
80       var mySelect = $('#' + p);
81       if (p == "operator") {
82         $.each(props[p], function(val, text) {
83           mySelect.append(
84             $('<option></option>').val(val).html(val)
85           );
86         });
87       } else {
88         $.each(props[p], function(val, text) {
89           mySelect.append(
90             $('<option></option>').val(val).html(text)
91           );
92         });
93       }
94     } else {
95       if (p == "pname") {
96         var ms = new Date().getTime();
97         props[p] = "Policy" + ms;
98       }
99       $("#" + p).val(props[p])
100     }
101   }
102   setTimeout(function() {
103     setMultiSelect(type);
104   }, 100);
105
106
107
108 }
109
110 function setMultiSelect() {
111   $("select").each(function(index, mySelect) {
112
113     var mySelectObj = $(mySelect);
114     if (! mySelectObj.parents(".multiselect-native-select")) {
115       // keep native for this one
116       return;
117     }
118
119     if (mySelectObj.parents(".multiselect-native-select").length > 0) {
120       var selectDrop = mySelectObj.parent(".multiselect-native-select").find("select");
121       mySelectObj.parent(".multiselect-native-select").parent().html(selectDrop);
122     }
123
124     var options = {
125       numberDisplayed: 1,
126       maxHeight: 200
127     };
128
129     if (mySelectObj.attr("multiple")
130         && mySelectObj.attr("multiple") != 'false') {
131       options.includeSelectAllOption = true;
132     }
133     
134     if (mySelectObj.attr("enableFilter")
135         && mySelectObj.attr("enableFilter") != 'false') {
136       options.enableCaseInsensitiveFiltering = true;
137       options.enableFiltering = true;
138     }
139
140     mySelectObj.multiselect(options);
141   });
142 }
143
144 function loadSharedPropertyByService(onChangeUUID, refresh, callBack) {
145   var uuid = onChangeUUID;
146   if (uuid === undefined) {
147     uuid = elementMap["global"] && elementMap["global"].length > 0 ?
148       elementMap["global"][0].value : "";
149   }
150   var share = null,
151     serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid;
152   if (refresh) {
153     serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid + '?refresh=true';
154   }
155
156   $.ajax({
157     async: false,
158     dataType: "json",
159     url: serviceUrl,
160     success: function(data) {
161       vf_Services = JSON.parse(data);
162       setASDCFields()
163       if (refresh) {
164         $("#paramsWarnrefresh").hide();
165       }
166       if ($("#paramsWarn")) {
167         $("#paramsWarn").hide();
168       }
169       if (callBack && _.isFunction(callBack)) {
170         callBack(true);
171       }
172     },
173     error: function(s, a, err) {
174       if (refresh) {
175         $("#paramsWarnrefresh").show();
176       }
177       if ($("#paramsWarn")) {
178         $("#paramsWarn").show();
179       }
180
181       $('#servName').text($("#service option:selected").text());
182       if (callBack && _.isFunction(callBack)) {
183         callBack(false);
184       }
185       console.log(err)
186       console.log(s)
187       console.log(a)
188     },
189     timeout: 100000
190
191   });
192
193   //vf_Services=share['shared']['byService'][uuid];
194   //location_values = share['global']['location'];
195 }
196
197 function loadSharedPropertyByServiceProperties(callBack) {
198   $.ajax({
199     async: false,
200     dataType: "json",
201     url: '/restservices/clds/v1/clds/properties/',
202     success: function(data) {
203       vf_Services = JSON.parse(data);
204       setASDCFields();
205       if (callBack && _.isFunction(callBack)) {
206         callBack(true);
207       }
208     },
209     error: function(s, a, err) {
210       $('#servName').text($("#service option:selected").text());
211       if (callBack && _.isFunction(callBack)) {
212         callBack(false);
213       }
214     },
215     timeout: 100000
216
217   });
218 }
219
220 function setASDCFields() {
221   if (vf_Services === null || vf_Services === undefined) {
222     loadSharedPropertyByService()
223   } else {
224     try {
225       $("#vf").empty().multiselect("refresh");
226       $("#location").empty().multiselect("refresh");
227       $("#actionSet").empty().multiselect("refresh");
228       $("#vfc").empty().multiselect("refresh");
229       $("#paramsWarn").hide();
230       var uuid = Object.keys(vf_Services['shared']['byService'])[0];
231
232       var vf_values = vf_Services['shared']['byService'][uuid] &&
233         vf_Services['shared']['byService'][uuid]['vf'] &&
234         _.keys(vf_Services['shared']['byService'][uuid]['vf']).length > 0 ?
235         vf_Services['shared']['byService'][uuid]['vf'] : null;
236
237       var selectedVF = {}
238       for (let e in elementMap["global"]) {
239         if (elementMap['global'][e].name === "vf") {
240           selectedVF = elementMap['global'][e].value[0]
241         }
242       }
243
244       var vfc_values2 = selectedVF &&
245         vf_Services['shared']['byVf'][selectedVF] &&
246         vf_Services['shared']['byVf'][selectedVF]['vfc'] &&
247         _.keys(vf_Services['shared']['byVf'][selectedVF]['vfc']).length > 0 ?
248         vf_Services['shared']['byVf'][selectedVF]['vfc'] : null;
249
250       if (vf_values) {
251         for (key in vf_values) {
252           if ($("#vf").length > 0) {
253             $("#vf").append("<option value=\"" + key + "\">" + vf_values[key] + "</opton>")
254           }
255         }
256         $("#vf").multiselect("rebuild");
257       }
258
259       var location_values = vf_Services['global']['location'];
260       if (location_values) {
261         for (key in location_values) {
262           if ($("#location").length > 0) {
263             $("#location").append("<option value=\"" + key + "\">" + location_values[key] + "</opton>")
264           }
265         }
266         $("#location").multiselect("rebuild");
267       }
268
269       var actionSet_values = vf_Services['global']['actionSet'];
270       if (actionSet_values) {
271         for (key in actionSet_values) {
272           if ($("#actionSet").length > 0) {
273             $("#actionSet").append("<option value=\"" + key + "\">" + actionSet_values[key] + "</opton>")
274           }
275         }
276         $("#actionSet").multiselect("rebuild");
277       }
278
279       if (vfc_values2) {
280         $("#vfc").append("<option value=\"\"></opton>");
281         for (key in vfc_values2) {
282           if ($("#vfc").length > 0) {
283             $("#vfc").append("<option value=\"" + key.split("\"").join("") + "\">" + vfc_values2[key] + "</opton>")
284           }
285         }
286         $("#vfc").multiselect("rebuild");
287       }
288       if ($("#vfc").length > 0 && !vfc_values2) {
289         showWarn();
290       }
291       if ($("#vf").length > 0 && !vf_values) {
292         showWarn();
293       }
294       if ($("#location").length > 0 && !location_values) {
295         showWarn();
296       }
297
298       function showWarn() {
299         $("#paramsWarn").show();
300         $('#servName').text($("#service option:selected").text());
301       }
302     } catch (e) {
303       console.log(e)
304     }
305   }
306 }
307
308 //Typically used when opening a new model/template
309 function reloadDefaultVariables(isTemp) {
310   isTemplate = isTemp;
311   vf_Services = null;
312 }
313
314 $(window).on('load',function() {
315   $.ajax({
316     dataType: "json",
317     url: '/restservices/clds/v1/clds/properties',
318     success: function(data) {
319       defaults_props = JSON.parse(data);
320     },
321     error: function(s, a, err) {
322       console.log(err)
323       console.log(s)
324       console.log(a)
325     },
326     timeout: 100000
327   });
328 })