1 /**
  2  * @author Ilya Shaikovsky
  3  * @author Lukas Fryc
  4  */
  5 
  6 (function($, rf) {
  7 
  8     rf.ui = rf.ui || {};
  9 
 10     var defaultOptions = {
 11         enabledInInput : false,
 12         preventDefault : true
 13     };
 14     
 15     var types = [ 'keydown', 'keyup' ];
 16 
 17     rf.ui.HotKey = function(componentId, options) {
 18         $super.constructor.call(this, componentId);
 19         this.namespace = this.namespace || "." + rf.Event.createNamespace(this.name, this.id);
 20         this.attachToDom(this.componentId);
 21         this.options = $.extend({}, defaultOptions, options);
 22         this.__handlers = {};
 23         
 24         this.options.selector = (this.options.selector) ? this.options.selector : document;
 25 
 26         $(document).ready($.proxy(function() {
 27             this.__bindDefinedHandlers();
 28         }, this));
 29     };
 30 
 31     rf.BaseComponent.extend(rf.ui.HotKey);
 32 
 33     var $super = rf.ui.HotKey.$super;
 34 
 35     $.extend(rf.ui.HotKey.prototype, {
 36 
 37         name : "HotKey",
 38         
 39         __bindDefinedHandlers : function() {
 40             for (var i = 0; i < types.length; i++) {
 41                 if (this.options['on' + types[i]]) {
 42                     this.__bindHandler(types[i]);
 43                 }
 44             }
 45         },
 46         
 47         __bindHandler : function(type) {
 48             this.__handlers[type] = $.proxy(function(event) {
 49                 var result = this.invokeEvent.call(this, type, document.getElementById(this.id), event);
 50                 if (this.options.preventDefault) {
 51                     event.stopPropagation();
 52                     event.preventDefault();
 53                     return false;
 54                 }
 55                 return result;
 56             }, this);
 57             $(this.options.selector).bind(type + this.namespace, this.options, this.__handlers[type]);
 58         },
 59 
 60         destroy : function() {
 61             rf.Event.unbindById(this.id, this.namespace);
 62 
 63             for (var type in this.__handlers) {
 64                 if (this.__handlers.hasOwnProperty(type)) {
 65                     $(this.options.selector).unbind(type + this.namespace, this.__handlers[type]);
 66                 }
 67             }
 68 
 69             $super.destroy.call(this);
 70         }
 71     });
 72 
 73 })(RichFaces.jQuery, RichFaces);