text: /*
   * StreamTable.js
   * version: 1.1.1 (17/12/2013)
   *
   * Licensed under the MIT:
   *   http://www.opensource.org/licenses/mit-license.php
   *
   * Copyright 2013 Jiren Patel[ jiren@joshsoftware.com ]
   *
   * Dependency:
   *  jQuery(v1.8 >=)
   */
  !function(window,$){"use strict";var StreamTable=function(t,e,i){return new _StreamTable(t,e,i)};StreamTable.VERSION="1.1.0",$.fn.stream_table=function(t,e){var i=$(this);i.data("st")||i.data("st",new _StreamTable(i.selector,t,e))},window.StreamTable=StreamTable;var _StreamTable=function(t,e,i){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=[]),i&&(i=this.addData(i),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:"&laquo;",next_text:"&laquo;",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;var i='<div class="'+this.paging_opts.container_class+'"></div>';this.paging_opts.container?$(this.paging_opts.container).html(i):$(this.main_container).after(i),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,i=$(this),s=parseInt(i.text());return"NaN"==s.toString()&&(i.hasClass("prev")?s="prev":i.hasClass("next")?s="next":i.hasClass("first")?s=1:i.hasClass("last")&&(s=t.pageCount())),e=t.paginate(s),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="Search here...">'),this.opts.search_box="#st_search")},_F._makeTextFunc=function(record){var fields=this.opts.fields,cond_str=[],textFunc,is_array=!1;if("function"==typeof fields)textFunc=fields;else if(record.constructor==Object){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(" + ' ' + ")+"); }")}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(" + ' ' + ")+"); }")}else textFunc=function(t){return t.join(" ")};return textFunc},_F.buildTextIndex=function(t){var e=0,i=t.length;for(this.textFunc||(this.textFunc=this._makeTextFunc(t[0])),e;i>e;e++)this.text_index.push(this.textFunc(t[e]).toUpperCase())},_F.render=function(t){var e=t*this.paging_opts.per_page,i=e+this.paging_opts.per_page,s=[],a=this.has_sorting?this.getIndex():this.getData();if(a.length<i&&(i=a.length),this.has_sorting)for(e;i>e;e++)s.push(this.view(this.data[a[e]],e+1));else for(e;i>e;e++)s.push(this.view(a[e],e+1));this.$container.html(s)},_F.clearAndBuildTextIndex=function(t){this.text_index=[],this.buildTextIndex(t)},_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=[],i=0,s=this.text_index.length,a=t.toUpperCase();this.has_sorting?this.records_index:this.data}if(this.has_sorting){for(i;s>i;i++)-1!=this.text_index[i].indexOf(a)&&e.push(i);this.last_search_record_index=e}else{for(i;s>i;i++)-1!=this.text_index[i].indexOf(a)&&e.push(this.data[i]);this.last_search_result=e}},_F.addData=function(t){if(t=this.execCallbacks("before_add",t)||t,t.length){var e=this.data.length,i=t.length+e;if(this.buildTextIndex(t),this.data=this.data.concat(t),this.has_sorting)for(e;i>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",t),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(i){i=t.addData(i),null==e.limit||i&&i.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 i=0,s=t,a=['<ul class="'+this.paging_opts.ul_class+'">'],n=this.paging_opts.span,r=Math.ceil(n/2);for(t>n&&(a.push('<li><a href="#" class="first">First</a></li>'),e>r-1&&(i=e-r),t-r-1>e&&(s=i+n)),a.push('<li><a href="#" class="prev">'+this.paging_opts.prev_text+"</a></li>"),i;s>i;i++)e==i?a.push('<li class="active" data-page="'+i+'"><a href="#" class="active" >'+(i+1)+"</a></li>"):a.push('<li  data-page="'+i+'"><a href="#">'+(i+1)+"</a></li>");a.push('<li><a href="#" class="next">'+this.paging_opts.next_text+"</a></li>"),t>this.paging_opts.span&&a.push('<li><a href="#" class="last">Last</a></li>'),a.push("</ul>"),this.$pagination.html(a.join(""))},_F.addPerPage=function(){var t,e,i=this.paging_opts.per_page_select;if(i!==!1&&"string"!=typeof i){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 s=0,a=e.length;a>s;s++)t.push('<option value="'+e[s]+'">'+e[s]+"</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 i=this.opts.callbacks[t];if(i){if("pagination"==t){var s=this.paging_opts.per_page*this.current_page;e={from:s+1,to:this.paging_opts.per_page+s,total:this.dataLength(),page:this.current_page},0==e.total&&(e.from=0),e.to>e.total&&(e.to=e.total)}return i.call(this,e)}},_F.bindSortingEvents=function(){var t=this;$(this.main_container+" [data-sort]").each(function(e){var i=$(this),s=i.data("sort").split(":"),a={dir:s[1]||"asc",type:s[2]||"string",field:s[0]};t.sorting_opts[a.field]={dir:a.dir,type:a.type,field:a.field},i.on("click",a,function(e){var i=$(this);i.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),t.opts.callbacks.after_sort&&t.execCallbacks("after_sort")}),0==e&&t.opts.auto_sorting&&i.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 i=e.field,s=e.order,a=e.type;return"number"==a?function(e,a){return(t[e][i]-t[a][i])*s}:function(e,a){var n=t[e][i].toLowerCase(),r=t[a][i].toLowerCase();return r>n?-1*s:n>r?1*s:0}},_F.clear=function(){this.opts.search_box&&$(this.opts.search_box).html(""),$(this.main_container).html("")},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 i=e||0,s=this.length;s>i;i++)if(this[i]===t)return i;return-1}),Object.keys||(Object.keys=function(t){var e,i=[];for(e in t)i.push(e);return i});