michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Copied from the proposed JS library for Bedrock (ie, www.mozilla.org). michael@0: michael@0: // create namespace michael@0: if (typeof Mozilla == 'undefined') { michael@0: var Mozilla = {}; michael@0: } michael@0: michael@0: (function($) { michael@0: 'use strict'; michael@0: michael@0: // create namespace michael@0: if (typeof Mozilla.UITour == 'undefined') { michael@0: Mozilla.UITour = {}; michael@0: } michael@0: michael@0: var themeIntervalId = null; michael@0: function _stopCyclingThemes() { michael@0: if (themeIntervalId) { michael@0: clearInterval(themeIntervalId); michael@0: themeIntervalId = null; michael@0: } michael@0: } michael@0: michael@0: function _sendEvent(action, data) { michael@0: var event = new CustomEvent('mozUITour', { michael@0: bubbles: true, michael@0: detail: { michael@0: action: action, michael@0: data: data || {} michael@0: } michael@0: }); michael@0: michael@0: document.dispatchEvent(event); michael@0: } michael@0: michael@0: function _generateCallbackID() { michael@0: return Math.random().toString(36).replace(/[^a-z]+/g, ''); michael@0: } michael@0: michael@0: function _waitForCallback(callback) { michael@0: var id = _generateCallbackID(); michael@0: michael@0: function listener(event) { michael@0: if (typeof event.detail != "object") michael@0: return; michael@0: if (event.detail.callbackID != id) michael@0: return; michael@0: michael@0: document.removeEventListener("mozUITourResponse", listener); michael@0: callback(event.detail.data); michael@0: } michael@0: document.addEventListener("mozUITourResponse", listener); michael@0: michael@0: return id; michael@0: } michael@0: michael@0: Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY = 10 * 1000; michael@0: michael@0: Mozilla.UITour.registerPageID = function(pageID) { michael@0: _sendEvent('registerPageID', { michael@0: pageID: pageID michael@0: }); michael@0: }; michael@0: michael@0: Mozilla.UITour.showHighlight = function(target, effect) { michael@0: _sendEvent('showHighlight', { michael@0: target: target, michael@0: effect: effect michael@0: }); michael@0: }; michael@0: michael@0: Mozilla.UITour.hideHighlight = function() { michael@0: _sendEvent('hideHighlight'); michael@0: }; michael@0: michael@0: Mozilla.UITour.showInfo = function(target, title, text, icon, buttons, options) { michael@0: var buttonData = []; michael@0: if (Array.isArray(buttons)) { michael@0: for (var i = 0; i < buttons.length; i++) { michael@0: buttonData.push({ michael@0: label: buttons[i].label, michael@0: icon: buttons[i].icon, michael@0: style: buttons[i].style, michael@0: callbackID: _waitForCallback(buttons[i].callback) michael@0: }); michael@0: } michael@0: } michael@0: michael@0: var closeButtonCallbackID, targetCallbackID; michael@0: if (options && options.closeButtonCallback) michael@0: closeButtonCallbackID = _waitForCallback(options.closeButtonCallback); michael@0: if (options && options.targetCallback) michael@0: targetCallbackID = _waitForCallback(options.targetCallback); michael@0: michael@0: _sendEvent('showInfo', { michael@0: target: target, michael@0: title: title, michael@0: text: text, michael@0: icon: icon, michael@0: buttons: buttonData, michael@0: closeButtonCallbackID: closeButtonCallbackID, michael@0: targetCallbackID: targetCallbackID michael@0: }); michael@0: }; michael@0: michael@0: Mozilla.UITour.hideInfo = function() { michael@0: _sendEvent('hideInfo'); michael@0: }; michael@0: michael@0: Mozilla.UITour.previewTheme = function(theme) { michael@0: _stopCyclingThemes(); michael@0: michael@0: _sendEvent('previewTheme', { michael@0: theme: JSON.stringify(theme) michael@0: }); michael@0: }; michael@0: michael@0: Mozilla.UITour.resetTheme = function() { michael@0: _stopCyclingThemes(); michael@0: michael@0: _sendEvent('resetTheme'); michael@0: }; michael@0: michael@0: Mozilla.UITour.cycleThemes = function(themes, delay, callback) { michael@0: _stopCyclingThemes(); michael@0: michael@0: if (!delay) { michael@0: delay = Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY; michael@0: } michael@0: michael@0: function nextTheme() { michael@0: var theme = themes.shift(); michael@0: themes.push(theme); michael@0: michael@0: _sendEvent('previewTheme', { michael@0: theme: JSON.stringify(theme), michael@0: state: true michael@0: }); michael@0: michael@0: callback(theme); michael@0: } michael@0: michael@0: themeIntervalId = setInterval(nextTheme, delay); michael@0: nextTheme(); michael@0: }; michael@0: michael@0: Mozilla.UITour.addPinnedTab = function() { michael@0: _sendEvent('addPinnedTab'); michael@0: }; michael@0: michael@0: Mozilla.UITour.removePinnedTab = function() { michael@0: _sendEvent('removePinnedTab'); michael@0: }; michael@0: michael@0: Mozilla.UITour.showMenu = function(name) { michael@0: _sendEvent('showMenu', { michael@0: name: name michael@0: }); michael@0: }; michael@0: michael@0: Mozilla.UITour.hideMenu = function(name) { michael@0: _sendEvent('hideMenu', { michael@0: name: name michael@0: }); michael@0: }; michael@0: michael@0: Mozilla.UITour.getConfiguration = function(configName, callback) { michael@0: _sendEvent('getConfiguration', { michael@0: callbackID: _waitForCallback(callback), michael@0: configuration: configName, michael@0: }); michael@0: }; michael@0: michael@0: Mozilla.UITour.showFirefoxAccounts = function() { michael@0: _sendEvent('showFirefoxAccounts'); michael@0: }; michael@0: michael@0: })();