editor: Inserting document templates support
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 11 Dec 2013 12:18:41 +0000 (13:18 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:51 +0000 (22:32 +0100)
TODO: Refactor it out of documentToolbar module, use some kind of
more generalized mechanism.

src/editor/modules/documentToolbar/documentToolbar.js
src/editor/modules/documentToolbar/template.html
src/editor/modules/rng/rng.js

index d903483..84361ad 100644 (file)
@@ -1,48 +1,57 @@
 define(['libs/jquery', 'libs/underscore', 'utils/wlxml', 'libs/text!./template.html'], function($, _, wlxmlUtils, template) {
 
 'use strict';
+/* globals Node */
 
 return function(sandbox) {
     
+    var documentTemplates = sandbox.getBootstrappedData(),
+        currentNode;
+
     var view = {
-        node: $(_.template(template)({tagNames: wlxmlUtils.wlxmlTagNames, classNames: wlxmlUtils.wlxmlClassNames})),
+        node: $(_.template(template)({tagNames: wlxmlUtils.wlxmlTagNames, classNames: wlxmlUtils.wlxmlClassNames, templates: documentTemplates})),
         setup: function() {
             var view = this;
             
             this.node.find('button').click(function(e) {
                 e.stopPropagation();
+
                 var btn = $(e.currentTarget),
                     btnName = btn.attr('data-name'),
                     meta = btn.attr('data-meta'),
                     params = {},
                     command = btnName;
 
-                if(btn.attr('data-btn-type') === 'toggle') {
-                    command = 'toggle-' + command;
-                    btn.toggleClass('active');
-                    params.toggle = btn.hasClass('active');
-                }
+                if(myHandlers[btnName]) {
+                    myHandlers[btnName](btn);
+                } else {
+                    if(btn.attr('data-btn-type') === 'toggle') {
+                        command = 'toggle-' + command;
+                        btn.toggleClass('active');
+                        params.toggle = btn.hasClass('active');
+                    }
 
-                if(btnName === 'new-node') {
-                    command = 'newNodeRequested';
-                    params.wlxmlTag = view.getOption('newTag-tag');
-                    params.wlxmlClass = view.getOption('newTag-class');
-                    if(meta) {
-                        var split = meta.split('/');
-                        params.wlxmlTag = split[0];
-                        params.wlxmlClass = split[1];
+                    if(btnName === 'new-node') {
+                        command = 'newNodeRequested';
+                        params.wlxmlTag = view.getOption('newTag-tag');
+                        params.wlxmlClass = view.getOption('newTag-class');
+                        if(meta) {
+                            var split = meta.split('/');
+                            params.wlxmlTag = split[0];
+                            params.wlxmlClass = split[1];
+                        }
+                    } else {
+                        params.meta = meta;
                     }
-                } else {
-                    params.meta = meta;
-                }
 
-                if(command === 'undo' || command === 'redo') {
-                    params.callback = function(disable) {
-                        btn.attr('disabled', !disable);
-                    };
-                }
+                    if(command === 'undo' || command === 'redo') {
+                        params.callback = function(disable) {
+                            btn.attr('disabled', !disable);
+                        };
+                    }
 
-                sandbox.publish('command', command, params);
+                    sandbox.publish('command', command, params);
+                }
             });
         },
         getOption: function(option) {
@@ -50,11 +59,28 @@ return function(sandbox) {
         }
     };
     
+    var myHandlers = {
+        templatesBtn: function() {
+            if(currentNode && currentNode.nodeType === Node.ELEMENT_NODE) {
+                var templateId = parseInt(view.node.find('[data-name=templates-select]').val(), 10);
+                documentTemplates.forEach(function(template) {
+                    if(template.id === templateId) {
+                        var toAdd = currentNode.document.createDocumentNode(template.content);
+                        currentNode.after(toAdd);
+                    }
+                });
+            }
+        }
+    };
+
     view.setup();
 
     return {
         start: function() { sandbox.publish('ready'); },
         getView: function() { return view.node; },
+        setNodeElement: function(node) {
+            currentNode = node;
+        },
         getOption: function(option) { return view.getOption(option); }
     };
 };
index 0572bb5..17eecde 100644 (file)
         <button data-name="grid" data-btn-type="toggle" class="btn btn-mini"><i class="icon-th-large"></i></button>
     </div>
 
+    <% if(templates) { %>
+    <div class="rng-module-documentToolbar-toolbarGroup">
+        <select data-name="templates-select">
+        <% templates.forEach(function(template) { %>
+            <option value="<%= template.id %>"><%= template.name %></option>
+        <% }); %>
+        </select>
+        <button class="btn btn-mini" data-name="templatesBtn">+</button>
+    </div>
+    <% } %>
+
     <div style="clear: both;"></div>
 </div>
\ No newline at end of file
index ff23c9d..a5432c0 100644 (file)
@@ -42,6 +42,7 @@ return function(sandbox) {
             sandbox.getModule('nodePane').setNodeElement(nodeElement);
             sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
             sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
+            sandbox.getModule('documentToolbar').setNodeElement(nodeElement);
         },
         updateCurrentTextElement: function(textElement) {
             sandbox.getModule('nodeFamilyTree').setElement(textElement);