foist
[kismet-logviewer.git] / logviewer / static / js / kismet.ui.settings.js
1 (
2   typeof define === "function" ? function (m) { define("kismet-ui-settings-js", m); } :
3   typeof exports === "object" ? function (m) { module.exports = m(); } :
4   function(m){ this.kismet_ui_settings = 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.settings.css'
25     });
26
27 /*
28  * Settings are stored as an array of objects which define a title and a 
29  * callback executed when the title is selected, or when the save/reset
30  * buttons are called.
31  *
32  * The settings panel is a modular alert window which implements a two-pane
33  * settings window with multiple settings categories on the left and the
34  * settings pane itself in the center.
35  *
36  */
37
38 var SettingsPanes = new Array();
39
40 /* Add a settings pane
41  *
42  * Options is a dictionary which must include:
43  *
44  * listTitle: Title shown in list
45  * windowTitle: Title appended to window (optional, if omitted, will use listTitle)
46  * create: Function creating the settings panel in a provided element
47  * save: Function for saving the panel
48  * priority: priority in list, lower is higher (optional)
49  *
50  * Settings panels should notify the when a setting is changed via
51  * kismet_ui_settings.SettingsChanged()
52  *
53  */
54 exports.AddSettingsPane = function(options) {
55     if (! 'listTitle' in options ||
56         ! 'create' in options ||
57         ! 'save' in options) {
58         return;
59     }
60
61     if (! 'priority' in options)
62         options['priority'] = 0;
63
64     SettingsPanes.push(options);
65 };
66
67 var modified = false;
68 var settingspanel = null;
69 var alertpanel = null;
70
71 /* Indicate to the settings UI that an option has been modified so that the
72  * save and reset buttons can be activated */
73 exports.SettingsModified = function(mod = true) {
74     if (mod) {
75         modified = true;
76
77         $('.k-s-button-save', settingspanel.content).button("enable");
78         $('.k-s-button-save', settingspanel.content).addClass('k-s-button-hot');
79
80         $('.k-s-button-reset', settingspanel.content).button("enable");
81     } else {
82         modified = false;
83
84         $('.k-s-button-save', settingspanel.content).button("disable");
85         $('.k-s-button-save', settingspanel.content).removeClass('k-s-button-hot');
86
87         $('.k-s-button-reset', settingspanel.content).button("disable");
88     }
89
90 }
91
92 var selected_item = null;
93
94 function clickSetting(c) {
95     if (c == selected_item)
96         return;
97
98     selected_item = c;
99
100     if ('windowTitle' in c)
101         settingspanel.headerTitle('Settings - ' + c.windowTitle);
102     else
103         settingspanel.headerTitle('Settings - ' + c.listTitle);
104
105     exports.SettingsModified(false);
106
107     $('.k-s-list-item', settingspanel.content).removeClass('k-s-list-item-active');
108     $('.k-s-list-item#sb_' + c.position, 
109         settingspanel.content).addClass('k-s-list-item-active');
110
111     populateSetting(c);
112 }
113
114 function populateSetting(c) {
115             
116     var content = $('.k-s-pane-content', settingspanel.content);
117     content.empty();
118
119     c.create(content);
120 }
121
122 function createClickCallback(c) {
123     return function() { checkClose(c); };
124 }
125
126 function populateList(list) {
127     SettingsPanes.sort(function(a, b) {
128         if (a.priority < b.priority)
129             return -1;
130         if (a.priority > b.priority)
131             return 1;
132
133         return 0;
134     });
135
136     for (var i in SettingsPanes) {
137         var c = SettingsPanes[i];
138
139         c.position = i;
140
141         list.append(
142             $('<div>', {
143                 class: 'k-s-list-item',
144                 id: 'sb_' + c.position,
145             })
146             .html(c.listTitle)
147             .on('click', createClickCallback(c))
148         );
149     }
150 }
151
152 function checkClose(transfer = null) {
153     if (!modified) {
154         if (transfer != null) {
155             clickSetting(transfer);
156             return;
157         }
158
159         return true;
160     }
161
162     if (modified) {
163         var content = $('<div>', {
164             class: 'k-s-alert'
165         })
166         .append(
167             $('<div>', {
168                 class: 'k-s-alert-content'
169             })
170             .append(
171                 $('<div>', { 
172                     class: 'k-s-alert-header'
173                 })
174                 .html("Settings changed")
175             )
176             .append(
177                 $('<div>')
178                 .html("Would you like to save the changes?")
179             )
180         )
181         .append(
182             $('<div>', {
183                 class: 'k-s-pane-buttons'
184             })
185             .append(
186                 $('<button>', {
187                     class: 'k-s-button-reset'
188                 })
189                 .text("Don't Save")
190                 .button()
191                 .on('click', function() {
192                     exports.SettingsModified(false);
193                     if (transfer != null) {
194                         alertpanel.close();
195                         clickSetting(transfer);
196                     } else {
197                         settingspanel.close();
198                     }
199                 })
200             )
201             .append(
202                 $('<button>', {
203                     class: 'k-s-button-save'
204                 })
205                 .text("Save Changes")
206                 .button()
207                 .on('click', function() {
208                     if (selected_item != null) {
209                         selected_item.save(settingspanel.content);
210                         exports.SettingsModified(false);
211                     }
212
213                     if (transfer != null) {
214                         alertpanel.close();
215                         clickSetting(transfer);
216                     } else {
217                         settingspanel.close();
218                     }
219                 })
220             )
221         );
222
223         alertpanel = $.jsPanel({
224             template: jsPanel.tplContentOnly,
225             container: settingspanel,
226             paneltype: {
227                 tooltip: true,
228                 mode: 'sticky',
229                 iconfont: 'jsglyph',
230             },
231             position: {
232                 my: 'center', 
233                 at: 'center', 
234                 of: '.k-s-container', 
235             },
236             contentSize: {
237                 width: $('.k-s-container', settingspanel.content).width() * 0.4, 
238                 height: $('.k-s-container', settingspanel.content).height() * 0.25, 
239             },
240             theme: 'red filledlight',
241             border: '2px solid',
242             show: 'animated bounceInLeft',
243             content: content,
244         });
245
246         return false;
247     }
248 }
249
250 exports.ShowSettings = function(starter) {
251     var w = $(window).width() * 0.75;
252     var h = $(window).height() * 0.75;
253
254     if (w < 450 || h < 450) {
255         w = $(window).width() - 5;
256         h = $(window).height() - 5;
257     }
258
259     var content = $('<div>', {
260         class: 'k-s-container'
261     })
262     .append(
263         $('<div>', {
264             class: 'k-s-list'
265         })
266     )
267     .append(
268         $('<div>', {
269             class: 'k-s-pane-holder'
270         })
271         .append(
272             $('<div>', {
273                 class: 'k-s-pane-content'
274             })
275             .append(
276                 $('<h3>')
277                 .html("Kismet Settings")
278             )
279             .append(
280                 $('<p>')
281                 .html('Kismet UI settings are stored in your browsers local storage, and are unique to each browser.')
282             )
283             .append(
284                 $('<p>')
285                 .html('To perform some actions (configuring data sources, downloading pcap files, and changing other server-side options), you need to be logged in.  Kismet generates a random password, which can be found in the file <code>~/.kismet/kismet_httpd.conf</code>.')
286             )
287             .append(
288                 $('<p>')
289                 .html('If you do not want to log in or are a guest on this server, you can still set local preferences and view device information.')
290             )
291         )
292         .append(
293             $('<div>', {
294                 class: 'k-s-pane-buttons'
295             })
296             .append(
297                 $('<button>', {
298                     class: 'k-s-button-reset'
299                 })
300                 .text("Reset")
301                 .button()
302                 .button("disable")
303                 .on('click', function() {
304                     if (selected_item != null) {
305                         exports.SettingsModified(false);
306                         populateSetting(selected_item);
307                     }
308                 })
309             )
310             .append(
311                 $('<button>', {
312                     class: 'k-s-button-save'
313                 })
314                 .text("Save Changes")
315                 .button()
316                 .button("disable")
317                 .on('click', function() {
318                     if (selected_item != null) {
319                         selected_item.save(content);
320                         exports.SettingsModified(false);
321                     }
322                 })
323             )
324         )
325     );
326
327     populateList($('.k-s-list', content));
328
329     selected_item = null;
330
331     settingspanel = $.jsPanel({
332         id: 'settings',
333         headerTitle: '<i class="fa fa-gear" /> Settings',
334         paneltype: 'modal',
335         headerControls: {
336             controls: 'closeonly',
337             iconfont: 'jsglyph',
338         },
339         onbeforeclose: function() {
340             return checkClose();
341         },
342         content: content,
343     }).resize({
344         width: w,
345         height: h
346     }).reposition({
347         my: 'center',
348         at: 'center',
349         of: 'window',
350     });
351
352     if (starter) {
353         for (var i in SettingsPanes) {
354             var c = SettingsPanes[i];
355
356             if (c.id == starter) {
357                 clickSetting(c);
358                 break;
359             }
360         }
361     }
362
363 };
364
365 /* Add the settings sidebar */
366 kismet_ui_sidebar.AddSidebarItem({
367     id: 'sidebar-settings',
368     listTitle: '<i class="fa fa-gear"></i> Settings',
369     priority: -100000,
370     clickCallback: function() {
371         exports.ShowSettings();
372     }
373 });
374
375
376 // We're done loading
377 exports.load_complete = 1;
378
379 return exports;
380
381 });