[CLAMP-1] Initial ONAP CLAMP seed code commit
[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) {
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         $("select,input").attr("disabled", "");
71     }
72
73     var props = defaults_props[type];
74     
75     for (p in props) {
76         if (isObject(props[p])) {
77             var mySelect = $('#' + p);
78             $.each(props[p], function(val, text) {
79                 mySelect.append(
80                     $('<option></option>').val(val).html(text)
81                 );
82             });
83         } else {
84                 if (p=="pname"){
85                         var ms = new Date().getTime();
86                         props[p] = "Policy" + ms;
87                 }
88             $("#" + p).val(props[p])
89         }
90     }
91     setTimeout(function(){
92
93      setMultiSelect(type); }, 100);
94      
95 }
96
97 function setMultiSelect() {
98     $("select").each(function(index, mySelect) {
99         if ($(mySelect).parents(".multiselect-native-select") &&
100             $(mySelect).parents(".multiselect-native-select").length < 1) {
101             if (!$(mySelect).attr("multiple")) {
102                 if ($(mySelect).attr("enableFilter")) {
103                     $(mySelect).multiselect({
104                         numberDisplayed: 1,
105                         maxHeight: 200
106                     });
107                 } else {
108                     $(mySelect).multiselect({
109                         numberDisplayed: 1,
110                         enableFiltering: true,
111                         maxHeight: 200
112                     });
113                 }
114                 
115             } else {
116                 $(mySelect).multiselect({
117                     numberDisplayed: 1,
118                     includeSelectAllOption: true,
119                     enableFiltering: true,
120                     maxHeight: 200,
121                     enableCaseInsensitiveFiltering: true
122                 });
123             }
124
125         } else if ($(mySelect).parents(".multiselect-native-select") &&
126             $(mySelect).parents(".multiselect-native-select").length > 0) {
127             var selectDrop = $(mySelect).parent(".multiselect-native-select").find("select");
128             $(mySelect).parent(".multiselect-native-select").parent().html(selectDrop);
129             if (!$(mySelect).attr("multiple")) {
130                 if ($(mySelect).attr("enableFilter")) {
131                     $(mySelect).multiselect({
132                         numberDisplayed: 1,
133                         maxHeight: 200
134                     });
135                 } else {
136                     $(mySelect).multiselect({
137                         numberDisplayed: 1,
138                         enableFiltering: true,
139                         maxHeight: 200
140                     });
141                 }
142             } else {
143                 $(mySelect).multiselect({
144                     numberDisplayed: 1,
145                     includeSelectAllOption: true,
146                     enableFiltering: true,
147                     maxHeight: 200,
148                     enableCaseInsensitiveFiltering: true
149                 });
150             }
151         }
152     });
153     //refeshMultiSelect();
154 }
155 function loadSharedPropertyByService(onChangeUUID,refresh, callBack) {
156     var uuid = onChangeUUID
157     if (uuid === undefined){
158         uuid = elementMap["global"] && elementMap["global"].length>0 ? 
159         elementMap["global"][0].value : "";
160     } else if (uuid === "") {
161         vf_Services = null
162         if ($("#vf").length > 0)
163             $("#vf").empty();
164         if ($("#location").length > 0)
165             $("#location").empty();
166         if ($("#alarmCondition").length > 0)
167             $("#alarmCondition").empty();
168         return true;
169     }
170     var share = null,
171     serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid;
172     if(refresh){
173         serviceUrl = '/restservices/clds/v1/clds/properties/' + uuid +'?refresh=true';
174     }
175
176     $.ajax({
177         async: false,
178         dataType: "json",
179         url: serviceUrl,
180         success: function(data) {
181             vf_Services = data;
182             setASDCFields()
183              if(refresh){
184              $("#paramsWarnrefresh").hide();   
185             }
186              if($("#paramsWarn")){
187                 $("#paramsWarn").hide();
188             }
189             if(callBack && _.isFunction(callBack)){
190                 callBack(true);
191             }
192         },
193         error: function(s, a, err) {
194             if(refresh){
195              $("#paramsWarnrefresh").show();   
196             }
197             if($("#paramsWarn")){
198                 $("#paramsWarn").show();    
199             }
200             
201             $('#servName').text($("#service option:selected").text());
202             if(callBack && _.isFunction(callBack)){
203                 callBack(false);
204             }
205             console.log(err)
206             console.log(s)
207             console.log(a)
208         },
209         timeout: 100000
210
211     });
212     //vf_Services=share['shared']['byService'][uuid];
213     //location_values = share['global']['location'];
214 }
215
216 function loadSharedPropertyByServiceProperties(callBack) {
217     $.ajax({
218         async: false,
219         dataType: "json",
220         url: '/restservices/clds/v1/clds/properties/',
221         success: function(data) {
222             vf_Services = data;
223             setASDCFields();
224             if(callBack && _.isFunction(callBack)){
225                 callBack(true);
226             }
227         },
228         error: function(s, a, err) {
229             $('#servName').text($("#service option:selected").text());
230             if(callBack && _.isFunction(callBack)){
231                 callBack(false);
232             }
233         },
234         timeout: 100000
235
236     });
237 }
238
239 function setASDCFields() {
240     if (vf_Services === null || vf_Services === undefined) {
241         loadSharedPropertyByService()
242     } else {
243         try {
244             $("#vf").empty();
245             $("#location").empty();
246             $("#vfc").empty();
247             $("#paramsWarn").hide();
248             var uuid = Object.keys(vf_Services['shared']['byService'])[0];
249             var vf_values = vf_Services['shared']['byService'][uuid] &&
250                 vf_Services['shared']['byService'][uuid]['vf'] &&
251                                 _.keys(vf_Services['shared']['byService'][uuid]['vf']).length > 0  ?
252                 vf_Services['shared']['byService'][uuid]['vf'] : null;
253             var selectedVF = {}
254             for (let e in elementMap["global"]) {
255                 if (elementMap['global'][e].name === "vf") {
256                     selectedVF = elementMap['global'][e].value[0]
257                 }
258             }
259             var location_values = vf_Services['global']['location'];
260             var vfc_values2 = selectedVF &&
261                 vf_Services['shared']['byVf'][selectedVF] &&
262                 vf_Services['shared']['byVf'][selectedVF]['vfc'] &&
263                  _.keys(vf_Services['shared']['byVf'][selectedVF]['vfc']).length > 0 ?
264                 vf_Services['shared']['byVf'][selectedVF]['vfc'] : null;
265
266             if (vf_values) {
267                 for (key in vf_values) {
268                     if ($("#vf").length > 0) {
269                         $("#vf").append("<option value=\"" + key + "\">" + vf_values[key] + "</opton>")
270                     }
271                 }
272             } 
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             }            
280             if (vfc_values2) {
281                 $("#vfc").append("<option value=\"\"></opton>");
282                 for (key in vfc_values2) {
283                     if ($("#vfc").length > 0) {
284                         $("#vfc").append("<option value=\"" + key.split("\"").join("") + "\">" + vfc_values2[key] + "</opton>")
285                     }
286                 }
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     readOnly = false;
313     runningInstances = {}
314 }
315
316 $(window).load(function() {
317     $.ajax({
318         dataType: "json",
319         url: '/restservices/clds/v1/clds/properties',
320         success: function(data) {
321             console.log("success");
322             defaults_props = data;
323         },
324         error: function(s, a, err) {
325             console.log(err)
326             console.log(s)
327             console.log(a)
328         },
329         timeout: 100000
330     });
331
332
333
334 })