1. Controller switchEvents queue should be priority based. The queue holds switch...
[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     datagrid: {
71         /*
72          * The init function returns HTML markup for the datagrid per the options provided. Each consumer 
73          * of the datagrid must first call init and then provide the datasource for the grid.   
74          * id: this is the id of the table
75          * options: {
76          * searchable: true/false,
77          * pagination: turned off for now,
78          * flexibleRowsPerPage: turned off
79          * }
80          * classes : String containing bootstrap related classes. For ex: "table-striped table-condensed"
81          * The classes "table", "table-bordered" and "datagrid" will be added by default
82          */
83         init: function(id, options, classes) {
84             var $fuelGridContainerDiv = $(document.createElement("div"));
85             $fuelGridContainerDiv.addClass("fuelux");
86             $table = $(document.createElement("table"));
87             $table.attr("id", id);
88             $table.addClass("table table-bordered datagrid");
89             $table.addClass(classes);
90             // create datagrid header
91             $thead = $(document.createElement("thead"));
92             $headertr = $(document.createElement("tr"));
93             $headerth = $(document.createElement("th"));
94             // create datagrid footer
95             $tfoot = $(document.createElement("tfoot"));
96             $footertr = $(document.createElement("tr"));
97             $footerth = $(document.createElement("th"));
98             if(options.searchable == true) {
99                 $headerth.append(one.lib.dashlet.datagrid._searchable());
100             }
101             if(options.flexibleRowsPerPage == true) {
102                 $footerth.append(one.lib.dashlet.datagrid._rowsPerPage());
103             }
104             if(options.pagination == true) {
105                 $footerth.append(one.lib.dashlet.datagrid._pagination());
106             }
107             $headertr.append($headerth);
108             $thead.append($headertr);
109             $footertr.append($footerth);
110             $tfoot.append($footertr);
111             $table.append($thead).append($tfoot);
112             $fuelGridContainerDiv.append($table);
113             return $fuelGridContainerDiv;
114         },
115         _searchable: function() {
116             var searchHTML = "<div class='datagrid-header-left'><div class='input-append search datagrid-search'> <input type='text' class='input-medium' placeholder='Search'><button type='button' class='btn'><i class='icon-search'></i></button></div></div>";
117             return searchHTML;
118         },
119         _pagination: function() {
120             var html = '<div class="datagrid-footer-right" style="display:none;"><div class="grid-pager"><button type="button" class="btn grid-prevpage"><i class="icon-chevron-left"></i></button><span>Page</span> <div style="display:inline-block;"><input type="text" name="pagenumber" style="width:25px;margin-bottom:-10px;vertical-align:middle;margin-right:5px;"></div><span>of <span class="grid-pages"></span></span><button type="button" class="btn grid-nextpage"><i class="icon-chevron-right"></i></button></div></div>';
121             return html;
122         },
123         _rowsPerPage: function() {
124             var html = '<div class="datagrid-footer-left" style="display:none;"><div class="grid-controls"><span><span class="grid-start"></span>-<span class="grid-end"></span> of <span class="grid-count"></span></span><div class="select grid-pagesize" data-resize="auto" style="visibility:hidden;"><button type="button" data-toggle="dropdown" class="btn dropdown-toggle"><span class="dropdown-label"></span><span class="caret"></span></button><ul class="dropdown-menu"><li data-value="5" data-selected="true"><a href="#">5</a></li><li data-value="10"><a href="#">10</a></li><li data-value="20"><a href="#">20</a></li><li data-value="50"><a href="#">50</a></li><li data-value="100"><a href="#">100</a></li></ul></div><span style="display:none;">Per Page</span></div></div>';
125             return html;
126         }
127     },
128     table : {
129         table : function(classes, id) {
130             var $table = $(document.createElement('table'));
131             $table.addClass("table");
132             $(classes).each(function(index, value) {
133                 $table.addClass(value);
134             });
135             if (!(typeof id === 'undefined'))
136                 $table.attr("id", id);
137             return $table;
138         },
139         header : function(headers) {
140             var $thead = $(document.createElement('thead'));
141             var $tr = $(document.createElement('tr'));
142             $(headers).each(function(index, value) {
143                 $th = $(document.createElement('th'));
144                 $th.append(value);
145                 $tr.append($th);
146             });
147             $thead.append($tr);
148             return $thead;
149         },
150         body : function(body, thead) {
151             var $tbody = $(document.createElement('tbody'));
152             // if empty
153             if (body.length == 0 && !(typeof thead === 'undefined')) {
154                 var $tr = $(document.createElement('tr'));
155                 var $td = $(document.createElement('td'));
156                 $td.attr('colspan', thead.length);
157                 $td.text('No data available');
158                 $td.addClass('empty');
159                 $tr.append($td);
160                 $tbody.append($tr);
161                 return $tbody;
162             }
163             // else, populate as usual
164             $(body).each(function(index, value) {
165                 var $tr = $(document.createElement('tr'));
166                 $.each(value, function(key, value) {
167                     if (key == 'type') {
168                         // add classes
169                         $(value).each(function(index, value) {
170                             $tr.addClass(value);
171                         });
172                     } else if (key == 'entry') {
173                         // add entries
174                         $(value).each(function(index, value) {
175                             var $td = $(document.createElement('td'));
176                             $td.append(value);
177                             $tr.append($td);
178                         });
179                     } else {
180                         // data field
181                         $tr.attr('data-' + key, value);
182                     }
183                     $tbody.append($tr);
184                 });
185             });
186             return $tbody;
187         }
188     },
189     description : function(description, horizontal) {
190         var $dl = $(document.createElement('dl'));
191         if (horizontal == true) {
192             $dl.addClass("dl-horizontal");
193         }
194         $(description).each(function(index, value) {
195             var $dt = $(document.createElement('dt'));
196             $dt.text(value.dt);
197             var $dd = $(document.createElement('dd'));
198             $dd.text(value.dd);
199             $dl.append($dt).append($dd);
200         });
201         return $dl;
202     }
203 }
204
205 /** MODAL */
206 one.lib.modal = {
207     // clone default modal
208     clone : function(id) {
209         var $clone = $("#modal").clone(true);
210         $clone.attr("id", id);
211         return $clone;
212     },
213     // populate modal
214     populate : function($modal, header, $body, footer) {
215         var $h3 = $modal.find("h3");
216         $h3.text(header);
217
218         var $modalBody = $modal.find(".modal-body");
219         $modalBody.append($body);
220
221         $(footer).each(function(index, value) {
222             $modal.find(".modal-footer").append(value);
223         });
224     },
225     // clone and populate modal
226     spawn : function(id, header, $body, footer) {
227         var $modal = one.lib.modal.clone(id);
228         one.lib.modal.populate($modal, header, $body, footer);
229         return $modal;
230     },
231     // empty modal
232     empty : function($modal) {
233         $modal.find("h3").empty();
234         $modal.find(".modal-body").empty();
235         $modal.find(".modal-footer").empty();
236     },
237     // injection
238     inject : {
239         body : function($modal, $body) {
240             $modal.find(".modal-body").empty();
241             $modal.find(".modal-body").append($body);
242         }
243     }
244 }
245
246 /** FORM */
247 one.lib.form = {
248     // create select-option form element
249     select : {
250         create : function(options, multiple) {
251             // assert - auto assign
252             if (options == undefined)
253                 options = {};
254
255             var $select = $(document.createElement('select'));
256             if (multiple == true) {
257                 $select.attr("multiple", "multiple");
258             }
259             var optionArray = one.lib.form.select.options(options);
260             $(optionArray).each(function(index, value) {
261                 $select.append(value);
262             });
263             return $select;
264         },
265         options : function(options) {
266             var result = [];
267             $.each(options, function(key, value) {
268                 var $option = $(document.createElement('option'));
269                 $option.attr("value", key);
270                 $option.text(value);
271                 result.push($option);
272             });
273             return result;
274         },
275         empty : function($select) {
276             $select.empty();
277         },
278         inject : function($select, options) {
279             $select.empty();
280             var options = one.lib.form.select.options(options);
281             $select.append(options);
282         },
283         prepend : function($select, options) {
284             var options = one.lib.form.select.options(options);
285             $select.prepend(options);
286         },
287         bubble : function($select, bubble) {
288             $($select.find("option")).each(function(index, value) {
289                 if ($(value).attr("value") == bubble) {
290                     var option = $(value);
291                     $(value).remove();
292                     $select.prepend(option);
293                     return;
294                 }
295             });
296         }
297     },
298     // create legend form element
299     legend : function(name) {
300         var $legend = $(document.createElement('legend'));
301         $legend.text(name);
302         return $legend;
303     },
304     // create label form element
305     label : function(name) {
306         var $label = $(document.createElement('label'));
307         $label.text(name);
308         return $label;
309     },
310     // create help block form element
311     help : function(help) {
312         var $help = $(document.createElement('span'));
313         $help.text(help);
314         $help.addClass("help-block");
315         return $help;
316     },
317     // create generic text input
318     input : function(placeholder) {
319         var $input = $(document.createElement('input'));
320         $input.attr('type', 'text');
321         $input.attr('placeholder', placeholder);
322         return $input;
323     }
324 }
325
326 /** NAV */
327 one.lib.nav = {
328     unfocus : function($nav) {
329         $($nav.find("li")).each(function(index, value) {
330             $(value).removeClass("active");
331         });
332     }
333 }
334
335 /** ALERT */
336 one.lib.alert = function(alert) {
337     $("#alert p").text(alert);
338     $("#alert").hide();
339     $("#alert").slideToggle();
340     clearTimeout(one.lib.registry.alert);
341     one.lib.registry.alert = setTimeout(function() {
342         $("#alert").slideUp();
343     }, 8000);
344 }