foist
[kismet-logviewer.git] / logviewer / static / js / kismet.ui.tabpane.js
1 (
2   typeof define === "function" ? function (m) { define("kismet-ui-tabpane-js", m); } :
3   typeof exports === "object" ? function (m) { module.exports = m(); } :
4   function(m){ this.kismet_ui_tabpane = m(); }
5 )(function () {
6
7 "use strict";
8
9 var exports = {};
10
11 var local_uri_prefix = ""; 
12 if (typeof(KISMET_URI_PREFIX) !== 'undefined')
13     local_uri_prefix = KISMET_URI_PREFIX;
14
15 // Flag we're still loading
16 exports.load_complete = 0;
17
18 // Load our css
19 $('<link>')
20     .appendTo('head')
21     .attr({
22         type: 'text/css',
23         rel: 'stylesheet',
24         href: local_uri_prefix + 'css/kismet.ui.tabpane.css'
25     });
26
27 var tabholders = {};
28
29 /* Add a tab
30  *
31  * By default adds to the 'south' container, which previously was the only tab holder available.
32  *
33  * Options is a dictionary which must include:
34  *
35  * id: id for created div
36  * tabTitle: Title show in tab bar
37  * createCallback: function called after the div is created, passed the new div
38  *
39  * priority: order priority in list (optional)
40  */
41 exports.AddTab = function(options, group="south") {
42     if (!('id' in options) ||
43         !('tabTitle' in options) ||
44         !('createCallback' in options)) {
45         return;
46     }
47
48     if (!('priority' in options)) {
49         options.priority = 0;
50     }
51
52     if (!('expandable' in options)) {
53         options.expandable = false;
54     }
55
56     if (!('expandCallback' in options)) {
57         options.expandCallback = null;
58     }
59
60     options.expanded = false;
61
62     if (group in tabholders) {
63         for (var x = 0; x < tabholders[group].TabItems.length; x++) {
64             if (tabholders[group].TabItems[x].id == options["id"]) {
65                 tabholders[group].TabItems.splice(x, 1);
66                 break;
67             }
68         }
69
70         tabholders[group].TabItems.push(options);
71     } else {
72         tabholders[group] = {
73             TabItems: [options],
74         };
75     }
76 };
77
78 exports.RemoveTab = function(id, group="south") {
79     try {
80         for (var x = 0; x < tabholders[group].TabItems.length; x++) {
81             if (tabholders[group].TabItems[x].id == id) {
82                 tabholders[group].TabItems.splice(x, 1);
83                 break;
84             }
85         }
86     } catch (error) {
87         alert(`UI attempted to remove tab ${id} from invalid group ${group}`);
88     }
89 }
90
91 function createListCallback(c) {
92     return function() {
93         c.createCallback();
94     };
95 }
96
97 function createExpanderCallback(c, group) {
98     return function() {
99         MoveToExpanded(c, group);
100     }
101 }
102
103 function populateList(div, group) {
104     if (group in tabholders) {
105         tabholders[group].TabDiv = div;
106     } else {
107         tabholders[group] = {
108             TabDiv: div,
109             TabItems: []
110         };
111     }
112
113     tabholders[group].TabItems.sort(function(a, b) {
114         if (a.priority < b.priority)
115             return -1;
116         if (a.priority > b.priority)
117             return 1;
118
119         return 0;
120     });
121
122     div.empty();
123
124     var ul = $('<ul>', {
125             id: `tabpane_ul_${group}`
126         });
127
128     div.append(ul);
129
130     for (var i in tabholders[group].TabItems) {
131         var c = tabholders[group].TabItems[i];
132
133         var title = c.tabTitle;;
134
135         if (c.expandable) {
136             title += ' <i class="fa fa-expand pseudolink"></i>';
137         }
138
139         ul.append(
140             $('<li>', { })
141             .append(
142                 $('<a>', {
143                     href: '#' + c.id
144                 })
145                 .html(title)
146             )
147         );
148
149         $('i', ul).tooltipster({content: 'Expand tab to own window'});
150
151         if (c.expandable) {
152             $('i', ul).on('click', createExpanderCallback(c, group));
153         }
154
155         var td =
156             $('<div>', {
157                 id: c.id
158             });
159
160         div.append(td);
161
162         c['content'] = td;
163
164         c.createCallback(td);
165     }
166
167     div.tabs({
168         heightStyle: 'fill',
169         activate: function(e, ui) {
170             var id = $('a', ui.newTab).attr('href');
171             kismet.putStorage(`kismet.base.${group}.last_tab`, id);
172         }
173     });
174
175     var lasttab = kismet.getStorage(`kismet.base.${group}.last_tab`, '');
176     $('a[href="' + lasttab + '"]', div).click();
177 }
178
179 function MoveToExpanded(tab, group) {
180     var div = $('div#' + tab.id, tabholders[group].TabDiv);
181
182     var placeholder = $('<div>', {
183         id: 'original_' + tab.id,
184     })
185     .html("Content moved to window");
186
187     var original = div;
188
189     var panelcontainer =
190         $('<div>', {
191             id: 'panel',
192             height: '100%',
193             width: '100%',
194         })
195         .append($('<div>', {
196             id: 'target',
197         }));
198
199     tab.jspanel = $.jsPanel({
200         id: 'tab_' + tab.id,
201         headerTitle: tab.tabTitle,
202         headerControls: {
203             iconfont: 'jsglyph',
204         },
205         content: panelcontainer,
206         onclosed: function() {
207             placeholder.replaceWith(original);
208             tabholders[group].TabDiv.tabs("refresh");
209         },
210     });
211
212     div.replaceWith(placeholder);
213
214     // Take out the fixed height and width imposed by tab width
215     original.removeProp('height');
216     original.removeProp('width');
217     original.css('height', '');
218     original.css('width', '');
219
220     $('#target', panelcontainer).replaceWith(original);
221
222     original.resize();
223
224     var w = $(window).width() * 0.75;
225     var h = $(window).height() * 0.5;
226     var offty = 20;
227
228     if ($(window).width() < 450 || $(window).height() < 450) {
229         w = $(window).width() - 5;
230         h = $(window).height() - 5;
231         offty = 0;
232     }
233
234     tab.jspanel.resize({
235         width: w,
236         height: h,
237     })
238     .reposition({
239         my: 'center-top',
240         at: 'center-top',
241         of: 'window',
242         offsetY: offty
243     });
244
245     // Call the tab expansion callback
246     if (('expandCallback' in tab) && tab['expandCallback'] != null) {
247         tab['expandCallback'](jspanel);
248     }
249
250 }
251
252 // Populate the sidebar content in the supplied div
253 exports.MakeTabPane = function(div, group) {
254     populateList(div, group);
255 };
256
257 // We're done loading
258 exports.load_complete = 1;
259
260 return exports;
261
262 });