2016-05-14 12:04:00 +02:00
|
|
|
import {Feature} from '../feature';
|
2016-05-25 09:31:53 +02:00
|
|
|
import {createElm, createOpt, createText, elm, removeElm} from '../dom';
|
2016-05-15 04:56:12 +02:00
|
|
|
import {isArray, isFn, isNull} from '../types';
|
2016-06-02 06:13:56 +02:00
|
|
|
import {addEvt, keyCode, removeEvt} from '../event';
|
2016-05-08 03:48:51 +02:00
|
|
|
import {INPUT, SELECT, NONE, ENTER_KEY} from '../const';
|
2014-12-16 04:21:27 +01:00
|
|
|
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Paging UI component
|
|
|
|
* @export
|
|
|
|
* @class Paging
|
|
|
|
* @extends {Feature}
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
export class Paging extends Feature {
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2014-12-16 04:21:27 +01:00
|
|
|
/**
|
2016-07-31 13:49:38 +02:00
|
|
|
* Creates an instance of Paging
|
|
|
|
* @param {TableFilter} tf TableFilter instance
|
2014-12-16 04:21:27 +01:00
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
constructor(tf) {
|
2015-11-14 16:14:13 +01:00
|
|
|
super(tf, 'paging');
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
// Configuration object
|
2015-11-14 16:14:13 +01:00
|
|
|
var f = this.config;
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Css class for the paging buttons (previous, next, etc.)
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2015-01-26 10:31:25 +01:00
|
|
|
this.btnPageCssClass = f.paging_btn_css_class || 'pgInp';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main select DOM element
|
|
|
|
* @type {DOMElement}
|
|
|
|
*/
|
2015-01-26 10:31:25 +01:00
|
|
|
this.pagingSlc = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Results per page select DOM element
|
|
|
|
* @type {DOMElement}
|
|
|
|
*/
|
2015-01-26 10:31:25 +01:00
|
|
|
this.resultsPerPageSlc = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of custom container element
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.pagingTgtId = f.paging_target_id || null;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of rows contained in a page
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.pagingLength = !isNaN(f.paging_length) ? f.paging_length : 10;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of custom container element for the results per page selector
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.resultsPerPageTgtId = f.results_per_page_target_id || null;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for the paging select element
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.pgSlcCssClass = f.paging_slc_css_class || 'pgSlc';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for the paging input element
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.pgInpCssClass = f.paging_inp_css_class || 'pgNbInp';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Label and values for the results per page select, example of usage:
|
|
|
|
* ['Records: ', [10,25,50,100]]
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
2015-01-26 10:31:25 +01:00
|
|
|
this.resultsPerPage = f.results_per_page || null;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if results per page is configured
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-05-15 04:56:12 +02:00
|
|
|
this.hasResultsPerPage = isArray(this.resultsPerPage);
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for the results per page select
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.resultsSlcCssClass = f.results_slc_css_class || 'rspg';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for the label preceding results per page select
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.resultsSpanCssClass = f.results_span_css_class || 'rspgSpan';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Index of the first row of current page
|
|
|
|
* @type {Number}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.startPagingRow = 0;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Total number of pages
|
|
|
|
* @type {Number}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.nbPages = 0;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current page number
|
|
|
|
* @type {Number}
|
|
|
|
* @private
|
|
|
|
*/
|
2015-01-26 10:31:25 +01:00
|
|
|
this.currentPageNb = 1;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Next page button text
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.btnNextPageText = f.btn_next_page_text || '>';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Previous page button text
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.btnPrevPageText = f.btn_prev_page_text || '<';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Last page button text
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.btnLastPageText = f.btn_last_page_text || '>|';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* First page button text
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.btnFirstPageText = f.btn_first_page_text || '|<';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Next page button HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.btnNextPageHtml = f.btn_next_page_html ||
|
2015-01-26 10:31:25 +01:00
|
|
|
(!tf.enableIcons ? null :
|
2016-04-30 03:45:04 +02:00
|
|
|
'<input type="button" value="" class="' + this.btnPageCssClass +
|
|
|
|
' nextPage" title="Next page" />');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Previous page button HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.btnPrevPageHtml = f.btn_prev_page_html ||
|
2015-01-26 10:31:25 +01:00
|
|
|
(!tf.enableIcons ? null :
|
2016-04-30 03:45:04 +02:00
|
|
|
'<input type="button" value="" class="' + this.btnPageCssClass +
|
|
|
|
' previousPage" title="Previous page" />');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* First page button HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.btnFirstPageHtml = f.btn_first_page_html ||
|
2015-01-26 10:31:25 +01:00
|
|
|
(!tf.enableIcons ? null :
|
2016-04-30 03:45:04 +02:00
|
|
|
'<input type="button" value="" class="' + this.btnPageCssClass +
|
|
|
|
' firstPage" title="First page" />');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Last page button HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.btnLastPageHtml = f.btn_last_page_html ||
|
2015-01-26 10:31:25 +01:00
|
|
|
(!tf.enableIcons ? null :
|
2016-04-30 03:45:04 +02:00
|
|
|
'<input type="button" value="" class="' + this.btnPageCssClass +
|
|
|
|
' lastPage" title="Last page" />');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text preceeding page selector drop-down
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.pageText = f.page_text || ' Page ';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text after page selector drop-down
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.ofText = f.of_text || ' of ';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for the span containing total number of pages
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2014-12-16 04:21:27 +01:00
|
|
|
this.nbPgSpanCssClass = f.nb_pages_css_class || 'nbpg';
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if paging buttons are enabled (default: true)
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
this.hasPagingBtns = f.paging_btns === false ? false : true;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines page selector type, two possible values: 'select', 'input'
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2016-05-07 13:59:42 +02:00
|
|
|
this.pageSelectorType = f.page_selector_type || SELECT;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired before the page is changed
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2016-05-15 04:56:12 +02:00
|
|
|
this.onBeforeChangePage = isFn(f.on_before_change_page) ?
|
2014-12-16 04:21:27 +01:00
|
|
|
f.on_before_change_page : null;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired after the page is changed
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2016-05-15 04:56:12 +02:00
|
|
|
this.onAfterChangePage = isFn(f.on_after_change_page) ?
|
2014-12-16 04:21:27 +01:00
|
|
|
f.on_after_change_page : null;
|
2015-04-12 09:52:19 +02:00
|
|
|
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Label preciding results per page select
|
2016-11-05 12:08:32 +01:00
|
|
|
* @type {DOMElement}
|
2016-07-31 13:49:38 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-11-05 12:08:32 +01:00
|
|
|
this.slcResultsTxt = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Span containing next page button
|
2016-11-05 12:08:32 +01:00
|
|
|
* @type {DOMElement}
|
2016-07-31 13:49:38 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-11-05 12:08:32 +01:00
|
|
|
this.btnNextCont = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Span containing previous page button
|
2016-11-05 12:08:32 +01:00
|
|
|
* @type {DOMElement}
|
2016-07-31 13:49:38 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-11-05 12:08:32 +01:00
|
|
|
this.btnPrevCont = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Span containing last page button
|
2016-11-05 12:08:32 +01:00
|
|
|
* @type {DOMElement}
|
2016-07-31 13:49:38 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-11-05 12:08:32 +01:00
|
|
|
this.btnLastCont = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Span containing first page button
|
2016-11-05 12:08:32 +01:00
|
|
|
* @type {DOMElement}
|
2016-07-31 13:49:38 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-11-05 12:08:32 +01:00
|
|
|
this.btnFirstCont = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Span for tot nb pages
|
2016-11-05 12:08:32 +01:00
|
|
|
* @type {DOMElement}
|
2016-07-31 13:49:38 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-11-05 12:08:32 +01:00
|
|
|
this.pgCont = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Span preceding pages select (contains 'Page')
|
2016-11-05 12:08:32 +01:00
|
|
|
* @type {DOMElement}
|
2016-07-31 13:49:38 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-11-05 12:08:32 +01:00
|
|
|
this.pgBefore = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Span following pages select (contains ' of ')
|
2016-11-05 12:08:32 +01:00
|
|
|
* @type {DOMElement}
|
2016-07-31 13:49:38 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-11-05 12:08:32 +01:00
|
|
|
this.pgAfter = null;
|
2015-04-12 09:52:19 +02:00
|
|
|
|
2016-01-02 15:33:31 +01:00
|
|
|
var start_row = tf.refRow;
|
2016-05-19 09:38:18 +02:00
|
|
|
var nrows = tf.getRowsNb(true);
|
2014-12-16 04:21:27 +01:00
|
|
|
//calculates page nb
|
2016-04-30 03:45:04 +02:00
|
|
|
this.nbPages = Math.ceil((nrows - start_row) / this.pagingLength);
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
var o = this;
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Paging DOM events handlers
|
|
|
|
* @type {String}
|
|
|
|
* @private
|
|
|
|
*/
|
2015-01-26 10:31:25 +01:00
|
|
|
this.evt = {
|
2016-04-30 03:45:04 +02:00
|
|
|
slcIndex() {
|
2016-05-07 13:59:42 +02:00
|
|
|
return (o.pageSelectorType === SELECT) ?
|
2015-01-26 10:31:25 +01:00
|
|
|
o.pagingSlc.options.selectedIndex :
|
2016-04-30 03:45:04 +02:00
|
|
|
parseInt(o.pagingSlc.value, 10) - 1;
|
2015-01-26 10:31:25 +01:00
|
|
|
},
|
2016-04-30 03:45:04 +02:00
|
|
|
nbOpts() {
|
2016-05-07 13:59:42 +02:00
|
|
|
return (o.pageSelectorType === SELECT) ?
|
2016-04-30 03:45:04 +02:00
|
|
|
parseInt(o.pagingSlc.options.length, 10) - 1 :
|
|
|
|
(o.nbPages - 1);
|
2015-01-26 10:31:25 +01:00
|
|
|
},
|
2016-04-30 03:45:04 +02:00
|
|
|
next() {
|
2015-01-26 10:31:25 +01:00
|
|
|
var nextIndex = o.evt.slcIndex() < o.evt.nbOpts() ?
|
2016-04-30 03:45:04 +02:00
|
|
|
o.evt.slcIndex() + 1 : 0;
|
2015-01-26 10:31:25 +01:00
|
|
|
o.changePage(nextIndex);
|
|
|
|
},
|
2016-04-30 03:45:04 +02:00
|
|
|
prev() {
|
|
|
|
var prevIndex = o.evt.slcIndex() > 0 ?
|
|
|
|
o.evt.slcIndex() - 1 : o.evt.nbOpts();
|
2015-01-26 10:31:25 +01:00
|
|
|
o.changePage(prevIndex);
|
|
|
|
},
|
2016-04-30 03:45:04 +02:00
|
|
|
last() {
|
2015-01-26 10:31:25 +01:00
|
|
|
o.changePage(o.evt.nbOpts());
|
|
|
|
},
|
2016-04-30 03:45:04 +02:00
|
|
|
first() {
|
2015-01-26 10:31:25 +01:00
|
|
|
o.changePage(0);
|
|
|
|
},
|
2016-04-30 03:45:04 +02:00
|
|
|
_detectKey(e) {
|
2016-06-02 06:13:56 +02:00
|
|
|
var key = keyCode(e);
|
2016-05-08 03:48:51 +02:00
|
|
|
if (key === ENTER_KEY) {
|
2016-04-30 03:45:04 +02:00
|
|
|
if (tf.sorted) {
|
2015-02-21 04:39:09 +01:00
|
|
|
tf.filter();
|
2015-01-26 10:31:25 +01:00
|
|
|
o.changePage(o.evt.slcIndex());
|
2016-04-30 03:45:04 +02:00
|
|
|
} else {
|
2015-01-26 10:31:25 +01:00
|
|
|
o.changePage();
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
2015-01-26 10:31:25 +01:00
|
|
|
this.blur();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
slcPagesChange: null,
|
|
|
|
nextEvt: null,
|
|
|
|
prevEvt: null,
|
|
|
|
lastEvt: null,
|
|
|
|
firstEvt: null
|
|
|
|
};
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
|
|
|
* Initialize DOM elements
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
init() {
|
2014-12-16 04:21:27 +01:00
|
|
|
var slcPages;
|
|
|
|
var tf = this.tf;
|
2015-01-26 10:31:25 +01:00
|
|
|
var evt = this.evt;
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.initialized) {
|
2015-11-14 16:14:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
// Check resultsPerPage is in expected format and initialise the
|
|
|
|
// results per page component
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.hasResultsPerPage) {
|
|
|
|
if (this.resultsPerPage.length < 2) {
|
2015-01-26 10:31:25 +01:00
|
|
|
this.hasResultsPerPage = false;
|
|
|
|
} else {
|
|
|
|
this.pagingLength = this.resultsPerPage[1][0];
|
|
|
|
this.setResultsPerPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
evt.slcPagesChange = (event) => {
|
2015-07-14 15:16:05 +02:00
|
|
|
var slc = event.target;
|
|
|
|
this.changePage(slc.selectedIndex);
|
2015-01-26 10:31:25 +01:00
|
|
|
};
|
2014-12-16 04:21:27 +01:00
|
|
|
|
|
|
|
// Paging drop-down list selector
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === SELECT) {
|
2016-11-05 12:08:32 +01:00
|
|
|
slcPages = createElm(SELECT);
|
2014-12-16 04:21:27 +01:00
|
|
|
slcPages.className = this.pgSlcCssClass;
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(slcPages, 'change', evt.slcPagesChange);
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Paging input selector
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === INPUT) {
|
2016-11-05 12:08:32 +01:00
|
|
|
slcPages = createElm(INPUT, ['value', this.currentPageNb]);
|
2014-12-16 04:21:27 +01:00
|
|
|
slcPages.className = this.pgInpCssClass;
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(slcPages, 'keypress', evt._detectKey);
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// btns containers
|
2016-11-05 12:08:32 +01:00
|
|
|
var btnNextSpan = createElm('span');
|
|
|
|
var btnPrevSpan = createElm('span');
|
|
|
|
var btnLastSpan = createElm('span');
|
|
|
|
var btnFirstSpan = createElm('span');
|
2014-12-16 04:21:27 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.hasPagingBtns) {
|
2014-12-16 04:21:27 +01:00
|
|
|
// Next button
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.btnNextPageHtml) {
|
2016-11-05 12:08:32 +01:00
|
|
|
var btnNext = createElm(INPUT,
|
2015-01-11 11:22:52 +01:00
|
|
|
['type', 'button'],
|
|
|
|
['value', this.btnNextPageText],
|
|
|
|
['title', 'Next']
|
2014-12-16 04:21:27 +01:00
|
|
|
);
|
2016-11-05 12:08:32 +01:00
|
|
|
btnNext.className = this.btnPageCssClass;
|
|
|
|
addEvt(btnNext, 'click', evt.next);
|
|
|
|
btnNextSpan.appendChild(btnNext);
|
2014-12-16 04:21:27 +01:00
|
|
|
} else {
|
|
|
|
btnNextSpan.innerHTML = this.btnNextPageHtml;
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(btnNextSpan, 'click', evt.next);
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
|
|
|
// Previous button
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.btnPrevPageHtml) {
|
2016-11-05 12:08:32 +01:00
|
|
|
var btnPrev = createElm(INPUT,
|
2015-04-12 09:52:19 +02:00
|
|
|
['type', 'button'],
|
|
|
|
['value', this.btnPrevPageText],
|
|
|
|
['title', 'Previous']
|
2014-12-16 04:21:27 +01:00
|
|
|
);
|
2016-11-05 12:08:32 +01:00
|
|
|
btnPrev.className = this.btnPageCssClass;
|
|
|
|
addEvt(btnPrev, 'click', evt.prev);
|
|
|
|
btnPrevSpan.appendChild(btnPrev);
|
2014-12-16 04:21:27 +01:00
|
|
|
} else {
|
|
|
|
btnPrevSpan.innerHTML = this.btnPrevPageHtml;
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(btnPrevSpan, 'click', evt.prev);
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
|
|
|
// Last button
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.btnLastPageHtml) {
|
2016-11-05 12:08:32 +01:00
|
|
|
var btnLast = createElm(INPUT,
|
2015-04-12 09:52:19 +02:00
|
|
|
['type', 'button'],
|
|
|
|
['value', this.btnLastPageText],
|
|
|
|
['title', 'Last']
|
2014-12-16 04:21:27 +01:00
|
|
|
);
|
2016-11-05 12:08:32 +01:00
|
|
|
btnLast.className = this.btnPageCssClass;
|
|
|
|
addEvt(btnLast, 'click', evt.last);
|
|
|
|
btnLastSpan.appendChild(btnLast);
|
2014-12-16 04:21:27 +01:00
|
|
|
} else {
|
|
|
|
btnLastSpan.innerHTML = this.btnLastPageHtml;
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(btnLastSpan, 'click', evt.last);
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
|
|
|
// First button
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.btnFirstPageHtml) {
|
2016-11-05 12:08:32 +01:00
|
|
|
var btnFirst = createElm(INPUT,
|
2015-04-12 09:52:19 +02:00
|
|
|
['type', 'button'],
|
|
|
|
['value', this.btnFirstPageText],
|
|
|
|
['title', 'First']
|
2014-12-16 04:21:27 +01:00
|
|
|
);
|
2016-11-05 12:08:32 +01:00
|
|
|
btnFirst.className = this.btnPageCssClass;
|
|
|
|
addEvt(btnFirst, 'click', evt.first);
|
|
|
|
btnFirstSpan.appendChild(btnFirst);
|
2014-12-16 04:21:27 +01:00
|
|
|
} else {
|
|
|
|
btnFirstSpan.innerHTML = this.btnFirstPageHtml;
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(btnFirstSpan, 'click', evt.first);
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// paging elements (buttons+drop-down list) are added to defined element
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.pagingTgtId) {
|
2015-02-21 04:39:09 +01:00
|
|
|
tf.setToolbar();
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
2016-05-25 09:31:53 +02:00
|
|
|
var targetEl = !this.pagingTgtId ? tf.mDiv : elm(this.pagingTgtId);
|
2014-12-16 04:21:27 +01:00
|
|
|
targetEl.appendChild(btnFirstSpan);
|
|
|
|
targetEl.appendChild(btnPrevSpan);
|
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
var pgBeforeSpan = createElm('span');
|
2016-05-24 10:42:11 +02:00
|
|
|
pgBeforeSpan.appendChild(createText(this.pageText));
|
2014-12-16 04:21:27 +01:00
|
|
|
pgBeforeSpan.className = this.nbPgSpanCssClass;
|
|
|
|
targetEl.appendChild(pgBeforeSpan);
|
|
|
|
targetEl.appendChild(slcPages);
|
2016-11-05 12:08:32 +01:00
|
|
|
var pgAfterSpan = createElm('span');
|
2016-05-24 10:42:11 +02:00
|
|
|
pgAfterSpan.appendChild(createText(this.ofText));
|
2014-12-16 04:21:27 +01:00
|
|
|
pgAfterSpan.className = this.nbPgSpanCssClass;
|
|
|
|
targetEl.appendChild(pgAfterSpan);
|
2016-11-05 12:08:32 +01:00
|
|
|
var pgSpan = createElm('span');
|
|
|
|
pgSpan.className = this.nbPgSpanCssClass;
|
|
|
|
pgSpan.appendChild(createText(' ' + this.nbPages + ' '));
|
|
|
|
targetEl.appendChild(pgSpan);
|
2014-12-16 04:21:27 +01:00
|
|
|
targetEl.appendChild(btnNextSpan);
|
|
|
|
targetEl.appendChild(btnLastSpan);
|
2016-11-05 12:08:32 +01:00
|
|
|
|
|
|
|
this.btnNextCont = btnNextSpan;
|
|
|
|
this.btnPrevCont = btnPrevSpan;
|
|
|
|
this.btnLastCont = btnLastSpan;
|
|
|
|
this.btnFirstCont = btnFirstSpan;
|
|
|
|
this.pgCont = pgSpan;
|
|
|
|
this.pgBefore = pgBeforeSpan;
|
|
|
|
this.pgAfter = pgAfterSpan;
|
|
|
|
this.pagingSlc = slcPages;
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
this.setPagingInfo();
|
|
|
|
|
|
|
|
if (!tf.fltGrid) {
|
2015-06-04 15:04:38 +02:00
|
|
|
tf.validateAllRows();
|
2015-01-11 11:22:52 +01:00
|
|
|
this.setPagingInfo(tf.validRowsIndex);
|
|
|
|
}
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
this.emitter.on(['after-filtering'], () => this.resetPagingInfo());
|
2016-03-18 07:58:32 +01:00
|
|
|
this.emitter.on(['change-page'],
|
|
|
|
(tf, pageNumber) => this.setPage(pageNumber));
|
|
|
|
this.emitter.on(['change-page-results'],
|
|
|
|
(tf, pageLength) => this.changeResultsPerPage(pageLength));
|
2015-12-28 07:53:03 +01:00
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
/** @inherited */
|
2015-11-14 16:14:13 +01:00
|
|
|
this.initialized = true;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
2015-06-13 11:03:33 +02:00
|
|
|
* Reset paging when filters are already instantiated
|
2015-01-26 10:31:25 +01:00
|
|
|
* @param {Boolean} filterTable Execute filtering once paging instanciated
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
reset(filterTable = false) {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.isEnabled()) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-11-14 16:14:13 +01:00
|
|
|
this.enable();
|
2015-01-11 11:22:52 +01:00
|
|
|
this.init();
|
2016-02-06 04:08:46 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (filterTable) {
|
2015-02-21 04:39:09 +01:00
|
|
|
tf.filter();
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 07:53:03 +01:00
|
|
|
/**
|
|
|
|
* Reset paging info from scratch after a filtering process
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
resetPagingInfo() {
|
2015-12-28 07:53:03 +01:00
|
|
|
this.startPagingRow = 0;
|
|
|
|
this.currentPageNb = 1;
|
|
|
|
this.setPagingInfo(this.tf.validRowsIndex);
|
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
|
|
|
* Calculate number of pages based on valid rows
|
|
|
|
* Refresh paging select according to number of pages
|
|
|
|
* @param {Array} validRows Collection of valid rows
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
setPagingInfo(validRows) {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
2016-05-25 09:31:53 +02:00
|
|
|
var mdiv = !this.pagingTgtId ? tf.mDiv : elm(this.pagingTgtId);
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2015-06-13 11:03:33 +02:00
|
|
|
//store valid rows indexes
|
2016-01-02 04:43:00 +01:00
|
|
|
tf.validRowsIndex = validRows || tf.getValidRows(true);
|
2015-01-11 11:22:52 +01:00
|
|
|
|
|
|
|
//calculate nb of pages
|
2016-04-30 03:45:04 +02:00
|
|
|
this.nbPages = Math.ceil(tf.validRowsIndex.length / this.pagingLength);
|
2015-01-11 11:22:52 +01:00
|
|
|
//refresh page nb span
|
2016-11-05 12:08:32 +01:00
|
|
|
this.pgCont.innerHTML = this.nbPages;
|
2015-01-11 11:22:52 +01:00
|
|
|
//select clearing shortcut
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === SELECT) {
|
2015-01-11 11:22:52 +01:00
|
|
|
this.pagingSlc.innerHTML = '';
|
|
|
|
}
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.nbPages > 0) {
|
2015-01-11 11:22:52 +01:00
|
|
|
mdiv.style.visibility = 'visible';
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === SELECT) {
|
2016-04-30 03:45:04 +02:00
|
|
|
for (var z = 0; z < this.nbPages; z++) {
|
2016-05-24 10:42:11 +02:00
|
|
|
var opt = createOpt(z + 1, z * this.pagingLength, false);
|
2015-07-15 15:34:19 +02:00
|
|
|
this.pagingSlc.options[z] = opt;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
2016-04-30 03:45:04 +02:00
|
|
|
} else {
|
2015-01-11 11:22:52 +01:00
|
|
|
//input type
|
|
|
|
this.pagingSlc.value = this.currentPageNb;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/*** if no results paging select and buttons are hidden ***/
|
|
|
|
mdiv.style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
this.groupByPage(tf.validRowsIndex);
|
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
|
|
|
* Group table rows by page and display valid rows
|
|
|
|
* @param {Array} validRows Collection of valid rows
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
groupByPage(validRows) {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
|
|
|
var rows = tf.tbl.rows;
|
2015-12-20 06:58:39 +01:00
|
|
|
var startPagingRow = parseInt(this.startPagingRow, 10);
|
|
|
|
var endPagingRow = startPagingRow + parseInt(this.pagingLength, 10);
|
2015-01-11 11:22:52 +01:00
|
|
|
|
|
|
|
//store valid rows indexes
|
2016-04-30 03:45:04 +02:00
|
|
|
if (validRows) {
|
2015-01-11 11:22:52 +01:00
|
|
|
tf.validRowsIndex = validRows;
|
|
|
|
}
|
|
|
|
|
|
|
|
//this loop shows valid rows of current page
|
2016-05-26 06:36:36 +02:00
|
|
|
for (var h = 0, len = tf.getValidRowsNb(true); h < len; h++) {
|
2015-06-14 04:23:27 +02:00
|
|
|
var validRowIdx = tf.validRowsIndex[h];
|
|
|
|
var r = rows[validRowIdx];
|
|
|
|
var isRowValid = r.getAttribute('validRow');
|
2016-01-02 04:43:00 +01:00
|
|
|
var rowDisplayed = false;
|
2015-06-14 04:23:27 +02:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (h >= startPagingRow && h < endPagingRow) {
|
2016-05-15 04:56:12 +02:00
|
|
|
if (isNull(isRowValid) || Boolean(isRowValid === 'true')) {
|
2015-01-11 11:22:52 +01:00
|
|
|
r.style.display = '';
|
2016-01-02 04:43:00 +01:00
|
|
|
rowDisplayed = true;
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
2015-01-11 11:22:52 +01:00
|
|
|
} else {
|
2016-05-07 13:59:42 +02:00
|
|
|
r.style.display = NONE;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
2016-01-02 04:43:00 +01:00
|
|
|
this.emitter.emit('row-paged', tf, validRowIdx, h, rowDisplayed);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2015-12-27 12:08:14 +01:00
|
|
|
// broadcast grouping by page
|
|
|
|
this.emitter.emit('grouped-by-page', tf, this);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2015-06-13 11:03:33 +02:00
|
|
|
/**
|
|
|
|
* Return the current page number
|
|
|
|
* @return {Number} Page number
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
getPage() {
|
2015-06-13 11:03:33 +02:00
|
|
|
return this.currentPageNb;
|
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
2016-04-30 03:45:04 +02:00
|
|
|
* Show page defined by passed argument (string or number):
|
2016-02-28 13:01:26 +01:00
|
|
|
* @param {String}/{Number} cmd possible string values: 'next',
|
|
|
|
* 'previous', 'last', 'first' or page number as per param
|
2015-01-26 10:31:25 +01:00
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
setPage(cmd) {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
2016-05-27 09:40:22 +02:00
|
|
|
if (!tf.isInitialized() || !this.isEnabled()) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-01-26 10:31:25 +01:00
|
|
|
var btnEvt = this.evt,
|
2015-01-11 11:22:52 +01:00
|
|
|
cmdtype = typeof cmd;
|
2016-04-30 03:45:04 +02:00
|
|
|
if (cmdtype === 'string') {
|
2016-05-20 10:08:39 +02:00
|
|
|
switch (cmd.toLowerCase()) {
|
2015-01-11 11:22:52 +01:00
|
|
|
case 'next':
|
|
|
|
btnEvt.next();
|
2016-02-22 08:14:58 +01:00
|
|
|
break;
|
2015-01-11 11:22:52 +01:00
|
|
|
case 'previous':
|
|
|
|
btnEvt.prev();
|
2016-02-22 08:14:58 +01:00
|
|
|
break;
|
2015-01-11 11:22:52 +01:00
|
|
|
case 'last':
|
|
|
|
btnEvt.last();
|
2016-02-22 08:14:58 +01:00
|
|
|
break;
|
2015-01-11 11:22:52 +01:00
|
|
|
case 'first':
|
|
|
|
btnEvt.first();
|
2016-02-22 08:14:58 +01:00
|
|
|
break;
|
2015-01-11 11:22:52 +01:00
|
|
|
default:
|
|
|
|
btnEvt.next();
|
2016-02-22 08:14:58 +01:00
|
|
|
break;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-30 03:45:04 +02:00
|
|
|
else if (cmdtype === 'number') {
|
|
|
|
this.changePage(cmd - 1);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
2015-02-01 09:26:48 +01:00
|
|
|
* Generates UI elements for the number of results per page drop-down
|
2015-01-26 10:31:25 +01:00
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
setResultsPerPage() {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
2015-01-26 10:31:25 +01:00
|
|
|
var evt = this.evt;
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.resultsPerPageSlc || !this.resultsPerPage) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
evt.slcResultsChange = (ev) => {
|
2016-03-18 07:58:32 +01:00
|
|
|
this.onChangeResultsPerPage();
|
2015-01-26 10:31:25 +01:00
|
|
|
ev.target.blur();
|
|
|
|
};
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
var slcR = createElm(SELECT);
|
2015-08-07 16:23:32 +02:00
|
|
|
slcR.className = this.resultsSlcCssClass;
|
2015-01-11 11:22:52 +01:00
|
|
|
var slcRText = this.resultsPerPage[0],
|
|
|
|
slcROpts = this.resultsPerPage[1];
|
2016-11-05 12:08:32 +01:00
|
|
|
var slcRSpan = createElm('span');
|
2015-01-11 11:22:52 +01:00
|
|
|
slcRSpan.className = this.resultsSpanCssClass;
|
|
|
|
|
|
|
|
// results per page select is added to external element
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.resultsPerPageTgtId) {
|
2015-02-21 04:39:09 +01:00
|
|
|
tf.setToolbar();
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
var targetEl = !this.resultsPerPageTgtId ?
|
2016-05-25 09:31:53 +02:00
|
|
|
tf.rDiv : elm(this.resultsPerPageTgtId);
|
2016-05-24 10:42:11 +02:00
|
|
|
slcRSpan.appendChild(createText(slcRText));
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2015-06-08 12:21:50 +02:00
|
|
|
var help = tf.feature('help');
|
2016-04-30 03:45:04 +02:00
|
|
|
if (help && help.btn) {
|
2015-11-21 04:41:40 +01:00
|
|
|
help.btn.parentNode.insertBefore(slcRSpan, help.btn);
|
|
|
|
help.btn.parentNode.insertBefore(slcR, help.btn);
|
2015-06-08 12:21:50 +02:00
|
|
|
} else {
|
|
|
|
targetEl.appendChild(slcRSpan);
|
|
|
|
targetEl.appendChild(slcR);
|
|
|
|
}
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
for (var r = 0; r < slcROpts.length; r++) {
|
2015-01-11 11:22:52 +01:00
|
|
|
var currOpt = new Option(slcROpts[r], slcROpts[r], false, false);
|
2015-06-08 12:21:50 +02:00
|
|
|
slcR.options[r] = currOpt;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(slcR, 'change', evt.slcResultsChange);
|
2016-11-05 12:08:32 +01:00
|
|
|
this.slcResultsTxt = slcRSpan;
|
2015-06-08 12:21:50 +02:00
|
|
|
this.resultsPerPageSlc = slcR;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
|
|
|
* Remove number of results per page UI elements
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
removeResultsPerPage() {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
2016-05-27 09:40:22 +02:00
|
|
|
if (!tf.isInitialized() || !this.resultsPerPageSlc ||
|
|
|
|
!this.resultsPerPage) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.resultsPerPageSlc) {
|
|
|
|
removeElm(this.resultsPerPageSlc);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.slcResultsTxt) {
|
|
|
|
removeElm(this.slcResultsTxt);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
this.resultsPerPageSlc = null;
|
2016-11-05 12:08:32 +01:00
|
|
|
this.slcResultsTxt = null;
|
2014-12-16 04:21:27 +01:00
|
|
|
}
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
2016-07-31 13:49:38 +02:00
|
|
|
* Change the page based on passed index
|
|
|
|
* @param {Number} index Index of the page (0-n)
|
2015-01-26 10:31:25 +01:00
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
changePage(index) {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.isEnabled()) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2016-03-18 07:58:32 +01:00
|
|
|
this.emitter.emit('before-page-change', tf, (index + 1));
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (index === null) {
|
2016-05-07 13:59:42 +02:00
|
|
|
index = this.pageSelectorType === SELECT ?
|
2016-04-30 03:45:04 +02:00
|
|
|
this.pagingSlc.options.selectedIndex : this.pagingSlc.value - 1;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
2016-04-30 03:45:04 +02:00
|
|
|
if (index >= 0 && index <= (this.nbPages - 1)) {
|
|
|
|
if (this.onBeforeChangePage) {
|
2016-03-18 07:58:32 +01:00
|
|
|
this.onBeforeChangePage.call(null, this, (index + 1));
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
2016-04-30 03:45:04 +02:00
|
|
|
this.currentPageNb = parseInt(index, 10) + 1;
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === SELECT) {
|
2015-01-11 11:22:52 +01:00
|
|
|
this.pagingSlc.options[index].selected = true;
|
|
|
|
} else {
|
|
|
|
this.pagingSlc.value = this.currentPageNb;
|
|
|
|
}
|
|
|
|
|
2016-05-07 13:59:42 +02:00
|
|
|
this.startPagingRow = (this.pageSelectorType === SELECT) ?
|
2016-04-30 03:45:04 +02:00
|
|
|
this.pagingSlc.value : (index * this.pagingLength);
|
2015-01-11 11:22:52 +01:00
|
|
|
|
|
|
|
this.groupByPage();
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.onAfterChangePage) {
|
2016-03-18 07:58:32 +01:00
|
|
|
this.onAfterChangePage.call(null, this, (index + 1));
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2016-03-18 07:58:32 +01:00
|
|
|
this.emitter.emit('after-page-change', tf, (index + 1));
|
|
|
|
}
|
|
|
|
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Change the number of results per page based on passed value
|
|
|
|
* @param {String} val The number of results per page
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
changeResultsPerPage(val) {
|
|
|
|
if (!this.isEnabled() || isNaN(val)) {
|
2016-03-18 07:58:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.resultsPerPageSlc.value = val;
|
|
|
|
this.onChangeResultsPerPage();
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
2015-06-13 11:03:33 +02:00
|
|
|
* Change rows according to page results drop-down
|
2015-01-26 10:31:25 +01:00
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
onChangeResultsPerPage() {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.isEnabled()) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2016-03-16 08:08:26 +01:00
|
|
|
this.emitter.emit('before-page-length-change', tf);
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2015-01-11 11:22:52 +01:00
|
|
|
var slcR = this.resultsPerPageSlc;
|
2016-02-06 04:08:46 +01:00
|
|
|
var slcIndex = slcR.selectedIndex;
|
2016-05-07 13:59:42 +02:00
|
|
|
var slcPagesSelIndex = (this.pageSelectorType === SELECT) ?
|
2016-04-30 03:45:04 +02:00
|
|
|
this.pagingSlc.selectedIndex :
|
|
|
|
parseInt(this.pagingSlc.value - 1, 10);
|
2016-02-06 04:08:46 +01:00
|
|
|
this.pagingLength = parseInt(slcR.options[slcIndex].value, 10);
|
2016-04-30 03:45:04 +02:00
|
|
|
this.startPagingRow = this.pagingLength * slcPagesSelIndex;
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!isNaN(this.pagingLength)) {
|
|
|
|
if (this.startPagingRow >= tf.nbFilterableRows) {
|
|
|
|
this.startPagingRow = (tf.nbFilterableRows - this.pagingLength);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
this.setPagingInfo();
|
|
|
|
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === SELECT) {
|
2016-02-06 04:08:46 +01:00
|
|
|
var slcIdx =
|
2016-04-30 03:45:04 +02:00
|
|
|
(this.pagingSlc.options.length - 1 <= slcPagesSelIndex) ?
|
|
|
|
(this.pagingSlc.options.length - 1) : slcPagesSelIndex;
|
2016-02-06 04:08:46 +01:00
|
|
|
this.pagingSlc.options[slcIdx].selected = true;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2016-03-18 07:58:32 +01:00
|
|
|
this.emitter.emit('after-page-length-change', tf, this.pagingLength);
|
2016-01-02 15:33:31 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
|
|
|
* Re-set page nb at page re-load
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
resetPage() {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.isEnabled()) {
|
2016-01-02 15:33:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.emitter.emit('before-reset-page', tf);
|
2016-02-06 04:08:46 +01:00
|
|
|
var pgNb = tf.feature('store').getPageNb();
|
2016-04-30 03:45:04 +02:00
|
|
|
if (pgNb !== '') {
|
|
|
|
this.changePage((pgNb - 1));
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
2016-02-06 04:08:46 +01:00
|
|
|
this.emitter.emit('after-reset-page', tf, pgNb);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
2015-06-08 12:21:50 +02:00
|
|
|
* Re-set page length value at page re-load
|
2015-01-26 10:31:25 +01:00
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
resetPageLength() {
|
2015-01-11 11:22:52 +01:00
|
|
|
var tf = this.tf;
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.isEnabled()) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-02 15:33:31 +01:00
|
|
|
this.emitter.emit('before-reset-page-length', tf);
|
2016-02-06 04:08:46 +01:00
|
|
|
var pglenIndex = tf.feature('store').getPageLength();
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (pglenIndex !== '') {
|
2015-01-11 11:22:52 +01:00
|
|
|
this.resultsPerPageSlc.options[pglenIndex].selected = true;
|
|
|
|
this.changeResultsPerPage();
|
|
|
|
}
|
2016-01-02 15:33:31 +01:00
|
|
|
this.emitter.emit('after-reset-page-length', tf, pglenIndex);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
/**
|
|
|
|
* Remove paging feature
|
|
|
|
*/
|
2016-04-30 03:45:04 +02:00
|
|
|
destroy() {
|
|
|
|
if (!this.initialized) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-26 10:31:25 +01:00
|
|
|
var evt = this.evt;
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.pagingSlc) {
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === SELECT) {
|
2016-06-02 06:13:56 +02:00
|
|
|
removeEvt(this.pagingSlc, 'change', evt.slcPagesChange);
|
2015-01-26 10:31:25 +01:00
|
|
|
}
|
2016-05-07 13:59:42 +02:00
|
|
|
else if (this.pageSelectorType === INPUT) {
|
2016-06-02 06:13:56 +02:00
|
|
|
removeEvt(this.pagingSlc, 'keypress', evt._detectKey);
|
2015-01-26 10:31:25 +01:00
|
|
|
}
|
2016-05-24 10:42:11 +02:00
|
|
|
removeElm(this.pagingSlc);
|
2015-01-26 10:31:25 +01:00
|
|
|
}
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.btnNextCont) {
|
|
|
|
removeEvt(this.btnNextCont, 'click', evt.next);
|
|
|
|
removeElm(this.btnNextCont);
|
|
|
|
this.btnNextCont = null;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.btnPrevCont) {
|
|
|
|
removeEvt(this.btnPrevCont, 'click', evt.prev);
|
|
|
|
removeElm(this.btnPrevCont);
|
|
|
|
this.btnPrevCont = null;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.btnLastCont) {
|
|
|
|
removeEvt(this.btnLastCont, 'click', evt.last);
|
|
|
|
removeElm(this.btnLastCont);
|
|
|
|
this.btnLastCont = null;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.btnFirstCont) {
|
|
|
|
removeEvt(this.btnFirstCont, 'click', evt.first);
|
|
|
|
removeElm(this.btnFirstCont);
|
|
|
|
this.btnFirstCont = null;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.pgBefore) {
|
|
|
|
removeElm(this.pgBefore);
|
|
|
|
this.pgBefore = null;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.pgAfter) {
|
|
|
|
removeElm(this.pgAfter);
|
|
|
|
this.pgAfter = null;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-05 12:08:32 +01:00
|
|
|
if (this.pgCont) {
|
|
|
|
removeElm(this.pgCont);
|
|
|
|
this.pgCont = null;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.hasResultsPerPage) {
|
2015-01-26 10:31:25 +01:00
|
|
|
this.removeResultsPerPage();
|
|
|
|
}
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
this.emitter.off(['after-filtering'], () => this.resetPagingInfo());
|
2016-03-18 07:58:32 +01:00
|
|
|
this.emitter.off(['change-page'],
|
|
|
|
(tf, pageNumber) => this.setPage(pageNumber));
|
|
|
|
this.emitter.off(['change-page-results'],
|
|
|
|
(tf, pageLength) => this.changeResultsPerPage(pageLength));
|
2015-12-28 07:53:03 +01:00
|
|
|
|
2015-01-11 11:22:52 +01:00
|
|
|
this.pagingSlc = null;
|
2015-01-26 10:31:25 +01:00
|
|
|
this.nbPages = 0;
|
2015-11-14 16:14:13 +01:00
|
|
|
this.disable();
|
|
|
|
this.initialized = false;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
}
|