source: branches/posledni/forum/styles/WoWMoonclaw/template/editor.js

Last change on this file was 400, checked in by george, 16 years ago
  • Přidáno: Nové forum phpBB 3.
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;
19window.onload = initInsertions;
20
21/**
22* Shows the help messages in the helpline window
23*/
24function helpline(help)
25{
26 document.forms[form_name].helpbox.value = help_line[help];
27}
28
29/**
30* Fix a bug involving the TextRange object. From
31* http://www.frostjedi.com/terra/scripts/demo/caretBug.html
32*/
33function initInsertions()
34{
35 var doc;
36
37 if (document.forms[form_name])
38 {
39 doc = document;
40 }
41 else
42 {
43 doc = opener.document;
44 }
45
46 var textarea = doc.forms[form_name].elements[text_name];
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
165 else if (textarea.createTextRange && textarea.caretPos)
166 {
167 if (baseHeight != textarea.caretPos.boundingHeight)
168 {
169 textarea.focus();
170 storeCaret(textarea);
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 }
176 else
177 {
178 textarea.value = textarea.value + text;
179 }
180 if (!popup)
181 {
182 textarea.focus();
183 }
184
185}
186
187/**
188* Add inline attachment at position
189*/
190function attach_inline(index, filename)
191{
192 insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
193 document.forms[form_name].elements[text_name].focus();
194}
195
196/**
197* Add quote text to message
198*/
199function addquote(post_id, username)
200{
201 var message_name = 'message_' + post_id;
202 var theSelection = '';
203 var divarea = false;
204
205 if (document.all)
206 {
207 divarea = document.all[message_name];
208 }
209 else
210 {
211 divarea = document.getElementById(message_name);
212 }
213
214 // Get text selection - not only the post content :(
215 if (window.getSelection)
216 {
217 theSelection = window.getSelection().toString();
218 }
219 else if (document.getSelection)
220 {
221 theSelection = document.getSelection();
222 }
223 else if (document.selection)
224 {
225 theSelection = document.selection.createRange().text;
226 }
227
228 if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
229 {
230 if (divarea.innerHTML)
231 {
232 theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
233 theSelection = theSelection.replace(/<br\/>/ig, '\n');
234 theSelection = theSelection.replace(/&lt\;/ig, '<');
235 theSelection = theSelection.replace(/&gt\;/ig, '>');
236 theSelection = theSelection.replace(/&amp\;/ig, '&');
237 }
238 else if (document.all)
239 {
240 theSelection = divarea.innerText;
241 }
242 else if (divarea.textContent)
243 {
244 theSelection = divarea.textContent;
245 }
246 else if (divarea.firstChild.nodeValue)
247 {
248 theSelection = divarea.firstChild.nodeValue;
249 }
250 }
251
252 if (theSelection)
253 {
254 insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
255 }
256
257 return;
258}
259
260/**
261* From http://www.massless.org/mozedit/
262*/
263function mozWrap(txtarea, open, close)
264{
265 var selLength = txtarea.textLength;
266 var selStart = txtarea.selectionStart;
267 var selEnd = txtarea.selectionEnd;
268 var scrollTop = txtarea.scrollTop;
269
270 if (selEnd == 1 || selEnd == 2)
271 {
272 selEnd = selLength;
273 }
274
275 var s1 = (txtarea.value).substring(0,selStart);
276 var s2 = (txtarea.value).substring(selStart, selEnd)
277 var s3 = (txtarea.value).substring(selEnd, selLength);
278
279 txtarea.value = s1 + open + s2 + close + s3;
280 txtarea.selectionStart = selEnd + open.length + close.length;
281 txtarea.selectionEnd = txtarea.selectionStart;
282 txtarea.focus();
283 txtarea.scrollTop = scrollTop;
284
285 return;
286}
287
288/**
289* Insert at Caret position. Code from
290* http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
291*/
292function storeCaret(textEl)
293{
294 if (textEl.createTextRange)
295 {
296 textEl.caretPos = document.selection.createRange().duplicate();
297 }
298}
299
300/**
301* Color pallette
302*/
303function colorPalette(dir, width, height)
304{
305 var r = 0, g = 0, b = 0;
306 var numberList = new Array(6);
307 var color = '';
308
309 numberList[0] = '00';
310 numberList[1] = '40';
311 numberList[2] = '80';
312 numberList[3] = 'BF';
313 numberList[4] = 'FF';
314
315 document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
316
317 for (r = 0; r < 5; r++)
318 {
319 if (dir == 'h')
320 {
321 document.writeln('<tr>');
322 }
323
324 for (g = 0; g < 5; g++)
325 {
326 if (dir == 'v')
327 {
328 document.writeln('<tr>');
329 }
330
331 for (b = 0; b < 5; b++)
332 {
333 color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
334 document.write('<td bgcolor="#' + color + '">');
335 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>');
336 document.writeln('</td>');
337 }
338
339 if (dir == 'v')
340 {
341 document.writeln('</tr>');
342 }
343 }
344
345 if (dir == 'h')
346 {
347 document.writeln('</tr>');
348 }
349 }
350 document.writeln('</table>');
351}
352
353
354/**
355* Caret Position object
356*/
357function caretPosition()
358{
359 var start = null;
360 var end = null;
361}
362
363
364/**
365* Get the caret position in an textarea
366*/
367function getCaretPosition(txtarea)
368{
369 var caretPos = new caretPosition();
370
371 // simple Gecko/Opera way
372 if(txtarea.selectionStart || txtarea.selectionStart == 0)
373 {
374 caretPos.start = txtarea.selectionStart;
375 caretPos.end = txtarea.selectionEnd;
376 }
377 // dirty and slow IE way
378 else if(document.selection)
379 {
380 // get current selection
381 var range = document.selection.createRange();
382
383 // a new selection of the whole textarea
384 var range_all = document.body.createTextRange();
385 range_all.moveToElementText(txtarea);
386
387 // calculate selection start point by moving beginning of range_all to beginning of range
388 var sel_start;
389 for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
390 {
391 range_all.moveStart('character', 1);
392 }
393
394 txtarea.sel_start = sel_start;
395
396 // we ignore the end value for IE, this is already dirty enough and we don't need it
397 caretPos.start = txtarea.sel_start;
398 caretPos.end = txtarea.sel_start;
399 }
400
401 return caretPos;
402}
Note: See TracBrowser for help on using the repository browser.