Stackedit/js/extensions/notifications.js

117 lines
4.0 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-05-27 19:45:33 +00:00
"jgrowl"
2013-05-27 23:27:38 +00:00
], function($, _, utils, jGrowl) {
2013-05-29 19:55:23 +00:00
var notifications = {
extensionId: "notifications",
extensionName: "Notifications",
defaultConfig: {
2013-05-29 23:04:52 +00:00
timeout: 8000
2013-05-29 19:55:23 +00:00
},
2013-05-27 23:27:38 +00:00
settingsBloc: [
2013-05-29 19:55:23 +00:00
'<p>Shows notification messages in the bottom-right corner of the screen.</p>',
'<div class="form-horizontal">',
' <div class="control-group">',
' <label class="control-label" for="input-notifications-timeout">Timeout</label>',
' <div class="controls">',
' <input type="text" id="input-notifications-timeout" class="input-mini">',
' <span class="help-inline">ms</span>',
' </div>',
' </div>',
'</div>'
].join("")
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);
};
notifications.onReady = function() {
// jGrowl configuration
jGrowl.defaults.life = notifications.config.timeout;
jGrowl.defaults.closer = false;
jGrowl.defaults.closeTemplate = '';
jGrowl.defaults.position = 'bottom-right';
};
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-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 || {};
iconClass = iconClass || "icon-info-sign";
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)) {
showMessage(error, "icon-warning-sign");
}
else if(_.isObject(error)) {
showMessage(error.message, "icon-warning-sign");
}
};
notifications.onOfflineChanged = function(isOffline) {
if(isOffline === true) {
showMessage("You are offline.", "icon-exclamation-sign msg-offline", {
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
});