summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/Layer.js
blob: 9eeaf7c72bd26e31414aa54b913690ecc388a0d9 (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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
 * @class YAHOO.ext.Layer
 * @extends YAHOO.ext.Element
 * An extended Element object that supports a shadow and shim, constrain to viewport and
 * automatic maintaining of shadow/shim positions.
 * @cfg {Boolean} shim False to disable the iframe shim in browsers which need one (defaults to true)
 * @cfg {String/Boolean} shadow True to create a shadow element with default class "ylayer-shadow" or 
 * you can pass a string with a css class name. False turns off the shadow.
 * @cfg {Object} dh DomHelper object config to create element with (defaults to {tag: 'div', cls: 'ylayer'}).  
 * @cfg {Boolean} constrain False to disable constrain to viewport (defaults to true)
 * @cfg {String} cls CSS class to add to the element
 * @cfg {Number} zindex Starting z-index (defaults to 11000!)
 * @cfg {Number} shadowOffset Offset for the shadow (defaults to 3)
 * @constructor
 * @param {Object} config
 * @param {String/HTMLElement} existingEl (optional) Uses an existing dom element. If the element is not found it creates it.
 */
YAHOO.ext.Layer = function(config, existingEl){
    config = config || {};
    var dh = YAHOO.ext.DomHelper;
    if(existingEl){
        this.dom = YAHOO.util.Dom.get(existingEl);
    }
    if(!this.dom){
        var o = config.dh || {tag: 'div', cls: 'ylayer'};
        this.dom = dh.insertBefore(document.body.firstChild, o);
    }
    if(config.cls){
        this.addClass(config.cls);
    }
    this.constrain = config.constrain !== false;
    this.visibilityMode = YAHOO.ext.Element.VISIBILITY;
    this.id = YAHOO.util.Dom.generateId(this.dom);
    var zindex = (config.zindex || parseInt(this.getStyle('z-index'), 10)) || 11000;
    this.setAbsolutePositioned(zindex);
    if(config.shadow){
        var cls = (typeof config.shadow == 'string' ? config.shadow : 'ylayer-shadow');
        this.shadow = dh.insertBefore(this.dom, 
            {tag: 'div', cls: cls}, true);
        this.shadowOffset = config.shadowOffset || 3;
        this.shadow.setAbsolutePositioned(zindex-1);
    }else{
        this.shadowOffset = 0;
    }
    var b = YAHOO.ext.util.Browser;
    if(config.shim !== false && (b.isIE || (b.isGecko && b.isMac))){
        this.shim = this.createShim();
        this.shim.setOpacity(0);
        this.shim.setAbsolutePositioned(zindex-2);
    }
    this.hide();
};
YAHOO.extendX(YAHOO.ext.Layer, YAHOO.ext.Element, {
    sync : function(doShow){
        if(this.isVisible() && (this.shadow || this.shim)){
            var b = this.getBox();
            if(this.shim){
                if(doShow){
                   this.shim.show();
                }
                this.shim.setBox(b);
            }
            if(this.shadow){
                if(doShow){
                    this.shadow.show();
                }
                b.x += this.shadowOffset;
                b.y += this.shadowOffset;
                this.shadow.setBox(b);
            }
        }
    },
    
    syncLocalXY : function(){
        var l = this.getLeft(true);
        var t = this.getTop(true);
        if(this.shim){
            this.shim.setLeftTop(l, t);
        }
        if(this.shadow){
            this.shadow.setLeftTop(l + this.shadowOffset, 
              t + this.shadowOffset);
        }
    },
    
    hideUnders : function(negOffset){
        if(this.shadow){
            this.shadow.hide();
            if(negOffset){
                this.shadow.setLeftTop(-10000,-10000);
            }
        }
        if(this.shim){
           this.shim.hide();
           if(negOffset){
               this.shim.setLeftTop(-10000,-10000);
           }
        }
    },
    
    constrainXY : function(){
        if(this.constrain){
            var vw = YAHOO.util.Dom.getViewportWidth(),
                vh = YAHOO.util.Dom.getViewportHeight();
            var xy = this.getXY();
            var x = xy[0], y = xy[1];   
            var w = this.dom.offsetWidth+this.shadowOffset, h = this.dom.offsetHeight+this.shadowOffset;
            // only move it if it needs it
            var moved = false;
            // first validate right/bottom
            if(x + w > vw){
                x = vw - w;
                moved = true;
            }
            if(y + h > vh){
                y = vh - h;
                moved = true;
            }
            // then make sure top/left isn't negative
            if(x < 0){
                x = 0;
                moved = true;
            }
            if(y < 0){
                y = 0;
                moved = true;
            }
            if(moved){
                xy = [x, y];
                this.lastXY = xy;
                this.beforeAction();
                YAHOO.ext.Layer.superclass.setXY.call(this, xy);
                this.sync(true);
            }
        }
    },
    
    setVisible : function(v, a, d, c, e){
        if(this.lastXY){
            YAHOO.ext.Layer.superclass.setXY.call(this, this.lastXY);
        }
        if(a && v){
            var cb = function(){
                this.sync(true);
                if(c){
                    c();
                }
            }.createDelegate(this);
            YAHOO.ext.Layer.superclass.setVisible.call(this, true, true, d, cb, e);
        }else{
            if(!v){
                this.hideUnders(true);
            }
            var cb = c;
            if(a){
                cb = function(){
                    this.setLeftTop(-10000,-10000);
                    if(c){
                        c();
                    }
                }.createDelegate(this);
            }
            YAHOO.ext.Layer.superclass.setVisible.call(this, v, a, d, cb, e);
            if(v){
                this.sync(true);
            }else if(!a){
                this.setLeftTop(-10000,-10000);
            }
        }
    },
    
    beforeAction : function(){
        if(this.shadow){
            this.shadow.hide();
        }
    },
    
    setXY : function(xy, a, d, c, e){
        this.lastXY = xy;
        this.beforeAction();
        var cb = this.createCB(c);
        YAHOO.ext.Layer.superclass.setXY.call(this, xy, a, d, cb, e);
        if(!a){
            cb();
        }
    },
    
    createCB : function(c){
        var el = this;
        return function(){
            el.constrainXY();
            el.sync(true);
            if(c){
                c();
            }
        };
    },
    
    setX : function(x, a, d, c, e){
        this.setXY([x, this.getY()], a, d, c, e);
    },
    
    setY : function(y, a, d, c, e){
        this.setXY([this.getX(), y], a, d, c, e);
    },
    
    setSize : function(w, h, a, d, c, e){
        this.beforeAction();
        var cb = this.createCB(c);
        YAHOO.ext.Layer.superclass.setSize.call(this, w, h, a, d, cb, e);
        if(!a){
            cb();
        }
    },
    
    setWidth : function(w, a, d, c, e){
        this.beforeAction();
        var cb = this.createCB(c);
        YAHOO.ext.Layer.superclass.setWidth.call(this, w, a, d, cb, e);
        if(!a){
            cb();
        }
    },
    
    setHeight : function(h, a, d, c, e){
        this.beforeAction();
        var cb = this.createCB(c);
        YAHOO.ext.Layer.superclass.setHeight.call(this, h, a, d, cb, e);
        if(!a){
            cb();
        }
    },
    
    setBounds : function(x, y, w, h, a, d, c, e){
        this.beforeAction();
        var cb = this.createCB(c);
        if(!a){
            YAHOO.ext.Layer.superclass.setXY.call(this, [x, y]);
            YAHOO.ext.Layer.superclass.setSize.call(this, w, h, a, d, cb, e);
            cb();
        }else{
            YAHOO.ext.Layer.superclass.setBounds.call(this, x, y, w, h, a, d, cb, e);
        }
        return this;
    }
});