forked from EloquentStudio/StreamTable.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_table.min.js
13 lines (13 loc) · 9.09 KB
/
stream_table.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
* StreamTable.js
* version: 1.1.0 (28/7/2013)
*
* Licensed under the MIT:
* http://www.opensource.org/licenses/mit-license.php
*
* Copyright 2013 Jiren Patel[ [email protected] ]
*
* Dependency:
* jQuery(v1.8 >=)
*/
!function(window,$){"use strict";var StreamTable=function(t,e,a){return new _StreamTable(t,e,a)};StreamTable.VERSION="1.1.0",$.fn.stream_table=function(t,e){var a=$(this);a.data("st")||a.data("st",new _StreamTable(a.selector,t,e))},window.StreamTable=StreamTable;var _StreamTable=function(t,e,a){this.data=[],this.main_container=t,this.$container=$(t),this.opts=e,this.view=this.opts.view,this.text_index=[],this.last_search_result=[],this.last_search_text="",this.current_page=0,this.textFunc=null,this.stream_after=1e3*(this.opts.stream_after||2),this.timer=null,this.opts.callbacks=this.opts.callbacks||{},this.view||$.error("Add view function in options."),"TABLE"==this.$container.get(0).tagName&&(this.$container=this.$container.find("tbody")),this.initPagination(this.opts.pagination||{}),this.addSearchBox(),this.addPerPage(),this.has_sorting=$(this.main_container+" [data-sort]").length>0?!0:!1,this.has_sorting&&(this.sorting_opts={},this.records_index=[],this.last_search_record_index=[]),a&&(a=this.addData(a),this.render(0)),this.bindEvents(),this.bindSortingEvents(),this.streamData(this.stream_after)},_F=_StreamTable.prototype;_F.getIndex=function(){return this.last_search_text.length>0?this.last_search_record_index:this.records_index},_F.getData=function(){return this.last_search_text.length>0?this.last_search_result:this.data},_F.dataLength=function(){return this.has_sorting?this.getIndex().length:this.getData().length},_F.initPagination=function(t){this.paging_opts=$.extend({span:5,prev_text:"«",next_text:"«",per_page_select:!0,per_page_opts:[10,25,50]},t);var e=["st_pagination"];t.container_class&&(e=[].concat.apply(e,[t.container_class])),this.paging_opts.per_page=this.paging_opts.per_page_opts[0]||10,this.paging_opts.container_class=e.join(" "),this.paging_opts.ul_class=["pagination",t.ul_class].join(" "),this.paging_opts.per_page_class=["st_per_page",t.per_page_class].join(" "),this.opts.pagination=this.paging_opts,$(this.main_container).after('<div class="'+this.paging_opts.container_class+'"></div>'),this.$pagination=$("."+e.join("."))},_F.bindEvents=function(){var t=this,e=this.opts.search_box;$(e).on("keyup",function(){t.search($(this).val())}),$(e).on("keypress",function(t){return 13==t.keyCode?!1:void 0}),t.paging_opts.per_page_select&&$(t.paging_opts.per_page_select).on("change",function(){t.renderByPerPage($(this).val())}),t.$pagination.on("click","a",function(){var e,a=$(this),i=parseInt(a.text());return"NaN"==i.toString()&&(a.hasClass("prev")?i="prev":a.hasClass("next")?i="next":a.hasClass("first")?i=1:a.hasClass("last")&&(i=t.pageCount())),e=t.paginate(i),e>=0&&($(".st_pagination .active").removeClass("active"),$(".st_pagination li[data-page="+e+"]").addClass("active")),!1})},_F.addSearchBox=function(){this.opts.search_box||($(this.main_container).before('<input name="search" type="text" id="st_search" class="st_search" placeholder="Type here...">'),this.opts.search_box="#st_search")},_F._makeTextFunc=function(record){var fields=this.opts.fields,cond_str=[],textFunc,is_array=!1;if("Object"==record.constructor.name){fields=fields||Object.keys(record);for(var i=0,l=fields.length;l>i;i++)cond_str.push("d."+fields[i]);eval("textFunc = function(d) { return ("+cond_str.join(" + ' ' + ")+").toUpperCase(); }")}else if(fields){for(var i=0,l=fields.length;l>i;i++)cond_str.push("d["+fields[i]+"]");eval("textFunc = function(d) { return ("+cond_str.join(" + ' ' + ")+").toUpperCase(); }")}else textFunc=function(t){return t.join(" ").toUpperCase()};return textFunc},_F.buildTextIndex=function(t){var e=0,a=t.length;for(this.textFunc||(this.textFunc=this._makeTextFunc(t[0])),e;a>e;e++)this.text_index.push(this.textFunc(t[e]))},_F.render=function(t){var e=t*this.paging_opts.per_page,a=e+this.paging_opts.per_page,i=[],s=this.has_sorting?this.getIndex():this.getData();if(s.length<a&&(a=s.length),this.has_sorting)for(e;a>e;e++)i.push(this.view(this.data[s[e]],e+1));else for(e;a>e;e++)i.push(this.view(s[e],e+1));this.$container.html(i)},_F.search=function(t){var e=$.trim(t);e!=this.last_search_text&&(this.last_search_text=e,0==e.length?this.render(0):(this.searchInData(e),this.render(0)),this.current_page=0,this.renderPagination(this.pageCount(),this.current_page),this.execCallbacks("pagination"))},_F.searchInData=function(t){var e=[],a=0,i=this.text_index.length,s=t.toUpperCase();if(this.has_sorting?this.records_index:this.data,this.has_sorting){for(a;i>a;a++)-1!=this.text_index[a].indexOf(s)&&e.push(a);this.last_search_record_index=e}else{for(a;i>a;a++)-1!=this.text_index[a].indexOf(s)&&e.push(this.data[a]);this.last_search_result=e}},_F.addData=function(t){if(t=this.execCallbacks("before_add",t)||t,t.length){var e=this.data.length,a=t.length+e;if(this.buildTextIndex(t),this.data=this.data.concat(t),this.has_sorting)for(e;a>e;e++)this.records_index.push(e);this.last_search_text.length>0&&this.searchInData(this.last_search_text),this.opts.auto_sorting&&this.current_sorting&&this.sort(this.current_sorting),this.render(this.current_page),this.renderPagination(this.pageCount(),this.current_page),this.execCallbacks("after_add"),this.execCallbacks("pagination")}return t},_F.fetchData=function(){var t=this,e={q:this.last_search_text};this.opts.fetch_data_limit&&(e.limit=this.opts.fetch_data_limit,e.offset=this.data.length),$.getJSON(this.opts.data_url,e).done(function(a){a=t.addData(a),null==e.limit||a&&a.length?t.setStreamInterval():t.stopStreaming()}).fail(function(){t.stopStreaming()})},_F.setStreamInterval=function(){var t=this;1!=t.opts.stop_streaming&&(t.timer=setTimeout(function(){t.fetchData()},t.stream_after))},_F.stopStreaming=function(){this.opts.stop_streaming=!0,this.timer&&clearTimeout(this.timer)},_F.streamData=function(){if(this.opts.data_url){var t=this;t.setStreamInterval(),t.opts.fetch_data_limit||t.stopStreaming()}},_F.pageCount=function(){return Math.ceil(this.dataLength()/this.paging_opts.per_page)},_F.paginate=function(t){var e=this.pageCount();return"prev"==t?t=this.current_page-1:"next"==t?t=this.current_page+1:t-=1,t==this.current_page||0>t||t>=e?void 0:(this.render(t),this.current_page=t,this.paging_opts.span<=e&&this.renderPagination(e,this.current_page),this.execCallbacks("pagination"),this.current_page)},_F.renderPagination=function(t,e){var a=0,i=t,s=['<ul class="'+this.paging_opts.ul_class+'">'],n=this.paging_opts.span,r=Math.ceil(n/2);for(t>n&&(s.push('<li><a href="#" class="first">First</a></li>'),e>r-1&&(a=e-r),t-r-1>e&&(i=a+n)),s.push('<li><a href="#" class="prev">'+this.paging_opts.prev_text+"</a></li>"),a;i>a;a++)e==a?s.push('<li class="active" data-page="'+a+'"><a href="#" class="active" >'+(a+1)+"</a></li>"):s.push('<li data-page="'+a+'"><a href="#">'+(a+1)+"</a></li>");s.push('<li><a href="#" class="next">'+this.paging_opts.next_text+"</a></li>"),t>this.paging_opts.span&&s.push('<li><a href="#" class="last">Last</a></li>'),s.push("</ul>"),this.$pagination.html(s.join(""))},_F.addPerPage=function(){var t,e,a=this.paging_opts.per_page_select;if(a!==!1&&"string"!=typeof a){this.paging_opts.per_page_select=".st_per_page",t=['<select size="1" name="per_page" class="'+this.paging_opts.per_page_class+'">'],e=this.paging_opts.per_page_opts;for(var i=0,s=e.length;s>i;i++)t.push('<option value="'+e[i]+'">'+e[i]+"</option>");t.push("</select>"),$(this.main_container).before(t.join(""))}},_F.renderByPerPage=function(t){this.paging_opts.per_page!=t&&(this.paging_opts.per_page=parseInt(t),this.current_page=0,this.render(0),this.renderPagination(this.pageCount(),0),this.execCallbacks("pagination"))},_F.execCallbacks=function(t,e){var a=this.opts.callbacks[t];if(a){if("pagination"==t){var i=this.paging_opts.per_page*this.current_page;e={from:i+1,to:this.paging_opts.per_page+i,total:this.dataLength(),page:this.current_page},0==e.total&&(e.from=0),e.to>e.total&&(e.to=e.total)}return a.call(this,e)}},_F.bindSortingEvents=function(){var t=this;$(this.main_container+" [data-sort]").each(function(e){var a=$(this),i=a.data("sort").split(":"),s={dir:i[1]||"asc",type:i[2]||"string",field:i[0]};t.sorting_opts[s.field]={dir:s.dir,type:s.type,field:s.field},a.on("click",s,function(e){var a=$(this);a.addClass(e.data.dir),t.current_sorting={dir:e.data.dir,type:e.data.type,field:e.data.field},t.sort(e.data),t.render(t.current_page),e.data.dir="asc"==e.data.dir?"desc":"asc",$(this).removeClass(e.data.dir)}),0==e&&a.trigger("click")})},_F.sort=function(t){return t.order="asc"==t.dir?1:-1,this.getIndex().sort(this._sortingFunc(this.data,t))},_F._sortingFunc=function(t,e){var a=e.field,i=e.order,s=e.type;return"number"==s?function(e,s){return(t[e][a]-t[s][a])*i}:function(e,s){var n=t[e][a].toLowerCase(),r=t[s][a].toLowerCase();return r>n?-1*i:n>r?1*i:0}},StreamTable.extend=function(t,e){_StreamTable.prototype[t]=function(){return e.apply(this,arguments)}}}(this,window.jQuery),Array.prototype.indexOf||(Array.prototype.indexOf=function(t,e){for(var a=e||0,i=this.length;i>a;a++)if(this[a]===t)return a;return-1}),Object.keys||(Object.keys=function(t){var e,a=[];for(e in t)a.push(e);return a});