1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-19 14:56:40 +02:00
TableFilter/src/modules/help.js

161 lines
5.4 KiB
JavaScript
Raw Normal View History

import {Feature} from '../feature';
2016-05-25 09:31:53 +02:00
import {createElm, createText, elm, removeElm} from '../dom';
2016-06-02 06:13:56 +02:00
import {addEvt} from '../event';
import {NONE} from '../const';
const WIKI_URL = 'https://github.com/koalyptus/TableFilter/wiki/' +
'4.-Filter-operators';
const WEBSITE_URL = 'http://koalyptus.github.io/TableFilter/';
export class Help extends Feature {
2015-02-20 05:03:57 +01:00
/**
* Help UI component
* @param {Object} tf TableFilter instance
*/
constructor(tf) {
super(tf, 'help');
var f = this.config;
2015-02-20 05:03:57 +01:00
//id of custom container element for instructions
2015-06-08 12:21:50 +02:00
this.tgtId = f.help_instructions_target_id || null;
2015-02-20 05:03:57 +01:00
//id of custom container element for instructions
2015-06-08 12:21:50 +02:00
this.contTgtId = f.help_instructions_container_target_id ||
2015-02-20 05:03:57 +01:00
null;
//defines help text
2016-02-16 08:41:47 +01:00
this.instrText = f.help_instructions_text ?
2015-02-20 05:03:57 +01:00
f.help_instructions_text :
'Use the filters above each column to filter and limit table ' +
2015-08-23 13:27:32 +02:00
'data. Advanced searches can be performed by using the following ' +
2015-02-20 05:03:57 +01:00
'operators: <br /><b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, ' +
'<b>&gt;=</b>, <b>=</b>, <b>*</b>, <b>!</b>, <b>{</b>, <b>}</b>, ' +
'<b>||</b>,<b>&amp;&amp;</b>, <b>[empty]</b>, <b>[nonempty]</b>, ' +
'<b>rgx:</b><br/><a href="' + WIKI_URL + '" target="_blank">' +
'Learn more</a><hr/>';
2015-02-20 05:03:57 +01:00
//defines help innerHtml
2015-06-08 12:21:50 +02:00
this.instrHtml = f.help_instructions_html || null;
2015-02-20 05:03:57 +01:00
//defines reset button text
2015-06-08 12:21:50 +02:00
this.btnText = f.help_instructions_btn_text || '?';
2015-02-20 05:03:57 +01:00
//defines reset button innerHtml
2015-06-08 12:21:50 +02:00
this.btnHtml = f.help_instructions_btn_html || null;
2015-02-20 05:03:57 +01:00
//defines css class for help button
2015-06-08 12:21:50 +02:00
this.btnCssClass = f.help_instructions_btn_css_class || 'helpBtn';
2015-02-20 05:03:57 +01:00
//defines css class for help container
2015-06-08 12:21:50 +02:00
this.contCssClass = f.help_instructions_container_css_class ||
2015-02-20 05:03:57 +01:00
'helpCont';
//help button element
2015-06-08 12:21:50 +02:00
this.btn = null;
//help content div
2015-06-08 12:21:50 +02:00
this.cont = null;
this.defaultHtml = '<div class="helpFooter"><h4>TableFilter ' +
'v' + tf.version + '</h4>' + '<a href="' + WEBSITE_URL +
'" target="_blank">' + WEBSITE_URL + '</a>' +
'<br/><span>&copy;2015-' + tf.year + ' {AUTHOR}</span>' +
2015-02-20 05:03:57 +01:00
'<div align="center" style="margin-top:8px;">' +
'<a href="javascript:void(0);" class="close">Close</a></div></div>';
2015-02-20 05:03:57 +01:00
//id prefix for help elements
this.prfxHelpSpan = 'helpSpan_';
//id prefix for help elements
this.prfxHelpDiv = 'helpDiv_';
2016-01-19 13:13:34 +01:00
this.emitter.on(['init-help'], () => this.init());
2015-02-20 05:03:57 +01:00
}
/**
* Initialise Help instance
*
* @returns (description)
*/
init() {
if (this.initialized) {
2015-02-20 05:03:57 +01:00
return;
}
var tf = this.tf;
var helpspan = createElm('span', ['id', this.prfxHelpSpan + tf.id]);
var helpdiv = createElm('div', ['id', this.prfxHelpDiv + tf.id]);
2015-02-20 05:03:57 +01:00
//help button is added to defined element
if (!this.tgtId) {
tf.setToolbar();
2015-02-20 05:03:57 +01:00
}
2016-05-25 09:31:53 +02:00
var targetEl = !this.tgtId ? tf.rDiv : elm(this.tgtId);
2015-02-20 05:03:57 +01:00
targetEl.appendChild(helpspan);
2016-05-25 09:31:53 +02:00
var divContainer = !this.contTgtId ? helpspan : elm(this.contTgtId);
2015-02-20 05:03:57 +01:00
if (!this.btnHtml) {
2015-02-20 05:03:57 +01:00
divContainer.appendChild(helpdiv);
2016-05-24 10:42:11 +02:00
var helplink = createElm('a', ['href', 'javascript:void(0);']);
2015-06-08 12:21:50 +02:00
helplink.className = this.btnCssClass;
2016-05-24 10:42:11 +02:00
helplink.appendChild(createText(this.btnText));
2015-02-20 05:03:57 +01:00
helpspan.appendChild(helplink);
2016-06-02 06:13:56 +02:00
addEvt(helplink, 'click', () => this.toggle());
2015-02-20 05:03:57 +01:00
} else {
2015-06-08 12:21:50 +02:00
helpspan.innerHTML = this.btnHtml;
2015-02-20 05:03:57 +01:00
var helpEl = helpspan.firstChild;
2016-06-02 06:13:56 +02:00
addEvt(helpEl, 'click', () => this.toggle());
2015-02-20 05:03:57 +01:00
divContainer.appendChild(helpdiv);
}
if (!this.instrHtml) {
2015-06-08 12:21:50 +02:00
helpdiv.innerHTML = this.instrText;
helpdiv.className = this.contCssClass;
2016-06-02 06:13:56 +02:00
addEvt(helpdiv, 'dblclick', () => this.toggle());
2015-02-20 05:03:57 +01:00
} else {
if (this.contTgtId) {
2015-02-20 05:03:57 +01:00
divContainer.appendChild(helpdiv);
}
2015-06-08 12:21:50 +02:00
helpdiv.innerHTML = this.instrHtml;
if (!this.contTgtId) {
2015-06-08 12:21:50 +02:00
helpdiv.className = this.contCssClass;
2016-06-02 06:13:56 +02:00
addEvt(helpdiv, 'dblclick', () => this.toggle());
2015-02-20 05:03:57 +01:00
}
}
2015-06-08 12:21:50 +02:00
helpdiv.innerHTML += this.defaultHtml;
2016-06-02 06:13:56 +02:00
addEvt(helpdiv, 'click', () => this.toggle());
2015-02-20 05:03:57 +01:00
2015-06-08 12:21:50 +02:00
this.cont = helpdiv;
this.btn = helpspan;
this.initialized = true;
2015-02-20 05:03:57 +01:00
}
/**
* Toggle help pop-up
*/
toggle() {
// check only if explicitily set to false as in this case undefined
// signifies the help feature is enabled by default
if (this.enabled === false) {
2015-02-20 05:03:57 +01:00
return;
}
2015-06-08 12:21:50 +02:00
var divDisplay = this.cont.style.display;
if (divDisplay === '' || divDisplay === NONE) {
this.cont.style.display = 'inline';
2015-02-20 05:03:57 +01:00
} else {
this.cont.style.display = NONE;
2015-02-20 05:03:57 +01:00
}
}
/**
* Remove help UI
*/
destroy() {
if (!this.initialized) {
2015-02-20 05:03:57 +01:00
return;
}
2016-05-24 10:42:11 +02:00
removeElm(this.btn);
2015-06-08 12:21:50 +02:00
this.btn = null;
if (!this.cont) {
2015-02-20 05:03:57 +01:00
return;
}
2016-05-24 10:42:11 +02:00
removeElm(this.cont);
2015-06-08 12:21:50 +02:00
this.cont = null;
this.initialized = false;
2015-02-20 05:03:57 +01:00
}
}