From 7fa5dd757eb3d0d18668a76d200cb19d51f1550a Mon Sep 17 00:00:00 2001 From: johnyma22 Date: Fri, 2 Nov 2012 15:05:47 +0000 Subject: [PATCH] remove cruft from js and move minify json to seperate file and also send emit back to server on save settings --- src/static/js/admin/minify.json.js | 61 ++++++++ src/static/js/admin/settings.js | 217 +++-------------------------- src/templates/admin/settings.html | 2 + 3 files changed, 84 insertions(+), 196 deletions(-) create mode 100644 src/static/js/admin/minify.json.js diff --git a/src/static/js/admin/minify.json.js b/src/static/js/admin/minify.json.js new file mode 100644 index 00000000..4edbd6e1 --- /dev/null +++ b/src/static/js/admin/minify.json.js @@ -0,0 +1,61 @@ +/*! JSON.minify() + v0.1 (c) Kyle Simpson + MIT License +*/ + +(function(global){ + if (typeof global.JSON == "undefined" || !global.JSON) { + global.JSON = {}; + } + + global.JSON.minify = function(json) { + + var tokenizer = /"|(\/\*)|(\*\/)|(\/\/)|\n|\r/g, + in_string = false, + in_multiline_comment = false, + in_singleline_comment = false, + tmp, tmp2, new_str = [], ns = 0, from = 0, lc, rc + ; + + tokenizer.lastIndex = 0; + + while (tmp = tokenizer.exec(json)) { + lc = RegExp.leftContext; + rc = RegExp.rightContext; + if (!in_multiline_comment && !in_singleline_comment) { + tmp2 = lc.substring(from); + if (!in_string) { + tmp2 = tmp2.replace(/(\n|\r|\s)*/g,""); + } + new_str[ns++] = tmp2; + } + from = tokenizer.lastIndex; + + if (tmp[0] == "\"" && !in_multiline_comment && !in_singleline_comment) { + tmp2 = lc.match(/(\\)*$/); + if (!in_string || !tmp2 || (tmp2[0].length % 2) == 0) { // start of string with ", or unescaped " character found to end string + in_string = !in_string; + } + from--; // include " character in next catch + rc = json.substring(from); + } + else if (tmp[0] == "/*" && !in_string && !in_multiline_comment && !in_singleline_comment) { + in_multiline_comment = true; + } + else if (tmp[0] == "*/" && !in_string && in_multiline_comment && !in_singleline_comment) { + in_multiline_comment = false; + } + else if (tmp[0] == "//" && !in_string && !in_multiline_comment && !in_singleline_comment) { + in_singleline_comment = true; + } + else if ((tmp[0] == "\n" || tmp[0] == "\r") && !in_string && !in_multiline_comment && in_singleline_comment) { + in_singleline_comment = false; + } + else if (!in_multiline_comment && !in_singleline_comment && !(/\n|\r|\s/.test(tmp[0]))) { + new_str[ns++] = tmp[0]; + } + } + new_str[ns++] = rc; + return new_str.join(""); + }; +})(this); diff --git a/src/static/js/admin/settings.js b/src/static/js/admin/settings.js index af5ec46f..efaa78fa 100644 --- a/src/static/js/admin/settings.js +++ b/src/static/js/admin/settings.js @@ -11,150 +11,40 @@ $(document).ready(function () { //connect socket = io.connect(url, {resource : resource}).of("/settings"); - $('.search-results').data('query', { - pattern: '', - offset: 0, - limit: 12, - }); - - var doUpdate = false; - - var search = function () { - socket.emit("search", $('.search-results').data('query')); - } - - function updateHandlers() { - $("#progress.dialog .close").unbind('click').click(function () { - $("#progress.dialog").hide(); - }); - - $("#do-search").unbind('click').click(function () { - var query = $('.search-results').data('query'); - query.pattern = $("#search-query")[0].value; - query.offset = 0; - search(); - }); - - $(".do-install").unbind('click').click(function (e) { - var row = $(e.target).closest("tr"); - doUpdate = true; - socket.emit("install", row.find(".name").html()); - }); - - $(".do-uninstall").unbind('click').click(function (e) { - var row = $(e.target).closest("tr"); - doUpdate = true; - socket.emit("uninstall", row.find(".name").html()); - }); - - $(".do-prev-page").unbind('click').click(function (e) { - var query = $('.search-results').data('query'); - query.offset -= query.limit; - if (query.offset < 0) { - query.offset = 0; - } - search(); - }); - $(".do-next-page").unbind('click').click(function (e) { - var query = $('.search-results').data('query'); - var total = $('.search-results').data('total'); - if (query.offset + query.limit < total) { - query.offset += query.limit; - } - search(); - }); - } - - updateHandlers(); -/* - socket.on('progress', function (data) { - if (data.progress > 0 && $('#progress.dialog').data('progress') > data.progress) return; - - $("#progress.dialog .close").hide(); - $("#progress.dialog").show(); - - $('#progress.dialog').data('progress', data.progress); - - var message = "Unknown status"; - if (data.message) { - message = "" + data.message.toString() + ""; - } - if (data.error) { - message = "" + data.error.toString() + ""; - } - $("#progress.dialog .message").html(message); - $("#progress.dialog .history").append("
" + message + "
"); - - if (data.progress >= 1) { - if (data.error) { - $("#progress.dialog .close").show(); - } else { - if (doUpdate) { - doUpdate = false; - socket.emit("load"); - } - $("#progress.dialog").hide(); - } - } - }); - - socket.on('search-result', function (data) { - var widget=$(".search-results"); - - widget.data('query', data.query); - widget.data('total', data.total); - - widget.find('.offset').html(data.query.offset); - widget.find('.limit').html(data.query.offset + data.query.limit); - widget.find('.total').html(data.total); - - widget.find(".results *").remove(); - for (plugin_name in data.results) { - var plugin = data.results[plugin_name]; - var row = widget.find(".template tr").clone(); - - for (attr in plugin) { - row.find("." + attr).html(plugin[attr]); - } - widget.find(".results").append(row); - } - - updateHandlers(); - }); -*/ - socket.on('settings', function (settings) { /* Check to make sure the JSON is clean before proceeding */ - if(isJSONClean(settings.results)) { $('.settings').append(settings.results); + $('.settings').focus(); } else{ - alert("YOUR JSON IS BAD AND YOU SHOULD FEEL BAD") + alert("YOUR JSON IS BAD AND YOU SHOULD FEEL BAD"); } - - $('#saveSettings').on('click', function(){ - var editedSettings = $('.settings').val(); - if(isJSONClean(editedSettings)){ - // JSON is clean so emit it to the server - }else{ - alert("YOUR JSON IS BAD AND YOU SHOULD FEEL BAD") - } - }); - - $('#restartEtherpad').on('click', function(){ - - }); - - }); - socket.emit("load"); - search(); + /* When the admin clicks save Settings check the JSON then send the JSON back to the server */ + $('#saveSettings').on('click', function(){ + var editedSettings = $('.settings').val(); + if(isJSONClean(editedSettings)){ + // JSON is clean so emit it to the server + socket.emit("saveSettings", $('.settings').val()); + }else{ + alert("YOUR JSON IS BAD AND YOU SHOULD FEEL BAD") + $('.settings').focus(); + } + }); + + /* Tell Etherpad Server to restart */ + $('#restartEtherpad').on('click', function(){ + socket.emit("restartEtherpad"); + }); + + socket.emit("load"); // Load the JSON from the server }); + function isJSONClean(data){ var cleanSettings = JSON.minify(data); try{ @@ -169,68 +59,3 @@ function isJSONClean(data){ return true; } } - - -/* Strip crap out of JSON */ -/*! JSON.minify() - v0.1 (c) Kyle Simpson - MIT License - https://github.com/getify/JSON.minify -*/ - -(function(global){ - if (typeof global.JSON == "undefined" || !global.JSON) { - global.JSON = {}; - } - - global.JSON.minify = function(json) { - - var tokenizer = /"|(\/\*)|(\*\/)|(\/\/)|\n|\r/g, - in_string = false, - in_multiline_comment = false, - in_singleline_comment = false, - tmp, tmp2, new_str = [], ns = 0, from = 0, lc, rc - ; - - tokenizer.lastIndex = 0; - - while (tmp = tokenizer.exec(json)) { - lc = RegExp.leftContext; - rc = RegExp.rightContext; - if (!in_multiline_comment && !in_singleline_comment) { - tmp2 = lc.substring(from); - if (!in_string) { - tmp2 = tmp2.replace(/(\n|\r|\s)*/g,""); - } - new_str[ns++] = tmp2; - } - from = tokenizer.lastIndex; - - if (tmp[0] == "\"" && !in_multiline_comment && !in_singleline_comment) { - tmp2 = lc.match(/(\\)*$/); - if (!in_string || !tmp2 || (tmp2[0].length % 2) == 0) { // start of string with ", or unescaped " character found to end string - in_string = !in_string; - } - from--; // include " character in next catch - rc = json.substring(from); - } - else if (tmp[0] == "/*" && !in_string && !in_multiline_comment && !in_singleline_comment) { - in_multiline_comment = true; - } - else if (tmp[0] == "*/" && !in_string && in_multiline_comment && !in_singleline_comment) { - in_multiline_comment = false; - } - else if (tmp[0] == "//" && !in_string && !in_multiline_comment && !in_singleline_comment) { - in_singleline_comment = true; - } - else if ((tmp[0] == "\n" || tmp[0] == "\r") && !in_string && !in_multiline_comment && in_singleline_comment) { - in_singleline_comment = false; - } - else if (!in_multiline_comment && !in_singleline_comment && !(/\n|\r|\s/.test(tmp[0]))) { - new_str[ns++] = tmp[0]; - } - } - new_str[ns++] = rc; - return new_str.join(""); - }; -})(this); diff --git a/src/templates/admin/settings.html b/src/templates/admin/settings.html index 7a7cc6d6..eaaf094c 100644 --- a/src/templates/admin/settings.html +++ b/src/templates/admin/settings.html @@ -5,7 +5,9 @@ + +