OSDN Git Service

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