Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / editor / tinymce / jscripts / tiny_mce / utils / mclayer.js
blob09a949677aa5ee35d1893ba76a4ffe014887e96e
1 /**
2  * $RCSfile$
3  * $Revision$
4  * $Date$
5  *
6  * Moxiecode floating layer script.
7  *
8  * @author Moxiecode
9  * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
10  */
12 function MCLayer(id) {
13         this.id = id;
14         this.settings = new Array();
15         this.blockerElement = null;
16         this.isMSIE = navigator.appName == "Microsoft Internet Explorer";
17         this.events = false;
18         this.autoHideCallback = null;
21 MCLayer.prototype = {
22         moveRelativeTo : function(re, p, a) {
23                 var rep = this.getAbsPosition(re);
24                 var w = parseInt(re.offsetWidth);
25                 var h = parseInt(re.offsetHeight);
26                 var x, y;
28                 switch (p) {
29                         case "tl":
30                                 break;
32                         case "tr":
33                                 x = rep.absLeft + w;
34                                 y = rep.absTop;
35                                 break;
37                         case "bl":
38                                 break;
40                         case "br":
41                                 break;
42                 }
44                 this.moveTo(x, y);
45         },
47         moveBy : function(dx, dy) {
48                 var e = this.getElement();
49                 var x = parseInt(e.style.left);
50                 var y = parseInt(e.style.top);
52                 e.style.left = (x + dx) + "px";
53                 e.style.top = (y + dy) + "px";
55                 this.updateBlocker();
56         },
58         moveTo : function(x, y) {
59                 var e = this.getElement();
61                 e.style.left = x + "px";
62                 e.style.top = y + "px";
64                 this.updateBlocker();
65         },
67         show : function() {
68                 MCLayer.visibleLayer = this;
70                 this.getElement().style.display = 'block';
71                 this.updateBlocker();
72         },
74         hide : function() {
75                 this.getElement().style.display = 'none';
76                 this.updateBlocker();
77         },
79         setAutoHide : function(s, cb) {
80                 this.autoHideCallback = cb;
81                 this.registerEventHandlers();
82         },
84         getElement : function() {
85                 return document.getElementById(this.id);
86         },
88         updateBlocker : function() {
89                 if (!this.isMSIE)
90                         return;
92                 var e = this.getElement();
93                 var b = this.getBlocker();
94                 var x = this.parseInt(e.style.left);
95                 var y = this.parseInt(e.style.top);
96                 var w = this.parseInt(e.offsetWidth);
97                 var h = this.parseInt(e.offsetHeight);
99                 b.style.left = x + 'px';
100                 b.style.top = y + 'px';
101                 b.style.width = w + 'px';
102                 b.style.height = h + 'px';
103                 b.style.display = e.style.display;
104         },
106         getBlocker : function() {
107                 if (!this.blockerElement) {
108                         var d = document, b = d.createElement("iframe");
110                         b.style.cssText = 'display: none; left: 0px; position: absolute; top: 0';
111                         b.src = 'javascript:false;';
112                         b.frameBorder = '0';
113                         b.scrolling = 'no';
115                         d.body.appendChild(b);
116                         this.blockerElement = b;
117                 }
119                 return this.blockerElement;
120         },
122         getAbsPosition : function(n) {
123                 var p = {absLeft : 0, absTop : 0};
125                 while (n) {
126                         p.absLeft += n.offsetLeft;
127                         p.absTop += n.offsetTop;
128                         n = n.offsetParent;
129                 }
131                 return p;
132         },
134         registerEventHandlers : function() {
135                 if (!this.events) {
136                         var d = document;
138                         this.addEvent(d, 'mousedown', MCLayer.prototype.onMouseDown);
140                         this.events = true;
141                 }
142         },
144         addEvent : function(o, n, h) {
145                 if (o.attachEvent)
146                         o.attachEvent("on" + n, h);
147                 else
148                         o.addEventListener(n, h, false);
149         },
151         onMouseDown : function(e) {
152                 e = typeof(e) == "undefined" ? window.event : e;
153                 var b = document.body;
154                 var l = MCLayer.visibleLayer;
156                 if (l) {
157                         var mx = l.isMSIE ? e.clientX + b.scrollLeft : e.pageX;
158                         var my = l.isMSIE ? e.clientY + b.scrollTop : e.pageY;
159                         var el = l.getElement();
160                         var x = parseInt(el.style.left);
161                         var y = parseInt(el.style.top);
162                         var w = parseInt(el.offsetWidth);
163                         var h = parseInt(el.offsetHeight);
165                         if (!(mx > x && mx < x + w && my > y && my < y + h)) {
166                                 MCLayer.visibleLayer = null;
168                                 if (l.autoHideCallback && l.autoHideCallback(l, e, mx, my))
169                                         return true;
171                                 l.hide();
172                         }
173                 }
174         },
176         addCSSClass : function(e, c) {
177                 this.removeCSSClass(e, c);
178                 var a = this.explode(' ', e.className);
179                 a[a.length] = c;
180                 e.className = a.join(' ');
181         },
183         removeCSSClass : function(e, c) {
184                 var a = this.explode(' ', e.className), i;
186                 for (i=0; i<a.length; i++) {
187                         if (a[i] == c)
188                                 a[i] = '';
189                 }
191                 e.className = a.join(' ');
192         },
194         explode : function(d, s) {
195                 var ar = s.split(d);
196                 var oar = new Array();
198                 for (var i = 0; i<ar.length; i++) {
199                         if (ar[i] != "")
200                                 oar[oar.length] = ar[i];
201                 }
203                 return oar;
204         },
206         parseInt : function(s) {
207                 if (s == null || s == '')
208                         return 0;
210                 return parseInt(s);
211         }