summaryrefslogtreecommitdiff
path: root/lib/scripts/edit.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/scripts/edit.js')
-rw-r--r--lib/scripts/edit.js337
1 files changed, 125 insertions, 212 deletions
diff --git a/lib/scripts/edit.js b/lib/scripts/edit.js
index a96a346dc..33a8f61b5 100644
--- a/lib/scripts/edit.js
+++ b/lib/scripts/edit.js
@@ -11,37 +11,39 @@
* Style the buttons through the toolbutton class
*
* @author Andreas Gohr <andi@splitbrain.org>
+ * @author Michal Rezler <m.rezler@centrum.cz>
*/
function createToolButton(icon,label,key,id,classname){
- var btn = document.createElement('button');
- var ico = document.createElement('img');
+ var $btn = jQuery(document.createElement('button')),
+ $ico = jQuery(document.createElement('img'));
- // preapare the basic button stuff
- btn.className = 'toolbutton';
+ // prepare the basic button stuff
+ $btn.addClass('toolbutton');
if(classname){
- btn.className += ' '+classname;
+ $btn.addClass(classname);
}
- btn.title = label;
+
+ $btn.attr('title', label);
if(key){
- btn.title += ' ['+key.toUpperCase()+']';
- btn.accessKey = key;
+ $btn.attr('title', label + ' ['+key.toUpperCase()+']')
+ .attr('accessKey', key);
}
// set IDs if given
if(id){
- btn.id = id;
- ico.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.src = icon;
- }else{
- ico.src = DOKU_BASE+'lib/images/toolbar/'+icon;
+ if(icon.substr(0,1) !== '/'){
+ icon = DOKU_BASE + 'lib/images/toolbar/' + icon;
}
- btn.appendChild(ico);
+ $ico.attr('src', icon);
+ $btn.append($ico);
- return btn;
+ // we have to return a DOM object (for compatibility reasons)
+ return $btn[0];
}
/**
@@ -59,56 +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'];
-
// create the wrapping div
- var picker = document.createElement('div');
- picker.className = 'picker';
+ var $picker = jQuery(document.createElement('div'));
+
+ $picker.addClass('picker a11y');
if(props['class']){
- picker.className += ' '+props['class'];
+ $picker.addClass(props['class']);
}
- picker.id = id;
- picker.style.position = 'absolute';
- picker.style.marginLeft = '-10000px'; // no display:none, to keep access keys working
- picker.style.marginTop = '-10000px';
- for(var key in list){
- if (!list.hasOwnProperty(key)) continue;
+ $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;
+ }
+
+ 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 = document.createElement('button');
- btn.className = 'pickerbutton';
- var ico = document.createElement('img');
- if(list[key].substr(0,1) == '/'){
- ico.src = list[key];
- }else{
- ico.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;
}
- btn.title = key;
- btn.appendChild(ico);
- addEvent(btn,'click',bind(pickerInsert,key,edid));
- picker.appendChild(btn);
- }else if(isString(list[key])){
+ 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 = document.createElement('button');
- btn.className = 'pickerbutton';
- var txt = document.createTextNode(list[key]);
- btn.title = list[key];
- btn.appendChild(txt);
- addEvent(btn,'click',bind(pickerInsert,list[key],edid));
- picker.appendChild(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 = document.getElementsByTagName('body')[0];
- body.appendChild(picker);
- return picker;
+ });
+ jQuery('body').append($picker);
+
+ // we have to return a DOM object (for compatibility reasons)
+ return $picker[0];
}
/**
@@ -130,131 +127,41 @@ 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 != ''){
- addEvent(btn,'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;
}
/**
- * Make intended formattings easier to handle
- *
- * Listens to all key inputs and handle indentions
- * of lists and code blocks
- *
- * Currently handles space, backspce and enter presses
- *
- * @author Andreas Gohr <andi@splitbrain.org>
- * @fixme handle tabs
- */
-function keyHandler(e){
- if(e.keyCode != 13 &&
- e.keyCode != 8 &&
- e.keyCode != 32) return;
- var field = e.target;
- var selection = getSelection(field);
- if(selection.getLength()) return; //there was text selected, keep standard behavior
- var search = "\n"+field.value.substr(0,selection.start);
- var linestart = Math.max(search.lastIndexOf("\n"),
- search.lastIndexOf("\r")); //IE workaround
- search = search.substr(linestart);
-
-
- if(e.keyCode == 13){ // Enter
- // keep current indention for lists and code
- var match = search.match(/(\n +([\*-] ?)?)/);
- if(match){
- var scroll = field.scrollHeight;
- var match2 = search.match(/^\n +[\*-]\s*$/);
- // Cancel list if the last item is empty (i. e. two times enter)
- if (match2 && field.value.substr(selection.start).match(/^($|\r?\n)/)) {
- field.value = field.value.substr(0, linestart) + "\n" +
- field.value.substr(selection.start);
- selection.start = linestart + 1;
- selection.end = linestart + 1;
- setSelection(selection);
- } else {
- insertAtCarret(field.id,match[1]);
- }
- field.scrollTop += (field.scrollHeight - scroll);
- e.preventDefault(); // prevent enter key
- return false;
- }
- }else if(e.keyCode == 8){ // Backspace
- // unindent lists
- var match = search.match(/(\n +)([*-] ?)$/);
- if(match){
- var spaces = match[1].length-1;
-
- if(spaces > 3){ // unindent one level
- field.value = field.value.substr(0,linestart)+
- field.value.substr(linestart+2);
- selection.start = selection.start - 2;
- selection.end = selection.start;
- }else{ // delete list point
- field.value = field.value.substr(0,linestart)+
- field.value.substr(selection.start);
- selection.start = linestart;
- selection.end = linestart;
- }
- setSelection(selection);
- e.preventDefault(); // prevent backspace
- return false;
- }
- }else if(e.keyCode == 32){ // Space
- // intend list item
- var match = search.match(/(\n +)([*-] )$/);
- if(match){
- field.value = field.value.substr(0,linestart)+' '+
- field.value.substr(linestart);
- selection.start = selection.start + 2;
- selection.end = selection.start;
- setSelection(selection);
- e.preventDefault(); // prevent space
- return false;
- }
- }
-}
-
-//FIXME consolidate somewhere else
-addInitEvent(function(){
- var field = $('wiki__text');
- if(!field) return;
- // in Firefox, keypress doesn't send the correct keycodes,
- // in Opera, the default of keydown can't be prevented
- if (is_opera) {
- addEvent(field,'keypress',keyHandler);
- } else {
- addEvent(field,'keydown',keyHandler);
- }
-});
-
-/**
* Determine the current section level while editing
*
* @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;
}
@@ -267,23 +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 = $('dw__editform');
- if(dwform){
- var params = 'call=draftdel';
- params += '&id='+encodeURIComponent(dwform.elements.id.value);
-
- var sackobj = new sack(DOKU_BASE + 'lib/exe/ajax.php');
- // this needs to be synchronous and GET to not be aborted upon page unload
- sackobj.asynchronous = false;
- sackobj.method = 'GET';
- sackobj.AjaxFailedAlert = '';
- sackobj.encodeURIString = false;
- sackobj.runAJAX(params);
+ 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()
+ }
+ );
}
/**
@@ -292,28 +199,33 @@ function deleteDraft() {
*
* Sets focus to the editbox as well
*/
-addInitEvent(function (){
- var editform = $('dw__editform');
- if (!editform) return;
+jQuery(function () {
+ var $editform = jQuery('#dw__editform');
+ if ($editform.length == 0) {
+ return;
+ }
- var edit_text = $('wiki__text');
- if(edit_text) {
- if(edit_text.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);
+ var sel = getSelection($edit_text[0]);
sel.start = 0;
sel.end = 0;
setSelection(sel);
- edit_text.focus();
+ $edit_text.focus();
}
- var checkfunc = function(){
- window.textChanged = true; //global var
+ var checkfunc = function() {
+ textChanged = true; //global var
summaryCheck();
};
- addEvent(editform, 'change', checkfunc);
- addEvent(editform, 'keydown', checkfunc);
+
+ $editform.change(checkfunc);
+ $editform.keydown(checkfunc);
window.onbeforeunload = function(){
if(window.textChanged) {
@@ -323,20 +235,25 @@ addInitEvent(function (){
window.onunload = deleteDraft;
// reset change memory var on submit
- addEvent($('edbtn__save'), 'click', function(){
- window.onbeforeunload = '';
- window.textChanged = false;
- });
- addEvent($('edbtn__preview'), 'click', function(){
- window.onbeforeunload = '';
- window.textChanged = false;
- window.keepDraft = true; // needed to keep draft on page unload
- });
+ jQuery('#edbtn__save').click(
+ function() {
+ window.onbeforeunload = '';
+ textChanged = false;
+ }
+ );
+ jQuery('#edbtn__preview').click(
+ function() {
+ window.onbeforeunload = '';
+ textChanged = false;
+ window.keepDraft = true; // needed to keep draft on page unload
+ }
+ );
- var summary = $('edit__summary');
- addEvent(summary, 'change', summaryCheck);
- addEvent(summary, 'keyup', summaryCheck);
- if (window.textChanged) summaryCheck();
+ var $summary = jQuery('#edit__summary');
+ $summary.change(summaryCheck);
+ $summary.keyup(summaryCheck);
+
+ if (textChanged) summaryCheck();
});
/**
@@ -345,11 +262,7 @@ addInitEvent(function (){
* @author Andreas Gohr <andi@splitbrain.org>
*/
function summaryCheck(){
- var sum = document.getElementById('edit__summary');
- if(sum.value === ''){
- sum.className='missing';
- }else{
- sum.className='edit';
- }
+ var $sum = jQuery('#edit__summary'),
+ missing = $sum.val() === '';
+ $sum.toggleClass('missing', missing).toggleClass('edit', !missing);
}
-