jquery.keyboard.extension-scramble.min.js 3.5 KB

12
  1. /*! jQuery UI Virtual Keyboard Scramble Extension v1.7.1 */
  2. !function(e){"function"==typeof define&&define.amd?define(["jquery"],e):"object"==typeof module&&"object"==typeof module.exports?module.exports=e(require("jquery")):e(jQuery)}(function(f){"use strict";f.keyboard=f.keyboard||{},f.fn.addScramble=function(e){var o={targetKeys:/[a-z\d]/i,byRow:!0,byKeySet:!1,randomizeOnce:!0,randomizeInput:!1,init:null,sameForAll:!1};return this.each(function(){var b,c=f(this).data("keyboard"),t=c.namespace+"Scramble",a=c.options;c&&!c.scramble_options&&(b=c.scramble_options=f.extend({},o,e),c.extensionNamespace.push(t),b.orig_create=a.create,c.scramble_setup=function(e){var t,a,o,n,r,i,u,s,d,y,l;if(t=e.find("."+f.keyboard.css.keySet),e.length){for(b.byKeySet&&(t=t.eq(0)),a=0;a<t.length;a++)if(o=t.eq(a),u=0,l=[],d=[],s=[],y=[],o.children("button, span, br").each(function(){"BR"===this.tagName?(b.byRow?(l.push(this),d.push(!1),y[u]=l,s[u]=d,l=[],d=[]):(y[u]=this,s[u]=!1),u++):(i=!(1!==(i=f(this).attr("data-value")||"").length||!b.targetKeys.test(i))&&i,b.byRow?(l.push(this),d.push(i)):(y[u]=this,s[u]=i,u++))}),o.find("."+f.keyboard.css.endRow).remove(),b.byRow)for(r=0;r<y.length;r++)for(l=c.shuffle(y[r],s[r]),n=0;n<l.length;n++)o.append(l[n]);else for(l=c.shuffle(y,s),n=0;n<l.length;n++)o.append(l[n]);return b.byKeySet&&(e=c.realign(e)),e}},c.getRandomUInt=function(e){var t=window.crypto||window.msCrypto;if(void 0===t)return Math.floor(Math.random()*e);var a=new Uint32Array(1);return t.getRandomValues(a),a[0]%e},c.shuffle=function(e,t){for(var a,o,n=e.length;0<n;)o=c.getRandomUInt(n),!1===t[n-1]&&n--,!1!==t[n-1]&&!1!==t[o]&&(a=e[--n],e[n]=e[o],e[o]=a);return e},c.realign=function(e){var a,o,n,r=e.find("."+f.keyboard.css.keySet),t=r.eq(0);return r=r.filter(":gt(0)"),t.children().each(function(e,t){o="BR"===t.tagName,n=f(t).attr("data-pos"),r.each(function(e,t){a=o?"br:first":'button[data-pos="'+n+'"]',f(t).find(a).appendTo(t)})}),e},c.setScrambleLayout=function(){if(!/^scrambled/.test(a.layout)){c.orig_layout=a.layout;var e=e||"scrambled"+Math.round(1e4*Math.random());a.layout=b.sameForAll?e:"scrambled"+Math.round(1e4*Math.random())}},a.create=function(){var e=a.layout;f.keyboard.builtLayouts[e]={mappedKeys:{},acceptedKeys:[],$keyboard:null},c.layout=a.layout=c.orig_layout,c.buildKeyboard(c.layout,!0),c.layout=a.layout=e,f.keyboard.builtLayouts[e]=f.extend(!0,{},f.keyboard.builtLayouts[c.orig_layout]),b.randomizeOnce&&(f.keyboard.builtLayouts[e].$keyboard=c.scramble_setup(f.keyboard.builtLayouts[c.orig_layout].$keyboard.clone())),c.$keyboard=f.keyboard.builtLayouts[e].$keyboard,b.randomizeInput?c.$el.unbind(f.keyboard.events.kbChange+t).bind(f.keyboard.events.kbChange+t,function(e,t){b.targetKeys.test(t.last.key)&&(t.$keyboard.find("."+a.css.buttonHover).removeClass(a.css.buttonHover),t.$keyboard=t.scramble_setup(t.$keyboard),f(document.elementFromPoint(e.clientX,e.clientY)).trigger("mouseenter"))}):b.randomizeOnce||c.$el.unbind(f.keyboard.events.kbBeforeVisible+t).bind(f.keyboard.events.kbBeforeVisible+t,function(e,t){t.$keyboard=t.scramble_setup(t.$keyboard)}),f.isFunction(b.orig_create)&&b.orig_create(c)},c.setScrambleLayout(),a.alwaysOpen&&c.$keyboard.length?setTimeout(function(){var e=f.keyboard.builtLayouts;c.$keyboard=c.scramble_setup(c.$keyboard),c.setScrambleLayout(),void 0===e[a.layout]&&(e[a.layout]={mappedKeys:f.extend({},e[c.layout].mappedKeys),acceptedKeys:f.extend([],e[c.layout].acceptedKeys),$keyboard:c.$keyboard.clone()}),f.isFunction(b.init)&&b.init(c)},0):f.isFunction(b.init)&&b.init(c))})}});