1 |
dpavlin |
55 |
// ========================================================================= |
2 |
|
|
/// IWF - Interactive Website Framework. Javascript library for creating |
3 |
|
|
/// responsive thin client interfaces. |
4 |
|
|
/// |
5 |
|
|
/// Copyright (C) 2005 Brock Weaver brockweaver@users.sourceforge.net |
6 |
|
|
/// |
7 |
|
|
/// This library is free software; you can redistribute it and/or modify |
8 |
|
|
/// it under the terms of the GNU Lesser General Public License as published |
9 |
|
|
/// by the Free Software Foundation; either version 2.1 of the License, or |
10 |
|
|
/// (at your option) any later version. |
11 |
|
|
/// |
12 |
|
|
/// This library is distributed in the hope that it will be useful, but |
13 |
|
|
/// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
14 |
|
|
/// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
15 |
|
|
/// License for more details. |
16 |
|
|
/// |
17 |
|
|
/// You should have received a copy of the GNU Lesser General Public License |
18 |
|
|
/// along with this library; if not, write to the Free Software Foundation, |
19 |
|
|
/// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
/// |
21 |
|
|
/// Brock Weaver |
22 |
|
|
/// brockweaver@users.sourceforge.net |
23 |
|
|
/// 1605 NW Maple Pl |
24 |
|
|
/// Ankeny, IA 50021 |
25 |
|
|
/// |
26 |
|
|
//! http://iwf.sourceforge.net/ |
27 |
|
|
// -------------------------------------------------------------------------- |
28 |
|
|
//! NOTE: To minimize file size, strip all fluffy comments (except the LGPL, of course!) |
29 |
|
|
//! using the following regex (global flag and multiline on): |
30 |
|
|
//! ^\t*//([^/!].*|$) |
31 |
dpavlin |
46 |
// |
32 |
dpavlin |
55 |
// This reduces file size by about 30%, give or take. |
33 |
dpavlin |
46 |
// |
34 |
dpavlin |
55 |
//! To rip out only logging statements (commented or uncommented): |
35 |
|
|
//! ^/{0,2}iwfLog.* |
36 |
|
|
// -------------------------------------------------------------------------- |
37 |
dpavlin |
46 |
|
38 |
dpavlin |
55 |
|
39 |
dpavlin |
46 |
// -------------------------------------------------------------------------- |
40 |
|
|
// |
41 |
dpavlin |
55 |
//! iwfajax.js |
42 |
|
|
// |
43 |
dpavlin |
46 |
// Thread-safe background xml request via XmlHttpRequest object |
44 |
|
|
// |
45 |
dpavlin |
55 |
//! Dependencies: |
46 |
|
|
//! iwfxml.js |
47 |
|
|
//! iwfcore.js |
48 |
|
|
//! iwfgui.js (optional -- pretty progress bar) |
49 |
dpavlin |
46 |
// |
50 |
dpavlin |
55 |
//! Brock Weaver - brockweaver@users.sourceforge.net |
51 |
|
|
//! v 0.2 - 2005-11-14 |
52 |
|
|
//! core bug patch |
53 |
dpavlin |
46 |
// -------------------------------------------------------------------------- |
54 |
dpavlin |
55 |
//! v 0.1 - 2005-06-05 |
55 |
|
|
//! Initial release. |
56 |
|
|
// |
57 |
|
|
//! Known Issues: |
58 |
|
|
//! AddToHistory does not work |
59 |
|
|
//! Iframe not implemented |
60 |
|
|
// |
61 |
|
|
// ========================================================================= |
62 |
dpavlin |
46 |
|
63 |
dpavlin |
55 |
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
// show progress bar if they included iwfgui.js, window.status otherwise. |
67 |
|
|
var _iwfShowGuiProgress = iwfExists(window.iwfShow); |
68 |
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
// ----------------------------------- |
75 |
|
|
// Begin: Dependency Check |
76 |
|
|
// ----------------------------------- |
77 |
|
|
|
78 |
dpavlin |
46 |
if (!window.iwfGetById || !window.iwfXmlDoc){ |
79 |
|
|
iwfLog("IWF Dependency Error: iwfajax.js is dependent upon both iwfcore.js and iwfxml.js, so you *must* reference those files first.\n\nExample:\n\n<script type='text/javascript' src='iwfcore.js'></script>\n<script type='text/javascript' src='iwfxml.js'></script>\n<script type='text/javascript' src='iwfajax.js'></script>", true); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
// ----------------------------------- |
83 |
dpavlin |
55 |
// End: Dependency Check |
84 |
|
|
// ----------------------------------- |
85 |
|
|
|
86 |
|
|
// ----------------------------------- |
87 |
dpavlin |
46 |
// Begin: AJAX Request and Response |
88 |
|
|
// ----------------------------------- |
89 |
|
|
|
90 |
|
|
function iwfRequest(urlOrForm, targetElementOnResponse, addToHistory, callback){ |
91 |
|
|
|
92 |
|
|
// we use a javascript feature here called "inner functions" |
93 |
|
|
// using these means the local variables retain their values after the outer function |
94 |
|
|
// has returned. this is useful for thread safety, so |
95 |
|
|
// reassigning the onreadystatechange function doesn't stomp over earlier requests. |
96 |
|
|
|
97 |
|
|
function iwfBindCallback(){ |
98 |
|
|
if (req.readyState == 4) { |
99 |
|
|
_iwfOnRequestEnd(); |
100 |
|
|
if (req.status == 200 || req.status == 0) { |
101 |
dpavlin |
55 |
//iwfLog('exact response from server:\n\n' + req.responseText); |
102 |
dpavlin |
46 |
_iwfResponseHandler(req.responseText, localCallback, localTarget); |
103 |
|
|
} else { |
104 |
|
|
_iwfOnRequestError(req.status, req.statusText, req.responseText); |
105 |
|
|
} |
106 |
|
|
return; |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
// determine how to hit the server... |
111 |
|
|
var url = null; |
112 |
|
|
var method = 'GET'; |
113 |
|
|
var postdata = null; |
114 |
|
|
var isFromForm = true; |
115 |
|
|
var contentType = 'application/x-www-form-urlencoded'; |
116 |
|
|
|
117 |
|
|
if (iwfIsString(urlOrForm)){ |
118 |
|
|
|
119 |
|
|
// if we get here, they either specified the url or the name of the form. |
120 |
|
|
// either way, flag so we return nothing. |
121 |
|
|
isFromForm = false; |
122 |
|
|
|
123 |
|
|
var frm = iwfGetForm(urlOrForm); |
124 |
|
|
if (!frm){ |
125 |
|
|
// is a url. |
126 |
|
|
url = urlOrForm; |
127 |
|
|
method = 'GET'; |
128 |
|
|
postdata = null; |
129 |
|
|
} else { |
130 |
|
|
// is name of a form. |
131 |
|
|
// fill with the form object. |
132 |
|
|
urlOrForm = frm; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
// use a local variable to hold our callback and target until the inner function is called... |
138 |
|
|
var localCallback = callback; |
139 |
|
|
var localTarget = targetElementOnResponse; |
140 |
|
|
|
141 |
|
|
if (!iwfIsString(urlOrForm)){ |
142 |
|
|
|
143 |
|
|
var ctl = null; |
144 |
|
|
|
145 |
|
|
|
146 |
|
|
// is a form or a control in the form. |
147 |
|
|
if (iwfExists(urlOrForm.form)){ |
148 |
|
|
// is a control in the form. jump up to the form. |
149 |
|
|
ctl = urlOrForm; |
150 |
|
|
urlOrForm = urlOrForm.form; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
|
154 |
|
|
// if they passed a form and no local target, lookup the form.iwfTarget attribute and use it if possible |
155 |
|
|
if (!localTarget){ |
156 |
|
|
localTarget = iwfAttribute(urlOrForm, 'iwfTarget'); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
if (localTarget){ |
160 |
dpavlin |
55 |
var elTgt = iwfGetOrCreateByNameWithinForm(urlOrForm, 'iwfTarget', 'input', 'hidden'); |
161 |
dpavlin |
46 |
if (elTgt){ |
162 |
|
|
iwfAttribute(elTgt, 'value', localTarget); |
163 |
|
|
iwfRemoveAttribute(elTgt, 'disabled'); |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
url = urlOrForm.action; |
169 |
|
|
method = urlOrForm.method.toUpperCase(); |
170 |
|
|
switch(method){ |
171 |
|
|
case "POST": |
172 |
|
|
postdata = _iwfGetFormData(urlOrForm, url, ctl); |
173 |
|
|
|
174 |
|
|
// we also need to properly set the content-type header... |
175 |
|
|
var frm = iwfGetForm(urlOrForm); |
176 |
|
|
if (frm){ |
177 |
|
|
var enc = iwfAttribute(frm, 'encoding'); |
178 |
|
|
if (!enc){ |
179 |
|
|
enc = iwfAttribute(frm, 'enctype'); |
180 |
|
|
} |
181 |
|
|
if (enc){ |
182 |
|
|
contentType = enc; |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
break; |
187 |
|
|
case "GET": |
188 |
|
|
default: |
189 |
|
|
url = _iwfGetFormData(urlOrForm, url, ctl); |
190 |
|
|
break; |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
// prevent any browser caching of our url by requesting a unique url everytime... |
195 |
|
|
url += ((url.indexOf('?') > -1) ? '&' : '?') + 'iwfRequestId=' + new Date().valueOf(); |
196 |
|
|
|
197 |
dpavlin |
55 |
//iwfLog("url = " + url); |
198 |
|
|
//iwfLog("method = " + method); |
199 |
|
|
//iwfLog("postdata = " + postdata); |
200 |
|
|
//iwfLog("contenttype = " + contentType); |
201 |
dpavlin |
46 |
|
202 |
|
|
|
203 |
|
|
var req = null; |
204 |
|
|
if (!addToHistory){ |
205 |
dpavlin |
55 |
//iwfLog("using XHR to perform request..."); |
206 |
dpavlin |
46 |
// use XHR to perform the request, as this will |
207 |
|
|
// prevent the browser from adding it to history. |
208 |
|
|
req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); |
209 |
|
|
|
210 |
|
|
// bind our callback |
211 |
|
|
req.onreadystatechange = iwfBindCallback; |
212 |
|
|
|
213 |
|
|
// show progress if it's not already visible... |
214 |
|
|
_iwfOnRequestStart(); |
215 |
|
|
|
216 |
|
|
// hit the server |
217 |
|
|
req.open(method, url, true); |
218 |
|
|
req.setRequestHeader('Content-Type', contentType); |
219 |
|
|
req.send(postdata); |
220 |
|
|
} else { |
221 |
|
|
// use an IFRAME element to perform the request, |
222 |
|
|
// as this will cause the browser to add it to history. |
223 |
|
|
// TODO: make this work!!! |
224 |
|
|
iwfLog("using IFRAME to perform request..."); |
225 |
|
|
iwfLog('request and add to history not implemented yet!!!', true); |
226 |
|
|
return; |
227 |
|
|
|
228 |
|
|
var el = iwfGetById("iwfHistoryFrame"); |
229 |
|
|
if (!el){ |
230 |
|
|
iwfLog("To enable history tracking in IWF, you must add an invisible IFRAME element to your page:\n<IFRAME id='iwfHistoryFrame' style='display:none'></IFRAME>", true); |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
el.src = url; |
234 |
|
|
|
235 |
|
|
// show progress if it's not already visible... |
236 |
|
|
_iwfOnRequestStart(); |
237 |
|
|
|
238 |
|
|
|
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
// if this is called from the form.onsubmit event, make sure the form doesn't submit... |
242 |
|
|
|
243 |
|
|
if (isFromForm){ |
244 |
|
|
return false; |
245 |
|
|
} else { |
246 |
|
|
// return absolutely nothing so anchor tags don't hork the current page |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
function _iwfGetFormData(form, url, ctl){ |
252 |
|
|
|
253 |
|
|
var method = form.method; |
254 |
|
|
if (!method) method = "get"; |
255 |
|
|
|
256 |
|
|
var output = null; |
257 |
|
|
|
258 |
|
|
if (method == 'get'){ |
259 |
|
|
output = url + ((url.indexOf('?') > -1) ? '&' : '?'); |
260 |
|
|
} else { |
261 |
|
|
output = ''; |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
|
265 |
|
|
// if there is a target specified in the <form> tag, |
266 |
|
|
// copy its contents to a hidden <input type='hidden'> tag. |
267 |
|
|
|
268 |
dpavlin |
55 |
//iwfLog("total elements in form named '" + form.name + "': " + form.elements.length); |
269 |
dpavlin |
46 |
for(var i=0;i<form.elements.length;i++){ |
270 |
|
|
var el = form.elements[i]; |
271 |
|
|
var nm = iwfAttribute(el, 'name'); |
272 |
|
|
var val = null; |
273 |
|
|
if (!iwfAttribute(el, 'disabled') && nm){ |
274 |
|
|
switch (el.tagName.toLowerCase()){ |
275 |
|
|
case 'input': |
276 |
|
|
switch(iwfAttribute(el, 'type')){ |
277 |
|
|
case 'checkbox': |
278 |
dpavlin |
69 |
if (iwfAttribute(el, 'checked')){ |
279 |
|
|
val = 'on'; |
280 |
|
|
} |
281 |
dpavlin |
46 |
case 'radio': |
282 |
dpavlin |
69 |
if (iwfAttribute(el, 'checked')){ |
283 |
|
|
val = iwfAttribute(el, 'value'); |
284 |
dpavlin |
46 |
} |
285 |
|
|
break; |
286 |
|
|
case 'button': |
287 |
|
|
if (el == ctl){ |
288 |
|
|
val = iwfAttribute(el, 'value'); |
289 |
|
|
} |
290 |
|
|
break; |
291 |
|
|
case 'submit': |
292 |
|
|
if (el == ctl){ |
293 |
|
|
val = iwfAttribute(el, 'value'); |
294 |
|
|
} |
295 |
|
|
break; |
296 |
|
|
case 'text': |
297 |
|
|
default: |
298 |
|
|
val = iwfAttribute(el, 'value'); |
299 |
|
|
break; |
300 |
|
|
case 'file': |
301 |
|
|
iwfLog('TODO: implement <input type="file"> in _iwfGetFormData', true); |
302 |
|
|
val = iwfAttribute(el, 'value'); |
303 |
|
|
break; |
304 |
|
|
case 'image': |
305 |
|
|
iwfLog('TODO: implement <input type="image"> in _iwfGetFormData', true); |
306 |
|
|
val = iwfAttribute(el, 'value'); |
307 |
|
|
break; |
308 |
|
|
case 'reset': |
309 |
|
|
iwfLog('TODO: implement <input type="reset"> in _iwfGetFormData', true); |
310 |
|
|
break; |
311 |
|
|
case 'hidden': |
312 |
|
|
val = iwfAttribute(el, 'value'); |
313 |
|
|
break; |
314 |
|
|
} |
315 |
|
|
break; |
316 |
|
|
case 'textarea': |
317 |
dpavlin |
56 |
val = iwfAttribute(el, 'innerText') || el.value; |
318 |
dpavlin |
46 |
break; |
319 |
|
|
case 'button': |
320 |
|
|
if (el == ctl){ |
321 |
dpavlin |
56 |
val = iwfAttribute(el, 'innerText') || el.value; |
322 |
dpavlin |
46 |
} |
323 |
|
|
break; |
324 |
|
|
case 'select': |
325 |
|
|
for(var j=0;j<el.options.length;j++){ |
326 |
|
|
if (iwfAttribute(el.options[j], 'selected') == 'true'){ |
327 |
|
|
if (!val){ |
328 |
|
|
val = iwfAttribute(el.options[j], 'value'); |
329 |
|
|
} else { |
330 |
|
|
val += '+' + iwfAttribute(el.options[j], 'value'); |
331 |
|
|
} |
332 |
|
|
} |
333 |
|
|
} |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
if (val){ |
337 |
|
|
if (output.length > 0){ |
338 |
|
|
output += '&'; |
339 |
|
|
} |
340 |
|
|
output += escape(nm) + '=' + escape(val); |
341 |
|
|
} |
342 |
|
|
} |
343 |
|
|
} |
344 |
|
|
if (output.length == 0){ |
345 |
|
|
return null; |
346 |
|
|
} else { |
347 |
|
|
return output; |
348 |
|
|
} |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
function _iwfResponseReceived(doc){ |
352 |
dpavlin |
55 |
iwfLog('iframeloaded'); |
353 |
dpavlin |
46 |
var xmlDoc = new iwfXmlDoc(doc.innerHTML); |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
function _iwfResponseHandler(origText, callback, tgt){ |
357 |
|
|
var doc = new iwfXmlDoc(origText); |
358 |
|
|
if (!doc.response){ |
359 |
|
|
// not in our default xml format. |
360 |
|
|
// just throw out the xml to the callback, if any |
361 |
|
|
if (callback){ |
362 |
|
|
callback(doc); |
363 |
|
|
} else { |
364 |
|
|
iwfLog("IWF Ajax Error: No callback defined for non-standard response:\n" + origText, true); |
365 |
|
|
} |
366 |
|
|
} else { |
367 |
|
|
if (doc.response.debugging == 'true'){ |
368 |
|
|
iwfLoggingEnabled = true; |
369 |
|
|
iwfLog("IWF Ajax Debugging:\nParsed response:\n\n" + doc.response.outerXml(true), true); |
370 |
|
|
iwfShowLog(); |
371 |
|
|
} |
372 |
|
|
for(var i=0; i< doc.response.childNodes.length; i++){ |
373 |
|
|
var node = doc.response.childNodes[i]; |
374 |
|
|
if (node.nodeName.indexOf("#") != 0){ |
375 |
dpavlin |
55 |
//iwfLog('node.target=' + node.target + '\ntgt=' + tgt); |
376 |
dpavlin |
46 |
if (!tgt) { |
377 |
|
|
// server target is ignored if a client target exists. |
378 |
|
|
tgt = node.target; |
379 |
|
|
} |
380 |
|
|
if (node.errorCode && iwfToInt(node.errorCode, true) != 0){ |
381 |
|
|
// an error occurred. |
382 |
|
|
_iwfOnRequestError(node.errorCode, node.errorMessage); |
383 |
|
|
} else { |
384 |
|
|
if (!node.type){ |
385 |
|
|
node.type = ""; |
386 |
|
|
} |
387 |
|
|
switch(node.type.toLowerCase()){ |
388 |
|
|
case "html": |
389 |
|
|
case "xhtml": |
390 |
|
|
var innerHtml = node.innerHtml(); |
391 |
|
|
//iwfLog('parsed html response:\n\n' + innerHtml); |
392 |
|
|
_iwfInsertHtml(innerHtml, tgt); |
393 |
|
|
break; |
394 |
|
|
case "javascript": |
395 |
|
|
case "js": |
396 |
|
|
var bomb = true; |
397 |
|
|
if (node.childNodes.length == 1){ |
398 |
|
|
var js = node.childNodes[0]; |
399 |
|
|
if (js.nodeName == '#cdata' || js.nodeName == '#comment'){ |
400 |
|
|
bomb = false; |
401 |
|
|
var code = js.getText(); |
402 |
|
|
eval(code); |
403 |
|
|
} |
404 |
|
|
} |
405 |
|
|
if (bomb){ |
406 |
|
|
iwfLog("IWF Ajax Error: When an <action> is defined of type javascript, it content must be contained by either a Comment (<!-- -->) or CDATA (<![CDATA[ ]]>).\nCDATA is the more appropriate manner, as this is the xml-compliant one.\nHowever, COMMENT is also allowed as this is html-compliant, such as within a <script> element.\n\n<action> xml returned:\n\n" + node.outerXml(), true); |
407 |
|
|
} |
408 |
|
|
break; |
409 |
|
|
case "xml": |
410 |
|
|
if (callback){ |
411 |
|
|
callback(node); |
412 |
|
|
} |
413 |
|
|
break; |
414 |
|
|
case "debug": |
415 |
|
|
iwfLog("IWF Debug: <action> type identified as 'debug'.\nXml received for current action:\n\n" + node.outerXml(), true); |
416 |
|
|
break; |
417 |
|
|
default: |
418 |
|
|
iwfLog('IWF Ajax Error: <action> type of "' + node.type + '" is not a valid option.\n\nValid options:\n\'html\' or \'xhtml\' = parse as html and inject into element with the id specified by the target attribute\n\'javascript\' or \'js\' = parse as javascript and execute\n\'xml\' = parse as xml and call the callback specified when iwfRequest() was called\n\'debug\' = parse as xml and log/alert the result\n\n<action> xml returned:\n\n' + node.outerXml(), true); |
419 |
|
|
break; |
420 |
|
|
} |
421 |
|
|
} |
422 |
|
|
} |
423 |
|
|
} |
424 |
|
|
} |
425 |
|
|
|
426 |
|
|
} |
427 |
|
|
|
428 |
|
|
function _iwfInsertHtml(html, parentNodeId){ |
429 |
|
|
if(!parentNodeId){ |
430 |
|
|
parentNodeId = 'iwfContent'; |
431 |
|
|
iwfLog("IWF Ajax Warning: <action> with a type of 'html' does not have its target attribute specified, so using the default of 'iwfContent'."); |
432 |
|
|
} |
433 |
|
|
|
434 |
|
|
if(!parentNodeId){ |
435 |
|
|
iwfLog("IWF Ajax Error: <action> node with a type of 'html' does not have its target attribute specified to a valid element.\nPlease specify the id of the element into which the contents of the <action> node should be placed.\nTo fill the entire page, simply specify 'body'.", true); |
436 |
|
|
} else { |
437 |
|
|
var el = iwfGetById(parentNodeId); |
438 |
|
|
if (!el){ |
439 |
|
|
if (parentNodeId == 'body'){ |
440 |
|
|
el = iwfGetByTagName('body'); |
441 |
|
|
if (!el || el.length == 0){ |
442 |
|
|
iwfLog("IWF Ajax Error: Could not locate the tag named 'body'", true); |
443 |
|
|
return; |
444 |
|
|
} else { |
445 |
|
|
el = el[0]; |
446 |
|
|
} |
447 |
|
|
} else { |
448 |
|
|
iwfLog('IWF Ajax Error: Could not locate element with id of ' + parentNodeId + ' into which the following html should be placed:\n\n' + html, true); |
449 |
|
|
return; |
450 |
|
|
} |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
//iwfLog(iwfElementToString(el)); |
454 |
|
|
//iwfLog(html); |
455 |
|
|
|
456 |
|
|
// trying to shove a <form> node inside another <form> node is a bad thing. |
457 |
|
|
// make sure we don't do that here. |
458 |
|
|
var re = /<form/i; |
459 |
|
|
var match = re.exec(html); |
460 |
|
|
if (match && document.forms.length > 0){ |
461 |
|
|
// our html to inject contains a form node. |
462 |
|
|
// bubble up the chain until we find a <form> node, or we have no parents |
463 |
|
|
var elParent = el; |
464 |
dpavlin |
66 |
// I have doubts about adding elParent.tagName here |
465 |
|
|
// but I don't have better idea. -dpavlin |
466 |
|
|
while (elParent && elParent.tagName && elParent.tagName.toLowerCase() != 'form'){ |
467 |
dpavlin |
46 |
elParent = iwfGetParent(elParent); |
468 |
|
|
} |
469 |
|
|
|
470 |
dpavlin |
66 |
if (elParent && elParent.tagName && elParent.tagName.toLowerCase() == 'form'){ |
471 |
dpavlin |
46 |
iwfLog('IWF Ajax Error: Attempting to inject html which contains a <form> node into a target element which is itself a <form> node, or is already contained by a <form> node.\nThis is bad html, and will not work appropriately on some major browsers.', true); |
472 |
|
|
} |
473 |
|
|
} |
474 |
|
|
|
475 |
|
|
|
476 |
|
|
el.innerHTML = html; |
477 |
|
|
|
478 |
|
|
|
479 |
|
|
// if there is a script element, we have to explicitly add it to the body element, |
480 |
|
|
// as IE and firefox just don't like it when you try to add it as part of the innerHTML |
481 |
|
|
// property. Go figure. |
482 |
|
|
|
483 |
|
|
var i = 0; |
484 |
|
|
// don't stomp on any existing scripts... |
485 |
|
|
while (iwfGetById('iwfScript' + i)){ |
486 |
|
|
i++; |
487 |
|
|
} |
488 |
|
|
|
489 |
|
|
var scriptStart = html.indexOf("<script"); |
490 |
|
|
while(scriptStart > -1){ |
491 |
|
|
scriptStart = html.indexOf(">", scriptStart) + 1; |
492 |
|
|
|
493 |
|
|
// copy contents of script into a default holder |
494 |
|
|
var scriptEnd = html.indexOf("</script>", scriptStart); |
495 |
|
|
var scriptHtml = html.substr(scriptStart, scriptEnd - scriptStart); |
496 |
|
|
|
497 |
|
|
var re = /^\s*<!--/; |
498 |
|
|
var match = re.exec(scriptHtml); |
499 |
|
|
if (!match){ |
500 |
|
|
iwfLog("IWF Ajax Error: Developer, you *must* put the <!-- and //--> 'safety net' around your script within all <script> tags.\nThe offending <script> tag contains the following code:\n\n" + scriptHtml + "\n\nThis requirement is due to how IWF injects the html so the browser is able to actually parse the script and make its contents available for execution.", true); |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
// this code is the worst hack in this entire framework. |
504 |
|
|
// the code in the try portion should work anywhere -- but |
505 |
dpavlin |
55 |
// IE barfs when you try to set the innerHTML on a script element. |
506 |
|
|
// not only that, but IE won't parse script unless a visible element |
507 |
|
|
// is contained in the innerHTML when it is set, so we need the <code>IE hack here</code> |
508 |
dpavlin |
46 |
// and it cannot be removed. |
509 |
|
|
// I don't understand why creating a new node and setting its innerHTML causes the browsers |
510 |
|
|
// to parse and execute the script, but it does. |
511 |
|
|
// Note there is a major leak here, as we do not try to reuse existing iwfScriptXXXX elements |
512 |
|
|
// in case they are in use by other targets. To clean these up, simply call iwfCleanScripts |
513 |
|
|
// periodically. This is so app-dependent, I didn't want to try to keep track of everything |
514 |
|
|
// and possibly miss a case, causing really weird results that only show up occassionally. |
515 |
|
|
// |
516 |
|
|
// Plus I'm getting lazy. :) |
517 |
|
|
// |
518 |
|
|
try { |
519 |
dpavlin |
55 |
//! moz (DOM) |
520 |
|
|
var elScript = iwfGetOrCreateById('iwfScript' + i, 'script'); |
521 |
dpavlin |
46 |
elScript.type = 'text/javascript'; |
522 |
|
|
elScript.defer = 'true'; |
523 |
|
|
elScript.innerHTML = scriptHtml; |
524 |
|
|
|
525 |
dpavlin |
55 |
iwfAppendChild('body', elScript); |
526 |
|
|
|
527 |
dpavlin |
46 |
} catch(e){ |
528 |
dpavlin |
56 |
//iwfLog("IE Hack for injecting script tag...", true); |
529 |
dpavlin |
55 |
//! IE hack |
530 |
|
|
// IE needs a visible tag within a non-script element to have scripting apply... Don't ask me why, ask the IE team why. |
531 |
|
|
// My guess is the visible element causes the page to at least partially re-render, which in turn kicks off any script parsing |
532 |
|
|
// code in IE. |
533 |
dpavlin |
46 |
var elDiv = iwfGetOrCreateById('iwfScript' + i, '<div style="display:none"></div>', 'body'); |
534 |
dpavlin |
55 |
elDiv.innerHTML = '<code>IE hack here</code><script id="iwfScript' + i + '" defer="true">' + scriptHtml + '</script' + '>'; |
535 |
dpavlin |
46 |
} |
536 |
|
|
|
537 |
|
|
i++; |
538 |
|
|
|
539 |
|
|
scriptStart = html.indexOf("<script", scriptEnd+8); |
540 |
|
|
} |
541 |
|
|
} |
542 |
|
|
} |
543 |
|
|
|
544 |
|
|
function iwfCleanScripts(){ |
545 |
|
|
var i = 0; |
546 |
dpavlin |
55 |
while(iwfRemoveNode('iwfScript' + (i++))); |
547 |
|
|
|
548 |
dpavlin |
46 |
} |
549 |
|
|
|
550 |
|
|
|
551 |
|
|
|
552 |
|
|
var _iwfTotalRequests = 0; |
553 |
|
|
var _iwfPendingRequests = 0; |
554 |
|
|
var _iwfRequestTicker = null; |
555 |
|
|
var _iwfRequestTickCount = 0; |
556 |
|
|
var _iwfRequestTickDuration = 100; |
557 |
dpavlin |
55 |
|
558 |
|
|
|
559 |
|
|
// TODO: make the styles applied be configurable variables at the top of this file. |
560 |
|
|
function _iwfGetBusyBar(inner){ |
561 |
|
|
var b = iwfGetById('iwfBusy'); |
562 |
|
|
if (!b){ |
563 |
|
|
b = iwfGetOrCreateById('iwfBusy', 'div', 'body'); |
564 |
|
|
if(b.style){ |
565 |
|
|
b.style.position = 'absolute'; |
566 |
|
|
b.style.border = '1px solid black'; |
567 |
|
|
b.style.backgroundColor = '#efefef'; |
568 |
|
|
b.style.textAlign = 'center'; |
569 |
|
|
} |
570 |
|
|
iwfWidth(b, 100); |
571 |
|
|
iwfHeight(b, 20); |
572 |
|
|
iwfZIndex(b, 9999); |
573 |
|
|
|
574 |
|
|
iwfX(b, 0); |
575 |
|
|
iwfY(b, 0); |
576 |
|
|
|
577 |
|
|
// iwfX(b, iwfClientWidth() - iwfWidth(b)-5); |
578 |
|
|
// iwfY(b, iwfClientHeight() - iwfHeight(b)-5); |
579 |
|
|
|
580 |
|
|
iwfHide(b); |
581 |
|
|
} |
582 |
|
|
|
583 |
|
|
|
584 |
|
|
|
585 |
|
|
var bb = iwfGetById('iwfBusyBar'); |
586 |
|
|
if(!bb){ |
587 |
|
|
bb = iwfGetOrCreateById('iwfBusyBar', 'div', b); |
588 |
|
|
bb.style.backgroundColor = 'navy'; |
589 |
|
|
bb.style.color = 'white'; |
590 |
|
|
bb.style.textAlign = 'center'; |
591 |
|
|
iwfWidth(bb, 1); |
592 |
|
|
iwfHeight(bb, 20); |
593 |
|
|
iwfX(bb, 0); |
594 |
|
|
iwfY(bb, 0); |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
if(inner){ |
598 |
|
|
return bb; |
599 |
|
|
} else { |
600 |
|
|
return b; |
601 |
|
|
} |
602 |
|
|
|
603 |
|
|
} |
604 |
|
|
|
605 |
dpavlin |
46 |
function _iwfOnRequestStart(){ |
606 |
|
|
_iwfPendingRequests++; |
607 |
|
|
_iwfTotalRequests++; |
608 |
|
|
|
609 |
|
|
_iwfRequestTickDuration = 100; |
610 |
|
|
|
611 |
|
|
if (!_iwfRequestTicker){ |
612 |
|
|
_iwfRequestTickCount = 0; |
613 |
|
|
if (window.iwfOnRequestStart){ |
614 |
|
|
_iwfRequestTickDuration = iwfOnRequestStart(); |
615 |
dpavlin |
55 |
} else if (_iwfShowGuiProgress) { |
616 |
|
|
// use gui busy implementation |
617 |
|
|
var bb = _iwfGetBusyBar(true); |
618 |
|
|
iwfWidth(bb, 1); |
619 |
|
|
bb.innerHTML = '0%'; |
620 |
|
|
iwfShow(_iwfGetBusyBar(false)); |
621 |
dpavlin |
46 |
} else { |
622 |
|
|
// use default busy implementation... |
623 |
|
|
window.status = 'busy.'; |
624 |
|
|
} |
625 |
|
|
if (!_iwfRequestTickDuration){ |
626 |
|
|
_iwfRequestTickDuration = 100; |
627 |
|
|
} |
628 |
|
|
_iwfRequestTicker = setInterval(_iwfOnRequestTick, _iwfRequestTickDuration); |
629 |
|
|
} |
630 |
|
|
} |
631 |
|
|
|
632 |
|
|
function _iwfOnRequestTick(){ |
633 |
|
|
_iwfRequestTickCount++; |
634 |
|
|
if (window.iwfOnRequestTick){ |
635 |
|
|
iwfOnRequestTick(_iwfRequestTickCount, _iwfRequestTickDuration, _iwfPendingRequests); |
636 |
|
|
} else if (!window.iwfOnRequestStart) { |
637 |
dpavlin |
55 |
if (_iwfShowGuiProgress) { |
638 |
|
|
// use gui busy implementation |
639 |
|
|
var bar = _iwfGetBusyBar(true); |
640 |
|
|
if(bar){ |
641 |
|
|
var w = iwfWidth(bar) + 1; |
642 |
|
|
if (w > 95){ |
643 |
|
|
w = 95; |
644 |
|
|
} |
645 |
|
|
iwfWidth(bar, w); |
646 |
|
|
bar.innerHTML = "loading " + iwfIntFormat(w) + "%"; |
647 |
|
|
} |
648 |
|
|
} else { |
649 |
|
|
// use default busy implementation... |
650 |
|
|
window.status = 'busy...............................................'.substr(0, (_iwfRequestTickCount % 45) + 5); |
651 |
|
|
} |
652 |
dpavlin |
46 |
} else { |
653 |
|
|
// they didn't define a tick function, |
654 |
|
|
// but they did define a start one, so do nothing. |
655 |
|
|
} |
656 |
|
|
} |
657 |
|
|
|
658 |
|
|
function _iwfOnRequestEnd(){ |
659 |
|
|
_iwfPendingRequests--; |
660 |
|
|
if (_iwfPendingRequests < 1){ |
661 |
|
|
_iwfPendingRequests = 0; |
662 |
|
|
_iwfTotalRequests = 0; |
663 |
|
|
clearInterval(_iwfRequestTicker); |
664 |
|
|
_iwfRequestTicker = null; |
665 |
|
|
if (window.iwfOnRequestEnd){ |
666 |
|
|
iwfOnRequestEnd(); |
667 |
|
|
} else if (!window.iwfOnRequestStart) { |
668 |
dpavlin |
55 |
if (_iwfShowGuiProgress) { |
669 |
|
|
// use gui busy implementation |
670 |
|
|
var bar = _iwfGetBusyBar(true); |
671 |
|
|
if(bar){ |
672 |
|
|
iwfWidth(bar, 100); |
673 |
|
|
bar.innerHTML = "Done"; |
674 |
|
|
iwfHideGentlyDelay(_iwfGetBusyBar(false), 15, 500); |
675 |
|
|
} |
676 |
|
|
} else { |
677 |
|
|
// use default busy implementation... |
678 |
|
|
window.status = 'done.'; |
679 |
|
|
} |
680 |
dpavlin |
46 |
} else { |
681 |
|
|
// they didn't define an end function, |
682 |
|
|
// but they did define a start one, so do nothing. |
683 |
|
|
} |
684 |
|
|
|
685 |
|
|
} else { |
686 |
|
|
if (window.iwfOnRequestProgress){ |
687 |
|
|
iwfOnRequestProgress(_iwfPendingRequests, _iwfTotalRequests); |
688 |
|
|
} else if (!window.iwfOnRequestStart) { |
689 |
dpavlin |
55 |
if (_iwfShowGuiProgress) { |
690 |
|
|
// use gui busy implementation |
691 |
|
|
var pct = (1 - (_iwfPendingRequests/_iwfTotalRequests)) * 100; |
692 |
|
|
if (pct > 100){ |
693 |
|
|
pct = 100; |
694 |
|
|
} |
695 |
|
|
var bar = _iwfGetBusyBar(true); |
696 |
|
|
if(bar){ |
697 |
|
|
iwfWidth(bar, pct); |
698 |
|
|
bar.innerHTML = "loading " + iwfIntFormat(pct) + "%"; |
699 |
|
|
} |
700 |
|
|
} else { |
701 |
|
|
// use default busy implementation... |
702 |
dpavlin |
56 |
window.status = 'Remaining: ' + _iwfPendingRequests; |
703 |
dpavlin |
55 |
} |
704 |
dpavlin |
46 |
} else { |
705 |
|
|
// they didn't define an end function, |
706 |
|
|
// but they did define a start one, so do nothing. |
707 |
|
|
} |
708 |
|
|
} |
709 |
|
|
} |
710 |
|
|
|
711 |
|
|
function _iwfOnRequestError(code, msg, text){ |
712 |
|
|
iwfLog("Error " + code + ": " + msg + ":\n\n" + text); |
713 |
|
|
if (window.iwfOnRequestError){ |
714 |
|
|
iwfOnRequestError(code, msg, text); |
715 |
|
|
} else { |
716 |
|
|
alert("Error " + code + ": " + msg + ":\n\n" + text); |
717 |
|
|
} |
718 |
|
|
} |
719 |
|
|
|
720 |
|
|
// ----------------------------------- |
721 |
|
|
// End: AJAX Request and Response |
722 |
dpavlin |
48 |
// ----------------------------------- |