diff --git a/CHANGELOG.md b/CHANGELOG.md index ca3fea1..b8961c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,34 +1,45 @@ +1.2.2 +----- + +- Message strings (as opposed to full objects) can now be passed into message.update. + +1.2.1 +----- + +- Added `ajax` alias for `run` with `$.ajax` as the action. This is essentially identical + to the default behavior, but is useful to those who wish to be more explicit. + 1.2.0 ----- -Added global Messenger object which can be removed with `Messenger.noConflict()`. The -Messenger object will provide a container other than the jQuery object for messenger -externals. +- Added global Messenger object which can be removed with `Messenger.noConflict()`. The + Messenger object will provide a container other than the jQuery object for messenger + externals. -Messenger may also be called to provide the same functionality as `$.globalMessenger`. +- Messenger may also be called to provide the same functionality as `$.globalMessenger`. -Messenger default options may now be set on `window.Messenger.options` as well as -`$._messengerDefaults`. +- Messenger default options may now be set on `window.Messenger.options` as well as + `$._messengerDefaults`. -The current instance of ActionMessenger which will be used by Messenger()/$.globalMessenger -calls is now accessable as `window.Messenger.instance`, not `$._messengerInstance`. +- The current instance of ActionMessenger which will be used by `Messenger()`/`$.globalMessenger` + calls is now accessable as `window.Messenger.instance`, not `$._messengerInstance`. -Added `run` alias for `go`. This change makes it easier for developers writing js. -In JavaScript, go, being a reserved word, always had to be referenced using array -notation, this provides a way around that limitation. +- Added `run` alias for `go`. This change makes it easier for developers writing js. + In JavaScript, go, being a reserved word, always had to be referenced using array + notation, this provides a way around that limitation. -Created common way for themes to define extra JavaScript. Themes can now define their -own Messenger and/or Message objects in the `window.Messenger.themes.` object. -If the theme name is provided in the options to globalMessenger as `options.theme`, the -defined classes will be used. As the theme now has to be passed in as a seperate option, the -`messenger-theme-x` class will now be automatically added, and does not need to be -provided in extraClasses. +- Created common way for themes to define extra JavaScript. Themes can now define their + own Messenger and/or Message objects in the `window.Messenger.themes.` object. + If the theme name is provided in the options to globalMessenger as `options.theme`, the + defined classes will be used. As the theme now has to be passed in as a seperate option, the + `messenger-theme-x` class will now be automatically added, and does not need to be + provided in extraClasses. -MagicMessage has been renamed RetryingMessage. +- `MagicMessage` has been renamed `RetryingMessage`. -The base classes Message and Messenger have been renamed _Message and _Messenger to -signify that they are only for the internal structuring of the code, and not expected -to be used directly. +- The base classes `Message` and `Messenger` have been renamed `_Message` and `_Messenger` to + signify that they are only for the internal structuring of the code, and not expected + to be used directly. -Messenger now exposes ActionMessenger (as Messenger) and RetryingMessage (as Message) for -use by theme which wish to extend them. +- Messenger now exposes `ActionMessenger` (as `Messenger`) and `RetryingMessage` (as `Message`) for + use by theme which wish to extend them. diff --git a/build/js/messenger.js b/build/js/messenger.js index dd1ea79..1a03930 100644 --- a/build/js/messenger.js +++ b/build/js/messenger.js @@ -1,4 +1,4 @@ -/*! messenger 1.2.1 2013-03-12 */ +/*! messenger 1.2.2 2013-03-12 */ (function() { var $, ActionMessenger, RetryingMessage, _Message, _Messenger, _prevMessenger, __hasProp = {}.hasOwnProperty, @@ -64,6 +64,11 @@ _Message.prototype.update = function(opts) { var _ref, _this = this; + if (_.isString(opts)) { + opts = { + message: opts + }; + } $.extend(this.options, opts); this.lastUpdate = new Date(); this.rendered = false; @@ -712,6 +717,13 @@ ActionMessenger.prototype["do"] = ActionMessenger.prototype.run; + ActionMessenger.prototype.ajax = function() { + var args, m_opts; + 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))); + }; + return ActionMessenger; })(_Messenger); diff --git a/build/js/messenger.min.js b/build/js/messenger.min.js index 4932b37..05153dd 100644 --- a/build/js/messenger.min.js +++ b/build/js/messenger.min.js @@ -1,2 +1,2 @@ -/*! messenger 1.2.1 2013-03-12 */ -(function(){var e,t,s,n,r,o,i={}.hasOwnProperty,a=function(e,t){function s(){this.constructor=e}for(var n in t)i.call(t,n)&&(e[n]=t[n]);return s.prototype=t.prototype,e.prototype=new s,e.__super__=t.prototype,e},l=[].slice,u=[].indexOf||function(e){for(var t=0,s=this.length;s>t;t++)if(t in this&&this[t]===e)return t;return-1};e=jQuery,n=function(t){function s(){return s.__super__.constructor.apply(this,arguments)}return a(s,t),s.prototype.defaults={hideAfter:10,scroll:!0},s.prototype.initialize=function(t){return null==t&&(t={}),this.shown=!1,this.rendered=!1,this.messenger=t.messenger,this.options=e.extend({},this.options,t,this.defaults)},s.prototype.show=function(){var e;return this.rendered||this.render(),this.$message.removeClass("messenger-hidden"),e=this.shown,this.shown=!0,e?void 0:this.trigger("show")},s.prototype.hide=function(){var e;if(this.rendered)return this.$message.addClass("messenger-hidden"),e=this.shown,this.shown=!1,e?this.trigger("hide"):void 0},s.prototype.cancel=function(){return this.hide()},s.prototype.update=function(t){var s,n=this;return e.extend(this.options,t),this.lastUpdate=new Date,this.rendered=!1,this.events=null!=(s=this.options.events)?s:{},this.render(),this.actionsToEvents(),this.delegateEvents(),this.checkClickable(),this.options.hideAfter?(this.$message.addClass("messenger-will-hide-after"),null!=this._hideTimeout&&clearTimeout(this._hideTimeout),this._hideTimeout=setTimeout(function(){return n.hide()},1e3*this.options.hideAfter)):this.$message.removeClass("messenger-will-hide-after"),this.options.hideOnNavigate?(this.$message.addClass("messenger-will-hide-on-navigate"),null!=Backbone.history&&Backbone.history.on("route",function(){return n.hide()})):this.$message.removeClass("messenger-will-hide-on-navigate"),this.trigger("update",this),this.trigger("update",this)},s.prototype.scrollTo=function(){return this.options.scroll?e.scrollTo(this.$el,{duration:400,offset:{left:0,top:-20}}):void 0},s.prototype.timeSinceUpdate=function(){return this.lastUpdate?new Date-this.lastUpdate:null},s.prototype.actionsToEvents=function(){var e,t,s,n,r=this;s=this.options.actions,n=[];for(t in s)e=s[t],n.push(this.events['click [data-action="'+t+'"] a']=function(e){return function(s){return s.preventDefault(),s.stopPropagation(),r.trigger("action:"+t,e,s),e.action(s)}}(e));return n},s.prototype.checkClickable=function(){var e,t,s,n;s=this.events,n=[];for(t in s)e=s[t],"click"===t?n.push(this.$message.addClass("messenger-clickable")):n.push(void 0);return n},s.prototype.undelegateEvents=function(){var e;return s.__super__.undelegateEvents.apply(this,arguments),null!=(e=this.$message)?e.removeClass("messenger-clickable"):void 0},s.prototype.parseActions=function(){var t,s,n,r,o,i;s=[],o=this.options.actions;for(r in o)t=o[r],n=e.extend({},t),n.name=r,null==(i=n.label)&&(n.label=r),s.push(n);return s},s.prototype.template=function(t){var s,n,r,o,i,a,l,u,h,c,d=this;for(i=e("
"),t.showCloseButton&&(r=e(''),r.click(function(){return d.cancel(),!0}),i.append(r)),a=e('
'+t.message+"
"),i.append(a),t.actions.length&&(n=e('
')),c=t.actions,u=0,h=c.length;h>u;u++)l=c[u],s=e(""),s.attr("data-action",""+l.name),o=e(""),o.html(l.label),s.append(e('')),s.append(o),n.append(s);return i.append(n),i},s.prototype.render=function(){var t;if(!this.rendered)return this._hasSlot||(this.setElement(this.messenger._reserveMessageSlot(this)),this._hasSlot=!0),t=e.extend({},this.options,{actions:this.parseActions()}),this.$message=e(this.template(t)),this.$el.html(this.$message),this.shown=!0,this.rendered=!0,this.trigger("render")},s}(Backbone.View),s=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return a(t,e),t.prototype.initialize=function(){return t.__super__.initialize.apply(this,arguments),this._timers={}},t.prototype.cancel=function(){return this.clearTimers(),this.hide(),null!=this._actionInstance&&null!=this._actionInstance.abort?this._actionInstance.abort():void 0},t.prototype.clearTimers=function(){var e,t,s,n;s=this._timers;for(e in s)t=s[e],clearTimeout(t);return this._timers={},null!=(n=this.$message)?n.removeClass("messenger-retry-soon messenger-retry-later"):void 0},t.prototype.render=function(){var e,s,n,r;t.__super__.render.apply(this,arguments),this.clearTimers(),n=this.options.actions,r=[];for(s in n)e=n[s],e.auto?r.push(this.startCountdown(s,e)):r.push(void 0);return r},t.prototype.renderPhrase=function(e,t){var s;return s=e.phrase.replace("TIME",this.formatTime(t))},t.prototype.formatTime=function(e){var t;return t=function(e,t){return e=Math.floor(e),1!==e&&(t+="s"),"in "+e+" "+t},0===Math.floor(e)?"now...":60>e?t(e,"second"):(e/=60,60>e?t(e,"minute"):(e/=60,t(e,"hour")))},t.prototype.startCountdown=function(e,t){var s,n,r,o,i=this;if(null==this._timers[e])return s=this.$message.find("[data-action='"+e+"'] .messenger-phrase"),n=null!=(o=t.delay)?o:3,10>=n?(this.$message.removeClass("messenger-retry-later"),this.$message.addClass("messenger-retry-soon")):(this.$message.removeClass("messenger-retry-soon"),this.$message.addClass("messenger-retry-later")),r=function(){var o;return s.text(i.renderPhrase(t,n)),n>0?(o=Math.min(n,1),n-=o,i._timers[e]=setTimeout(r,1e3*o)):(i.$message.removeClass("messenger-retry-soon messenger-retry-later"),delete i._timers[e],t.action())},r()},t}(n),r=function(t){function r(){return r.__super__.constructor.apply(this,arguments)}return a(r,t),r.prototype.tagName="ul",r.prototype.className="messenger",r.prototype.messageDefaults={type:"info"},r.prototype.initialize=function(t){return this.options=null!=t?t:{},this.history=[],this.messageDefaults=e.extend({},this.messageDefaults,this.options.messageDefaults)},r.prototype.render=function(){return this.updateMessageSlotClasses()},r.prototype.findById=function(e){return _.filter(this.history,function(t){return t.msg.options.id===e})},r.prototype._reserveMessageSlot=function(t){var s,n,r=this;for(s=e("
  • "),s.addClass("messenger-message-slot"),this.$el.prepend(s),this.history.push({msg:t,$slot:s}),this._enforceIdConstraint(t),t.on("update",function(){return r._enforceIdConstraint(t)});this.options.maxMessages&&this.history.length>this.options.maxMessages;)n=this.history.shift(),n.msg.remove(),n.$slot.remove();return s},r.prototype._enforceIdConstraint=function(e){var t,s,n,r,o;if(null!=e.options.id)for(o=this.history,s=0,n=o.length;n>s;s++)if(t=o[s],r=t.msg,null!=r.options.id&&r.options.id===e.options.id&&e!==r){if(e.options.singleton)return e.hide(),void 0;r.hide()}},r.prototype.newMessage=function(e){var t,r,o,i,a=this;return null==e&&(e={}),e.messenger=this,n=null!=(r=null!=(o=window.Messenger.themes[null!=(i=e.theme)?i:this.options.theme])?o.Message:void 0)?r:s,t=new n(e),t.on("show",function(){return e.scrollTo&&"fixed"!==a.$el.css("position")?t.scrollTo():void 0}),t.on("hide show render",this.updateMessageSlotClasses,this),t},r.prototype.updateMessageSlotClasses=function(){var e,t,s,n,r,o,i;for(n=!0,t=null,e=!1,i=this.history,r=0,o=i.length;o>r;r++)s=i[r],s.$slot.removeClass("first last shown"),s.msg.shown&&s.msg.rendered&&(s.$slot.addClass("shown"),e=!0,t=s,n&&(n=!1,s.$slot.addClass("first")));return null!=t&&t.$slot.addClass("last"),this.$el[""+(e?"remove":"add")+"Class"]("messenger-empty")},r.prototype.hideAll=function(){var e,t,s,n,r;for(n=this.history,r=[],t=0,s=n.length;s>t;t++)e=n[t],r.push(e.msg.hide());return r},r.prototype.post=function(t){var s;return _.isString(t)&&(t={message:t}),t=e.extend(!0,{},this.messageDefaults,t),s=this.newMessage(t),s.update(t),s},r}(Backbone.View),t=function(t){function s(){return s.__super__.constructor.apply(this,arguments)}return a(s,t),s.prototype.doDefaults={progressMessage:null,successMessage:null,errorMessage:"Error connecting to the server.",showSuccessWithoutError:!0,retry:{auto:!0,allow:!0},action:e.ajax},s.prototype.hookBackboneAjax=function(t){var s,n=this;return null==t&&(t={}),t=_.defaults(t,{id:"BACKBONE_ACTION",errorMessage:!1,successMessage:"Request completed successfully.",showSuccessWithoutError:!1}),s=function(e){var s;return s=_.extend({},t,e.messenger),n["do"](s,e)},null!=Backbone.ajax?(Backbone.ajax._withoutMessenger&&(Backbone.ajax=Backbone.ajax._withoutMessenger),(null==t.action||t.action===this.doDefaults.action)&&(t.action=Backbone.ajax),s._withoutMessenger=Backbone.ajax,Backbone.ajax=s):Backbone.sync=_.wrap(Backbone.sync,function(){var t,n,r;return r=arguments[0],t=arguments.length>=2?l.call(arguments,1):[],n=e.ajax,e.ajax=s,r.call.apply(r,[this].concat(l.call(t))),e.ajax=n})},s.prototype._getMessage=function(e,t){return e===!1?!1:e===!0||null==e||"string"!=typeof e?t:e},s.prototype._parseEvents=function(e){var t,s,n,r,o,i,a;null==e&&(e={}),o={};for(r in e)n=e[r],s=r.indexOf(" "),i=r.substring(0,s),t=r.substring(s+1),null==(a=o[i])&&(o[i]={}),o[i][t]=n;return o},s.prototype._normalizeResponse=function(){var e,t,s,n,r,o,i;for(s=arguments.length>=1?l.call(arguments,0):[],n=null,r=null,e=null,o=0,i=s.length;i>o;o++)t=s[o],"success"===t||"timeout"===t||"abort"===t?n=t:null!=(null!=t?t.readyState:void 0)&&null!=(null!=t?t.responseText:void 0)?r=t:_.isObject(t)&&(e=t);return[n,e,r]},s.prototype.run=function(){var t,s,n,r,o,i,a,h,c,d,p,g=this;for(r=arguments[0],i=arguments[1],t=arguments.length>=3?l.call(arguments,2):[],null==i&&(i={}),r=e.extend(!0,{},this.messageDefaults,this.doDefaults,null!=r?r:{}),n=this._parseEvents(r.events),o=null!=(d=r.messageInstance)?d:this.newMessage(r),null!=r.id&&(o.options.id=r.id),null!=r.progressMessage&&o.update(e.extend({},r,{message:r.progressMessage,type:"info"})),_.each(["error","success"],function(s){var a,h,c;return(null!=(h=i[s])?h._originalHandler:void 0)&&(i[s]=i[s]._originalHandler),a=null!=(c=i[s])?c:function(){},i[s]=function(){var h,c,d,p,m,f,y,v,_,w,C,M,b,x;return f=arguments.length>=1?l.call(arguments,0):[],v=g._normalizeResponse.apply(g,f),m=v[0],h=v[1],y=v[2],"success"===s&&null==o.errorCount&&r.showSuccessWithoutError===!1&&(r.successMessage=null),"error"===s&&(null==(_=r.errorCount)&&(r.errorCount=0),r.errorCount+=1),d=g._getMessage(p=a.apply(null,f),r[s+"Message"]),"error"!==s||0!==(null!=y?y.status:void 0)&&"abort"!==m?"error"===s&&null!=r.ignoredErrorCodes&&(w=null!=y?y.status:void 0,u.call(r.ignoredErrorCodes,w)>=0)?(o.hide(),void 0):(c=e.extend({},r,{message:d,type:s,events:null!=(C=n[s])?C:{},hideOnNavigate:"success"===s}),"number"==typeof(null!=(M=c.retry)?M.allow:void 0)&&c.retry.allow--,"error"===s&&(null!=y?y.status:void 0)>=500&&(null!=(b=c.retry)?b.allow:void 0)?(null==c.retry.delay&&(c.retry.delay=4>c.errorCount?10:300),c.hideAfter&&(null==(x=c._hideAfter)&&(c._hideAfter=c.hideAfter),c.hideAfter=c._hideAfter+c.retry.delay),c._retryActions=!0,c.actions={retry:{label:"retry now",phrase:"Retrying TIME",auto:c.retry.auto,delay:c.retry.delay,action:function(){return c.messageInstance=o,setTimeout(function(){return g["do"].apply(g,[c,i].concat(l.call(t)))},0)}},cancel:{action:function(){return o.cancel()}}}):c._retryActions&&(delete c.actions.retry,delete c.actions.cancel,delete r._retryActions),o.update(c),d?(e.globalMessenger(),o.show()):o.hide()):(o.hide(),void 0)},i[s]._originalHandler=a}),o._actionInstance=r.action.apply(r,[i].concat(l.call(t))),a=["done","progress","fail","state","then"],h=0,c=a.length;c>h;h++)s=a[h],null!=o[s]&&delete o[s],o[s]=null!=(p=o._actionInstance)?p[s]:void 0;return o},s.prototype["do"]=s.prototype.run,s}(r),e.fn.messenger=function(){var s,n,o,i,a,u,h,c;return o=arguments[0],n=arguments.length>=2?l.call(arguments,1):[],null==o&&(o={}),s=this,null!=o&&_.isString(o)?(c=s.data("messenger"))[o].apply(c,n):(a=o,null==s.data("messenger")&&(r=null!=(u=null!=(h=window.Messenger.themes[a.theme])?h.Messenger:void 0)?u:t,s.data("messenger",i=new r(e.extend({el:s},a))),i.render()),s.data("messenger"))},o=window.Messenger,window.Messenger=function(t){var s,n,r,o,i,a,l,u,h,c,d;if(a={extraClasses:"messenger-fixed messenger-on-bottom messenger-on-right",theme:"future",maxMessages:9,parentLocations:["body"]},t=e.extend(a,e._messengerDefaults,window.Messenger.options,t),null!=t.theme&&(t.extraClasses+=" messenger-theme-"+t.theme),l=t.instance||window.Messenger.instance,null==t.instance){for(h=t.parentLocations,n=null,r=null,c=0,d=h.length;d>c;c++)if(u=h[c],n=e(u),n.length){o=u;break}l?e(l._location)!==e(o)&&(l.$el.detach(),n.prepend(l.$el)):(s=e("