X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/b0d77fd4dd2b177e77e2bb038a5864567adfd9df..36f6233fd79390ad5af8a1532eac60a0ae57c825:/platforma/static/js/button_scripts.js diff --git a/platforma/static/js/button_scripts.js b/platforma/static/js/button_scripts.js index 4f93d9bd..0d38b004 100644 --- a/platforma/static/js/button_scripts.js +++ b/platforma/static/js/button_scripts.js @@ -1,21 +1,21 @@ (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() { @@ -23,15 +23,32 @@ 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; @@ -86,9 +103,11 @@ function ScriptletCenter() } } 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"; @@ -102,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); - - 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, done) { + var self = this; - this.scriptlets['fulltextregexp'] = function(context, params) { var exprs = $.map(params.exprs, function(expr) { var opts = "mg"; if(expr.length > 2) { @@ -148,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); @@ -182,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 ''; @@ -192,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) { @@ -221,20 +249,36 @@ function ScriptletCenter() if (!text) { this.XMLEditorMoveCursorForward(context, params.tag.length + 2); } - + + done(); }.bind(this); } -ScriptletCenter.prototype.XMLEditorSelectedText = function(panel) { - return panel.selection(); +ScriptletCenter.prototype.callInteractive = function(opts) { + $progress = $('Executing script'); + 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.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) {