2014-06-18 17:56:02 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// create namespace
|
|
|
|
if (typeof Mozilla == 'undefined') {
|
|
|
|
var Mozilla = {};
|
|
|
|
}
|
|
|
|
|
2014-10-14 17:48:49 -07:00
|
|
|
;(function($) {
|
|
|
|
'use strict';
|
2014-06-18 17:56:02 -07:00
|
|
|
|
|
|
|
// create namespace
|
|
|
|
if (typeof Mozilla.UITour == 'undefined') {
|
|
|
|
Mozilla.UITour = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var themeIntervalId = null;
|
|
|
|
function _stopCyclingThemes() {
|
|
|
|
if (themeIntervalId) {
|
|
|
|
clearInterval(themeIntervalId);
|
|
|
|
themeIntervalId = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _sendEvent(action, data) {
|
|
|
|
var event = new CustomEvent('mozUITour', {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
action: action,
|
|
|
|
data: data || {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
document.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _generateCallbackID() {
|
|
|
|
return Math.random().toString(36).replace(/[^a-z]+/g, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
function _waitForCallback(callback) {
|
|
|
|
var id = _generateCallbackID();
|
|
|
|
|
|
|
|
function listener(event) {
|
2014-12-04 17:50:45 -08:00
|
|
|
if (typeof event.detail != 'object')
|
2014-06-18 17:56:02 -07:00
|
|
|
return;
|
|
|
|
if (event.detail.callbackID != id)
|
|
|
|
return;
|
|
|
|
|
2014-12-04 17:50:45 -08:00
|
|
|
document.removeEventListener('mozUITourResponse', listener);
|
2014-06-18 17:56:02 -07:00
|
|
|
callback(event.detail.data);
|
|
|
|
}
|
2014-12-04 17:50:45 -08:00
|
|
|
document.addEventListener('mozUITourResponse', listener);
|
2014-06-18 17:56:02 -07:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2014-12-04 17:50:45 -08:00
|
|
|
var notificationListener = null;
|
|
|
|
function _notificationListener(event) {
|
|
|
|
if (typeof event.detail != 'object')
|
|
|
|
return;
|
|
|
|
if (typeof notificationListener != 'function')
|
|
|
|
return;
|
|
|
|
|
|
|
|
notificationListener(event.detail.event, event.detail.params);
|
|
|
|
}
|
|
|
|
|
2014-06-18 17:56:02 -07:00
|
|
|
Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY = 10 * 1000;
|
|
|
|
|
2014-12-04 17:50:45 -08:00
|
|
|
Mozilla.UITour.CONFIGNAME_SYNC = 'sync';
|
|
|
|
Mozilla.UITour.CONFIGNAME_AVAILABLETARGETS = 'availableTargets';
|
|
|
|
|
|
|
|
Mozilla.UITour.ping = function(callback) {
|
|
|
|
var data = {};
|
|
|
|
if (callback) {
|
|
|
|
data.callbackID = _waitForCallback(callback);
|
|
|
|
}
|
|
|
|
_sendEvent('ping', data);
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.observe = function(listener, callback) {
|
|
|
|
notificationListener = listener;
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
document.addEventListener('mozUITourNotification',
|
|
|
|
_notificationListener);
|
|
|
|
Mozilla.UITour.ping(callback);
|
|
|
|
} else {
|
|
|
|
document.removeEventListener('mozUITourNotification',
|
|
|
|
_notificationListener);
|
|
|
|
}
|
|
|
|
};
|
2014-10-14 17:48:49 -07:00
|
|
|
|
2014-06-18 17:56:02 -07:00
|
|
|
Mozilla.UITour.registerPageID = function(pageID) {
|
|
|
|
_sendEvent('registerPageID', {
|
|
|
|
pageID: pageID
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.showHighlight = function(target, effect) {
|
|
|
|
_sendEvent('showHighlight', {
|
|
|
|
target: target,
|
|
|
|
effect: effect
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.hideHighlight = function() {
|
|
|
|
_sendEvent('hideHighlight');
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.showInfo = function(target, title, text, icon, buttons, options) {
|
|
|
|
var buttonData = [];
|
|
|
|
if (Array.isArray(buttons)) {
|
|
|
|
for (var i = 0; i < buttons.length; i++) {
|
|
|
|
buttonData.push({
|
|
|
|
label: buttons[i].label,
|
|
|
|
icon: buttons[i].icon,
|
|
|
|
style: buttons[i].style,
|
|
|
|
callbackID: _waitForCallback(buttons[i].callback)
|
2014-10-14 17:48:49 -07:00
|
|
|
});
|
2014-06-18 17:56:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeButtonCallbackID, targetCallbackID;
|
|
|
|
if (options && options.closeButtonCallback)
|
|
|
|
closeButtonCallbackID = _waitForCallback(options.closeButtonCallback);
|
|
|
|
if (options && options.targetCallback)
|
|
|
|
targetCallbackID = _waitForCallback(options.targetCallback);
|
|
|
|
|
|
|
|
_sendEvent('showInfo', {
|
|
|
|
target: target,
|
|
|
|
title: title,
|
|
|
|
text: text,
|
|
|
|
icon: icon,
|
|
|
|
buttons: buttonData,
|
|
|
|
closeButtonCallbackID: closeButtonCallbackID,
|
|
|
|
targetCallbackID: targetCallbackID
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.hideInfo = function() {
|
|
|
|
_sendEvent('hideInfo');
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.previewTheme = function(theme) {
|
|
|
|
_stopCyclingThemes();
|
|
|
|
|
|
|
|
_sendEvent('previewTheme', {
|
|
|
|
theme: JSON.stringify(theme)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.resetTheme = function() {
|
|
|
|
_stopCyclingThemes();
|
|
|
|
|
|
|
|
_sendEvent('resetTheme');
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.cycleThemes = function(themes, delay, callback) {
|
|
|
|
_stopCyclingThemes();
|
|
|
|
|
|
|
|
if (!delay) {
|
|
|
|
delay = Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextTheme() {
|
|
|
|
var theme = themes.shift();
|
|
|
|
themes.push(theme);
|
|
|
|
|
|
|
|
_sendEvent('previewTheme', {
|
|
|
|
theme: JSON.stringify(theme),
|
|
|
|
state: true
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
themeIntervalId = setInterval(nextTheme, delay);
|
|
|
|
nextTheme();
|
|
|
|
};
|
|
|
|
|
2014-10-14 17:48:49 -07:00
|
|
|
Mozilla.UITour.showMenu = function(name, callback) {
|
|
|
|
var showCallbackID;
|
|
|
|
if (callback)
|
|
|
|
showCallbackID = _waitForCallback(callback);
|
|
|
|
|
2014-06-18 17:56:02 -07:00
|
|
|
_sendEvent('showMenu', {
|
2014-10-14 17:48:49 -07:00
|
|
|
name: name,
|
|
|
|
showCallbackID: showCallbackID,
|
2014-06-18 17:56:02 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.hideMenu = function(name) {
|
|
|
|
_sendEvent('hideMenu', {
|
|
|
|
name: name
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-10-14 17:48:49 -07:00
|
|
|
Mozilla.UITour.startUrlbarCapture = function(text, url) {
|
|
|
|
_sendEvent('startUrlbarCapture', {
|
|
|
|
text: text,
|
|
|
|
url: url
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.endUrlbarCapture = function() {
|
|
|
|
_sendEvent('endUrlbarCapture');
|
|
|
|
};
|
|
|
|
|
2014-06-18 17:56:02 -07:00
|
|
|
Mozilla.UITour.getConfiguration = function(configName, callback) {
|
|
|
|
_sendEvent('getConfiguration', {
|
|
|
|
callbackID: _waitForCallback(callback),
|
|
|
|
configuration: configName,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-12-17 19:37:19 -08:00
|
|
|
Mozilla.UITour.setConfiguration = function(configName, configValue) {
|
|
|
|
_sendEvent('setConfiguration', {
|
|
|
|
configuration: configName,
|
|
|
|
value: configValue,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-06-18 17:56:02 -07:00
|
|
|
Mozilla.UITour.showFirefoxAccounts = function() {
|
|
|
|
_sendEvent('showFirefoxAccounts');
|
|
|
|
};
|
|
|
|
|
2014-09-17 05:58:00 -07:00
|
|
|
Mozilla.UITour.resetFirefox = function() {
|
|
|
|
_sendEvent('resetFirefox');
|
|
|
|
};
|
|
|
|
|
2014-10-06 15:49:30 -07:00
|
|
|
Mozilla.UITour.addNavBarWidget= function(name, callback) {
|
|
|
|
_sendEvent('addNavBarWidget', {
|
|
|
|
name: name,
|
|
|
|
callbackID: _waitForCallback(callback),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-12 12:53:15 -08:00
|
|
|
Mozilla.UITour.setDefaultSearchEngine = function(identifier) {
|
|
|
|
_sendEvent('setDefaultSearchEngine', {
|
|
|
|
identifier: identifier,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-19 14:52:23 -08:00
|
|
|
Mozilla.UITour.setTreatmentTag = function(name, value) {
|
|
|
|
_sendEvent('setTreatmentTag', {
|
|
|
|
name: name,
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.getTreatmentTag = function(name, callback) {
|
|
|
|
_sendEvent('getTreatmentTag', {
|
|
|
|
name: name,
|
|
|
|
callbackID: _waitForCallback(callback)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-21 13:19:31 -08:00
|
|
|
Mozilla.UITour.setSearchTerm = function(term) {
|
|
|
|
_sendEvent('setSearchTerm', {
|
|
|
|
term: term
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mozilla.UITour.openSearchPanel = function(callback) {
|
|
|
|
_sendEvent('openSearchPanel', {
|
|
|
|
callbackID: _waitForCallback(callback)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-06-18 17:56:02 -07:00
|
|
|
})();
|