xref: /dokuwiki/lib/scripts/toolbar.js (revision 0071aa2162e87ac729531c1c625d9bfb31f2adec)
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 format buttons
82 *
83 * This works exactly as tb_format() except that, if multiple lines
84 * are selected, each line will be formatted seperately
85 *
86 * @param  DOMElement btn   Button element to add the action to
87 * @param  array      props Associative array of button properties
88 * @param  string     edid  ID of the editor textarea
89 * @author Gabriel Birke <birke@d-scribe.de>
90 * @author Andreas Gohr <andi@splitbrain.org>
91 */
92function tb_formatln(btn, props, edid) {
93    var sample = props['title'];
94    if(props['sample']){
95        sample = props['sample'];
96    }
97    sample = fixtxt(sample);
98
99    var selection = getSelection($(edid));
100    if(selection.getLength()) sample = selection.getText();
101
102    props['open']  = fixtxt(props['open']);
103    props['close'] = fixtxt(props['close']);
104
105    sample = sample.split("\n").join(props['close']+"\n"+props['open']);
106    sample = props['open']+sample+props['close'];
107
108    pasteText(selection,sample,{nosel: true});
109
110    pickerClose();
111    return false;
112}
113
114/**
115 * Button action for insert buttons
116 *
117 * @param  DOMElement btn   Button element to add the action to
118 * @param  array      props Associative array of button properties
119 * @param  string     edid  ID of the editor textarea
120 * @author Gabriel Birke <birke@d-scribe.de>
121 * @author Andreas Gohr <andi@splitbrain.org>
122 */
123function tb_insert(btn, props, edid) {
124    insertAtCarret(edid,fixtxt(props['insert']));
125    pickerClose();
126}
127
128
129/**
130 * Add button action for picker buttons and create picker element
131 *
132 * @param  DOMElement btn   Button element to add the action to
133 * @param  array      props Associative array of button properties
134 * @param  string     edid  ID of the editor textarea
135 * @return boolean    If button should be appended
136 * @author Gabriel Birke <birke@d-scribe.de>
137 */
138function addBtnActionPicker(btn, props, edid) {
139    var pickerid = 'picker'+(pickercounter++);
140    createPicker(pickerid, props, edid);
141    addEvent(btn,'click',function(){
142        pickerToggle(pickerid,btn);
143        return false;
144    });
145    return true;
146}
147
148function addBtnActionLinkwiz(btn, props, edid) {
149    linkwiz.init($(edid));
150    addEvent(btn,'click',function(){
151        linkwiz.toggle();
152        return false;
153    });
154    return true;
155}
156
157
158/**
159 * Show/Hide a previosly created picker window
160 *
161 * @author Andreas Gohr <andi@splitbrain.org>
162 */
163function pickerToggle(pickerid,btn){
164    var picker = $(pickerid);
165    if(picker.style.display == 'none'){
166        var x = findPosX(btn);
167        var y = findPosY(btn);
168        picker.style.display = 'block';
169        picker.style.left = (x+3)+'px';
170        picker.style.top = (y+btn.offsetHeight+3)+'px';
171    }else{
172        picker.style.display = 'none';
173    }
174}
175
176/**
177 * Close all open pickers
178 *
179 * @author Andreas Gohr <andi@splitbrain.org>
180 */
181function pickerClose(){
182    var pobjs = getElementsByClass('picker');
183    for(var i=0; i<pobjs.length; i++){
184        pobjs[i].style.display = 'none';
185    }
186}
187
188
189/**
190 * Replaces \n with linebreaks
191 */
192function fixtxt(str){
193    return str.replace(/\\n/g,"\n");
194}
195
196