diff options
Diffstat (limited to 'lib/scripts/edit.js')
-rw-r--r-- | lib/scripts/edit.js | 222 |
1 files changed, 103 insertions, 119 deletions
diff --git a/lib/scripts/edit.js b/lib/scripts/edit.js index 816568e92..33a8f61b5 100644 --- a/lib/scripts/edit.js +++ b/lib/scripts/edit.js @@ -14,38 +14,36 @@ * @author Michal Rezler <m.rezler@centrum.cz> */ function createToolButton(icon,label,key,id,classname){ - var $ = jQuery; - var btn = $('<button>'); - var ico = $('<img />'); + var $btn = jQuery(document.createElement('button')), + $ico = jQuery(document.createElement('img')); - // preapare the basic button stuff - btn.attr('class', 'toolbutton'); + // prepare the basic button stuff + $btn.addClass('toolbutton'); if(classname){ - btn.attr('class', 'toolbutton '+classname); + $btn.addClass(classname); } - btn.attr('title', label); + $btn.attr('title', label); if(key){ - btn.attr('title', label + ' ['+key.toUpperCase()+']') + $btn.attr('title', label + ' ['+key.toUpperCase()+']') .attr('accessKey', key); } // set IDs if given if(id){ - btn.attr('id', id); - ico.attr('id', id+'_ico'); + $btn.attr('id', id); + $ico.attr('id', id+'_ico'); } // create the icon and add it to the button - if(icon.substr(0,1) == '/'){ - ico.attr('src', icon); - }else{ - ico.attr('src', DOKU_BASE+'lib/images/toolbar/'+icon); + if(icon.substr(0,1) !== '/'){ + icon = DOKU_BASE + 'lib/images/toolbar/' + icon; } - btn.append(ico); + $ico.attr('src', icon); + $btn.append($ico); - // we have to return a javascript object (for compatibility reasons) - return btn[0]; + // we have to return a DOM object (for compatibility reasons) + return $btn[0]; } /** @@ -63,69 +61,51 @@ function createToolButton(icon,label,key,id,classname){ * @author Andreas Gohr <andi@splitbrain.org> */ function createPicker(id,props,edid){ - var icobase = props['icobase']; - var list = props['list']; - var $ = jQuery; - // create the wrapping div - var picker = $('<div></div>'); + var $picker = jQuery(document.createElement('div')); - var className = 'picker'; + $picker.addClass('picker a11y'); if(props['class']){ - className += ' '+props['class']; + $picker.addClass(props['class']); } - picker.attr('class', className) - .attr('id', id) - .css('position', 'absolute') - .css('marginLeft', '-10000px') // no display:none, to keep access keys working - .css('marginTop', '-10000px'); + $picker.attr('id', id).css('position', 'absolute'); + + function $makebutton(title) { + var $btn = jQuery(document.createElement('button')) + .addClass('pickerbutton').attr('title', title) + .bind('click', bind(pickerInsert, title, edid)) + .appendTo($picker); + return $btn; + } - for(var key in list){ - if (!list.hasOwnProperty(key)) continue; + jQuery.each(props.list, function (key, item) { + if (!props.list.hasOwnProperty(key)) { + return; + } if(isNaN(key)){ - // associative array -> treat as image/value pairs - var btn = $('<button>'); - btn.attr('class', 'pickerbutton') - .attr('title', key); - - var ico = $('<img>'); - if (list[key].substr(0,1) == '/') { - var src = list[key]; - } else { - var src = DOKU_BASE+'lib/images/'+icobase+'/'+list[key]; + // associative array -> treat as text => image pairs + if (item.substr(0,1) !== '/') { + item = DOKU_BASE+'lib/images/'+props.icobase+'/'+item; } - - ico.attr('src', src); - btn.append(ico); - - btn.bind('click', bind(pickerInsert, key, edid)); - picker.append(btn); - }else if (typeof (list[key]) == 'string'){ + jQuery(document.createElement('img')) + .attr('src', item) + .appendTo($makebutton(key)); + }else if (typeof item == 'string'){ // a list of text -> treat as text picker - var btn = $('<button>'); - btn.attr('class', 'pickerbutton') - .attr('title', list[key]); - - var txt = $(document.createTextNode(list[key])); - btn.append(txt); - - btn.bind('click', bind(pickerInsert, list[key], edid)); - - picker.append(btn); + $makebutton(item).text(item); }else{ // a list of lists -> treat it as subtoolbar - initToolbar(picker,edid,list); - break; // all buttons handled already + initToolbar($picker,edid,props.list); + return false; // all buttons handled already } - } - var body = $('body'); - body.append(picker); + }); + jQuery('body').append($picker); - // we have to return a javascript object (for compatibility reasons) - return picker[0]; + // we have to return a DOM object (for compatibility reasons) + return $picker[0]; } /** @@ -147,9 +127,9 @@ function pickerInsert(text,edid){ * @return boolean If button should be appended * @author Gabriel Birke <birke@d-scribe.de> */ -function addBtnActionSignature(btn, props, edid) { - if(typeof(SIG) != 'undefined' && SIG != ''){ - btn.bind('click', bind(insertAtCarret,edid,SIG)); +function addBtnActionSignature($btn, props, edid) { + if(typeof SIG != 'undefined' && SIG != ''){ + $btn.bind('click', bind(insertAtCarret,edid,SIG)); return true; } return false; @@ -161,24 +141,27 @@ function addBtnActionSignature(btn, props, edid) { * @author Andreas Gohr <gohr@cosmocode.de> */ function currentHeadlineLevel(textboxId){ - var field = $(textboxId); - var selection = getSelection(field); - var search = "\n"+field.value.substr(0,selection.start); - var lasthl = search.lastIndexOf("\n=="); - if(lasthl == -1 && field.form.prefix){ + var field = jQuery('#' + textboxId)[0], + s = false, + opts = [field.value.substr(0,getSelection(field).start)]; + if (field.form.prefix) { // we need to look in prefix context - search = field.form.prefix.value; - lasthl = search.lastIndexOf("\n=="); + opts.push(field.form.prefix.value); } - search = search.substr(lasthl+1,6); - if(search == '======') return 1; - if(search.substr(0,5) == '=====') return 2; - if(search.substr(0,4) == '====') return 3; - if(search.substr(0,3) == '===') return 4; - if(search.substr(0,2) == '==') return 5; - - return 0; + jQuery.each(opts, function (_, opt) { + // Check whether there is a headline in the given string + var str = "\n" + opt, + lasthl = str.lastIndexOf("\n=="); + if (lasthl !== -1) { + s = str.substr(lasthl+1,6); + return false; + } + }); + if (s === false) { + return 0; + } + return 7 - s.match(/^={2,6}/)[0].length; } @@ -191,21 +174,23 @@ window.textChanged = false; * Delete the draft before leaving the page */ function deleteDraft() { - if (is_opera) return; - if (window.keepDraft) return; + if (is_opera || window.keepDraft) { + return; + } - // remove a possibly saved draft using ajax - var dwform = jQuery('#dw__editform'); - if(dwform.length != 0) { - - jQuery.post( - DOKU_BASE + 'lib/exe/ajax.php', - { - call: 'draftdel', - id: jQuery('#dw__editform input[name=id]').val() - } - ); + var $dwform = jQuery('#dw__editform'); + + if($dwform.length === 0) { + return; } + + // remove a possibly saved draft using ajax + jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', + { + call: 'draftdel', + id: $dwform.find('input[name=id]').val() + } + ); } /** @@ -214,21 +199,24 @@ function deleteDraft() { * * Sets focus to the editbox as well */ -addInitEvent(function () { - var $ = jQuery; - var editform = $('#dw__editform'); - if (editform.length == 0) return; +jQuery(function () { + var $editform = jQuery('#dw__editform'); + if ($editform.length == 0) { + return; + } - var edit_text = $('#wiki__text'); - if (edit_text.length > 0) { - if(edit_text.attr('readOnly')) return; + var $edit_text = jQuery('#wiki__text'); + if ($edit_text.length > 0) { + if($edit_text.attr('readOnly')) { + return; + } // set focus and place cursor at the start - var sel = getSelection(edit_text.get(0)); + var sel = getSelection($edit_text[0]); sel.start = 0; sel.end = 0; setSelection(sel); - edit_text.focus(); + $edit_text.focus(); } var checkfunc = function() { @@ -236,8 +224,8 @@ addInitEvent(function () { summaryCheck(); }; - editform.change(checkfunc); - editform.keydown(checkfunc); + $editform.change(checkfunc); + $editform.keydown(checkfunc); window.onbeforeunload = function(){ if(window.textChanged) { @@ -247,13 +235,13 @@ addInitEvent(function () { window.onunload = deleteDraft; // reset change memory var on submit - $('#edbtn__save').click( + jQuery('#edbtn__save').click( function() { window.onbeforeunload = ''; textChanged = false; } ); - $('#edbtn__preview').click( + jQuery('#edbtn__preview').click( function() { window.onbeforeunload = ''; textChanged = false; @@ -261,9 +249,9 @@ addInitEvent(function () { } ); - var summary = $('#edit__summary'); - summary.change(summaryCheck); - summary.keyup(summaryCheck); + var $summary = jQuery('#edit__summary'); + $summary.change(summaryCheck); + $summary.keyup(summaryCheck); if (textChanged) summaryCheck(); }); @@ -274,11 +262,7 @@ addInitEvent(function () { * @author Andreas Gohr <andi@splitbrain.org> */ function summaryCheck(){ - var sum = jQuery('#edit__summary'); - - if (sum.val() === '') { - sum.attr('class', 'missing'); - } else{ - sum.attr('class', 'edit'); - } + var $sum = jQuery('#edit__summary'), + missing = $sum.val() === ''; + $sum.toggleClass('missing', missing).toggleClass('edit', !missing); } |