Merge "Refactor frontend JS"
[controller.git] / opendaylight / web / root / src / main / resources / js / lib.js
1 // global
2 var one = {
3     // global variables
4     global : {
5         remoteAddress : "/"
6     },
7     role : null
8 }
9
10 // one ui library
11 one.lib = {};
12
13 // registry
14 one.lib.registry = {};
15
16 /** DASHLET */
17 one.lib.dashlet = {
18     empty : function($dashlet) {
19         $dashlet.empty();
20     },
21     append : function($dashlet, $content) {
22         $dashlet.append($content);
23     },
24     header : function(header) {
25         var $h4 = $(document.createElement('h4'));
26         $h4.text(header);
27         return $h4;
28     },
29     list : function(list) {
30         var $ul = $(document.createElement('ul'));
31         $(list).each(function(index, value) {
32             var $li = $(document.createElement('li'));
33             $li.append(value);
34             $ul.append($li);
35         });
36         return $ul;
37     },
38     button : {
39         config : function(name, id, type, size) {
40             var button = {};
41             button['name'] = name;
42             button['id'] = id;
43             button['type'] = type;
44             button['size'] = size;
45             return button;
46         },
47         single : function(name, id, type, size) {
48             var buttonList = [];
49             var button = one.lib.dashlet.button.config(name, id, type, size);
50             buttonList.push(button);
51             return buttonList;
52         },
53         button : function(buttonList) {
54             var $buttonGroup = $(document.createElement('div'));
55             $buttonGroup.addClass("btn-group");
56             $(buttonList).each(function(index, value) {
57                 var $button = $(document.createElement('button'));
58                 $button.text(value.name);
59                 $button.addClass('btn');
60                 $button.addClass(value['type']);
61                 $button.addClass(value['size']);
62                 if (!(typeof value.id === 'undefined')) {
63                     $button.attr('id', value.id);
64                 }
65                 $buttonGroup.append($button);
66             });
67             return $buttonGroup;
68         }
69     },
70     table : {
71         table : function(classes, id) {
72             var $table = $(document.createElement('table'));
73             $table.addClass("table");
74             $(classes).each(function(index, value) {
75                 $table.addClass(value);
76             });
77             if (!(typeof id === 'undefined'))
78                 $table.attr("id", id);
79             return $table;
80         },
81         header : function(headers) {
82             var $thead = $(document.createElement('thead'));
83             var $tr = $(document.createElement('tr'));
84             $(headers).each(function(index, value) {
85                 $th = $(document.createElement('th'));
86                 $th.append(value);
87                 $tr.append($th);
88             });
89             $thead.append($tr);
90             return $thead;
91         },
92         body : function(body, thead) {
93             var $tbody = $(document.createElement('tbody'));
94             // if empty
95             if (body.length == 0 && !(typeof thead === 'undefined')) {
96                 var $tr = $(document.createElement('tr'));
97                 var $td = $(document.createElement('td'));
98                 $td.attr("colspan", thead.length);
99                 $td.text("No data available");
100                 $td.addClass("empty");
101                 $tr.append($td);
102                 $tbody.append($tr);
103                 return $tbody;
104             }
105             // else, populate as usual
106             $(body).each(function(index, value) {
107                 var $tr = $(document.createElement('tr'));
108                 // data-id
109                 if (value['id'] != undefined) {
110                     $tr.attr('data-id', value['id']);
111                 }
112                 // add classes
113                 $(value["type"]).each(function(index, value) {
114                     $tr.addClass(value);
115                 });
116                 // add entries
117                 $(value["entry"]).each(function(index, value) {
118                     var $td = $(document.createElement('td'));
119                     $td.append(value);
120                     $tr.append($td);
121                 });
122                 $tbody.append($tr);
123             });
124             return $tbody;
125         }
126     },
127     description : function(description, horizontal) {
128         var $dl = $(document.createElement('dl'));
129         if (horizontal == true) {
130             $dl.addClass("dl-horizontal");
131         }
132         $(description).each(function(index, value) {
133             var $dt = $(document.createElement('dt'));
134             $dt.text(value.dt);
135             var $dd = $(document.createElement('dd'));
136             $dd.text(value.dd);
137             $dl.append($dt).append($dd);
138         });
139         return $dl;
140     }
141 }
142
143 /** MODAL */
144 one.lib.modal = {
145     // clone default modal
146     clone : function(id) {
147         var $clone = $("#modal").clone(true);
148         $clone.attr("id", id);
149         return $clone;
150     },
151     // populate modal
152     populate : function($modal, header, $body, footer) {
153         var $h3 = $modal.find("h3");
154         $h3.text(header);
155
156         var $modalBody = $modal.find(".modal-body");
157         $modalBody.append($body);
158
159         $(footer).each(function(index, value) {
160             $modal.find(".modal-footer").append(value);
161         });
162     },
163     // clone and populate modal
164     spawn : function(id, header, $body, footer) {
165         var $modal = one.lib.modal.clone(id);
166         one.lib.modal.populate($modal, header, $body, footer);
167         return $modal;
168     },
169     // empty modal
170     empty : function($modal) {
171         $modal.find("h3").empty();
172         $modal.find(".modal-body").empty();
173         $modal.find(".modal-footer").empty();
174     },
175     // injection
176     inject : {
177         body : function($modal, $body) {
178             $modal.find(".modal-body").empty();
179             $modal.find(".modal-body").append($body);
180         }
181     }
182 }
183
184 /** FORM */
185 one.lib.form = {
186     // create select-option form element
187     select : {
188         create : function(options, multiple) {
189             // assert - auto assign
190             if (options == undefined)
191                 options = {};
192
193             var $select = $(document.createElement('select'));
194             if (multiple == true) {
195                 $select.attr("multiple", "multiple");
196             }
197             var optionArray = one.lib.form.select.options(options);
198             $(optionArray).each(function(index, value) {
199                 $select.append(value);
200             });
201             return $select;
202         },
203         options : function(options) {
204             var result = [];
205             $.each(options, function(key, value) {
206                 var $option = $(document.createElement('option'));
207                 $option.attr("value", key);
208                 $option.text(value);
209                 result.push($option);
210             });
211             return result;
212         },
213         empty : function($select) {
214             $select.empty();
215         },
216         inject : function($select, options) {
217             $select.empty();
218             var options = one.lib.form.select.options(options);
219             $select.append(options);
220         },
221         prepend : function($select, options) {
222             var options = one.lib.form.select.options(options);
223             $select.prepend(options);
224         },
225         bubble : function($select, bubble) {
226             $($select.find("option")).each(function(index, value) {
227                 if ($(value).attr("value") == bubble) {
228                     var option = $(value);
229                     $(value).remove();
230                     $select.prepend(option);
231                     return;
232                 }
233             });
234         }
235     },
236     // create legend form element
237     legend : function(name) {
238         var $legend = $(document.createElement('legend'));
239         $legend.text(name);
240         return $legend;
241     },
242     // create label form element
243     label : function(name) {
244         var $label = $(document.createElement('label'));
245         $label.text(name);
246         return $label;
247     },
248     // create help block form element
249     help : function(help) {
250         var $help = $(document.createElement('span'));
251         $help.text(help);
252         $help.addClass("help-block");
253         return $help;
254     },
255     // create generic text input
256     input : function(placeholder) {
257         var $input = $(document.createElement('input'));
258         $input.attr('type', 'text');
259         $input.attr('placeholder', placeholder);
260         return $input;
261     }
262 }
263
264 /** NAV */
265 one.lib.nav = {
266     unfocus : function($nav) {
267         $($nav.find("li")).each(function(index, value) {
268             $(value).removeClass("active");
269         });
270     }
271 }
272
273 /** ALERT */
274 one.lib.alert = function(alert) {
275     $("#alert p").text(alert);
276     $("#alert").hide();
277     $("#alert").slideToggle();
278     clearTimeout(one.lib.registry.alert);
279     one.lib.registry.alert = setTimeout(function() {
280         $("#alert").slideUp();
281     }, 8000);
282 }