source: trunk/forum/styles/prosilver/template/editor.js

Last change on this file was 702, checked in by george, 15 years ago
  • Upraveno: Aktualizace fóra.
File size: 9.5 KB
Line 
1/**
2* bbCode control by subBlue design [ www.subBlue.com ]
3* Includes unixsafe colour palette selector by SHS`
4*/
5
6// Startup variables
7var imageTag = false;
8var theSelection = false;
9
10var bbcodeEnabled = true;
11// Check for Browser & Platform for PC & IE specific bits
12// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
13var clientPC = navigator.userAgent.toLowerCase(); // Get client info
14var clientVer = parseInt(navigator.appVersion); // Get browser version
15
16var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
17var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));
18var baseHeight;
19
20/**
21* Shows the help messages in the helpline window
22*/
23function helpline(help)
24{
25 document.forms[form_name].helpbox.value = help_line[help];
26}
27
28/**
29* Fix a bug involving the TextRange object. From
30* http://www.frostjedi.com/terra/scripts/demo/caretBug.html
31*/
32function initInsertions()
33{
34 var doc;
35
36 if (document.forms[form_name])
37 {
38 doc = document;
39 }
40 else
41 {
42 doc = opener.document;
43 }
44
45 var textarea = doc.forms[form_name].elements[text_name];
46
47 if (is_ie && typeof(baseHeight) != 'number')
48 {
49 textarea.focus();
50 baseHeight = doc.selection.createRange().duplicate().boundingHeight;
51
52 if (!document.forms[form_name])
53 {
54 document.body.focus();
55 }
56 }
57}
58
59/**
60* bbstyle
61*/
62function bbstyle(bbnumber)
63{
64 if (bbnumber != -1)
65 {
66 bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
67 }
68 else
69 {
70 insert_text('[*]');
71 document.forms[form_name].elements[text_name].focus();
72 }
73}
74
75/**
76* Apply bbcodes
77*/
78function bbfontstyle(bbopen, bbclose)
79{
80 theSelection = false;
81
82 var textarea = document.forms[form_name].elements[text_name];
83
84 textarea.focus();
85
86 if ((clientVer >= 4) && is_ie && is_win)
87 {
88 // Get text selection
89 theSelection = document.selection.createRange().text;
90
91 if (theSelection)
92 {
93 // Add tags around selection
94 document.selection.createRange().text = bbopen + theSelection + bbclose;
95 document.forms[form_name].elements[text_name].focus();
96 theSelection = '';
97 return;
98 }
99 }
100 else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
101 {
102 mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
103 document.forms[form_name].elements[text_name].focus();
104 theSelection = '';
105 return;
106 }
107
108 //The new position for the cursor after adding the bbcode
109 var caret_pos = getCaretPosition(textarea).start;
110 var new_pos = caret_pos + bbopen.length;
111
112 // Open tag
113 insert_text(bbopen + bbclose);
114
115 // Center the cursor when we don't have a selection
116 // Gecko and proper browsers
117 if (!isNaN(textarea.selectionStart))
118 {
119 textarea.selectionStart = new_pos;
120 textarea.selectionEnd = new_pos;
121 }
122 // IE
123 else if (document.selection)
124 {
125 var range = textarea.createTextRange();
126 range.move("character", new_pos);
127 range.select();
128 storeCaret(textarea);
129 }
130
131 textarea.focus();
132 return;
133}
134
135/**
136* Insert text at position
137*/
138function insert_text(text, spaces, popup)
139{
140 var textarea;
141
142 if (!popup)
143 {
144 textarea = document.forms[form_name].elements[text_name];
145 }
146 else
147 {
148 textarea = opener.document.forms[form_name].elements[text_name];
149 }
150 if (spaces)
151 {
152 text = ' ' + text + ' ';
153 }
154
155 if (!isNaN(textarea.selectionStart))
156 {
157 var sel_start = textarea.selectionStart;
158 var sel_end = textarea.selectionEnd;
159
160 mozWrap(textarea, text, '')
161 textarea.selectionStart = sel_start + text.length;
162 textarea.selectionEnd = sel_end + text.length;
163 }
164 else if (textarea.createTextRange && textarea.caretPos)
165 {
166 if (baseHeight != textarea.caretPos.boundingHeight)
167 {
168 textarea.focus();
169 storeCaret(textarea);
170 }
171
172 var caret_pos = textarea.caretPos;
173 caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
174 }
175 else
176 {
177 textarea.value = textarea.value + text;
178 }
179 if (!popup)
180 {
181 textarea.focus();
182 }
183}
184
185/**
186* Add inline attachment at position
187*/
188function attach_inline(index, filename)
189{
190 insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
191 document.forms[form_name].elements[text_name].focus();
192}
193
194/**
195* Add quote text to message
196*/
197function addquote(post_id, username)
198{
199 var message_name = 'message_' + post_id;
200 var theSelection = '';
201 var divarea = false;
202
203 if (document.all)
204 {
205 divarea = document.all[message_name];
206 }
207 else
208 {
209 divarea = document.getElementById(message_name);
210 }
211
212 // Get text selection - not only the post content :(
213 if (window.getSelection)
214 {
215 theSelection = window.getSelection().toString();
216 }
217 else if (document.getSelection)
218 {
219 theSelection = document.getSelection();
220 }
221 else if (document.selection)
222 {
223 theSelection = document.selection.createRange().text;
224 }
225
226 if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
227 {
228 if (divarea.innerHTML)
229 {
230 theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
231 theSelection = theSelection.replace(/<br\/>/ig, '\n');
232 theSelection = theSelection.replace(/&lt\;/ig, '<');
233 theSelection = theSelection.replace(/&gt\;/ig, '>');
234 theSelection = theSelection.replace(/&amp\;/ig, '&');
235 theSelection = theSelection.replace(/&nbsp\;/ig, ' ');
236 }
237 else if (document.all)
238 {
239 theSelection = divarea.innerText;
240 }
241 else if (divarea.textContent)
242 {
243 theSelection = divarea.textContent;
244 }
245 else if (divarea.firstChild.nodeValue)
246 {
247 theSelection = divarea.firstChild.nodeValue;
248 }
249 }
250
251 if (theSelection)
252 {
253 if (bbcodeEnabled)
254 {
255 insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
256 }
257 else
258 {
259 var lines = split_lines(theSelection);
260 for (i = 0; i < lines.length; i++)
261 {
262 insert_text('> ' + lines[i] + '\n')
263 }
264 }
265 }
266
267 return;
268}
269
270function split_lines(text)
271{
272 var lines = text.split('\n');
273 var splitLines = new Array();
274 var j = 0;
275 for(i = 0; i < lines.length; i++)
276 {
277 if (lines[i].length <= 80)
278 {
279 splitLines[j] = lines[i];
280 j++;
281 }
282 else
283 {
284 var line = lines[i];
285 do
286 {
287 var splitAt = line.indexOf(' ', 80);
288
289 if (splitAt == -1)
290 {
291 splitLines[j] = line;
292 j++
293 }
294 else
295 {
296 splitLines[j] = line.substring(0, splitAt);
297 line = line.substring(splitAt);
298 j++;
299 }
300 }
301 while(splitAt != -1)
302 }
303 }
304 return splitLines;
305}
306/**
307* From http://www.massless.org/mozedit/
308*/
309function mozWrap(txtarea, open, close)
310{
311 var selLength = (typeof(txtarea.textLength) == 'undefined') ? txtarea.value.length : txtarea.textLength;
312 var selStart = txtarea.selectionStart;
313 var selEnd = txtarea.selectionEnd;
314 var scrollTop = txtarea.scrollTop;
315
316 if (selEnd == 1 || selEnd == 2)
317 {
318 selEnd = selLength;
319 }
320
321 var s1 = (txtarea.value).substring(0,selStart);
322 var s2 = (txtarea.value).substring(selStart, selEnd)
323 var s3 = (txtarea.value).substring(selEnd, selLength);
324
325 txtarea.value = s1 + open + s2 + close + s3;
326 txtarea.selectionStart = selStart + open.length;
327 txtarea.selectionEnd = selEnd + open.length;
328 txtarea.focus();
329 txtarea.scrollTop = scrollTop;
330
331 return;
332}
333
334/**
335* Insert at Caret position. Code from
336* http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
337*/
338function storeCaret(textEl)
339{
340 if (textEl.createTextRange)
341 {
342 textEl.caretPos = document.selection.createRange().duplicate();
343 }
344}
345
346/**
347* Color pallette
348*/
349function colorPalette(dir, width, height)
350{
351 var r = 0, g = 0, b = 0;
352 var numberList = new Array(6);
353 var color = '';
354
355 numberList[0] = '00';
356 numberList[1] = '40';
357 numberList[2] = '80';
358 numberList[3] = 'BF';
359 numberList[4] = 'FF';
360
361 document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
362
363 for (r = 0; r < 5; r++)
364 {
365 if (dir == 'h')
366 {
367 document.writeln('<tr>');
368 }
369
370 for (g = 0; g < 5; g++)
371 {
372 if (dir == 'v')
373 {
374 document.writeln('<tr>');
375 }
376
377 for (b = 0; b < 5; b++)
378 {
379 color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
380 document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
381 document.write('<a href="#" onclick="bbfontstyle(\'[color=#' + color + ']\', \'[/color]\'); return false;"><img src="images/spacer.gif" width="' + width + '" height="' + height + '" alt="#' + color + '" title="#' + color + '" /></a>');
382 document.writeln('</td>');
383 }
384
385 if (dir == 'v')
386 {
387 document.writeln('</tr>');
388 }
389 }
390
391 if (dir == 'h')
392 {
393 document.writeln('</tr>');
394 }
395 }
396 document.writeln('</table>');
397}
398
399
400/**
401* Caret Position object
402*/
403function caretPosition()
404{
405 var start = null;
406 var end = null;
407}
408
409
410/**
411* Get the caret position in an textarea
412*/
413function getCaretPosition(txtarea)
414{
415 var caretPos = new caretPosition();
416
417 // simple Gecko/Opera way
418 if(txtarea.selectionStart || txtarea.selectionStart == 0)
419 {
420 caretPos.start = txtarea.selectionStart;
421 caretPos.end = txtarea.selectionEnd;
422 }
423 // dirty and slow IE way
424 else if(document.selection)
425 {
426
427 // get current selection
428 var range = document.selection.createRange();
429
430 // a new selection of the whole textarea
431 var range_all = document.body.createTextRange();
432 range_all.moveToElementText(txtarea);
433
434 // calculate selection start point by moving beginning of range_all to beginning of range
435 var sel_start;
436 for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
437 {
438 range_all.moveStart('character', 1);
439 }
440
441 txtarea.sel_start = sel_start;
442
443 // we ignore the end value for IE, this is already dirty enough and we don't need it
444 caretPos.start = txtarea.sel_start;
445 caretPos.end = txtarea.sel_start;
446 }
447
448 return caretPos;
449}
Note: See TracBrowser for help on using the repository browser.