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