* Readonly document view.
[redakcja.git] / platforma / static / js / button_scripts.js
index 40f45f6..0d38b00 100644 (file)
@@ -1,11 +1,58 @@
+(function() {
+  var slice = Array.prototype.slice;
+
+  function update(array, args) {
+    var arrayLength = array.length, length = args.length;
+    while (length--) array[arrayLength + length] = args[length];
+    return array;
+  };
+
+  function merge(array, args) {
+    array = slice.call(array, 0);
+    return update(array, args);
+  };
+
+  Function.prototype.bind = function(context) {
+    if (arguments.length < 2 && typeof arguments[0] === 'undefined') {
+      return this;
+    }
+    var __method = this;
+    var args = slice.call(arguments, 1);
+    return function() {
+      var a = merge(args, arguments);
+      return __method.apply(context, a);
+    }
+  }
+
+})();
+
+function nblck_each(array, body, after) {
+       $.each(array, function(i) {
+               body(this, i);
+       });
+
+       after();
+};
+
+function nblck_map(array, func, after) {
+       var acc = [];
+
+       nblck_each(array, function(elem, index) {
+               acc.push(func(elem, index));
+       }, function(){
+               after(acc);
+       });
+};
+
 function ScriptletCenter()
 {
     this.scriptlets = {};
 
-    this.scriptlets['insert_tag'] = function(context, params)
+    this.scriptlets['insert_tag'] = function(context, params, done)
     {
         var text = this.XMLEditorSelectedText(context);
         var start_tag = '<'+params.tag;
+        var move_cursor = false;
 
         for (var attr in params.attrs) {
             start_tag += ' '+attr+'="' + params.attrs[attr] + '"';
@@ -39,17 +86,28 @@ function ScriptletCenter()
             output += token;
         }
         else {
-            output = start_tag + end_tag;
+            if(params.nocontent) {
+                output = "<"+params.tag +" />";
+            }
+            else {
+                output = start_tag + end_tag;
+                move_cursor = true;
+            }
         }
 
         this.XMLEditorReplaceSelectedText(context, output);
 
-        if (text.length == 0) {
-            this.XMLEditorMoveCursorForward(context, -params.tag.length-3);
-        }        
+        try {
+            if (move_cursor) {
+                this.XMLEditorMoveCursorForward(context, params.tag.length+2);
+            }
+        } catch(e) {}
+
+               done();
     }.bind(this);
 
-    this.scriptlets['lineregexp'] = function(context, params) {
+    this.scriptlets['lineregexp'] = function(context, params, done) {
+               var self = this;
 
         var exprs = $.map(params.exprs, function(expr) {
             var opts = "g";
@@ -63,40 +121,34 @@ function ScriptletCenter()
 
         var partial = true;
         var text = this.XMLEditorSelectedText(context);
-        if(!text) return;
+        if(!text) return done();
 
         var changed = 0;
         var lines = text.split('\n');
-        lines = $.map(lines, function(line) {
+
+               nblck_map(lines, function(line, index) {
             var old_line = line;
             $(exprs).each(function() {
                 var expr = this;
                 line = line.replace(expr.rx, expr.repl);
             });
 
+                       $progress.html(index);
+
             if(old_line != line) changed += 1;
             return line;
-        });
+        }, function(newlines) {
+                       if(changed > 0) {
+                               self.XMLEditorReplaceSelectedText(context, newlines.join('\n') );
+                       };
 
-        if(changed > 0) {
-            this.XMLEditorReplaceSelectedText(context, lines.join('\n') );
-        }
+                       done();
+               });
     }.bind(this);
 
-    this.scriptlets['codemirror_fontsize'] = function(context, params) {
-        var frameBody = this.XMLEditorBody(context);
+    this.scriptlets['fulltextregexp'] = function(context, params, done) {
+               var self = this;
 
-        if(params.fontSize) {
-            frameBody.css('font-size', params.fontSize);
-        }
-        else {
-            var old_size = parseInt(frameBody.css('font-size'), 10);
-            frameBody.css('font-size', old_size + (params.change || 0) );
-        }
-        
-    }.bind(this);
-
-    this.scriptlets['fulltextregexp'] = function(context, params) {
         var exprs = $.map(params.exprs, function(expr) {
             var opts = "mg";
             if(expr.length > 2) {
@@ -109,27 +161,40 @@ function ScriptletCenter()
         });
 
         var text = this.XMLEditorSelectedText(context);
-        if(!text) return;
+        if(!text) return done();
         var original = text;
-        $(exprs).each(function() {
-            text = text.replace(this.rx, this.repl);
-        });
 
-        if( original != text) {
-            this.XMLEditorReplaceSelectedText(context, text);
-        }
+               nblck_each(exprs, function(expr, index) {
+                       $progress.html(600 + index);
+            text = text.replace(expr.rx, expr.repl);
+        }, function() {
+                       if( original != text) {
+                  self.XMLEditorReplaceSelectedText(context, text);
+               }
+
+                       done();
+               });
     }.bind(this);
 
-    this.scriptlets['macro'] = function(context, params) {
+    this.scriptlets['macro'] = function(context, params, done) {
         var self = this;
-
-        $(params).each(function() {
-            $.log(this[0], this[1]);
-            self.scriptlets[this[0]](context, this[1]);
-        });
+               var i = 0;
+
+               function next() {
+               if (i < params.length) {
+                               var e = params[i];
+                               i = i + 1;
+                               self.scriptlets[e[0]](context, e[1], next);
+                       }
+                       else {
+                               done();
+                       }
+        };
+
+               next();
     }.bind(this);
 
-    this.scriptlets['lowercase'] = function(context, params)
+    this.scriptlets['lowercase'] = function(context, params, done)
     {
         var text = this.XMLEditorSelectedText(context);
 
@@ -143,7 +208,7 @@ function ScriptletCenter()
         else if(ucase != text) repl = lcase; /* neither lower- or upper-case */
         else { /* upper case -> camel-case */
             var words = $(lcase.split(/\s/)).map(function() {
-                if(this.length > 0) { 
+                if(this.length > 0) {
                     return this[0].toUpperCase() + this.slice(1);
                 } else {
                     return '';
@@ -153,10 +218,12 @@ function ScriptletCenter()
         }
 
         if(repl != text) this.XMLEditorReplaceSelectedText(context, repl);
+
+               done();
     }.bind(this);
 
 
-    this.scriptlets["insert_stanza"] = function(context, params) {
+    this.scriptlets["insert_stanza"] = function(context, params, done) {
         var text = this.XMLEditorSelectedText(context);
 
         if(text) {
@@ -182,25 +249,41 @@ function ScriptletCenter()
         if (!text) {
             this.XMLEditorMoveCursorForward(context, params.tag.length + 2);
         }
-        
+
+               done();
     }.bind(this);
 
 }
 
-ScriptletCenter.prototype.XMLEditorSelectedText = function(panel) {
-    return panel.contentView.editor.selection();
+ScriptletCenter.prototype.callInteractive = function(opts) {
+       $progress = $('<span>Executing script</span>');
+       var self = this;
+
+       $.blockUI({
+               message: $progress,
+
+       });
+
+
+       self.scriptlets[opts.action](opts.context, opts.extra, function(){
+               $.unblockUI(); // done
+       });
+}
+
+ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
+
+    return editor.selection();
 };
 
-ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(panel, replacement)
+ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
 {
-    panel.contentView.editor.replaceSelection(replacement);
-    // Tell XML view that it's data has changed
-    panel.contentView.editorDataChanged();
+       $progress.html("Replacing text");
+    editor.replaceSelection(replacement);
 };
 
 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
-    var pos = panel.contentView.editor.cursorPosition();
-    panel.contentView.editor.selectLines(pos.line, pos.character + n);
+    var pos = panel.cursorPosition();
+    panel.selectLines(pos.line, pos.character + n);
 };
 
 var scriptletCenter;