OSDN Git Service

f0d575a5acd5829caed77be469121b80ecdd0320
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / javascript / xmlhttprequest.js
1 /**
2   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 
3   * Copyright (C) 2002-2012 The Nucleus Group
4   *
5   * This program is free software; you can redistribute it and/or
6   * modify it under the terms of the GNU General Public License
7   * as published by the Free Software Foundation; either version 2
8   * of the License, or (at your option) any later version.
9   * (see nucleus/documentation/index.html#license for more info)
10   *
11   *
12   * This page contains xmlHTTPRequest functions for:
13   * - AutoSaveDraft
14   *
15   *
16   * Usage:
17   * - Add in the page before the form open tag:
18   *     <script type="text/javascript" src="javascript/xmlhttprequest.js"></script>
19   * - Add in the page behind the form close tag:
20   *     var xmlhttprequest = new Array();
21   *     xmlhttprequest[0] = createHTTPHandler(); // AutoDraft handler
22   *     xmlhttprequest[1] = createHTTPHandler(); // UpdateTicket handler
23   *     var seconds = now(); // Last AutoDraft time
24   *     var checks = 0; // Number of checks since last AutoDraft
25   *     var addform = document.getElementById('addform'); // The form id
26   *     var goal = document.getElementById('lastsaved'); // The html div id where 'Last saved: date time' must come
27   *     var goalurl = 'action.php'; // The PHP file where the content must be posted to (action.php)
28   *     var lastsavedtext = 'Last saved'; // The language variable for 'Last saved'
29   *     var formtype = 'add'; // Add or edit form
30   * - Add to the form tag:
31   *     id="addform"
32   * - Add to the textarea's and text fields:
33   *     onkeyup="doMonitor();"
34   * - Add tot the selectboxes and radio buttons
35   *     onchange="doMonitor();"
36   * - Add to the form:
37   *     <input type="hidden" name="draftid" value="0" />
38   * - Optionally a autosave now button can be add:
39   *     <input type="button" name="autosavenow" value="AutoSave now" onclick="autoSaveDraft();" />
40   */
41
42 /**
43  * Creates the xmlHTTPRequest handler
44  */
45 function createHTTPHandler() {
46         var httphandler = false;
47         /*@cc_on @*/
48         /*@if (@_jscript_version >= 5)
49                 // JScript gives us Conditional compilation, we can cope with old IE versions.
50                 // and security blocked creation of the objects.
51                 try {
52                         httphandler = new ActiveXObject("Msxml2.XMLHTTP");
53                 }
54                 catch (e) {
55                         try {
56                                 httphandler = new ActiveXObject("Microsoft.XMLHTTP");
57                         }
58                         catch (E) {
59                                 httphandler = false;
60                         }
61                 }
62         @end @*/
63         if (!httphandler && typeof XMLHttpRequest != 'undefined') {
64                 httphandler = new XMLHttpRequest();
65         }
66         return httphandler;
67 }
68
69 /**
70  * Auto saves as draft
71  */
72 function autoSaveDraft() {
73         checks = 0;
74         seconds = now();
75
76         var title = encodeURI(addform.title.value);
77         var body = encodeURI(addform.body.value);
78         var catid = addform.catid.options[addform.catid.selectedIndex].value;
79         var more = encodeURI(addform.more.value);
80         var closed = 0;
81         if (addform.closed[0].checked) {
82                 closed = addform.closed[0].value;
83         }
84         else if (addform.closed[1].checked) {
85                 closed = addform.closed[1].value;
86         }
87         var ticket = addform.ticket.value;
88
89         var querystring = 'action=autodraft';
90         querystring += '&title=' + title;
91         querystring += '&body=' + body;
92         querystring += '&catid=' + catid;
93         querystring += '&more=' + more;
94         querystring += '&closed=' + closed;
95         querystring += '&ticket=' + ticket;
96         if (formtype == 'edit') {
97                 querystring += '&itemid=' + addform.itemid.value;
98                 querystring += '&type=edit';
99         }
100         else {
101                 querystring += '&blogid=' + addform.blogid.value;
102                 querystring += '&type=add';
103         }
104         if (addform.draftid.value > 0) {
105                 querystring += '&draftid=' + addform.draftid.value;
106         }
107
108         xmlhttprequest[0].open('POST', goalurl, true);
109         xmlhttprequest[0].onreadystatechange = checkMonitor;
110         xmlhttprequest[0].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
111         xmlhttprequest[0].send(querystring);
112
113         var querystring = 'action=updateticket&ticket=' + ticket;
114
115         xmlhttprequest[1].open('POST', goalurl, true);
116         xmlhttprequest[1].onreadystatechange = updateTicket;
117         xmlhttprequest[1].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
118         xmlhttprequest[1].send(querystring);
119 }
120
121 /**
122  * Monitors the edits
123  */
124 function doMonitor() {
125         if (checks * (now() - seconds) > 120 * 1000 * 50) {
126                 autoSaveDraft();
127         }
128         else {
129                 checks++;
130         }
131 }
132
133 /**
134  * Checks the process of the saving
135  */
136 function checkMonitor() {
137         if (xmlhttprequest[0].readyState == 4) {
138                 if (xmlhttprequest[0].responseText) {
139                         if (xmlhttprequest[0].responseText.substr(0, 4) == 'err:') {
140                                 goal.innerHTML = xmlhttprequest[0].responseText.substr(4) + ' (' + formattedDate() + ')';
141                         }
142                         else {
143                                 addform.draftid.value = xmlhttprequest[0].responseText;
144                                 goal.innerHTML = lastsavedtext + ' ' + formattedDate();
145                         }
146                 }
147         }
148 }
149
150 /**
151  * Checks the process of the ticket updating
152  */
153 function updateTicket() {
154         if (xmlhttprequest[1].readyState == 4) {
155                 if (xmlhttprequest[1].responseText) {
156                         if (xmlhttprequest[1].responseText.substr(0, 4) == 'err:') {
157                                 goal.innerHTML = xmlhttprequest[1].responseText.substr(4) + ' (' + formattedDate() + ')';
158                         }
159                         else {
160                                 addform.ticket.value = xmlhttprequest[1].responseText;
161                         }
162                 }
163         }
164 }
165
166 /**
167  * Gets now in milliseconds
168  */
169 function now() {
170         var now = new Date();
171         return now.getTime();
172 }
173
174 /**
175  * Gets now in the local dateformat
176  */
177 function formattedDate() {
178         var now = new Date();
179         return now.toLocaleDateString() + ' ' + now.toLocaleTimeString();
180 }