1 // used to identify pickers
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>
13 function initToolbar(tbid,edid,tb, allowblock){
15 if (typeof tbid == 'string') {
16 $toolbar = jQuery('#' + tbid);
18 $toolbar = jQuery(tbid);
21 $edit = jQuery('#' + edid);
23 if ($toolbar.length == 0 || $edit.length == 0 || $edit.attr('readOnly')) {
27 if (typeof allowblock === 'undefined') {
31 //empty the toolbar area:
34 jQuery.each(tb, function (k, val) {
35 if (!tb.hasOwnProperty(k) || (!allowblock && val.block === true)) {
40 // create new button (jQuery object)
41 $btn = jQuery(createToolButton(val.icon, val.title, val.key, val.id,
44 // type is a tb function -> assign it as onclick
45 actionFunc = 'tb_'+val.type;
46 if( jQuery.isFunction(window[actionFunc]) ){
47 $btn.on('click', bind(window[actionFunc],$btn,val,edid) );
48 $toolbar.append($btn);
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 var pickerid = window[actionFunc]($btn, val, edid);
57 $toolbar.append($btn);
58 $btn.attr('aria-controls', pickerid);
59 if (actionFunc === 'addBtnActionPicker') {
60 $btn.attr('aria-haspopup', 'true');
66 alert('unknown toolbar type: '+val.type+' '+actionFunc);
71 * Button action for format buttons
73 * @param DOMElement btn Button element to add the action to
74 * @param array props Associative array of button properties
75 * @param string edid ID of the editor textarea
76 * @author Gabriel Birke <birke@d-scribe.de>
77 * @author Andreas Gohr <andi@splitbrain.org>
79 function tb_format(btn, props, edid) {
80 var sample = props.sample || props.title;
90 * Button action for format buttons
92 * This works exactly as tb_format() except that, if multiple lines
93 * are selected, each line will be formatted seperately
95 * @param DOMElement btn Button element to add the action to
96 * @param array props Associative array of button properties
97 * @param string edid ID of the editor textarea
98 * @author Gabriel Birke <birke@d-scribe.de>
99 * @author Andreas Gohr <andi@splitbrain.org>
101 function tb_formatln(btn, props, edid) {
102 var sample = props.sample || props.title,
104 selection = DWgetSelection(jQuery('#'+edid)[0]);
106 sample = fixtxt(sample);
107 props.open = fixtxt(props.open);
108 props.close = fixtxt(props.close);
110 // is something selected?
111 if(selection.getLength()){
112 sample = selection.getText();
113 opts = {nosel: true};
116 startofs: props.open.length,
117 endofs: props.close.length
121 sample = sample.split("\n").join(props.close+"\n"+props.open);
122 sample = props.open+sample+props.close;
124 pasteText(selection,sample,opts);
131 * Button action for insert buttons
133 * @param DOMElement btn Button element to add the action to
134 * @param array props Associative array of button properties
135 * @param string edid ID of the editor textarea
136 * @author Gabriel Birke <birke@d-scribe.de>
137 * @author Andreas Gohr <andi@splitbrain.org>
139 function tb_insert(btn, props, edid) {
140 insertAtCarret(edid,fixtxt(props.insert));
146 * Button action for the media popup
148 * @param DOMElement btn Button element to add the action to
149 * @param array props Associative array of button properties
150 * @param string edid ID of the editor textarea
151 * @author Andreas Gohr <andi@splitbrain.org>
153 function tb_mediapopup(btn, props, edid) {
155 DOKU_BASE+props.url+encodeURIComponent(NS)+'&edid='+encodeURIComponent(edid),
162 * Button action for automatic headlines
164 * Insert a new headline based on the current section level
166 * @param DOMElement btn Button element to add the action to
167 * @param array props Associative array of button properties
168 * @param string edid ID of the editor textarea
169 * @author Andreas Gohr <andi@splitbrain.org>
171 function tb_autohead(btn, props, edid){
172 var lvl = currentHeadlineLevel(edid),
175 // determine new level
180 tags = (new Array(8 - lvl)).join('=');
181 insertTags(edid, tags+' ', ' '+tags+"\n", props.text);
188 * Add button action for picker buttons and create picker element
190 * @param jQuery btn Button element to add the action to
191 * @param array props Associative array of button properties
192 * @param string edid ID of the editor textarea
193 * @return boolean If button should be appended
194 * @author Gabriel Birke <birke@d-scribe.de>
196 function addBtnActionPicker($btn, props, edid) {
197 var pickerid = 'picker'+(pickercounter++);
198 var picker = createPicker(pickerid, props, edid);
199 jQuery(picker).attr('aria-hidden', 'true');
203 pickerToggle(pickerid,$btn);
213 * Add button action for the link wizard button
215 * @param DOMElement btn Button element to add the action to
216 * @param array props Associative array of button properties
217 * @param string edid ID of the editor textarea
218 * @return boolean If button should be appended
219 * @author Andreas Gohr <gohr@cosmocode.de>
221 function addBtnActionLinkwiz($btn, props, edid) {
222 dw_linkwiz.init(jQuery('#'+edid));
223 jQuery($btn).click(function(e){
224 dw_linkwiz.val = props;
234 * Show/Hide a previously created picker window
236 * @author Andreas Gohr <andi@splitbrain.org>
238 function pickerToggle(pickerid,$btn){
239 var $picker = jQuery('#' + pickerid),
241 if ($picker.hasClass('a11y')) {
242 $picker.removeClass('a11y').attr('aria-hidden', 'false');
244 $picker.addClass('a11y').attr('aria-hidden', 'true');
246 var picker_left = pos.left + 3,
247 picker_width = $picker.width(),
248 window_width = jQuery(window).width();
249 if (picker_width > 300) {
250 $picker.css("max-width", "300");
253 if ((picker_left + picker_width + 40) > window_width) {
254 picker_left = window_width - picker_width - 40;
256 if (picker_left < 0) {
259 $picker.offset({left: picker_left, top: pos.top+$btn[0].offsetHeight+3});
263 * Close all open pickers
265 * @author Andreas Gohr <andi@splitbrain.org>
267 function pickerClose(){
268 jQuery('.picker').addClass('a11y');
273 * Replaces \n with linebreaks
275 function fixtxt(str){
276 return str.replace(/\\n/g,"\n");
280 initToolbar('tool__bar','wiki__text',toolbar);
281 jQuery('#tool__bar').attr('role', 'toolbar');