2 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
3 * Copyright (C) 2002-2005 The Nucleus Group
\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
11 * This file contains functions to allow adding items from inside the weblog.
\r
12 * Also contains code to avoid submitting form data twice.
\r
14 * $Id: edit.js,v 1.4 2005-03-23 06:14:53 kimitake Exp $
\r
15 * $NucleusJP: edit.js,v 1.3 2005/03/12 06:19:04 kimitake Exp $
\r
18 var nucleusConvertBreaks = true;
\r
19 var nucleusMediaPopupURL = '';
\r
20 var nucleusMediaURL = 'media/';
\r
21 var nucleusAuthorId = 0;
\r
23 function setConvertBreaks(newval) { nucleusConvertBreaks = newval; }
\r
24 function setMediaUrl(url) { nucleusMediaURL = url; }
\r
25 function setAuthorId(id) { nucleusAuthorId = id; }
\r
27 function preview(id, value) {
\r
28 elem = document.getElementById(id);
\r
31 var preview = nucleusConvertBreaks ? str_replace("\n","<br />",value)+" " : value+" ";
\r
33 // expand the media commands (without explicit collection)
\r
34 preview = preview.replace(/\<\%image\(([^\/\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<img src='"+nucleusMediaURL+nucleusAuthorId+"/$1' width='$2' height='$3' alt=\"$4\" />");
\r
36 // expand the media commands (with collection)
\r
37 preview = preview.replace(/\<\%image\(([^\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<img src='"+nucleusMediaURL+"$1' width='$2' height='$3' alt=\"$4\" />");
\r
38 preview = preview.replace(/\<\%popup\(([^\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<a href='' onclick='if (event && event.preventDefault) event.preventDefault(); alert(\"popup image\"); return false;' title='popup'>$4</a>");
\r
39 preview = preview.replace(/\<\%media\(([^\|]*)\|([^)]*)\)\%\>/g,"<a href='' title='media link'>$2</a>");
\r
41 elem.innerHTML = preview;
\r
44 function showedit() {
\r
45 prevval = document.getElementById('edit').style.display;
\r
46 if (prevval == "block")
\r
50 document.getElementById('edit').style.display = newval;
\r
52 if (newval == "block")
\r
56 function updAllPreviews() {
\r
57 updPreview('title');
\r
62 function isEditVisible() {
\r
63 var editform = document.getElementById('edit');
\r
64 if (!editform) return true;
\r
65 var prevval = editform.style.display;
\r
66 return (prevval == "none") ? false : true;
\r
69 function updPreview(id) {
\r
70 // don't update when preview is hidden
\r
71 if (!isEditVisible()) return;
\r
73 var inputField = document.getElementById('input' + id);
\r
74 if (!inputField) return;
\r
75 preview('prev' + id, inputField.value);
\r
78 // replace a in s by b (taken from milov.nl)
\r
79 function str_replace(a, b, s)
\r
81 if (a == b || !s.length || !a.length) return s;
\r
82 if ((p=s.indexOf(a)) == -1) { return s; }
\r
83 else { ns = s.substring(0,p) + b + s.substring(p+a.length,s.length); }
\r
84 return (s.indexOf(a) != -1) ? str_replace(a, b, ns) : ns;
\r
87 function shortCuts() {
\r
88 if (!event || (event.ctrlKey != true)) return;
\r
90 switch (event.keyCode) {
\r
92 ahrefThis(); break; // ctrl-shift-a
\r
94 boldThis(); break; // ctrl-shift-b
\r
96 italicThis(); break; // ctrl-shift-i
\r
98 addMedia(); break; // ctrl-shift-m
\r
105 function cutThis() { execAndUpdate('cut'); }
\r
106 function copyThis() { execAndUpdate('copy'); }
\r
107 function pasteThis() { execAndUpdate('paste'); }
\r
108 function boldThis() { insertAroundCaret('<b>','</b>'); }
\r
109 function italicThis() { insertAroundCaret('<i>','</i>'); }
\r
110 function leftThis() { insertAroundCaret('<div class="leftbox">','</div>'); }
\r
111 function rightThis() { insertAroundCaret('<div class="rightbox">','</div>'); }
\r
112 function alignleftThis() { insertAroundCaret('<div style="text-align: left">','</div>'); }
\r
113 function alignrightThis() { insertAroundCaret('<div style="text-align: right">','</div>'); }
\r
114 function aligncenterThis() { insertAroundCaret('<div style="text-align: center">','</div>'); }
\r
117 function ahrefThis() {
\r
118 if (document.selection)
\r
119 strSelection = document.selection.createRange().text;
\r
123 strHref = prompt("Create a link to:","http://");
\r
124 if (strHref == null) return;
\r
126 var textpre = "<a href=\"" + strHref + "\">";
\r
127 insertAroundCaret(textpre, "</a>");
\r
130 function execAndUpdate(action) {
\r
131 lastCaretPos.execCommand(action);
\r
136 var nonie_FormType = 'body';
\r
138 // Add media to new item
\r
139 function addMedia() {
\r
141 var mediapopup = window.open(nucleusMediaPopupURL + 'media.php','name',
\r
142 'status=yes,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,top=0,left=0');
\r
148 function setMediaPopupURL(url) {
\r
149 nucleusMediaPopupURL = url;
\r
152 function includeImage(collection, filename, type, width, height) {
\r
153 if (isCaretEmpty()) {
\r
154 text = prompt("Text to display ?","");
\r
156 text = getCaretText();
\r
159 // add collection name when not private collection (or editing a message that's not your)
\r
161 if (isNaN(collection) || (nucleusAuthorId != collection)) {
\r
162 fullName = collection + '/' + filename;
\r
164 fullName = filename;
\r
171 replaceBy = '<%popup(' + fullName + '|'+width+'|'+height+'|' + text +')%>';
\r
175 replaceBy = '<%image(' + fullName + '|'+width+'|'+height+'|' + text +')%>';
\r
178 insertAtCaret(replaceBy);
\r
184 function includeOtherMedia(collection, filename) {
\r
185 if (isCaretEmpty()) {
\r
186 text = prompt("Text to display ?","");
\r
188 text = getCaretText();
\r
191 // add collection name when not private collection (or editing a message that's not your)
\r
193 if (isNaN(collection) || (nucleusAuthorId != collection)) {
\r
194 fullName = collection + '/' + filename;
\r
196 fullName = filename;
\r
199 var replaceBy = '<%media(' + fullName + '|' + text +')%>';
\r
201 insertAtCaret(replaceBy);
\r
207 // function to prevent submitting form data twice
\r
209 function checkSubmit() {
\r
210 if (submitcount == 0) {
\r
219 // code to store the caret (cursor) position of a text field/text area
\r
220 // taken from javascript.faqts and modified
\r
221 // http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
\r
223 // stores the caret
\r
224 var lastSelected, lastCaretPos;
\r
225 function storeCaret (textEl) {
\r
228 if (textEl.createTextRange)
\r
229 //textEl.caretPos = document.selection.createRange().duplicate();
\r
230 lastCaretPos = document.selection.createRange().duplicate();
\r
232 // also store lastselectedelement
\r
233 lastSelected = textEl;
\r
235 nonie_FormType = textEl.name;
\r
238 //var lastSelected;
\r
240 // inserts text at caret (overwriting selection)
\r
241 function insertAtCaret (text) {
\r
242 var textEl = lastSelected;
\r
243 if (textEl && textEl.createTextRange && lastCaretPos) {
\r
244 var caretPos = lastCaretPos;
\r
245 caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
\r
246 } else if (!document.all && document.getElementById) {
\r
247 mozReplace(document.getElementById('input' + nonie_FormType), text);
\r
248 } else if (textEl) {
\r
249 textEl.value += text;
\r
251 document.getElementById('input' + nonie_FormType).value += text;
\r
256 // inserts a tag around the selected text
\r
257 function insertAroundCaret (textpre, textpost) {
\r
258 var textEl = lastSelected;
\r
260 if (textEl && textEl.createTextRange && lastCaretPos) {
\r
261 var caretPos = lastCaretPos;
\r
262 caretPos.text = textpre + caretPos.text + textpost;
\r
263 } else if (!document.all && document.getElementById) {
\r
264 mozWrap(document.getElementById('input' + nonie_FormType), textpre, textpost);
\r
266 document.getElementById('input' + nonie_FormType).value += textpre + textpost;
\r
272 /* some methods to get things working in Mozilla as well */
\r
273 function mozWrap(txtarea, lft, rgt) {
\r
274 var selLength = txtarea.textLength;
\r
275 var selStart = txtarea.selectionStart;
\r
276 var selEnd = txtarea.selectionEnd;
\r
277 if (selEnd==1 || selEnd==2) selEnd=selLength;
\r
278 var s1 = (txtarea.value).substring(0,selStart);
\r
279 var s2 = (txtarea.value).substring(selStart, selEnd)
\r
280 var s3 = (txtarea.value).substring(selEnd, selLength);
\r
281 txtarea.value = s1 + lft + s2 + rgt + s3;
\r
283 function mozReplace(txtarea, newText) {
\r
284 var selLength = txtarea.textLength;
\r
285 var selStart = txtarea.selectionStart;
\r
286 var selEnd = txtarea.selectionEnd;
\r
287 if (selEnd==1 || selEnd==2) selEnd=selLength;
\r
288 var s1 = (txtarea.value).substring(0,selStart);
\r
289 var s2 = (txtarea.value).substring(selStart, selEnd)
\r
290 var s3 = (txtarea.value).substring(selEnd, selLength);
\r
291 txtarea.value = s1 + newText + s3;
\r
293 function mozSelectedText() {
\r
294 var txtarea = document.getElementById('input' + nonie_FormType);
\r
295 var selLength = txtarea.textLength;
\r
296 var selStart = txtarea.selectionStart;
\r
297 var selEnd = txtarea.selectionEnd;
\r
298 if (selEnd==1 || selEnd==2) selEnd=selLength;
\r
299 return (txtarea.value).substring(selStart, selEnd);
\r
302 function getCaretText() {
\r
303 if (!document.all && document.getElementById)
\r
304 return mozSelectedText();
\r
306 return lastCaretPos.text;
\r
309 function isCaretEmpty() {
\r
310 if (lastSelected && lastSelected.createTextRange && lastCaretPos)
\r
311 return (lastCaretPos.text == '');
\r
312 else if (!document.all && document.getElementById)
\r
313 return (mozSelectedText() == '');
\r
318 function BtnHighlight(el) {
\r
320 borderLeft="1px solid gray";
\r
321 borderRight="1px solid #e9e9e9";
\r
322 borderTop="1px solid gray";
\r
323 borderBottom="1px solid #e9e9e9";
\r
327 function BtnNormal(el) {
\r
330 border="1px solid #dddddd";
\r