From c7bfdbfcd187cf08fa1023770a67420a64203391 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 31 Jan 2017 15:28:02 +0000 Subject: [PATCH] [WebUI] Further fixes --- interface/js/app/rspamd.js | 796 ++++++++++++------------------------- interface/js/app/stats.js | 4 +- interface/js/main.js | 3 +- 3 files changed, 249 insertions(+), 554 deletions(-) diff --git a/interface/js/app/rspamd.js b/interface/js/app/rspamd.js index cfa531416..800ad20fe 100644 --- a/interface/js/app/rspamd.js +++ b/interface/js/app/rspamd.js @@ -22,37 +22,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -define(['jquery', 'd3evolution','d3pie','visibility','datatables', 'humanize'], - function ($, D3Evolution, d3pie, visibility, DataTable, Humanize) { +define(['jquery', 'visibility', 'bootstrap'], + function ($, D3Evolution, d3pie, visibility, DataTable) { // begin - var pie; - var rrd_pie; - var history; - var errors; - var graph; - var symbols; + var graphs = {}; + var tables = {}; var read_only = false; var neighbours = []; //list of clusters var checked_server = "All SERVERS"; + var interface = {}; var timer_id = []; var selData; // Graph's dataset selector state - // Bind event handlers to selectors - $("#selData").change(function () { - selData = this.value; - tabClick("#throughput_nav"); - }); - $("#selConvert").change(function () { - graph.convert(this.value); - }); - $("#selType").change(function () { - graph.type(this.value); - }); - $("#selInterpolate").change(function () { - graph.interpolate(this.value); - }); - function stopTimers() { for (var key in timer_id) { Visibility.stop(timer_id[key]); @@ -60,31 +42,34 @@ define(['jquery', 'd3evolution','d3pie','visibility','datatables', 'humanize'], } function disconnect() { - if (pie) { - pie.destroy(); + if (graphs.chart) { + graphs.chart.destroy(); + graphs.chart = undefined; } - if (rrd_pie) { - rrd_pie.destroy(); + if (graphs.rrd_pie) { + graphs.rrd_pie.destroy(); + graphs.rrd_pie = undefined; } - if (graph) { - graph.destroy(); - graph = undefined; + if (graphs.graph) { + graphs.graph.destroy(); + graphs.graph = undefined; } - if (history) { - history.destroy(); + if (tables.history) { + tables.history.destroy(); + tables.history = undefined; } - if (errors) { - errors.destroy(); + if (tables.errors) { + tables.errors.destroy(); + tables.errors = undefined; } - if (symbols) { - symbols.destroy(); - symbols = null; + if (tables.symbols) { + tables.symbols.destroy(); + tables.symbols = undefined; } + stopTimers(); cleanCredentials(); - connectRSPAMD(); - // window.location.reload(); - return false; + interface.connect(); } function tabClick(tab_id) { @@ -99,14 +84,18 @@ define(['jquery', 'd3evolution','d3pie','visibility','datatables', 'humanize'], switch (tab_id) { case "#status_nav": - statWidgets(); + require(['app/stats'], function(stats) { + stats.statWidgets(interface, graphs, checked_server); + }); timer_id.status = Visibility.every(10000, function () { - statWidgets(); + require(['app/stats'], function(stats) { + stats.statWidgets(interface, graphs, checked_server); + }); }); break; case "#throughput_nav": getGraphData(selData); - const autoRefresh = { + var autoRefresh = { hourly: 60000, daily: 300000 }; @@ -137,29 +126,16 @@ define(['jquery', 'd3evolution','d3pie','visibility','datatables', 'humanize'], }, 1000); } - // @supports session storage - function supportsSessionStorage() { - return typeof (Storage) !== "undefined"; - } // @return password function getPassword() { return sessionStorage.getItem('Password'); } - // @detect session storate - supportsSessionStorage(); // @save credentials function saveCredentials(password) { sessionStorage.setItem('Password', password); } - // @update credentials - function saveActions(data) { - sessionStorage.setItem('Actions', JSON.stringify(data)); - } - // @update credentials - function saveMaps(data) { - sessionStorage.setItem('Maps', JSON.stringify(data)); - } + // @clean credentials function cleanCredentials() { sessionStorage.clear(); @@ -178,8 +154,62 @@ define(['jquery', 'd3evolution','d3pie','visibility','datatables', 'humanize'], } return false; } - // @alert popover - function alertMessage(alertState, alertText) { + + function displayUI() { + // @toggle auth and main + var disconnect = $('#navBar .pull-right'); + $('#mainUI').show(); + $('#progress').show(); + $(disconnect).show(); + tabClick("#refresh"); + $('#progress').hide(); + } + + // Public functions + interface.setup = function() { + // Bind event handlers to selectors + $("#selData").change(function () { + selData = this.value; + tabClick("#throughput_nav"); + }); + $("#selConvert").change(function () { + graph.convert(this.value); + }); + $("#selType").change(function () { + graph.type(this.value); + }); + $("#selInterpolate").change(function () { + graph.interpolate(this.value); + }); + $.ajaxSetup({ + timeout: 2000, + jsonp: false + }); + + $(document).ajaxStart(function () { + $('#navBar').addClass('loading'); + }); + $(document).ajaxComplete(function () { + setTimeout(function () { + $('#navBar').removeClass('loading'); + }, 1000); + }); + + $('a[data-toggle="tab"]').on('click', function (e) { + var tab_id = "#" + $(e.target).attr("id"); + tabClick(tab_id); + }); + + // Radio buttons + $(document).on('click', 'input:radio[name="clusterName"]', function () { + if (!this.disabled) { + checked_server = this.value; + tabClick("#status_nav"); + } + }); + }; + + interface.alertMessage = function (alertState, alertText) { if ($('.alert').is(':visible')) { $(alert).hide().remove(); } @@ -192,208 +222,86 @@ define(['jquery', 'd3evolution','d3pie','visibility','datatables', 'humanize'], $(alert).remove(); }, 3600); } - // @get maps id - function getMaps() { - var items = []; - $('#listMaps').closest('.widget-box').hide(); - $.ajax({ - dataType: 'json', - url: 'maps', - jsonp: false, - beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - }, - error: function (data) { - alertMessage('alert-modal alert-error', data.statusText); - }, - success: function (data) { - $('#listMaps').empty(); - saveMaps(data); - getMapById(); - $.each(data, function (i, item) { - var caption; - var label; - if ((item.editable === false || read_only)) { - caption = 'View'; - label = 'Read'; - } else { - caption = 'Edit'; - label = 'Read Write'; - } - items.push('' + - '' + label + '' + - '' + - '' + item.uri + '' + - '' + - '' + - item.description + - '' + - ''); - }); - $('', { - html: items.join('') - }).appendTo('#listMaps'); - $('#listMaps').closest('.widget-box').show(); + + interface.connect = function() { + if (isLogged()) { + var data = JSON.parse(sessionStorage.getItem('Credentials')); + + if (data && data[checked_server].read_only) { + read_only = true; + $('#learning_nav').hide(); + $('#resetHistory').attr('disabled', true); + $('#errors-history').hide(); + } + else { + read_only = false; + $('#learning_nav').show(); + $('#resetHistory').removeAttr('disabled', true); + } + displayUI(); + return; + } + + var ui = $('#mainUI'); + var dialog = $('#connectDialog'); + var backdrop = $('#backDrop'); + $(ui).hide(); + $(dialog).show(); + $(backdrop).show(); + $('#connectPassword').focus(); + $('#connectForm').off('submit'); + + $('#connectForm').on('submit', function (e) { + e.preventDefault(); + var password = $('#connectPassword').val(); + if (!/^[\u0000-\u007f]*$/.test(password)) { + alertMessage('alert-modal alert-error', 'Invalid characters in the password'); + $('#connectPassword').focus(); + return; } - }); - } - // @get map by id - function getMapById() { - var data = JSON.parse(sessionStorage.getItem('Maps')); - $('#modalBody').empty(); - $.each(data, function (i, item) { $.ajax({ - dataType: 'text', - url: 'getmap', + global: false, jsonp: false, + dataType: 'json', + type: 'GET', + url: 'auth', beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - xhr.setRequestHeader('Map', item.map); - }, - error: function () { - alertMessage('alert-error', 'Cannot receive maps data'); + xhr.setRequestHeader('Password', password); }, - success: function (text) { - var disabled = ''; - if ((item.editable === false || read_only)) { - disabled = 'disabled="disabled"'; - } + success: function (data) { + $('#connectPassword').val(''); + if (data.auth === 'failed') { + // Is actually never returned by Rspamd + } else { + if (data.read_only) { + read_only = true; + $('#learning_nav').hide(); + $('#resetHistory').attr('disabled', true); + $('#errors-history').hide(); + } + else { + read_only = false; + $('#learning_nav').show(); + $('#resetHistory').removeAttr('disabled', true); + } - $('