2 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
3 * Copyright (C) 2002-2012 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.
14 * $Id: edit.js 1388 2009-07-18 06:31:28Z shizuki $
17 var nucleusConvertBreaks = true;
18 var nucleusMediaPopupURL = '';
19 var nucleusMediaURL = 'media/';
20 var nucleusAuthorId = 0;
23 function setConvertBreaks(newval) { nucleusConvertBreaks = newval; }
24 function setMediaUrl(url) { nucleusMediaURL = url; }
25 function setAuthorId(id) { nucleusAuthorId = id; }
27 function preview(id, value) {
28 elem = document.getElementById(id);
31 var preview = nucleusConvertBreaks ? str_replace("\n","<br />",value)+" " : value+" ";
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\" />");
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 && 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>");
41 elem.innerHTML = preview;
45 prevval = document.getElementById('edit').style.display;
46 if (prevval == "block")
50 document.getElementById('edit').style.display = newval;
52 if (newval == "block")
56 function updAllPreviews() {
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;
69 function updPreview(id) {
70 // don't update when preview is hidden
71 if (!isEditVisible()) return;
73 var inputField = document.getElementById('input' + id);
74 if (!inputField) return;
75 preview('prev' + id, inputField.value);
78 // replace a in s by b (taken from milov.nl)
79 function str_replace(a, b, s)
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;
87 function shortCuts() {
88 if (!event || (event.ctrlKey != true)) return;
90 switch (event.keyCode) {
92 ahrefThis(); break; // ctrl-shift-a
94 boldThis(); break; // ctrl-shift-b
96 italicThis(); break; // ctrl-shift-i
98 addMedia(); break; // ctrl-shift-m
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>'); }
117 function ahrefThis() {
118 if (document.selection)
119 strSelection = document.selection.createRange().text;
123 strHref = prompt("Create a link to:","http://");
124 if (strHref == null) return;
126 var textpre = "<a href=\"" + strHref.replace(/&/g,'&') + "\">";
127 insertAroundCaret(textpre, "</a>");
130 function execAndUpdate(action) {
131 lastSelected.caretPos.execCommand(action);
136 var nonie_FormType = 'body';
138 // Add media to new item
139 function addMedia() {
140 if ( typeof(medium) != 'undefined' )
146 var mediapopup = window.open(nucleusMediaPopupURL + 'media.php','name',
147 'status=yes,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,top=0,left=0');
151 function setMediaPopupURL(url) {
152 nucleusMediaPopupURL = url;
155 function includeImage(collection, filename, type, width, height) {
156 if (isCaretEmpty()) {
157 text = prompt("Text to display ?",filename);
159 text = getCaretText();
162 // add collection name when not private collection (or editing a message that's not your)
164 if (isNaN(collection) || (nucleusAuthorId != collection)) {
165 fullName = collection + '/' + filename;
174 replaceBy = '<%popup(' + fullName + '|'+width+'|'+height+'|' + text +')%>';
178 replaceBy = '<%image(' + fullName + '|'+width+'|'+height+'|' + text +')%>';
181 insertAtCaret(replaceBy);
187 function includeOtherMedia(collection, filename) {
188 if (isCaretEmpty()) {
189 text = prompt("Text to display ?",filename);
191 text = getCaretText();
194 // add collection name when not private collection (or editing a message that's not your)
196 if (isNaN(collection) || (nucleusAuthorId != collection)) {
197 fullName = collection + '/' + filename;
202 var replaceBy = '<%media(' + fullName + '|' + text +')%>';
204 insertAtCaret(replaceBy);
210 // function to prevent submitting form data twice
212 function checkSubmit() {
213 if (submitcount == 0) {
222 // code to store the caret (cursor) position of a text field/text area
223 // taken from javascript.faqts and modified
224 // http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
227 function storeCaret (textEl) {
230 if (textEl.createTextRange)
231 textEl.caretPos = 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 && textEl.caretPos) {
247 var caretPos = textEl.caretPos;
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 && textEl.caretPos) {
270 var caretPos = textEl.caretPos;
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 lastSelected.caretPos.text;
324 function isCaretEmpty() {
325 if (lastSelected && lastSelected.createTextRange && lastSelected.caretPos)
326 return (lastSelected.caretPos.text == '');
327 else if (!document.all && document.getElementById)
328 return (mozSelectedText() == '');
333 function BtnHighlight(el) {
335 borderLeft="1px solid #e9e9e9";
336 borderRight="1px solid gray";
337 borderTop="1px solid #e9e9e9";
338 borderBottom="1px solid gray";
342 function BtnNormal(el) {
345 border="1px solid #dddddd";