calendar/lib: calendar_set_filters() use pre-fetched context and course recs
[moodle-pu.git] / lib / overlib / overlib_anchor.js
blob5993ac8ad05ef4b8b16c37c814112a2f47122e24
1 //\/////
2 //\  overLIB Anchor Plugin
3 //\  This file requires overLIB 4.10 or later.
4 //\
5 //\  overLIB 4.10 - You may not remove or change this notice.
6 //\  Copyright Erik Bosrup 1998-2004. All rights reserved.
7 //\  Contributors are listed on the homepage.
8 //\  See http://www.bosrup.com/web/overlib/ for details.
9 //   $Revision$                      $Date$
10 //\/////
11 //\mini
14 ////////
15 // PRE-INIT
16 // Ignore these lines, configuration is below.
17 ////////
18 if (typeof olInfo == 'undefined' || typeof olInfo.meets == 'undefined' || !olInfo.meets(4.10)) alert('overLIB 4.10 or later is required for the Anchor Plugin.');
19 else {
20 registerCommands('anchor,anchorx,anchory,noanchorwarn,anchoralign');
24 ////////
25 // DEFAULT CONFIGURATION
26 // Settings you want everywhere are set here. All of this can also be
27 // changed on your html page or through an overLIB call.
28 ////////
29 if (typeof ol_anchor ==  'undefined') var ol_anchor = '';
30 if (typeof ol_anchorx ==  'undefined') var ol_anchorx = 0;
31 if (typeof ol_anchory ==  'undefined') var ol_anchory = 0;
32 if (typeof ol_noanchorwarn ==  'undefined') var ol_noanchorwarn = 1;
33 if (typeof ol_anchoralign ==  'undefined') var ol_anchoralign = 'UL';
35 ////////
36 // END OF CONFIGURATION
37 // Don't change anything below this line, all configuration is above.
38 ////////
44 ////////
45 // INIT
46 ////////
47 // Runtime variables init. Don't change for config!
48 var o3_anchor = "";
49 var o3_anchorx = 0;
50 var o3_anchory = 0;
51 var o3_noanchorwarn = 1;
52 var o3_anchoralign = 'UL';
53 var mrkObj, rmrkPosition;  //reference mark object, reference mark position, an array;
56 ////////
57 // PLUGIN FUNCTIONS
58 ////////
59 function setAnchorVariables() {
60         o3_anchor = ol_anchor;
61         o3_anchorx = ol_anchorx;
62         o3_anchory = ol_anchory;
63         o3_noanchorwarn = ol_noanchorwarn;
64         o3_anchoralign = ol_anchoralign;
65         mrkObj = null;  // initialize this variable
68 // Parses Reference Mark commands
69 function parseAnchorExtras(pf,i,ar) {
70         var v, k=i;
72         if (k < ar.length) {
73                 if (ar[k] ==  ANCHOR) { eval(pf + "anchor = '" + escSglQuote(ar[++k]) + "'"); return k; }
74                 if (ar[k] ==  ANCHORX) { eval(pf + 'anchorx = ' + ar[++k]); return k; }
75                 if (ar[k] ==  ANCHORY) { eval(pf + 'anchory = ' + ar[++k]); return k; }
76                 if (ar[k] ==  NOANCHORWARN) { eval(pf + 'noanchorwarn = (' + pf + 'noanchorwarn==1) ? 0 : 1'); return k; }
77                 if (ar[k] ==  ANCHORALIGN) { k = opt_MULTIPLEARGS(++k, ar, (pf + 'anchoralign'));  return k; }
78         }
80         return -1;
84 ///////
85 //  FUNCTION WHICH CHECKS FOR THE EXISTENCE OF A REFERENCE MARKER
86 ///////
87 function checkAnchorObject() {
88         var w = o3_anchor;
90         if (w) {
91                 if (!(mrkObj = getAnchorObjectRef(w))) {
92                         if (o3_noanchorwarn) {
93                                 alert('WARNING!  Reference mark "' + w + '" not found.');
94                                 return false;
95                         } else w = '';
96                 }
97         }
99         return true;
102 ///////
103 // EXTERNAL SUPPORT FUNCTIONS TO HANDLE ANCHOR PROPERTIES
104 ///////
106 // Horizontal placement routine with anchors
107 function anchorHorizontal(browserWidth, horizontalScrollAmount, widthFix) {
108         var hasAnchor = (typeof o3_anchor != 'undefined' && o3_anchor);
109         if (!hasAnchor) return void(0);
111         // set o3_relx for follow scroll if defined
112         if (typeof o3_followscroll != 'undefined' && o3_followscroll && o3_sticky) o3_relx = rmrkPosition[0];
114         return rmrkPosition[0];
117 // Vertical placement routine with anchors
118 function anchorVertical(browserHeight,verticalScrollAmount) {
119         var hasAnchor = (typeof o3_anchor != 'undefined' && o3_anchor); 
120         if (!hasAnchor) return void(0);
122         // set o3_rely for follow scroll if defined
123         if (typeof o3_followscroll != 'undefined' && o3_followscroll && o3_sticky) o3_rely = rmrkPosition[1];
125         return rmrkPosition[1];
128 // Stub function for the runHook routine
129 function anchorPreface() {
130         if (!mrkObj) return;
131         rmrkPosition = getAnchorLocation(mrkObj);
134 // Get Reference Mark object 
135 function getAnchorObjectRef(aObj) {
136         return getRefById(aObj, o3_frame.document) || getRefByName(aObj, o3_frame.document)
139 // Adapted to overlib from jwin by Jason Anderson -- http://www.jwinlib.com
140 function getAnchorLocation(objRef){
141         var mkObj, of, offsets, mlyr
142         
143         mkObj = mlyr = objRef
144         offsets = [o3_anchorx, o3_anchory]
145         
146         if (document.layers){
147                 if (typeof mlyr.length != 'undefined' &&  mlyr.length > 1) {
148                                 mkObj = mlyr[0]
149                                 offsets[0] += mlyr[0].x + mlyr[1].pageX
150                                 offsets[1] += mlyr[0].y + mlyr[1].pageY
151                         } else {
152                                 if(mlyr.toString().indexOf('Image') != -1 || mlyr.toString().indexOf('Anchor') != -1){
153                                         offsets[0] += mlyr.x
154                                         offsets[1] += mlyr.y
155                                 } else {
156                                         offsets[0] += mlyr.pageX
157                                         offsets[1] += mlyr.pageY
158                                 }
159                         }          
160         } else {
161                 offsets[0] += pageLocation(mlyr, 'Left')
162                 offsets[1] += pageLocation(mlyr, 'Top')
163         }
164         
165         of = getAnchorOffsets(mkObj)
166         
167         if (typeof o3_dragimg != 'undefined' &&  o3_dragimg) {
168                 olImgLeft = offsets[0];
169                 olImgTop = offsets[1];
170         }
171         
172         offsets[0] += of[0]                    
173         offsets[1] += of[1]
174         
175         if (typeof o3_dragimg != 'undefined' &&  o3_dragimg) {
176                 olImgRight = offsets[0]; 
177                 olImgBottom = offsets[1];
178                 return;
179         }
180         
181         return offsets;
184 // Adapted to overlib from jwin by Jason Anderson -- http://www.jwinlib.com
185 function getAnchorOffsets(mkObj){
186         var fx = fy = 0,  mp, puc, mkAry, sx = sy = 0, w = o3_anchoralign  
187         var mW = mH = pW = pH = 0
188         var off = [0, 0]
190         mkAry = w.split(',');
192         if (mkAry.length < 3) {
193                 mp = mkAry[0].toUpperCase();
194                 puc = (mkAry.length == 1) ? mp : mkAry[1].toUpperCase();
195         } else if (mkAry.length == 3) {
196                 if (!isNaN(mkAry[0])) {
197                         mp = mkAry.slice(0, 2);
198                         puc = mkAry[2].toUpperCase();
199                  } else {
200                         mp = mkAry[0].toUpperCase();
201                         puc = mkAry.slice(1);
202                  }
203         } else {
204                 mp = mkAry.slice(0, 2);
205                 puc = mkAry.slice(2);
206         }
208         var shdwPresent = typeof o3_shadow != 'undefined' &&  o3_shadow
210         if (shdwPresent) {
211                 sx = Math.abs(o3_shadowx);
212                 sy = Math.abs(o3_shadowy);
213         }
215         pW = (shdwPresent ? parseInt(o3_width) : (olNs4 ? over.clip.width : over.offsetWidth))
216         pH = (shdwPresent ? parseInt(o3_aboveheight) : (olNs4 ? over.clip.height : over.offsetHeight))
218         if (olOp &&  o3_wrap) {
219                 pW = (shdwPresent ? parseInt(o3_width) : (olNs4 ? over.clip.width : over.offsetWidth))
220                 pH = (shdwPresent ? parseInt(o3_aboveheight) : (olNs4 ? over.clip.height : over.offsetHeight))
221         }
223         if (!olOp &&  mkObj.toString().indexOf('Image') != -1){
224                 mW = mkObj.width
225                 mH = mkObj.height
226         } else if (!olOp &&  mkObj.toString().indexOf('Anchor') != -1) {  // enforced only for NS4
227                 mp = 'UL'
228         } else {
229                 mW = (olNs4) ? mkObj.clip.width : mkObj.offsetWidth
230                 mH = (olNs4) ? mkObj.clip.height : mkObj.offsetHeight
231         }
233         if (!isNaN(mp) || typeof mp == 'object') {
234                 if (typeof mp == 'object') {
235                         fx = parseFloat(mp[0]);
236                         fy = parseFloat(mp[1]);
237                 } else
238                         fx = fy = parseFloat(mp);
239                 off = [Math.round(fx*mW), Math.round(fy*mH)];
240         } else {
241                  if (mp == 'UR') off = [mW, 0]
242                  else if (mp == 'LL') off = [0, mH]
243                  else if (mp == 'LR') off = [mW, mH]
244         }
246         if (typeof o3_dragimg != 'undefined' &&  o3_dragimg) return off;
247         else {
248                 if (!isNaN(puc) || typeof puc == 'object' ) {
249                         if (typeof puc == 'object') {
250                                 fx = parseFloat(puc[0]);
251                                 fy = parseFloat(puc[1]);
252                         } else
253                                 fx = fy = parseFloat(puc);
254                         off[0] -= Math.round(fx*(pW - sx));
255                         off[1] -= Math.round(fy*(pH - sy));
256                 } else {
257                         if (puc == 'UR') {
258                                 off[0] -= (pW - sx); 
259                                 off[1] -= sy
260                         } else if (puc == 'LL') {
261                                 off[0] -= sx;
262                                 off[1] -= (pH - sy)
263                         } else if (puc == 'LR') {
264                                 off[0] -= (pW-sx);
265                                 off[1] -= (pH - sy)
266                         }
267                 }
268                 return off
269         }
272 // Adapted to overlib from jwin by Jason Anderson -- http://www.jwinlib.com
273 function pageLocation(o, t){
274         var x = 0
276         while(o.offsetParent){
277                 x += o['offset' + t]
278                 o = o.offsetParent
279         }
280         x += o['offset' + t]
282         return x
285 // Adapted to overlib from jwin by Jason Anderson -- http://www.jwinlib.com
286 function getRefById(l, d){
287         var r = "", j
289         d = (d || document)
290         if (d.all) return d.all[l]    
291         else if (d.getElementById) return d.getElementById(l)
292         else if (d.layers &&  d.layers.length > 0) {
293                 if (d.layers[l]) return d.layers[l]
294                 
295                 for (j=0; j < d.layers.length; j++) {
296                         r = getRefById(l, d.layers[j].document)
297                         if(r) return r               
298                 }
299         }
301         return false
304 // Adapted to overlib from jwin by Jason Anderson -- http://www.jwinlib.com
305 function getRefByName(l, d) {
306         var r = null, j
308         d = (d || document)
310         if (d.images[l]) return d.images[l]
311         else if (d.anchors[l]) return d.anchors[l];
312         else if (d.layers &&  d.layers.length > 0) {
313                 for (j=0; j < d.layers.length; j++) {
314                         r = getRefByName(l, d.layers[j].document)
315                         if (r &&  r.length > 0) return r
316                         else if (r) return [r, d.layers[j]]
317                 }
318         }
320         return null
323 ////////
324 // PLUGIN REGISTRATIONS
325 ////////
326 registerRunTimeFunction(setAnchorVariables);
327 registerCmdLineFunction(parseAnchorExtras);
328 registerPostParseFunction(checkAnchorObject);
329 registerHook("createPopup", anchorPreface, FAFTER);
330 registerHook("horizontalPlacement", anchorHorizontal, FCHAIN);
331 registerHook("verticalPlacement", anchorVertical, FCHAIN);
332 if(olInfo.meets(4.10)) registerNoParameterCommands('noanchorwarn');