-
Notifications
You must be signed in to change notification settings - Fork 1
/
keymap.min.js
14 lines (13 loc) · 4.37 KB
/
keymap.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @author Andr3as <andranode@gmail.com>
* @license https://github.com/Andr3as/jQuery-Keymap/blob/master/LICENSE.md MIT
* @version 2.0.0
* @date 15/08/2015
*/
/*
* Copyright (c) Andr3as
* as-is and without warranty under the MIT License.
* See http://opensource.org/licenses/MIT for more information.
* This information must remain intact.
*/
var scripts=document.getElementsByTagName("script"),path=scripts[scripts.length-1].src.split("?")[0],curpath=path.split("/").slice(0,-1).join("/")+"/";!function(e){var t={css:{key:{}},selector:""};e.fn.keymap=function(a){a=a||{};var s=this;var i,o,n,c,r;i=function(){a.layout=a.layout||"qwerty";if(typeof a.layout=="object"){n()}else if(typeof a.layout=="string"){e.getJSON(curpath+"layouts/"+a.layout+".json",function(e){a.layout=e;n()}).fail(function(e){o('Failed to load layout! Check <a href="https://github.com/Andr3as/jQuery-Keymap/wiki/Troubleshooting">error #1</a> for more details.')})}else{o("Unknown layout type")}};o=function(t){e(s).html('<p class="keymap-error">'+t+"</p>")};r=function(t){p=document.createElement("p");if(typeof t=="string"){e(p).text(t)}else if(typeof t=="object"){if(typeof t.name!="undefined"){e(p).text(t.name)}if(typeof t.class!="undefined"){e(p).addClass(t.class)}}return p};n=function(){e(s).html("");layout=a.layout;e(s).addClass("keymap");var i=s.width()/a.layout.line;var o=a.layout.keys;var n,l,d,f,p,y,u,h;for(var k=0;k<o.length;k++){h=document.createElement("div");e(h).addClass("keymap-line");for(var m=0;m<o[k].length;m++){n=o[k][m];p=document.createElement("span");l=i*(n.width||1)-2+"px";e(p).width(l);d=(i*n.height||i-2)+"px";e(p).height(d);f=(i*n.offset||1)+"px";e(p).css("margin-left",f);u=n.value||n.name;e(p).attr("data-value",u);if(e(p).height()>i){e(p).attr("data-height",e(p).height()-2);e(p).height(i)}if(typeof n.code!="undefined"){e(p).addClass("keymap-key");e(p).attr("data-key",n.code)}if(typeof n.second!="undefined"){y=r(n.second);e(y).addClass("second");p.appendChild(y);e(p).addClass("hasSecond")}if(typeof n.name!="undefined"){y=r(n.name);e(y).addClass("first");p.appendChild(y);e(p).addClass("hasFirst")}if(typeof n.third!="undefined"){y=r(n.third);e(y).addClass("third");p.appendChild(y);e(p).addClass("hasThird")}if(typeof n["z-index"]!="undefined"){e(p).attr("data-index",n["z-index"])}h.appendChild(p)}s.append(h)}c(a.css);e(s.selector+" span p").css("font-size",i/4+"px");e(s.selector+" span[data-height]").each(function(){e(this).height(e(this).attr("data-height"))});t.selector=s.selector;t.css=a.css};c=function(t){t=t||a.css;if(typeof t!="undefined"){if(typeof t.key!="undefined"){if(typeof t.key.color!="undefined"){e(s.selector+" .keymap-key").css("color",t.key.color)}else{e(s.selector+" .keymap-key").css("color","")}if(typeof t.key.background!="undefined"){e(s.selector+" .keymap-key").css("background-color",t.key.background)}else{e(s.selector+" .keymap-key").css("background-color","")}if(typeof t.key.class!="undefined"){e(s.selector+" .keymap-key").addClass(t.key.class)}}}};this.keymap.style=c;if(a.action=="reset"){c()}else if(a=="delete"){this.html("")}else{i()}return this};e.fn.createShortcut=function(a,s,i,o){var n=this;var c;if(typeof s=="undefined"){return false}a=a||t.selector;i=i||{};if(typeof i.class=="undefined"){c=false;i.background=i.background||"red";i.color=i.color||"black"}else{c=true}o=o||"";var r=function(t){if(c){e(t).addClass(i.class)}else{e(t).css("background-color",i.background);e(t).css("color",i.color)}};var l=function(){if(typeof t.css=="undefined"){if(c){e(a+" .keymap-key").removeClass(i.class)}e(a+" .keymap-key").css("color","");e(a+" .keymap-key").css("background-color","")}else{e(a).keymap.style(t.css)}};var d=function(){l();var t;String.prototype.capitalizeFirstLetter=function(){return this.charAt(0).toUpperCase()+this.slice(1)};if(e.isArray(s)){for(var i=0;i<s.length;i++){if(e.isNumeric(s[i])){r(a+" [data-key="+s[i]+"]"+o)}else{t=[s[i],s[i].toLowerCase(),s[i].toUpperCase(),s[i].capitalizeFirstLetter()];e.each(t,function(e,t){r(a+" [data-value="+t+"]"+o)})}}}else if(e.isNumeric(s)){r(a+" [data-key="+s+"]"+o)}else{t=[s,s.toLowerCase(),s.toUpperCase(),,s.capitalizeFirstLetter()];e.each(t,function(e,t){r(a+" [data-value="+t+"]"+o)})}};this.click(function(){if(e(".keymap-clicked").length===0){d();n.addClass("keymap-clicked")}else{l();e(".keymap-clicked").removeClass("keymap-clicked")}});this.hover(d,function(){if(e(".keymap-clicked").length===0){l()}});return this}}(jQuery);