xref: /dokuwiki/lib/scripts/toolbar.js (revision fedfae56f18b18786fe55bbbc71d4313eea52c08)
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
17    //empty the toolbar area:
18    toolbar.innerHTML='';
19
20    var cnt = tb.length;
21    for(var i=0; i<cnt; i++){
22        var actionFunc;
23
24        // create new button
25        var btn = createToolButton(tb[i]['icon'],
26                                   tb[i]['title'],
27                                   tb[i]['key']);
28
29
30        // type is a tb function -> assign it as onclick
31        actionFunc = 'tb_'+tb[i]['type'];
32        if( isFunction(window[actionFunc]) ){
33            addEvent(btn,'click', function(func,btn, props, edid){
34                return function(){
35                    window[func](btn, props, edid);
36                    return false;
37                }
38            }(actionFunc,btn,tb[i],edid) );
39            //above fixes the scope problem as descried at http://www.mennovanslooten.nl/blog/post/62
40            toolbar.appendChild(btn);
41            continue;
42        }
43
44        // type is a init function -> execute it
45        actionFunc = 'addBtnAction'+tb[i]['type'].charAt(0).toUpperCase()+tb[i]['type'].substring(1);
46        if( isFunction(window[actionFunc]) ){
47            if(window[actionFunc](btn, tb[i], edid)){
48                toolbar.appendChild(btn);
49            }
50            continue;
51        }
52
53        alert('unknown toolbar type: '+tb[i]['type']+'  '+actionFunc);
54    } // end for
55
56}
57
58/**
59 * Button action for format buttons
60 *
61 * @param  DOMElement btn   Button element to add the action to
62 * @param  array      props Associative array of button properties
63 * @param  string     edid  ID of the editor textarea
64 * @author Gabriel Birke <birke@d-scribe.de>
65 * @author Andreas Gohr <andi@splitbrain.org>
66 */
67function tb_format(btn, props, edid) {
68    var sample = props['title'];
69    if(props['sample']){
70        sample = props['sample'];
71    }
72    insertTags(edid,
73               fixtxt(props['open']),
74               fixtxt(props['close']),
75               fixtxt(sample));
76    pickerClose();
77    return false;
78}
79
80/**
81 * Button action for insert buttons
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_insert(btn, props, edid) {
90    insertAtCarret(edid,fixtxt(props['insert']));
91    pickerClose();
92}
93
94
95/**
96 * Add button action for picker buttons and create picker element
97 *
98 * @param  DOMElement btn   Button element to add the action to
99 * @param  array      props Associative array of button properties
100 * @param  string     edid  ID of the editor textarea
101 * @return boolean    If button should be appended
102 * @author Gabriel Birke <birke@d-scribe.de>
103 */
104function addBtnActionPicker(btn, props, edid) {
105    var pickerid = 'picker'+(pickercounter++);
106    createPicker(pickerid, props, edid);
107    addEvent(btn,'click',function(){
108        pickerToggle(pickerid,btn);
109        return false;
110    });
111    return true;
112}
113
114/**
115 * Show/Hide a previosly created picker window
116 *
117 * @author Andreas Gohr <andi@splitbrain.org>
118 */
119function pickerToggle(pickerid,btn){
120    var picker = $(pickerid);
121    if(picker.style.display == 'none'){
122        var x = findPosX(btn);
123        var y = findPosY(btn);
124        picker.style.display = 'block';
125        picker.style.left = (x+3)+'px';
126        picker.style.top = (y+btn.offsetHeight+3)+'px';
127    }else{
128        picker.style.display = 'none';
129    }
130}
131
132/**
133 * Close all open pickers
134 *
135 * @author Andreas Gohr <andi@splitbrain.org>
136 */
137function pickerClose(){
138    var pobjs = getElementsByClass('picker');
139    for(var i=0; i<pobjs.length; i++){
140        pobjs[i].style.display = 'none';
141    }
142}
143
144
145/**
146 * Replaces \n with linebreaks
147 */
148function fixtxt(str){
149    return str.replace(/\\n/g,"\n");
150}
151
152