From 72bfb94580418c010931bb5ccf461f17bd4334e6 Mon Sep 17 00:00:00 2001 From: Samit Badle Date: Sun, 14 Sep 2014 17:08:14 +0200 Subject: [PATCH] Creating PromptService so that the XUL specific calls can be encapsulated and hidden away. Also fixing the incorrect doctype --- .../content/browser/mozilla/prompt-service.js | 54 +++++++++++++++++++ ide/main/src/content/editor.js | 32 +++++------ ide/main/src/content/selenium-ide-common.xul | 6 +-- 3 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 ide/main/src/content/browser/mozilla/prompt-service.js diff --git a/ide/main/src/content/browser/mozilla/prompt-service.js b/ide/main/src/content/browser/mozilla/prompt-service.js new file mode 100644 index 0000000000000..72aa4cbc225fd --- /dev/null +++ b/ide/main/src/content/browser/mozilla/prompt-service.js @@ -0,0 +1,54 @@ +/* + Encapsulate the low level calls into a service that is functionally useful and avoids use of cryptic numbers + */ +var PromptService = (function() { + function PromptService() { + } + + PromptService.prototype.svc = function() { + if (!this._svc) { + this._svc = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService); + + this.saveFlags = this._svc.BUTTON_TITLE_SAVE * this._svc.BUTTON_POS_0 + + this._svc.BUTTON_TITLE_CANCEL * this._svc.BUTTON_POS_1 + + this._svc.BUTTON_TITLE_DONT_SAVE * this._svc.BUTTON_POS_2; + + this.yesNoFlags = this._svc.BUTTON_TITLE_YES * this._svc.BUTTON_POS_0 + + this._svc.BUTTON_TITLE_NO * this._svc.BUTTON_POS_1; + + this.yesNoCancelFlags = this._svc.BUTTON_TITLE_YES * this._svc.BUTTON_POS_0 + + this._svc.BUTTON_TITLE_CANCEL * this._svc.BUTTON_POS_1 + + this._svc.BUTTON_TITLE_NO * this._svc.BUTTON_POS_2; + } + return this._svc; + }; + + PromptService.prototype.save = function(prompt, title) { + title = title || "Save?"; + var btn = this.svc().confirmEx(window, title, prompt, this.saveFlags, null, null, null, null, {}); + return { + save: btn == 0, + cancel: btn == 1, + dontSave: btn == 2 + }; + }; + + PromptService.prototype.yesNo = function(prompt, title) { + var btn = this.svc().confirmEx(window, title, prompt, this.yesNoFlags, null, null, null, null, {}); + return { + yes: btn == 0, + no: btn == 1 + }; + }; + + PromptService.prototype.yesNoCancel = function(prompt, title) { + var btn = this.svc().confirmEx(window, title, prompt, this.yesNoCancelFlags, null, null, null, null, {}); + return { + yes: btn == 0, + no: btn == 2, + cancel: btn == 1 + }; + }; + + return new PromptService(); +})(); diff --git a/ide/main/src/content/editor.js b/ide/main/src/content/editor.js index feee439d53bb6..a8922aa5afc1d 100644 --- a/ide/main/src/content/editor.js +++ b/ide/main/src/content/editor.js @@ -469,28 +469,20 @@ Editor.prototype.confirmClose = function () { "Would you like to save the " + changedTestCases + " changed test case/s?", "Would you like to save the test suite and the " + changedTestCases + " changed test case/s?" ][promptType]; - var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService); - var flags = - promptService.BUTTON_TITLE_SAVE * promptService.BUTTON_POS_0 + - promptService.BUTTON_TITLE_CANCEL * promptService.BUTTON_POS_1 + - promptService.BUTTON_TITLE_DONT_SAVE * promptService.BUTTON_POS_2; - - var result = promptService.confirmEx(window, "Save?", prompt, flags, null, null, null, null, {}); - - switch (result) { - case 0: - if (curSuite.isTempSuite()) { - //For temp suites, just save the test case (as there is only one test case) - return this.saveTestCase(); - } - //For all others, save the suite (perhaps unnecessary) and all test cases that have changed - return this.app.saveTestSuite(true); - case 1: - return false; - case 2: - return true; + var result = PromptService.save(prompt, "Save?"); + if (result.save) { + if (curSuite.isTempSuite()) { + //For temp suites, just save the test case (as there is only one test case) + return this.saveTestCase(); + } + //For all others, save the suite (perhaps unnecessary) and all test cases that have changed + return this.app.saveTestSuite(true); + } else if (result.cancel) { + return false; } + //result.dontSave + return true; } } else { //TODO: Why is there no current suite??? diff --git a/ide/main/src/content/selenium-ide-common.xul b/ide/main/src/content/selenium-ide-common.xul index 17c0784f5f91c..efc99b9377624 100644 --- a/ide/main/src/content/selenium-ide-common.xul +++ b/ide/main/src/content/selenium-ide-common.xul @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - %browserDTD; @@ -23,8 +23,7 @@ limitations under the License. %seleniumIdeDTD; ]> - +