browser/modules/test/uitour.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:9bd3e24f1b18
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/. */
4
5 // Copied from the proposed JS library for Bedrock (ie, www.mozilla.org).
6
7 // create namespace
8 if (typeof Mozilla == 'undefined') {
9 var Mozilla = {};
10 }
11
12 (function($) {
13 'use strict';
14
15 // create namespace
16 if (typeof Mozilla.UITour == 'undefined') {
17 Mozilla.UITour = {};
18 }
19
20 var themeIntervalId = null;
21 function _stopCyclingThemes() {
22 if (themeIntervalId) {
23 clearInterval(themeIntervalId);
24 themeIntervalId = null;
25 }
26 }
27
28 function _sendEvent(action, data) {
29 var event = new CustomEvent('mozUITour', {
30 bubbles: true,
31 detail: {
32 action: action,
33 data: data || {}
34 }
35 });
36
37 document.dispatchEvent(event);
38 }
39
40 function _generateCallbackID() {
41 return Math.random().toString(36).replace(/[^a-z]+/g, '');
42 }
43
44 function _waitForCallback(callback) {
45 var id = _generateCallbackID();
46
47 function listener(event) {
48 if (typeof event.detail != "object")
49 return;
50 if (event.detail.callbackID != id)
51 return;
52
53 document.removeEventListener("mozUITourResponse", listener);
54 callback(event.detail.data);
55 }
56 document.addEventListener("mozUITourResponse", listener);
57
58 return id;
59 }
60
61 Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY = 10 * 1000;
62
63 Mozilla.UITour.registerPageID = function(pageID) {
64 _sendEvent('registerPageID', {
65 pageID: pageID
66 });
67 };
68
69 Mozilla.UITour.showHighlight = function(target, effect) {
70 _sendEvent('showHighlight', {
71 target: target,
72 effect: effect
73 });
74 };
75
76 Mozilla.UITour.hideHighlight = function() {
77 _sendEvent('hideHighlight');
78 };
79
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 }
92
93 var closeButtonCallbackID, targetCallbackID;
94 if (options && options.closeButtonCallback)
95 closeButtonCallbackID = _waitForCallback(options.closeButtonCallback);
96 if (options && options.targetCallback)
97 targetCallbackID = _waitForCallback(options.targetCallback);
98
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 };
109
110 Mozilla.UITour.hideInfo = function() {
111 _sendEvent('hideInfo');
112 };
113
114 Mozilla.UITour.previewTheme = function(theme) {
115 _stopCyclingThemes();
116
117 _sendEvent('previewTheme', {
118 theme: JSON.stringify(theme)
119 });
120 };
121
122 Mozilla.UITour.resetTheme = function() {
123 _stopCyclingThemes();
124
125 _sendEvent('resetTheme');
126 };
127
128 Mozilla.UITour.cycleThemes = function(themes, delay, callback) {
129 _stopCyclingThemes();
130
131 if (!delay) {
132 delay = Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY;
133 }
134
135 function nextTheme() {
136 var theme = themes.shift();
137 themes.push(theme);
138
139 _sendEvent('previewTheme', {
140 theme: JSON.stringify(theme),
141 state: true
142 });
143
144 callback(theme);
145 }
146
147 themeIntervalId = setInterval(nextTheme, delay);
148 nextTheme();
149 };
150
151 Mozilla.UITour.addPinnedTab = function() {
152 _sendEvent('addPinnedTab');
153 };
154
155 Mozilla.UITour.removePinnedTab = function() {
156 _sendEvent('removePinnedTab');
157 };
158
159 Mozilla.UITour.showMenu = function(name) {
160 _sendEvent('showMenu', {
161 name: name
162 });
163 };
164
165 Mozilla.UITour.hideMenu = function(name) {
166 _sendEvent('hideMenu', {
167 name: name
168 });
169 };
170
171 Mozilla.UITour.getConfiguration = function(configName, callback) {
172 _sendEvent('getConfiguration', {
173 callbackID: _waitForCallback(callback),
174 configuration: configName,
175 });
176 };
177
178 Mozilla.UITour.showFirefoxAccounts = function() {
179 _sendEvent('showFirefoxAccounts');
180 };
181
182 })();

mercurial