Librarian in regular requirements.
[redakcja.git] / redakcja / static / js / button_scripts.js
index fb53dd2..37f37a7 100644 (file)
@@ -48,6 +48,11 @@ function ScriptletCenter()
 {
     this.scriptlets = {};
 
+    this.scriptlets['insert_text'] = function(context, params, text, move_forward, move_up, done)
+    {
+        done(params.text, move_forward, move_up);
+    }.bind(this);
+
     this.scriptlets['insert_tag'] = function(context, params, text, move_forward, move_up, done)
     {
         var padding_top = '';
@@ -241,38 +246,51 @@ function ScriptletCenter()
     {
         if(!text.match(/^\n+$/)) done(text, move_forward, move_up);
 
-        function insert_done(output, mf) {
-            text += output;
+        var output = '';
+
+        function insert_done(text, mf, mu) {
+            output += text;
         }
 
         if (!params.split) params.split = 2;
         if (!params.padding) params.padding = 3;
 
-        chunks = text.replace(/^\n+|\n+$/, '').split(new RegExp("\\n{"+params.split+",}"));
-        text = text.match(/^\n+/);
-        if (!text)
-            text = '';
-        padding = '';
-        for(; params.padding; params.padding--) {
-            padding += "\n";
-        }
-
         if (params.tag == 'strofa')
             tagger = this.scriptlets['insert_stanza'];
         else
             tagger = this.scriptlets['insert_tag'];
 
-        for (i in chunks) {
-            if (chunks[i]) {
-                if (params.tag == 'akap' && chunks[i].match(/^---/))
-                    tag = 'akap_dialog';
-                else tag = params.tag;
-                tagger(context, {tag: tag}, chunks[i], 0, insert_done);
-                text += padding;
-            }
+        var padding_top = text.match(/^\n+/)
+        output = padding_top ? padding_top[0] : '';
+
+        padding = '';
+        for(var i=params.padding; i; --i) {
+            padding += "\n";
         }
 
-        done(text, move_forward, move_up);
+        text = text.substr(output.length);
+        var chunk_reg = new RegExp("^([\\s\\S]+?)(\\n{"+params.split+",}|$)");
+        while (match = text.match(chunk_reg)) {
+            if (params.tag == 'akap' && match[1].match(/^---/))
+                tag = 'akap_dialog';
+            else tag = params.tag;
+            tagger(context, {tag: tag}, match[1], 0, 0, insert_done);
+            if (match[2].length > params.padding)
+                output += match[2];
+            else
+                output += padding;
+            text = text.substr(match[0].length)
+        }
+
+        output += text;
+
+        done(output, move_forward, move_up);
+    }.bind(this);
+
+
+    this.scriptlets['slugify'] = function(context, params, text, move_forward, move_up, done)
+    {
+        done(slugify(text.replace(/_/g, '-')), move_forward, move_up);
     }.bind(this);
 
 }
@@ -289,22 +307,26 @@ ScriptletCenter.prototype.callInteractive = function(opts) {
 
        $.blockUI({message: $progress, showOverlay: false});
 
+    $('#save-button').attr('disabled', true);
     var input = self.XMLEditorSelectedText(opts.context);
-       self.scriptlets[opts.action](opts.context, opts.extra, input, 0, 0, function(output, move_forward, move_up){
-           /*if(timer)
-               clearTimeout(timer);
-           else */
-        if (input != output) {
-            self.XMLEditorReplaceSelectedText(opts.context, output)
-        }
-        if (move_forward || move_up) {
-            try {
-                self.XMLEditorMoveCursorForward(opts.context, move_forward, move_up)
+    window.setTimeout(function() {
+        self.scriptlets[opts.action](opts.context, opts.extra, input, 0, 0, function(output, move_forward, move_up){
+            /*if(timer)
+                clearTimeout(timer);
+            else */
+            if (input != output) {
+                self.XMLEditorReplaceSelectedText(opts.context, output)
             }
-            catch(e) {}
-        }
-           $.unblockUI(); // done
-       });
+            if (move_forward || move_up) {
+                try {
+                    self.XMLEditorMoveCursorForward(opts.context, move_forward, move_up)
+                }
+                catch(e) {}
+            }
+            $.unblockUI({onUnblock: function() { $('#save-button').attr('disabled', null)}}); // done
+        });
+    }, 0);
+
 }
 
 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
@@ -342,4 +364,4 @@ var scriptletCenter;
 
 $(function() {
     scriptletCenter = new ScriptletCenter();
-});
\ No newline at end of file
+});