Naprawienie przycisków.
authorŁukasz Rekucki <lrekucki@gmail.com>
Fri, 2 Oct 2009 10:29:09 +0000 (12:29 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Fri, 2 Oct 2009 10:29:09 +0000 (12:29 +0200)
Użycie względnych URL z django.

apps/api/urls.py
project/static/js/button_scripts.js [new file with mode: 0644]
project/static/js/models.js
project/static/js/views/button_toolbar.js
project/static/js/views/panel_container.js
project/templates/explorer/editor.html

index 773ef8c..0ae1420 100644 (file)
@@ -17,8 +17,13 @@ urlpatterns = patterns('',
 #    url(r'^hello\.(?P<emitter_format>.+)$', hello_resource),
 
     # Toolbar
 #    url(r'^hello\.(?P<emitter_format>.+)$', 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,
 
     # 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 (file)
index 0000000..04caf00
--- /dev/null
@@ -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 = '</'+params.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 ? '<strofa>\n' : '') + verses[i];
+                    ebuf = '';
+                    first = false;
+                } else {
+                    ebuf += '\n' + verses[i];
+                }
+            };
+            text = text + buf + '\n</strofa>' + 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
index e3fb0ce..b32f192 100644 (file)
@@ -1,7 +1,4 @@
 /*globals Editor fileId SplitView PanelContainerView EditorView FlashView messageCenter*/
 /*globals Editor fileId SplitView PanelContainerView EditorView FlashView messageCenter*/
-var documentsUrl = '/platforma/api/documents/';
-
-
 Editor.Model = Editor.Object.extend({
   synced: false,
   data: null
 Editor.Model = Editor.Object.extend({
   synced: false,
   data: null
@@ -9,8 +6,8 @@ Editor.Model = Editor.Object.extend({
 
 
 Editor.ToolbarButtonsModel = Editor.Model.extend({
 
 
 Editor.ToolbarButtonsModel = Editor.Model.extend({
-  _className: 'Editor.ToolbarButtonsModel',
-  serverURL: '/platforma/api/toolbar/buttons',
+  className: 'Editor.ToolbarButtonsModel',
+  serverURL: '/api/toolbar/buttons',
   buttons: {},
   
   init: function() {
   buttons: {},
   
   init: function() {
@@ -404,7 +401,11 @@ Editor.DocumentModel = Editor.Model.extend({
 
 var leftPanelView, rightPanelContainer, doc;
 
 
 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();
   doc = new Editor.DocumentModel();
   var editor = new EditorView('#body-wrap', doc);
   editor.freeze();
index 3e0a148..71bfa97 100644 (file)
@@ -1,61 +1,66 @@
 /*globals View render_template scriptletCenter*/
 var ButtonToolbarView = View.extend({
 /*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();
+    }
 });
 
 });
 
index 8d383e1..af067a3 100644 (file)
@@ -23,9 +23,12 @@ var PanelContainerView = View.extend({
       this.contentView.dispose();
       this.contentView = null;
     }
       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);
     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) {
   },
   
   refreshButtonClicked: function(event) {
index 40b5f7a..83843f1 100644 (file)
@@ -14,7 +14,7 @@
     <script src="{{STATIC_URL}}js/lib/codemirror/codemirror.js" type="text/javascript" charset="utf-8"></script>
        <script src="{{STATIC_URL}}js/lib/jquery.modal.js" type="text/javascript" charset="utf-8"></script>
        {# Scriptlets #}
     <script src="{{STATIC_URL}}js/lib/codemirror/codemirror.js" type="text/javascript" charset="utf-8"></script>
        <script src="{{STATIC_URL}}js/lib/jquery.modal.js" type="text/javascript" charset="utf-8"></script>
        {# Scriptlets #}
-       <script src="http://localhost:8000/api/toolbar/scriptlets" type="text/javascript" charset="utf-8"></script>
+       <script src="{{STATIC_URL}}js/button_scripts.js" type="text/javascript" charset="utf-8"></script>
        
        {# App and views #}
        <script src="{{STATIC_URL}}js/app.js" type="text/javascript" charset="utf-8"></script>
        
        {# App and views #}
        <script src="{{STATIC_URL}}js/app.js" type="text/javascript" charset="utf-8"></script>
@@ -35,6 +35,7 @@
        <script type="text/html" charset="utf-8" id="panel-container-view-template">
                <div class="panel-main-toolbar">
                 <p><select>
        <script type="text/html" charset="utf-8" id="panel-container-view-template">
                <div class="panel-main-toolbar">
                 <p><select>
+                        <option value="empty" selected="selected"> ---- </option>
                        <% for (panel in panels) { %>
                        <option value="<%= panel %>"><%= panel %></option>
                        <% }; %>
                        <% for (panel in panels) { %>
                        <option value="<%= panel %>"><%= panel %></option>
                        <% }; %>
 {% endblock %}
 
 {% block maincontent %}
 {% endblock %}
 
 {% block maincontent %}
-       <div id="splitview">
-               <div id="left-panel-container" class='panel-container'></div>
-           <div id="right-panel-container" class='panel-container'></div>
-    </div>
+    <p style="display: none;" id="api-base-url">{% url document_list_view %}</p>
+    <p style="display: none;" id="api-toolbar-url">{% url toolbar_buttons %}</p>
 
 
+    <div id="splitview">
+        <div id="left-panel-container" class='panel-container'></div>
+       <div id="right-panel-container" class='panel-container'></div>
+    </div>
 
     <div id="commit-dialog" class="jqmWindow" style="display:none">
         <form action="{% url file_commit fileid %}" method="POST">
 
     <div id="commit-dialog" class="jqmWindow" style="display:none">
         <form action="{% url file_commit fileid %}" method="POST">