vendor/assets/javascripts/messenger.js in messengerjs-rails-1.3.0 vs vendor/assets/javascripts/messenger.js in messengerjs-rails-1.3.3
- old
+ new
@@ -1,6 +1,6 @@
-/*! messenger 1.3.0 2013-03-21 */
+/*! messenger 1.3.3 */
/*
* This file begins the output concatenated into messenger.js
*
* It establishes the Messenger object while preserving whatever it was before
* (for noConflict), and making it a callable function.
@@ -297,21 +297,21 @@
};
return eventsShim();
})();
(function() {
- var $, ActionMessenger, BaseView, Events, RetryingMessage, _, _Message, _Messenger, _ref, _ref1,
+ var $, ActionMessenger, BaseView, Events, RetryingMessage, _, _Message, _Messenger, _ref, _ref1, _ref2,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
$ = jQuery;
- _ = _ != null ? _ : window.Messenger._;
+ _ = (_ref = window._) != null ? _ref : window.Messenger._;
- Events = (_ref = typeof Backbone !== "undefined" && Backbone !== null ? Backbone.Events : void 0) != null ? _ref : window.Messenger.Events;
+ Events = (_ref1 = typeof Backbone !== "undefined" && Backbone !== null ? Backbone.Events : void 0) != null ? _ref1 : window.Messenger.Events;
BaseView = (function() {
function BaseView(options) {
$.extend(this, Events);
@@ -424,21 +424,21 @@
_Message.prototype.cancel = function() {
return this.hide();
};
_Message.prototype.update = function(opts) {
- var _ref1,
+ var _ref2,
_this = this;
if (_.isString(opts)) {
opts = {
message: opts
};
}
$.extend(this.options, opts);
this.lastUpdate = new Date();
this.rendered = false;
- this.events = (_ref1 = this.options.events) != null ? _ref1 : {};
+ this.events = (_ref2 = this.options.events) != null ? _ref2 : {};
this.render();
this.actionsToEvents();
this.delegateEvents();
this.checkClickable();
if (this.options.hideAfter) {
@@ -485,67 +485,67 @@
return null;
}
};
_Message.prototype.actionsToEvents = function() {
- var act, name, _ref1, _results,
+ var act, name, _ref2, _results,
_this = this;
- _ref1 = this.options.actions;
+ _ref2 = this.options.actions;
_results = [];
- for (name in _ref1) {
- act = _ref1[name];
+ for (name in _ref2) {
+ act = _ref2[name];
_results.push(this.events["click [data-action=\"" + name + "\"] a"] = (function(act) {
return function(e) {
e.preventDefault();
e.stopPropagation();
_this.trigger("action:" + name, act, e);
- return act.action(e);
+ return act.action.call(_this, e, _this);
};
})(act));
}
return _results;
};
_Message.prototype.checkClickable = function() {
- var evt, name, _ref1, _results;
- _ref1 = this.events;
+ var evt, name, _ref2, _results;
+ _ref2 = this.events;
_results = [];
- for (name in _ref1) {
- evt = _ref1[name];
+ for (name in _ref2) {
+ evt = _ref2[name];
if (name === 'click') {
_results.push(this.$message.addClass('messenger-clickable'));
} else {
_results.push(void 0);
}
}
return _results;
};
_Message.prototype.undelegateEvents = function() {
- var _ref1;
+ var _ref2;
_Message.__super__.undelegateEvents.apply(this, arguments);
- return (_ref1 = this.$message) != null ? _ref1.removeClass('messenger-clickable') : void 0;
+ return (_ref2 = this.$message) != null ? _ref2.removeClass('messenger-clickable') : void 0;
};
_Message.prototype.parseActions = function() {
- var act, actions, n_act, name, _ref1, _ref2;
+ var act, actions, n_act, name, _ref2, _ref3;
actions = [];
- _ref1 = this.options.actions;
- for (name in _ref1) {
- act = _ref1[name];
+ _ref2 = this.options.actions;
+ for (name in _ref2) {
+ act = _ref2[name];
n_act = $.extend({}, act);
n_act.name = name;
- if ((_ref2 = n_act.label) == null) {
+ if ((_ref3 = n_act.label) == null) {
n_act.label = name;
}
actions.push(n_act);
}
return actions;
};
_Message.prototype.template = function(opts) {
- var $action, $actions, $cancel, $link, $message, $text, action, _i, _len, _ref1,
+ var $action, $actions, $cancel, $link, $message, $text, action, _i, _len, _ref2,
_this = this;
$message = $("<div class='messenger-message message alert " + opts.type + " message-" + opts.type + " alert-" + opts.type + "'>");
if (opts.showCloseButton) {
$cancel = $('<button type="button" class="close" data-dismiss="alert">×</button>');
$cancel.click(function() {
@@ -557,13 +557,13 @@
$text = $("<div class=\"messenger-message-inner\">" + opts.message + "</div>");
$message.append($text);
if (opts.actions.length) {
$actions = $('<div class="messenger-actions">');
}
- _ref1 = opts.actions;
- for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
- action = _ref1[_i];
+ _ref2 = opts.actions;
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ action = _ref2[_i];
$action = $('<span>');
$action.attr('data-action', "" + action.name);
$link = $('<a>');
$link.html(action.label);
$action.append($('<span class="messenger-phrase">'));
@@ -617,28 +617,28 @@
return this._actionInstance.abort();
}
};
RetryingMessage.prototype.clearTimers = function() {
- var name, timer, _ref1, _ref2;
- _ref1 = this._timers;
- for (name in _ref1) {
- timer = _ref1[name];
+ var name, timer, _ref2, _ref3;
+ _ref2 = this._timers;
+ for (name in _ref2) {
+ timer = _ref2[name];
clearTimeout(timer);
}
this._timers = {};
- return (_ref2 = this.$message) != null ? _ref2.removeClass('messenger-retry-soon messenger-retry-later') : void 0;
+ return (_ref3 = this.$message) != null ? _ref3.removeClass('messenger-retry-soon messenger-retry-later') : void 0;
};
RetryingMessage.prototype.render = function() {
- var action, name, _ref1, _results;
+ var action, name, _ref2, _results;
RetryingMessage.__super__.render.apply(this, arguments);
this.clearTimers();
- _ref1 = this.options.actions;
+ _ref2 = this.options.actions;
_results = [];
- for (name in _ref1) {
- action = _ref1[name];
+ for (name in _ref2) {
+ action = _ref2[name];
if (action.auto) {
_results.push(this.startCountdown(name, action));
} else {
_results.push(void 0);
}
@@ -674,17 +674,17 @@
time /= 60;
return pluralize(time, 'hour');
};
RetryingMessage.prototype.startCountdown = function(name, action) {
- var $phrase, remaining, tick, _ref1,
+ var $phrase, remaining, tick, _ref2,
_this = this;
if (this._timers[name] != null) {
return;
}
$phrase = this.$message.find("[data-action='" + name + "'] .messenger-phrase");
- remaining = (_ref1 = action.delay) != null ? _ref1 : 3;
+ remaining = (_ref2 = action.delay) != null ? _ref2 : 3;
if (remaining <= 10) {
this.$message.removeClass('messenger-retry-later');
this.$message.addClass('messenger-retry-soon');
} else {
this.$message.removeClass('messenger-retry-soon');
@@ -763,17 +763,17 @@
}
return $slot;
};
_Messenger.prototype._enforceIdConstraint = function(msg) {
- var entry, _i, _len, _msg, _ref1;
+ var entry, _i, _len, _msg, _ref2;
if (msg.options.id == null) {
return;
}
- _ref1 = this.history;
- for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
- entry = _ref1[_i];
+ _ref2 = this.history;
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ entry = _ref2[_i];
_msg = entry.msg;
if ((_msg.options.id != null) && _msg.options.id === msg.options.id && msg !== _msg) {
if (msg.options.singleton) {
msg.hide();
return;
@@ -783,17 +783,17 @@
}
}
};
_Messenger.prototype.newMessage = function(opts) {
- var msg, _ref1, _ref2, _ref3,
+ var msg, _ref2, _ref3, _ref4,
_this = this;
if (opts == null) {
opts = {};
}
opts.messenger = this;
- _Message = (_ref1 = (_ref2 = Messenger.themes[(_ref3 = opts.theme) != null ? _ref3 : this.options.theme]) != null ? _ref2.Message : void 0) != null ? _ref1 : RetryingMessage;
+ _Message = (_ref2 = (_ref3 = Messenger.themes[(_ref4 = opts.theme) != null ? _ref4 : this.options.theme]) != null ? _ref3.Message : void 0) != null ? _ref2 : RetryingMessage;
msg = new _Message(opts);
msg.on('show', function() {
if (opts.scrollTo && _this.$el.css('position') !== 'fixed') {
return msg.scrollTo();
}
@@ -801,40 +801,40 @@
msg.on('hide show render', this.updateMessageSlotClasses, this);
return msg;
};
_Messenger.prototype.updateMessageSlotClasses = function() {
- var anyShown, last, rec, willBeFirst, _i, _len, _ref1;
+ var anyShown, last, rec, willBeFirst, _i, _len, _ref2;
willBeFirst = true;
last = null;
anyShown = false;
- _ref1 = this.history;
- for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
- rec = _ref1[_i];
- rec.$slot.removeClass('first last shown');
+ _ref2 = this.history;
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ rec = _ref2[_i];
+ rec.$slot.removeClass('messenger-first messenger-last messenger-shown');
if (rec.msg.shown && rec.msg.rendered) {
- rec.$slot.addClass('shown');
+ rec.$slot.addClass('messenger-shown');
anyShown = true;
last = rec;
if (willBeFirst) {
willBeFirst = false;
- rec.$slot.addClass('first');
+ rec.$slot.addClass('messenger-first');
}
}
}
if (last != null) {
- last.$slot.addClass('last');
+ last.$slot.addClass('messenger-last');
}
return this.$el["" + (anyShown ? 'remove' : 'add') + "Class"]('messenger-empty');
};
_Messenger.prototype.hideAll = function() {
- var rec, _i, _len, _ref1, _results;
- _ref1 = this.history;
+ var rec, _i, _len, _ref2, _results;
+ _ref2 = this.history;
_results = [];
- for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
- rec = _ref1[_i];
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ rec = _ref2[_i];
_results.push(rec.msg.hide());
}
return _results;
};
@@ -914,32 +914,32 @@
return $.ajax = _old_ajax;
});
}
};
- ActionMessenger.prototype._getMessage = function(returnVal, def) {
+ ActionMessenger.prototype._getHandlerResponse = function(returnVal) {
if (returnVal === false) {
return false;
}
- if (returnVal === true || !(returnVal != null) || typeof returnVal !== 'string') {
- return def;
+ if (returnVal === true || !(returnVal != null)) {
+ return true;
}
return returnVal;
};
ActionMessenger.prototype._parseEvents = function(events) {
- var desc, firstSpace, func, label, out, type, _ref1;
+ var desc, firstSpace, func, label, out, type, _ref2;
if (events == null) {
events = {};
}
out = {};
for (label in events) {
func = events[label];
firstSpace = label.indexOf(' ');
type = label.substring(0, firstSpace);
desc = label.substring(firstSpace + 1);
- if ((_ref1 = out[type]) == null) {
+ if ((_ref2 = out[type]) == null) {
out[type] = {};
}
out[type][desc] = func;
}
return out;
@@ -963,62 +963,76 @@
}
return [type, data, xhr];
};
ActionMessenger.prototype.run = function() {
- var args, attr, events, m_opts, msg, opts, promiseAttrs, _i, _len, _ref1, _ref2,
+ var args, attr, events, getMessageText, handler, handlers, m_opts, msg, old, opts, promiseAttrs, type, _i, _len, _ref2, _ref3,
_this = this;
m_opts = arguments[0], opts = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
if (opts == null) {
opts = {};
}
m_opts = $.extend(true, {}, this.messageDefaults, this.doDefaults, m_opts != null ? m_opts : {});
events = this._parseEvents(m_opts.events);
- msg = (_ref1 = m_opts.messageInstance) != null ? _ref1 : this.newMessage(m_opts);
+ getMessageText = function(type, xhr) {
+ var message;
+ message = m_opts[type + 'Message'];
+ if (_.isFunction(message)) {
+ return message.call(_this, type, xhr);
+ }
+ return message;
+ };
+ msg = (_ref2 = m_opts.messageInstance) != null ? _ref2 : this.newMessage(m_opts);
if (m_opts.id != null) {
msg.options.id = m_opts.id;
}
if (m_opts.progressMessage != null) {
msg.update($.extend({}, m_opts, {
- message: m_opts.progressMessage,
+ message: getMessageText('progress', null),
type: 'info'
}));
}
+ handlers = {};
_.each(['error', 'success'], function(type) {
- var old, _ref2, _ref3;
- if ((_ref2 = opts[type]) != null ? _ref2._originalHandler : void 0) {
- opts[type] = opts[type]._originalHandler;
- }
- old = (_ref3 = opts[type]) != null ? _ref3 : function() {};
- opts[type] = function() {
- var data, msgOpts, msgText, r, reason, resp, xhr, _ref10, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
+ return handlers[type] = function() {
+ var data, defaultOpts, handlerResp, msgOpts, reason, resp, responseOpts, xhr, _base, _ref10, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
resp = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ if ((_ref3 = opts[type]) != null ? _ref3._originalHandler : void 0) {
+ opts[type] = opts[type]._originalHandler;
+ }
_ref4 = _this._normalizeResponse.apply(_this, resp), reason = _ref4[0], data = _ref4[1], xhr = _ref4[2];
if (type === 'success' && !(msg.errorCount != null) && m_opts.showSuccessWithoutError === false) {
m_opts['successMessage'] = null;
}
if (type === 'error') {
if ((_ref5 = m_opts.errorCount) == null) {
m_opts.errorCount = 0;
}
m_opts.errorCount += 1;
}
- msgText = _this._getMessage(r = old.apply(null, resp), m_opts[type + 'Message']);
+ handlerResp = m_opts.returnsPromise ? resp[0] : typeof (_base = opts[type])._originalHandler === "function" ? _base._originalHandler.apply(_base, resp) : void 0;
+ responseOpts = _this._getHandlerResponse(handlerResp);
+ if (_.isString(responseOpts)) {
+ responseOpts = {
+ message: responseOpts
+ };
+ }
if (type === 'error' && ((xhr != null ? xhr.status : void 0) === 0 || reason === 'abort')) {
msg.hide();
return;
}
if (type === 'error' && ((m_opts.ignoredErrorCodes != null) && (_ref6 = xhr != null ? xhr.status : void 0, __indexOf.call(m_opts.ignoredErrorCodes, _ref6) >= 0))) {
msg.hide();
return;
}
- msgOpts = $.extend({}, m_opts, {
- message: msgText,
+ defaultOpts = {
+ message: getMessageText(type, xhr),
type: type,
events: (_ref7 = events[type]) != null ? _ref7 : {},
hideOnNavigate: type === 'success'
- });
+ };
+ msgOpts = $.extend({}, m_opts, defaultOpts, responseOpts);
if (typeof ((_ref8 = msgOpts.retry) != null ? _ref8.allow : void 0) === 'number') {
msgOpts.retry.allow--;
}
if (type === 'error' && (xhr != null ? xhr.status : void 0) >= 500 && ((_ref9 = msgOpts.retry) != null ? _ref9.allow : void 0)) {
if (msgOpts.retry.delay == null) {
@@ -1058,27 +1072,37 @@
delete msgOpts.actions.retry;
delete msgOpts.actions.cancel;
delete m_opts._retryActions;
}
msg.update(msgOpts);
- if (msgText) {
- $.globalMessenger();
+ if (responseOpts && msgOpts.message) {
+ Messenger();
return msg.show();
} else {
return msg.hide();
}
};
- return opts[type]._originalHandler = old;
});
+ if (!m_opts.returnsPromise) {
+ for (type in handlers) {
+ handler = handlers[type];
+ old = opts[type];
+ opts[type] = handler;
+ opts[type]._originalHandler = old;
+ }
+ }
msg._actionInstance = m_opts.action.apply(m_opts, [opts].concat(__slice.call(args)));
+ if (m_opts.returnsPromise) {
+ msg._actionInstance.then(handlers.success, handlers.error);
+ }
promiseAttrs = ['done', 'progress', 'fail', 'state', 'then'];
for (_i = 0, _len = promiseAttrs.length; _i < _len; _i++) {
attr = promiseAttrs[_i];
if (msg[attr] != null) {
delete msg[attr];
}
- msg[attr] = (_ref2 = msg._actionInstance) != null ? _ref2[attr] : void 0;
+ msg[attr] = (_ref3 = msg._actionInstance) != null ? _ref3[attr] : void 0;
}
return msg;
};
ActionMessenger.prototype["do"] = ActionMessenger.prototype.run;
@@ -1088,33 +1112,41 @@
m_opts = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
m_opts.action = $.ajax;
return this.run.apply(this, [m_opts].concat(__slice.call(args)));
};
+ ActionMessenger.prototype.expectPromise = function(action, m_opts) {
+ m_opts = _.extend({}, m_opts, {
+ action: action,
+ returnsPromise: true
+ });
+ return this.run(m_opts);
+ };
+
return ActionMessenger;
})(_Messenger);
$.fn.messenger = function() {
- var $el, args, func, instance, opts, _ref1, _ref2, _ref3;
+ var $el, args, func, instance, opts, _ref2, _ref3, _ref4;
func = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (func == null) {
func = {};
}
$el = this;
if (!(func != null) || !_.isString(func)) {
opts = func;
if (!($el.data('messenger') != null)) {
- _Messenger = (_ref1 = (_ref2 = Messenger.themes[opts.theme]) != null ? _ref2.Messenger : void 0) != null ? _ref1 : ActionMessenger;
+ _Messenger = (_ref2 = (_ref3 = Messenger.themes[opts.theme]) != null ? _ref3.Messenger : void 0) != null ? _ref2 : ActionMessenger;
$el.data('messenger', instance = new _Messenger($.extend({
el: $el
}, opts)));
instance.render();
}
return $el.data('messenger');
} else {
- return (_ref3 = $el.data('messenger'))[func].apply(_ref3, args);
+ return (_ref4 = $el.data('messenger'))[func].apply(_ref4, args);
}
};
window.Messenger._call = function(opts) {
var $el, $parent, choosen_loc, chosen_loc, classes, defaultOpts, inst, loc, locations, _i, _len;
@@ -1161,10 +1193,10 @@
};
$.extend(Messenger, {
Message: RetryingMessage,
Messenger: ActionMessenger,
- themes: (_ref1 = Messenger.themes) != null ? _ref1 : {}
+ themes: (_ref2 = Messenger.themes) != null ? _ref2 : {}
});
$.globalMessenger = window.Messenger = Messenger;
}).call(this);