Refactored hotkey plugin for the better memory handling. Added dispose-control event support.

This commit is contained in:
alekseybobkov 2015-04-24 21:08:07 -07:00
parent fa48fbd8f0
commit 87b34295eb
2 changed files with 180 additions and 122 deletions

View File

@ -1139,34 +1139,37 @@ data[option].apply(data,methodArgs)}})}
$.fn.fileList.Constructor=FileList
$.fn.fileList.noConflict=function(){$.fn.fileList=old
return this}
$(document).ready(function(){$('[data-control=filelist]').fileList()})}(window.jQuery);+function($){"use strict";var HotKey=function(element,options){var $el=this.$el=$(element)
var $target=this.$target=$(options.hotkeyTarget)
$(document).ready(function(){$('[data-control=filelist]').fileList()})}(window.jQuery);+function($){"use strict";var Base=$.oc.foundation.base,BaseProto=Base.prototype
var HotKey=function(element,options){if(!options.hotkey)
throw new Error('No hotkey has been defined.');this.$el=$(element)
this.$target=$(options.hotkeyTarget)
this.options=options||{}
if(!options.hotkey)
throw new Error('No hotkey has been defined.');if(options.hotkeyMac)options.hotkey+=', '+options.hotkeyMac
var
keys=options.hotkey.toLowerCase().split(','),keysCount=keys.length,keyConditions=[],keyPressed={shift:false,ctrl:false,cmd:false,alt:false},keyMap={'esc':27,'tab':9,'space':32,'return':13,'enter':13,'backspace':8,'scroll':145,'capslock':20,'numlock':144,'pause':19,'break':19,'insert':45,'home':36,'delete':46,'suppr':46,'end':35,'pageup':33,'pagedown':34,'left':37,'up':38,'right':39,'down':40,'f1':112,'f2':113,'f3':114,'f4':115,'f5':116,'f6':117,'f7':118,'f8':119,'f9':120,'f10':121,'f11':122,'f12':123}
for(var i=0;i<keysCount;i++){keyConditions.push(makeCondition(trim(keys[i])))}
$target.keydown(function(event){keyPressed.shift=event.originalEvent.shiftKey
keyPressed.ctrl=event.originalEvent.ctrlKey
keyPressed.cmd=event.originalEvent.metaKey
keyPressed.alt=event.originalEvent.altKey
if(testConditions(event)){if(options.hotkeyVisible&&!$el.is(':visible'))
return
if(options.callback)
return options.callback($el,this)
keyPressed.shift=false
keyPressed.ctrl=false
keyPressed.cmd=false
keyPressed.alt=false}});$target.keyup(function(event){keyPressed.shift=event.originalEvent.shiftKey
keyPressed.ctrl=event.originalEvent.ctrlKey
keyPressed.cmd=event.originalEvent.metaKey
keyPressed.alt=event.originalEvent.altKey});function testConditions(event){var count=keyConditions.length,condition
for(var i=0;i<count;i++){condition=keyConditions[i]
if(event.which==condition.specific&&keyPressed.shift==condition.shift&&keyPressed.ctrl==condition.ctrl&&keyPressed.cmd==condition.cmd&&keyPressed.alt==condition.alt){return true}}
return false}
function makeCondition(keyBind){var condition={shift:false,ctrl:false,cmd:false,alt:false,specific:-1},keys=keyBind.split('+'),count=keys.length
for(var i=0;i<count;i++){switch(keys[i]){case'shift':condition.shift=true
this.keyConditions=[]
this.keyMap=null
Base.call(this)
this.init()}
HotKey.prototype=Object.create(BaseProto)
HotKey.prototype.constructor=HotKey
HotKey.prototype.dispose=function(){this.unregisterHandlers()
this.$el.removeData('oc.hotkey')
this.$target=null
this.$el=null
this.keyConditions=null
this.keyMap=null
this.options=null
BaseProto.dispose.call(this)}
HotKey.prototype.init=function(){if(this.options.hotkeyMac)
this.options.hotkey+=', '+this.options.hotkeyMac
this.initKeyMap()
var keys=this.options.hotkey.toLowerCase().split(',')
for(var i=0,len=keys.length;i<len;i++){var keysTrimmed=this.trim(keys[i])
this.keyConditions.push(this.makeCondition(keysTrimmed))}
this.$target.on('keydown',this.proxy(this.onKeyDown))
this.$el.on('dispose-control',this.proxy(this.dispose))}
HotKey.prototype.unregisterHandlers=function(){this.$target.off('keydown',this.proxy(this.onKeyDown))
this.$el.off('dispose-control',this.proxy(this.dispose))}
HotKey.prototype.makeCondition=function(keyBind){var condition={shift:false,ctrl:false,cmd:false,alt:false,specific:-1},keys=keyBind.split('+')
for(var i=0,len=keys.length;i<len;i++){switch(keys[i]){case'shift':condition.shift=true
break
case'ctrl':condition.ctrl=true
break
@ -1174,11 +1177,19 @@ case'command':case'cmd':case'meta':condition.cmd=true
break
case'alt':condition.alt=true
break}}
condition.specific=keyMap[keys[keys.length-1]]
condition.specific=this.keyMap[keys[keys.length-1]]
if(typeof(condition.specific)=='undefined')
condition.specific=keys[keys.length-1].toUpperCase().charCodeAt()
return condition}
function trim(str){return str.replace(/^\s+/,"").replace(/\s+$/,"")}}
HotKey.prototype.initKeyMap=function(){this.keyMap={'esc':27,'tab':9,'space':32,'return':13,'enter':13,'backspace':8,'scroll':145,'capslock':20,'numlock':144,'pause':19,'break':19,'insert':45,'home':36,'delete':46,'suppr':46,'end':35,'pageup':33,'pagedown':34,'left':37,'up':38,'right':39,'down':40,'f1':112,'f2':113,'f3':114,'f4':115,'f5':116,'f6':117,'f7':118,'f8':119,'f9':120,'f10':121,'f11':122,'f12':123}}
HotKey.prototype.trim=function(str){return str.replace(/^\s+/,"").replace(/\s+$/,"")}
HotKey.prototype.testConditions=function(ev){for(var i=0,len=this.keyConditions.length;i<len;i++){var condition=this.keyConditions[i]
if(ev.which==condition.specific&&ev.originalEvent.shiftKey==condition.shift&&ev.originalEvent.ctrlKey==condition.ctrl&&ev.originalEvent.metaKey==condition.cmd&&ev.originalEvent.altKey==condition.alt){return true}}
return false}
HotKey.prototype.onKeyDown=function(ev){if(this.testConditions(ev)){if(this.options.hotkeyVisible&&!this.$el.is(':visible'))
return
if(this.options.callback)
return this.options.callback(this.$el,ev.currentTarget)}}
HotKey.DEFAULTS={hotkey:null,hotkeyMac:null,hotkeyTarget:'html',hotkeyVisible:true,callback:function(element){element.trigger('click')
return false}}
var old=$.fn.hotKey
@ -1186,7 +1197,7 @@ $.fn.hotKey=function(option){var args=arguments;return this.each(function(){var
var data=$this.data('oc.hotkey')
var options=$.extend({},HotKey.DEFAULTS,$this.data(),typeof option=='object'&&option)
if(!data)$this.data('oc.hotkey',(data=new HotKey(this,options)))
if(typeof option=='string')data[option].call($this)})}
if(typeof option=='string')data[option].apply(data,args)})}
$.fn.hotKey.Constructor=HotKey
$.fn.hotKey.noConflict=function(){$.fn.hotKey=old
return this}

View File

@ -2,7 +2,7 @@
* Hot key binding.
*
* Data attributes:
* - data-hotkey="ctrl+s, cmd+s" - enables the autocomplete plugin
* - data-hotkey="ctrl+s, cmd+s" - enables the hotkey plugin
*
* JavaScript API:
*
@ -10,114 +10,161 @@
*/
+function ($) { "use strict";
var Base = $.oc.foundation.base,
BaseProto = Base.prototype
var HotKey = function (element, options) {
var $el = this.$el = $(element)
var $target = this.$target = $(options.hotkeyTarget)
this.options = options || {}
if (!options.hotkey)
throw new Error('No hotkey has been defined.');
if (options.hotkeyMac) options.hotkey += ', ' + options.hotkeyMac // @todo deprecated
this.$el = $(element)
this.$target = $(options.hotkeyTarget)
this.options = options || {}
this.keyConditions = []
this.keyMap = null
var
keys = options.hotkey.toLowerCase().split(','),
keysCount = keys.length,
keyConditions = [],
keyPressed = { shift: false, ctrl: false, cmd: false, alt: false },
keyMap = {'esc':27, 'tab':9, 'space':32, 'return':13, 'enter':13, 'backspace':8, 'scroll':145, 'capslock':20, 'numlock':144, 'pause':19,
'break':19, 'insert':45, 'home':36, 'delete':46, 'suppr':46, 'end':35, 'pageup':33, 'pagedown':34, 'left':37, 'up':38, 'right':39, 'down':40,
'f1':112, 'f2':113, 'f3':114, 'f4':115, 'f5':116, 'f6':117, 'f7':118, 'f8':119, 'f9':120, 'f10':121, 'f11':122, 'f12':123}
Base.call(this)
for (var i = 0; i < keysCount; i++) {
keyConditions.push(makeCondition(trim(keys[i])))
this.init()
}
HotKey.prototype = Object.create(BaseProto)
HotKey.prototype.constructor = HotKey
HotKey.prototype.dispose = function() {
this.unregisterHandlers()
this.$el.removeData('oc.hotkey')
this.$target = null
this.$el = null
this.keyConditions = null
this.keyMap = null
this.options = null
BaseProto.dispose.call(this)
}
HotKey.prototype.init = function() {
if (this.options.hotkeyMac)
this.options.hotkey += ', ' + this.options.hotkeyMac // @todo deprecated
this.initKeyMap()
var keys = this.options.hotkey.toLowerCase().split(',')
for (var i = 0, len = keys.length; i < len; i++) {
var keysTrimmed = this.trim(keys[i])
this.keyConditions.push(this.makeCondition(keysTrimmed))
}
$target.keydown(function (event) {
keyPressed.shift = event.originalEvent.shiftKey
keyPressed.ctrl = event.originalEvent.ctrlKey
keyPressed.cmd = event.originalEvent.metaKey
keyPressed.alt = event.originalEvent.altKey
this.$target.on('keydown', this.proxy(this.onKeyDown))
this.$el.on('dispose-control', this.proxy(this.dispose))
}
if (testConditions(event)) {
if (options.hotkeyVisible && !$el.is(':visible'))
return
HotKey.prototype.unregisterHandlers = function() {
this.$target.off('keydown', this.proxy(this.onKeyDown))
this.$el.off('dispose-control', this.proxy(this.dispose))
}
if (options.callback)
return options.callback($el, this)
HotKey.prototype.makeCondition = function(keyBind) {
var condition = { shift: false, ctrl: false, cmd: false, alt: false, specific: -1 },
keys = keyBind.split('+')
keyPressed.shift = false
keyPressed.ctrl = false
keyPressed.cmd = false
keyPressed.alt = false
for (var i = 0, len = keys.length; i < len; i++) {
switch (keys[i]) {
case 'shift':
condition.shift = true
break
case 'ctrl':
condition.ctrl = true
break
case 'command':
case 'cmd':
case 'meta':
condition.cmd = true
break
case 'alt':
condition.alt = true
break
}
});
$target.keyup(function (event) {
keyPressed.shift = event.originalEvent.shiftKey
keyPressed.ctrl = event.originalEvent.ctrlKey
keyPressed.cmd = event.originalEvent.metaKey
keyPressed.alt = event.originalEvent.altKey
});
function testConditions(event) {
var count = keyConditions.length,
condition
for (var i = 0; i < count; i++) {
condition = keyConditions[i]
if (event.which == condition.specific
&& keyPressed.shift == condition.shift
&& keyPressed.ctrl == condition.ctrl
&& keyPressed.cmd == condition.cmd
&& keyPressed.alt == condition.alt) {
return true
}
}
return false
}
function makeCondition(keyBind) {
var condition = { shift: false, ctrl: false, cmd: false, alt: false, specific: -1 },
keys = keyBind.split('+'),
count = keys.length
condition.specific = this.keyMap[keys[keys.length-1]]
for (var i = 0; i < count; i++) {
switch (keys[i]) {
case 'shift':
condition.shift = true
break
case 'ctrl':
condition.ctrl = true
break
case 'command':
case 'cmd':
case 'meta':
condition.cmd = true
break
case 'alt':
condition.alt = true
break
}
if (typeof (condition.specific) == 'undefined')
condition.specific = keys[keys.length-1].toUpperCase().charCodeAt()
return condition
}
HotKey.prototype.initKeyMap = function() {
this.keyMap = {
'esc': 27,
'tab': 9,
'space': 32,
'return': 13,
'enter': 13,
'backspace': 8,
'scroll': 145,
'capslock': 20,
'numlock': 144,
'pause': 19,
'break': 19,
'insert': 45,
'home': 36,
'delete': 46,
'suppr': 46,
'end': 35,
'pageup': 33,
'pagedown': 34,
'left': 37,
'up': 38,
'right': 39,
'down': 40,
'f1': 112,
'f2': 113,
'f3': 114,
'f4': 115,
'f5': 116,
'f6': 117,
'f7': 118,
'f8': 119,
'f9': 120,
'f10': 121,
'f11': 122,
'f12': 123
}
}
HotKey.prototype.trim = function(str) {
return str
.replace(/^\s+/, "")
.replace(/\s+$/, "")
}
HotKey.prototype.testConditions = function(ev) {
for (var i = 0, len = this.keyConditions.length; i < len; i++) {
var condition = this.keyConditions[i]
if (ev.which == condition.specific
&& ev.originalEvent.shiftKey == condition.shift
&& ev.originalEvent.ctrlKey == condition.ctrl
&& ev.originalEvent.metaKey == condition.cmd
&& ev.originalEvent.altKey == condition.alt) {
return true
}
condition.specific = keyMap[keys[keys.length-1]]
if (typeof (condition.specific) == 'undefined')
condition.specific = keys[keys.length-1].toUpperCase().charCodeAt()
return condition
}
function trim(str){
return str
.replace(/^\s+/, "") // Left
.replace(/\s+$/, "") // Right
return false
}
HotKey.prototype.onKeyDown = function(ev) {
if (this.testConditions(ev)) {
if (this.options.hotkeyVisible && !this.$el.is(':visible'))
return
if (this.options.callback)
return this.options.callback(this.$el, ev.currentTarget)
}
}
@ -145,7 +192,7 @@
var data = $this.data('oc.hotkey')
var options = $.extend({}, HotKey.DEFAULTS, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('oc.hotkey', (data = new HotKey(this, options)))
if (typeof option == 'string') data[option].call($this)
if (typeof option == 'string') data[option].apply(data, args)
})
}