2 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
3 * Copyright (C) 2002-2012 The Nucleus Group
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
9 * (see nucleus/documentation/index.html#license for more info)
\r
12 * This page contains xmlHTTPRequest functions for:
\r
17 * - Add in the page before the form open tag:
\r
18 * <script type="text/javascript" src="javascript/xmlhttprequest.js"></script>
\r
19 * - Add in the page behind the form close tag:
\r
20 * var xmlhttprequest = new Array();
\r
21 * xmlhttprequest[0] = createHTTPHandler(); // AutoDraft handler
\r
22 * xmlhttprequest[1] = createHTTPHandler(); // UpdateTicket handler
\r
23 * var seconds = now(); // Last AutoDraft time
\r
24 * var checks = 0; // Number of checks since last AutoDraft
\r
25 * var addform = document.getElementById('addform'); // The form id
\r
26 * var goal = document.getElementById('lastsaved'); // The html div id where 'Last saved: date time' must come
\r
27 * var goalurl = 'action.php'; // The PHP file where the content must be posted to (action.php)
\r
28 * var lastsavedtext = 'Last saved'; // The language variable for 'Last saved'
\r
29 * var formtype = 'add'; // Add or edit form
\r
30 * - Add to the form tag:
\r
32 * - Add to the textarea's and text fields:
\r
33 * onkeyup="doMonitor();"
\r
34 * - Add tot the selectboxes and radio buttons
\r
35 * onchange="doMonitor();"
\r
36 * - Add to the form:
\r
37 * <input type="hidden" name="draftid" value="0" />
\r
38 * - Optionally a autosave now button can be add:
\r
39 * <input type="button" name="autosavenow" value="AutoSave now" onclick="autoSaveDraft();" />
\r
42 * $Id: xmlhttprequest.js 1388 2009-07-18 06:31:28Z shizuki $
\r
46 * Creates the xmlHTTPRequest handler
\r
48 function createHTTPHandler() {
\r
49 var httphandler = false;
\r
51 /*@if (@_jscript_version >= 5)
\r
52 // JScript gives us Conditional compilation, we can cope with old IE versions.
\r
53 // and security blocked creation of the objects.
\r
55 httphandler = new ActiveXObject("Msxml2.XMLHTTP");
\r
59 httphandler = new ActiveXObject("Microsoft.XMLHTTP");
\r
62 httphandler = false;
\r
66 if (!httphandler && typeof XMLHttpRequest != 'undefined') {
\r
67 httphandler = new XMLHttpRequest();
\r
73 * Auto saves as draft
\r
75 function autoSaveDraft() {
\r
79 var title = encodeURIComponen(addform.title.value);
\r
80 var body = encodeURIComponen(addform.body.value);
\r
81 var catid = addform.catid.options[addform.catid.selectedIndex].value;
\r
82 var more = encodeURIComponen(addform.more.value);
\r
84 if (addform.closed[0].checked) {
\r
85 closed = addform.closed[0].value;
\r
87 else if (addform.closed[1].checked) {
\r
88 closed = addform.closed[1].value;
\r
90 var ticket = addform.ticket.value;
\r
92 var querystring = 'action=autodraft';
\r
93 querystring += '&title=' + title;
\r
94 querystring += '&body=' + body;
\r
95 querystring += '&catid=' + catid;
\r
96 querystring += '&more=' + more;
\r
97 querystring += '&closed=' + closed;
\r
98 querystring += '&ticket=' + ticket;
\r
99 if (formtype == 'edit') {
\r
100 querystring += '&itemid=' + addform.itemid.value;
\r
101 querystring += '&type=edit';
\r
104 querystring += '&blogid=' + addform.blogid.value;
\r
105 querystring += '&type=add';
\r
107 if (addform.draftid.value > 0) {
\r
108 querystring += '&draftid=' + addform.draftid.value;
\r
111 xmlhttprequest[0].open('POST', goalurl, true);
\r
112 xmlhttprequest[0].onreadystatechange = checkMonitor;
\r
113 xmlhttprequest[0].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
\r
114 xmlhttprequest[0].send(querystring);
\r
116 var querystring = 'action=updateticket&ticket=' + ticket;
\r
118 xmlhttprequest[1].open('POST', goalurl, true);
\r
119 xmlhttprequest[1].onreadystatechange = updateTicket;
\r
120 xmlhttprequest[1].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
\r
121 xmlhttprequest[1].send(querystring);
\r
125 * Monitors the edits
\r
127 function doMonitor() {
\r
128 if (checks * (now() - seconds) > 120 * 1000 * 50) {
\r
137 * Checks the process of the saving
\r
139 function checkMonitor() {
\r
140 if (xmlhttprequest[0].readyState == 4) {
\r
141 if (xmlhttprequest[0].responseText) {
\r
142 if (xmlhttprequest[0].responseText.substr(0, 4) == 'err:') {
\r
143 goal.innerHTML = xmlhttprequest[0].responseText.substr(4) + ' (' + formattedDate() + ')';
\r
146 addform.draftid.value = xmlhttprequest[0].responseText;
\r
147 goal.innerHTML = lastsavedtext + ' ' + formattedDate();
\r
154 * Checks the process of the ticket updating
\r
156 function updateTicket() {
\r
157 if (xmlhttprequest[1].readyState == 4) {
\r
158 if (xmlhttprequest[1].responseText) {
\r
159 if (xmlhttprequest[1].responseText.substr(0, 4) == 'err:') {
\r
160 goal.innerHTML = xmlhttprequest[1].responseText.substr(4) + ' (' + formattedDate() + ')';
\r
163 addform.ticket.value = xmlhttprequest[1].responseText;
\r
170 * Gets now in milliseconds
\r
173 var now = new Date();
\r
174 return now.getTime();
\r
178 * Gets now in the local dateformat
\r
180 function formattedDate() {
\r
181 var now = new Date();
\r
182 return now.toLocaleDateString() + ' ' + now.toLocaleTimeString();
\r