xref: /dokuwiki/lib/scripts/toolbar.js (revision 6b6da7f587c020b3439dc14c8250766f895adb93)
1/**
2 * Create a toolbar
3 *
4 * @param  string tbid ID of the element where to insert the toolbar
5 * @param  string edid ID of the editor textarea
6 * @param  array  tb   Associative array defining the buttons
7 * @author Andreas Gohr <andi@splitbrain.org>
8 */
9function initToolbar(tbid,edid,tb){
10    var toolbar = $(tbid);
11    if(!toolbar) return;
12
13    //empty the toolbar area:
14    toolbar.innerHTML='';
15
16    var cnt = tb.length;
17    for(var i=0; i<cnt; i++){
18        var actionFunc;
19
20        // create new button
21        var btn = createToolButton(tb[i]['icon'],
22                                   tb[i]['title'],
23                                   tb[i]['key']);
24
25
26        // type is a tb function -> assign it as onclick
27        actionFunc = 'tb_'+tb[i]['type'];
28        if( isFunction(window[actionFunc]) ){
29            addEvent(btn,'click', function(func,btn, props, edid){
30                return function(){
31                    window[func](btn, props, edid);
32                    return false;
33                }
34            }(actionFunc,btn,tb[i],edid) );
35            //above fixes the scope problem as descried at http://www.mennovanslooten.nl/blog/post/62
36            toolbar.appendChild(btn);
37            continue;
38        }
39
40        // type is a init function -> execute it
41        actionFunc = 'addBtnAction'+tb[i]['type'].charAt(0).toUpperCase()+tb[i]['type'].substring(1);
42        if( isFunction(window[actionFunc]) ){
43            if(window[actionFunc](btn, tb[i], edid, i)){
44                toolbar.appendChild(btn);
45            }
46            continue;
47        }
48
49        console.log('unknown toolbar type: '+tb[i]['type']+'  '+actionFunc); //FIXME make alert
50    } // end for
51
52}
53
54/**
55 * Button action for format buttons
56 *
57 * @param  DOMElement btn   Button element to add the action to
58 * @param  array      props Associative array of button properties
59 * @param  string     edid  ID of the editor textarea
60 * @author Gabriel Birke <birke@d-scribe.de>
61 * @author Andreas Gohr <andi@splitbrain.org>
62 */
63function tb_format(btn, props, edid) {
64    var sample = props['title'];
65    if(props['sample']){
66        sample = props['sample'];
67    }
68    insertTags(edid,
69               fixtxt(props['open']),
70               fixtxt(props['close']),
71               fixtxt(sample));
72    return false;
73}
74
75/**
76 * Button action for insert buttons
77 *
78 * @param  DOMElement btn   Button element to add the action to
79 * @param  array      props Associative array of button properties
80 * @param  string     edid  ID of the editor textarea
81 * @author Gabriel Birke <birke@d-scribe.de>
82 * @author Andreas Gohr <andi@splitbrain.org>
83 */
84function tb_insert(btn, props, edid) {
85    insertAtCarret(edid,fixtxt(props['insert']));
86}
87
88
89
90
91
92
93/**
94 * Replaces \n with linebreaks
95 */
96function fixtxt(str){
97    return str.replace(/\\n/g,"\n");
98}
99
100