From fc45a6177b46f6da7dd56d7bf142c2a12df11a36 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Tue, 28 Jun 2011 23:34:12 +0200 Subject: [PATCH] Newscript: Validate that name and id are provided If no name or id are given, accepting the dialog will fail and the corresponding fields will be highlighted. Also, the required fields got a placeholder text now, asking the user to specifiy the text. (Note: Not using HTML5 Form validation here, as this does not work reliably when intermixed with XUL) --- extension/content/js/newscript.js | 25 ++++++++++++++++++++- extension/content/newscript.xul | 4 ++-- extension/locale/de/scriptish.properties | 2 ++ extension/locale/en-US/scriptish.properties | 2 ++ extension/locale/es-ES/scriptish.properties | 2 ++ extension/locale/ja-JP/scriptish.properties | 2 ++ extension/locale/pt-BR/scriptish.properties | 2 ++ extension/locale/ru-RU/scriptish.properties | 2 ++ extension/locale/sv-SE/scriptish.properties | 2 ++ extension/skin/newscript.css | 16 +++++++++++++ 10 files changed, 56 insertions(+), 3 deletions(-) diff --git a/extension/content/js/newscript.js b/extension/content/js/newscript.js index 487f958a..ae68954c 100644 --- a/extension/content/js/newscript.js +++ b/extension/content/js/newscript.js @@ -6,6 +6,8 @@ Components.utils.import("resource://scriptish/utils/Scriptish_localizeDOM.js"); Components.utils.import("resource://scriptish/utils/Scriptish_stringBundle.js"); var $ = function(aID) document.getElementById(aID); +var $$ = function(q) document.querySelector(q); +var $$$ = function(q) document.querySelectorAll(q); Scriptish_localizeOnLoad(this); @@ -17,7 +19,17 @@ try { } catch(e) {} window.addEventListener("load", function() { - $("scriptish").addEventListener("dialogaccept", function() doInstall(), false); + $("scriptish").addEventListener("dialogaccept", function(evt) { + try { + if (doInstall()) { + return true; + } + } catch (e) {} + + evt.preventDefault(); + evt.stopProgapation(); + return false; + }, false); // load defaults $("id").value = Scriptish_prefRoot.getValue("newscript_id", ""); @@ -48,6 +60,17 @@ window.addEventListener("load", function() { }, false); function doInstall() { + // validate some + var ok = Array.reduce($$$("*[required]"), function(p,v) { + var vok = !!v.value; + v.setAttribute("invalid", !vok); + return p & vok; + }, true); + + if (!ok) { + return false; + } + var tools = {}; Components.utils.import("resource://scriptish/utils/Scriptish_openInEditor.js", tools); Components.utils.import("resource://scriptish/utils/Scriptish_getTempFile.js", tools); diff --git a/extension/content/newscript.xul b/extension/content/newscript.xul index 7290f774..0950aa86 100644 --- a/extension/content/newscript.xul +++ b/extension/content/newscript.xul @@ -18,12 +18,12 @@