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