xref: /dokuwiki/lib/scripts/toolbar.js (revision 3c2d94a305b6f0f97a947d09e805676bcd733090)
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    props['open']  = fixtxt(props['open']);
100    props['close'] = fixtxt(props['close']);
101
102    // is something selected?
103    var opts;
104    var selection = getSelection($(edid));
105    if(selection.getLength()){
106        sample = selection.getText();
107        opts = {nosel: true};
108    }else{
109        opts = {
110            startofs: props['open'].length,
111            endofs: props['close'].length
112        };
113    }
114
115    sample = sample.split("\n").join(props['close']+"\n"+props['open']);
116    sample = props['open']+sample+props['close'];
117
118    pasteText(selection,sample,opts);
119
120    pickerClose();
121    return false;
122}
123
124/**
125 * Button action for insert buttons
126 *
127 * @param  DOMElement btn   Button element to add the action to
128 * @param  array      props Associative array of button properties
129 * @param  string     edid  ID of the editor textarea
130 * @author Gabriel Birke <birke@d-scribe.de>
131 * @author Andreas Gohr <andi@splitbrain.org>
132 */
133function tb_insert(btn, props, edid) {
134    insertAtCarret(edid,fixtxt(props['insert']));
135    pickerClose();
136}
137
138
139/**
140 * Add button action for picker buttons and create picker element
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 * @return boolean    If button should be appended
146 * @author Gabriel Birke <birke@d-scribe.de>
147 */
148function addBtnActionPicker(btn, props, edid) {
149    var pickerid = 'picker'+(pickercounter++);
150    createPicker(pickerid, props, edid);
151    addEvent(btn,'click',function(){
152        pickerToggle(pickerid,btn);
153        return false;
154    });
155    return true;
156}
157
158function addBtnActionLinkwiz(btn, props, edid) {
159    linkwiz.init($(edid));
160    addEvent(btn,'click',function(){
161        linkwiz.toggle();
162        return false;
163    });
164    return true;
165}
166
167
168/**
169 * Show/Hide a previosly created picker window
170 *
171 * @author Andreas Gohr <andi@splitbrain.org>
172 */
173function pickerToggle(pickerid,btn){
174    var picker = $(pickerid);
175    if(picker.style.display == 'none'){
176        var x = findPosX(btn);
177        var y = findPosY(btn);
178        picker.style.display = 'block';
179        picker.style.left = (x+3)+'px';
180        picker.style.top = (y+btn.offsetHeight+3)+'px';
181    }else{
182        picker.style.display = 'none';
183    }
184}
185
186/**
187 * Close all open pickers
188 *
189 * @author Andreas Gohr <andi@splitbrain.org>
190 */
191function pickerClose(){
192    var pobjs = getElementsByClass('picker');
193    for(var i=0; i<pobjs.length; i++){
194        pobjs[i].style.display = 'none';
195    }
196}
197
198
199/**
200 * Replaces \n with linebreaks
201 */
202function fixtxt(str){
203    return str.replace(/\\n/g,"\n");
204}
205
206