browser/modules/test/uitour.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/modules/test/uitour.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,182 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// Copied from the proposed JS library for Bedrock (ie, www.mozilla.org).
     1.9 +
    1.10 +// create namespace
    1.11 +if (typeof Mozilla == 'undefined') {
    1.12 +	var Mozilla = {};
    1.13 +}
    1.14 +
    1.15 +(function($) {
    1.16 +  'use strict';
    1.17 +
    1.18 +	// create namespace
    1.19 +	if (typeof Mozilla.UITour == 'undefined') {
    1.20 +		Mozilla.UITour = {};
    1.21 +	}
    1.22 +
    1.23 +	var themeIntervalId = null;
    1.24 +	function _stopCyclingThemes() {
    1.25 +		if (themeIntervalId) {
    1.26 +			clearInterval(themeIntervalId);
    1.27 +			themeIntervalId = null;
    1.28 +		}
    1.29 +	}
    1.30 +
    1.31 +	function _sendEvent(action, data) {
    1.32 +		var event = new CustomEvent('mozUITour', {
    1.33 +			bubbles: true,
    1.34 +			detail: {
    1.35 +				action: action,
    1.36 +				data: data || {}
    1.37 +			}
    1.38 +		});
    1.39 +
    1.40 +		document.dispatchEvent(event);
    1.41 +	}
    1.42 +
    1.43 +	function _generateCallbackID() {
    1.44 +		return Math.random().toString(36).replace(/[^a-z]+/g, '');
    1.45 +	}
    1.46 +
    1.47 +	function _waitForCallback(callback) {
    1.48 +		var id = _generateCallbackID();
    1.49 +
    1.50 +		function listener(event) {
    1.51 +			if (typeof event.detail != "object")
    1.52 +				return;
    1.53 +			if (event.detail.callbackID != id)
    1.54 +				return;
    1.55 +
    1.56 +			document.removeEventListener("mozUITourResponse", listener);
    1.57 +			callback(event.detail.data);
    1.58 +		}
    1.59 +		document.addEventListener("mozUITourResponse", listener);
    1.60 +
    1.61 +		return id;
    1.62 +	}
    1.63 +
    1.64 +	Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY = 10 * 1000;
    1.65 +
    1.66 +	Mozilla.UITour.registerPageID = function(pageID) {
    1.67 +		_sendEvent('registerPageID', {
    1.68 +			pageID: pageID
    1.69 +		});
    1.70 +	};
    1.71 +
    1.72 +	Mozilla.UITour.showHighlight = function(target, effect) {
    1.73 +		_sendEvent('showHighlight', {
    1.74 +			target: target,
    1.75 +			effect: effect
    1.76 +		});
    1.77 +	};
    1.78 +
    1.79 +	Mozilla.UITour.hideHighlight = function() {
    1.80 +		_sendEvent('hideHighlight');
    1.81 +	};
    1.82 +
    1.83 +	Mozilla.UITour.showInfo = function(target, title, text, icon, buttons, options) {
    1.84 +		var buttonData = [];
    1.85 +		if (Array.isArray(buttons)) {
    1.86 +			for (var i = 0; i < buttons.length; i++) {
    1.87 +				buttonData.push({
    1.88 +					label: buttons[i].label,
    1.89 +					icon: buttons[i].icon,
    1.90 +					style: buttons[i].style,
    1.91 +					callbackID: _waitForCallback(buttons[i].callback)
    1.92 +			});
    1.93 +			}
    1.94 +		}
    1.95 +
    1.96 +		var closeButtonCallbackID, targetCallbackID;
    1.97 +		if (options && options.closeButtonCallback)
    1.98 +			closeButtonCallbackID = _waitForCallback(options.closeButtonCallback);
    1.99 +		if (options && options.targetCallback)
   1.100 +			targetCallbackID = _waitForCallback(options.targetCallback);
   1.101 +
   1.102 +		_sendEvent('showInfo', {
   1.103 +			target: target,
   1.104 +			title: title,
   1.105 +			text: text,
   1.106 +			icon: icon,
   1.107 +			buttons: buttonData,
   1.108 +			closeButtonCallbackID: closeButtonCallbackID,
   1.109 +			targetCallbackID: targetCallbackID
   1.110 +		});
   1.111 +	};
   1.112 +
   1.113 +	Mozilla.UITour.hideInfo = function() {
   1.114 +		_sendEvent('hideInfo');
   1.115 +	};
   1.116 +
   1.117 +	Mozilla.UITour.previewTheme = function(theme) {
   1.118 +		_stopCyclingThemes();
   1.119 +
   1.120 +		_sendEvent('previewTheme', {
   1.121 +			theme: JSON.stringify(theme)
   1.122 +		});
   1.123 +	};
   1.124 +
   1.125 +	Mozilla.UITour.resetTheme = function() {
   1.126 +		_stopCyclingThemes();
   1.127 +
   1.128 +		_sendEvent('resetTheme');
   1.129 +	};
   1.130 +
   1.131 +	Mozilla.UITour.cycleThemes = function(themes, delay, callback) {
   1.132 +		_stopCyclingThemes();
   1.133 +
   1.134 +		if (!delay) {
   1.135 +			delay = Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY;
   1.136 +		}
   1.137 +
   1.138 +		function nextTheme() {
   1.139 +			var theme = themes.shift();
   1.140 +			themes.push(theme);
   1.141 +
   1.142 +			_sendEvent('previewTheme', {
   1.143 +				theme: JSON.stringify(theme),
   1.144 +				state: true
   1.145 +			});
   1.146 +
   1.147 +			callback(theme);
   1.148 +		}
   1.149 +
   1.150 +		themeIntervalId = setInterval(nextTheme, delay);
   1.151 +		nextTheme();
   1.152 +	};
   1.153 +
   1.154 +	Mozilla.UITour.addPinnedTab = function() {
   1.155 +		_sendEvent('addPinnedTab');
   1.156 +	};
   1.157 +
   1.158 +	Mozilla.UITour.removePinnedTab = function() {
   1.159 +		_sendEvent('removePinnedTab');
   1.160 +	};
   1.161 +
   1.162 +	Mozilla.UITour.showMenu = function(name) {
   1.163 +		_sendEvent('showMenu', {
   1.164 +			name: name
   1.165 +		});
   1.166 +	};
   1.167 +
   1.168 +	Mozilla.UITour.hideMenu = function(name) {
   1.169 +		_sendEvent('hideMenu', {
   1.170 +			name: name
   1.171 +		});
   1.172 +	};
   1.173 +
   1.174 +	Mozilla.UITour.getConfiguration = function(configName, callback) {
   1.175 +		_sendEvent('getConfiguration', {
   1.176 +			callbackID: _waitForCallback(callback),
   1.177 +			configuration: configName,
   1.178 +		});
   1.179 +	};
   1.180 +
   1.181 +	Mozilla.UITour.showFirefoxAccounts = function() {
   1.182 +		_sendEvent('showFirefoxAccounts');
   1.183 +	};
   1.184 +
   1.185 +})();

mercurial