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

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