diff --git a/app/assets/javascripts/light/StreamTable.js b/app/assets/javascripts/light/StreamTable.js deleted file mode 100644 index bc334d5..0000000 --- a/app/assets/javascripts/light/StreamTable.js +++ /dev/null @@ -1,545 +0,0 @@ -/* - * 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(container, opts, data) { - return new _StreamTable(container, opts, data); - }; - - StreamTable.VERSION = '1.1.0'; - - $.fn.stream_table = function (opts, data) { - var $this = $(this); - if ($this.data('st')) return; - $this.data('st', new _StreamTable($this.selector, opts, data)); - }; - - window.StreamTable = StreamTable; - - var _StreamTable = function(container, opts, data) { - this.data = []; - this.main_container = container; - this.$container = $(container); - this.opts = opts; - 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 = (this.opts.stream_after || 2)*1000; - this.timer = null; - this.opts.callbacks = this.opts.callbacks || {}; - - if (!this.view) $.error('Add view function in options.'); - - if (this.$container.get(0).tagName == 'TABLE') 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 ? true : false; - - if (this.has_sorting) { - this.sorting_opts = {}; - this.records_index = []; - this.last_search_record_index = []; - } - - if (data) { - data = this.addData(data); - this.render(0); - } - - this.bindEvents(); - this.bindSortingEvents(); - this.streamData(this.stream_after); - } - - var _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(opts){ - this.paging_opts = $.extend({ - span: 5, - prev_text: '«', - next_text: '«', - per_page_select: true, - per_page_opts: [10,25,50] - }, opts); - - var p_classes = ['st_pagination']; - - if (opts.container_class){ - p_classes = [].concat.apply(p_classes, [opts.container_class]) - } - - this.paging_opts.per_page = this.paging_opts.per_page_opts[0] || 10; - this.paging_opts.container_class = p_classes.join(' '); - this.paging_opts.ul_class = ['pagination', opts.ul_class].join(' '); - this.paging_opts.per_page_class = ['st_per_page', opts.per_page_class].join(' '); - this.opts.pagination = this.paging_opts; - - var html = '
'; - - if(this.paging_opts.container){ - $(this.paging_opts.container).html(html); - }else{ - $(this.main_container).after(html); - } - - this.$pagination = $('.' + p_classes.join('.')); - }; - - _F.bindEvents = function(){ - var _self = this, - search_box = this.opts.search_box; - - $(search_box).on('keyup', function(e){ - _self.search($(this).val()); - }); - - $(search_box).on('keypress', function(e){ - if ( e.keyCode == 13 ) return false; - }); - - if (_self.paging_opts.per_page_select){ - $(_self.paging_opts.per_page_select).on('change', function(){ - _self.renderByPerPage($(this).val()); - }); - } - - _self.$pagination.on('click', 'a', function(e){ - var $this = $(this), page = parseInt($this.text()), current_page; - - if (page.toString() == 'NaN'){ - if ($this.hasClass('prev')) page = 'prev'; - else if ($this.hasClass('next')) page = 'next'; - else if ($this.hasClass('first')) page = 1; - else if ($this.hasClass('last')) page = _self.pageCount(); - } - - current_page = _self.paginate(page); - if (current_page >= 0) { - $('.st_pagination .active').removeClass('active'); - $('.st_pagination li[data-page='+ current_page +']').addClass('active'); - } - - return false; - }); - - }; - - _F.addSearchBox = function(){ - if (this.opts.search_box) return; - $(this.main_container).before(''); - this.opts.search_box = '#st_search'; - }; - - _F._makeTextFunc = function(record){ - var fields = this.opts.fields, cond_str = [], textFunc, is_array = false; - - if(typeof fields == 'function'){ - textFunc = fields; - } else if (record.constructor == Object){ - fields = fields || Object.keys(record) - - for (var i = 0, l = fields.length; i < l; 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; i < l ; i++){ - cond_str.push("d["+ fields[i] + "]"); - } - eval("textFunc = function(d) { return (" + cond_str.join(" + ' ' + ") + "); }"); - }else{ - textFunc = function(d) { - return d.join(' '); - } - } - } - - return textFunc; - }; - - _F.buildTextIndex = function(data){ - var i = 0, l = data.length; - - if (!this.textFunc) { - this.textFunc = this._makeTextFunc(data[0]); - } - - for(i; i < l; i++){ - this.text_index.push(this.textFunc(data[i]).toUpperCase()); - } - }; - - _F.render = function(page){ - var i = (page * this.paging_opts.per_page), - l = (i + this.paging_opts.per_page), - eles = [], - index, - d = this.has_sorting ? this.getIndex() : this.getData(); - - if (d.length < l) l = d.length; - - if (this.has_sorting){ - for (i; i < l; i++){ - eles.push(this.view(this.data[d[i]], (i+1))); - } - }else{ - for (i; i < l; i++){ - eles.push(this.view(d[i], (i+1))); - } - } - - this.$container.html(eles); - }; - - _F.clearAndBuildTextIndex = function(data){ - this.text_index = [] - this.buildTextIndex(data) - }; - - _F.search = function(text){ - var q = $.trim(text), count = 0; - - if (q == this.last_search_text) return; - - this.last_search_text = q; - - if(q.length == 0 ){ - this.render(0); - }else{ - this.searchInData(q); - this.render(0); - } - - this.current_page = 0; - this.renderPagination(this.pageCount(), this.current_page); - this.execCallbacks('pagination'); - }; - - _F.searchInData = function(text){ - var result = [], - i = 0, - l = this.text_index.length, - t = text.toUpperCase(), - d = this.has_sorting ? this.records_index : this.data; - - if(this.has_sorting){ - for (i; i < l; i++){ - if (this.text_index[i].indexOf(t) != -1) result.push(i); - } - this.last_search_record_index = result - }else{ - for (i; i < l; i++){ - if (this.text_index[i].indexOf(t) != -1) result.push(this.data[i]); - } - this.last_search_result = result - } - - }; - - _F.addData = function(data){ - data = this.execCallbacks('before_add', data) || data; - - if (data.length){ - var i = this.data.length, l = data.length + i; - - this.buildTextIndex(data); - this.data = this.data.concat(data); - - if(this.has_sorting){ - for(i; i < l; i++){ - this.records_index.push(i); - } - } - - if (this.last_search_text.length > 0){ - this.searchInData(this.last_search_text); - } - - if (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', data); - this.execCallbacks('pagination'); - } - - return data; - }; - - _F.fetchData = function(){ - var _self = this, params = {q: this.last_search_text} - - if (this.opts.fetch_data_limit) { - params['limit'] = this.opts.fetch_data_limit; - params['offset'] = this.data.length; - } - - $.getJSON(this.opts.data_url, params).done(function(data){ - data = _self.addData(data); - - if (params.limit != null && (!data || !data.length ) ) { - _self.stopStreaming(); - }else{ - _self.setStreamInterval(); - } - - }).fail(function(e){ - _self.stopStreaming(); - }); - }; - - _F.setStreamInterval = function(){ - var _self = this; - if(_self.opts.stop_streaming == true) return; - - _self.timer = setTimeout(function(){ - _self.fetchData(); - }, _self.stream_after); - }; - - _F.stopStreaming = function(){ - this.opts.stop_streaming = true; - if (this.timer) clearTimeout(this.timer); - }; - - _F.streamData = function(time){ - if (!this.opts.data_url) return; - var _self = this, timer; - - _self.setStreamInterval(); - - if(!_self.opts.fetch_data_limit) _self.stopStreaming(); - }; - - _F.pageCount = function(){ - return Math.ceil(this.dataLength()/this.paging_opts.per_page); - }; - - //Render table rows for given page - _F.paginate = function(page){ - var page_count = this.pageCount(); - - if(page == 'prev'){ - page = this.current_page - 1; - }else if (page == 'next'){ - page = this.current_page + 1; - }else { - page = page - 1; - } - - if (page == this.current_page || page < 0 || page >= page_count) return; - - this.render(page); - this.current_page = page; - - if (this.paging_opts.span <= page_count) this.renderPagination(page_count, this.current_page); - - this.execCallbacks('pagination'); - - return this.current_page; - }; - - // Render Pagination call after new data added or search - _F.renderPagination = function(page_count, current_page){ - var i = 0, - l = page_count, - links = [ '