OSDN Git Service

FIX:変数名の誤記を修正
[nucleus-jp/nucleus-next.git] / skins / admin / bookmarklet / javascripts / 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   * $Id: xmlhttprequest.js 1388 2009-07-18 06:31:28Z shizuki $
43   */
44
45 /**
46  * Creates the xmlHTTPRequest handler
47  */
48 function createHTTPHandler() {
49         var httphandler = false;
50         /*@cc_on @*/
51         /*@if (@_jscript_version >= 5)
52                 // JScript gives us Conditional compilation, we can cope with old IE versions.
53                 // and security blocked creation of the objects.
54                 try {
55                         httphandler = new ActiveXObject("Msxml2.XMLHTTP");
56                 }
57                 catch (e) {
58                         try {
59                                 httphandler = new ActiveXObject("Microsoft.XMLHTTP");
60                         }
61                         catch (E) {
62                                 httphandler = false;
63                         }
64                 }
65         @end @*/
66         if (!httphandler && typeof XMLHttpRequest != 'undefined') {
67                 httphandler = new XMLHttpRequest();
68         }
69         return httphandler;
70 }
71
72 /**
73  * Auto saves as draft
74  */
75 function autoSaveDraft() {
76         checks = 0;
77         seconds = now();
78
79         var title = encodeURIComponent(addform.title.value);
80         var body = encodeURIComponent(addform.body.value);
81         var catid = addform.catid.options[addform.catid.selectedIndex].value;
82         var more = encodeURIComponent(addform.more.value);
83         var closed = 0;
84         if (addform.closed[0].checked) {
85                 closed = addform.closed[0].value;
86         }
87         else if (addform.closed[1].checked) {
88                 closed = addform.closed[1].value;
89         }
90         var ticket = addform.ticket.value;
91
92         var querystring = 'action=autodraft';
93         querystring += '&title=' + title;
94         querystring += '&body=' + body;
95         querystring += '&catid=' + catid;
96         querystring += '&more=' + more;
97         querystring += '&closed=' + closed;
98         querystring += '&ticket=' + ticket;
99         if (formtype == 'edit') {
100                 querystring += '&itemid=' + addform.itemid.value;
101                 querystring += '&type=edit';
102         }
103         else {
104                 querystring += '&blogid=' + addform.blogid.value;
105                 querystring += '&type=add';
106         }
107         if (addform.draftid.value > 0) {
108                 querystring += '&draftid=' + addform.draftid.value;
109         }
110
111         xmlhttprequest[0].open('POST', goalurl, true);
112         xmlhttprequest[0].onreadystatechange = checkMonitor;
113         xmlhttprequest[0].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
114         xmlhttprequest[0].send(querystring);
115
116         var querystring = 'action=updateticket&ticket=' + ticket;
117
118         xmlhttprequest[1].open('POST', goalurl, true);
119         xmlhttprequest[1].onreadystatechange = updateTicket;
120         xmlhttprequest[1].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
121         xmlhttprequest[1].send(querystring);
122 }
123
124 /**
125  * Monitors the edits
126  */
127 function doMonitor() {
128         if (checks * (now() - seconds) > 120 * 1000 * 50) {
129                 autoSaveDraft();
130         }
131         else {
132                 checks++;
133         }
134 }
135
136 /**
137  * Checks the process of the saving
138  */
139 function checkMonitor() {
140         if (xmlhttprequest[0].readyState == 4) {
141                 if (xmlhttprequest[0].responseText) {
142                         if (xmlhttprequest[0].responseText.substr(0, 4) == 'err:') {
143                                 goal.innerHTML = xmlhttprequest[0].responseText.substr(4) + ' (' + formattedDate() + ')';
144                         }
145                         else {
146                                 addform.draftid.value = xmlhttprequest[0].responseText;
147                                 goal.innerHTML = lastsavedtext + ' ' + formattedDate();
148                         }
149                 }
150         }
151 }
152
153 /**
154  * Checks the process of the ticket updating
155  */
156 function updateTicket() {
157         if (xmlhttprequest[1].readyState == 4) {
158                 if (xmlhttprequest[1].responseText) {
159                         if (xmlhttprequest[1].responseText.substr(0, 4) == 'err:') {
160                                 goal.innerHTML = xmlhttprequest[1].responseText.substr(4) + ' (' + formattedDate() + ')';
161                         }
162                         else {
163                                 addform.ticket.value = xmlhttprequest[1].responseText;
164                         }
165                 }
166         }
167 }
168
169 /**
170  * Gets now in milliseconds
171  */
172 function now() {
173         var now = new Date();
174         return now.getTime();
175 }
176
177 /**
178  * Gets now in the local dateformat
179  */
180 function formattedDate() {
181         var now = new Date();
182         return now.toLocaleDateString() + ' ' + now.toLocaleTimeString();
183 }