summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/scripts/edit.js73
-rw-r--r--lib/scripts/events.js19
-rw-r--r--lib/scripts/toolbar.js50
3 files changed, 66 insertions, 76 deletions
diff --git a/lib/scripts/edit.js b/lib/scripts/edit.js
index 86ebb230d..3b1ca8834 100644
--- a/lib/scripts/edit.js
+++ b/lib/scripts/edit.js
@@ -67,7 +67,7 @@ function createPicker(id,props,edid){
}
picker.id = id;
picker.style.position = 'absolute';
- picker.style.marginLeft = '-10000px'; // no display none, to keep access keys working
+ picker.style.marginLeft = '-10000px'; // no display:none, to keep access keys working
for(var key in list){
if (!list.hasOwnProperty(key)) continue;
@@ -84,9 +84,7 @@ function createPicker(id,props,edid){
}
btn.title = key;
btn.appendChild(ico);
- eval("btn.onclick = function(){pickerInsert('"+
- jsEscape(key)+"','"+
- jsEscape(edid)+"');return false;}");
+ addEvent(btn,'click',bind(pickerInsert,key,edid));
picker.appendChild(btn);
}else if(isString(list[key])){
// a list of text -> treat as text picker
@@ -95,9 +93,7 @@ function createPicker(id,props,edid){
var txt = document.createTextNode(list[key]);
btn.title = list[key];
btn.appendChild(txt);
- eval("btn.onclick = function(){pickerInsert('"+
- jsEscape(list[key])+"','"+
- jsEscape(edid)+"');return false;}");
+ addEvent(btn,'click',bind(pickerInsert,list[key],edid));
picker.appendChild(btn);
}else{
// a list of lists -> treat it as subtoolbar
@@ -117,9 +113,7 @@ function createPicker(id,props,edid){
* @author Andreas Gohr <andi@splitbrain.org>
*/
function pickerInsert(text,edid){
- // insert
insertAtCarret(edid,text);
- // close picker
pickerClose();
}
@@ -132,51 +126,15 @@ function pickerInsert(text,edid){
* @return boolean If button should be appended
* @author Gabriel Birke <birke@d-scribe.de>
*/
-function addBtnActionSignature(btn, props, edid)
-{
+function addBtnActionSignature(btn, props, edid) {
if(typeof(SIG) != 'undefined' && SIG != ''){
- eval("btn.onclick = function(){insertAtCarret('"+
- jsEscape(edid)+"','"+
- jsEscape(SIG)+
- "');return false;}");
+ addEvent(btn,'click',bind(insertAtCarret,edid,SIG));
return true;
}
return false;
}
/**
- * Add button action for the mediapopup button
- *
- * @param DOMElement btn Button element to add the action to
- * @param array props Associative array of button properties
- * @return boolean If button should be appended
- * @author Gabriel Birke <birke@d-scribe.de>
- */
-function addBtnActionMediapopup(btn, props) {
- eval("btn.onclick = function(){window.open('"+DOKU_BASE+
- jsEscape(props['url']+encodeURIComponent(NS))+"','"+
- jsEscape(props['name'])+"','"+
- jsEscape(props['options'])+
- "');return false;}");
- return true;
-}
-
-/**
- * Add button action for the headline buttons
- *
- * @param DOMElement btn Button element to add the action to
- * @param array props Associative array of button properties
- * @return boolean If button should be appended
- * @author Andreas Gohr <gohr@cosmocode.de>
- */
-function addBtnActionAutohead(btn, props, edid, id) {
- eval("btn.onclick = function(){"+
- "insertHeadline('"+edid+"',"+props['mod']+",'"+jsEscape(props['text'])+"'); "+
- "return false};");
- return true;
-}
-
-/**
* Make intended formattings easier to handle
*
* Listens to all key inputs and handle indentions
@@ -272,27 +230,6 @@ function currentHeadlineLevel(textboxId){
return 0;
}
-/**
- * Insert a new headline based on the current section level
- *
- * @param string textboxId - the edit field ID
- * @param int mod - the headline modificator ( -1, 0, 1)
- * @param string text - the sample text passed to insertTags
- */
-function insertHeadline(textboxId,mod,text){
- var lvl = currentHeadlineLevel(textboxId);
-
-
- // determine new level
- lvl += mod;
- if(lvl < 1) lvl = 1;
- if(lvl > 5) lvl = 5;
-
- var tags = '=';
- for(var i=0; i<=5-lvl; i++) tags += '=';
- insertTags(textboxId, tags+' ', ' '+tags+"\n", text);
- pickerClose();
-}
/**
* global var used for not saved yet warning
diff --git a/lib/scripts/events.js b/lib/scripts/events.js
index 907526375..e7526ced7 100644
--- a/lib/scripts/events.js
+++ b/lib/scripts/events.js
@@ -156,4 +156,21 @@ function addInitEvent(func) {
}
}
-
+/**
+ * Bind variables to a function call creating a closure
+ *
+ * Use this to circumvent variable scope problems when creating closures
+ * inside a loop
+ *
+ * @author Adrian Lang <lang@cosmocode.de>
+ * @link http://www.cosmocode.de/en/blog/gohr/2009-10/15-javascript-fixing-the-closure-scope-in-loops
+ * @param functionref fnc - the function to be called
+ * @param mixed - any arguments to be passed to the function
+ * @returns functionref
+ */
+function bind (fnc) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ return function() {
+ return fnc.apply(this, args);
+ }
+}
diff --git a/lib/scripts/toolbar.js b/lib/scripts/toolbar.js
index 599386fb8..46f6ad789 100644
--- a/lib/scripts/toolbar.js
+++ b/lib/scripts/toolbar.js
@@ -33,13 +33,7 @@ function initToolbar(tbid,edid,tb){
// type is a tb function -> assign it as onclick
actionFunc = 'tb_'+tb[i]['type'];
if( isFunction(window[actionFunc]) ){
- addEvent(btn,'click', function(func,btn, props, edid){
- return function(){
- window[func](btn, props, edid);
- return false;
- }
- }(actionFunc,btn,tb[i],edid) );
- //above fixes the scope problem as descried at http://www.mennovanslooten.nl/blog/post/62
+ addEvent(btn,'click', bind(window[actionFunc],btn,tb[i],edid));
toolbar.appendChild(btn);
continue;
}
@@ -136,6 +130,48 @@ function tb_formatln(btn, props, edid) {
function tb_insert(btn, props, edid) {
insertAtCarret(edid,fixtxt(props['insert']));
pickerClose();
+ return false;
+}
+
+/**
+ * Button action for the media popup
+ *
+ * @param DOMElement btn Button element to add the action to
+ * @param array props Associative array of button properties
+ * @param string edid ID of the editor textarea
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function tb_mediapopup(btn, props, edid) {
+ window.open(
+ DOKU_BASE+props['url']+encodeURIComponent(NS),
+ props['name'],
+ props['options']);
+ return false;
+}
+
+/**
+ * Button action for automatic headlines
+ *
+ * Insert a new headline based on the current section level
+ *
+ * @param DOMElement btn Button element to add the action to
+ * @param array props Associative array of button properties
+ * @param string edid ID of the editor textarea
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function tb_autohead(btn, props, edid){
+ var lvl = currentHeadlineLevel(edid);
+
+ // determine new level
+ lvl += props['mod'];
+ if(lvl < 1) lvl = 1;
+ if(lvl > 5) lvl = 5;
+
+ var tags = '=';
+ for(var i=0; i<=5-lvl; i++) tags += '=';
+ insertTags(edid, tags+' ', ' '+tags+"\n", props['text']);
+ pickerClose();
+ return false;
}