OSDN Git Service

d8da8b29cde530bd48af331bbeeb435cf3405716
[nucleus-jp/nucleus-next.git] / skins / admin / defaultadmin / javascripts / xmlhttprequest.js
1 /**\r
2   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
3   * Copyright (C) 2002-2012 The Nucleus Group\r
4   *\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
10   *\r
11   *\r
12   * This page contains xmlHTTPRequest functions for:\r
13   * - AutoSaveDraft\r
14   *\r
15   *\r
16   * Usage:\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
31   *     id="addform"\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
40   *\r
41   *\r
42   * $Id: xmlhttprequest.js 1388 2009-07-18 06:31:28Z shizuki $\r
43   */\r
44 \r
45 /**\r
46  * Creates the xmlHTTPRequest handler\r
47  */\r
48 function createHTTPHandler() {\r
49         var httphandler = false;\r
50         /*@cc_on @*/\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
54                 try {\r
55                         httphandler = new ActiveXObject("Msxml2.XMLHTTP");\r
56                 }\r
57                 catch (e) {\r
58                         try {\r
59                                 httphandler = new ActiveXObject("Microsoft.XMLHTTP");\r
60                         }\r
61                         catch (E) {\r
62                                 httphandler = false;\r
63                         }\r
64                 }\r
65         @end @*/\r
66         if (!httphandler && typeof XMLHttpRequest != 'undefined') {\r
67                 httphandler = new XMLHttpRequest();\r
68         }\r
69         return httphandler;\r
70 }\r
71 \r
72 /**\r
73  * Auto saves as draft\r
74  */\r
75 function autoSaveDraft() {\r
76         checks = 0;\r
77         seconds = now();\r
78 \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
83         var closed = 0;\r
84         if (addform.closed[0].checked) {\r
85                 closed = addform.closed[0].value;\r
86         }\r
87         else if (addform.closed[1].checked) {\r
88                 closed = addform.closed[1].value;\r
89         }\r
90         var ticket = addform.ticket.value;\r
91 \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
102         }\r
103         else {\r
104                 querystring += '&blogid=' + addform.blogid.value;\r
105                 querystring += '&type=add';\r
106         }\r
107         if (addform.draftid.value > 0) {\r
108                 querystring += '&draftid=' + addform.draftid.value;\r
109         }\r
110 \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
115 \r
116         var querystring = 'action=updateticket&ticket=' + ticket;\r
117 \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
122 }\r
123 \r
124 /**\r
125  * Monitors the edits\r
126  */\r
127 function doMonitor() {\r
128         if (checks * (now() - seconds) > 120 * 1000 * 50) {\r
129                 autoSaveDraft();\r
130         }\r
131         else {\r
132                 checks++;\r
133         }\r
134 }\r
135 \r
136 /**\r
137  * Checks the process of the saving\r
138  */\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
144                         }\r
145                         else {\r
146                                 addform.draftid.value = xmlhttprequest[0].responseText;\r
147                                 goal.innerHTML = lastsavedtext + ' ' + formattedDate();\r
148                         }\r
149                 }\r
150         }\r
151 }\r
152 \r
153 /**\r
154  * Checks the process of the ticket updating\r
155  */\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
161                         }\r
162                         else {\r
163                                 addform.ticket.value = xmlhttprequest[1].responseText;\r
164                         }\r
165                 }\r
166         }\r
167 }\r
168 \r
169 /**\r
170  * Gets now in milliseconds\r
171  */\r
172 function now() {\r
173         var now = new Date();\r
174         return now.getTime();\r
175 }\r
176 \r
177 /**\r
178  * Gets now in the local dateformat\r
179  */\r
180 function formattedDate() {\r
181         var now = new Date();\r
182         return now.toLocaleDateString() + ' ' + now.toLocaleTimeString();\r
183 }