root / trunk / XinhaLoader.js

Revision 1230, 8.3 kB (checked in by ray, 2 years ago)

avoid unecessary pollution of global namespace

  • Property svn:executable set to *
Line 
1var Xinha = {};
2
3// Auto detect _editor_url if it's not set.
4if (!window._editor_url)
5{
6  (function() // wrap this in an ad-hoc function to avoid unecessary pollution of global namespace
7  {
8    // Because of the way the DOM is loaded, this is guaranteed to always pull our script tag.
9    var scripts = document.getElementsByTagName('script');
10    var this_script = scripts[scripts.length - 1];
11 
12    // We'll allow two ways to specify arguments.  We'll accept them in the
13    // argument of the script, or we'll accept them embedded into our script tag.
14    var args = this_script.src.split('?');
15    args = args.length == 2 ? args[1].split('&') : '';
16    for (var index = 0; index < args.length; ++index)
17    {
18      var arg = args[index].split('=');
19      if (arg.length == 2)
20      {
21        switch (arg[0])
22        {
23          case 'lang':
24          case 'icons':
25          case 'skin':
26          case 'url':
27            window['_editor_' + arg[0]] = arg[1];
28            break;
29        }
30      }
31    }
32   
33    // We can grab the script innerHTML and execute that to cut down on script
34    // tags.  Thanks John Resig!
35    // http://ejohn.org/blog/degrading-script-tags/
36    if (this_script.innerHTML.replace(/\s+/, ''))
37    {
38      eval(this_script.innerHTML);
39    }
40   
41    // Default values
42    _editor_lang = window._editor_lang || 'en';
43   
44    // Chop off any query string.  Chop the filename off of the URL.
45    _editor_url = window._editor_url || this_script.src.split('?')[0].split('/').slice(0, -1).join('/');
46
47  })()
48}
49_editor_url = _editor_url.replace(/\x2f*$/, '/');
50
51Xinha.agt       = navigator.userAgent.toLowerCase();
52Xinha.is_ie    = ((Xinha.agt.indexOf("msie") != -1) && (Xinha.agt.indexOf("opera") == -1));
53Xinha.ie_version= parseFloat(Xinha.agt.substring(Xinha.agt.indexOf("msie")+5));
54Xinha.is_opera  = (Xinha.agt.indexOf("opera") != -1);
55Xinha.is_khtml  = (Xinha.agt.indexOf("khtml") != -1);
56Xinha.is_webkit  = (Xinha.agt.indexOf("applewebkit") != -1);
57Xinha.is_safari  = (Xinha.agt.indexOf("safari") != -1);
58Xinha.opera_version = navigator.appVersion.substring(0, navigator.appVersion.indexOf(" "))*1;
59Xinha.is_mac   = (Xinha.agt.indexOf("mac") != -1);
60Xinha.is_mac_ie = (Xinha.is_ie && Xinha.is_mac);
61Xinha.is_win_ie = (Xinha.is_ie && !Xinha.is_mac);
62Xinha.is_gecko  = (navigator.product == "Gecko" && !Xinha.is_safari); // Safari lies!
63Xinha.isRunLocally = document.URL.toLowerCase().search(/^file:/) != -1;
64Xinha.is_designMode = (typeof document.designMode != 'undefined' && !Xinha.is_ie); // IE has designMode, but we're not using it
65Xinha.isSupportedBrowser = Xinha.is_gecko || (Xinha.is_opera && Xinha.opera_version >= 9.1) || Xinha.ie_version >= 5.5 || Xinha.is_safari;
66
67Xinha.loadPlugins = function(plugins, callbackIfNotReady)
68{
69  if ( !Xinha.isSupportedBrowser ) return;
70 
71  Xinha.loadStyle(typeof _editor_css == "string" ? _editor_css : "Xinha.css","XinhaCoreDesign");
72  Xinha.createLoadingMessages(xinha_editors);
73  var loadingMessages = Xinha.loadingMessages;
74  Xinha._loadback(_editor_url + "XinhaCore.js",function () {
75    Xinha.removeLoadingMessages(xinha_editors); 
76    Xinha.createLoadingMessages(xinha_editors); 
77    callbackIfNotReady()
78  });
79  return false;
80}
81
82Xinha._loadback = function(Url, Callback, Scope, Bonus)
83
84  var T = !Xinha.is_ie ? "onload" : 'onreadystatechange';
85  var S = document.createElement("script");
86  S.type = "text/javascript";
87  S.src = Url;
88  if ( Callback )
89  {
90    S[T] = function()
91    {     
92      if ( Xinha.is_ie && ( ! ( /loaded|complete/.test(window.event.srcElement.readyState) ) ) )
93      {
94        return;
95      }
96     
97      Callback.call(Scope ? Scope : this, Bonus);
98      S[T] = null;
99    };
100  }
101  document.getElementsByTagName("head")[0].appendChild(S);
102};
103
104Xinha.getElementTopLeft = function(element)
105{
106  var curleft = 0;
107  var curtop = 0;
108  if (element.offsetParent)
109  {
110    curleft = element.offsetLeft
111    curtop = element.offsetTop
112    while (element = element.offsetParent)
113    {
114      curleft += element.offsetLeft
115      curtop += element.offsetTop
116    }
117  }
118  return { top:curtop, left:curleft };
119}
120
121// find X position of an element
122Xinha.findPosX = function(obj)
123{
124  var curleft = 0;
125  if ( obj.offsetParent )
126  {
127    return Xinha.getElementTopLeft(obj).left;   
128  }
129  else if ( obj.x )
130  {
131    curleft += obj.x;
132  }
133  return curleft;
134};
135
136// find Y position of an element
137Xinha.findPosY = function(obj)
138{
139  var curtop = 0;
140  if ( obj.offsetParent )
141  {
142    return Xinha.getElementTopLeft(obj).top;   
143  }
144  else if ( obj.y )
145  {
146    curtop += obj.y;
147  }
148  return curtop;
149};
150
151Xinha.createLoadingMessages = function(xinha_editors)
152{
153  if ( Xinha.loadingMessages || !Xinha.isSupportedBrowser )
154  {
155    return;
156  }
157  Xinha.loadingMessages = [];
158 
159  for (var i=0;i<xinha_editors.length;i++)
160  {
161    if (!document.getElementById(xinha_editors[i]))
162    {
163          continue;
164    }
165    Xinha.loadingMessages.push(Xinha.createLoadingMessage(document.getElementById(xinha_editors[i])));
166  }
167}
168
169Xinha.createLoadingMessage = function(textarea,text)
170{
171  if ( document.getElementById("loading_" + textarea.id) || !Xinha.isSupportedBrowser)
172  {
173    return;
174  }
175  // Create and show the main loading message and the sub loading message for details of loading actions
176  // global element
177  var loading_message = document.createElement("div");
178  loading_message.id = "loading_" + textarea.id;
179  loading_message.className = "loading";
180 
181  loading_message.style.left = (Xinha.findPosX(textarea) + textarea.offsetWidth / 2) - 106 +  'px';
182  loading_message.style.top = (Xinha.findPosY(textarea) + textarea.offsetHeight / 2) - 50 +  'px';
183  // main static message
184  var loading_main = document.createElement("div");
185  loading_main.className = "loading_main";
186  loading_main.id = "loading_main_" + textarea.id;
187  loading_main.appendChild(document.createTextNode(Xinha._lc("Loading in progress. Please wait!")));
188  // sub dynamic message
189  var loading_sub = document.createElement("div");
190  loading_sub.className = "loading_sub";
191  loading_sub.id = "loading_sub_" + textarea.id;
192  text = text ? text : Xinha._lc("Loading Core");
193  loading_sub.appendChild(document.createTextNode(text));
194  loading_message.appendChild(loading_main);
195  loading_message.appendChild(loading_sub);
196  document.body.appendChild(loading_message);
197 
198  return loading_sub;
199}
200
201Xinha.loadStyle = function(style, id)
202{
203  var url = _editor_url || '';
204 
205  url += style;
206 
207  var head = document.getElementsByTagName("head")[0];
208  var link = document.createElement("link");
209  link.rel = "stylesheet";
210  link.href = url;
211  if (id) link.id = id;
212  head.appendChild(link);
213};
214Xinha._lc = function(string) {return string;}
215
216Xinha._addEvent = function(el, evname, func)
217{
218  if ( document.addEventListener )
219  {
220    el.addEventListener(evname, func, true);
221  }
222  else
223  {
224    el.attachEvent("on" + evname, func);
225  }
226}
227Xinha.addOnloadHandler = function (func)
228{
229  // Dean Edwards/Matthias Miller/John Resig
230  // http://dean.edwards.name/weblog/2006/06/again/
231  // IE part from jQuery
232 
233 
234  var init = function ()
235  {
236    // quit if this function has already been called
237    if (arguments.callee.done) return;
238    // flag this function so we don't do the same thing twice
239    arguments.callee.done = true;
240    // kill the timer
241    if (Xinha.onloadTimer) clearInterval(Xinha.onloadTimer);
242   
243    func.call();
244  }
245  if (Xinha.is_ie)
246  {
247    // ensure firing before onload,
248    // maybe late but safe also for iframes
249    document.attachEvent("onreadystatechange", function(){
250      if ( document.readyState === "complete" ) {
251        document.detachEvent( "onreadystatechange", arguments.callee );
252        init();
253      }
254    });
255    if ( document.documentElement.doScroll && typeof window.frameElement === "undefined" ) (function(){
256      if (arguments.callee.done) return;
257      try {
258        // If IE is used, use the trick by Diego Perini
259        // http://javascript.nwbox.com/IEContentLoaded/
260        document.documentElement.doScroll("left");
261      } catch( error ) {
262        setTimeout( arguments.callee, 0 );
263        return;
264      }
265      // and execute any waiting functions
266      init();
267    })();
268  }
269  else if (/WebKit/i.test(navigator.userAgent))
270  {
271    Xinha.onloadTimer = setInterval(function()
272    {
273      if (/loaded|complete/.test(document.readyState))
274      {
275        init(); // call the onload handler
276      }
277    }, 10);
278  }
279  else /* for Mozilla/Opera9 */
280  {
281    document.addEventListener("DOMContentLoaded", init, false); 
282  }
283}
Note: See TracBrowser for help on using the browser.