backbone.cycle
Version:
Navigating and selecting Backbone Models and Collections.
8 lines (6 loc) • 6.41 kB
JavaScript
// Backbone.Cycle, v3.0.0
// Copyright (c) 2014-2016 Michael Heim, Zeilenwechsel.de
// Distributed under MIT license
// http://github.com/hashchange/backbone.cycle
!function(a,b){"use strict";var c="object"==typeof exports&&exports&&!exports.nodeType&&"object"==typeof module&&module&&!module.nodeType;"function"==typeof define&&"object"==typeof define.amd&&define.amd?define(["exports","underscore","backbone","backbone.select"],b):c?b(exports,require("underscore"),require("backbone"),require("backbone.select")):b({},_,Backbone)}(this,function(a,_,Backbone){"use strict";function b(a){var b,c=arguments.length&&!(arguments[0]instanceof Backbone.Model),d=a&&a.silent,e="_cycle_skipSelectInitial_";_.each(this._cycleOpts.autoSelect,function(a,g){var i,j=e+g;!this.length||this[g]||this[j]||(b=h(a,this.models),c||this.find(function(a){return a[g]})&&(this[j]=!0),!this[j]&&f(b,this)&&(i={label:g},d&&(i.silent=!0),c&&!d&&(i["@bbs:silentLocally"]=!0),this.selectAt(b,i))),this[g]&&this[j]&&delete this[j]},this)}function c(a,b,c){var d,f=c&&c.index,g=c&&c.label||b._pickyDefaultLabel,h=b._cycleOpts.selectIfRemoved&&b._cycleOpts.selectIfRemoved[g];c&&"remove"===c._externalEvent&&h&&b.length&&(d=h.indexOf("next")!==-1?f:f-1,h.indexOf("NoLoop")!==-1&&(d=Math.max(Math.min(d,b.length-1),0)),b.select(e(d,b),c))}function d(a,b,d){var e=d&&d["@bbs:wasSelected"]||{};_.each(e,function(e,f){var g=_.clone(d);g._externalEvent="remove",g.label=f,e&&c(a,b,g)})}function e(a,b){var c=a%b.length;return c<0&&(c=b.length+c),b.at(c)}function f(a,b){var c=a>=0&&a<b.length;return c?b.at(a):void 0}function g(a){return!isNaN(a)&&parseInt(Number(a),10)==a&&!isNaN(parseInt(a,10))}function h(a,b){return"first"===a?0:"last"===a?b.length-1:parseInt(a,10)}function i(a,b,c){var d=a._cycleOpts[b],e=_.isObject(d)?_.values(d):[d],f=_.isFunction(c);_.each(e,function(a){var e,g=f?c(a):_.contains(c,a);if(!g)throw e=b+' option: Invalid value "'+a+'"',_.isObject(d)&&(e+=" inside hash"),new Error(e)})}function j(a,b){var c=a._cycleOpts[b],d=_.keys(c),e=_.intersection(a._pickyIgnoredLabels,d);if(e.length)throw e=_.map(e,function(a){return'"'+a+'"'}),new Error("Conflicting options: Can't define "+b+" behaviour for label "+e.join(", ")+" because it is ignored in the collection.")}function k(a){var b=function(a){return"none"!==a};return _.isObject(a)?_.some(a,b):b(a)}function l(a,b){var c={},d=a._cycleOpts[b];_.isObject(d)?_.each(d,function(a,b){a&&"none"!==a&&(c[b]=a)}):d&&"none"!==d&&(c[a._pickyDefaultLabel]=d),a._cycleOpts[b]=c}var m=Backbone.Select.Me.custom.applyModelMixin,n={Model:{ahead:function(a,b){var c=b||this.collection,d=c.indexOf(this);if(d===-1)throw new Error("Model "+this.cid+"doesn't exist in the collection.");return e(d+a,c)},behind:function(a,b){return this.ahead(-a,b)},next:function(a){return this.ahead(1,a)},prev:function(a){return this.behind(1,a)},aheadNoLoop:function(a,b){var c=b||this.collection,d=c.indexOf(this);if(d===-1)throw new Error("Model "+this.cid+"doesn't exist in the collection.");return f(d+a,c)},behindNoLoop:function(a,b){return this.aheadNoLoop(-a,b)},nextNoLoop:function(a){return this.aheadNoLoop(1,a)},prevNoLoop:function(a){return this.behindNoLoop(1,a)},_cycleType:"Backbone.Cycle.Model"},SelectableCollection:{selectAt:function(a,b){var c=f(a,this);if(!c)throw new Error("Model with index "+a+" doesn't exist in the collection and can't be selected.");return this.select(c,b),this},selectNext:function(a){return this.select(this.next(a),a),this},selectPrev:function(a){return this.select(this.prev(a),a),this},selectNextNoLoop:function(a){var b=this.nextNoLoop(a);return b&&this.select(b,a),this},selectPrevNoLoop:function(a){var b=this.prevNoLoop(a);return b&&this.select(b,a),this},ahead:function(a,b){var c=b&&b.label||this._pickyDefaultLabel;if(!this[c])throw new Error('Illegal call of SelectableCollection navigation method. No model had been selected to begin with (using label "'+c+'").');return e(this.indexOf(this[c])+a,this)},behind:function(a,b){return this.ahead(-a,b)},next:function(a){return this.ahead(1,a)},prev:function(a){return this.behind(1,a)},aheadNoLoop:function(a,b){var c=b&&b.label||this._pickyDefaultLabel;if(!this[c])throw new Error('Illegal call of SelectableCollection navigation method. No model had been selected to begin with (using label "'+c+'").');return f(this.indexOf(this[c])+a,this)},behindNoLoop:function(a,b){return this.aheadNoLoop(-a,b)},nextNoLoop:function(a){return this.aheadNoLoop(1,a)},prevNoLoop:function(a){return this.behindNoLoop(1,a)},_cycleType:"Backbone.Cycle.SelectableCollection"}};Backbone.Cycle={Model:{applyTo:function(a){_.extend(a,n.Model)}},SelectableModel:{applyTo:function(a,b){Backbone.Select.Me.applyTo(a,b),_.extend(a,n.Model,{_cycleType:"Backbone.Cycle.SelectableModel"})}},SelectableCollection:{applyTo:function(a,e,f){var m,o,p;if(arguments.length<2)throw new Error("The `models` parameter has not been passed to Backbone.Cycle.SelectableCollection.applyTo. Its value can be undefined if no models are passed in during instantiation, but even so, it must be provided.");f||(f={}),a._cycleOpts={},a._cycleOpts.autoSelect=f.autoSelect||f.initialSelection||"none",a._cycleOpts.selectIfRemoved=f.selectIfRemoved||"none",i(a,"autoSelect",function(a){return _.contains(["first","last","none"],a)||g(a)}),i(a,"selectIfRemoved",["prev","next","prevNoLoop","nextNoLoop","none"]),m=k(a._cycleOpts.autoSelect),o=k(a._cycleOpts.selectIfRemoved),_.extend(a,n.SelectableCollection),Backbone.Select.One.applyTo(a,e,f),l(a,"autoSelect"),l(a,"selectIfRemoved"),j(a,"autoSelect"),j(a,"selectIfRemoved"),o&&(a.listenTo(a,"deselect:one",c),a.listenTo(a,"@bbs:remove:silent",d)),m&&(_.isArray(e)&&e.length&&_.each(a._cycleOpts.autoSelect,function(b,c){var d;a[c]||(p=h(b,e),d=e[p],d&&d instanceof Backbone.Model&&(a[c]=d,d.select({label:c})))}),a.listenTo(a,"add",b),a.listenTo(a,"reset",b),a.listenTo(a,"@bbs:add:silent",_.partial(b,{silent:!0})),a.listenTo(a,"@bbs:reset:silent",_.partial(b,{silent:!0})))}},version:"3.0.0"},Backbone.Select.Me.custom.applyModelMixin=function(a,b,c){"Backbone.Cycle.SelectableCollection"===b._cycleType?Backbone.Cycle.SelectableModel.applyTo(a,c):m?m(a,b,c):Backbone.Select.Me.applyTo(a,c)},a.info="Backbone.Cycle has loaded. Don't use the exported value of the module. Its functionality is available inside the Backbone namespace."});
//# sourceMappingURL=backbone.cycle.min.js.map