Added buttonAttributes option. Improved buttonTheme handling. Cleanup. Fixes #196

This commit is contained in:
Francisco Hodge 2019-08-11 20:21:21 -04:00
parent 23da05ee60
commit f158a603c9
2 changed files with 64 additions and 61 deletions

View File

@ -51,6 +51,7 @@ class SimpleKeyboard {
* @property {boolean} mergeDisplay By default, when you set the display property, you replace the default one. This setting merges them instead. * @property {boolean} mergeDisplay By default, when you set the display property, you replace the default one. This setting merges them instead.
* @property {string} theme A prop to add your own css classes to the keyboard wrapper. You can add multiple classes separated by a space. * @property {string} theme A prop to add your own css classes to the keyboard wrapper. You can add multiple classes separated by a space.
* @property {Array} buttonTheme A prop to add your own css classes to one or several buttons. * @property {Array} buttonTheme A prop to add your own css classes to one or several buttons.
* @property {Array} buttonAttributes A prop to add your own attributes to one or several buttons.
* @property {boolean} debug Runs a console.log every time a key is pressed. Displays the buttons pressed and the current input. * @property {boolean} debug Runs a console.log every time a key is pressed. Displays the buttons pressed and the current input.
* @property {boolean} newLineOnEnter Specifies whether clicking the ENTER button will input a newline (\n) or not. * @property {boolean} newLineOnEnter Specifies whether clicking the ENTER button will input a newline (\n) or not.
* @property {boolean} tabCharOnTab Specifies whether clicking the TAB button will input a tab character (\t) or not. * @property {boolean} tabCharOnTab Specifies whether clicking the TAB button will input a tab character (\t) or not.
@ -733,47 +734,62 @@ class SimpleKeyboard {
/** /**
* Process buttonTheme option * Process buttonTheme option
*/ */
getButtonTheme() { getButtonThemeClasses(button) {
let buttonThemesParsed = {}; let buttonTheme = this.options.buttonTheme;
let buttonClasses = [];
this.options.buttonTheme.forEach(themeObj => { if (Array.isArray(buttonTheme)) {
if (themeObj.buttons && themeObj.class) { buttonTheme.forEach(themeObj => {
let themeButtons; if (
themeObj.class &&
typeof themeObj.class === "string" &&
(themeObj.buttons && typeof themeObj.buttons === "string")
) {
let themeObjClasses = themeObj.class.split(" ");
let themeObjButtons = themeObj.buttons.split(" ");
if (typeof themeObj.buttons === "string") { if (themeObjButtons.includes(button)) {
themeButtons = themeObj.buttons.split(" "); buttonClasses = [...buttonClasses, ...themeObjClasses];
}
} else {
console.warn(
`Incorrect "buttonTheme". Please check the documentation.`,
themeObj
);
} }
});
}
if (themeButtons) { return buttonClasses;
themeButtons.forEach(themeButton => { }
let themeParsed = buttonThemesParsed[themeButton];
// If the button has already been added /**
if (themeParsed) { * Process buttonAttributes option
// Making sure we don't add duplicate classes, even when buttonTheme has duplicates */
if ( setDOMButtonAttributes(button, callback) {
!this.utilities.countInArray( let buttonAttributes = this.options.buttonAttributes;
themeParsed.split(" "),
themeObj.class if (Array.isArray(buttonAttributes)) {
) buttonAttributes.forEach(attrObj => {
) { if (
buttonThemesParsed[ attrObj.attribute &&
themeButton typeof attrObj.attribute === "string" &&
] = `${themeParsed} ${themeObj.class}`; (attrObj.value && typeof attrObj.value === "string") &&
} (attrObj.buttons && typeof attrObj.buttons === "string")
} else { ) {
buttonThemesParsed[themeButton] = themeObj.class; let attrObjButtons = attrObj.buttons.split(" ");
}
}); if (attrObjButtons.includes(button)) {
callback(attrObj.attribute, attrObj.value);
}
} else {
console.warn(
`Incorrect "buttonAttributes". Please check the documentation.`,
attrObj
);
} }
} else { });
console.warn( }
`buttonTheme row is missing the "buttons" or the "class". Please check the documentation.`
);
}
});
return buttonThemesParsed;
} }
onTouchDeviceDetected() { onTouchDeviceDetected() {
@ -1061,13 +1077,6 @@ class SimpleKeyboard {
let useMouseEvents = this.options.useMouseEvents || false; let useMouseEvents = this.options.useMouseEvents || false;
let disableRowButtonContainers = this.options.disableRowButtonContainers; let disableRowButtonContainers = this.options.disableRowButtonContainers;
/**
* Account for buttonTheme, if set
*/
let buttonThemesParsed = Array.isArray(this.options.buttonTheme)
? this.getButtonTheme()
: {};
/** /**
* Adding themeClass, layoutClass to keyboardDOM * Adding themeClass, layoutClass to keyboardDOM
*/ */
@ -1132,7 +1141,6 @@ class SimpleKeyboard {
* Processing button options * Processing button options
*/ */
let fctBtnClass = this.utilities.getButtonClass(button); let fctBtnClass = this.utilities.getButtonClass(button);
let buttonThemeClass = buttonThemesParsed[button];
let buttonDisplayName = this.utilities.getButtonDisplayName( let buttonDisplayName = this.utilities.getButtonDisplayName(
button, button,
this.options.display, this.options.display,
@ -1144,9 +1152,19 @@ class SimpleKeyboard {
*/ */
let buttonType = this.options.useButtonTag ? "button" : "div"; let buttonType = this.options.useButtonTag ? "button" : "div";
let buttonDOM = document.createElement(buttonType); let buttonDOM = document.createElement(buttonType);
buttonDOM.className += `hg-button ${fctBtnClass}${ buttonDOM.className += `hg-button ${fctBtnClass}`;
buttonThemeClass ? " " + buttonThemeClass : ""
}`; /**
* Adding buttonTheme
*/
buttonDOM.classList.add(...this.getButtonThemeClasses(button));
/**
* Adding buttonAttributes
*/
this.setDOMButtonAttributes(button, (attribute, value) => {
buttonDOM.setAttribute(attribute, value);
});
/** /**
* Handle button click event * Handle button click event
@ -1205,11 +1223,6 @@ class SimpleKeyboard {
let buttonUID = `${this.options.layoutName}-r${rIndex}b${bIndex}`; let buttonUID = `${this.options.layoutName}-r${rIndex}b${bIndex}`;
buttonDOM.setAttribute("data-skBtnUID", buttonUID); buttonDOM.setAttribute("data-skBtnUID", buttonUID);
/**
* Adding display label
*/
buttonDOM.setAttribute("data-displayLabel", buttonDisplayName);
/** /**
* Adding button label to button * Adding button label to button
*/ */

View File

@ -401,16 +401,6 @@ class Utilities {
word.length ? string + word[0].toUpperCase() + word.slice(1) : string word.length ? string + word[0].toUpperCase() + word.slice(1) : string
); );
} }
/**
* Counts the number of duplicates in a given array
*
* @param {Array} array The haystack to search in
* @param {string} value The needle to search for
*/
countInArray(array, value) {
return array.reduce((n, x) => n + (x === value), 0);
}
} }
export default Utilities; export default Utilities;