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';
|
2017-07-09 07:11:51 +02:00
|
|
|
import {isArray, isNull, EMPTY_FN} from '../types';
|
2018-05-20 13:43:20 +02:00
|
|
|
import {addEvt, removeEvt, isKeyPressed, bound} from '../event';
|
2016-05-08 03:48:51 +02:00
|
|
|
import {INPUT, SELECT, NONE, ENTER_KEY} from '../const';
|
2017-07-09 07:11:51 +02:00
|
|
|
import {
|
|
|
|
defaultsStr, defaultsNb, defaultsBool, defaultsArr, defaultsFn
|
|
|
|
} from '../settings';
|
2017-10-08 12:14:25 +02:00
|
|
|
import {CENTER, RIGHT} from './toolbar';
|
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
|
2017-06-14 05:59:04 +02:00
|
|
|
let f = this.config.paging || {};
|
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}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnCssClass = defaultsStr(f.btn_css_class, 'pgInp');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
2017-06-28 12:47:32 +02:00
|
|
|
/**
|
2016-07-31 13:49:38 +02:00
|
|
|
* Main select DOM element
|
|
|
|
* @type {DOMElement}
|
|
|
|
*/
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Results per page select DOM element
|
|
|
|
* @type {DOMElement}
|
|
|
|
*/
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageLengthSlc = null;
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of custom container element
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.tgtId = defaultsStr(f.target_id, null);
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of rows contained in a page
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.pageLength = defaultsNb(f.length, 10);
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of custom container element for the results per page selector
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.pageLengthTgtId = defaultsStr(f.results_per_page_target_id, null);
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for the paging select element
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.pgSlcCssClass = defaultsStr(f.slc_css_class, 'pgSlc');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for the paging input element
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.pgInpCssClass = defaultsStr(f.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}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.resultsPerPage = defaultsArr(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}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.resultsSlcCssClass = defaultsStr(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}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.resultsSpanCssClass = defaultsStr(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}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnNextPageText = defaultsStr(f.btn_next_page_text, '>');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Previous page button text
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnPrevPageText = defaultsStr(f.btn_prev_page_text, '<');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Last page button text
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnLastPageText = defaultsStr(f.btn_last_page_text, '>|');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* First page button text
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnFirstPageText = defaultsStr(f.btn_first_page_text, '|<');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Next page button HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnNextPageHtml = defaultsStr(f.btn_next_page_html,
|
2015-01-26 10:31:25 +01:00
|
|
|
(!tf.enableIcons ? null :
|
2017-06-14 05:59:04 +02:00
|
|
|
'<input type="button" value="" class="' + this.btnCssClass +
|
2017-07-09 07:11:51 +02:00
|
|
|
' nextPage" title="Next page" />'));
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Previous page button HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnPrevPageHtml = defaultsStr(f.btn_prev_page_html,
|
2015-01-26 10:31:25 +01:00
|
|
|
(!tf.enableIcons ? null :
|
2017-06-14 05:59:04 +02:00
|
|
|
'<input type="button" value="" class="' + this.btnCssClass +
|
2017-07-09 07:11:51 +02:00
|
|
|
' previousPage" title="Previous page" />'));
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* First page button HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnFirstPageHtml = defaultsStr(f.btn_first_page_html,
|
2015-01-26 10:31:25 +01:00
|
|
|
(!tf.enableIcons ? null :
|
2017-06-14 05:59:04 +02:00
|
|
|
'<input type="button" value="" class="' + this.btnCssClass +
|
2017-07-09 07:11:51 +02:00
|
|
|
' firstPage" title="First page" />'));
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Last page button HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnLastPageHtml = defaultsStr(f.btn_last_page_html,
|
2015-01-26 10:31:25 +01:00
|
|
|
(!tf.enableIcons ? null :
|
2017-06-14 05:59:04 +02:00
|
|
|
'<input type="button" value="" class="' + this.btnCssClass +
|
2017-07-09 07:11:51 +02:00
|
|
|
' lastPage" title="Last page" />'));
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text preceeding page selector drop-down
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.pageText = defaultsStr(f.page_text, ' Page ');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text after page selector drop-down
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.ofText = defaultsStr(f.of_text, ' of ');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for the span containing total number of pages
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.nbPgSpanCssClass = defaultsStr(f.nb_pages_css_class, 'nbpg');
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if paging buttons are enabled (default: true)
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.hasBtns = defaultsBool(f.btns, true);
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines page selector type, two possible values: 'select', 'input'
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.pageSelectorType = defaultsStr(f.page_selector_type, SELECT);
|
2016-07-31 13:49:38 +02:00
|
|
|
|
2017-10-08 12:14:25 +02:00
|
|
|
/**
|
|
|
|
* Default position in toolbar ('left'|'center'|'right')
|
|
|
|
* @type {String}
|
|
|
|
*/
|
|
|
|
this.toolbarPosition = defaultsStr(f.toolbar_position, CENTER);
|
|
|
|
|
2016-07-31 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Callback fired before the page is changed
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onBeforeChangePage = defaultsFn(f.on_before_change_page, EMPTY_FN);
|
2016-07-31 13:49:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired after the page is changed
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onAfterChangePage = defaultsFn(f.on_after_change_page, EMPTY_FN);
|
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
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
let startRow = tf.refRow;
|
|
|
|
let nrows = tf.getRowsNb(true);
|
2014-12-16 04:21:27 +01:00
|
|
|
//calculates page nb
|
2017-06-14 05:59:04 +02:00
|
|
|
this.nbPages = Math.ceil((nrows - startRow) / this.pageLength);
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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) ?
|
2017-06-14 05:59:04 +02:00
|
|
|
o.pageSlc.options.selectedIndex :
|
|
|
|
parseInt(o.pageSlc.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) ?
|
2017-06-14 05:59:04 +02:00
|
|
|
parseInt(o.pageSlc.options.length, 10) - 1 :
|
2016-04-30 03:45:04 +02:00
|
|
|
(o.nbPages - 1);
|
2015-01-26 10:31:25 +01:00
|
|
|
},
|
2016-04-30 03:45:04 +02:00
|
|
|
next() {
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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() {
|
2017-06-14 05:59:04 +02:00
|
|
|
let prevIndex = o.evt.slcIndex() > 0 ?
|
2016-04-30 03:45:04 +02:00
|
|
|
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) {
|
2018-02-02 12:26:28 +01:00
|
|
|
if (isKeyPressed(e, [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() {
|
2017-06-14 05:59:04 +02:00
|
|
|
let slcPages;
|
|
|
|
let tf = this.tf;
|
|
|
|
let evt = this.evt;
|
2015-01-26 10:31:25 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.initialized) {
|
2015-11-14 16:14:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-08 12:14:25 +02:00
|
|
|
this.emitter.emit('initializing-feature', this, !isNull(this.tgtId));
|
|
|
|
|
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 {
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageLength = this.resultsPerPage[1][0];
|
2015-01-26 10:31:25 +01:00
|
|
|
this.setResultsPerPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
evt.slcPagesChange = (event) => {
|
2017-06-14 05:59:04 +02:00
|
|
|
let slc = event.target;
|
2015-07-14 15:16:05 +02:00
|
|
|
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
|
2017-06-14 05:59:04 +02:00
|
|
|
let btnNextSpan = createElm('span');
|
|
|
|
let btnPrevSpan = createElm('span');
|
|
|
|
let btnLastSpan = createElm('span');
|
|
|
|
let btnFirstSpan = createElm('span');
|
2014-12-16 04:21:27 +01:00
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
if (this.hasBtns) {
|
2014-12-16 04:21:27 +01:00
|
|
|
// Next button
|
2016-04-30 03:45:04 +02:00
|
|
|
if (!this.btnNextPageHtml) {
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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
|
|
|
);
|
2017-06-14 05:59:04 +02:00
|
|
|
btnNext.className = this.btnCssClass;
|
2016-11-05 12:08:32 +01:00
|
|
|
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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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
|
|
|
);
|
2017-06-14 05:59:04 +02:00
|
|
|
btnPrev.className = this.btnCssClass;
|
2016-11-05 12:08:32 +01:00
|
|
|
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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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
|
|
|
);
|
2017-06-14 05:59:04 +02:00
|
|
|
btnLast.className = this.btnCssClass;
|
2016-11-05 12:08:32 +01:00
|
|
|
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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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
|
|
|
);
|
2017-06-14 05:59:04 +02:00
|
|
|
btnFirst.className = this.btnCssClass;
|
2016-11-05 12:08:32 +01:00
|
|
|
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
|
2017-10-09 12:56:58 +02:00
|
|
|
let targetEl = !this.tgtId ?
|
|
|
|
tf.feature('toolbar').container(this.toolbarPosition) :
|
2017-10-08 12:14:25 +02:00
|
|
|
elm(this.tgtId);
|
2014-12-16 04:21:27 +01:00
|
|
|
targetEl.appendChild(btnFirstSpan);
|
|
|
|
targetEl.appendChild(btnPrevSpan);
|
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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);
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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);
|
2017-06-14 05:59:04 +02:00
|
|
|
let pgSpan = createElm('span');
|
2016-11-05 12:08:32 +01:00
|
|
|
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;
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc = 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);
|
|
|
|
}
|
|
|
|
|
2018-05-20 13:43:20 +02:00
|
|
|
this.emitter.on(['after-filtering'], bound(this.resetPagingInfo, this));
|
|
|
|
this.emitter.on(['change-page'], bound(this.changePageHandler, this));
|
2016-03-18 07:58:32 +01:00
|
|
|
this.emitter.on(['change-page-results'],
|
2018-05-20 13:43:20 +02:00
|
|
|
bound(this.changePageResultsHandler, this));
|
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;
|
2017-10-08 12:14:25 +02:00
|
|
|
|
|
|
|
this.emitter.emit('feature-initialized', this);
|
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-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) {
|
2017-05-13 12:51:10 +02:00
|
|
|
this.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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
let tf = this.tf;
|
2017-10-09 12:56:58 +02:00
|
|
|
let cont = !this.tgtId ?
|
|
|
|
tf.feature('toolbar').container(this.toolbarPosition) :
|
2017-10-08 12:14:25 +02:00
|
|
|
elm(this.tgtId);
|
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
|
2017-06-14 05:59:04 +02:00
|
|
|
this.nbPages = Math.ceil(tf.validRowsIndex.length / this.pageLength);
|
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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc.innerHTML = '';
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.nbPages > 0) {
|
2017-10-08 12:14:25 +02:00
|
|
|
cont.style.visibility = 'visible';
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === SELECT) {
|
2017-06-14 05:59:04 +02:00
|
|
|
for (let z = 0; z < this.nbPages; z++) {
|
|
|
|
let opt = createOpt(z + 1, z * this.pageLength, false);
|
|
|
|
this.pageSlc.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
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc.value = this.currentPageNb;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/*** if no results paging select and buttons are hidden ***/
|
2017-10-08 12:14:25 +02:00
|
|
|
cont.style.visibility = 'hidden';
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
let tf = this.tf;
|
|
|
|
let rows = tf.dom().rows;
|
|
|
|
let startPagingRow = parseInt(this.startPagingRow, 10);
|
|
|
|
let endPagingRow = startPagingRow + parseInt(this.pageLength, 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
|
2017-06-14 05:59:04 +02:00
|
|
|
for (let h = 0, len = tf.getValidRowsNb(true); h < len; h++) {
|
|
|
|
let validRowIdx = tf.validRowsIndex[h];
|
|
|
|
let r = rows[validRowIdx];
|
|
|
|
let isRowValid = r.getAttribute('validRow');
|
|
|
|
let 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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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;
|
|
|
|
}
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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() {
|
2017-06-14 05:59:04 +02:00
|
|
|
let tf = this.tf;
|
|
|
|
let evt = this.evt;
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
if (this.pageLengthSlc || !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
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
let slcR = createElm(SELECT);
|
2015-08-07 16:23:32 +02:00
|
|
|
slcR.className = this.resultsSlcCssClass;
|
2017-06-14 05:59:04 +02:00
|
|
|
let slcRText = this.resultsPerPage[0],
|
2015-01-11 11:22:52 +01:00
|
|
|
slcROpts = this.resultsPerPage[1];
|
2017-06-14 05:59:04 +02:00
|
|
|
let slcRSpan = createElm('span');
|
2015-01-11 11:22:52 +01:00
|
|
|
slcRSpan.className = this.resultsSpanCssClass;
|
|
|
|
|
|
|
|
// results per page select is added to external element
|
2017-06-14 05:59:04 +02:00
|
|
|
let targetEl = !this.pageLengthTgtId ?
|
2017-10-08 12:14:25 +02:00
|
|
|
tf.feature('toolbar').container(RIGHT) :
|
|
|
|
elm(this.pageLengthTgtId);
|
2016-05-24 10:42:11 +02:00
|
|
|
slcRSpan.appendChild(createText(slcRText));
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
for (let r = 0; r < slcROpts.length; r++) {
|
|
|
|
let 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;
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageLengthSlc = 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() {
|
2017-06-14 05:59:04 +02:00
|
|
|
let tf = this.tf;
|
|
|
|
if (!tf.isInitialized() || !this.pageLengthSlc ||
|
2016-05-27 09:40:22 +02:00
|
|
|
!this.resultsPerPage) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-06-14 05:59:04 +02:00
|
|
|
if (this.pageLengthSlc) {
|
|
|
|
removeElm(this.pageLengthSlc);
|
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
|
|
|
}
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageLengthSlc = 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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
let tf = this.tf;
|
2015-01-11 11:22:52 +01:00
|
|
|
|
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 ?
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc.options.selectedIndex : this.pageSlc.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)) {
|
2016-12-22 12:01:59 +01:00
|
|
|
this.onBeforeChangePage(this, (index + 1));
|
|
|
|
|
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) {
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc.options[index].selected = true;
|
2015-01-11 11:22:52 +01:00
|
|
|
} else {
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc.value = this.currentPageNb;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-05-07 13:59:42 +02:00
|
|
|
this.startPagingRow = (this.pageSelectorType === SELECT) ?
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc.value : (index * this.pageLength);
|
2015-01-11 11:22:52 +01:00
|
|
|
|
|
|
|
this.groupByPage();
|
|
|
|
|
2016-12-22 12:01:59 +01:00
|
|
|
this.onAfterChangePage(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;
|
|
|
|
}
|
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageLengthSlc.value = val;
|
2016-03-18 07:58:32 +01:00
|
|
|
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() {
|
2017-03-25 05:19:12 +01:00
|
|
|
let tf = this.tf;
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2017-03-25 05:19:12 +01:00
|
|
|
if (!this.isEnabled() || tf.getValidRowsNb() === 0) {
|
2015-01-11 11:22:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2017-03-25 05:19:12 +01:00
|
|
|
let {
|
2017-06-14 05:59:04 +02:00
|
|
|
pageLengthSlc: slcR, pageSelectorType, pageSlc, emitter
|
2017-03-25 05:19:12 +01:00
|
|
|
} = this;
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2017-03-25 05:19:12 +01:00
|
|
|
emitter.emit('before-page-length-change', tf);
|
|
|
|
|
|
|
|
let slcIndex = slcR.selectedIndex;
|
|
|
|
let slcPagesSelIndex = (pageSelectorType === SELECT) ?
|
2017-06-14 05:59:04 +02:00
|
|
|
pageSlc.selectedIndex : parseInt(pageSlc.value - 1, 10);
|
|
|
|
this.pageLength = parseInt(slcR.options[slcIndex].value, 10);
|
|
|
|
this.startPagingRow = this.pageLength * slcPagesSelIndex;
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
if (!isNaN(this.pageLength)) {
|
2016-04-30 03:45:04 +02:00
|
|
|
if (this.startPagingRow >= tf.nbFilterableRows) {
|
2017-06-14 05:59:04 +02:00
|
|
|
this.startPagingRow = (tf.nbFilterableRows - this.pageLength);
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
this.setPagingInfo();
|
|
|
|
|
2017-03-25 05:19:12 +01:00
|
|
|
if (pageSelectorType === SELECT) {
|
2017-06-28 12:47:32 +02:00
|
|
|
let slcIdx = (pageSlc.options.length - 1 <= slcPagesSelIndex) ?
|
|
|
|
(pageSlc.options.length - 1) :
|
|
|
|
slcPagesSelIndex;
|
2017-06-14 05:59:04 +02:00
|
|
|
pageSlc.options[slcIdx].selected = true;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-02 15:33:31 +01:00
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
emitter.emit('after-page-length-change', tf, this.pageLength);
|
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() {
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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);
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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() {
|
2017-06-14 05:59:04 +02:00
|
|
|
let 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);
|
2017-06-14 05:59:04 +02:00
|
|
|
let pglenIndex = tf.feature('store').getPageLength();
|
2015-01-11 11:22:52 +01:00
|
|
|
|
2016-04-30 03:45:04 +02:00
|
|
|
if (pglenIndex !== '') {
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageLengthSlc.options[pglenIndex].selected = true;
|
2015-01-11 11:22:52 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-20 13:43:20 +02:00
|
|
|
/** @private */
|
|
|
|
changePageHandler(tf, pageNumber) {
|
|
|
|
this.setPage(pageNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @private */
|
|
|
|
changePageResultsHandler(tf, pageLength) {
|
|
|
|
this.changeResultsPerPage(pageLength);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
let evt = this.evt;
|
2015-01-26 10:31:25 +01:00
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
if (this.pageSlc) {
|
2016-05-07 13:59:42 +02:00
|
|
|
if (this.pageSelectorType === SELECT) {
|
2017-06-14 05:59:04 +02:00
|
|
|
removeEvt(this.pageSlc, 'change', evt.slcPagesChange);
|
2015-01-26 10:31:25 +01:00
|
|
|
}
|
2016-05-07 13:59:42 +02:00
|
|
|
else if (this.pageSelectorType === INPUT) {
|
2017-06-14 05:59:04 +02:00
|
|
|
removeEvt(this.pageSlc, 'keypress', evt._detectKey);
|
2015-01-26 10:31:25 +01:00
|
|
|
}
|
2017-06-14 05:59:04 +02:00
|
|
|
removeElm(this.pageSlc);
|
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();
|
|
|
|
}
|
|
|
|
|
2018-05-20 13:43:20 +02:00
|
|
|
this.emitter.off(['after-filtering'],
|
|
|
|
bound(this.resetPagingInfo, this));
|
|
|
|
this.emitter.off(['change-page'], bound(this.changePageHandler, this));
|
2016-03-18 07:58:32 +01:00
|
|
|
this.emitter.off(['change-page-results'],
|
2018-05-20 13:43:20 +02:00
|
|
|
bound(this.changePageResultsHandler, this));
|
2015-12-28 07:53:03 +01:00
|
|
|
|
2017-06-14 05:59:04 +02:00
|
|
|
this.pageSlc = null;
|
2015-01-26 10:31:25 +01:00
|
|
|
this.nbPages = 0;
|
2017-05-13 12:51:10 +02:00
|
|
|
|
2015-11-14 16:14:13 +01:00
|
|
|
this.initialized = false;
|
2015-01-11 11:22:52 +01:00
|
|
|
}
|
|
|
|
}
|