Stackedit/res/extensions/notifications.js

110 lines
3.7 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
"underscore",
2013-05-27 23:27:38 +00:00
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
2013-06-10 21:22:32 +00:00
"jgrowl",
2013-06-22 23:48:57 +00:00
"text!html/notificationsSettingsBlock.html",
], function($, _, utils, Extension, jGrowl, notificationsSettingsBlockHTML) {
2013-05-29 19:55:23 +00:00
2013-06-22 23:48:57 +00:00
var notifications = new Extension("notifications", "Notifications");
notifications.settingsBlock = notificationsSettingsBlockHTML;
notifications.defaultConfig = {
timeout: 8000
2013-05-27 23:27:38 +00:00
};
2013-05-29 19:55:23 +00:00
notifications.onLoadSettings = function() {
utils.setInputValue("#input-notifications-timeout", notifications.config.timeout);
};
2013-05-27 23:27:38 +00:00
notifications.onSaveSettings = function(newConfig, event) {
2013-05-29 19:55:23 +00:00
newConfig.timeout = utils.getInputIntValue("#input-notifications-timeout", event, 1, 60000);
};
2013-06-19 20:33:46 +00:00
var isInit = false;
function init() {
if(isInit === false) {
// jGrowl configuration
jGrowl.defaults.life = notifications.config.timeout;
jGrowl.defaults.closer = false;
jGrowl.defaults.closeTemplate = '';
jGrowl.defaults.position = 'bottom-right';
isInit = true;
}
}
2013-05-29 19:55:23 +00:00
2013-05-29 23:04:52 +00:00
function showMessage(message, iconClass, options) {
2013-06-09 09:49:19 +00:00
logger.info(message);
2013-06-19 20:33:46 +00:00
init();
2013-05-29 23:04:52 +00:00
if(!message) {
2013-05-29 19:55:23 +00:00
return;
}
2013-05-29 23:04:52 +00:00
var endOfMsg = message.indexOf("|");
2013-05-29 19:55:23 +00:00
if(endOfMsg !== -1) {
2013-05-29 23:04:52 +00:00
message = message.substring(0, endOfMsg);
if(!message) {
2013-05-29 19:55:23 +00:00
return;
}
}
options = options || {};
2013-08-20 22:40:19 +00:00
iconClass = iconClass || "icon-info-circled";
2013-05-29 23:04:52 +00:00
jGrowl("<i class='icon-white " + iconClass + "'></i> " + _.escape(message), options);
2013-05-29 19:55:23 +00:00
}
notifications.onMessage = function(message) {
showMessage(message);
};
notifications.onError = function(error) {
logger.error(error);
if(_.isString(error)) {
2013-08-20 22:40:19 +00:00
showMessage(error, "icon-attention");
2013-05-29 19:55:23 +00:00
}
else if(_.isObject(error)) {
2013-08-20 22:40:19 +00:00
showMessage(error.message, "icon-attention");
2013-05-29 19:55:23 +00:00
}
};
notifications.onOfflineChanged = function(isOffline) {
if(isOffline === true) {
2013-08-18 00:02:23 +00:00
showMessage("You are offline.", "icon-attention-circled msg-offline", {
2013-05-29 19:55:23 +00:00
sticky: true,
close: function() {
showMessage("You are back online!", "icon-signal");
}
});
}
else {
$(".msg-offline").parents(".jGrowl-notification").trigger('jGrowl.beforeClose');
}
};
notifications.onSyncImportSuccess = function(fileDescList, provider) {
var titles = _.map(fileDescList, function(fileDesc) {
return fileDesc.title;
}).join(", ");
showMessage(titles + ' imported successfully from ' + provider.providerName + '.');
};
notifications.onSyncExportSuccess = function(fileDesc, syncAttributes) {
showMessage('"' + fileDesc.title + '" will now be synchronized on ' + syncAttributes.provider.providerName + '.');
};
notifications.onSyncRemoved = function(fileDesc, syncAttributes) {
showMessage(syncAttributes.provider.providerName + " synchronized location has been removed.");
};
notifications.onPublishSuccess = function(fileDesc) {
showMessage('"' + fileDesc.title + '" successfully published.');
};
2013-05-29 23:04:52 +00:00
notifications.onNewPublishSuccess = function(fileDesc, publishAttributes) {
2013-05-29 19:55:23 +00:00
showMessage('"' + fileDesc.title + '" is now published on ' + publishAttributes.provider.providerName + '.');
};
notifications.onPublishRemoved = function(fileDesc, publishAttributes) {
showMessage(publishAttributes.provider.providerName + " publish location has been removed.");
2013-05-27 23:27:38 +00:00
};
2013-05-29 19:55:23 +00:00
return notifications;
2013-05-25 00:34:04 +00:00
});