browser/modules/test/uitour.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 // Copied from the proposed JS library for Bedrock (ie, www.mozilla.org).
     7 // create namespace
     8 if (typeof Mozilla == 'undefined') {
     9 	var Mozilla = {};
    10 }
    12 (function($) {
    13   'use strict';
    15 	// create namespace
    16 	if (typeof Mozilla.UITour == 'undefined') {
    17 		Mozilla.UITour = {};
    18 	}
    20 	var themeIntervalId = null;
    21 	function _stopCyclingThemes() {
    22 		if (themeIntervalId) {
    23 			clearInterval(themeIntervalId);
    24 			themeIntervalId = null;
    25 		}
    26 	}
    28 	function _sendEvent(action, data) {
    29 		var event = new CustomEvent('mozUITour', {
    30 			bubbles: true,
    31 			detail: {
    32 				action: action,
    33 				data: data || {}
    34 			}
    35 		});
    37 		document.dispatchEvent(event);
    38 	}
    40 	function _generateCallbackID() {
    41 		return Math.random().toString(36).replace(/[^a-z]+/g, '');
    42 	}
    44 	function _waitForCallback(callback) {
    45 		var id = _generateCallbackID();
    47 		function listener(event) {
    48 			if (typeof event.detail != "object")
    49 				return;
    50 			if (event.detail.callbackID != id)
    51 				return;
    53 			document.removeEventListener("mozUITourResponse", listener);
    54 			callback(event.detail.data);
    55 		}
    56 		document.addEventListener("mozUITourResponse", listener);
    58 		return id;
    59 	}
    61 	Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY = 10 * 1000;
    63 	Mozilla.UITour.registerPageID = function(pageID) {
    64 		_sendEvent('registerPageID', {
    65 			pageID: pageID
    66 		});
    67 	};
    69 	Mozilla.UITour.showHighlight = function(target, effect) {
    70 		_sendEvent('showHighlight', {
    71 			target: target,
    72 			effect: effect
    73 		});
    74 	};
    76 	Mozilla.UITour.hideHighlight = function() {
    77 		_sendEvent('hideHighlight');
    78 	};
    80 	Mozilla.UITour.showInfo = function(target, title, text, icon, buttons, options) {
    81 		var buttonData = [];
    82 		if (Array.isArray(buttons)) {
    83 			for (var i = 0; i < buttons.length; i++) {
    84 				buttonData.push({
    85 					label: buttons[i].label,
    86 					icon: buttons[i].icon,
    87 					style: buttons[i].style,
    88 					callbackID: _waitForCallback(buttons[i].callback)
    89 			});
    90 			}
    91 		}
    93 		var closeButtonCallbackID, targetCallbackID;
    94 		if (options && options.closeButtonCallback)
    95 			closeButtonCallbackID = _waitForCallback(options.closeButtonCallback);
    96 		if (options && options.targetCallback)
    97 			targetCallbackID = _waitForCallback(options.targetCallback);
    99 		_sendEvent('showInfo', {
   100 			target: target,
   101 			title: title,
   102 			text: text,
   103 			icon: icon,
   104 			buttons: buttonData,
   105 			closeButtonCallbackID: closeButtonCallbackID,
   106 			targetCallbackID: targetCallbackID
   107 		});
   108 	};
   110 	Mozilla.UITour.hideInfo = function() {
   111 		_sendEvent('hideInfo');
   112 	};
   114 	Mozilla.UITour.previewTheme = function(theme) {
   115 		_stopCyclingThemes();
   117 		_sendEvent('previewTheme', {
   118 			theme: JSON.stringify(theme)
   119 		});
   120 	};
   122 	Mozilla.UITour.resetTheme = function() {
   123 		_stopCyclingThemes();
   125 		_sendEvent('resetTheme');
   126 	};
   128 	Mozilla.UITour.cycleThemes = function(themes, delay, callback) {
   129 		_stopCyclingThemes();
   131 		if (!delay) {
   132 			delay = Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY;
   133 		}
   135 		function nextTheme() {
   136 			var theme = themes.shift();
   137 			themes.push(theme);
   139 			_sendEvent('previewTheme', {
   140 				theme: JSON.stringify(theme),
   141 				state: true
   142 			});
   144 			callback(theme);
   145 		}
   147 		themeIntervalId = setInterval(nextTheme, delay);
   148 		nextTheme();
   149 	};
   151 	Mozilla.UITour.addPinnedTab = function() {
   152 		_sendEvent('addPinnedTab');
   153 	};
   155 	Mozilla.UITour.removePinnedTab = function() {
   156 		_sendEvent('removePinnedTab');
   157 	};
   159 	Mozilla.UITour.showMenu = function(name) {
   160 		_sendEvent('showMenu', {
   161 			name: name
   162 		});
   163 	};
   165 	Mozilla.UITour.hideMenu = function(name) {
   166 		_sendEvent('hideMenu', {
   167 			name: name
   168 		});
   169 	};
   171 	Mozilla.UITour.getConfiguration = function(configName, callback) {
   172 		_sendEvent('getConfiguration', {
   173 			callbackID: _waitForCallback(callback),
   174 			configuration: configName,
   175 		});
   176 	};
   178 	Mozilla.UITour.showFirefoxAccounts = function() {
   179 		_sendEvent('showFirefoxAccounts');
   180 	};
   182 })();

mercurial