Subversion Repositories wimsdev

Rev

Rev 20 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 reyssat 1
/*
2
        DynAPI Distribution
3
        MouseEvent Class
4
 
5
        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
13573 obado 6
 
20 reyssat 7
        requires: dynapi.api.DynDocument
8
*/
9
 
10
function MouseEvent(dyndoc) {
11
        this.DynEvent = DynEvent;
12
        this.DynEvent();
13
        this.bubble = true;
14
        this._mouseEvent = null;
15
        this._relative = null;
16
        this._dyndoc = dyndoc;
17
};
18
var p = dynapi.setPrototype('MouseEvent','DynEvent');
19
p.getX = function() {return this.x};
20
p.getY = function() {return this.y};
21
p.getPageX = function() {return this.pageX};
22
p.getPageY = function() {return this.pageY};
23
p.getRelative = function() {return this._relative};
24
p.preventBubble = function() {this.bubble = false;};
25
p.getButton = function() {
26
        if (!this._mouseEvent) return "left";
27
        var b = this._mouseEvent.button;
28
        if (b==4) return "middle";
29
        if (b==2) return "right";
30
        else return "left";
31
};
32
p._init = function(type,e,src) {
33
        this.type = type;
34
        this._mouseEvent = e;
35
        this.origin = src;
36
        this.bubbleChild = null;
37
        this.defaultValue = true;
38
        this.bubble = true;
39
};
40
p._invoke = function() {
41
        var o = this.origin;
42
        o.invokeEvent(this.type,this);
43
};
44
 
45
MouseEvent._getContainerLayerOf = function(element) {
46
        if (!element) return null;
47
        while (!element._dynobj && element.parentElement && element.parentElement!=element) {
48
                element = element.parentElement;
49
        }
50
        return element._dynobj;
51
};
52
MouseEvent.trapMouseUp = dynapi.functions.False; // or MouseEvent.trapMouseUp=null
53
 
54
MouseEvent._eventHandler = function() {
55
        var e = dynapi.frame.event;
56
        var dynobj;
57
        if (this._dynobj) dynobj = this._dynobj;
58
        else if (e.srcElement._dynobj) dynobj = e.srcElement._dynobj;
59
        else dynobj = dynapi.document;
60
 
61
        var dyndoc = dynobj._dyndoc;
62
        var target = e.srcElement;
63
 
64
        var me = dyndoc._mouseEvent;
65
        var src = MouseEvent._getContainerLayerOf(target);
66
        me._init(e.type,e,src);
67
 
68
        var rel = e.type=="mouseout"? e.toElement : e.fromElement;
69
        var r = me._relative = MouseEvent._getContainerLayerOf(rel);
70
        if (e.type=="mouseout" || e.type=="mouseover") {
71
                if (r && (r==src||src.isParentOf(r))) return; //fix for #15 (ie only)
72
                //if (r&&(r==src.parent||r.isChildOf(src.parent))) me.bubble=false;
73
                if (r&&(r==src.parent||r.isChildOf(src.parent))) me.bubble=false;
74
        }
75
        me.pageX = e.clientX;
76
        me.pageY = e.clientY;
77
        if(!src) return;
78
        me.x = me.pageX - src.getPageX(); //offsetX;
79
        me.y = me.pageY - src.getPageY(); //offsetY;
80
        e.cancelBubble = true;
81
        me._invoke();
82
 
83
        var tt=target.type;
84
        var tn=(target.tagName+'').toLowerCase();
85
 
86
        // fix for form elements inside drag-enabled layer #08
87
        if(tt=='textarea'||tt=='text' && target.onselectstart==null) target.onselectstart = dynapi.functions.Allow;
88
        if(e.type=='mousedown' && tn=='input'||tn=='textarea'||tn=='button') {
89
                var de=dynapi.frame.DragEvent;
90
                de=(de && de.dragevent)? de.dragevent:null;
91
                if(de && de.isDragging) de.cancelDrag();
92
        }
93
 
94
        // prevent image dragging
95
        if(target.tagName=='IMG' && typeof(target.ondragstart)!="function") {
96
                target.ondragstart=dynapi.functions.False;
97
        }
98
 
99
};
100
 
101
DynElement.prototype.captureMouseEvents = function() {
102
        this._hasMouseEvents = true;
103
        if (this.elm) {
104
                var elm = (this.getClassName()=='DynDocument')? this.doc : this.elm;
105
                elm.onmouseover = elm.onmouseout = elm.onmousedown = elm.onmouseup = elm.onclick = elm.ondblclick = elm.onmousemove = MouseEvent._eventHandler;
106
                //elm.oncontextmenu = MouseEvent.trapMouseUp;
107
        }
108
};
109
 
110
DynElement.prototype.releaseMouseEvents = function() {
111
        this._hasMouseEvents = false;
112
        if (this.elm) {
113
                var elm = (this.getClassName()=='DynDocument')? this.doc : this.elm;
114
                elm.onmousedown = elm.onmouseup = elm.onclick = elm.ondblclick = null;
115
                elm.oncontextmenu = null;
116
        }
117
};
118
 
119
function main() {
120
        dynapi.document._mouseEvent = new MouseEvent(dynapi.document);
121
};
122
if (!dynapi.loaded) main();