From 5f64ec44e6ddb3d88143cc80925676d34a2c0589 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 31 Jan 2017 17:16:49 +0000 Subject: [WebUI] Move configuration tab to a separate module --- interface/js/app/config.js | 284 +++++++++++++++++++++++++++++++++++++++++++++ interface/js/app/graph.js | 2 +- interface/js/app/rspamd.js | 276 +++---------------------------------------- 3 files changed, 299 insertions(+), 263 deletions(-) create mode 100644 interface/js/app/config.js (limited to 'interface/js') diff --git a/interface/js/app/config.js b/interface/js/app/config.js new file mode 100644 index 000000000..903874d16 --- /dev/null +++ b/interface/js/app/config.js @@ -0,0 +1,284 @@ +/* + The MIT License (MIT) + + Copyright (C) 2017 Vsevolod Stakhov + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +define(['jquery'], +function($) { + var interface = {} + + function save_map_success(rspamd) { + rspamd.alertMessage('alert-modal alert-success', 'Map data successfully saved'); + $('#modalDialog').modal('hide'); + } + function save_map_error(rspamd, serv, jqXHR, textStatus, errorThrown) { + rspamd.alertMessage('alert-modal alert-error', 'Save map error on ' + + serv.name + ': ' + errorThrown); + } + // @upload map from modal + function saveMap(rspamd, action, id) { + var data = $('#' + id).find('textarea').val(); + $.ajax({ + data: data, + dataType: 'text', + type: 'POST', + jsonp: false, + url: action, + beforeSend: function (xhr) { + xhr.setRequestHeader('Password', rspamd.getPassword()); + xhr.setRequestHeader('Map', id); + xhr.setRequestHeader('Debug', true); + }, + error: function (data) { + save_map_error(rspamd, 'local', null, null, data.statusText); + }, + success: function() {save_map_success(rspamd)}, + }); + } + + // @get maps id + function getMaps(rspamd) { + var items = []; + $('#listMaps').closest('.widget-box').hide(); + $.ajax({ + dataType: 'json', + url: 'maps', + jsonp: false, + beforeSend: function (xhr) { + xhr.setRequestHeader('Password', rspamd.getPassword()); + }, + error: function (data) { + rspamd.alertMessage('alert-modal alert-error', data.statusText); + }, + success: function (data) { + $('#listMaps').empty(); + $('#modalBody').empty(); + + $.each(data, function (i, item) { + var caption; + var label; + getMapById(rspamd, item); + if ((item.editable === false || rspamd.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(); + } + }); + } + // @get map by id + function getMapById(rspamd, item) { + $.ajax({ + dataType: 'text', + url: 'getmap', + jsonp: false, + beforeSend: function (xhr) { + xhr.setRequestHeader('Password', rspamd.getPassword()); + xhr.setRequestHeader('Map', item.map); + }, + error: function () { + rspamd.alertMessage('alert-error', 'Cannot receive maps data'); + }, + success: function (text) { + var disabled = ''; + if ((item.editable === false || rspamd.read_only)) { + disabled = 'disabled="disabled"'; + } + + $(''); + if (rspamd.read_only) { + $('#actionsFormField').attr('disabled', true); + } + } + }); + } + // @upload edited actions + interface.setup = function(rspamd) { + $('#actionsForm').change('submit', function () { + var inputs = $('#actionsForm :input[type="slider"]'); + var url = 'saveactions'; + var values = []; + // Rspamd order: [spam,probable_spam,greylist] + values[0] = parseFloat(inputs[2].value); + values[1] = parseFloat(inputs[1].value); + values[2] = parseFloat(inputs[0].value); + $.ajax({ + data: JSON.stringify(values), + dataType: 'json', + type: 'POST', + url: url, + jsonp: false, + beforeSend: function (xhr) { + xhr.setRequestHeader('Password', rspamd.getPassword()); + }, + success: function () { + alertMessage('alert-success', 'Actions successfully saved'); + }, + error: function (data) { + alertMessage('alert-modal alert-error', data.statusText); + } + }); + return false; + }); + // Modal form for maps + $(document).on('click', '[data-toggle="modal"]', function () { + var source = $(this).data('source'); + var editable = $(this).data('editable'); + var title = $(this).data('title'); + $('#modalTitle').html(title); + $('#modalBody ' + source).show(); + var target = $(this).data('target'); + $(target + ' .progress').hide(); + $(target).modal(show = true, backdrop = true, keyboard = show); + if (editable === false) { + $('#modalSave').hide(); + $('#modalSaveAll').hide(); + } else { + $('#modalSave').show(); + $('#modalSaveAll').show(); + } + return false; + }); + // close modal without saving + $('[data-dismiss="modal"]').on('click', function () { + $('#modalBody form').hide(); + }); + // @save forms from modal + $('#modalSave').on('click', function () { + var form = $('#modalBody').children().filter(':visible'); + // var map = $(form).data('map'); + // var type = $(form).data('type'); + var action = $(form).attr('action'); + var id = $(form).attr('id'); + saveMap(rspamd, action, id); + }); + $('#modalSaveAll').on('click', function () { + var form = $('#modalBody').children().filter(':visible'); + // var map = $(form).data('map'); + // var type = $(form).data('type'); + var action = $(form).attr('action'); + var id = $(form).attr('id'); + var data = $('#' + id).find('textarea').val(); + rspamd.queryNeighbours(action, save_map_success, save_map_error, "POST", { + "Map": id, + }, { + data: data, + dataType: "text", + }); + }); + } + + interface.getActions = getActions; + interface.getMaps = getMaps; + + return interface; +}); \ No newline at end of file diff --git a/interface/js/app/graph.js b/interface/js/app/graph.js index d25648557..3ff73956d 100644 --- a/interface/js/app/graph.js +++ b/interface/js/app/graph.js @@ -23,7 +23,7 @@ THE SOFTWARE. */ -define('app/graph', ['jquery', 'd3evolution', 'datatables'], +define(['jquery', 'd3evolution', 'datatables'], function($, D3Evolution, unused) { var rrd_pie_config = { header: {}, diff --git a/interface/js/app/rspamd.js b/interface/js/app/rspamd.js index b827f79ad..fcf462dfd 100644 --- a/interface/js/app/rspamd.js +++ b/interface/js/app/rspamd.js @@ -22,15 +22,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph'], - function ($, d3pie, visibility, tab_stat, tab_graph) { +define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph', 'app/config'], + function ($, d3pie, visibility, tab_stat, tab_graph, tab_config) { // begin var graphs = {}; var tables = {}; - var read_only = false; var neighbours = []; //list of clusters var checked_server = "All SERVERS"; - var interface = {}; + var interface = { + read_only: false, + }; var timer_id = []; var selData; // Graph's dataset selector state @@ -101,9 +102,8 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph'], }); break; case "#configuration_nav": - getActions(); - $('#modalBody').empty(); - getMaps(); + tab_config.getActions(interface); + tab_config.getMaps(interface); break; case "#symbols_nav": getSymbols(); @@ -195,6 +195,7 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph'], tabClick("#status_nav"); } }); + tab_config.setup(interface); }; interface.alertMessage = function (alertState, alertText) { @@ -216,13 +217,13 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph'], var data = JSON.parse(sessionStorage.getItem('Credentials')); if (data && data[checked_server].read_only) { - read_only = true; + interface.read_only = true; $('#learning_nav').hide(); $('#resetHistory').attr('disabled', true); $('#errors-history').hide(); } else { - read_only = false; + interface.read_only = false; $('#learning_nav').show(); $('#resetHistory').removeAttr('disabled', true); } @@ -263,13 +264,13 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph'], // Is actually never returned by Rspamd } else { if (data.read_only) { - read_only = true; + interface.read_only = true; $('#learning_nav').hide(); $('#resetHistory').attr('disabled', true); $('#errors-history').hide(); } else { - read_only = false; + interface.read_only = false; $('#learning_nav').show(); $('#resetHistory').removeAttr('disabled', true); } @@ -473,112 +474,7 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph'], // @alert popover - // @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(); - } - }); - } - // @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', - jsonp: false, - beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - xhr.setRequestHeader('Map', item.map); - }, - error: function () { - alertMessage('alert-error', 'Cannot receive maps data'); - }, - success: function (text) { - var disabled = ''; - if ((item.editable === false || read_only)) { - disabled = 'disabled="disabled"'; - } - - $(''); - if (read_only) { - $('#actionsFormField').attr('disabled', true); - } - } - }); - } - // @upload edited actions - $('#actionsForm').on('submit', function () { - var inputs = $('#actionsForm :input[type="slider"]'); - var url = 'saveactions'; - var values = []; - // Rspamd order: [spam,probable_spam,greylist] - values[0] = parseFloat(inputs[2].value); - values[1] = parseFloat(inputs[1].value); - values[2] = parseFloat(inputs[0].value); - $.ajax({ - data: JSON.stringify(values), - dataType: 'json', - type: 'POST', - url: url, - jsonp: false, - beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - }, - success: function () { - alertMessage('alert-success', 'Actions successfully saved'); - }, - error: function (data) { - alertMessage('alert-modal alert-error', data.statusText); - } - }); - return false; - }); // @watch textarea changes $('textarea').change(function () { if ($(this).val().length !== '') { @@ -1145,57 +948,6 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph'], $(this).closest('form').find('button').attr('disabled').addClass('disabled'); } }); - function save_map_success() { - alertMessage('alert-modal alert-success', 'Map data successfully saved'); - $('#modalDialog').modal('hide'); - } - function save_map_error(serv, jqXHR, textStatus, errorThrown) { - alertMessage('alert-modal alert-error', 'Save map error on ' + - serv.name + ': ' + errorThrown); - } - // @save forms from modal - $('#modalSave').on('click', function () { - var form = $('#modalBody').children().filter(':visible'); - // var map = $(form).data('map'); - // var type = $(form).data('type'); - var action = $(form).attr('action'); - var id = $(form).attr('id'); - saveMap(action, id); - }); - $('#modalSaveAll').on('click', function () { - var form = $('#modalBody').children().filter(':visible'); - // var map = $(form).data('map'); - // var type = $(form).data('type'); - var action = $(form).attr('action'); - var id = $(form).attr('id'); - var data = $('#' + id).find('textarea').val(); - interface.queryNeighbours(action, save_map_success, save_map_error, "POST", { - "Map": id, - }, { - data: data, - dataType: "text", - }); - }); - // @upload map from modal - function saveMap(action, id) { - var data = $('#' + id).find('textarea').val(); - $.ajax({ - data: data, - dataType: 'text', - type: 'POST', - jsonp: false, - url: action, - beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - xhr.setRequestHeader('Map', id); - xhr.setRequestHeader('Debug', true); - }, - error: function (data) { - save_map_error('local', null, null, data.statusText); - }, - success: save_map_success, - }); - } // @upload symbols from modal function saveSymbols(action, id, is_cluster) { -- cgit v1.2.3