petejkim / socks

A tiny web GUI toolkit for JavaScript, inspired by Shoes

This URL has Read+Write access

socks / socks.element.control.js
100644 139 lines (114 sloc) 3.46 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Socks .Element.Control
*
* Copyright (c) 2009 Peter Jihoon Kim
*
* Licensed under the MIT License (MIT-LICENSE.txt)
*
* http://wiki.github.com/petejkim/socks
*
*/
 
(function(Socks){
  
  var toBoolean = Socks.Common.toBoolean;
  var type = Socks.Common.type;
  
  Socks.Element.Control = function() {
    this._socks_type = 'Socks.Element.Control';
  };
  
  Socks.Element.Control.prototype = new Socks.Element();
  
  Socks.Element.Control.prototype.isDisabled = function() {
    if('_disabled' in this) {
      return this._disabled;
    } else {
      return false;
    }
  };
  
  Socks.Element.Control.prototype.setDisabled = function(disabled) {
    disabled = toBoolean(disabled);
    this._disabled = disabled;
    
    if(this._element !== undefined) {
      this._element.disabled = (this._disabled ? 'disabled' : '');
    }
    return this;
  };
  
  Socks.Element.Control.prototype.disable = function() {
    return this.setDisabled(true);
  };
 
  Socks.Element.Control.prototype.enable = function() {
    return this.setDisabled(false);
  };
  
  Socks.Element.Control.prototype.focus = function(){
    if(this._element !== undefined) {
      this._element.focus();
    }
    return this;
  };
  
  Socks.Element.Control.prototype.blur = function(){
    if(this._element !== undefined) {
      this._element.blur();
    }
    return this;
  };
  
  //------------------------------------------------------
  // Events
  
  // gets the default event's function
  Socks.Element.Control.prototype.getFunction = function() {
    if(this._events !== undefined) {
      var defaultEvent = this._events[this._defaultEventType];
    
      if(defaultEvent !== undefined) {
        return defaultEvent.getFunction();
      }
    }
    return null;
  };
  
  // sets the default event's function
  Socks.Element.Control.prototype.setFunction = function(fun) {
    if(this._events === undefined) {
      this._events = {};
    }
    var defaultEvent = this._events[this._defaultEventType];
 
    if(defaultEvent !== undefined) {
      defaultEvent.setFunction(fun); // set function
      defaultEvent.addListener(); // re-add event listener
    } else {
      this._events[this._defaultEventType] = this.addEvent(this._defaultEventType, fun);
    }
    return this;
  };
  
  Socks.Element.Control.prototype._onKeyAction = function(eventType, args) {
    var preventDefault = false;
    var stopPropagation = true;
    var fun;
    eventType = eventType || 'keydown';
    
for(var i=0; i<args.length; i++) {
var arg = args[i];
switch(type(arg)) {
case 'Object':
if('preventDefault' in arg) {
preventDefault = arg.preventDefault;
}
if('stopPropagation' in arg) {
stopPropagation = arg.stopPropagation;
}
break;
case 'Function':
fun = arg;
}
}
 
if(fun !== undefined) {
      var funWrapper = function(e) {
        var key = Socks.Common.keyCodeToString(e.keyCode);
        fun.call(this, key, e);
      };
  
      return this.addEvent({type:eventType, preventDefault:preventDefault, stopPropagation:stopPropagation}, funWrapper);
}
return null;
  };
  
  Socks.Element.Control.prototype.onKeyDown = function() {
    // onKeyDown { |key, e| }
    return this._onKeyAction('keydown',arguments);
  };
  
  Socks.Element.Control.prototype.onKeyUp = function() {
    // onKeyUp { |key, e| }
    return this._onKeyAction('keyup',arguments);
  };
  
})(window.SOCKS);