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