2016-05-14 14:09:28 +02:00
|
|
|
import {Feature} from '../../feature';
|
2016-05-25 09:31:53 +02:00
|
|
|
import {
|
|
|
|
addClass, removeClass, createCheckItem, createElm, elm, removeElm,
|
2016-09-03 15:30:31 +02:00
|
|
|
getText
|
2016-05-25 09:31:53 +02:00
|
|
|
} from '../../dom';
|
2017-07-09 07:11:51 +02:00
|
|
|
import {isUndef, EMPTY_FN} from '../../types';
|
2017-01-07 12:51:49 +01:00
|
|
|
import {addEvt, targetEvt, removeEvt} from '../../event';
|
|
|
|
import {root} from '../../root';
|
2017-06-25 13:51:15 +02:00
|
|
|
import {NONE} from '../../const';
|
2017-07-09 07:11:51 +02:00
|
|
|
import {
|
|
|
|
defaultsBool, defaultsStr, defaultsFn,
|
|
|
|
defaultsNb, defaultsArr
|
|
|
|
} from '../../settings';
|
2015-04-12 10:32:24 +02:00
|
|
|
|
2016-09-03 05:19:55 +02:00
|
|
|
/**
|
|
|
|
* Columns Visibility extension
|
|
|
|
*/
|
2016-05-14 14:09:28 +02:00
|
|
|
export default class ColsVisibility extends Feature {
|
2015-04-12 10:32:24 +02:00
|
|
|
|
|
|
|
/**
|
2016-09-03 05:19:55 +02:00
|
|
|
* Creates an instance of ColsVisibility
|
|
|
|
* @param {TableFilter} tf TableFilter instance
|
|
|
|
* @param {Object} Configuration object
|
2015-04-12 10:32:24 +02:00
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
constructor(tf, f) {
|
2016-05-14 14:09:28 +02:00
|
|
|
super(tf, f.name);
|
2015-04-20 09:20:28 +02:00
|
|
|
|
2015-04-12 10:32:24 +02:00
|
|
|
// Configuration object
|
2016-09-03 15:30:31 +02:00
|
|
|
let cfg = this.config;
|
2015-04-12 10:32:24 +02:00
|
|
|
|
2016-09-03 05:19:55 +02:00
|
|
|
/**
|
|
|
|
* Module name
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2015-06-06 12:06:15 +02:00
|
|
|
this.name = f.name;
|
2016-09-03 05:19:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module description
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.desc = defaultsStr(f.description, 'Columns visibility manager');
|
2015-04-12 10:32:24 +02:00
|
|
|
|
2016-09-03 05:19:55 +02:00
|
|
|
/**
|
|
|
|
* show/hide columns container element
|
|
|
|
* @private
|
|
|
|
*/
|
2015-04-24 12:38:20 +02:00
|
|
|
this.spanEl = null;
|
2016-09-03 05:19:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* show/hide columns button element
|
|
|
|
* @private
|
|
|
|
*/
|
2015-04-24 12:38:20 +02:00
|
|
|
this.btnEl = null;
|
2016-09-03 05:19:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* show/hide columns main container element
|
|
|
|
* @private
|
|
|
|
*/
|
2015-04-24 12:38:20 +02:00
|
|
|
this.contEl = null;
|
2015-04-12 12:24:32 +02:00
|
|
|
|
2016-09-03 05:19:55 +02:00
|
|
|
/**
|
|
|
|
* Enable tick to hide a column, defaults to true
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.tickToHide = defaultsBool(f.tick_to_hide, true);
|
2016-09-03 05:19:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable columns manager UI, defaults to true
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.manager = defaultsBool(f.manager, true);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Headers HTML table reference only if headers are external
|
|
|
|
* @type {DOMElement}
|
|
|
|
*/
|
|
|
|
this.headersTbl = f.headers_table || null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Headers row index only if headers are external
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.headersIndex = defaultsNb(f.headers_index, 1);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of main container element
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.contElTgtId = defaultsStr(f.container_target_id, null);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alternative text for column headers in column manager UI
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.headersText = defaultsArr(f.headers_text, []);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of button's container element
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnTgtId = defaultsStr(f.btn_target_id, null);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Button's text, defaults to Columns▼
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnText = defaultsStr(f.btn_text, 'Columns▼');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Button's inner HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnHtml = defaultsStr(f.btn_html, null);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css class for button
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnCssClass = defaultsStr(f.btn_css_class, 'colVis');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Columns manager UI close link text, defaults to 'Close'
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnCloseText = defaultsStr(f.btn_close_text, 'Close');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Columns manager UI close link HTML
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnCloseHtml = defaultsStr(f.btn_close_html, null);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css for columns manager UI close link
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.btnCloseCssClass = defaultsStr(f.btn_close_css_class,
|
|
|
|
this.btnCssClass);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension's stylesheet filename
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.stylesheet = defaultsStr(f.stylesheet, 'colsVisibility.css');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css for columns manager UI span
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.spanCssClass = defaultsStr(f.span_css_class, 'colVisSpan');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css for columns manager UI main container
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.contCssClass = defaultsStr(f.cont_css_class, 'colVisCont');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css for columns manager UI checklist (ul)
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.listCssClass = defaultsStr(cfg.list_css_class, 'cols_checklist');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css for columns manager UI checklist item (li)
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.listItemCssClass = defaultsStr(cfg.checklist_item_css_class,
|
|
|
|
'cols_checklist_item');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Css for columns manager UI checklist item selected state (li)
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.listSlcItemCssClass = defaultsStr(
|
|
|
|
cfg.checklist_selected_item_css_class,
|
|
|
|
'cols_checklist_slc_item'
|
|
|
|
);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text preceding the columns list, defaults to 'Hide' or 'Show'
|
|
|
|
* depending on tick mode (tick_to_hide option)
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.text = defaultsStr(f.text, this.tickToHide ? 'Hide: ' : 'Show: ');
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of columns indexes to be hidden at initialization
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.atStart = defaultsArr(f.at_start, []);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable hover behaviour on columns manager button/link
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2015-04-24 12:38:20 +02:00
|
|
|
this.enableHover = Boolean(f.enable_hover);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable select all option, disabled by default
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2015-04-24 12:38:20 +02:00
|
|
|
this.enableTickAll = Boolean(f.enable_tick_all);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text for select all option, defaults to 'Select all:'
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.tickAllText = defaultsStr(f.tick_all_text, 'Select all:');
|
2015-04-24 12:38:20 +02:00
|
|
|
|
2016-09-03 15:30:31 +02:00
|
|
|
/**
|
|
|
|
* List of indexes of hidden columns
|
|
|
|
* @private
|
|
|
|
*/
|
2015-04-24 12:38:20 +02:00
|
|
|
this.hiddenCols = [];
|
2015-04-15 09:09:20 +02:00
|
|
|
|
2017-01-07 12:51:49 +01:00
|
|
|
/**
|
|
|
|
* Bound mouseup wrapper
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.boundMouseup = null;
|
|
|
|
|
2016-09-03 15:30:31 +02:00
|
|
|
/**
|
|
|
|
* Callback fired when the extension is initialized
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onLoaded = defaultsFn(f.on_loaded, EMPTY_FN);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired before the columns manager is opened
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onBeforeOpen = defaultsFn(f.on_before_open, EMPTY_FN);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired after the columns manager is opened
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onAfterOpen = defaultsFn(f.on_after_open, EMPTY_FN);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired before the columns manager is closed
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onBeforeClose = defaultsFn(f.on_before_close, EMPTY_FN);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired after the columns manager is closed
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onAfterClose = defaultsFn(f.on_after_close, EMPTY_FN);
|
2015-04-15 09:09:20 +02:00
|
|
|
|
2016-09-03 15:30:31 +02:00
|
|
|
/**
|
|
|
|
* Callback fired before a column is hidden
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onBeforeColHidden = defaultsFn(f.on_before_col_hidden, EMPTY_FN);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired after a column is hidden
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onAfterColHidden = defaultsFn(f.on_after_col_hidden, EMPTY_FN);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired before a column is displayed
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onBeforeColDisplayed = defaultsFn(f.on_before_col_displayed,
|
|
|
|
EMPTY_FN);
|
2016-09-03 15:30:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback fired after a column is displayed
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
2017-07-09 07:11:51 +02:00
|
|
|
this.onAfterColDisplayed = defaultsFn(f.on_after_col_displayed,
|
|
|
|
EMPTY_FN);
|
2015-04-15 09:09:20 +02:00
|
|
|
|
2016-09-03 15:30:31 +02:00
|
|
|
//Grid layout support
|
2016-04-17 11:59:52 +02:00
|
|
|
if (tf.gridLayout) {
|
2015-06-07 17:31:15 +02:00
|
|
|
this.headersTbl = tf.feature('gridLayout').headTbl; //headers table
|
2015-04-24 12:38:20 +02:00
|
|
|
this.headersIndex = 0; //headers index
|
|
|
|
}
|
2015-04-15 09:09:20 +02:00
|
|
|
|
|
|
|
//Loads extension stylesheet
|
2017-07-16 02:52:40 +02:00
|
|
|
tf.import(f.name + 'Style', tf.getStylePath() + this.stylesheet, null,
|
2016-04-17 11:59:52 +02:00
|
|
|
'link');
|
2015-04-15 09:09:20 +02:00
|
|
|
|
2016-05-14 14:09:28 +02:00
|
|
|
this.enable();
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 12:51:49 +01:00
|
|
|
/**
|
|
|
|
* Mouse-up event handler handling popup auto-close behaviour
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
onMouseup(evt) {
|
|
|
|
let targetElm = targetEvt(evt);
|
|
|
|
|
|
|
|
while (targetElm && targetElm !== this.contEl
|
|
|
|
&& targetElm !== this.btnEl) {
|
|
|
|
targetElm = targetElm.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetElm !== this.contEl && targetElm !== this.btnEl) {
|
|
|
|
this.toggle();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-03 15:30:31 +02:00
|
|
|
/**
|
|
|
|
* Toggle columns manager UI
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
toggle() {
|
2017-01-07 12:51:49 +01:00
|
|
|
// ensure mouseup event handler is removed
|
|
|
|
removeEvt(root, 'mouseup', this.boundMouseup);
|
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
let contDisplay = this.contEl.style.display;
|
2015-04-19 13:05:33 +02:00
|
|
|
|
2016-12-22 12:01:59 +01:00
|
|
|
if (contDisplay !== 'inline') {
|
|
|
|
this.onBeforeOpen(this);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
2016-12-22 12:01:59 +01:00
|
|
|
if (contDisplay === 'inline') {
|
|
|
|
this.onBeforeClose(this);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
this.contEl.style.display = contDisplay === 'inline' ?
|
2017-06-25 13:51:15 +02:00
|
|
|
NONE : 'inline';
|
2015-04-17 07:01:06 +02:00
|
|
|
|
2016-12-22 12:01:59 +01:00
|
|
|
if (contDisplay !== 'inline') {
|
|
|
|
this.onAfterOpen(this);
|
2017-01-07 12:51:49 +01:00
|
|
|
addEvt(root, 'mouseup', this.boundMouseup);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
2016-12-22 12:01:59 +01:00
|
|
|
if (contDisplay === 'inline') {
|
|
|
|
this.onAfterClose(this);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-03 15:30:31 +02:00
|
|
|
/**
|
|
|
|
* Check an item in columns manager UI
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
checkItem(lbl) {
|
|
|
|
let li = lbl.parentNode;
|
|
|
|
if (!li || !lbl) {
|
2015-04-19 13:05:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-04-17 11:59:52 +02:00
|
|
|
let isChecked = lbl.firstChild.checked;
|
|
|
|
let colIndex = lbl.firstChild.getAttribute('id').split('_')[1];
|
2015-04-24 12:38:20 +02:00
|
|
|
colIndex = parseInt(colIndex, 10);
|
2016-04-17 11:59:52 +02:00
|
|
|
if (isChecked) {
|
2016-05-24 10:42:11 +02:00
|
|
|
addClass(li, this.listSlcItemCssClass);
|
2015-04-19 13:05:33 +02:00
|
|
|
} else {
|
2016-05-24 10:42:11 +02:00
|
|
|
removeClass(li, this.listSlcItemCssClass);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
2015-04-24 12:38:20 +02:00
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
let hide = false;
|
|
|
|
if ((this.tickToHide && isChecked) ||
|
|
|
|
(!this.tickToHide && !isChecked)) {
|
2015-04-19 13:05:33 +02:00
|
|
|
hide = true;
|
|
|
|
}
|
|
|
|
this.setHidden(colIndex, hide);
|
2015-04-15 09:09:20 +02:00
|
|
|
}
|
|
|
|
|
2016-09-03 15:30:31 +02:00
|
|
|
/**
|
|
|
|
* Initializes ColsVisibility instance
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
init() {
|
2016-05-14 14:09:28 +02:00
|
|
|
if (this.initialized || !this.manager) {
|
2015-04-20 09:20:28 +02:00
|
|
|
return;
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
2016-04-20 16:03:35 +02:00
|
|
|
|
|
|
|
this.emitter.on(['hide-column'],
|
|
|
|
(tf, colIndex) => this.hideCol(colIndex));
|
|
|
|
|
2015-04-20 09:20:28 +02:00
|
|
|
this.buildBtn();
|
|
|
|
this.buildManager();
|
2015-04-24 12:38:20 +02:00
|
|
|
|
2017-01-07 12:51:49 +01:00
|
|
|
/** @inherited */
|
2015-04-24 12:38:20 +02:00
|
|
|
this.initialized = true;
|
2016-09-04 10:17:54 +02:00
|
|
|
|
2017-01-07 12:51:49 +01:00
|
|
|
this.boundMouseup = this.onMouseup.bind(this);
|
|
|
|
|
2016-04-28 06:05:24 +02:00
|
|
|
this.emitter.emit('columns-visibility-initialized', this.tf, this);
|
2016-04-20 10:41:20 +02:00
|
|
|
|
2016-09-03 15:30:31 +02:00
|
|
|
// Hide columns at start at very end of initialization, do not move
|
|
|
|
// as order is important
|
2016-04-20 16:03:35 +02:00
|
|
|
this._hideAtStart();
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
|
|
|
* Build main button UI
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
buildBtn() {
|
|
|
|
if (this.btnEl) {
|
2015-04-17 07:01:06 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-04-17 11:59:52 +02:00
|
|
|
let tf = this.tf;
|
2016-11-02 13:01:31 +01:00
|
|
|
let span = createElm('span');
|
2015-04-24 12:38:20 +02:00
|
|
|
span.className = this.spanCssClass;
|
2015-04-17 07:01:06 +02:00
|
|
|
|
|
|
|
//Container element (rdiv or custom element)
|
2016-04-17 11:59:52 +02:00
|
|
|
if (!this.btnTgtId) {
|
2015-04-19 13:05:33 +02:00
|
|
|
tf.setToolbar();
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
2016-05-25 09:31:53 +02:00
|
|
|
let targetEl = !this.btnTgtId ? tf.rDiv : elm(this.btnTgtId);
|
2015-04-15 09:09:20 +02:00
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
if (!this.btnTgtId) {
|
|
|
|
let firstChild = targetEl.firstChild;
|
2015-04-24 12:38:20 +02:00
|
|
|
firstChild.parentNode.insertBefore(span, firstChild);
|
2015-04-17 07:01:06 +02:00
|
|
|
} else {
|
2015-04-24 12:38:20 +02:00
|
|
|
targetEl.appendChild(span);
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
if (!this.btnHtml) {
|
2016-05-24 10:42:11 +02:00
|
|
|
let btn = createElm('a', ['href', 'javascript:;']);
|
2015-04-24 12:38:20 +02:00
|
|
|
btn.className = this.btnCssClass;
|
2015-06-06 12:06:15 +02:00
|
|
|
btn.title = this.desc;
|
2015-04-17 07:01:06 +02:00
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
btn.innerHTML = this.btnText;
|
|
|
|
span.appendChild(btn);
|
2016-04-17 11:59:52 +02:00
|
|
|
if (!this.enableHover) {
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(btn, 'click', (evt) => this.toggle(evt));
|
2015-04-17 07:01:06 +02:00
|
|
|
} else {
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(btn, 'mouseover', (evt) => this.toggle(evt));
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
} else { //Custom html
|
2015-04-24 12:38:20 +02:00
|
|
|
span.innerHTML = this.btnHtml;
|
2016-04-17 11:59:52 +02:00
|
|
|
let colVisEl = span.firstChild;
|
|
|
|
if (!this.enableHover) {
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(colVisEl, 'click', (evt) => this.toggle(evt));
|
2015-04-17 07:01:06 +02:00
|
|
|
} else {
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(colVisEl, 'mouseover', (evt) => this.toggle(evt));
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
this.spanEl = span;
|
|
|
|
this.btnEl = this.spanEl.firstChild;
|
2015-04-17 07:01:06 +02:00
|
|
|
|
2016-12-22 12:01:59 +01:00
|
|
|
this.onLoaded(this);
|
2015-04-12 10:32:24 +02:00
|
|
|
}
|
2015-04-17 07:01:06 +02:00
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
|
|
|
* Build columns manager UI
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
buildManager() {
|
|
|
|
let tf = this.tf;
|
2015-04-17 07:01:06 +02:00
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
let container = !this.contElTgtId ?
|
2016-11-02 13:01:31 +01:00
|
|
|
createElm('div') :
|
2016-05-25 09:31:53 +02:00
|
|
|
elm(this.contElTgtId);
|
2015-04-24 12:38:20 +02:00
|
|
|
container.className = this.contCssClass;
|
2015-04-17 07:01:06 +02:00
|
|
|
|
|
|
|
//Extension description
|
2016-05-24 10:42:11 +02:00
|
|
|
let extNameLabel = createElm('p');
|
2015-04-24 12:38:20 +02:00
|
|
|
extNameLabel.innerHTML = this.text;
|
2015-04-17 07:01:06 +02:00
|
|
|
container.appendChild(extNameLabel);
|
|
|
|
|
|
|
|
//Headers list
|
2016-11-02 13:01:31 +01:00
|
|
|
let ul = createElm('ul');
|
2015-04-24 12:38:20 +02:00
|
|
|
ul.className = this.listCssClass;
|
2015-04-17 07:01:06 +02:00
|
|
|
|
2017-06-25 13:51:15 +02:00
|
|
|
let tbl = this.headersTbl || tf.dom();
|
2016-04-17 11:59:52 +02:00
|
|
|
let headerIndex = this.headersTbl ?
|
2015-04-24 12:38:20 +02:00
|
|
|
this.headersIndex : tf.getHeadersRowIndex();
|
2016-04-17 11:59:52 +02:00
|
|
|
let headerRow = tbl.rows[headerIndex];
|
2015-04-17 07:01:06 +02:00
|
|
|
|
|
|
|
//Tick all option
|
2016-04-17 11:59:52 +02:00
|
|
|
if (this.enableTickAll) {
|
2016-05-24 10:42:11 +02:00
|
|
|
let li = createCheckItem('col__' + tf.id, this.tickAllText,
|
|
|
|
this.tickAllText);
|
|
|
|
addClass(li, this.listItemCssClass);
|
2015-04-17 07:01:06 +02:00
|
|
|
ul.appendChild(li);
|
2015-04-24 12:38:20 +02:00
|
|
|
li.check.checked = !this.tickToHide;
|
2015-04-17 08:32:42 +02:00
|
|
|
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(li.check, 'click', () => {
|
2016-04-17 11:59:52 +02:00
|
|
|
for (let h = 0; h < headerRow.cells.length; h++) {
|
2016-05-25 09:31:53 +02:00
|
|
|
let itm = elm('col_' + h + '_' + tf.id);
|
2016-04-17 11:59:52 +02:00
|
|
|
if (itm && li.check.checked !== itm.checked) {
|
2015-04-17 08:32:42 +02:00
|
|
|
itm.click();
|
2015-04-24 12:38:20 +02:00
|
|
|
itm.checked = li.check.checked;
|
2015-04-17 08:32:42 +02:00
|
|
|
}
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
2015-04-17 08:32:42 +02:00
|
|
|
});
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
for (let i = 0; i < headerRow.cells.length; i++) {
|
|
|
|
let cell = headerRow.cells[i];
|
2017-07-09 07:11:51 +02:00
|
|
|
let cellText = this.headersText[i] || this._getHeaderText(cell);
|
2016-05-24 10:42:11 +02:00
|
|
|
let liElm = createCheckItem('col_' + i + '_' + tf.id, cellText,
|
|
|
|
cellText);
|
|
|
|
addClass(liElm, this.listItemCssClass);
|
2016-04-17 11:59:52 +02:00
|
|
|
if (!this.tickToHide) {
|
2016-05-24 10:42:11 +02:00
|
|
|
addClass(liElm, this.listSlcItemCssClass);
|
2015-04-17 08:32:42 +02:00
|
|
|
}
|
|
|
|
ul.appendChild(liElm);
|
2016-04-17 11:59:52 +02:00
|
|
|
if (!this.tickToHide) {
|
2015-04-17 08:32:42 +02:00
|
|
|
liElm.check.checked = true;
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
2015-04-24 12:38:20 +02:00
|
|
|
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(liElm.check, 'click', (evt) => {
|
|
|
|
let elm = targetEvt(evt);
|
2016-04-17 11:59:52 +02:00
|
|
|
let lbl = elm.parentNode;
|
2015-04-20 09:20:28 +02:00
|
|
|
this.checkItem(lbl);
|
2015-04-17 08:32:42 +02:00
|
|
|
});
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//separator
|
2016-05-24 10:42:11 +02:00
|
|
|
let p = createElm('p', ['align', 'center']);
|
2016-04-17 11:59:52 +02:00
|
|
|
let btn;
|
2015-04-17 07:01:06 +02:00
|
|
|
//Close link
|
2016-04-17 11:59:52 +02:00
|
|
|
if (!this.btnCloseHtml) {
|
2016-05-24 10:42:11 +02:00
|
|
|
btn = createElm('a', ['href', 'javascript:;']);
|
2015-04-24 12:38:20 +02:00
|
|
|
btn.className = this.btnCloseCssClass;
|
|
|
|
btn.innerHTML = this.btnCloseText;
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(btn, 'click', (evt) => this.toggle(evt));
|
2015-04-17 07:01:06 +02:00
|
|
|
p.appendChild(btn);
|
|
|
|
} else {
|
2015-04-24 12:38:20 +02:00
|
|
|
p.innerHTML = this.btnCloseHtml;
|
2015-04-17 08:32:42 +02:00
|
|
|
btn = p.firstChild;
|
2016-06-02 06:13:56 +02:00
|
|
|
addEvt(btn, 'click', (evt) => this.toggle(evt));
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
container.appendChild(ul);
|
|
|
|
container.appendChild(p);
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
this.btnEl.parentNode.insertBefore(container, this.btnEl);
|
|
|
|
this.contEl = container;
|
2015-04-17 08:32:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
|
|
|
* Hide or show specified columns
|
2016-09-03 15:30:31 +02:00
|
|
|
* @param {Number} colIndex Column index
|
2016-04-17 11:59:52 +02:00
|
|
|
* @param {Boolean} hide Hide column if true or show if false
|
2015-04-24 12:38:20 +02:00
|
|
|
*/
|
2016-04-19 15:28:37 +02:00
|
|
|
setHidden(colIndex, hide) {
|
2016-04-17 11:59:52 +02:00
|
|
|
let tf = this.tf;
|
2017-04-14 11:30:58 +02:00
|
|
|
let tbl = tf.dom();
|
2015-04-24 12:38:20 +02:00
|
|
|
|
2016-12-22 12:01:59 +01:00
|
|
|
if (hide) {
|
|
|
|
this.onBeforeColHidden(this, colIndex);
|
2015-04-24 12:38:20 +02:00
|
|
|
}
|
2016-12-22 12:01:59 +01:00
|
|
|
if (!hide) {
|
|
|
|
this.onBeforeColDisplayed(this, colIndex);
|
2015-04-24 12:38:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this._hideCells(tbl, colIndex, hide);
|
2016-04-17 11:59:52 +02:00
|
|
|
if (this.headersTbl) {
|
2015-04-24 12:38:20 +02:00
|
|
|
this._hideCells(this.headersTbl, colIndex, hide);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
let hiddenCols = this.hiddenCols;
|
|
|
|
let itemIndex = hiddenCols.indexOf(colIndex);
|
|
|
|
if (hide) {
|
|
|
|
if (itemIndex === -1) {
|
2015-04-24 12:38:20 +02:00
|
|
|
this.hiddenCols.push(colIndex);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-17 11:59:52 +02:00
|
|
|
if (itemIndex !== -1) {
|
2015-04-24 12:38:20 +02:00
|
|
|
this.hiddenCols.splice(itemIndex, 1);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 11:04:50 +02:00
|
|
|
if (hide) {
|
2016-12-22 12:01:59 +01:00
|
|
|
this.onAfterColHidden(this, colIndex);
|
2016-04-18 11:04:50 +02:00
|
|
|
this.emitter.emit('column-hidden', tf, this, colIndex,
|
|
|
|
this.hiddenCols);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 11:04:50 +02:00
|
|
|
if (!hide) {
|
2016-12-22 12:01:59 +01:00
|
|
|
this.onAfterColDisplayed(this, colIndex);
|
2016-04-18 11:04:50 +02:00
|
|
|
this.emitter.emit('column-shown', tf, this, colIndex,
|
|
|
|
this.hiddenCols);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
|
|
|
* Show specified column
|
|
|
|
* @param {Number} colIndex Column index
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
showCol(colIndex) {
|
2017-06-25 13:51:15 +02:00
|
|
|
if (isUndef(colIndex) || !this.isColHidden(colIndex)) {
|
2015-04-19 13:05:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-04-17 11:59:52 +02:00
|
|
|
if (this.manager && this.contEl) {
|
2016-05-25 09:31:53 +02:00
|
|
|
let itm = elm('col_' + colIndex + '_' + this.tf.id);
|
2016-04-17 11:59:52 +02:00
|
|
|
if (itm) {
|
|
|
|
itm.click();
|
|
|
|
}
|
2015-04-19 13:05:33 +02:00
|
|
|
} else {
|
|
|
|
this.setHidden(colIndex, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
|
|
|
* Hide specified column
|
|
|
|
* @param {Number} colIndex Column index
|
|
|
|
*/
|
2016-04-20 10:41:20 +02:00
|
|
|
hideCol(colIndex) {
|
2017-06-25 13:51:15 +02:00
|
|
|
if (isUndef(colIndex) || this.isColHidden(colIndex)) {
|
2015-04-19 13:05:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-04-17 11:59:52 +02:00
|
|
|
if (this.manager && this.contEl) {
|
2016-05-25 09:31:53 +02:00
|
|
|
let itm = elm('col_' + colIndex + '_' + this.tf.id);
|
2016-04-17 11:59:52 +02:00
|
|
|
if (itm) {
|
|
|
|
itm.click();
|
|
|
|
}
|
2015-04-19 13:05:33 +02:00
|
|
|
} else {
|
|
|
|
this.setHidden(colIndex, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
|
|
|
* Determine if specified column is hidden
|
|
|
|
* @param {Number} colIndex Column index
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
isColHidden(colIndex) {
|
|
|
|
if (this.hiddenCols.indexOf(colIndex) !== -1) {
|
2015-04-19 13:05:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
|
|
|
* Toggle visibility of specified column
|
|
|
|
* @param {Number} colIndex Column index
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
toggleCol(colIndex) {
|
2017-06-25 13:51:15 +02:00
|
|
|
if (isUndef(colIndex) || this.isColHidden(colIndex)) {
|
2015-04-24 12:38:20 +02:00
|
|
|
this.showCol(colIndex);
|
2015-04-19 13:05:33 +02:00
|
|
|
} else {
|
2015-04-24 12:38:20 +02:00
|
|
|
this.hideCol(colIndex);
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
2016-04-20 10:41:20 +02:00
|
|
|
* Return the indexes of the columns currently hidden
|
2015-04-24 12:38:20 +02:00
|
|
|
* @return {Array} column indexes
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
getHiddenCols() {
|
2015-04-24 12:38:20 +02:00
|
|
|
return this.hiddenCols;
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
/**
|
|
|
|
* Remove the columns manager
|
|
|
|
*/
|
2016-04-17 11:59:52 +02:00
|
|
|
destroy() {
|
2016-05-14 14:09:28 +02:00
|
|
|
if (!this.initialized) {
|
2015-04-19 13:05:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-05-25 09:31:53 +02:00
|
|
|
if (elm(this.contElTgtId)) {
|
|
|
|
elm(this.contElTgtId).innerHTML = '';
|
2015-04-19 13:05:33 +02:00
|
|
|
} else {
|
2015-04-24 12:38:20 +02:00
|
|
|
this.contEl.innerHTML = '';
|
2016-05-24 10:42:11 +02:00
|
|
|
removeElm(this.contEl);
|
2015-04-24 12:38:20 +02:00
|
|
|
this.contEl = null;
|
|
|
|
}
|
|
|
|
this.btnEl.innerHTML = '';
|
2016-05-24 10:42:11 +02:00
|
|
|
removeElm(this.btnEl);
|
2015-04-24 12:38:20 +02:00
|
|
|
this.btnEl = null;
|
2016-04-18 11:04:50 +02:00
|
|
|
|
|
|
|
this.emitter.off(['hide-column'],
|
2016-04-19 15:28:37 +02:00
|
|
|
(tf, colIndex) => this.hideCol(colIndex));
|
|
|
|
|
2017-01-07 12:51:49 +01:00
|
|
|
this.boundMouseup = null;
|
|
|
|
|
2015-04-24 12:38:20 +02:00
|
|
|
this.initialized = false;
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
2015-04-17 08:32:42 +02:00
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
_getHeaderText(cell) {
|
|
|
|
if (!cell.hasChildNodes) {
|
2015-04-17 08:32:42 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
for (let i = 0; i < cell.childNodes.length; i++) {
|
|
|
|
let n = cell.childNodes[i];
|
|
|
|
if (n.nodeType === 3) {
|
2015-04-17 08:32:42 +02:00
|
|
|
return n.nodeValue;
|
2016-04-17 11:59:52 +02:00
|
|
|
} else if (n.nodeType === 1) {
|
|
|
|
if (n.id && n.id.indexOf('popUp') !== -1) {
|
2015-04-17 08:32:42 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
2016-05-24 10:42:11 +02:00
|
|
|
return getText(n);
|
2015-04-17 08:32:42 +02:00
|
|
|
}
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
2015-04-17 08:32:42 +02:00
|
|
|
continue;
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
2015-04-17 08:32:42 +02:00
|
|
|
return '';
|
2015-04-17 07:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 11:59:52 +02:00
|
|
|
_hideCells(tbl, colIndex, hide) {
|
|
|
|
for (let i = 0; i < tbl.rows.length; i++) {
|
|
|
|
let row = tbl.rows[i];
|
|
|
|
let cell = row.cells[colIndex];
|
|
|
|
if (cell) {
|
2017-06-25 13:51:15 +02:00
|
|
|
cell.style.display = hide ? NONE : '';
|
2015-04-19 13:05:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:03:35 +02:00
|
|
|
_hideAtStart() {
|
|
|
|
this.atStart.forEach((colIdx) => {
|
|
|
|
this.hideCol(colIdx);
|
|
|
|
});
|
|
|
|
}
|
2015-04-12 10:32:24 +02:00
|
|
|
}
|