From: Łukasz Rekucki Date: Fri, 2 Oct 2009 10:29:09 +0000 (+0200) Subject: Naprawienie przycisków. X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/755cace721087441becc6e1f22a3e1eed5bbec32?ds=sidebyside;hp=--cc Naprawienie przycisków. Użycie względnych URL z django. --- 755cace721087441becc6e1f22a3e1eed5bbec32 diff --git a/apps/api/urls.py b/apps/api/urls.py index 773ef8cb..0ae14203 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -17,8 +17,13 @@ urlpatterns = patterns('', # url(r'^hello\.(?P.+)$', hello_resource), # Toolbar - url(r'^toolbar/buttons$', toolbar_buttons, {'emitter_format': 'json'}), - url(r'^toolbar/scriptlets$', scriptlets, {'emitter_format': 'json'}), + url(r'^toolbar/buttons$', toolbar_buttons, {'emitter_format': 'json'}, + name="toolbar_buttons" + ), + + url(r'^toolbar/scriptlets$', scriptlets, {'emitter_format': 'json'}, + name="toolbar_scriptlets" + ), # Pull requests url(r"^pull-requests$", pullrequest_collection, diff --git a/project/static/js/button_scripts.js b/project/static/js/button_scripts.js new file mode 100644 index 00000000..04caf003 --- /dev/null +++ b/project/static/js/button_scripts.js @@ -0,0 +1,207 @@ +function ScriptletCenter() +{ + this.scriptlets = {} + + this.scriptlets['insert_tag'] = function(context, params) + { + var text = this.XMLEditorSelectedText(context); + var start_tag = '<'+params.tag; + + for (var attr in params.attrs) { + start_tag += ' '+attr+'="' + params.attrs[attr] + '"'; + }; + + start_tag += '>'; + var end_tag = ''; + + if(text.length > 0) { + // tokenize + var output = '' + var token = '' + for(var index=0; index < text.length; index++) + { + if (text[index].match(/\s/)) { // whitespace + token += text[index]; + } + else { // character + output += token; + if(output == token) output += start_tag; + token = '' + output += text[index]; + } + } + + if( output[output.length-1] == '\\' ) { + output = output.substr(0, output.length-1) + end_tag + '\\'; + } else { + output += end_tag; + } + output += token; + } + else { + output = start_tag + end_tag; + } + + if (text.length == 0) { + this.XMLEditorMoveCursorForward(context, params.tag.length + 2); + } + + this.XMLEditorReplaceSelectedText(context, output); + }.bind(this); + + this.scriptlets['lineregexp'] = function(context, params) { + + var exprs = $.map(params.exprs, function(expr) { + var opts = "g"; + if(expr.length > 2) + opts = expr[2]; + return { + rx: new RegExp(expr[0], opts), + repl: expr[1] + }; + }); + + var partial = true; + var text = this.XMLEditorSelectedText(context); + if(!text) return; + + var changed = 0; + var lines = text.split('\n'); + var lines = $.map(lines, function(line) { + var old_line = line; + $(exprs).each(function() { + var expr = this; + line = line.replace(expr.rx, expr.repl); + }); + + if(old_line != line) changed += 1; + return line; + }); + + if(changed > 0) { + this.XMLEditorReplaceSelectedText(context, lines.join('\n') ); + } + }.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')); + 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) + opts = expr[2]; + + return { + rx: new RegExp(expr[0], opts), + repl: expr[1] + }; + }); + + var text = this.XMLEditorSelectedText(context); + if(!text) return; + var original = text; + $(exprs).each(function() { + text = text.replace(this.rx, this.repl); + }); + + if( original != text) { + this.XMLEditorReplaceSelectedText(context, text); + } + }.bind(this); + + this.scriptlets['macro'] = function(context, params) { + var self = this; + + $(params).each(function() { + $.log(this[0], this[1]); + self.scriptlets[this[0]](context, this[1]); + }); + }.bind(this); + + this.scriptlets['lowercase'] = function(context, params) + { + var text = this.XMLEditorSelectedText(context); + + if(!text) return; + + var repl = ''; + var lcase = text.toLowerCase(); + var ucase = text.toUpperCase(); + + if(lcase == text) repl = ucase; /* was lowercase */ + 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) { + return this[0].toUpperCase() + this.slice(1); + } else { + return '' + } + }); + repl = words.join(' '); + } + + if(repl != text) this.XMLEditorReplaceSelectedText(context, repl); + }.bind(this); + + + this.scriptlets["insert_stanza"] = function(context, params) { + var text = this.XMLEditorSelectedText(context); + + if(text) { + var verses = text.split('\n'); + var text = ''; var buf = ''; var ebuf = ''; + var first = true; + + for(var i=0; i < verses.length; i++) { + verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, ""); + if(verse) { + text += (buf ? buf + '\\\n' : '') + ebuf; + buf = (first ? '\n' : '') + verses[i]; + ebuf = ''; + first = false; + } else { + ebuf += '\n' + verses[i]; + } + }; + text = text + buf + '\n' + ebuf; + this.XMLEditorReplaceSelectedText(context, text); + } + + if (!text) { + this.XMLEditorMoveCursorForward(context, params.tag.length + 2); + } + + }.bind(this); + +} + +ScriptletCenter.prototype.XMLEditorSelectedText = function(panel) { + return panel.contentView.editor.selection(); +} + +ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(panel, replacement) +{ + panel.contentView.editor.replaceSelection(replacement); +/* TODO: fire the change event */ +} + +ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) { + var pos = panel.contentView.editor.cursorPosition(); + panel.contentView.editor.selectLines(pos.line, pos.character + n); +} + +$(function() { + scriptletCenter = new ScriptletCenter(); +}); \ No newline at end of file diff --git a/project/static/js/models.js b/project/static/js/models.js index e3fb0ce6..b32f192e 100644 --- a/project/static/js/models.js +++ b/project/static/js/models.js @@ -1,7 +1,4 @@ /*globals Editor fileId SplitView PanelContainerView EditorView FlashView messageCenter*/ -var documentsUrl = '/platforma/api/documents/'; - - Editor.Model = Editor.Object.extend({ synced: false, data: null @@ -9,8 +6,8 @@ Editor.Model = Editor.Object.extend({ Editor.ToolbarButtonsModel = Editor.Model.extend({ - _className: 'Editor.ToolbarButtonsModel', - serverURL: '/platforma/api/toolbar/buttons', + className: 'Editor.ToolbarButtonsModel', + serverURL: '/api/toolbar/buttons', buttons: {}, init: function() { @@ -404,7 +401,11 @@ Editor.DocumentModel = Editor.Model.extend({ var leftPanelView, rightPanelContainer, doc; -$(function() { +$(function() +{ + documentsUrl = $('#api-base-url').text() + '/'; + Editor.ToolbarButtonsModel.serverURL = $('#api-toolbar-url').text(); + doc = new Editor.DocumentModel(); var editor = new EditorView('#body-wrap', doc); editor.freeze(); diff --git a/project/static/js/views/button_toolbar.js b/project/static/js/views/button_toolbar.js index 3e0a1488..71bfa975 100644 --- a/project/static/js/views/button_toolbar.js +++ b/project/static/js/views/button_toolbar.js @@ -1,61 +1,66 @@ /*globals View render_template scriptletCenter*/ var ButtonToolbarView = View.extend({ - _className: 'ButtonToolbarView', - template: null, - buttons: null, + _className: 'ButtonToolbarView', + template: null, + buttons: null, - init: function(element, model, parent, template) { - this._super(element, model, null); - this.parent = parent; - this.template = 'button-toolbar-view-template'; + init: function(element, model, parent, template) { + this._super(element, model, null); + this.parent = parent; + this.template = 'button-toolbar-view-template'; - this.model.addObserver(this, 'buttons', this.modelButtonsChanged.bind(this)); - this.buttons = this.model.get('buttons'); - this.model.load(); - this.render(); - }, + this.model.addObserver(this, 'buttons', this.modelButtonsChanged.bind(this)); + this.buttons = this.model.get('buttons'); + this.model.load(); + this.render(); + }, - modelButtonsChanged: function(property, value) { - this.set('buttons', value); - this.render(); - }, + modelButtonsChanged: function(property, value) { + this.set('buttons', value); + this.render(); + }, - render: function() { - $('.buttontoolbarview-tab', this.element).unbind('click.buttontoolbarview'); - $('.buttontoolbarview-button', this.element).unbind('click.buttontoolbarview'); - var self = this; + render: function() { + $('.buttontoolbarview-tab', this.element).unbind('click.buttontoolbarview'); + $('.buttontoolbarview-button', this.element).unbind('click.buttontoolbarview'); + var self = this; - this.element.html(render_template(this.template, this)); + this.element.html(render_template(this.template, this)); - $('.buttontoolbarview-tab', this.element).bind('click.buttontoolbarview', function() { - var groupIndex = $(this).attr('ui:groupindex'); - $('.buttontoolbarview-group', self.element).each(function() { - if ($(this).attr('ui:groupindex') == groupIndex) { - $(this).show(); - } else { - $(this).hide(); - } - }); - $(self.element).trigger('resize'); - }); + $('.buttontoolbarview-tab', this.element).bind('click.buttontoolbarview', function() { + var groupIndex = $(this).attr('ui:groupindex'); + $('.buttontoolbarview-group', self.element).each(function() { + if ($(this).attr('ui:groupindex') == groupIndex) { + $(this).show(); + } else { + $(this).hide(); + } + }); + $(self.element).trigger('resize'); + }); - $('.buttontoolbarview-button', this.element).bind('click.buttontoolbarview', function(event) { - var groupIndex = parseInt($(this).attr('ui:groupindex'), 10); - var buttonIndex = parseInt($(this).attr('ui:buttonindex'), 10); - var button = self.get('buttons')[groupIndex].buttons[buttonIndex]; - var scriptletId = button.scriptlet_id; - var params = eval('(' + button.params + ')'); // To nie powinno być potrzebne - console.log('Executing', scriptletId, 'with params', params); - scriptletCenter.scriptlets[scriptletId](self.parent, params); - }); + $('.buttontoolbarview-button', this.element).bind('click.buttontoolbarview', function(event) { + var groupIndex = parseInt($(this).attr('ui:groupindex'), 10); + var buttonIndex = parseInt($(this).attr('ui:buttonindex'), 10); + var button = self.get('buttons')[groupIndex].buttons[buttonIndex]; + var scriptletId = button.scriptlet_id; + var params = eval('(' + button.params + ')'); // To nie powinno być potrzebne + + console.log('Executing', scriptletId, 'with params', params); + try { + scriptletCenter.scriptlets[scriptletId](self.parent, params); + } catch(e) { + console.log("Scriptlet", scriptletId, "failed."); + } + }); - $(this.element).trigger('resize'); - }, + $(this.element).trigger('resize'); + }, - dispose: function() { - $('.buttontoolbarview-tab', this.element).unbind('click.buttontoolbarview'); - $('.buttontoolbarview-button', this.element).unbind('click.buttontoolbarview'); - this._super(); - } + dispose: function() { + $('.buttontoolbarview-tab', this.element).unbind('click.buttontoolbarview'); + $('.buttontoolbarview-button', this.element).unbind('click.buttontoolbarview'); + this._super(); + } }); diff --git a/project/static/js/views/panel_container.js b/project/static/js/views/panel_container.js index 8d383e1a..af067a3d 100644 --- a/project/static/js/views/panel_container.js +++ b/project/static/js/views/panel_container.js @@ -23,9 +23,12 @@ var PanelContainerView = View.extend({ this.contentView.dispose(); this.contentView = null; } + + if( value != 'empty') { this.contentView = new klass($('.content-view', this.element.get(0)), this.model.contentModels[value], this); $('.panel-main-toolbar .refresh', this.element.get(0)).attr('disabled', null); + } }, refreshButtonClicked: function(event) { diff --git a/project/templates/explorer/editor.html b/project/templates/explorer/editor.html index 40b5f7ae..83843f14 100644 --- a/project/templates/explorer/editor.html +++ b/project/templates/explorer/editor.html @@ -14,7 +14,7 @@ {# Scriptlets #} - + {# App and views #} @@ -35,6 +35,7 @@