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