6b73668ab885d3c351e2bfcf15622710555381d8
[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                 $.each(value, function(key, value) {
109                     if (key == 'type') {
110                         // add classes
111                         $(value).each(function(index, value) {
112                             $tr.addClass(value);
113                         });
114                     } else if (key == 'entry') {
115                         // add entries
116                         $(value).each(function(index, value) {
117                             var $td = $(document.createElement('td'));
118                             $td.append(value);
119                             $tr.append($td);
120                         });
121                     } else {
122                         // data field
123                         $tr.attr('data-' + key, value);
124                     }
125                     $tbody.append($tr);
126                 });
127             });
128             return $tbody;
129         }
130     },
131     description : function(description, horizontal) {
132         var $dl = $(document.createElement('dl'));
133         if (horizontal == true) {
134             $dl.addClass("dl-horizontal");
135         }
136         $(description).each(function(index, value) {
137             var $dt = $(document.createElement('dt'));
138             $dt.text(value.dt);
139             var $dd = $(document.createElement('dd'));
140             $dd.text(value.dd);
141             $dl.append($dt).append($dd);
142         });
143         return $dl;
144     }
145 }
146
147 /** MODAL */
148 one.lib.modal = {
149     // clone default modal
150     clone : function(id) {
151         var $clone = $("#modal").clone(true);
152         $clone.attr("id", id);
153         return $clone;
154     },
155     // populate modal
156     populate : function($modal, header, $body, footer) {
157         var $h3 = $modal.find("h3");
158         $h3.text(header);
159
160         var $modalBody = $modal.find(".modal-body");
161         $modalBody.append($body);
162
163         $(footer).each(function(index, value) {
164             $modal.find(".modal-footer").append(value);
165         });
166     },
167     // clone and populate modal
168     spawn : function(id, header, $body, footer) {
169         var $modal = one.lib.modal.clone(id);
170         one.lib.modal.populate($modal, header, $body, footer);
171         return $modal;
172     },
173     // empty modal
174     empty : function($modal) {
175         $modal.find("h3").empty();
176         $modal.find(".modal-body").empty();
177         $modal.find(".modal-footer").empty();
178     },
179     // injection
180     inject : {
181         body : function($modal, $body) {
182             $modal.find(".modal-body").empty();
183             $modal.find(".modal-body").append($body);
184         }
185     }
186 }
187
188 /** FORM */
189 one.lib.form = {
190     // create select-option form element
191     select : {
192         create : function(options, multiple) {
193             // assert - auto assign
194             if (options == undefined)
195                 options = {};
196
197             var $select = $(document.createElement('select'));
198             if (multiple == true) {
199                 $select.attr("multiple", "multiple");
200             }
201             var optionArray = one.lib.form.select.options(options);
202             $(optionArray).each(function(index, value) {
203                 $select.append(value);
204             });
205             return $select;
206         },
207         options : function(options) {
208             var result = [];
209             $.each(options, function(key, value) {
210                 var $option = $(document.createElement('option'));
211                 $option.attr("value", key);
212                 $option.text(value);
213                 result.push($option);
214             });
215             return result;
216         },
217         empty : function($select) {
218             $select.empty();
219         },
220         inject : function($select, options) {
221             $select.empty();
222             var options = one.lib.form.select.options(options);
223             $select.append(options);
224         },
225         prepend : function($select, options) {
226             var options = one.lib.form.select.options(options);
227             $select.prepend(options);
228         },
229         bubble : function($select, bubble) {
230             $($select.find("option")).each(function(index, value) {
231                 if ($(value).attr("value") == bubble) {
232                     var option = $(value);
233                     $(value).remove();
234                     $select.prepend(option);
235                     return;
236                 }
237             });
238         }
239     },
240     // create legend form element
241     legend : function(name) {
242         var $legend = $(document.createElement('legend'));
243         $legend.text(name);
244         return $legend;
245     },
246     // create label form element
247     label : function(name) {
248         var $label = $(document.createElement('label'));
249         $label.text(name);
250         return $label;
251     },
252     // create help block form element
253     help : function(help) {
254         var $help = $(document.createElement('span'));
255         $help.text(help);
256         $help.addClass("help-block");
257         return $help;
258     },
259     // create generic text input
260     input : function(placeholder) {
261         var $input = $(document.createElement('input'));
262         $input.attr('type', 'text');
263         $input.attr('placeholder', placeholder);
264         return $input;
265     }
266 }
267
268 /** NAV */
269 one.lib.nav = {
270     unfocus : function($nav) {
271         $($nav.find("li")).each(function(index, value) {
272             $(value).removeClass("active");
273         });
274     }
275 }
276
277 /** ALERT */
278 one.lib.alert = function(alert) {
279     $("#alert p").text(alert);
280     $("#alert").hide();
281     $("#alert").slideToggle();
282     clearTimeout(one.lib.registry.alert);
283     one.lib.registry.alert = setTimeout(function() {
284         $("#alert").slideUp();
285     }, 8000);
286 }