init
[garradin.git] / www / admin / static / wikitoolbar.js
1 (function () {
2 // Source: http://stackoverflow.com/questions/401593/textarea-selection
3 var selection =
4 {
5 get: function (e)
6 {
7 //Mozilla and DOM 3.0
8 if('selectionStart' in e)
9 {
10 var l = e.selectionEnd - e.selectionStart;
11 return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
12 }
13 //IE
14 else if(document.selection)
15 {
16 e.focus();
17 var r = document.selection.createRange();
18 var tr = e.createTextRange();
19 var tr2 = tr.duplicate();
20 tr2.moveToBookmark(r.getBookmark());
21 tr.setEndPoint('EndToStart',tr2);
22 if (r == null || tr == null) return { start: e.value.length, end: e.value.length, length: 0, text: '' };
23 var text_part = r.text.replace(/[\r\n]/g,'.'); //for some reason IE doesn't always count the \n and \r in the length
24 var text_whole = e.value.replace(/[\r\n]/g,'.');
25 var the_start = text_whole.indexOf(text_part,tr.text.length);
26 return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };
27 }
28 //Browser not supported
29 else return { start: e.value.length, end: e.value.length, length: 0, text: '' };
30 },
31
32 replace: function (e, replace_str)
33 {
34 var selection = this.get(e);
35 var start_pos = selection.start;
36 var end_pos = start_pos + replace_str.length;
37 e.value = e.value.substr(0, start_pos) + replace_str + e.value.substr(selection.end, e.value.length);
38 this.set(e,start_pos,end_pos);
39 return {start: start_pos, end: end_pos, length: replace_str.length, text: replace_str};
40 },
41
42 set: function (e, start_pos,end_pos)
43 {
44 //Mozilla and DOM 3.0
45 if('selectionStart' in e)
46 {
47 e.focus();
48 e.selectionStart = start_pos;
49 e.selectionEnd = end_pos;
50 }
51 //IE
52 else if(document.selection)
53 {
54 e.focus();
55 var tr = e.createTextRange();
56
57 //Fix IE from counting the newline characters as two seperate characters
58 var stop_it = start_pos;
59 for (i=0; i < stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) start_pos = start_pos - .5;
60 stop_it = end_pos;
61 for (i=0; i < stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) end_pos = end_pos - .5;
62
63 tr.moveEnd('textedit',-1);
64 tr.moveStart('character',start_pos);
65 tr.moveEnd('character',end_pos - start_pos);
66 tr.select();
67 }
68 return this.get(e);
69 },
70
71 wrap: function (e, left_str, right_str, sel_offset, sel_length)
72 {
73 var scroll = e.scrollTop;
74 var the_sel_text = this.get(e).text;
75 var selection = this.replace(e, left_str + the_sel_text + right_str );
76 if(sel_offset !== undefined && sel_length !== undefined) selection = this.set(e, selection.start + sel_offset, selection.start + sel_offset + sel_length);
77 else if(the_sel_text == '') selection = this.set(e, selection.start + left_str.length, selection.start + left_str.length);
78 e.scrollTop = scroll;
79 return selection;
80 }
81 };
82
83 function launchToolbar()
84 {
85 function addBtn(className, label, action)
86 {
87 var btn = document.createElement('input');
88 btn.type = 'button';
89 btn.className = className;
90 btn.value = label;
91 btn.onclick = action;
92 toolbar.appendChild(btn);
93 }
94
95 var txt = document.getElementById('f_contenu');
96 var parent = txt.parentNode.parentNode;
97 var toolbar = document.createElement('div');
98 toolbar.className = "toolbar";
99
100 addBtn('title', 'Titre', function () { selection.wrap(txt, '{{{', "}}}\n"); } );
101 addBtn('italic', 'Italique', function () { selection.wrap(txt, '{', '}'); } );
102 addBtn('bold', 'Gras', function () { selection.wrap(txt, '{{', '}}'); } );
103 addBtn('strike', 'Barré', function () { selection.wrap(txt, '<del>', '</del>'); } );
104 addBtn('code', 'Chasse fixe', function () { selection.wrap(txt, '<pre>', '</pre>'); } );
105 addBtn('link', 'Lien', function () {
106 if (url = window.prompt('Adresse du lien ?'))
107 {
108 selection.wrap(txt, '[', '->' + url + ']');
109 }
110 } );
111
112 parent.insertBefore(toolbar, txt.parentNode);
113 }
114
115 if (document.addEventListener)
116 {
117 document.addEventListener("DOMContentLoaded", launchToolbar, false);
118 }
119 else
120 {
121 document.attachEvent("onDOMContentLoaded", launchToolbar);
122 }
123 } () );