2 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
3 * Copyright (C) 2002-2009 The Nucleus Group
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)
11 * This file contains functions to allow adding items from inside the weblog.
12 * Also contains code to avoid submitting form data twice.
15 * $NucleusJP: edit.js,v 1.5 2006/07/12 07:11:47 kimitake Exp $
18 var nucleusConvertBreaks = true;
19 var nucleusMediaPopupURL = '';
20 var nucleusMediaURL = 'media/';
21 var nucleusAuthorId = 0;
24 function setConvertBreaks(newval) { nucleusConvertBreaks = newval; }
25 function setMediaUrl(url) { nucleusMediaURL = url; }
26 function setAuthorId(id) { nucleusAuthorId = id; }
28 function preview(id, value) {
29 elem = document.getElementById(id);
32 var preview = nucleusConvertBreaks ? str_replace("\n","<br />",value)+" " : value+" ";
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\" />");
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 && 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>");
42 elem.innerHTML = preview;
46 prevval = document.getElementById('edit').style.display;
47 if (prevval == "block")
51 document.getElementById('edit').style.display = newval;
53 if (newval == "block")
57 function updAllPreviews() {
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;
70 function updPreview(id) {
71 // don't update when preview is hidden
72 if (!isEditVisible()) return;
74 var inputField = document.getElementById('input' + id);
75 if (!inputField) return;
76 preview('prev' + id, inputField.value);
79 // replace a in s by b (taken from milov.nl)
80 function str_replace(a, b, s)
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;
88 function shortCuts() {
89 if (!event || (event.ctrlKey != true)) return;
91 switch (event.keyCode) {
93 ahrefThis(); break; // ctrl-shift-a
95 boldThis(); break; // ctrl-shift-b
97 italicThis(); break; // ctrl-shift-i
99 addMedia(); break; // ctrl-shift-m
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>'); }
118 function ahrefThis() {
119 if (document.selection)
120 strSelection = document.selection.createRange().text;
124 strHref = prompt("Create a link to:","http://");
125 if (strHref == null) return;
127 var textpre = "<a href=\"" + strHref.replace(/&/g,'&') + "\">";
128 insertAroundCaret(textpre, "</a>");
131 function execAndUpdate(action) {
132 lastSelected.caretPos.execCommand(action);
137 var nonie_FormType = 'body';
139 // Add media to new item
140 function addMedia() {
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');
149 function setMediaPopupURL(url) {
150 nucleusMediaPopupURL = url;
153 function includeImage(collection, filename, type, width, height) {
154 if (isCaretEmpty()) {
155 text = prompt("Text to display ?", filename);
157 text = getCaretText();
160 // add collection name when not private collection (or editing a message that's not your)
162 if (isNaN(collection) || (nucleusAuthorId != collection)) {
163 fullName = collection + '/' + filename;
172 replaceBy = '<%popup(' + fullName + '|'+width+'|'+height+'|' + text +')%>';
176 replaceBy = '<%image(' + fullName + '|'+width+'|'+height+'|' + text +')%>';
179 insertAtCaret(replaceBy);
185 function includeOtherMedia(collection, filename) {
186 if (isCaretEmpty()) {
187 text = prompt("Text to display ?", filename);
189 text = getCaretText();
192 // add collection name when not private collection (or editing a message that's not your)
194 if (isNaN(collection) || (nucleusAuthorId != collection)) {
195 fullName = collection + '/' + filename;
200 var replaceBy = '<%media(' + fullName + '|' + text +')%>';
202 insertAtCaret(replaceBy);
208 // function to prevent submitting form data twice
210 function checkSubmit() {
211 if (submitcount == 0) {
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
225 var lastSelected, lastCaretPos;
226 function storeCaret (textEl) {
229 if (textEl.createTextRange)
230 //textEl.caretPos = document.selection.createRange().duplicate();
231 lastCaretPos = document.selection.createRange().duplicate();
233 // also store lastselectedelement
234 lastSelected = textEl;
236 nonie_FormType = textEl.name;
238 scrollTop = textEl.scrollTop;
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);
252 document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
255 textEl.value += text;
257 document.getElementById('input' + nonie_FormType).value += text;
259 document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
265 // inserts a tag around the selected text
266 function insertAroundCaret (textpre, textpost) {
267 var textEl = lastSelected;
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);
275 document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
278 document.getElementById('input' + nonie_FormType).value += textpre + textpost;
280 document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
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;
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;
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);
317 function getCaretText() {
318 if (!document.all && document.getElementById)
319 return mozSelectedText();
321 return lastCaretPos.text;
324 function isCaretEmpty() {
325 if (lastSelected && lastSelected.createTextRange && lastCaretPos)
326 return (lastCaretPos.text == '');
327 else if (!document.all && document.getElementById)
328 return (mozSelectedText() == '');
333 function BtnHighlight(el) {
335 borderLeft="1px solid gray";
336 borderRight="1px solid #e9e9e9";
337 borderTop="1px solid gray";
338 borderBottom="1px solid #e9e9e9";
342 function BtnNormal(el) {
345 border="1px solid #dddddd";