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