Fix Properties CL UI issue
[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   }
154   var share = null,
155     serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid;
156   if (refresh) {
157     serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid + '?refresh=true';
158   }
159
160   $.ajax({
161     async: false,
162     dataType: "json",
163     url: serviceUrl,
164     success: function(data) {
165       vf_Services = data;
166       setASDCFields()
167       if (refresh) {
168         $("#paramsWarnrefresh").hide();
169       }
170       if ($("#paramsWarn")) {
171         $("#paramsWarn").hide();
172       }
173       if (callBack && _.isFunction(callBack)) {
174         callBack(true);
175       }
176     },
177     error: function(s, a, err) {
178       if (refresh) {
179         $("#paramsWarnrefresh").show();
180       }
181       if ($("#paramsWarn")) {
182         $("#paramsWarn").show();
183       }
184
185       $('#servName').text($("#service option:selected").text());
186       if (callBack && _.isFunction(callBack)) {
187         callBack(false);
188       }
189       console.log(err)
190       console.log(s)
191       console.log(a)
192     },
193     timeout: 100000
194
195   });
196
197   //vf_Services=share['shared']['byService'][uuid];
198   //location_values = share['global']['location'];
199 }
200
201 function loadSharedPropertyByServiceProperties(callBack) {
202   $.ajax({
203     async: false,
204     dataType: "json",
205     url: '/restservices/clds/v1/clds/properties/',
206     success: function(data) {
207       vf_Services = data;
208       setASDCFields();
209       if (callBack && _.isFunction(callBack)) {
210         callBack(true);
211       }
212     },
213     error: function(s, a, err) {
214       $('#servName').text($("#service option:selected").text());
215       if (callBack && _.isFunction(callBack)) {
216         callBack(false);
217       }
218     },
219     timeout: 100000
220
221   });
222 }
223
224 function setASDCFields() {
225   if (vf_Services === null || vf_Services === undefined) {
226     loadSharedPropertyByService()
227   } else {
228     try {
229       $("#vf").empty().multiselect("refresh");
230       $("#location").empty().multiselect("refresh");
231       $("#actionSet").empty().multiselect("refresh");
232       $("#vfc").empty().multiselect("refresh");
233       $("#paramsWarn").hide();
234       var uuid = Object.keys(vf_Services['shared']['byService'])[0];
235
236       var vf_values = vf_Services['shared']['byService'][uuid] &&
237         vf_Services['shared']['byService'][uuid]['vf'] &&
238         _.keys(vf_Services['shared']['byService'][uuid]['vf']).length > 0 ?
239         vf_Services['shared']['byService'][uuid]['vf'] : null;
240
241       var selectedVF = {}
242       for (let e in elementMap["global"]) {
243         if (elementMap['global'][e].name === "vf") {
244           selectedVF = elementMap['global'][e].value[0]
245         }
246       }
247
248       var vfc_values2 = selectedVF &&
249         vf_Services['shared']['byVf'][selectedVF] &&
250         vf_Services['shared']['byVf'][selectedVF]['vfc'] &&
251         _.keys(vf_Services['shared']['byVf'][selectedVF]['vfc']).length > 0 ?
252         vf_Services['shared']['byVf'][selectedVF]['vfc'] : null;
253
254       if (vf_values) {
255         for (key in vf_values) {
256           if ($("#vf").length > 0) {
257             $("#vf").append("<option value=\"" + key + "\">" + vf_values[key] + "</opton>")
258           }
259         }
260         $("#vf").multiselect("rebuild");
261       }
262
263       var location_values = vf_Services['global']['location'];
264       if (location_values) {
265         for (key in location_values) {
266           if ($("#location").length > 0) {
267             $("#location").append("<option value=\"" + key + "\">" + location_values[key] + "</opton>")
268           }
269         }
270         $("#location").multiselect("rebuild");
271       }
272
273       var actionSet_values = vf_Services['global']['actionSet'];
274       if (actionSet_values) {
275         for (key in actionSet_values) {
276           if ($("#actionSet").length > 0) {
277             $("#actionSet").append("<option value=\"" + key + "\">" + actionSet_values[key] + "</opton>")
278           }
279         }
280         $("#actionSet").multiselect("rebuild");
281       }
282
283       if (vfc_values2) {
284         $("#vfc").append("<option value=\"\"></opton>");
285         for (key in vfc_values2) {
286           if ($("#vfc").length > 0) {
287             $("#vfc").append("<option value=\"" + key.split("\"").join("") + "\">" + vfc_values2[key] + "</opton>")
288           }
289         }
290         $("#vfc").multiselect("rebuild");
291       }
292       if ($("#vfc").length > 0 && !vfc_values2) {
293         showWarn();
294       }
295       if ($("#vf").length > 0 && !vf_values) {
296         showWarn();
297       }
298       if ($("#location").length > 0 && !location_values) {
299         showWarn();
300       }
301
302       function showWarn() {
303         $("#paramsWarn").show();
304         $('#servName').text($("#service option:selected").text());
305       }
306     } catch (e) {
307       console.log(e)
308     }
309   }
310 }
311
312 //Typically used when opening a new model/template
313 function reloadDefaultVariables(isTemp) {
314   isTemplate = isTemp;
315   vf_Services = null;
316   readOnly = false;
317 }
318
319 $(window).load(function() {
320   $.ajax({
321     dataType: "json",
322     url: '/restservices/clds/v1/clds/properties',
323     success: function(data) {
324
325       defaults_props = data;
326     },
327     error: function(s, a, err) {
328       console.log(err)
329       console.log(s)
330       console.log(a)
331     },
332     timeout: 100000
333   });
334 })