pourover
Version:
A library for simple, fast filtering and sorting of large collections in the browser
1 lines • 30.7 kB
JavaScript
!function(t,e){"function"==typeof define&&define.amd?define(["underscore"],e):"object"==typeof exports?module.exports=e(require("underscore")):t.PourOver=e(t._)}(this,function(t){var e=function(){var i=function(t,e){console.warn("Deprecation warning: "+t+" will be renamed to "+e+" in the next major release.")},r=function(){},n=t.create||function(t){r.prototype=t;var e=new r;return r.prototype=null,e};e={union_sorted:function(t,e){return i("union_sorted","unionSort"),this.unionSort(t,e)},unionSort:function(e,i){for(var r,n,s=0,c=0,o=e.length,a=i.length,u=[];o>s||a>c;){if(r=e[s],n=i[c],t.isUndefined(r)&&(r=1/0),t.isUndefined(n)&&(n=1/0),s==o)return u.concat(i.slice(c,a));if(c==a)return u.concat(e.slice(s,o));r==n?(u.push(r),s++,c++):n>r?(u.push(r),s++):(u.push(n),c++)}return u},intersect_sorted:function(t,e){return i("intersect_sorted","intersectSort"),this.intersectSort(t,e)},intersectSort:function(t,e){for(var i,r,n=0,s=0,c=t.length,o=e.length,a=[];c>n&&o>s;)i=t[n],r=e[s],i==r?(a.push(i),n++,s++):r>i?n++:s++;return a},subtract_sorted:function(t,e){return i("subtract_sorted","subtractSort"),this.subtractSort(t,e)},subtractSort:function(t,e){for(var i,r,n=0,s=0,c=t.length,o=e.length,a=[];c>n||o>s;){if(i=t[n],r=e[s],c==n)return a;if(o==s)return a.concat(t.slice(n,c));i==r?(n++,s++):r>i?(a.push(i),n++):s++}return a},insert_sorted:function(t,e){return i("insert_sorted","insertSort"),this.insertSort(t,e)},insertSort:function(t,e){var i=t.length,r=0,n=t[i-1];if(e>n)return t.push(e),t;for(;i>r;){if(e<t[r])return t.slice(0,r).concat([e]).concat(t.slice(r,i));r++}return t.push(e),t},build_permutation_array:function(t,e){return i("build_permutation_array","buildPermutationArray"),this.buildPermutationArray(t,e)},buildPermutationArray:function(e,i){var r=t.clone(e),n=[];return r.sort("function"==typeof i?i:function(t,e){return i.fn.call(i,t,e)}),t.each(r,function(t,e){n[t.cid]=e}),n},permute_from_array:function(t,e){return i("permute_from_array","permuteFromArray"),this.permuteFromArray(t,e)},permuteFromArray:function(e,i){var r=[];return"number"==typeof e[0]?t.each(e,function(t){r[i[t]]=t}):t.each(e,function(t){r[i[t.cid]]=t}),t.without(r,void 0)},remove_sorted:function(t,e){return i("remove_sorted","removeSort"),this.removeSort(t,e)},removeSort:function(t,e){for(var i=t.length,r=0;i>r;){if(e==t[r])return t.slice(0,r).concat(t.slice(r+1,i));r++}return t},bisect_by:function(t){return i("bisect_by","bisectBy"),this.bisectBy(t)},bisectBy:function(t){function e(e,i,r,n){for(;n>r;){var s=r+n>>>1;t(e[s])<i?r=s+1:n=s}return r}function i(e,i,r,n){for(;n>r;){var s=r+n>>>1;i<t(e[s])?n=s:r=s+1}return r}return i.right=i,i.left=e,i},cacheMethods:{defaultCache:function(e){var i=this;t.each(i.possibilities,function(r){var n=t.filter(e,function(t){return i.fn(r,t)}),s=t.pluck(n,"cid");r.matching_cids=s})},defaultAddCache:function(i){var r=this;t.each(r.possibilities,function(n){var s=t.filter(i,function(t){return r.fn(n,t)}),c=t.pluck(s,"cid");n.matching_cids=e.unionSort(n.matching_cids,c)})},exactCache:function(i){var r=this,n=this.attr||this.name;t.each(i,function(t){var i=r.possibilities[t[n]];i&&(i.matching_cids=e.insertSort(i.matching_cids,t.cid))})},exactAddCache:function(t){e.cacheMethods.exactCache.call(this,t)},inclusionCache:function(i){var r=this,n=this.attr||this.name;t.each(i,function(i){t.each(i[n],function(t){var n=r.possibilities[t];n&&(n.matching_cids=e.insertSort(n.matching_cids,i.cid))})})},inclusionAddCache:function(t){e.cacheMethods.inclusionCache.call(this,t)}}};var s=[],c=(s.push,s.slice),o=(s.splice,e.Events={on:function(t,e,i){if(!u(this,"on",t,[e,i])||!e)return this;this._events||(this._events={});var r=this._events[t]||(this._events[t]=[]);return r.push({callback:e,context:i,ctx:i||this}),this},once:function(e,i,r){if(!u(this,"once",e,[i,r])||!i)return this;var n=this,s=t.once(function(){n.off(e,s),i.apply(this,arguments)});return s._callback=i,this.on(e,s,r)},off:function(e,i,r){var n,s,c,o,a,h,f,l;if(!this._events||!u(this,"off",e,[i,r]))return this;if(!e&&!i&&!r)return this._events=void 0,this;for(o=e?[e]:t.keys(this._events),a=0,h=o.length;h>a;a++)if(e=o[a],c==this._events[e]){if(this._events[e]=n=[],i||r)for(f=0,l=c.length;l>f;f++)s=c[f],(i&&i!==s.callback&&i!==s.callback._callback||r&&r!==s.context)&&n.push(s);n.length||delete this._events[e]}return this},trigger:function(t){if(!this._events)return this;var e=c.call(arguments,1);if(!u(this,"trigger",t,e))return this;var i=this._events[t],r=this._events.all;return i&&h(i,e),r&&h(r,arguments),this},stopListening:function(e,i,r){var n=this._listeningTo;if(!n)return this;var s=!i&&!r;r||"object"!=typeof i||(r=this),e&&((n={})[e._listenId]=e);for(var c in n)e=n[c],e.off(i,r,this),(s||t.isEmpty(e._events))&&delete this._listeningTo[c];return this}}),a=/\s+/,u=function(t,e,i,r){if(!i)return!0;if("object"==typeof i){for(var n in i)t[e].apply(t,[n,i[n]].concat(r));return!1}if(a.test(i)){for(var s=i.split(a),c=0,o=s.length;o>c;c++)t[e].apply(t,[s[c]].concat(r));return!1}return!0},h=function(t,e){var i,r=-1,n=t.length,s=e[0],c=e[1],o=e[2];switch(e.length){case 0:for(;++r<n;)(i=t[r]).callback.call(i.ctx);return;case 1:for(;++r<n;)(i=t[r]).callback.call(i.ctx,s);return;case 2:for(;++r<n;)(i=t[r]).callback.call(i.ctx,s,c);return;case 3:for(;++r<n;)(i=t[r]).callback.call(i.ctx,s,c,o);return;default:for(;++r<n;)(i=t[r]).callback.apply(i.ctx,e);return}},f={listenTo:"on",listenToOnce:"once"};return t.each(f,function(e,i){o[i]=function(i,r,n){var s=this._listeningTo||(this._listeningTo={}),c=i._listenId||(i._listenId=t.uniqueId("l"));return s[c]=i,n||"object"!=typeof r||(n=this),i[e](r,n,this),this}}),o.bind=o.on,o.unbind=o.off,t.extend(e,o),e.Collection=function(e,i){"undefined"==typeof e&&(e=[]),this.items=[],this.filters={},this.sorts={},this.addItems(e),this.on("change",function(){t.each(this.filters,function(t){t.current_query&&t.current_query.refresh()})}),this.initialize.apply(this,arguments)},t.extend(e.Collection.prototype,e.Events,{initialize:function(){},refresh:function(){this.trigger("queryChange")},get:function(t){return e.Collection.prototype.getBy.call(this,"cid",t,!0)},getBy:function(e,i,r){if(!t.isArray(i))var i=[i];"undefined"==typeof r&&(r=!1);var n,s=0,c=this.items.length,o=0,a=i.length,u=[],h=this.items;if(r===!0)for(;c>s&&a>o;)i[o]==(n=h[s])[e]?(u.push(n),s++,o++):i[o]<n[e]?o++:s++;else if("reverse"==r)for(;c>s&&a>o;)i[o]==(n=h[s])[e]?(u.push(n),s++,o++):i[o]>n[e]?o++:s++;else for(;c>s&&a>o;)t.include(i,(n=h[s])[e])?(u.push(n),i=t.without(i,n[e]),s++,o++):s++;return u},getByFirst:function(t,e,i){"undefined"==typeof i&&(i=!1);var r,n,s=0,c=this.items.length,o=this.items;if(i===!0)for(;c>s;){if(e==(n=o[s])[t]){r=n;break}if(e<n[t])break;s++}else if("reverse"==i)for(;c>s;){if(e==(n=o[s])[t]){r=n;break}if(e>n[t])break;s++}else for(;c>s;){if(e==(n=o[s])[t]){r=n;break}s++}return r},addItems:function(i){this.trigger("will_change"),t.isArray(i)||(i=[i]);var r,n=this.items.length>0?t.last(this.items).cid+1:0;r=t.map(i,function(t){var i=e.Item(t);return i.cid=n++,i}),this.items=this.items.concat(r),this.regenerateFilterSets(r),this.trigger("change",t(r).pluck("cid"))},removeItems:function(e,i){if(this.trigger("will_change"),"undefined"==typeof i)var i=!1;if(!t.isArray(e))var e=[e];if(i){e=e.sort(function(t,e){return t.cid-e.cid});for(var r=[],n=this.items,s=e.length,c=this.items.length,o=0,a=0;c>a;){if(s>!o){r=r.concat(n.slice(a));break}n[a].cid===e[o].cid?(o++,a++):(r.push(n[a]),a++)}}else for(var r=[],n=this.items,c=this.items.length,a=0,u=t.pluck(e,"cid");c>a&&u.length>0;)t.include(u,n[a].cid)||r.push(n[a]),a++;this.items=r,this.regenerateFilterSets(),this.trigger("change",t(e).pluck("cid"))},addFilters:function(e){var i,r=this;t.isArray(e)||(e=[e]),i=t.reduce(e,function(t,e){return t[e.name]=n(e),t[e.name].collection=r,t},{}),this.filters=t.extend(this.filters,i),t.each(i,function(e){e.on("queryChange",function(){r.trigger("queryChange")}),e.cacheResults(r.items),e.associated_attrs&&t.each(e.associated_attrs,function(t){r.on("change:"+t,function(t){e.removeFromCache(t),e.addCacheResults(t),e.current_query&&e.current_query.refresh()})})})},regenerateFilterSets:function(e){var i=this;"undefined"==typeof e?t.each(this.filters,function(t){t.cacheResults(i.items)}):t.each(this.filters,function(t){t.addCacheResults(e)})},getAllItems:function(){var i=t.pluck(this.items,"cid");return new e.MatchSet(i,this,["all"])},getCurrentFilteredItems:function(t,i){return"undefined"==typeof i&&(i=!1),this.filters[t].current_query&&this.filters[t].current_query.stack.length>0?this.filters[t].current_query:i?new e.MatchSet([],this,[]):this.getAllItems()},getFilteredItems:function(e,i){var r=this.filters[e];if(t.isUndefined(r))throw"The filter "+e+" does not exist.";return r.getFn(i)},addSort:function(e){var i=this;this.sorts[e.name]=e,e.collection=this,e.rebuildSort(),this.on("change",function(){e.rebuildSort(!0)}),e.associated_attrs&&t.each(e.associated_attrs,function(t){i.on("change:"+t,function(t){e.rebuildSort()})})},addSorts:function(e){"undefined"==typeof opts&&(opts={}),t.isArray(e)||(e=[e]);var i=this;t.each(e,function(t){i.addSort(t)})},getSortedItems:function(t){var e=this.sorts[t];return e.sort(this.items)},getItemValue:function(e,i){var r=t.find(this.items,function(t){return t.cid===Number(e)});return r[i]},updateItem:function(e,i,r){this.trigger("will_incremental_change");var n=t.find(this.items,function(t){return t.cid===Number(e)});return n[i]=r,this.trigger("change:"+i,[n]),this.trigger("incremental_change",[i]),this.trigger("update","updateItem"),n.guid},removeItemAttribute:function(e,i,r){this.trigger("will_incremental_change");var n=t.find(this.items,function(t){return t.cid===Number(e)});return delete n[i],this.trigger("change:"+i,[n]),this.trigger("incremental_change",[i]),this.trigger("update","updateItem"),n.guid},batchUpdateItems:function(e,i,r){this.trigger("will_incremental_change");var n=this.get(e,!0);return t.each(n,function(t){t[i]=r}),this.trigger("change:"+i,n),this.trigger("incremental_change",[i]),this.trigger("update","batchUpdate"),t.pluck(n,"guid")},updateAttributes:function(e,i,r){if("undefined"==typeof r)var r=!1;this.trigger("will_incremental_change");var n=t.find(this.items,function(t){return t.cid===Number(e)}),s=this;return t.each(i,function(t,e){n[e]=t,s.trigger("change:"+e,[n])}),this.trigger("incremental_change",t.keys(i)),r||this.trigger("update","updateAttribute"),n.guid},batchUpdateAttributes:function(e,i,r){if("undefined"==typeof r)var r=!1;this.trigger("will_incremental_change");var n=this.get(e,!0),s=this;return t.each(n,function(e){t.each(i,function(t,i){e[i]=t})}),t.each(i,function(t,e){s.trigger("change:"+e,n)}),this.trigger("incremental_change",t.keys(i)),r||(this.trigger("update","batchUpdate"),this.trigger("batchUpdateAttribute")),t.pluck(n,"guid")},batchLoadItems:function(i){this.trigger("will_incremental_change");var r=[],n=t.pluck(i,"guid"),s=this.getBy("guid",n),c={};t(s).each(function(t){c[t.guid]=t}),t.each(i,t.bind(function(i){var n,o=c[i.guid],a=this.items.length>0?t(this.items).last().cid+1:0;o?(n=o,t.each(i,function(t,e){n[e]=t})):(o=e.Item(i),o.cid=a++,r.push(o.cid),this.items=this.items.concat([o]),s[o.guid]=o)},this)),this.regenerateFilterSets(),this.trigger("incremental_change","*"),this.trigger("change",r),this.trigger("update","batchLoad"),this.trigger("batchLoadItems")}}),e.Item=function(t){return t},e.Filter=function(e,i,r){"undefined"==typeof r&&(r={}),this.name=e,this.possibilities=this.createPossibilities(i),this.values=t.pluck(i,"value"),t.extend(this,r),this.initialize.apply(this,arguments)},t.extend(e.Filter.prototype,e.Events,{initialize:function(){},create_possibilities:function(t){return i("create_possibilities","createPossibilities"),this.createPossibilities(t)},createPossibilities:function(e){var i={};return t.each(e,function(t){var e=t.name||String(t.value);i[e]=t,i[e].matching_cids=[]}),i},cacheResults:function(t){throw"No cache function has been defined for this filter '"+this.name+"'."},addCacheResults:function(t){throw"No add cache function has been defined for this filter '"+this.name+"'."},makeQueryMatchSet:function(t,i){return new e.MatchSet(t,this.getCollection(),[[this,i]])},removeFromCache:function(i){var r=t.pluck(i,"cid").sort(function(t,e){return t-e});t.each(this.possibilities,function(t){t.matching_cids=e.subtractSort(t.matching_cids,r)})},query:function(t,e){if("undefined"==typeof e)var e=!1;var i=this.getFn(t);this.setQuery(i,e)},setQuery:function(t,e){if("undefined"==typeof e)var e=!1;this.current_query=t,e||this.trigger("queryChange")},clearQuery:function(t){if("undefined"==typeof t)var t=!1;this.current_query=!1,t||this.trigger("queryChange")},unionQuery:function(e,i){if("undefined"==typeof i)var i=!1;if("string"==typeof e||"number"==typeof e||t.isArray(e))var e=this.getFn(e);this.current_query=this.current_query?this.current_query.or(e):e,i||this.trigger("queryChange")},intersectQuery:function(e,i){if("undefined"==typeof i)var i=!1;if("string"==typeof e||"number"==typeof e||t.isArray(e))var e=this.getFn(e);this.current_query=this.current_query?this.current_query.and(e):e,i||this.trigger("queryChange")},subtractQuery:function(e,i){if("undefined"==typeof i)var i=!1;if("string"==typeof e||"number"==typeof e||t.isArray(e))var e=this.getFn(e);this.current_query=this.current_query?this.current_query.not(e):e,i||this.trigger("queryChange")},removeSingleQuery:function(e,i){if(!this.current_query)return!1;if("undefined"==typeof i)var i=!1;if("string"==typeof e||"number"==typeof e||t.isArray(e))var e=this.getFn(e);var r,n=[],s=this.current_query.stack,c=function(e){return t.isString(e)&&e.match(/^(or|and|not)$/)};r=t.reduce(s,function(t,i){return i[1]===e.stack[0][1]?t:c(i[0])&&i[1][0][1]===e.stack[0][1]?t:(t.push(i),t)},n),!r[0]||"and"!=r[0][0]&&"or"!=r[0][0]&&"not"!=r[0][0]||(r[0]=r[0][1][0]),this.current_query.stack=r,this.current_query.refresh(),i||this.trigger("queryChange")},getCollection:function(){return this.collection},getByPossibilityGroups:function(){var e=this.collection;return t.reduce(this.possibilities,function(t,i,r){return t[r]=e.get(i.matching_cids),t},{})}}),e.Sort=function(e,i){this.name=e,t.extend(this,i),this.initialize.apply(this,arguments)},t.extend(e.Sort.prototype,e.Events,{initialize:function(){},view:!1,sort:function(t){return e.permuteFromArray(t,this.permutation_array)},rebuild_sort:function(t){i("rebuild_sort","rebuildSort"),this.rebuildSort(t)},rebuildSort:function(t){if("undefined"==typeof t&&(t=!1),this.view)var i=this.view.match_set.all();else var i=this.collection.items;this.permutation_array=e.buildPermutationArray(i,this),this.trigger("resort",t)}}),e.View=function(i,r,n){var s=this;this.name=i,"undefined"==typeof n&&(n={}),this.collection=r,this.match_set=new e.MatchSet(t.pluck(this.collection.items,"cid"),this.collection,["all"]),n.template&&(this.template=n.template),this.collection.on("will_change will_incremental_change",function(){s.storeViewPosition()}),this.collection.on("change",function(){s.match_set.refresh(),s.setNaturalSelection(),s.resetPage(),s.trigger("collection-change")}),this.collection.on("incremental_change",function(t){s.match_set.refresh(),s.setNaturalSelection(t),s.resetPage(),s.trigger("collection-incremental-change")}),this.collection.on("update",function(t){s.trigger("update",t)}),this.collection.on("queryChange",function(){s.setNaturalSelection(),s.trigger("update","query")}),this.on("sortChange",function(){this.trigger("update","sort")}),this.on("pageChange",function(){this.trigger("update","page")}),this.view_sorts=[],t.extend(this,n),this.initialize.apply(this,arguments)},t.extend(e.View.prototype,e.Events,{initialize:function(){},current_page:0,page_size:1/0,current_sort:!1,removeSort:function(){this.current_sort.off&&this.current_sort.off("resort"),this.current_sort=!1,this.trigger("sortChange")},setSort:function(e,i,r){"undefined"==typeof i&&(i=!1),"undefined"==typeof r&&(r=!1);var n=this;this.current_sort.off&&this.current_sort.off("resort"),e&&i?(this.current_sort=this.view_sorts[e],this.current_sort.on("resort",t.bind(function(t){this.silent_sort&&t||n.trigger("sortChange")},this))):e?(this.current_sort=this.collection.sorts[e],this.current_sort.on("resort",t.bind(function(t){this.silent_sort&&t||n.trigger("sortChange")},this))):this.current_sort=!1,r||this.trigger("sortChange")},getSort:function(){return this.current_sort?this.current_sort.name:!1},addViewSorts:function(e){"undefined"==typeof opts&&(opts={}),t.isArray(e)||(e=[e]);var i=this;t.each(e,function(e){i.view_sorts[e.name]=e,e.collection=i.collection,e.view=i,e.rebuildSort(),i.on("selectionChange",function(i){(void 0===e.associated_attrs||"*"===i)&&e.rebuildSort(),e.associated_attrs&&t.intersection(e.associated_attrs,i).length>0&&e.rebuildSort()})})},selectionFn:function(){var e=this.collection;if(t.isEmpty(e.filters))return e.getAllItems();var i=t.reduce(e.filters,function(i,r){var n=r.current_query;return!i||n&&!t.isEmpty(n.stack)?i||n&&!t.isEmpty(n.stack)?i?i.and(n):n:e.getAllItems():i},!1);return i},setSelection:function(t,e){this.match_set=t,this.trigger("selectionChange",e)},setNaturalSelection:function(t){var e;e=this.selectionFn(),this.setSelection(e,t)},clearSelection:function(){this.match_set=this.collection.getAllItems()},getCurrentItems:function(e){if(!this.match_set)return[];if("undefined"==typeof e)var e=this.current_page;if(this.page_size==1/0)if(this.current_sort)var i=this.match_set.allSorted(this.current_sort);else var i=this.match_set.all();else if(this.current_sort){var i=this.match_set.allSortedCids(this.current_sort);i=i.slice(this.page_size*e,this.page_size*(e+1));var r=t.clone(i).sort(function(t,e){return t-e}),n=this.collection.get(r);i=t.map(i,function(e){return t.findWhere(n,{cid:e})})}else{var i=this.match_set.cids;i=i.slice(this.page_size*e,this.page_size*(e+1)),i=this.collection.get(i)}return i},storeViewPosition:function(){var t=this.getCurrentItems()[0];t&&(this.last_head_cid=t.cid)},resetPage:function(){this.last_head_cid&&(this.current_sort&&this.current_sort.rebuildSort(),this.pageTo(this.last_head_cid,!0)),this.last_head_cid=void 0},page:function(t){var e=t+this.current_page;0>e&&(e=0),e>Math.ceil(this.match_set.length()/this.page_size-1)&&(e=Math.ceil(this.match_set.length()/this.page_size-1)),this.current_page=e,this.trigger("pageChange")},pageTo:function(e,i){if("undefined"==typeof i)var i=!1;if(this.current_sort)var r=t.indexOf(this.match_set.allSortedCids(this.current_sort),e),n=(this.match_set.cids.length,Math.floor(r/this.page_size));else var r=t.indexOf(this.match_set.cids,e),n=(this.match_set.cids.length,Math.floor(r/this.page_size));r>=0&&(this.current_page=n,i||this.trigger("pageChange"))},setPage:function(t){0>t&&(t=0),t>Math.ceil(this.match_set.length()/this.page_size-1)&&(t=Math.ceil(this.match_set.length()/this.page_size-1)),this.current_page=t,this.trigger("pageChange")},setPageSize:function(t){this.page_size=t,this.trigger("pageChange")},render:function(){}}),e.MatchSet=function(t,e,i){this.cids=t,this.collection=e,this.stack=i,this.initialize.apply(this,arguments)},t.extend(e.MatchSet.prototype,e.Events,{initialize:function(){},refresh:function(i,r){if("undefined"==typeof i)var i=this.stack||[];if(i.length<1&&r)return this.cids=r.cids,this;if(i.length<1)return this.cids=!1,this;var n=i[0],s=n[0],c=function(e){return t.isString(e)&&e.match(/^(or|and|not)$/)};if("object"==typeof s)return r=s.getFn(n[1]),this.refresh(t.rest(i),r);if("all"===s||"all"===n){var o=t.pluck(this.collection.items,"cid");return r=new e.MatchSet(o,this,["all"]),this.refresh(t.rest(i),r)}if(c(s))var a=r[s](this.refresh(n[1]));else var a=this.refresh(n[1]);return this.refresh(t.rest(i),a)},and:function(t){if(this.stack.length<1&&t)return t;if(t){var i=e.intersectSort(this.cids,t.cids);return new e.MatchSet(i,this.collection,this.stack.concat([["and",t.stack]]))}return this},or:function(t){if(this.stack.length<1&&t)return t;if(t){var i=e.unionSort(this.cids,t.cids);return new e.MatchSet(i,this.collection,this.stack.concat([["or",t.stack]]))}return this},not:function(t){if(this.stack.length<1||!t)return this;var i=e.subtractSort(this.cids,t.cids);return new e.MatchSet(i,this.collection,this.stack.concat([["not",t.stack]]))},all:function(){return this.collection.get(this.cids)},slice:function(t,e){return this.collection.get(this.cids.slice(t,e))},all_sorted:function(t){return i("all_sorted","allSorted"),this.allSorted(t)},allSorted:function(t){var e=this.all();return t?t.sort(e):e},all_sorted_cids:function(t){return i("all_sorted_cids","allSortedCids"),this.allSortedCids(t)},allSortedCids:function(t){var e=this.cids;return t?t.sort(e):e},length:function(){return this.cids.length}}),e.UI={},e.UI.Element=function(e){if("undefined"==typeof e)var e={};t.extend(this,e),this.initialize.apply(this,arguments)},t.extend(e.UI.Element.prototype,e.Events,{initialize:function(){},getMatchSet:function(){throw"No get match set function specified"},getFilterState:function(){throw"No get filter state specified"},template:function(){throw"No template specified"},render:function(){var t=this.getFilterState(),e=this.template({state:t});return e},getSimpleSelectState:function(e,i,r){if("undefined"==typeof e||!e||!e.stack)return!1;if("undefined"==typeof i&&(i=e.stack),"undefined"==typeof r&&(r=[]),i.length<1)return r;if("object"==typeof i[0][0])return r.push(i[0][1]),this.getSimpleSelectState(e,t.rest(i),r);if("or"===i[0][0])return r=r.concat(this.getSimpleSelectState(e,i[0][1])),this.getSimpleSelectState(e,t.rest(i),r);throw"This does not appear to be a valid, simple selectElement stack."},getIntersectedSelectState:function(e,i,r){if("undefined"==typeof e||!e||!e.stack)return!1;if("undefined"==typeof i&&(i=e.stack),"undefined"==typeof r&&(r=[]),i.length<1)return r;if("object"==typeof i[0][0])return r.push(i[0][1]),this.getIntersectedSelectState(e,t.rest(i),r);if("and"===i[0][0])return r=r.concat(this.getIntersectedSelectState(e,i[0][1])),this.getIntersectedSelectState(e,t.rest(i),r);throw"This does not appear to be a valid, simple selectElement stack."},getSimpleRangeState:function(t){if("undefined"==typeof t||!t||!t.stack)return!1;if(stack=t.stack,1!==stack.length||2!==stack[0][1].length)throw"The filter specified does not appear to have a simple range; stack.";return stack[0][1]}}),e.extend=function(e,i){var r,n=this;r=e&&t.has(e,"constructor")?e.constructor:function(){return n.apply(this,arguments)},t.extend(r,n,i);var s=function(){this.constructor=r};return s.prototype=n.prototype,r.prototype=new s,e&&t.extend(r.prototype,e),r.__super__=n.prototype,r},e.Collection.extend=e.View.extend=e.Filter.extend=e.Sort.extend=e.MatchSet.extend=e.UI.Element.extend=e.extend,e.BufferedCollection=e.Collection.extend({initialize:function(){this.buffered_items={}},stripFutures:function(e){return t.reduce(e,function(t,e,i){return"undefined"!=typeof e&&(t[i]=e),t},{})},get:function(i,r){"undefined"==typeof r&&(r=!1);var n=e.Collection.prototype.get.call(this,i),s=this;return r?n:t.map(n,function(e){var i=e.guid;return s.buffered_items.hasOwnProperty(i)?t.extend(s.buffered_items[i],s.stripFutures(e)):e})},getBy:function(i,r,n,s){"undefined"==typeof s&&(s=!1);var c=e.Collection.prototype.getBy.call(this,i,r,n),o=this;return s?c:t.map(c,function(e){var i=e.guid;return o.buffered_items.hasOwnProperty(i)?t.extend(o.buffered_items[i],o.stripFutures(e)):e})},getBufferedValue:function(t,e){return this.buffered_items.hasOwnProperty(t)?this.buffered_items[t][e]||!1:!1},clearBufferedItems:function(){var t=this.buffered_items;for(var e in t)t.hasOwnProperty(e)&&delete t[e]},getBufferUrl:function(t){throw"You must override getBufferUrl"},preprocessItem:function(t){return[t.guid,t]},bufferGuids:function(e){var i=this;e=t.select(e,function(t){return t&&!i.buffered_items.hasOwnProperty(t)});{var r=this.getBufferUrl(e),n=r[0];r[1]}return e.length>0?$.ajax({url:n,dataType:"jsonp",cache:!0}).always(function(e){t.isArray(e)&&(items=t.map(e,i.preprocessItem,i),t.each(items,function(t){i.buffered_items[t[0]]=t[1]}))}):$.Deferred().resolve(!1)}}),e.BufferedView=e.View.extend({buffer_pages:1,bufferAroundCurrentPage:function(){var e=this.current_page,i=e-this.buffer_pages>0?e-this.buffer_pages:0,r=e+this.buffer_pages,n=t.range(i,r+1),s=this;n=t.map(n,function(e){return t.pluck(s.getCurrentItems(e),"guid")});var c=t.flatten(n);buffer_deferred=this.collection.bufferGuids(c),buffer_deferred.done(function(t){t&&s.render()})},bufferRender:function(){var e=t.pluck(this.getCurrentItems(),"guid"),i=this.collection.bufferGuids(e);i.done(t.bind(function(){this.render()},this))},page:function(t){e.View.prototype.page.call(this,t),this.bufferAroundCurrentPage()},pageTo:function(t,i){"undefined"==typeof i&&(i=!1),e.View.prototype.pageTo.call(this,t,i),this.bufferAroundCurrentPage()}}),e.manualFilter=e.Filter.extend({cacheResults:function(){return!1},addCacheResults:function(){return!1},getFn:function(i){if(t.isArray(i))return i=i.sort(function(t,e){return t-e}),new e.MatchSet(i,this.getCollection(),[[this,i]]);if("number"==typeof i)return new e.MatchSet([i],this.getCollection(),[[this,i]]);throw"Manual filters only support querying by one or more cids"},addItems:function(i){if(t.isArray(i)||(i=[i]),i=i.sort(function(t,e){return t-e}),this.current_query)var r=this.current_query.cids,n=e.unionSort(r,i);else var n=i;this.query(n)},removeItems:function(i){t.isArray(i)||(i=[i]),i=i.sort(function(t,e){return t-e});var r=this.current_query.cids,n=e.subtractSort(r,i);this.query(n)}}),e.makeManualFilter=function(t){var i=new e.manualFilter(t,[]);return i},e.exactFilter=e.Filter.extend({cacheResults:e.cacheMethods.exactCache,addCacheResults:e.cacheMethods.exactAddCache,getFn:function(i){var r=this;if(t.isArray(i)){var n=t.reduce(i,function(t,e){return t?t.or(r.getFn(e)):r.getFn(e)},!1);return n}var s=this.possibilities[i];if(t.isUndefined(s))throw"The filter "+this.name+" does not have a match for the query '"+i+"'.";return new e.MatchSet(s.matching_cids,this.getCollection(),[[this,i]])}}),e.makeExactFilter=function(i,r,n){"undefined"==typeof n&&(n={});var s=n.attr||i;return r=t.map(r,function(t){return{value:t}}),n=t.extend({associated_attrs:[s]},n),new e.exactFilter(i,r,n)},e.inclusionFilter=e.exactFilter.extend({cacheResults:e.cacheMethods.inclusionCache,addCacheResults:e.cacheMethods.inclusionAddCache}),e.makeInclusionFilter=function(i,r,n){"undefined"==typeof n&&(n={});var s=n.attr||i;return r=t.map(r,function(t){return{value:t}}),n=t.extend({associated_attrs:[s]},n),new e.inclusionFilter(i,r,n)},e.rangeFilter=e.Filter.extend({cacheResults:e.cacheMethods.defaultCache,addCacheResults:e.cacheMethods.defaultAddCache,fn:function(t,e){var i=this.attr||this.name;return t.low<=e[i]&&t.high>=e[i]},getFn:function(i){var r=this.possibilities[i.join("-")];if(t.isUndefined(r))throw"The filter "+this.name+" does not have a match for the query '"+i+"'.";return new e.MatchSet(r.matching_cids,this.getCollection(),[[this,i]])}}),e.makeRangeFilter=function(i,r,n){"undefined"==typeof n&&(n={});var s=t.map(r,function(t){return{low:t[0],high:t[1],value:t.join("-")}}),c=n.attr||i,o=t.extend({associated_attrs:[c]},n),a=new e.rangeFilter(i,s,o);return a},e.dvrangeFilter=e.Filter.extend({cacheResults:e.cacheMethods.exactCache,addCacheResults:e.cacheMethods.exactAddCache,getFn:function(i){if(!i[0]||!i[1])return new e.MatchSet([],this.getCollection(),[[this,i]]);var r,n,s,c,o;return r=t.indexOf(this.values,i[0]),n=t.indexOf(this.values,i[1]),s=this,c=t.map(this.values.slice(r,n+1),function(t){return s.possibilities[t]}),o=t.reduce(c,function(t,i){return e.unionSort(t,i.matching_cids)},[]),new e.MatchSet(o,this.getCollection(),[[this,i]])}}),e.makeDVrangeFilter=function(i,r,n){"undefined"==typeof n&&(n={});var s=t.map(r,function(t){return{value:t}}),c=n.attr||i,o=t.extend({associated_attrs:[c]},n),a=new e.dvrangeFilter(i,s,o);return a},e.continuousRangeFilter=e.Filter.extend({cacheResults:function(e){this.values=t.map(e,function(t){return{cid:t.cid,val:t[this.name]}},this),this.values.sort(function(t,e){return t.val-e.val})},addCacheResults:function(t){this.values=this.values.concat(t),this.values.sort(function(t,e){return t.val-e.val})},getFn:function(i){var r,n,s=this.values.length,c=e.bisectBy(function(t){return t.val});if(t.isArray(i)){if(t.isUndefined(i[0])||t.isUndefined(i[1]))return new e.MatchSet([],this.getCollection(),[[this,i]]);r=c.left(this.values,i[0],0,s),n=c.left(this.values,i[1],0,s)}else{if(t.isUndefined(i))return new e.MatchSet([],this.getCollection(),[[this,i]]);r=c.left(this.values,i,0,s),n=c.right(this.values,i,0,s)}for(var o=[],a=r;n>a;)o.push(this.values[a].cid),++a;return o.sort(function(t,e){return t-e}),new e.MatchSet(o,this.getCollection(),[[this,i]])}}),e.makeContinuousRangeFilter=function(i,r){"undefined"==typeof r&&(r={});var n=r.attr||i,s=t.extend({associated_attrs:[n]},r),c=new e.continuousRangeFilter(i,s);return c},e.explicitSort=e.Sort.extend({fn:function(e,i){var r=t.indexOf(this.order,e[this.attr]),n=t.indexOf(this.order,i[this.attr]);return-1===r&&(r=1/0),-1===n&&(n=1/0),r-n},reset:function(e){this.order=t.pluck(e,this.attr),this.rebuildSort()},insert:function(e,i){"undefined"==typeof i&&(i=this.order.length),t.isArray(e)||(e=[e]);var r=t.pluck(e,this.attr),n=[i,0].concat(r);this.order.splice.apply(this.order,n),this.rebuildSort()},remove:function(e){t.isArray(e)||(e=[e]);var i=t.pluck(e,this.attr);this.order=t.difference(this.order,i),this.rebuildSort()},move:function(e,i){t.isArray(e)||(e=[e]);var r=t.pluck(e,this.attr);this.order=t.map(this.order,function(e){return t.include(r,e)?null:e}),this.insert(e,i),this.order=t.compact(this.order)}}),e.makeExplicitSort=function(t,i,r,n,s){var c=new e.explicitSort(t,s);return c.associated_attrs=[r],c.order=n,c},e.reverseCidSort=e.Sort.extend({fn:function(t,e){return e.cid-t.cid}}),e.makeReverseCidSort=function(t,i){var r=new e.reverseCidSort(t);return r.associated_attrs=["cid"],r},e.UI.SimpleSelectElement=e.UI.Element.extend({initialize:function(t){if(!t.filter)throw"A simple select element must have a filter specified";this.filter=t.filter},getMatchSet:function(){return this.filter.current_query},getFilterState:function(){var t=this.getMatchSet();return this.getSimpleSelectState(t)}}),e.UI.SimpleDVRangeElement=e.UI.Element.extend({initialize:function(t){if(!t.filter)throw"A simple dv range element must have a filter specified";this.filter=t.filter},getMatchSet:function(){return this.filter.current_query},getFilterState:function(){var t=this.getMatchSet();return this.getSimpleRangeState(t)}}),e}();return e});