messengerjs-rails 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +18 -0
- data/README.md +56 -0
- data/lib/messengerjs-rails.rb +8 -0
- data/lib/messengerjs-rails/version.rb +5 -0
- data/vendor/assets/javascripts/messenger-theme-future.js +33 -0
- data/vendor/assets/javascripts/messenger.js +1171 -0
- data/vendor/assets/stylesheets/messenger-spinner.css +235 -0
- data/vendor/assets/stylesheets/messenger-theme-air.css +443 -0
- data/vendor/assets/stylesheets/messenger-theme-block.css +46 -0
- data/vendor/assets/stylesheets/messenger-theme-future.css +496 -0
- data/vendor/assets/stylesheets/messenger-theme-ice.css +118 -0
- data/vendor/assets/stylesheets/messenger.css +93 -0
- metadata +56 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
2
|
+
a copy of this software and associated documentation files (the
|
3
|
+
"Software"), to deal in the Software without restriction, including
|
4
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
5
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
6
|
+
permit persons to whom the Software is furnished to do so, subject to
|
7
|
+
the following conditions:
|
8
|
+
|
9
|
+
The above copyright notice and this permission notice shall be
|
10
|
+
included in all copies or substantial portions of the Software.
|
11
|
+
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
13
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
14
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
15
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
16
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
17
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
18
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# messengerjs-rails
|
2
|
+
|
3
|
+
messengerjs-rails is a Rails (3.1 and above) wrapper for [Messenger](http://github.hubspot.com/messenger/) javascript library.
|
4
|
+
|
5
|
+
# Messenger
|
6
|
+
|
7
|
+
- Show transactional messages in your app.
|
8
|
+
- Wrap AJAX requests with progress, success and error messages, and add retry to your failed requests.
|
9
|
+
- Add actions (undo, cancel, etc.) to your messages.
|
10
|
+
- 4kb minified and compressed.
|
11
|
+
- Works in everything modern, and IE7 and above.
|
12
|
+
|
13
|
+
<a href="http://hubspot.github.com/messenger">
|
14
|
+
<img src="https://raw.github.com/HubSpot/messenger/f500e931fe7099da460aaf9b5c1013f377e40775/images/messenger_preview.png" title="Thanks for checking out Messenger!" width="400px" style="max-width: 400px"/>
|
15
|
+
</a>
|
16
|
+
|
17
|
+
[Demo and Usage](http://hubspot.github.com/messenger)
|
18
|
+
|
19
|
+
## Requirements
|
20
|
+
|
21
|
+
1. jQuery
|
22
|
+
|
23
|
+
2. Plays well with, but doesn't require, Bootstrap
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add the following to your gemfile:
|
28
|
+
|
29
|
+
gem 'messengerjs-rails'
|
30
|
+
|
31
|
+
Add the following directive to your application.coffee / application.js:
|
32
|
+
|
33
|
+
//= require messenger
|
34
|
+
//= require messenger-theme-future
|
35
|
+
|
36
|
+
Add the following directive to your application.scss / application.css. There are four themes provided (future, air, block and ice), change the required stylesheet as needed.
|
37
|
+
|
38
|
+
*= require messenger
|
39
|
+
*= require messenger-spinner
|
40
|
+
*= require messenger-theme-future
|
41
|
+
|
42
|
+
|
43
|
+
## Versioning
|
44
|
+
|
45
|
+
Every attempt is made to mirror the currently shipping Messenger version number wherever possible.
|
46
|
+
The major, minor, and patch version numbers will always represent the Messenger version.
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
Feel free to open an issue ticket if you find something that could be improved.
|
51
|
+
|
52
|
+
## Acknowledgements
|
53
|
+
|
54
|
+
Special thanks to [HubSpot](http://dev.hubspot.com/) and all Messenger contributor.
|
55
|
+
|
56
|
+
Copyright [Ben Song](zbin.song@gmail.com), released under the MIT License.
|
@@ -0,0 +1,33 @@
|
|
1
|
+
(function() {
|
2
|
+
var $, FutureMessage, spinner_template,
|
3
|
+
__hasProp = {}.hasOwnProperty,
|
4
|
+
__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; };
|
5
|
+
|
6
|
+
$ = jQuery;
|
7
|
+
|
8
|
+
spinner_template = '<div class="messenger-spinner">\n <span class="messenger-spinner-side messenger-spinner-side-left">\n <span class="messenger-spinner-fill"></span>\n </span>\n <span class="messenger-spinner-side messenger-spinner-side-right">\n <span class="messenger-spinner-fill"></span>\n </span>\n</div>';
|
9
|
+
|
10
|
+
FutureMessage = (function(_super) {
|
11
|
+
|
12
|
+
__extends(FutureMessage, _super);
|
13
|
+
|
14
|
+
function FutureMessage() {
|
15
|
+
return FutureMessage.__super__.constructor.apply(this, arguments);
|
16
|
+
}
|
17
|
+
|
18
|
+
FutureMessage.prototype.template = function(opts) {
|
19
|
+
var $message;
|
20
|
+
$message = FutureMessage.__super__.template.apply(this, arguments);
|
21
|
+
$message.append($(spinner_template));
|
22
|
+
return $message;
|
23
|
+
};
|
24
|
+
|
25
|
+
return FutureMessage;
|
26
|
+
|
27
|
+
})(window.Messenger.Message);
|
28
|
+
|
29
|
+
window.Messenger.themes.future = {
|
30
|
+
Message: FutureMessage
|
31
|
+
};
|
32
|
+
|
33
|
+
}).call(this);
|
@@ -0,0 +1,1171 @@
|
|
1
|
+
/*! messenger 1.3.0 2013-03-21 */
|
2
|
+
/*
|
3
|
+
* This file begins the output concatenated into messenger.js
|
4
|
+
*
|
5
|
+
* It establishes the Messenger object while preserving whatever it was before
|
6
|
+
* (for noConflict), and making it a callable function.
|
7
|
+
*/
|
8
|
+
|
9
|
+
(function(){
|
10
|
+
var _prevMessenger = window.Messenger;
|
11
|
+
var localMessenger;
|
12
|
+
|
13
|
+
localMessenger = window.Messenger = function(){
|
14
|
+
return localMessenger._call.apply(this, arguments);
|
15
|
+
}
|
16
|
+
|
17
|
+
window.Messenger.noConflict = function(){
|
18
|
+
window.Messenger = _prevMessenger;
|
19
|
+
|
20
|
+
return localMessenger;
|
21
|
+
}
|
22
|
+
})();
|
23
|
+
|
24
|
+
/*
|
25
|
+
* This file contains shims for when Underscore and Backbone
|
26
|
+
* are not included.
|
27
|
+
*
|
28
|
+
* Portions taken from Underscore.js and Backbone.js
|
29
|
+
* Both of which are Copyright (c) 2009-2013 Jeremy Ashkenas, DocumentCloud
|
30
|
+
*/
|
31
|
+
window.Messenger._ = (function() {
|
32
|
+
if (window._)
|
33
|
+
return window._
|
34
|
+
|
35
|
+
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
|
36
|
+
|
37
|
+
// Create quick reference variables for speed access to core prototypes.
|
38
|
+
var push = ArrayProto.push,
|
39
|
+
slice = ArrayProto.slice,
|
40
|
+
concat = ArrayProto.concat,
|
41
|
+
toString = ObjProto.toString,
|
42
|
+
hasOwnProperty = ObjProto.hasOwnProperty;
|
43
|
+
|
44
|
+
// All **ECMAScript 5** native function implementations that we hope to use
|
45
|
+
// are declared here.
|
46
|
+
var
|
47
|
+
nativeForEach = ArrayProto.forEach,
|
48
|
+
nativeMap = ArrayProto.map,
|
49
|
+
nativeReduce = ArrayProto.reduce,
|
50
|
+
nativeReduceRight = ArrayProto.reduceRight,
|
51
|
+
nativeFilter = ArrayProto.filter,
|
52
|
+
nativeEvery = ArrayProto.every,
|
53
|
+
nativeSome = ArrayProto.some,
|
54
|
+
nativeIndexOf = ArrayProto.indexOf,
|
55
|
+
nativeLastIndexOf = ArrayProto.lastIndexOf,
|
56
|
+
nativeIsArray = Array.isArray,
|
57
|
+
nativeKeys = Object.keys,
|
58
|
+
nativeBind = FuncProto.bind;
|
59
|
+
|
60
|
+
// Create a safe reference to the Underscore object for use below.
|
61
|
+
var _ = {};
|
62
|
+
|
63
|
+
// Establish the object that gets returned to break out of a loop iteration.
|
64
|
+
var breaker = {};
|
65
|
+
|
66
|
+
var each = _.each = _.forEach = function(obj, iterator, context) {
|
67
|
+
if (obj == null) return;
|
68
|
+
if (nativeForEach && obj.forEach === nativeForEach) {
|
69
|
+
obj.forEach(iterator, context);
|
70
|
+
} else if (obj.length === +obj.length) {
|
71
|
+
for (var i = 0, l = obj.length; i < l; i++) {
|
72
|
+
if (iterator.call(context, obj[i], i, obj) === breaker) return;
|
73
|
+
}
|
74
|
+
} else {
|
75
|
+
for (var key in obj) {
|
76
|
+
if (_.has(obj, key)) {
|
77
|
+
if (iterator.call(context, obj[key], key, obj) === breaker) return;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
}
|
81
|
+
};
|
82
|
+
|
83
|
+
_.result = function(object, property) {
|
84
|
+
if (object == null) return null;
|
85
|
+
var value = object[property];
|
86
|
+
return _.isFunction(value) ? value.call(object) : value;
|
87
|
+
};
|
88
|
+
|
89
|
+
_.once = function(func) {
|
90
|
+
var ran = false, memo;
|
91
|
+
return function() {
|
92
|
+
if (ran) return memo;
|
93
|
+
ran = true;
|
94
|
+
memo = func.apply(this, arguments);
|
95
|
+
func = null;
|
96
|
+
return memo;
|
97
|
+
};
|
98
|
+
};
|
99
|
+
|
100
|
+
var idCounter = 0;
|
101
|
+
_.uniqueId = function(prefix) {
|
102
|
+
var id = ++idCounter + '';
|
103
|
+
return prefix ? prefix + id : id;
|
104
|
+
};
|
105
|
+
|
106
|
+
_.filter = _.select = function(obj, iterator, context) {
|
107
|
+
var results = [];
|
108
|
+
if (obj == null) return results;
|
109
|
+
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
|
110
|
+
each(obj, function(value, index, list) {
|
111
|
+
if (iterator.call(context, value, index, list)) results[results.length] = value;
|
112
|
+
});
|
113
|
+
return results;
|
114
|
+
};
|
115
|
+
|
116
|
+
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.
|
117
|
+
each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
|
118
|
+
_['is' + name] = function(obj) {
|
119
|
+
return toString.call(obj) == '[object ' + name + ']';
|
120
|
+
};
|
121
|
+
});
|
122
|
+
|
123
|
+
_.defaults = function(obj) {
|
124
|
+
each(slice.call(arguments, 1), function(source) {
|
125
|
+
if (source) {
|
126
|
+
for (var prop in source) {
|
127
|
+
if (obj[prop] == null) obj[prop] = source[prop];
|
128
|
+
}
|
129
|
+
}
|
130
|
+
});
|
131
|
+
return obj;
|
132
|
+
};
|
133
|
+
|
134
|
+
_.extend = function(obj) {
|
135
|
+
each(slice.call(arguments, 1), function(source) {
|
136
|
+
if (source) {
|
137
|
+
for (var prop in source) {
|
138
|
+
obj[prop] = source[prop];
|
139
|
+
}
|
140
|
+
}
|
141
|
+
});
|
142
|
+
return obj;
|
143
|
+
};
|
144
|
+
|
145
|
+
_.keys = nativeKeys || function(obj) {
|
146
|
+
if (obj !== Object(obj)) throw new TypeError('Invalid object');
|
147
|
+
var keys = [];
|
148
|
+
for (var key in obj) if (_.has(obj, key)) keys[keys.length] = key;
|
149
|
+
return keys;
|
150
|
+
};
|
151
|
+
|
152
|
+
_.bind = function(func, context) {
|
153
|
+
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
|
154
|
+
var args = slice.call(arguments, 2);
|
155
|
+
return function() {
|
156
|
+
return func.apply(context, args.concat(slice.call(arguments)));
|
157
|
+
};
|
158
|
+
};
|
159
|
+
|
160
|
+
_.isObject = function(obj) {
|
161
|
+
return obj === Object(obj);
|
162
|
+
};
|
163
|
+
|
164
|
+
return _;
|
165
|
+
})();
|
166
|
+
|
167
|
+
window.Messenger.Events = (function() {
|
168
|
+
if (window.Backbone && Backbone.Events) {
|
169
|
+
return Backbone.Events;
|
170
|
+
}
|
171
|
+
|
172
|
+
var eventsShim = function() {
|
173
|
+
var eventSplitter = /\s+/;
|
174
|
+
|
175
|
+
var eventsApi = function(obj, action, name, rest) {
|
176
|
+
if (!name) return true;
|
177
|
+
if (typeof name === 'object') {
|
178
|
+
for (var key in name) {
|
179
|
+
obj[action].apply(obj, [key, name[key]].concat(rest));
|
180
|
+
}
|
181
|
+
} else if (eventSplitter.test(name)) {
|
182
|
+
var names = name.split(eventSplitter);
|
183
|
+
for (var i = 0, l = names.length; i < l; i++) {
|
184
|
+
obj[action].apply(obj, [names[i]].concat(rest));
|
185
|
+
}
|
186
|
+
} else {
|
187
|
+
return true;
|
188
|
+
}
|
189
|
+
};
|
190
|
+
|
191
|
+
var triggerEvents = function(events, args) {
|
192
|
+
var ev, i = -1, l = events.length;
|
193
|
+
switch (args.length) {
|
194
|
+
case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx);
|
195
|
+
return;
|
196
|
+
case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, args[0]);
|
197
|
+
return;
|
198
|
+
case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, args[0], args[1]);
|
199
|
+
return;
|
200
|
+
case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, args[0], args[1], args[2]);
|
201
|
+
return;
|
202
|
+
default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args);
|
203
|
+
}
|
204
|
+
};
|
205
|
+
|
206
|
+
var Events = {
|
207
|
+
|
208
|
+
on: function(name, callback, context) {
|
209
|
+
if (!(eventsApi(this, 'on', name, [callback, context]) && callback)) return this;
|
210
|
+
this._events || (this._events = {});
|
211
|
+
var list = this._events[name] || (this._events[name] = []);
|
212
|
+
list.push({callback: callback, context: context, ctx: context || this});
|
213
|
+
return this;
|
214
|
+
},
|
215
|
+
|
216
|
+
once: function(name, callback, context) {
|
217
|
+
if (!(eventsApi(this, 'once', name, [callback, context]) && callback)) return this;
|
218
|
+
var self = this;
|
219
|
+
var once = _.once(function() {
|
220
|
+
self.off(name, once);
|
221
|
+
callback.apply(this, arguments);
|
222
|
+
});
|
223
|
+
once._callback = callback;
|
224
|
+
this.on(name, once, context);
|
225
|
+
return this;
|
226
|
+
},
|
227
|
+
|
228
|
+
off: function(name, callback, context) {
|
229
|
+
var list, ev, events, names, i, l, j, k;
|
230
|
+
if (!this._events || !eventsApi(this, 'off', name, [callback, context])) return this;
|
231
|
+
if (!name && !callback && !context) {
|
232
|
+
this._events = {};
|
233
|
+
return this;
|
234
|
+
}
|
235
|
+
|
236
|
+
names = name ? [name] : _.keys(this._events);
|
237
|
+
for (i = 0, l = names.length; i < l; i++) {
|
238
|
+
name = names[i];
|
239
|
+
if (list = this._events[name]) {
|
240
|
+
events = [];
|
241
|
+
if (callback || context) {
|
242
|
+
for (j = 0, k = list.length; j < k; j++) {
|
243
|
+
ev = list[j];
|
244
|
+
if ((callback && callback !== ev.callback &&
|
245
|
+
callback !== ev.callback._callback) ||
|
246
|
+
(context && context !== ev.context)) {
|
247
|
+
events.push(ev);
|
248
|
+
}
|
249
|
+
}
|
250
|
+
}
|
251
|
+
this._events[name] = events;
|
252
|
+
}
|
253
|
+
}
|
254
|
+
|
255
|
+
return this;
|
256
|
+
},
|
257
|
+
|
258
|
+
trigger: function(name) {
|
259
|
+
if (!this._events) return this;
|
260
|
+
var args = Array.prototype.slice.call(arguments, 1);
|
261
|
+
if (!eventsApi(this, 'trigger', name, args)) return this;
|
262
|
+
var events = this._events[name];
|
263
|
+
var allEvents = this._events.all;
|
264
|
+
if (events) triggerEvents(events, args);
|
265
|
+
if (allEvents) triggerEvents(allEvents, arguments);
|
266
|
+
return this;
|
267
|
+
},
|
268
|
+
|
269
|
+
listenTo: function(obj, name, callback) {
|
270
|
+
var listeners = this._listeners || (this._listeners = {});
|
271
|
+
var id = obj._listenerId || (obj._listenerId = _.uniqueId('l'));
|
272
|
+
listeners[id] = obj;
|
273
|
+
obj.on(name, typeof name === 'object' ? this : callback, this);
|
274
|
+
return this;
|
275
|
+
},
|
276
|
+
|
277
|
+
stopListening: function(obj, name, callback) {
|
278
|
+
var listeners = this._listeners;
|
279
|
+
if (!listeners) return;
|
280
|
+
if (obj) {
|
281
|
+
obj.off(name, typeof name === 'object' ? this : callback, this);
|
282
|
+
if (!name && !callback) delete listeners[obj._listenerId];
|
283
|
+
} else {
|
284
|
+
if (typeof name === 'object') callback = this;
|
285
|
+
for (var id in listeners) {
|
286
|
+
listeners[id].off(name, callback, this);
|
287
|
+
}
|
288
|
+
this._listeners = {};
|
289
|
+
}
|
290
|
+
return this;
|
291
|
+
}
|
292
|
+
};
|
293
|
+
|
294
|
+
Events.bind = Events.on;
|
295
|
+
Events.unbind = Events.off;
|
296
|
+
return Events;
|
297
|
+
};
|
298
|
+
return eventsShim();
|
299
|
+
})();
|
300
|
+
|
301
|
+
(function() {
|
302
|
+
var $, ActionMessenger, BaseView, Events, RetryingMessage, _, _Message, _Messenger, _ref, _ref1,
|
303
|
+
__hasProp = {}.hasOwnProperty,
|
304
|
+
__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; },
|
305
|
+
__slice = [].slice,
|
306
|
+
__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; };
|
307
|
+
|
308
|
+
$ = jQuery;
|
309
|
+
|
310
|
+
_ = _ != null ? _ : window.Messenger._;
|
311
|
+
|
312
|
+
Events = (_ref = typeof Backbone !== "undefined" && Backbone !== null ? Backbone.Events : void 0) != null ? _ref : window.Messenger.Events;
|
313
|
+
|
314
|
+
BaseView = (function() {
|
315
|
+
|
316
|
+
function BaseView(options) {
|
317
|
+
$.extend(this, Events);
|
318
|
+
if (_.isObject(options)) {
|
319
|
+
if (options.el) {
|
320
|
+
this.setElement(options.el);
|
321
|
+
}
|
322
|
+
this.model = options.model;
|
323
|
+
}
|
324
|
+
this.initialize.apply(this, arguments);
|
325
|
+
}
|
326
|
+
|
327
|
+
BaseView.prototype.setElement = function(el) {
|
328
|
+
this.$el = $(el);
|
329
|
+
return this.el = this.$el[0];
|
330
|
+
};
|
331
|
+
|
332
|
+
BaseView.prototype.delegateEvents = function(events) {
|
333
|
+
var delegateEventSplitter, eventName, key, match, method, selector, _results;
|
334
|
+
if (!(events || (events = _.result(this, 'events')))) {
|
335
|
+
return;
|
336
|
+
}
|
337
|
+
delegateEventSplitter = /^(\S+)\s*(.*)$/;
|
338
|
+
this.undelegateEvents();
|
339
|
+
_results = [];
|
340
|
+
for (key in events) {
|
341
|
+
method = events[key];
|
342
|
+
if (!_.isFunction(method)) {
|
343
|
+
method = this[events[key]];
|
344
|
+
}
|
345
|
+
if (!method) {
|
346
|
+
throw new Error("Method " + events[key] + " does not exist");
|
347
|
+
}
|
348
|
+
match = key.match(delegateEventSplitter);
|
349
|
+
eventName = match[1];
|
350
|
+
selector = match[2];
|
351
|
+
method = _.bind(method, this);
|
352
|
+
eventName += ".delegateEvents" + this.cid;
|
353
|
+
if (selector === '') {
|
354
|
+
_results.push(this.$el.on(eventName, method));
|
355
|
+
} else {
|
356
|
+
_results.push(this.$el.on(eventName, selector, method));
|
357
|
+
}
|
358
|
+
}
|
359
|
+
return _results;
|
360
|
+
};
|
361
|
+
|
362
|
+
BaseView.prototype.undelegateEvents = function() {
|
363
|
+
return this.$el.off(".delegateEvents" + this.cid);
|
364
|
+
};
|
365
|
+
|
366
|
+
BaseView.prototype.remove = function() {
|
367
|
+
this.undelegateEvents();
|
368
|
+
return this.$el.remove();
|
369
|
+
};
|
370
|
+
|
371
|
+
return BaseView;
|
372
|
+
|
373
|
+
})();
|
374
|
+
|
375
|
+
_Message = (function(_super) {
|
376
|
+
|
377
|
+
__extends(_Message, _super);
|
378
|
+
|
379
|
+
function _Message() {
|
380
|
+
return _Message.__super__.constructor.apply(this, arguments);
|
381
|
+
}
|
382
|
+
|
383
|
+
_Message.prototype.defaults = {
|
384
|
+
hideAfter: 10,
|
385
|
+
scroll: true
|
386
|
+
};
|
387
|
+
|
388
|
+
_Message.prototype.initialize = function(opts) {
|
389
|
+
if (opts == null) {
|
390
|
+
opts = {};
|
391
|
+
}
|
392
|
+
this.shown = false;
|
393
|
+
this.rendered = false;
|
394
|
+
this.messenger = opts.messenger;
|
395
|
+
return this.options = $.extend({}, this.options, opts, this.defaults);
|
396
|
+
};
|
397
|
+
|
398
|
+
_Message.prototype.show = function() {
|
399
|
+
var wasShown;
|
400
|
+
if (!this.rendered) {
|
401
|
+
this.render();
|
402
|
+
}
|
403
|
+
this.$message.removeClass('messenger-hidden');
|
404
|
+
wasShown = this.shown;
|
405
|
+
this.shown = true;
|
406
|
+
if (!wasShown) {
|
407
|
+
return this.trigger('show');
|
408
|
+
}
|
409
|
+
};
|
410
|
+
|
411
|
+
_Message.prototype.hide = function() {
|
412
|
+
var wasShown;
|
413
|
+
if (!this.rendered) {
|
414
|
+
return;
|
415
|
+
}
|
416
|
+
this.$message.addClass('messenger-hidden');
|
417
|
+
wasShown = this.shown;
|
418
|
+
this.shown = false;
|
419
|
+
if (wasShown) {
|
420
|
+
return this.trigger('hide');
|
421
|
+
}
|
422
|
+
};
|
423
|
+
|
424
|
+
_Message.prototype.cancel = function() {
|
425
|
+
return this.hide();
|
426
|
+
};
|
427
|
+
|
428
|
+
_Message.prototype.update = function(opts) {
|
429
|
+
var _ref1,
|
430
|
+
_this = this;
|
431
|
+
if (_.isString(opts)) {
|
432
|
+
opts = {
|
433
|
+
message: opts
|
434
|
+
};
|
435
|
+
}
|
436
|
+
$.extend(this.options, opts);
|
437
|
+
this.lastUpdate = new Date();
|
438
|
+
this.rendered = false;
|
439
|
+
this.events = (_ref1 = this.options.events) != null ? _ref1 : {};
|
440
|
+
this.render();
|
441
|
+
this.actionsToEvents();
|
442
|
+
this.delegateEvents();
|
443
|
+
this.checkClickable();
|
444
|
+
if (this.options.hideAfter) {
|
445
|
+
this.$message.addClass('messenger-will-hide-after');
|
446
|
+
if (this._hideTimeout != null) {
|
447
|
+
clearTimeout(this._hideTimeout);
|
448
|
+
}
|
449
|
+
this._hideTimeout = setTimeout(function() {
|
450
|
+
return _this.hide();
|
451
|
+
}, this.options.hideAfter * 1000);
|
452
|
+
} else {
|
453
|
+
this.$message.removeClass('messenger-will-hide-after');
|
454
|
+
}
|
455
|
+
if (this.options.hideOnNavigate) {
|
456
|
+
this.$message.addClass('messenger-will-hide-on-navigate');
|
457
|
+
if ((typeof Backbone !== "undefined" && Backbone !== null ? Backbone.history : void 0) != null) {
|
458
|
+
Backbone.history.on('route', function() {
|
459
|
+
return _this.hide();
|
460
|
+
});
|
461
|
+
}
|
462
|
+
} else {
|
463
|
+
this.$message.removeClass('messenger-will-hide-on-navigate');
|
464
|
+
}
|
465
|
+
return this.trigger('update', this);
|
466
|
+
};
|
467
|
+
|
468
|
+
_Message.prototype.scrollTo = function() {
|
469
|
+
if (!this.options.scroll) {
|
470
|
+
return;
|
471
|
+
}
|
472
|
+
return $.scrollTo(this.$el, {
|
473
|
+
duration: 400,
|
474
|
+
offset: {
|
475
|
+
left: 0,
|
476
|
+
top: -20
|
477
|
+
}
|
478
|
+
});
|
479
|
+
};
|
480
|
+
|
481
|
+
_Message.prototype.timeSinceUpdate = function() {
|
482
|
+
if (this.lastUpdate) {
|
483
|
+
return (new Date) - this.lastUpdate;
|
484
|
+
} else {
|
485
|
+
return null;
|
486
|
+
}
|
487
|
+
};
|
488
|
+
|
489
|
+
_Message.prototype.actionsToEvents = function() {
|
490
|
+
var act, name, _ref1, _results,
|
491
|
+
_this = this;
|
492
|
+
_ref1 = this.options.actions;
|
493
|
+
_results = [];
|
494
|
+
for (name in _ref1) {
|
495
|
+
act = _ref1[name];
|
496
|
+
_results.push(this.events["click [data-action=\"" + name + "\"] a"] = (function(act) {
|
497
|
+
return function(e) {
|
498
|
+
e.preventDefault();
|
499
|
+
e.stopPropagation();
|
500
|
+
_this.trigger("action:" + name, act, e);
|
501
|
+
return act.action(e);
|
502
|
+
};
|
503
|
+
})(act));
|
504
|
+
}
|
505
|
+
return _results;
|
506
|
+
};
|
507
|
+
|
508
|
+
_Message.prototype.checkClickable = function() {
|
509
|
+
var evt, name, _ref1, _results;
|
510
|
+
_ref1 = this.events;
|
511
|
+
_results = [];
|
512
|
+
for (name in _ref1) {
|
513
|
+
evt = _ref1[name];
|
514
|
+
if (name === 'click') {
|
515
|
+
_results.push(this.$message.addClass('messenger-clickable'));
|
516
|
+
} else {
|
517
|
+
_results.push(void 0);
|
518
|
+
}
|
519
|
+
}
|
520
|
+
return _results;
|
521
|
+
};
|
522
|
+
|
523
|
+
_Message.prototype.undelegateEvents = function() {
|
524
|
+
var _ref1;
|
525
|
+
_Message.__super__.undelegateEvents.apply(this, arguments);
|
526
|
+
return (_ref1 = this.$message) != null ? _ref1.removeClass('messenger-clickable') : void 0;
|
527
|
+
};
|
528
|
+
|
529
|
+
_Message.prototype.parseActions = function() {
|
530
|
+
var act, actions, n_act, name, _ref1, _ref2;
|
531
|
+
actions = [];
|
532
|
+
_ref1 = this.options.actions;
|
533
|
+
for (name in _ref1) {
|
534
|
+
act = _ref1[name];
|
535
|
+
n_act = $.extend({}, act);
|
536
|
+
n_act.name = name;
|
537
|
+
if ((_ref2 = n_act.label) == null) {
|
538
|
+
n_act.label = name;
|
539
|
+
}
|
540
|
+
actions.push(n_act);
|
541
|
+
}
|
542
|
+
return actions;
|
543
|
+
};
|
544
|
+
|
545
|
+
_Message.prototype.template = function(opts) {
|
546
|
+
var $action, $actions, $cancel, $link, $message, $text, action, _i, _len, _ref1,
|
547
|
+
_this = this;
|
548
|
+
$message = $("<div class='messenger-message message alert " + opts.type + " message-" + opts.type + " alert-" + opts.type + "'>");
|
549
|
+
if (opts.showCloseButton) {
|
550
|
+
$cancel = $('<button type="button" class="close" data-dismiss="alert">×</button>');
|
551
|
+
$cancel.click(function() {
|
552
|
+
_this.cancel();
|
553
|
+
return true;
|
554
|
+
});
|
555
|
+
$message.append($cancel);
|
556
|
+
}
|
557
|
+
$text = $("<div class=\"messenger-message-inner\">" + opts.message + "</div>");
|
558
|
+
$message.append($text);
|
559
|
+
if (opts.actions.length) {
|
560
|
+
$actions = $('<div class="messenger-actions">');
|
561
|
+
}
|
562
|
+
_ref1 = opts.actions;
|
563
|
+
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
564
|
+
action = _ref1[_i];
|
565
|
+
$action = $('<span>');
|
566
|
+
$action.attr('data-action', "" + action.name);
|
567
|
+
$link = $('<a>');
|
568
|
+
$link.html(action.label);
|
569
|
+
$action.append($('<span class="messenger-phrase">'));
|
570
|
+
$action.append($link);
|
571
|
+
$actions.append($action);
|
572
|
+
}
|
573
|
+
$message.append($actions);
|
574
|
+
return $message;
|
575
|
+
};
|
576
|
+
|
577
|
+
_Message.prototype.render = function() {
|
578
|
+
var opts;
|
579
|
+
if (this.rendered) {
|
580
|
+
return;
|
581
|
+
}
|
582
|
+
if (!this._hasSlot) {
|
583
|
+
this.setElement(this.messenger._reserveMessageSlot(this));
|
584
|
+
this._hasSlot = true;
|
585
|
+
}
|
586
|
+
opts = $.extend({}, this.options, {
|
587
|
+
actions: this.parseActions()
|
588
|
+
});
|
589
|
+
this.$message = $(this.template(opts));
|
590
|
+
this.$el.html(this.$message);
|
591
|
+
this.shown = true;
|
592
|
+
this.rendered = true;
|
593
|
+
return this.trigger('render');
|
594
|
+
};
|
595
|
+
|
596
|
+
return _Message;
|
597
|
+
|
598
|
+
})(BaseView);
|
599
|
+
|
600
|
+
RetryingMessage = (function(_super) {
|
601
|
+
|
602
|
+
__extends(RetryingMessage, _super);
|
603
|
+
|
604
|
+
function RetryingMessage() {
|
605
|
+
return RetryingMessage.__super__.constructor.apply(this, arguments);
|
606
|
+
}
|
607
|
+
|
608
|
+
RetryingMessage.prototype.initialize = function() {
|
609
|
+
RetryingMessage.__super__.initialize.apply(this, arguments);
|
610
|
+
return this._timers = {};
|
611
|
+
};
|
612
|
+
|
613
|
+
RetryingMessage.prototype.cancel = function() {
|
614
|
+
this.clearTimers();
|
615
|
+
this.hide();
|
616
|
+
if ((this._actionInstance != null) && (this._actionInstance.abort != null)) {
|
617
|
+
return this._actionInstance.abort();
|
618
|
+
}
|
619
|
+
};
|
620
|
+
|
621
|
+
RetryingMessage.prototype.clearTimers = function() {
|
622
|
+
var name, timer, _ref1, _ref2;
|
623
|
+
_ref1 = this._timers;
|
624
|
+
for (name in _ref1) {
|
625
|
+
timer = _ref1[name];
|
626
|
+
clearTimeout(timer);
|
627
|
+
}
|
628
|
+
this._timers = {};
|
629
|
+
return (_ref2 = this.$message) != null ? _ref2.removeClass('messenger-retry-soon messenger-retry-later') : void 0;
|
630
|
+
};
|
631
|
+
|
632
|
+
RetryingMessage.prototype.render = function() {
|
633
|
+
var action, name, _ref1, _results;
|
634
|
+
RetryingMessage.__super__.render.apply(this, arguments);
|
635
|
+
this.clearTimers();
|
636
|
+
_ref1 = this.options.actions;
|
637
|
+
_results = [];
|
638
|
+
for (name in _ref1) {
|
639
|
+
action = _ref1[name];
|
640
|
+
if (action.auto) {
|
641
|
+
_results.push(this.startCountdown(name, action));
|
642
|
+
} else {
|
643
|
+
_results.push(void 0);
|
644
|
+
}
|
645
|
+
}
|
646
|
+
return _results;
|
647
|
+
};
|
648
|
+
|
649
|
+
RetryingMessage.prototype.renderPhrase = function(action, time) {
|
650
|
+
var phrase;
|
651
|
+
phrase = action.phrase.replace('TIME', this.formatTime(time));
|
652
|
+
return phrase;
|
653
|
+
};
|
654
|
+
|
655
|
+
RetryingMessage.prototype.formatTime = function(time) {
|
656
|
+
var pluralize;
|
657
|
+
pluralize = function(num, str) {
|
658
|
+
num = Math.floor(num);
|
659
|
+
if (num !== 1) {
|
660
|
+
str = str + 's';
|
661
|
+
}
|
662
|
+
return 'in ' + num + ' ' + str;
|
663
|
+
};
|
664
|
+
if (Math.floor(time) === 0) {
|
665
|
+
return 'now...';
|
666
|
+
}
|
667
|
+
if (time < 60) {
|
668
|
+
return pluralize(time, 'second');
|
669
|
+
}
|
670
|
+
time /= 60;
|
671
|
+
if (time < 60) {
|
672
|
+
return pluralize(time, 'minute');
|
673
|
+
}
|
674
|
+
time /= 60;
|
675
|
+
return pluralize(time, 'hour');
|
676
|
+
};
|
677
|
+
|
678
|
+
RetryingMessage.prototype.startCountdown = function(name, action) {
|
679
|
+
var $phrase, remaining, tick, _ref1,
|
680
|
+
_this = this;
|
681
|
+
if (this._timers[name] != null) {
|
682
|
+
return;
|
683
|
+
}
|
684
|
+
$phrase = this.$message.find("[data-action='" + name + "'] .messenger-phrase");
|
685
|
+
remaining = (_ref1 = action.delay) != null ? _ref1 : 3;
|
686
|
+
if (remaining <= 10) {
|
687
|
+
this.$message.removeClass('messenger-retry-later');
|
688
|
+
this.$message.addClass('messenger-retry-soon');
|
689
|
+
} else {
|
690
|
+
this.$message.removeClass('messenger-retry-soon');
|
691
|
+
this.$message.addClass('messenger-retry-later');
|
692
|
+
}
|
693
|
+
tick = function() {
|
694
|
+
var delta;
|
695
|
+
$phrase.text(_this.renderPhrase(action, remaining));
|
696
|
+
if (remaining > 0) {
|
697
|
+
delta = Math.min(remaining, 1);
|
698
|
+
remaining -= delta;
|
699
|
+
return _this._timers[name] = setTimeout(tick, delta * 1000);
|
700
|
+
} else {
|
701
|
+
_this.$message.removeClass('messenger-retry-soon messenger-retry-later');
|
702
|
+
delete _this._timers[name];
|
703
|
+
return action.action();
|
704
|
+
}
|
705
|
+
};
|
706
|
+
return tick();
|
707
|
+
};
|
708
|
+
|
709
|
+
return RetryingMessage;
|
710
|
+
|
711
|
+
})(_Message);
|
712
|
+
|
713
|
+
_Messenger = (function(_super) {
|
714
|
+
|
715
|
+
__extends(_Messenger, _super);
|
716
|
+
|
717
|
+
function _Messenger() {
|
718
|
+
return _Messenger.__super__.constructor.apply(this, arguments);
|
719
|
+
}
|
720
|
+
|
721
|
+
_Messenger.prototype.tagName = 'ul';
|
722
|
+
|
723
|
+
_Messenger.prototype.className = 'messenger';
|
724
|
+
|
725
|
+
_Messenger.prototype.messageDefaults = {
|
726
|
+
type: 'info'
|
727
|
+
};
|
728
|
+
|
729
|
+
_Messenger.prototype.initialize = function(options) {
|
730
|
+
this.options = options != null ? options : {};
|
731
|
+
this.history = [];
|
732
|
+
return this.messageDefaults = $.extend({}, this.messageDefaults, this.options.messageDefaults);
|
733
|
+
};
|
734
|
+
|
735
|
+
_Messenger.prototype.render = function() {
|
736
|
+
return this.updateMessageSlotClasses();
|
737
|
+
};
|
738
|
+
|
739
|
+
_Messenger.prototype.findById = function(id) {
|
740
|
+
return _.filter(this.history, function(rec) {
|
741
|
+
return rec.msg.options.id === id;
|
742
|
+
});
|
743
|
+
};
|
744
|
+
|
745
|
+
_Messenger.prototype._reserveMessageSlot = function(msg) {
|
746
|
+
var $slot, dmsg,
|
747
|
+
_this = this;
|
748
|
+
$slot = $('<li>');
|
749
|
+
$slot.addClass('messenger-message-slot');
|
750
|
+
this.$el.prepend($slot);
|
751
|
+
this.history.push({
|
752
|
+
msg: msg,
|
753
|
+
$slot: $slot
|
754
|
+
});
|
755
|
+
this._enforceIdConstraint(msg);
|
756
|
+
msg.on('update', function() {
|
757
|
+
return _this._enforceIdConstraint(msg);
|
758
|
+
});
|
759
|
+
while (this.options.maxMessages && this.history.length > this.options.maxMessages) {
|
760
|
+
dmsg = this.history.shift();
|
761
|
+
dmsg.msg.remove();
|
762
|
+
dmsg.$slot.remove();
|
763
|
+
}
|
764
|
+
return $slot;
|
765
|
+
};
|
766
|
+
|
767
|
+
_Messenger.prototype._enforceIdConstraint = function(msg) {
|
768
|
+
var entry, _i, _len, _msg, _ref1;
|
769
|
+
if (msg.options.id == null) {
|
770
|
+
return;
|
771
|
+
}
|
772
|
+
_ref1 = this.history;
|
773
|
+
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
774
|
+
entry = _ref1[_i];
|
775
|
+
_msg = entry.msg;
|
776
|
+
if ((_msg.options.id != null) && _msg.options.id === msg.options.id && msg !== _msg) {
|
777
|
+
if (msg.options.singleton) {
|
778
|
+
msg.hide();
|
779
|
+
return;
|
780
|
+
} else {
|
781
|
+
_msg.hide();
|
782
|
+
}
|
783
|
+
}
|
784
|
+
}
|
785
|
+
};
|
786
|
+
|
787
|
+
_Messenger.prototype.newMessage = function(opts) {
|
788
|
+
var msg, _ref1, _ref2, _ref3,
|
789
|
+
_this = this;
|
790
|
+
if (opts == null) {
|
791
|
+
opts = {};
|
792
|
+
}
|
793
|
+
opts.messenger = this;
|
794
|
+
_Message = (_ref1 = (_ref2 = Messenger.themes[(_ref3 = opts.theme) != null ? _ref3 : this.options.theme]) != null ? _ref2.Message : void 0) != null ? _ref1 : RetryingMessage;
|
795
|
+
msg = new _Message(opts);
|
796
|
+
msg.on('show', function() {
|
797
|
+
if (opts.scrollTo && _this.$el.css('position') !== 'fixed') {
|
798
|
+
return msg.scrollTo();
|
799
|
+
}
|
800
|
+
});
|
801
|
+
msg.on('hide show render', this.updateMessageSlotClasses, this);
|
802
|
+
return msg;
|
803
|
+
};
|
804
|
+
|
805
|
+
_Messenger.prototype.updateMessageSlotClasses = function() {
|
806
|
+
var anyShown, last, rec, willBeFirst, _i, _len, _ref1;
|
807
|
+
willBeFirst = true;
|
808
|
+
last = null;
|
809
|
+
anyShown = false;
|
810
|
+
_ref1 = this.history;
|
811
|
+
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
812
|
+
rec = _ref1[_i];
|
813
|
+
rec.$slot.removeClass('first last shown');
|
814
|
+
if (rec.msg.shown && rec.msg.rendered) {
|
815
|
+
rec.$slot.addClass('shown');
|
816
|
+
anyShown = true;
|
817
|
+
last = rec;
|
818
|
+
if (willBeFirst) {
|
819
|
+
willBeFirst = false;
|
820
|
+
rec.$slot.addClass('first');
|
821
|
+
}
|
822
|
+
}
|
823
|
+
}
|
824
|
+
if (last != null) {
|
825
|
+
last.$slot.addClass('last');
|
826
|
+
}
|
827
|
+
return this.$el["" + (anyShown ? 'remove' : 'add') + "Class"]('messenger-empty');
|
828
|
+
};
|
829
|
+
|
830
|
+
_Messenger.prototype.hideAll = function() {
|
831
|
+
var rec, _i, _len, _ref1, _results;
|
832
|
+
_ref1 = this.history;
|
833
|
+
_results = [];
|
834
|
+
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
835
|
+
rec = _ref1[_i];
|
836
|
+
_results.push(rec.msg.hide());
|
837
|
+
}
|
838
|
+
return _results;
|
839
|
+
};
|
840
|
+
|
841
|
+
_Messenger.prototype.post = function(opts) {
|
842
|
+
var msg;
|
843
|
+
if (_.isString(opts)) {
|
844
|
+
opts = {
|
845
|
+
message: opts
|
846
|
+
};
|
847
|
+
}
|
848
|
+
opts = $.extend(true, {}, this.messageDefaults, opts);
|
849
|
+
msg = this.newMessage(opts);
|
850
|
+
msg.update(opts);
|
851
|
+
return msg;
|
852
|
+
};
|
853
|
+
|
854
|
+
return _Messenger;
|
855
|
+
|
856
|
+
})(BaseView);
|
857
|
+
|
858
|
+
ActionMessenger = (function(_super) {
|
859
|
+
|
860
|
+
__extends(ActionMessenger, _super);
|
861
|
+
|
862
|
+
function ActionMessenger() {
|
863
|
+
return ActionMessenger.__super__.constructor.apply(this, arguments);
|
864
|
+
}
|
865
|
+
|
866
|
+
ActionMessenger.prototype.doDefaults = {
|
867
|
+
progressMessage: null,
|
868
|
+
successMessage: null,
|
869
|
+
errorMessage: "Error connecting to the server.",
|
870
|
+
showSuccessWithoutError: true,
|
871
|
+
retry: {
|
872
|
+
auto: true,
|
873
|
+
allow: true
|
874
|
+
},
|
875
|
+
action: $.ajax
|
876
|
+
};
|
877
|
+
|
878
|
+
ActionMessenger.prototype.hookBackboneAjax = function(msgr_opts) {
|
879
|
+
var _ajax,
|
880
|
+
_this = this;
|
881
|
+
if (msgr_opts == null) {
|
882
|
+
msgr_opts = {};
|
883
|
+
}
|
884
|
+
if (!(window.Backbone != null)) {
|
885
|
+
throw 'Expected Backbone to be defined';
|
886
|
+
}
|
887
|
+
msgr_opts = _.defaults(msgr_opts, {
|
888
|
+
id: 'BACKBONE_ACTION',
|
889
|
+
errorMessage: false,
|
890
|
+
successMessage: "Request completed successfully.",
|
891
|
+
showSuccessWithoutError: false
|
892
|
+
});
|
893
|
+
_ajax = function(options) {
|
894
|
+
var sync_msgr_opts;
|
895
|
+
sync_msgr_opts = _.extend({}, msgr_opts, options.messenger);
|
896
|
+
return _this["do"](sync_msgr_opts, options);
|
897
|
+
};
|
898
|
+
if (Backbone.ajax != null) {
|
899
|
+
if (Backbone.ajax._withoutMessenger) {
|
900
|
+
Backbone.ajax = Backbone.ajax._withoutMessenger;
|
901
|
+
}
|
902
|
+
if (!(msgr_opts.action != null) || msgr_opts.action === this.doDefaults.action) {
|
903
|
+
msgr_opts.action = Backbone.ajax;
|
904
|
+
}
|
905
|
+
_ajax._withoutMessenger = Backbone.ajax;
|
906
|
+
return Backbone.ajax = _ajax;
|
907
|
+
} else {
|
908
|
+
return Backbone.sync = _.wrap(Backbone.sync, function() {
|
909
|
+
var args, _old_ajax, _old_sync;
|
910
|
+
_old_sync = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
|
911
|
+
_old_ajax = $.ajax;
|
912
|
+
$.ajax = _ajax;
|
913
|
+
_old_sync.call.apply(_old_sync, [this].concat(__slice.call(args)));
|
914
|
+
return $.ajax = _old_ajax;
|
915
|
+
});
|
916
|
+
}
|
917
|
+
};
|
918
|
+
|
919
|
+
ActionMessenger.prototype._getMessage = function(returnVal, def) {
|
920
|
+
if (returnVal === false) {
|
921
|
+
return false;
|
922
|
+
}
|
923
|
+
if (returnVal === true || !(returnVal != null) || typeof returnVal !== 'string') {
|
924
|
+
return def;
|
925
|
+
}
|
926
|
+
return returnVal;
|
927
|
+
};
|
928
|
+
|
929
|
+
ActionMessenger.prototype._parseEvents = function(events) {
|
930
|
+
var desc, firstSpace, func, label, out, type, _ref1;
|
931
|
+
if (events == null) {
|
932
|
+
events = {};
|
933
|
+
}
|
934
|
+
out = {};
|
935
|
+
for (label in events) {
|
936
|
+
func = events[label];
|
937
|
+
firstSpace = label.indexOf(' ');
|
938
|
+
type = label.substring(0, firstSpace);
|
939
|
+
desc = label.substring(firstSpace + 1);
|
940
|
+
if ((_ref1 = out[type]) == null) {
|
941
|
+
out[type] = {};
|
942
|
+
}
|
943
|
+
out[type][desc] = func;
|
944
|
+
}
|
945
|
+
return out;
|
946
|
+
};
|
947
|
+
|
948
|
+
ActionMessenger.prototype._normalizeResponse = function() {
|
949
|
+
var data, elem, resp, type, xhr, _i, _len;
|
950
|
+
resp = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
951
|
+
type = null;
|
952
|
+
xhr = null;
|
953
|
+
data = null;
|
954
|
+
for (_i = 0, _len = resp.length; _i < _len; _i++) {
|
955
|
+
elem = resp[_i];
|
956
|
+
if (elem === 'success' || elem === 'timeout' || elem === 'abort') {
|
957
|
+
type = elem;
|
958
|
+
} else if (((elem != null ? elem.readyState : void 0) != null) && ((elem != null ? elem.responseText : void 0) != null)) {
|
959
|
+
xhr = elem;
|
960
|
+
} else if (_.isObject(elem)) {
|
961
|
+
data = elem;
|
962
|
+
}
|
963
|
+
}
|
964
|
+
return [type, data, xhr];
|
965
|
+
};
|
966
|
+
|
967
|
+
ActionMessenger.prototype.run = function() {
|
968
|
+
var args, attr, events, m_opts, msg, opts, promiseAttrs, _i, _len, _ref1, _ref2,
|
969
|
+
_this = this;
|
970
|
+
m_opts = arguments[0], opts = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
|
971
|
+
if (opts == null) {
|
972
|
+
opts = {};
|
973
|
+
}
|
974
|
+
m_opts = $.extend(true, {}, this.messageDefaults, this.doDefaults, m_opts != null ? m_opts : {});
|
975
|
+
events = this._parseEvents(m_opts.events);
|
976
|
+
msg = (_ref1 = m_opts.messageInstance) != null ? _ref1 : this.newMessage(m_opts);
|
977
|
+
if (m_opts.id != null) {
|
978
|
+
msg.options.id = m_opts.id;
|
979
|
+
}
|
980
|
+
if (m_opts.progressMessage != null) {
|
981
|
+
msg.update($.extend({}, m_opts, {
|
982
|
+
message: m_opts.progressMessage,
|
983
|
+
type: 'info'
|
984
|
+
}));
|
985
|
+
}
|
986
|
+
_.each(['error', 'success'], function(type) {
|
987
|
+
var old, _ref2, _ref3;
|
988
|
+
if ((_ref2 = opts[type]) != null ? _ref2._originalHandler : void 0) {
|
989
|
+
opts[type] = opts[type]._originalHandler;
|
990
|
+
}
|
991
|
+
old = (_ref3 = opts[type]) != null ? _ref3 : function() {};
|
992
|
+
opts[type] = function() {
|
993
|
+
var data, msgOpts, msgText, r, reason, resp, xhr, _ref10, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
|
994
|
+
resp = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
995
|
+
_ref4 = _this._normalizeResponse.apply(_this, resp), reason = _ref4[0], data = _ref4[1], xhr = _ref4[2];
|
996
|
+
if (type === 'success' && !(msg.errorCount != null) && m_opts.showSuccessWithoutError === false) {
|
997
|
+
m_opts['successMessage'] = null;
|
998
|
+
}
|
999
|
+
if (type === 'error') {
|
1000
|
+
if ((_ref5 = m_opts.errorCount) == null) {
|
1001
|
+
m_opts.errorCount = 0;
|
1002
|
+
}
|
1003
|
+
m_opts.errorCount += 1;
|
1004
|
+
}
|
1005
|
+
msgText = _this._getMessage(r = old.apply(null, resp), m_opts[type + 'Message']);
|
1006
|
+
if (type === 'error' && ((xhr != null ? xhr.status : void 0) === 0 || reason === 'abort')) {
|
1007
|
+
msg.hide();
|
1008
|
+
return;
|
1009
|
+
}
|
1010
|
+
if (type === 'error' && ((m_opts.ignoredErrorCodes != null) && (_ref6 = xhr != null ? xhr.status : void 0, __indexOf.call(m_opts.ignoredErrorCodes, _ref6) >= 0))) {
|
1011
|
+
msg.hide();
|
1012
|
+
return;
|
1013
|
+
}
|
1014
|
+
msgOpts = $.extend({}, m_opts, {
|
1015
|
+
message: msgText,
|
1016
|
+
type: type,
|
1017
|
+
events: (_ref7 = events[type]) != null ? _ref7 : {},
|
1018
|
+
hideOnNavigate: type === 'success'
|
1019
|
+
});
|
1020
|
+
if (typeof ((_ref8 = msgOpts.retry) != null ? _ref8.allow : void 0) === 'number') {
|
1021
|
+
msgOpts.retry.allow--;
|
1022
|
+
}
|
1023
|
+
if (type === 'error' && (xhr != null ? xhr.status : void 0) >= 500 && ((_ref9 = msgOpts.retry) != null ? _ref9.allow : void 0)) {
|
1024
|
+
if (msgOpts.retry.delay == null) {
|
1025
|
+
if (msgOpts.errorCount < 4) {
|
1026
|
+
msgOpts.retry.delay = 10;
|
1027
|
+
} else {
|
1028
|
+
msgOpts.retry.delay = 5 * 60;
|
1029
|
+
}
|
1030
|
+
}
|
1031
|
+
if (msgOpts.hideAfter) {
|
1032
|
+
if ((_ref10 = msgOpts._hideAfter) == null) {
|
1033
|
+
msgOpts._hideAfter = msgOpts.hideAfter;
|
1034
|
+
}
|
1035
|
+
msgOpts.hideAfter = msgOpts._hideAfter + msgOpts.retry.delay;
|
1036
|
+
}
|
1037
|
+
msgOpts._retryActions = true;
|
1038
|
+
msgOpts.actions = {
|
1039
|
+
retry: {
|
1040
|
+
label: 'retry now',
|
1041
|
+
phrase: 'Retrying TIME',
|
1042
|
+
auto: msgOpts.retry.auto,
|
1043
|
+
delay: msgOpts.retry.delay,
|
1044
|
+
action: function() {
|
1045
|
+
msgOpts.messageInstance = msg;
|
1046
|
+
return setTimeout(function() {
|
1047
|
+
return _this["do"].apply(_this, [msgOpts, opts].concat(__slice.call(args)));
|
1048
|
+
}, 0);
|
1049
|
+
}
|
1050
|
+
},
|
1051
|
+
cancel: {
|
1052
|
+
action: function() {
|
1053
|
+
return msg.cancel();
|
1054
|
+
}
|
1055
|
+
}
|
1056
|
+
};
|
1057
|
+
} else if (msgOpts._retryActions) {
|
1058
|
+
delete msgOpts.actions.retry;
|
1059
|
+
delete msgOpts.actions.cancel;
|
1060
|
+
delete m_opts._retryActions;
|
1061
|
+
}
|
1062
|
+
msg.update(msgOpts);
|
1063
|
+
if (msgText) {
|
1064
|
+
$.globalMessenger();
|
1065
|
+
return msg.show();
|
1066
|
+
} else {
|
1067
|
+
return msg.hide();
|
1068
|
+
}
|
1069
|
+
};
|
1070
|
+
return opts[type]._originalHandler = old;
|
1071
|
+
});
|
1072
|
+
msg._actionInstance = m_opts.action.apply(m_opts, [opts].concat(__slice.call(args)));
|
1073
|
+
promiseAttrs = ['done', 'progress', 'fail', 'state', 'then'];
|
1074
|
+
for (_i = 0, _len = promiseAttrs.length; _i < _len; _i++) {
|
1075
|
+
attr = promiseAttrs[_i];
|
1076
|
+
if (msg[attr] != null) {
|
1077
|
+
delete msg[attr];
|
1078
|
+
}
|
1079
|
+
msg[attr] = (_ref2 = msg._actionInstance) != null ? _ref2[attr] : void 0;
|
1080
|
+
}
|
1081
|
+
return msg;
|
1082
|
+
};
|
1083
|
+
|
1084
|
+
ActionMessenger.prototype["do"] = ActionMessenger.prototype.run;
|
1085
|
+
|
1086
|
+
ActionMessenger.prototype.ajax = function() {
|
1087
|
+
var args, m_opts;
|
1088
|
+
m_opts = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
|
1089
|
+
m_opts.action = $.ajax;
|
1090
|
+
return this.run.apply(this, [m_opts].concat(__slice.call(args)));
|
1091
|
+
};
|
1092
|
+
|
1093
|
+
return ActionMessenger;
|
1094
|
+
|
1095
|
+
})(_Messenger);
|
1096
|
+
|
1097
|
+
$.fn.messenger = function() {
|
1098
|
+
var $el, args, func, instance, opts, _ref1, _ref2, _ref3;
|
1099
|
+
func = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
|
1100
|
+
if (func == null) {
|
1101
|
+
func = {};
|
1102
|
+
}
|
1103
|
+
$el = this;
|
1104
|
+
if (!(func != null) || !_.isString(func)) {
|
1105
|
+
opts = func;
|
1106
|
+
if (!($el.data('messenger') != null)) {
|
1107
|
+
_Messenger = (_ref1 = (_ref2 = Messenger.themes[opts.theme]) != null ? _ref2.Messenger : void 0) != null ? _ref1 : ActionMessenger;
|
1108
|
+
$el.data('messenger', instance = new _Messenger($.extend({
|
1109
|
+
el: $el
|
1110
|
+
}, opts)));
|
1111
|
+
instance.render();
|
1112
|
+
}
|
1113
|
+
return $el.data('messenger');
|
1114
|
+
} else {
|
1115
|
+
return (_ref3 = $el.data('messenger'))[func].apply(_ref3, args);
|
1116
|
+
}
|
1117
|
+
};
|
1118
|
+
|
1119
|
+
window.Messenger._call = function(opts) {
|
1120
|
+
var $el, $parent, choosen_loc, chosen_loc, classes, defaultOpts, inst, loc, locations, _i, _len;
|
1121
|
+
defaultOpts = {
|
1122
|
+
extraClasses: 'messenger-fixed messenger-on-bottom messenger-on-right',
|
1123
|
+
theme: 'future',
|
1124
|
+
maxMessages: 9,
|
1125
|
+
parentLocations: ['body']
|
1126
|
+
};
|
1127
|
+
opts = $.extend(defaultOpts, $._messengerDefaults, Messenger.options, opts);
|
1128
|
+
if (opts.theme != null) {
|
1129
|
+
opts.extraClasses += " messenger-theme-" + opts.theme;
|
1130
|
+
}
|
1131
|
+
inst = opts.instance || Messenger.instance;
|
1132
|
+
if (opts.instance == null) {
|
1133
|
+
locations = opts.parentLocations;
|
1134
|
+
$parent = null;
|
1135
|
+
choosen_loc = null;
|
1136
|
+
for (_i = 0, _len = locations.length; _i < _len; _i++) {
|
1137
|
+
loc = locations[_i];
|
1138
|
+
$parent = $(loc);
|
1139
|
+
if ($parent.length) {
|
1140
|
+
chosen_loc = loc;
|
1141
|
+
break;
|
1142
|
+
}
|
1143
|
+
}
|
1144
|
+
if (!inst) {
|
1145
|
+
$el = $('<ul>');
|
1146
|
+
$parent.prepend($el);
|
1147
|
+
inst = $el.messenger(opts);
|
1148
|
+
inst._location = chosen_loc;
|
1149
|
+
Messenger.instance = inst;
|
1150
|
+
} else if ($(inst._location) !== $(chosen_loc)) {
|
1151
|
+
inst.$el.detach();
|
1152
|
+
$parent.prepend(inst.$el);
|
1153
|
+
}
|
1154
|
+
}
|
1155
|
+
if (inst._addedClasses != null) {
|
1156
|
+
inst.$el.removeClass(inst._addedClasses);
|
1157
|
+
}
|
1158
|
+
inst.$el.addClass(classes = "" + inst.className + " " + opts.extraClasses);
|
1159
|
+
inst._addedClasses = classes;
|
1160
|
+
return inst;
|
1161
|
+
};
|
1162
|
+
|
1163
|
+
$.extend(Messenger, {
|
1164
|
+
Message: RetryingMessage,
|
1165
|
+
Messenger: ActionMessenger,
|
1166
|
+
themes: (_ref1 = Messenger.themes) != null ? _ref1 : {}
|
1167
|
+
});
|
1168
|
+
|
1169
|
+
$.globalMessenger = window.Messenger = Messenger;
|
1170
|
+
|
1171
|
+
}).call(this);
|