-
Notifications
You must be signed in to change notification settings - Fork 1
/
XinhaLoader.js
executable file
·283 lines (257 loc) · 8.32 KB
/
XinhaLoader.js
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var Xinha = {};
// Auto detect _editor_url if it's not set.
if (!window._editor_url)
{
(function() // wrap this in an ad-hoc function to avoid unecessary pollution of global namespace
{
// Because of the way the DOM is loaded, this is guaranteed to always pull our script tag.
var scripts = document.getElementsByTagName('script');
var this_script = scripts[scripts.length - 1];
// We'll allow two ways to specify arguments. We'll accept them in the
// argument of the script, or we'll accept them embedded into our script tag.
var args = this_script.src.split('?');
args = args.length == 2 ? args[1].split('&') : '';
for (var index = 0; index < args.length; ++index)
{
var arg = args[index].split('=');
if (arg.length == 2)
{
switch (arg[0])
{
case 'lang':
case 'icons':
case 'skin':
case 'url':
window['_editor_' + arg[0]] = arg[1];
break;
}
}
}
// We can grab the script innerHTML and execute that to cut down on script
// tags. Thanks John Resig!
// http://ejohn.org/blog/degrading-script-tags/
if (this_script.innerHTML.replace(/\s+/, ''))
{
eval(this_script.innerHTML);
}
// Default values
_editor_lang = window._editor_lang || 'en';
// Chop off any query string. Chop the filename off of the URL.
_editor_url = window._editor_url || this_script.src.split('?')[0].split('/').slice(0, -1).join('/');
})()
}
_editor_url = _editor_url.replace(/\x2f*$/, '/');
Xinha.agt = navigator.userAgent.toLowerCase();
Xinha.is_ie = ((Xinha.agt.indexOf("msie") != -1) && (Xinha.agt.indexOf("opera") == -1));
Xinha.ie_version= parseFloat(Xinha.agt.substring(Xinha.agt.indexOf("msie")+5));
Xinha.is_opera = (Xinha.agt.indexOf("opera") != -1);
Xinha.is_khtml = (Xinha.agt.indexOf("khtml") != -1);
Xinha.is_webkit = (Xinha.agt.indexOf("applewebkit") != -1);
Xinha.is_safari = (Xinha.agt.indexOf("safari") != -1);
Xinha.opera_version = navigator.appVersion.substring(0, navigator.appVersion.indexOf(" "))*1;
Xinha.is_mac = (Xinha.agt.indexOf("mac") != -1);
Xinha.is_mac_ie = (Xinha.is_ie && Xinha.is_mac);
Xinha.is_win_ie = (Xinha.is_ie && !Xinha.is_mac);
Xinha.is_gecko = (navigator.product == "Gecko" && !Xinha.is_safari); // Safari lies!
Xinha.isRunLocally = document.URL.toLowerCase().search(/^file:/) != -1;
Xinha.is_designMode = (typeof document.designMode != 'undefined' && !Xinha.is_ie); // IE has designMode, but we're not using it
Xinha.isSupportedBrowser = Xinha.is_gecko || (Xinha.is_opera && Xinha.opera_version >= 9.1) || Xinha.ie_version >= 5.5 || Xinha.is_safari;
Xinha.loadPlugins = function(plugins, callbackIfNotReady)
{
if ( !Xinha.isSupportedBrowser ) return;
Xinha.loadStyle(typeof _editor_css == "string" ? _editor_css : "Xinha.css","XinhaCoreDesign");
Xinha.createLoadingMessages(xinha_editors);
var loadingMessages = Xinha.loadingMessages;
Xinha._loadback(_editor_url + "XinhaCore.js",function () {
Xinha.removeLoadingMessages(xinha_editors);
Xinha.createLoadingMessages(xinha_editors);
callbackIfNotReady()
});
return false;
}
Xinha._loadback = function(Url, Callback, Scope, Bonus)
{
var T = !Xinha.is_ie ? "onload" : 'onreadystatechange';
var S = document.createElement("script");
S.type = "text/javascript";
S.src = Url;
if ( Callback )
{
S[T] = function()
{
if ( Xinha.is_ie && ( ! ( /loaded|complete/.test(window.event.srcElement.readyState) ) ) )
{
return;
}
Callback.call(Scope ? Scope : this, Bonus);
S[T] = null;
};
}
document.getElementsByTagName("head")[0].appendChild(S);
};
Xinha.getElementTopLeft = function(element)
{
var curleft = 0;
var curtop = 0;
if (element.offsetParent)
{
curleft = element.offsetLeft
curtop = element.offsetTop
while (element = element.offsetParent)
{
curleft += element.offsetLeft
curtop += element.offsetTop
}
}
return { top:curtop, left:curleft };
}
// find X position of an element
Xinha.findPosX = function(obj)
{
var curleft = 0;
if ( obj.offsetParent )
{
return Xinha.getElementTopLeft(obj).left;
}
else if ( obj.x )
{
curleft += obj.x;
}
return curleft;
};
// find Y position of an element
Xinha.findPosY = function(obj)
{
var curtop = 0;
if ( obj.offsetParent )
{
return Xinha.getElementTopLeft(obj).top;
}
else if ( obj.y )
{
curtop += obj.y;
}
return curtop;
};
Xinha.createLoadingMessages = function(xinha_editors)
{
if ( Xinha.loadingMessages || !Xinha.isSupportedBrowser )
{
return;
}
Xinha.loadingMessages = [];
for (var i=0;i<xinha_editors.length;i++)
{
if (!document.getElementById(xinha_editors[i]))
{
continue;
}
Xinha.loadingMessages.push(Xinha.createLoadingMessage(document.getElementById(xinha_editors[i])));
}
}
Xinha.createLoadingMessage = function(textarea,text)
{
if ( document.getElementById("loading_" + textarea.id) || !Xinha.isSupportedBrowser)
{
return;
}
// Create and show the main loading message and the sub loading message for details of loading actions
// global element
var loading_message = document.createElement("div");
loading_message.id = "loading_" + textarea.id;
loading_message.className = "loading";
loading_message.style.left = (Xinha.findPosX(textarea) + textarea.offsetWidth / 2) - 106 + 'px';
loading_message.style.top = (Xinha.findPosY(textarea) + textarea.offsetHeight / 2) - 50 + 'px';
// main static message
var loading_main = document.createElement("div");
loading_main.className = "loading_main";
loading_main.id = "loading_main_" + textarea.id;
loading_main.appendChild(document.createTextNode(Xinha._lc("Loading in progress. Please wait!")));
// sub dynamic message
var loading_sub = document.createElement("div");
loading_sub.className = "loading_sub";
loading_sub.id = "loading_sub_" + textarea.id;
text = text ? text : Xinha._lc("Loading Core");
loading_sub.appendChild(document.createTextNode(text));
loading_message.appendChild(loading_main);
loading_message.appendChild(loading_sub);
document.body.appendChild(loading_message);
return loading_sub;
}
Xinha.loadStyle = function(style, id)
{
var url = _editor_url || '';
url += style;
var head = document.getElementsByTagName("head")[0];
var link = document.createElement("link");
link.rel = "stylesheet";
link.href = url;
if (id) link.id = id;
head.appendChild(link);
};
Xinha._lc = function(string) {return string;}
Xinha._addEvent = function(el, evname, func)
{
if ( document.addEventListener )
{
el.addEventListener(evname, func, true);
}
else
{
el.attachEvent("on" + evname, func);
}
}
Xinha.addOnloadHandler = function (func)
{
// Dean Edwards/Matthias Miller/John Resig
// http://dean.edwards.name/weblog/2006/06/again/
// IE part from jQuery
var init = function ()
{
// quit if this function has already been called
if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
arguments.callee.done = true;
// kill the timer
if (Xinha.onloadTimer) clearInterval(Xinha.onloadTimer);
func.call();
}
if (Xinha.is_ie)
{
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent("onreadystatechange", function(){
if ( document.readyState === "complete" ) {
document.detachEvent( "onreadystatechange", arguments.callee );
init();
}
});
if ( document.documentElement.doScroll && typeof window.frameElement === "undefined" ) (function(){
if (arguments.callee.done) return;
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
document.documentElement.doScroll("left");
} catch( error ) {
setTimeout( arguments.callee, 0 );
return;
}
// and execute any waiting functions
init();
})();
}
else if (/WebKit/i.test(navigator.userAgent))
{
Xinha.onloadTimer = setInterval(function()
{
if (/loaded|complete/.test(document.readyState))
{
init(); // call the onload handler
}
}, 10);
}
else /* for Mozilla/Opera9 */
{
document.addEventListener("DOMContentLoaded", init, false);
}
}