OSDN Git Service

e429400e39be0c86616dd76da1ffef4200a5200b
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / javascript / edit.js
1 /**
2   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
3   * Copyright (C) 2002-2010 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   * This file contains functions to allow adding items from inside the weblog.
12   * Also contains code to avoid submitting form data twice.
13   *
14   * $Id$
15   * $NucleusJP: edit.js,v 1.5 2006/07/12 07:11:47 kimitake Exp $
16   */
17
18 var nucleusConvertBreaks = true;
19 var nucleusMediaPopupURL = '';
20 var nucleusMediaURL = 'media/';
21 var nucleusAuthorId = 0;
22 var scrollTop = -1;
23
24 function setConvertBreaks(newval) {     nucleusConvertBreaks = newval; }
25 function setMediaUrl(url) { nucleusMediaURL = url; }
26 function setAuthorId(id) { nucleusAuthorId = id; }
27
28 function preview(id, value) {
29         elem = document.getElementById(id);
30         if (!elem) return;
31
32         var preview = nucleusConvertBreaks ? str_replace("\n","<br />",value)+"&#160;" : value+"&#160;";
33
34         // expand the media commands (without explicit collection)
35         preview = preview.replace(/\<\%image\(([^\/\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<img src='"+nucleusMediaURL+nucleusAuthorId+"/$1' width='$2' height='$3' alt=\"$4\" />");
36
37         // expand the media commands (with collection)
38         preview = preview.replace(/\<\%image\(([^\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<img src='"+nucleusMediaURL+"$1' width='$2' height='$3' alt=\"$4\" />");
39         preview = preview.replace(/\<\%popup\(([^\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<a href='' onclick='if (event &amp;&amp; event.preventDefault) event.preventDefault(); alert(\"popup image\"); return false;' title='popup'>$4</a>");
40         preview = preview.replace(/\<\%media\(([^\|]*)\|([^)]*)\)\%\>/g,"<a href='' title='media link'>$2</a>");
41
42         elem.innerHTML = preview;
43 }
44
45 function showedit() {
46         prevval = document.getElementById('edit').style.display;
47         if (prevval == "block")
48                 newval = "none";
49         else
50                 newval = "block";
51         document.getElementById('edit').style.display = newval;
52
53         if (newval == "block")
54                 updAllPreviews();
55 }
56
57 function updAllPreviews() {
58         updPreview('title');
59         updPreview('body');
60         updPreview('more');
61 }
62
63 function isEditVisible() {
64         var editform = document.getElementById('edit');
65         if (!editform) return true;
66         var prevval = editform.style.display;
67         return (prevval == "none") ? false : true;
68 }
69
70 function updPreview(id) {
71         // don't update when preview is hidden
72         if (!isEditVisible()) return;
73
74         var inputField = document.getElementById('input' + id);
75         if (!inputField) return;
76         preview('prev' + id, inputField.value);
77 }
78
79 // replace a in s by b (taken from milov.nl)
80 function str_replace(a, b, s)
81 {
82         if (a == b || !s.length || !a.length) return s;
83         if ((p=s.indexOf(a)) == -1) { return s; }
84         else { ns = s.substring(0,p) + b + s.substring(p+a.length,s.length); }
85         return (s.indexOf(a) != -1) ? str_replace(a, b, ns) : ns;
86 }
87
88 function shortCuts() {
89         if (!event || (event.ctrlKey != true)) return;
90
91         switch (event.keyCode) {
92                 case 1:
93                         ahrefThis(); break; // ctrl-shift-a
94                 case 2:
95                         boldThis(); break; // ctrl-shift-b
96                 case 9:
97                         italicThis(); break; // ctrl-shift-i
98                 case 13:
99                         addMedia(); break; // ctrl-shift-m
100                 default:
101                         return;
102         }
103         return;
104 }
105
106 function cutThis() { execAndUpdate('cut'); }
107 function copyThis() { execAndUpdate('copy'); }
108 function pasteThis() { execAndUpdate('paste'); }
109 function boldThis() { insertAroundCaret('<b>','</b>'); }
110 function italicThis() { insertAroundCaret('<i>','</i>'); }
111 function leftThis() { insertAroundCaret('<div class="leftbox">','</div>'); }
112 function rightThis() { insertAroundCaret('<div class="rightbox">','</div>'); }
113 function alignleftThis() { insertAroundCaret('<div style="text-align: left">','</div>'); }
114 function alignrightThis() { insertAroundCaret('<div style="text-align: right">','</div>'); }
115 function aligncenterThis() { insertAroundCaret('<div style="text-align: center">','</div>'); }
116
117
118 function ahrefThis() {
119         if (document.selection)
120                 strSelection = document.selection.createRange().text;
121         else
122                 strSelection = '';
123
124         strHref = prompt("Create a link to:","http://");
125         if (strHref == null) return;
126
127         var textpre = "<a href=\"" + strHref.replace(/&/g,'&amp;') + "\">";
128         insertAroundCaret(textpre, "</a>");
129 }
130
131 function execAndUpdate(action) {
132         lastSelected.caretPos.execCommand(action);
133         updAllPreviews();
134 }
135
136
137 var nonie_FormType = 'body';
138
139 // Add media to new item
140 function addMedia() {
141
142         var mediapopup = window.open(nucleusMediaPopupURL + 'media.php','name',
143                 'status=yes,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,top=0,left=0');
144
145         return;
146 }
147
148
149 function setMediaPopupURL(url) {
150         nucleusMediaPopupURL = url;
151 }
152
153 function includeImage(collection, filename, type, width, height) {
154         if (isCaretEmpty()) {
155                 text = prompt("Text to display ?", filename);
156         } else {
157                 text = getCaretText();
158         }
159
160         // add collection name when not private collection (or editing a message that's not your)
161         var fullName;
162         if (isNaN(collection) || (nucleusAuthorId != collection)) {
163                 fullName = collection + '/' + filename;
164         } else {
165                 fullName = filename;
166         }
167
168
169         var replaceBy;
170         switch(type) {
171                 case 'popup':
172                         replaceBy = '<%popup(' +  fullName + '|'+width+'|'+height+'|' + text +')%>';
173                         break;
174                 case 'inline':
175                 default:
176                         replaceBy = '<%image(' +  fullName + '|'+width+'|'+height+'|' + text +')%>';
177         }
178
179         insertAtCaret(replaceBy);
180         updAllPreviews();
181
182 }
183
184
185 function includeOtherMedia(collection, filename) {
186         if (isCaretEmpty()) {
187                 text = prompt("Text to display ?", filename);
188         } else {
189                 text = getCaretText();
190         }
191
192         // add collection name when not private collection (or editing a message that's not your)
193         var fullName;
194         if (isNaN(collection) || (nucleusAuthorId != collection)) {
195                 fullName = collection + '/' + filename;
196         } else {
197                 fullName = filename;
198         }
199
200         var replaceBy = '<%media(' +  fullName + '|' + text +')%>';
201
202         insertAtCaret(replaceBy);
203         updAllPreviews();
204 }
205
206
207
208 // function to prevent submitting form data twice
209 var submitcount=0;
210 function checkSubmit() {
211         if (submitcount == 0) {
212                 submitcount++;
213                 return true;
214         } else {
215                 return false;
216         }
217 }
218
219
220 // code to store the caret (cursor) position of a text field/text area
221 // taken from javascript.faqts and modified
222 // http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
223
224 // stores the caret
225 var lastSelected, lastCaretPos;
226 function storeCaret (textEl) {
227
228         // store caret
229         if (textEl.createTextRange) 
230                 //textEl.caretPos = document.selection.createRange().duplicate();
231                 lastCaretPos = document.selection.createRange().duplicate();
232
233         // also store lastselectedelement
234         lastSelected = textEl;
235         
236         nonie_FormType = textEl.name;
237
238         scrollTop = textEl.scrollTop;
239 }
240
241 //var lastSelected;
242
243 // inserts text at caret (overwriting selection)
244 function insertAtCaret (text) {
245         var textEl = lastSelected;
246         if (textEl && textEl.createTextRange && lastCaretPos) {
247                 var caretPos = lastCaretPos;
248                 caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
249         } else if (!document.all && document.getElementById) {
250                 mozReplace(document.getElementById('input' + nonie_FormType), text);                            
251                 if(scrollTop>-1) {
252                         document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
253                 }
254         } else if (textEl) {
255                 textEl.value  += text;
256         } else {
257                 document.getElementById('input' + nonie_FormType).value += text;                
258                 if(scrollTop>-1) {
259                         document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
260                 }
261         }
262         updAllPreviews();
263 }
264
265 // inserts a tag around the selected text
266 function insertAroundCaret (textpre, textpost) {
267         var textEl = lastSelected;
268         
269         if (textEl && textEl.createTextRange && lastCaretPos) {
270                 var caretPos = lastCaretPos;
271                 caretPos.text = textpre + caretPos.text + textpost;
272         } else if (!document.all && document.getElementById) {
273                 mozWrap(document.getElementById('input' + nonie_FormType), textpre, textpost);          
274                 if(scrollTop>-1) {
275                         document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
276                 }
277         } else {
278                 document.getElementById('input' + nonie_FormType).value += textpre + textpost;
279                 if(scrollTop>-1) {
280                         document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
281                 }
282         }
283
284         updAllPreviews();
285 }
286
287 /* some methods to get things working in Mozilla as well */
288 function mozWrap(txtarea, lft, rgt) {
289         var selLength = txtarea.textLength;
290         var selStart = txtarea.selectionStart;
291         var selEnd = txtarea.selectionEnd;
292         if (selEnd==1 || selEnd==2) selEnd=selLength;
293         var s1 = (txtarea.value).substring(0,selStart);
294         var s2 = (txtarea.value).substring(selStart, selEnd)
295         var s3 = (txtarea.value).substring(selEnd, selLength);
296         txtarea.value = s1 + lft + s2 + rgt + s3;
297 }
298 function mozReplace(txtarea, newText) {
299         var selLength = txtarea.textLength;
300         var selStart = txtarea.selectionStart;
301         var selEnd = txtarea.selectionEnd;
302         if (selEnd==1 || selEnd==2) selEnd=selLength;
303         var s1 = (txtarea.value).substring(0,selStart);
304         var s2 = (txtarea.value).substring(selStart, selEnd)
305         var s3 = (txtarea.value).substring(selEnd, selLength);
306         txtarea.value = s1 + newText + s3;
307 }
308 function mozSelectedText() {
309         var txtarea = document.getElementById('input' + nonie_FormType);
310         var selLength = txtarea.textLength;
311         var selStart = txtarea.selectionStart;
312         var selEnd = txtarea.selectionEnd;
313         if (selEnd==1 || selEnd==2) selEnd=selLength;
314         return (txtarea.value).substring(selStart, selEnd);
315 }
316
317 function getCaretText() {
318         if (!document.all && document.getElementById)
319                 return mozSelectedText();
320         else
321                 return lastCaretPos.text;
322 }
323
324 function isCaretEmpty() {
325         if (lastSelected && lastSelected.createTextRange && lastCaretPos)
326                 return (lastCaretPos.text == '');
327         else if (!document.all && document.getElementById)
328                 return (mozSelectedText() == '');
329         else
330                 return true;
331 }
332
333 function BtnHighlight(el) {
334         with(el.style){
335                 borderLeft="1px solid gray";
336                 borderRight="1px solid #e9e9e9";
337                 borderTop="1px solid gray";
338                 borderBottom="1px solid #e9e9e9";
339         }
340 }
341
342 function BtnNormal(el) {
343         with(el.style){
344                 padding="3px";
345                 border="1px solid #dddddd";
346         }
347 }
348