2015-06-22 22:11:37 +02:00
|
|
|
/**
|
|
|
|
* @provides javelin-behavior-desktop-notifications-control
|
|
|
|
* @requires javelin-behavior
|
|
|
|
* javelin-stratcom
|
|
|
|
* javelin-dom
|
|
|
|
* javelin-uri
|
|
|
|
* phabricator-notification
|
|
|
|
*/
|
|
|
|
|
|
|
|
JX.behavior('desktop-notifications-control', function(config, statics) {
|
|
|
|
|
|
|
|
function findEl(id) {
|
|
|
|
var el = null;
|
|
|
|
try {
|
|
|
|
el = JX.$(id);
|
|
|
|
} catch (e) {
|
|
|
|
// not found
|
|
|
|
}
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
function updateFormStatus(permission) {
|
2017-02-20 21:48:37 +01:00
|
|
|
var status_node = findEl(config.statusID);
|
|
|
|
if (!status_node) {
|
2015-06-22 22:11:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-02-20 21:48:37 +01:00
|
|
|
|
|
|
|
var message_node = JX.$(config.messageID);
|
|
|
|
|
2015-06-22 22:11:37 +02:00
|
|
|
switch (permission) {
|
|
|
|
case 'default':
|
2017-02-20 21:48:37 +01:00
|
|
|
JX.DOM.setContent(message_node, config.cancelAsk);
|
2015-06-22 22:11:37 +02:00
|
|
|
break;
|
|
|
|
case 'granted':
|
2017-02-20 21:48:37 +01:00
|
|
|
JX.DOM.setContent(message_node, config.grantedAsk);
|
2015-06-22 22:11:37 +02:00
|
|
|
break;
|
|
|
|
case 'denied':
|
2017-02-20 21:48:37 +01:00
|
|
|
JX.DOM.setContent(message_node, config.deniedAsk);
|
2015-06-22 22:11:37 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-02-20 21:48:37 +01:00
|
|
|
|
|
|
|
JX.DOM.show(status_node);
|
2015-06-22 22:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateBrowserStatus(permission) {
|
|
|
|
var browserStatusEl = findEl(config.browserStatusID);
|
|
|
|
if (!browserStatusEl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (permission) {
|
|
|
|
case 'default':
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-notice', true);
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-success', false);
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-error', false);
|
|
|
|
JX.DOM.setContent(browserStatusEl, JX.$H(config.defaultStatus));
|
|
|
|
break;
|
|
|
|
case 'granted':
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-success', true);
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-notice', false);
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-error', false);
|
|
|
|
JX.DOM.setContent(browserStatusEl, JX.$H(config.grantedStatus));
|
|
|
|
break;
|
|
|
|
case 'denied':
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-error', true);
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-notice', false);
|
|
|
|
JX.DOM.alterClass(browserStatusEl, 'phui-info-severity-success', false);
|
|
|
|
JX.DOM.setContent(browserStatusEl, JX.$H(config.deniedStatus));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
JX.DOM.show(browserStatusEl);
|
|
|
|
}
|
|
|
|
|
|
|
|
function installSelectListener() {
|
|
|
|
var controlEl = findEl(config.controlID);
|
|
|
|
if (!controlEl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var select = JX.DOM.find(controlEl, 'select');
|
|
|
|
JX.DOM.listen(
|
|
|
|
select,
|
|
|
|
'change',
|
|
|
|
null,
|
|
|
|
function (e) {
|
|
|
|
if (!JX.Notification.supportsDesktopNotifications()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var value = e.getTarget().value;
|
2017-08-23 23:35:02 +02:00
|
|
|
if ((value == config.desktop) || (value == config.desktopOnly)) {
|
|
|
|
window.Notification.requestPermission(
|
|
|
|
function (permission) {
|
|
|
|
updateFormStatus(permission);
|
|
|
|
updateBrowserStatus(permission);
|
|
|
|
});
|
2015-06-22 22:11:37 +02:00
|
|
|
} else {
|
|
|
|
var statusEl = JX.$(config.statusID);
|
|
|
|
JX.DOM.hide(statusEl);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function install() {
|
|
|
|
JX.Stratcom.listen(
|
|
|
|
'click',
|
|
|
|
'desktop-notifications-permission-button',
|
|
|
|
function () {
|
|
|
|
window.Notification.requestPermission(
|
|
|
|
function (permission) {
|
|
|
|
updateFormStatus(permission);
|
|
|
|
updateBrowserStatus(permission);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
statics.installed = statics.installed || install();
|
|
|
|
if (!JX.Notification.supportsDesktopNotifications()) {
|
|
|
|
var statusEl = JX.$(config.statusID);
|
|
|
|
JX.DOM.setContent(statusEl.firstChild, config.noSupport);
|
|
|
|
JX.DOM.show(statusEl);
|
|
|
|
} else {
|
|
|
|
updateBrowserStatus(window.Notification.permission);
|
|
|
|
}
|
|
|
|
installSelectListener();
|
|
|
|
});
|