From caa7bfce9b881b983b9e2e53ac458a5b641dbab1 Mon Sep 17 00:00:00 2001 From: Piro / SHIMODA Hiroshi Date: Tue, 1 Mar 2011 02:35:57 +0900 Subject: [PATCH] update library --- modules/lib/confirmWithTab.js | 61 ++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/modules/lib/confirmWithTab.js b/modules/lib/confirmWithTab.js index 8afc820f..9c9c53c5 100644 --- a/modules/lib/confirmWithTab.js +++ b/modules/lib/confirmWithTab.js @@ -1,7 +1,45 @@ /** * @fileOverview Tab Related Confirimation Library for Firefox 3.5 or later * @author SHIMODA "Piro" Hiroshi - * @version 3 + * @version 4 + * Basic usage: + * + * @example + * Components.utils.import('resource://my-modules/confirmWithTab.js'); + * + * var checkbox = { + * label : 'Never ask me', + * checked : false + * }; + * + * confirmWithTab({ + * tab : gBrowser.selectedTab, // the related tab + * label : 'Ara you ready?', // the message + * value : 'treestyletab-undo-close-tree', // the internal key (optional) + * buttons : ['Yes', 'No'], // button labels + * checkbox : checkbox, // checkbox definition (optional) + * cancelEvents : ['SSTabRestoring'] // events to cancel this deferred (optional) + * }) + * .next(function(aButtonIndex) { + * // the next callback receives the index of the clicked button. + * switch (aButtonIndex) { + * case 0: // Yes + * ... + * break; + * case 1: // No + * ... + * break; + * } + * // after the notification bar is closed, "checked" indicates + * // the state of the checkbox when the user clicks a button. + * var checked = checkbox.checked; + * ... + * }) + * .error(function(aError) { + * // if the tab is closed, or any event in the cancelEvents array + * // is fired, then an exception is raised. + * ... + * }); * * @license * The MIT License, Copyright (c) 2010-2011 SHIMODA "Piro" Hiroshi @@ -34,7 +72,7 @@ if (typeof namespace.Deferred == 'undefined') var confirmWithTab; (function() { - const currentRevision = 3; + const currentRevision = 4; var loadedRevision = 'confirmWithTab' in namespace ? namespace.confirmWithTab.revision : @@ -49,6 +87,19 @@ var confirmWithTab; confirmWithTab = function confirmWithTab(aOptions) { var deferred = new namespace.Deferred(); + if (!aOptions.buttons) { + return deferred + .next(function() { + throw new Error('confirmWithTab requires one or more buttons!'); + }); + } + + aOptions.cancelEvents = (aOptions.cancelEvents || []) + .concat(['TabClose']) + .sort() + .join('\n') + .replace(/^(.+)$\n(\1\n)+/gm, '$1\n') + .split('\n'); var tab = aOptions.tab; var b = getTabBrowserFromChild(tab); @@ -57,8 +108,8 @@ var confirmWithTab; var numericAccessKey = 0; var notification = box.appendNotification( aOptions.label, - aOptions.value, - aOptions.image, + aOptions.value || 'confirmWithTab-'+encodeURIComponent(aOptions.label), + aOptions.image || null, (aOptions.priority ? (typeof aOptions.priority == 'number' ? aOptions.priority : box[aOptions.priority] ) : box.PRIORITY_INFO_MEDIUM @@ -124,7 +175,7 @@ var confirmWithTab; var strip = b.tabContainer || b.mTabContainer; var handleEvent = function handleEvent(aEvent) { - if (aEvent.type == 'DOMNodeRemoved' && !aEvent.target != notification) + if (aEvent.type == 'DOMNodeRemoved' && aEvent.target != notification) return; if (aOptions.cancelEvents) aOptions.cancelEvents.forEach(function(aEventType) {