simple-keyboard/build/index.js
Francisco Hodge 280956dba3 Build update
2018-10-06 02:34:06 -04:00

13 lines
14 KiB
JavaScript

/*!
*
* simple-keyboard v2.5.0
* https://github.com/hodgef/simple-keyboard
*
* Copyright (c) Francisco Hodge (https://github.com/hodgef)
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
!function(t,n){"object"===typeof exports&&"object"===typeof module?module.exports=n():"function"===typeof define&&define.amd?define([],n):"object"===typeof exports?exports.SimpleKeyboard=n():t.SimpleKeyboard=n()}(this,function(){return function(t){function n(o){if(e[o])return e[o].exports;var i=e[o]={i:o,l:!1,exports:{}};return t[o].call(i.exports,i,i.exports,n),i.l=!0,i.exports}var e={};return n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:o})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=0)}([function(t,n,e){t.exports=e(1)},function(t,n,e){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var o=e(2);n.default=o.a},function(t,n,e){"use strict";function o(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}var i=e(3),s=(e.n(i),e(4)),a=e(5),r=e(6),u="function"===typeof Symbol&&"symbol"===typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"===typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},c=function t(){var n=this;o(this,t),this.handleButtonClicked=function(t){var e=n.options.debug;if("{//}"===t)return!1;"function"===typeof n.options.onKeyPress&&n.options.onKeyPress(t),n.input[n.options.inputName]||(n.input[n.options.inputName]="");var o=n.utilities.getUpdatedInput(t,n.input[n.options.inputName],n.options,n.caretPosition);if(n.input[n.options.inputName]!==o){if(n.options.maxLength&&n.utilities.handleMaxLength(n.input,n.options,o))return!1;n.input[n.options.inputName]=o,e&&console.log("Input changed:",n.input),n.options.syncInstanceInputs&&n.syncInstanceInputs(n.input),"function"===typeof n.options.onChange&&n.options.onChange(n.input[n.options.inputName])}e&&console.log("Key pressed:",t)},this.syncInstanceInputs=function(){n.dispatch(function(t){t.replaceInput(n.input)})},this.clearInput=function(t){t=t||n.options.inputName,n.input[n.options.inputName]="",n.options.syncInstanceInputs&&n.syncInstanceInputs(n.input)},this.getInput=function(t){return t=t||n.options.inputName,n.options.syncInstanceInputs&&n.syncInstanceInputs(n.input),n.input[n.options.inputName]},this.setInput=function(t,e){e=e||n.options.inputName,n.input[e]=t,n.options.syncInstanceInputs&&n.syncInstanceInputs(n.input)},this.replaceInput=function(t){n.input=t},this.setOptions=function(t){t=t||{},n.options=Object.assign(n.options,t),n.render()},this.clear=function(){n.keyboardDOM.innerHTML="",n.keyboardDOM.className=n.keyboardDOMClass,n.buttonElements={},n.caretPosition=null},this.dispatch=function(t){return window.SimpleKeyboardInstances?Object.keys(window.SimpleKeyboardInstances).forEach(function(n){t(window.SimpleKeyboardInstances[n],n)}):(console.error("SimpleKeyboardInstances is not defined. Dispatch cannot be called."),!1)},this.addButtonTheme=function(t,e){if(!e||!t)return!1;t.split(" ").forEach(function(o){e.split(" ").forEach(function(e){n.options.buttonTheme||(n.options.buttonTheme=[]);var i=!1;n.options.buttonTheme.map(function(t){if(t.class.split(" ").includes(e)){i=!0;var n=t.buttons.split(" ");n.includes(o)||(i=!0,n.push(o),t.buttons=n.join(" "))}return t}),i||n.options.buttonTheme.push({class:e,buttons:t})})}),n.render()},this.removeButtonTheme=function(t,e){if(!t&&!e)return n.options.buttonTheme=[],n.render(),!1;if(t&&Array.isArray(n.options.buttonTheme)&&n.options.buttonTheme.length){var o=t.split(" ");o.forEach(function(t,i){n.options.buttonTheme.map(function(i,s){if(e&&e.includes(i.class)||!e){var a=void 0;o.includes(t)&&(a=i.buttons.split(" ").filter(function(n){return n!==t})),a.length?i.buttons=a.join(" "):(n.options.buttonTheme.splice(s,1),i=null)}return i})}),n.render()}},this.getButtonElement=function(t){var e=void 0,o=n.buttonElements[t];return o&&(e=o.length>1?o:o[0]),e},this.handleCaret=function(){n.options.debug&&console.log("Caret handling started");var t=function(t){var e=t.target.tagName.toLowerCase();"textarea"!==e&&"input"!==e||(n.caretPosition=t.target.selectionStart,n.options.debug&&console.log("Caret at: ",t.target.selectionStart,t.target.tagName.toLowerCase()))};document.addEventListener("keyup",t),document.addEventListener("mouseup",t),document.addEventListener("touchend",t)},this.onInit=function(){n.options.debug&&console.log("Initialized"),n.handleCaret(),"function"===typeof n.options.onInit&&n.options.onInit()},this.onRender=function(){"function"===typeof n.options.onRender&&n.options.onRender()},this.render=function(){n.clear();var t=n.options.layout?"hg-layout-custom":"hg-layout-"+n.options.layoutName,e=n.options.layout||a.a.getLayout(n.options.layoutName),o={};Array.isArray(n.options.buttonTheme)&&n.options.buttonTheme.forEach(function(t){if(t.buttons&&t.class){var n=t.buttons.split(" ");Array.isArray(n)&&n.forEach(function(n){var e=o[n];o[n]=e?e+" "+t.class:t.class})}else console.warn('buttonTheme row is missing the "buttons" or the "class". Please check the documentation.')}),n.keyboardDOM.className+=" "+n.options.theme+" "+t,e[n.options.layoutName].forEach(function(t,e){var i=t.split(" "),s=document.createElement("div");s.className+="hg-row",i.forEach(function(t,i){var a=n.utilities.getButtonClass(t),r=o[t],u=n.utilities.getButtonDisplayName(t,n.options.display,n.options.mergeDisplay),c=document.createElement("div");c.className+="hg-button "+a+(r?" "+r:""),c.onclick=function(){return n.handleButtonClicked(t)},c.setAttribute("data-skBtn",t);var p=n.options.layoutName+"-r"+e+"b"+i;c.setAttribute("data-skBtnUID",p);var l=document.createElement("span");l.innerHTML=u,c.appendChild(l),n.buttonElements[t]||(n.buttonElements[t]=[]),n.buttonElements[t].push(c),s.appendChild(c)}),n.keyboardDOM.appendChild(s)}),n.onRender(),n.initialized||(n.initialized=!0,n.onInit())};var e="string"===typeof(arguments.length<=0?void 0:arguments[0])?arguments.length<=0?void 0:arguments[0]:".simple-keyboard",i="object"===u(arguments.length<=0?void 0:arguments[0])?arguments.length<=0?void 0:arguments[0]:arguments.length<=1?void 0:arguments[1];i||(i={}),this.utilities=new r.a(this),this.keyboardDOM=document.querySelector(e),this.options=i,this.options.layoutName=this.options.layoutName||"default",this.options.theme=this.options.theme||"hg-theme-default",this.options.inputName=this.options.inputName||"default",this.input={},this.input[this.options.inputName]="",this.keyboardDOMClass=e.split(".").join(""),this.timers={},this.buttonElements={},this.keyboardDOM?this.render():console.error('"'+e+'" was not found in the DOM.'),window.SimpleKeyboardInstances||(window.SimpleKeyboardInstances={}),window.SimpleKeyboardInstances[this.utilities.camelCase(this.keyboardDOMClass)]=this,this.physicalKeyboardInterface=new s.a(this)};n.a=c},function(t,n){},function(t,n,e){"use strict";function o(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}var i=function t(n){var e=this;if(o(this,t),this.initKeyboardListener=function(){document.addEventListener("keydown",function(t){if(e.simpleKeyboardInstance.options.physicalKeyboardHighlight){var n=e.getSimpleKeyboardLayoutKey(t);e.simpleKeyboardInstance.dispatch(function(t){var o=t.getButtonElement(n)||t.getButtonElement("{"+n+"}");o&&(o.style.backgroundColor=e.simpleKeyboardInstance.options.physicalKeyboardHighlightTextColor||"#9ab4d0",o.style.color=e.simpleKeyboardInstance.options.physicalKeyboardHighlightBgColor||"white")})}}),document.addEventListener("keyup",function(t){if(e.simpleKeyboardInstance.options.physicalKeyboardHighlight){var n=e.getSimpleKeyboardLayoutKey(t);e.simpleKeyboardInstance.dispatch(function(t){var e=t.getButtonElement(n)||t.getButtonElement("{"+n+"}");e&&e.removeAttribute("style")})}})},this.getSimpleKeyboardLayoutKey=function(t){e.simpleKeyboardInstance.options.debug&&console.log(t);var n=void 0;return n=t.code.includes("Numpad")||t.code.includes("Shift")||t.code.includes("Space")||t.code.includes("Backspace")?t.code:t.key,(n!==n.toUpperCase()||"F"===t.code[0]&&Number.isInteger(Number(t.code[1]))&&t.code.length<=3)&&(n=n.toLowerCase()),n},this.simpleKeyboardInstance=n,window.SimpleKeyboardPhysicalKeyboardInit)return!1;window.SimpleKeyboardPhysicalKeyboardInit=!0,this.initKeyboardListener()};n.a=i},function(t,n,e){"use strict";function o(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}var i=function t(){o(this,t)};i.getLayout=function(t){return"qwerty"===t?{default:["` 1 2 3 4 5 6 7 8 9 0 - = {bksp}","{tab} q w e r t y u i o p [ ] \\","{lock} a s d f g h j k l ; ' {enter}","{shift} z x c v b n m , . / {shift}",".com @ {space}"],shift:["~ ! @ # $ % ^ & * ( ) _ + {bksp}","{tab} Q W E R T Y U I O P { } |",'{lock} A S D F G H J K L : " {enter}',"{shift} Z X C V B N M < > ? {shift}",".com @ {space}"]}:"numeric"===t?{default:["1 2 3","4 5 6","7 8 9","{//} 0 {bksp}"]}:i.getLayout("qwerty")},n.a=i},function(t,n,e){"use strict";function o(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}var i="function"===typeof Symbol&&"symbol"===typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"===typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},s=function(){function t(t,n){for(var e=0;e<n.length;e++){var o=n[e];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}return function(n,e,o){return e&&t(n.prototype,e),o&&t(n,o),n}}(),a=function(){function t(n){var e=this;o(this,t),this.getButtonClass=function(t){var n=t.includes("{")&&"{//}"!==t?"functionBtn":"standardBtn",o=t.replace("{","").replace("}","");return"hg-"+n+("standardBtn"===n?e.normalizeString(o):" hg-button-"+o)},this.getButtonDisplayName=function(t,n,o){return n=o?Object.assign({},e.getDefaultDiplay(),n):n||e.getDefaultDiplay(),n[t]||t},this.getUpdatedInput=function(t,n,o,i){var s=n;return("{bksp}"===t||"{backspace}"===t)&&s.length>0?s=e.removeAt(s,i):"{space}"===t?s=e.addStringAt(s," ",i):"{tab}"!==t||"boolean"===typeof o.tabCharOnTab&&!1===o.tabCharOnTab?"{enter}"!==t&&"{numpadenter}"!==t||!o.newLineOnEnter?t.includes("numpad")&&Number.isInteger(Number(t[t.length-2]))?s=e.addStringAt(s,t[t.length-2],i):"{numpaddivide}"===t?s=e.addStringAt(s,"/",i):"{numpadmultiply}"===t?s=e.addStringAt(s,"*",i):"{numpadsubtract}"===t?s=e.addStringAt(s,"-",i):"{numpadadd}"===t?s=e.addStringAt(s,"+",i):"{numpadadd}"===t?s=e.addStringAt(s,"+",i):"{numpaddecimal}"===t?s=e.addStringAt(s,".",i):t.includes("{")||t.includes("}")||(s=e.addStringAt(s,t,i)):s=e.addStringAt(s,"\n",i):s=e.addStringAt(s,"\t",i),s},this.updateCaretPos=function(t,n){e.simpleKeyboardInstance.caretPosition=n?e.simpleKeyboardInstance.caretPosition-t:e.simpleKeyboardInstance.caretPosition+t},this.camelCase=function(t){return t.toLowerCase().trim().split(/[.\-_\s]/g).reduce(function(t,n){return t+n[0].toUpperCase()+n.slice(1)})},this.simpleKeyboardInstance=n}return s(t,[{key:"normalizeString",value:function(t){var n=void 0;return n="@"===t?"at":","===t?"comma":"."===t?"dot":"\\"===t?"backslash":"/"===t?"fordardslash":"*"===t?"asterisk":"&"===t?"ampersand":"$"===t?"dollarsign":"="===t?"equals":"+"===t?"plus":"-"===t?"minus":"'"===t?"apostrophe":";"===t?"colon":"["===t?"openbracket":"]"===t?"closebracket":"//"===t?"emptybutton":".com"===t?"com":"",n?" hg-button-"+n:""}},{key:"getDefaultDiplay",value:function(){return{"{bksp}":"backspace","{backspace}":"backspace","{enter}":"< enter","{shift}":"shift","{shiftleft}":"shift","{shiftright}":"shift","{alt}":"alt","{s}":"shift","{tab}":"tab","{lock}":"caps","{capslock}":"caps","{accept}":"Submit","{space}":" ","{//}":" ","{esc}":"esc","{escape}":"esc","{f1}":"f1","{f2}":"f2","{f3}":"f3","{f4}":"f4","{f5}":"f5","{f6}":"f6","{f7}":"f7","{f8}":"f8","{f9}":"f9","{f10}":"f10","{f11}":"f11","{f12}":"f12","{numpaddivide}":"/","{numlock}":"lock","{arrowup}":"\u2191","{arrowleft}":"\u2190","{arrowdown}":"\u2193","{arrowright}":"\u2192","{prtscr}":"print","{scrolllock}":"scroll","{pause}":"pause","{insert}":"ins","{home}":"home","{pageup}":"up","{delete}":"del","{end}":"end","{pagedown}":"down","{numpadmultiply}":"*","{numpadsubtract}":"-","{numpadadd}":"+","{numpadenter}":"enter","{period}":".","{numpaddecimal}":".","{numpad0}":"0","{numpad1}":"1","{numpad2}":"2","{numpad3}":"3","{numpad4}":"4","{numpad5}":"5","{numpad6}":"6","{numpad7}":"7","{numpad8}":"8","{numpad9}":"9"}}},{key:"addStringAt",value:function(t,n,e){var o=void 0;return this.simpleKeyboardInstance.options.debug&&console.log("Caret at:",e),e||0===e?(o=[t.slice(0,e),n,t.slice(e)].join(""),this.updateCaretPos(n.length)):o=t+n,o}},{key:"removeAt",value:function(t,n){var e=void 0,o=void 0,i=void 0,s=/([\uD800-\uDBFF][\uDC00-\uDFFF])/g;return n?(o=t.substring(n-2,n),i=o.match(s),i?(e=t.substr(0,n-2)+t.substr(n),this.updateCaretPos(2,!0)):(e=t.substr(0,n-1)+t.substr(n),this.updateCaretPos(1,!0))):(o=t.slice(-2),i=o.match(s),i?(e=t.slice(0,-2),this.updateCaretPos(2,!0)):(e=t.slice(0,-1),this.updateCaretPos(1,!0))),e}},{key:"handleMaxLength",value:function(t,n,e){var o=n.maxLength,s=t[n.inputName],a=s.length===o;if(e.length<=s.length)return!1;if(Number.isInteger(o))return n.debug&&console.log("maxLength (num) reached:",a),!!a;if("object"===("undefined"===typeof o?"undefined":i(o))){var r=s.length===o[n.inputName];return n.debug&&console.log("maxLength (obj) reached:",r),!!r}}}]),t}();n.a=a}])});
//# sourceMappingURL=index.js.map