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.3 2005-03-16 08:07:50 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 lastSelected.caretPos.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 function storeCaret (textEl) {
\r
227 if (textEl.createTextRange)
\r
228 //textEl.caretPos = document.selection.createRange().duplicate();
\r
229 lastCaretPos = document.selection.createRange().duplicate();
\r
231 // also store lastselectedelement
\r
232 lastSelected = textEl;
\r
234 nonie_FormType = textEl.name;
\r
239 // inserts text at caret (overwriting selection)
\r
240 function insertAtCaret (text) {
\r
241 var textEl = lastSelected;
\r
242 if (textEl && textEl.createTextRange && textEl.caretPos) {
\r
243 var caretPos = textEl.caretPos;
\r
244 caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
\r
245 } else if (!document.all && document.getElementById) {
\r
246 mozReplace(document.getElementById('input' + nonie_FormType), text);
\r
247 } else if (textEl) {
\r
248 textEl.value += text;
\r
250 document.getElementById('input' + nonie_FormType).value += text;
\r
255 // inserts a tag around the selected text
\r
256 function insertAroundCaret (textpre, textpost) {
\r
257 var textEl = lastSelected;
\r
259 if (textEl && textEl.createTextRange && textEl.caretPos) {
\r
260 var caretPos = textEl.caretPos;
\r
261 caretPos.text = textpre + caretPos.text + textpost;
\r
262 } else if (!document.all && document.getElementById) {
\r
263 mozWrap(document.getElementById('input' + nonie_FormType), textpre, textpost);
\r
265 document.getElementById('input' + nonie_FormType).value += textpre + textpost;
\r
271 /* some methods to get things working in Mozilla as well */
\r
272 function mozWrap(txtarea, lft, rgt) {
\r
273 var selLength = txtarea.textLength;
\r
274 var selStart = txtarea.selectionStart;
\r
275 var selEnd = txtarea.selectionEnd;
\r
276 if (selEnd==1 || selEnd==2) selEnd=selLength;
\r
277 var s1 = (txtarea.value).substring(0,selStart);
\r
278 var s2 = (txtarea.value).substring(selStart, selEnd)
\r
279 var s3 = (txtarea.value).substring(selEnd, selLength);
\r
280 txtarea.value = s1 + lft + s2 + rgt + s3;
\r
282 function mozReplace(txtarea, newText) {
\r
283 var selLength = txtarea.textLength;
\r
284 var selStart = txtarea.selectionStart;
\r
285 var selEnd = txtarea.selectionEnd;
\r
286 if (selEnd==1 || selEnd==2) selEnd=selLength;
\r
287 var s1 = (txtarea.value).substring(0,selStart);
\r
288 var s2 = (txtarea.value).substring(selStart, selEnd)
\r
289 var s3 = (txtarea.value).substring(selEnd, selLength);
\r
290 txtarea.value = s1 + newText + s3;
\r
292 function mozSelectedText() {
\r
293 var txtarea = document.getElementById('input' + nonie_FormType);
\r
294 var selLength = txtarea.textLength;
\r
295 var selStart = txtarea.selectionStart;
\r
296 var selEnd = txtarea.selectionEnd;
\r
297 if (selEnd==1 || selEnd==2) selEnd=selLength;
\r
298 return (txtarea.value).substring(selStart, selEnd);
\r
301 function getCaretText() {
\r
302 if (!document.all && document.getElementById)
\r
303 return mozSelectedText();
\r
305 return lastSelected.caretPos.text;
\r
308 function isCaretEmpty() {
\r
309 if (lastSelected && lastSelected.createTextRange && lastSelected.caretPos)
\r
310 return (lastSelected.caretPos.text == '');
\r
311 else if (!document.all && document.getElementById)
\r
312 return (mozSelectedText() == '');
\r
317 function BtnHighlight(el) {
\r
319 borderLeft="1px solid gray";
\r
320 borderRight="1px solid #e9e9e9";
\r
321 borderTop="1px solid gray";
\r
322 borderBottom="1px solid #e9e9e9";
\r
326 function BtnNormal(el) {
\r
329 border="1px solid #dddddd";
\r