!function(e){"function"==typeof define&&define.amd?define(["underscore","backbone","exports"],e):"object"==typeof exports?e(require("underscore"),require("backbone"),exports):e(_,Backbone,{})}(function(e,t,i){t.Stickit=i,i._handlers=[],i.addHandler=function(t){t=e.map(e.flatten([t]),function(t){return e.defaults({},t,{updateModel:!0,updateView:!0,updateMethod:"text"})}),this._handlers=this._handlers.concat(t)},i.ViewMixin={_modelBindings:null,unstickit:function(t,i){if(e.isObject(i))return void e.each(i,function(e,i){this.unstickit(t,i)},this);var n=[],a=[];this._modelBindings=e.reject(this._modelBindings,function(e){return t&&e.model!==t||i&&e.config.selector!=i?void 0:(e.model.off(e.event,e.fn),a.push(e.config._destroy),n.push(e.model),!0)}),e.invoke(e.uniq(n),"trigger","stickit:unstuck",this.cid),e.each(e.uniq(a),function(e){e.call(this)},this),this.$el.off(".stickit"+(t?"."+t.cid:""),i)},stickit:function(t,i){var n=t||this.model,a=i||e.result(this,"bindings")||{};this._modelBindings||(this._modelBindings=[]),this.addBinding(n,a);var o=this.remove;return o.stickitWrapped||(this.remove=function(){var e=this;return this.unstickit(),o&&(e=o.apply(this,arguments)),e}),this.remove.stickitWrapped=!0,this},addBinding:function(t,i,a){var v=t||this.model,g=".stickit."+v.cid;if(a=a||{},e.isObject(i)){var b=i;return void e.each(b,function(e,t){this.addBinding(v,t,e)},this)}var k=":el"===i?this.$el:this.$(i);if(this.unstickit(v,i),k.length){e.isString(a)&&(a={observe:a}),e.isFunction(a.observe)&&(a.observe=a.observe.call(this));var m=u(k,a),_=m.observe;m.selector=i,m.view=this;var y=m.bindId=e.uniqueId(),w=e.extend({stickitChange:m},m.setOptions);if(m._destroy=function(){o.call(this,m.destroy,k,v,m)},d(k,m,v,_),h(k,m,v,_),f(k,m,v,_),_){e.each(m.events,function(t){var a=t+g,l=function(e){var t=o.call(this,m.getVal,k,e,m,n.call(arguments,1)),i=r(m.updateModel,t,e,m);i&&c(v,_,t,w,m)},s=":el"===i?"":i;this.$el.on(a,s,e.bind(l,this))},this),e.each(e.flatten([_]),function(e){l(v,"change:"+e,m,function(e,t,i){var n=i&&i.stickitChange&&i.stickitChange.bindId;if(n!==y){var a=s(v,_,m);p(k,m,a,v)}})});var x=s(v,_,m);p(k,m,x,v,!0)}o.call(this,m.initialize,k,v,m)}}},e.extend(t.View.prototype,i.ViewMixin);var n=[].slice,a=function(t,i){var n=(i||"").split("."),a=e.reduce(n,function(e,t){return e[t]},t);return null==a?t:a},o=function(t){return t=e.isString(t)?a(this,t):t,t?t.apply(this,n.call(arguments,1)):void 0},r=function(t,i,n){if(e.isBoolean(t))return t;if(e.isFunction(t)||e.isString(t)){var a=e.last(arguments).view;return o.apply(a,arguments)}return!1},l=function(e,t,i,n){var a=i.view;e.on(t,n,a),a._modelBindings.push({model:e,event:t,fn:n,config:i})},c=function(t,i,n,a,r){var l={},c=r.view;r.onSet&&(n=o.call(c,r.onSet,n,r)),r.set?o.call(c,r.set,i,n,a,r):(l[i]=n,e.isArray(i)&&e.isArray(n)&&(l=e.reduce(i,function(t,i,a){return t[i]=e.has(n,a)?n[a]:null,t},{})),t.set(l,a))},s=function(t,i,n){var a=n.view,r=function(e){return t[n.escape?"escape":"get"](e)},l=function(e){return null==e?"":e},c=e.isArray(i)?e.map(i,r):r(i);return n.onGet&&(c=o.call(a,n.onGet,c,n)),e.isArray(c)?e.map(c,l):l(c)},u=i.getConfiguration=function(t,n){var a=[{updateModel:!1,updateMethod:"text",update:function(e,t,i,n){e[n.updateMethod]&&e[n.updateMethod](t)},getVal:function(e,t,i){return e[i.updateMethod]()}}];a=a.concat(e.filter(i._handlers,function(e){return t.is(e.selector)})),a.push(n);var o=e.extend.apply(e,a);return e.has(o,"updateView")||(o.updateView=!o.visible),o},d=function(t,i,n,a){var o=["autofocus","autoplay","async","checked","controls","defer","disabled","hidden","indeterminate","loop","multiple","open","readonly","required","scoped","selected"],r=i.view;e.each(i.attributes||[],function(c){c=e.clone(c),c.view=r;var u="",d=c.observe||(c.observe=a),f=function(){var i=e.contains(o,c.name)?"prop":"attr",a=s(n,d,c);"class"===c.name?(t.removeClass(u).addClass(a),u=a):t[i](c.name,a)};e.each(e.flatten([d]),function(e){l(n,"change:"+e,i,f)}),f()})},f=function(t,i,n,a){e.each(i.classes||[],function(a,o){e.isString(a)&&(a={observe:a}),a.view=i.view;var r=a.observe,c=function(){var e=s(n,r,a);t.toggleClass(o,!!e)};e.each(e.flatten([r]),function(e){l(n,"change:"+e,i,c)}),c()})},h=function(t,i,n,a){if(null!=i.visible){var r=i.view,c=function(){var l=i.visible,c=i.visibleFn,u=s(n,a,i),d=!!u;(e.isFunction(l)||e.isString(l))&&(d=!!o.call(r,l,u,i)),c?o.call(r,c,t,d,i):t.toggle(d)};e.each(e.flatten([a]),function(e){l(n,"change:"+e,i,c)}),c()}},p=function(e,t,i,n,a){var l=t.view;r(t.updateView,i,t)&&(o.call(l,t.update,e,i,n,t),a||o.call(l,t.afterUpdate,e,i,t))};return i.addHandler([{selector:"[contenteditable]",updateMethod:"html",events:["input","change"]},{selector:"input",events:["propertychange","input","change"],update:function(e,t){e.val(t)},getVal:function(e){return e.val()}},{selector:"textarea",events:["propertychange","input","change"],update:function(e,t){e.val(t)},getVal:function(e){return e.val()}},{selector:'input[type="radio"]',events:["change"],update:function(e,t){e.filter('[value="'+t+'"]').prop("checked",!0)},getVal:function(e){return e.filter(":checked").val()}},{selector:'input[type="checkbox"]',events:["change"],update:function(i,n,a,o){if(i.length>1)n||(n=[]),i.each(function(i,a){var o=t.$(a),r=e.contains(n,o.val());o.prop("checked",r)});else{var r=e.isBoolean(n)?n:n===i.val();i.prop("checked",r)}},getVal:function(i){var n;if(i.length>1)n=e.reduce(i,function(e,i){var n=t.$(i);return n.prop("checked")&&e.push(n.val()),e},[]);else{n=i.prop("checked");var a=i.val();"on"!==a&&null!=a&&(n=n?i.val():null)}return n}},{selector:"select",events:["change"],update:function(i,n,r,l){var c,u=l.selectOptions,d=u&&u.collection||void 0,f=i.prop("multiple");if(!u){u={};var h=function(e){return e.map(function(e,i){var n=t.$(i).data("stickit-bind-val");return{value:void 0!==n?n:i.value,label:i.text}}).get()};i.find("optgroup").length?(d={opt_labels:[]},i.find("> option").length&&(d.opt_labels.push(void 0),e.each(i.find("> option"),function(e){d[void 0]=h(t.$(e))})),e.each(i.find("optgroup"),function(e){var i=t.$(e).attr("label");d.opt_labels.push(i),d[i]=h(t.$(e).find("option"))})):d=h(i.find("option"))}u.valuePath=u.valuePath||"value",u.labelPath=u.labelPath||"label",u.disabledPath=u.disabledPath||"disabled";var p=function(i,n,o){e.each(i,function(i){var r,l,c,s=t.$(""),d=i,h=function(t,i,n){s.text(t),d=i,s.data("stickit-bind-val",d),e.isArray(d)||e.isObject(d)||s.val(d),n===!0&&s.prop("disabled","disabled")};"__default__"===i?(r=o.label,l=o.value,c=o.disabled):(r=a(i,u.labelPath),l=a(i,u.valuePath),c=a(i,u.disabledPath)),h(r,l,c);var p=function(){return f||null==d||null==o||d!==o?!(!e.isObject(o)||!e.isEqual(d,o)):!0};p()?s.prop("selected",!0):f&&e.isArray(o)&&e.each(o,function(t){e.isObject(t)&&(t=a(t,u.valuePath)),(t===d||e.isObject(t)&&e.isEqual(d,t))&&s.prop("selected",!0)}),n.append(s)})};if(i.find("*").remove(),e.isString(d)){var v=window;0===d.indexOf("this.")&&(v=this),d=d.replace(/^[a-z]*\.(.+)$/,"$1"),c=a(v,d)}else c=e.isFunction(d)?o.call(this,d,i,l):d;if(c instanceof t.Collection){var g=c,b=function(){var e=s(r,l.observe,l);o.call(this,l.update,i,e,r,l)},k=function(){g.off("add remove reset sort",b)},m=function(){k(),g.off("stickit:selectRefresh"),r.off("stickit:selectRefresh")};g.trigger("stickit:selectRefresh"),g.once("stickit:selectRefresh",k,this),g.on("add remove reset sort",b,this),r.trigger("stickit:selectRefresh"),r.once("stickit:selectRefresh",function(){r.off("stickit:unstuck",m)}),r.once("stickit:unstuck",m,this),c=c.toJSON()}if(u.defaultOption){var _=e.isFunction(u.defaultOption)?u.defaultOption.call(this,i,l):u.defaultOption;p(["__default__"],i,_)}if(e.isArray(c))p(c,i,n);else if(c.opt_labels)e.each(c.opt_labels,function(e){var a=t.$("").attr("label",e);p(c[e],a,n),i.append(a)});else{var y,w=[];for(var x in c)y={},y[u.valuePath]=x,y[u.labelPath]=c[x],w.push(y);w=e.sortBy(w,u.comparator||u.labelPath),p(w,i,n)}},getVal:function(i){var n=i.find("option:selected");return i.prop("multiple")?e.map(n,function(e){return t.$(e).data("stickit-bind-val")}):n.data("stickit-bind-val")}}]),i});
//# sourceMappingURL=backbone.stickit.min.js.map