xref: /dokuwiki/lib/scripts/toolbar.js (revision f2e00ec3da47e3b50c850b0f878f28695d536d2f)
1
2// used to identify pickers
3var pickercounter=0;
4
5/**
6 * Create a toolbar
7 *
8 * @param  string tbid ID of the element where to insert the toolbar
9 * @param  string edid ID of the editor textarea
10 * @param  array  tb   Associative array defining the buttons
11 * @author Andreas Gohr <andi@splitbrain.org>
12 */
13function initToolbar(tbid,edid,tb){
14    var toolbar = $(tbid);
15    if(!toolbar) return;
16    var edit = $(edid);
17    if(!edit) return;
18    if(edit.readOnly) return;
19
20    //empty the toolbar area:
21    toolbar.innerHTML='';
22
23    var cnt = tb.length;
24    for(var i=0; i<cnt; i++){
25        var actionFunc;
26
27        // create new button
28        var btn = createToolButton(tb[i]['icon'],
29                                   tb[i]['title'],
30                                   tb[i]['key']);
31
32
33        // type is a tb function -> assign it as onclick
34        actionFunc = 'tb_'+tb[i]['type'];
35        if( isFunction(window[actionFunc]) ){
36            addEvent(btn,'click', bind(window[actionFunc],btn,tb[i],edid));
37            toolbar.appendChild(btn);
38            continue;
39        }
40
41        // type is a init function -> execute it
42        actionFunc = 'addBtnAction'+tb[i]['type'].charAt(0).toUpperCase()+tb[i]['type'].substring(1);
43        if( isFunction(window[actionFunc]) ){
44            if(window[actionFunc](btn, tb[i], edid)){
45                toolbar.appendChild(btn);
46            }
47            continue;
48        }
49
50        alert('unknown toolbar type: '+tb[i]['type']+'  '+actionFunc);
51    } // end for
52
53}
54
55/**
56 * Button action for format buttons
57 *
58 * @param  DOMElement btn   Button element to add the action to
59 * @param  array      props Associative array of button properties
60 * @param  string     edid  ID of the editor textarea
61 * @author Gabriel Birke <birke@d-scribe.de>
62 * @author Andreas Gohr <andi@splitbrain.org>
63 */
64function tb_format(btn, props, edid) {
65    var sample = props['title'];
66    if(props['sample']){
67        sample = props['sample'];
68    }
69    insertTags(edid,
70               fixtxt(props['open']),
71               fixtxt(props['close']),
72               fixtxt(sample));
73    pickerClose();
74    return false;
75}
76
77/**
78 * Button action for format buttons
79 *
80 * This works exactly as tb_format() except that, if multiple lines
81 * are selected, each line will be formatted seperately
82 *
83 * @param  DOMElement btn   Button element to add the action to
84 * @param  array      props Associative array of button properties
85 * @param  string     edid  ID of the editor textarea
86 * @author Gabriel Birke <birke@d-scribe.de>
87 * @author Andreas Gohr <andi@splitbrain.org>
88 */
89function tb_formatln(btn, props, edid) {
90    var sample = props['title'];
91    if(props['sample']){
92        sample = props['sample'];
93    }
94    sample = fixtxt(sample);
95
96    props['open']  = fixtxt(props['open']);
97    props['close'] = fixtxt(props['close']);
98
99    // is something selected?
100    var opts;
101    var selection = getSelection($(edid));
102    if(selection.getLength()){
103        sample = selection.getText();
104        opts = {nosel: true};
105    }else{
106        opts = {
107            startofs: props['open'].length,
108            endofs: props['close'].length
109        };
110    }
111
112    sample = sample.split("\n").join(props['close']+"\n"+props['open']);
113    sample = props['open']+sample+props['close'];
114
115    pasteText(selection,sample,opts);
116
117    pickerClose();
118    return false;
119}
120
121/**
122 * Button action for insert buttons
123 *
124 * @param  DOMElement btn   Button element to add the action to
125 * @param  array      props Associative array of button properties
126 * @param  string     edid  ID of the editor textarea
127 * @author Gabriel Birke <birke@d-scribe.de>
128 * @author Andreas Gohr <andi@splitbrain.org>
129 */
130function tb_insert(btn, props, edid) {
131    insertAtCarret(edid,fixtxt(props['insert']));
132    pickerClose();
133    return false;
134}
135
136/**
137 * Button action for the media popup
138 *
139 * @param  DOMElement btn   Button element to add the action to
140 * @param  array      props Associative array of button properties
141 * @param  string     edid  ID of the editor textarea
142 * @author Andreas Gohr <andi@splitbrain.org>
143 */
144function tb_mediapopup(btn, props, edid) {
145    window.open(
146        DOKU_BASE+props['url']+encodeURIComponent(NS),
147        props['name'],
148        props['options']);
149    return false;
150}
151
152/**
153 * Button action for automatic headlines
154 *
155 * Insert a new headline based on the current section level
156 *
157 * @param  DOMElement btn   Button element to add the action to
158 * @param  array      props Associative array of button properties
159 * @param  string     edid  ID of the editor textarea
160 * @author Andreas Gohr <andi@splitbrain.org>
161 */
162function tb_autohead(btn, props, edid){
163    var lvl = currentHeadlineLevel(edid);
164
165    // determine new level
166    lvl += props['mod'];
167    if(lvl < 1) lvl = 1;
168    if(lvl > 5) lvl = 5;
169
170    var tags = '=';
171    for(var i=0; i<=5-lvl; i++) tags += '=';
172    insertTags(edid, tags+' ', ' '+tags+"\n", props['text']);
173    pickerClose();
174    return false;
175}
176
177
178/**
179 * Add button action for picker buttons and create picker element
180 *
181 * @param  DOMElement btn   Button element to add the action to
182 * @param  array      props Associative array of button properties
183 * @param  string     edid  ID of the editor textarea
184 * @return boolean    If button should be appended
185 * @author Gabriel Birke <birke@d-scribe.de>
186 */
187function addBtnActionPicker(btn, props, edid) {
188    var pickerid = 'picker'+(pickercounter++);
189    createPicker(pickerid, props, edid);
190    addEvent(btn,'click',function(){
191        pickerToggle(pickerid,btn);
192        return false;
193    });
194    return true;
195}
196
197/**
198 * Add button action for the link wizard button
199 *
200 * @param  DOMElement btn   Button element to add the action to
201 * @param  array      props Associative array of button properties
202 * @param  string     edid  ID of the editor textarea
203 * @return boolean    If button should be appended
204 * @author Andreas Gohr <gohr@cosmocode.de>
205 */
206function addBtnActionLinkwiz(btn, props, edid) {
207    linkwiz.init($(edid));
208    addEvent(btn,'click',function(){
209        linkwiz.toggle();
210        return false;
211    });
212    return true;
213}
214
215/**
216 * Show/Hide a previosly created picker window
217 *
218 * @author Andreas Gohr <andi@splitbrain.org>
219 */
220function pickerToggle(pickerid,btn){
221    var picker = $(pickerid);
222    if(picker.style.marginLeft == '-10000px'){
223        var x = findPosX(btn);
224        var y = findPosY(btn);
225        picker.style.left = (x+3)+'px';
226        picker.style.top = (y+btn.offsetHeight+3)+'px';
227        picker.style.marginLeft = '0px';
228    }else{
229        picker.style.marginLeft = '-10000px';
230    }
231}
232
233/**
234 * Close all open pickers
235 *
236 * @author Andreas Gohr <andi@splitbrain.org>
237 */
238function pickerClose(){
239    var pobjs = getElementsByClass('picker');
240    for(var i=0; i<pobjs.length; i++){
241        pobjs[i].style.marginLeft = '-10000px';
242    }
243}
244
245
246/**
247 * Replaces \n with linebreaks
248 */
249function fixtxt(str){
250    return str.replace(/\\n/g,"\n");
251}
252
253