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