// © 2014 Daniel Schulz function Ajax(obj, file, postData, func) { var req, formData, method, key; if (postData) { method = "POST"; formData = new FormData(); for (key in postData) { formData.append(key, postData[key]); } } else { method = "GET"; formData = false; } req = new XMLHttpRequest(); req._callbackContext = obj; req._callbackFunc = func; req.open("POST", file, true); req.addEventListener( "load", function(eve) { var data; if (this._callbackFunc) { //alert(this.getResponseHeader("Content-Type")); switch (this.getResponseHeader("Content-Type")) { case "application/json": data = JSON.parse(this.responseText); break; default: data = this.responseText; break; } return this._callbackFunc.call(this._callbackContext, data); } else { return true; } }, false ); if (formData) { req.send(formData); } else { req.send(); } /* var http; if (window.XMLHttpRequest) { http = new XMLHttpRequest(); } else { if (window.ActiveXObject) { http = new ActiveXObject("Microsoft.XMLHTTP"); } } if (http) { if (postData) { http.open("POST", file, true); } else { http.open("GET", file, true); } http.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); http.onreadystatechange = function() { var data; if (http.readyState == 4) { if (func) { //alert(http.getResponseHeader("Content-Type")); switch (http.getResponseHeader("Content-Type")) { case "application/json": data = JSON.parse(http.responseText); break; default: data = http.responseText; break; } return func.call(obj, data); } else return true; } return false; }; var str = []; for (var key in postData) { str.push(key + "=" + postData[key]); } str = str.join("&"); http.send(str); } //*/ } /******************************************************************************* * DOM v1.01 08.04.2014 © Daniel Schulz * * Changelog: * v1.01 08.04.2014 * console.log * v1.00 12.09.2012 * initial release ******************************************************************************/ var DOM = { logActivity : true, documentCache : {}, loadDocument : function(uri, callback) { var retDoc, req, fileURI, sync; try { fileURI = uri; sync = !callback; if (uri.indexOf("?") === -1) { //uri += "?"; } if (this.documentCache[fileURI]) { retDoc = this.documentCache[fileURI]; if (this.logActivity) { console.log("DOM.loadDocument served from internal cache! 0ms - %o", fileURI); } if (callback) { callback.call(window); } } else { req = new XMLHttpRequest(); req.open("GET", uri, !sync); req.setRequestHeader("Accept", "application/xslt+xml,application/xhtml+xml,application/xml,*/*"); req.setRequestHeader("Cache-Control", "max-age=0, must-revalidate"); req._domParam = { dom : this, fileURI : fileURI, callback : callback, startTime : new Date(), }; if (sync) { req.send(); retDoc = this._loadDocumentAnswer(req); } else { req.addEventListener( "load", function(eve) { this._domParam.dom._loadDocumentAnswer(this); }, false ); req.send(); retDoc = null; } } } catch(e) { console.log("Could not load XML ressource: %o", uri); console.log(" Exception: %o", e); retDoc = false; } return retDoc; }, _loadDocumentAnswer : function(req) { var retDoc, startTime, endTime, timeDiff; startTime = req._domParam.startTime; endTime = new Date(); timeDiff = endTime - startTime; retDoc = req.responseXML; if (!retDoc) { throw req.responseText; } retDoc.fileURI = req._domParam.fileURI; if (this.logActivity) { if (timeDiff < 25) { console.log("DOM.loadDocument served from browser cache! %oms - %o", timeDiff, retDoc.fileURI); } else { console.log("DOM.loadDocument served from server! %oms - %o", timeDiff, retDoc.fileURI); } } this.documentCache[retDoc.fileURI] = retDoc; if (this.logActivity) { console.log(retDoc); } if (req._domParam.callback) { req._domParam.callback.call(window); } return retDoc; }, saveDocument : function(uri, doc) { var retDoc, req; try { req = new XMLHttpRequest(); req.open("POST", uri, false); //req.setRequestHeader("Content-Type", "application/xml"); req.send(doc); retDoc = req.responseXML; //retDoc = this.sanitizeDocument(retDoc); if (this.logActivity) { console.log("DOM.saveDocument: %o", uri); console.log(req.responseText); } } catch(e) { console.log("Could not save XML ressource: %o", uri); console.log(" Exception: %o", e); retDoc = false; } return retDoc; }, jsonCache : {}, loadJSON : function(uri) { var retObj, req; var startTime, endTime, timeDiff; try { if (uri.indexOf("?") === -1) { uri += "?"; } if (this.jsonCache[uri]) { retObj = this.jsonCache[uri]; if (this.logActivity) { console.log("DOM.loadJSON served from internal cache! 0ms - %o", uri); } } else { req = new XMLHttpRequest(); req.open("GET", uri, false); req.setRequestHeader("Accept", "application/xslt+xml,application/xhtml+xml,application/xml,*/*"); req.setRequestHeader("Cache-Control", "max-age=0, must-revalidate"); startTime = new Date(); req.send(); endTime = new Date(); timeDiff = endTime - startTime; retObj = JSON.parse(req.responseText); if (!retObj) { throw req.responseText; } if (this.logActivity) { if (timeDiff < 25) { console.log("DOM.loadJSON served from browser cache! %oms - %o", timeDiff, uri); } else { console.log("DOM.loadJSON served from server! %oms - %o", timeDiff, uri); } } this.jsonCache[uri] = retObj; } } catch(e) { console.log("Could not load XML ressource: %o", uri); console.log(" Exception: %o", e); retObj = false; } if (this.logActivity) { console.log(retObj); } return retObj; }, loadXML : function(xml) { var doc, parser; parser = new DOMParser(); doc = parser.parseFromString(xml, "application/xml"); if (doc.documentElement.namespaceURI === NS.MOZ_ERR_PARSE) { throw ""+doc.documentElement.textContent; } //doc = this.sanitizeDocument(doc); return doc; }, saveXML : function(doc) { var xml, serializer; serializer = new XMLSerializer(); xml = serializer.serializeToString(doc); return xml; }, //makes sure doc is an XPath-compatible Document sanitizeDocument : function(dataDoc) { if (!dataDoc || dataDoc.evaluate) { return dataDoc; } var retDoc; retDoc = document.cloneNode(false); while (retDoc.hasChildNodes()) { retDoc.removeChild(retDoc.lastChild); } for (var i = 0; i < dataDoc.childNodes.length; i++) { retDoc.appendChild(retDoc.importNode(dataDoc.childNodes[i], true)); } return retDoc; }, }; // htmlArea v3.0 - Copyright (c) 2003-2004 interactivetools.com, inc. // This copyright notice MUST stay intact for use (see license.txt). // // Portions (c) dynarch.com, 2003-2004 // // A free WYSIWYG editor replacement for