summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/extended/event.js
blob: b6dccf3b07d082a1c17bb79b0b38384d1d039b0a (plain)
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
Object.extend(Event, {
	OnLoad : function (fn) {
		// opera onload is in document, not window
		var w = document.addEventListener && !window.addEventListener ? document : window;
		Event.__observe(w,'load',fn);
	},
	observe: function(elements, name, observer, useCapture) {
    if(!isList(elements))
		return this.__observe(elements, name, observer, useCapture);
	for(var i=0; i<elements.length; i++)
		this.__observe(elements[i], name, observer, useCapture);
  },
  __observe: function(element, name, observer, useCapture) {
    var element = $(element);
    useCapture = useCapture || false;
    
    if (name == 'keypress' &&
        ((navigator.appVersion.indexOf('AppleWebKit') > 0) 
        || element.attachEvent))
      name = 'keydown';
    
    this._observeAndCache(element, name, observer, useCapture);
  },
   keyCode : function(e)
	{
	   return e.keyCode != null ? e.keyCode : e.charCode
	},

	isHTMLEvent : function(type)
	{
		var events = ['abort', 'blur', 'change', 'error', 'focus', 'load', 'reset', 'resize', 'scroll', 'select', 'submit', 'unload'];
		return events.include(type);
	},

	isMouseEvent : function(type)
	{
		var events = ['click', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup'];
		return events.include(type);
	},

	fireEvent : function(element,type)
	{
		if(document.createEvent)
        {            
			if(Event.isHTMLEvent(type))
			{
				var event = document.createEvent('HTMLEvents');
	            event.initEvent(type, true, true);
			}
			else if(Event.isMouseEvent(type))
			{
				var event = document.createEvent('MouseEvents');
				event.initMouseEvent(type,true,true,
					document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
			}
			else
			{
				if(Logger) 
					Logger.error("undefined event", type);
				return;
			}
            element.dispatchEvent(event);
        }
        else if(element.fireEvent)
        {
            element.fireEvent('on'+type);
            element[type]();
        }
        else
            element[type]();
	}
});