// ajax.js // version 0.1 /* Copyright (C) 2008 Massimiliano Ortu www.3do.it find email... This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /////////////////////////////////////////// ////////////////////////////////////////// ////////////////////////////////////////// function timeoutFired(e){ alert(e); } var ajax_manager = {} /////////////////////////////////////////////// ajax_manager.request_handler = "server.php"; ajax_manager.use_history = true; ////////////////////////////////////////////// ajax_manager.start_history = function() { window.dhtmlHistory.create(); ajax_manager.historyListener = function(newLocation, historyData) { if (newLocation) { if (historyData['type'] == 'page') { ajax_manager.loadPage(historyData['parent'], historyData['page'], false); } else if (historyData['type'] == 'function') { ajax_manager.send_request(historyData['value'], historyData['params'], historyData['callback'], false, historyData['server']); } } } //window.onload = function() { dhtmlHistory.initialize(); dhtmlHistory.addListener(ajax_manager.historyListener); //}; } ajax_manager.__current_request = []; ajax_manager._parent = ''; ajax_manager._files_to_load = {}; ajax_manager._files_needed = {}; ajax_manager._files_loaded = []; ajax_manager._current_page = ''; ajax_manager._page_data = ''; // perform the XMLHttpRequest(); ajax_manager.send_request = function (func, args, callback, history, server) { //ajax_manager.abort_pending_request(); if (history == undefined) { history = ajax_manager.use_history; } if (server == undefined) { server = ajax_manager.request_handler; } if ( history && dhtmlHistory.isCreated){ var t = new Date(); var value = t.getTime(); dhtmlHistory.add(value, {'type':'function', 'params' : args, 'callback': callback, 'value': func, 'server': server }); } //reference our arguments //document.body.style.cursor = "wait"; var t = document.createElement("t"); t.setAttribute("id", "waiting_layer"); var w = (document.body.scrollWidth/2) - 160; var h = (document.body.scrollHeight/2) - 320; t.innerHTML = '
Loading...
'; document.body.appendChild(t); var url = ajax_manager.request_handler; url = server + "?timeStamp=" + new Date().getTime(); var xmlhttp = ( window.XMLHttpRequest ) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ; xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4) { try { ajax_manager.removeItem(ajax_manager.__current_request, xmlhttp) if( callback )callback( JSON.parse(xmlhttp.responseText) ); // xmlhttp.responseText.parseJSON() }catch (e) { } var el = document.getElementById("waiting_layer"); if (el) document.body.removeChild(el); } } xmlhttp.open( "POST" , url , true); xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/plain;charset=UTF-8"); //xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); var request = {}; request["func"] = func; request["args"] = args; //console.log( JSON.stringify( request ) ); //ajax_manager.__current_request.push(xmlhttp); //xmlhttp.send(request.toJSONString()); var post_str = JSON.stringify( request ); //xmlhttp.setRequestHeader("Content-length", post_str.length); //xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send( post_str ); } ajax_manager.remove_script = function(id) { if (id) { ajax_manager._files_loaded[id] = false; var obj = document.getElementById(id); if (obj) { document.getElementsByTagName("head").item(0).removeChild(obj); } } } ajax_manager.request_script = function(url) { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } if (req != undefined) { //req.onreadystatechange = function() { attach_script(url);}; ajax_manager.__current_request.push(req); req.open("GET", url, false); req.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); req.send(null); if(req.status == 200) { var fileref = document.createElement("script"); fileref.id = url; fileref.type = 'text/javascript'; fileref.text = req.responseText; document.getElementsByTagName("head")[0].appendChild(fileref); ajax_manager.script_loaded(url); } } } ajax_manager.request_page = function(url, callback) { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } if (req != undefined) { ajax_manager.__current_request.push(req); //req.onreadystatechange = function() { attach_script(url);}; req.onreadystatechange = function(){ if(req.readyState == 4) { callback(req.responseText); //script_loaded(url); } } req.open("GET", url, true); //var requestTimer = setTimeout(function() {req.abort(); clearTimeout(requestTimer);}, 100); req.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); req.send(null); //if (req.status == 200 ) { // callback(req.responseText); //} //clearTimeout(requestTimer); } } ajax_manager.request_css = function(url) { var fileref=document.createElement("link"); fileref.setAttribute("id", url); fileref.setAttribute("rel", "stylesheet"); fileref.setAttribute("type", "text/css"); fileref.setAttribute("href", url); //fileref.onload = script_loaded(url); document.getElementsByTagName("head")[0].appendChild(fileref); ajax_manager._files_to_load[url] = 2; ajax_manager.display_page(); } ajax_manager.loadScript = function(urls) { var i; var css = []; var js = []; for(i=0; i