list fixes: headers, invalid elements, easier ending
[fnpeditor.git] / src / editor / plugins / core / core.js
index 76be9e8..c56c5db 100644 (file)
@@ -12,9 +12,33 @@ var _ = require('libs/underscore'),
     Dialog = require('views/dialog/dialog'),
     canvasElements = require('plugins/core/canvasElements'),
     metadataEditor = require('plugins/core/metadataEditor/metadataEditor'),
-    edumed = require('plugins/core/edumed/edumed');
+    edumed = require('plugins/core/edumed/edumed'),
+    attachments = require('views/attachments/attachments');
 
 
+
+
+var exerciseFix = function(newNodes) {
+    var list, exercise, max, addedItem, answerValues;
+    if(newNodes.created.is('item')) {
+        list = newNodes.created.parent();
+        exercise = list.parent();
+        if(exercise && exercise.is('exercise')) {
+            if(exercise.is('exercise.order')) {
+                answerValues = exercise.object.getItems()
+                            .map(function(item) {
+                                if(!addedItem && item.node.sameNode(newNodes.created)) {
+                                    addedItem = item;
+                                }
+                                return item.getAnswer();
+                            });
+                max = Math.max.apply(Math.max, answerValues);
+                addedItem.setAnswer(max + 1);
+            }
+        }
+    }
+};
+
 plugin.documentExtension.textNode.transformations = {
     breakContent: {
         impl: function(args) {
@@ -38,6 +62,21 @@ plugin.documentExtension.textNode.transformations = {
                     return true; // break
                 }
             });
+
+            /* <hack>
+            /*
+                This makes sure that adding a new item to the list in some of the edumed exercises
+                sets an answer attribute that makes sense (and not just copies it which would create
+                a duplicate value).
+
+                This won't be neccessary when/if we introduce canvas element own key event handlers.
+
+                Alternatively, WLXML elements could implement their own item split methods that we
+                would delegate to.
+            */
+                exerciseFix(newNodes);
+            /* </hack> */
+
             parentDescribingNodes.forEach(function(node) {
                 newNodes.first.append(node);
             });
@@ -224,7 +263,16 @@ plugin.documentExtension.documentNode.transformations = {
                     } else if(prev.is({tagName: 'span'})) {
                         if((txtNode = prev.getLastTextNode())) {
                             txt = txtNode.getText();
-                            txtNode.setText(txt.substr(0, txt.length-1));
+                            if(txt.length > 1) {
+                                txtNode.setText(txt.substr(0, txt.length-1));
+                            } else {
+                                if(txtNode.parent().contents().length === 1) {
+                                    txtNode.parent().detach();
+                                } else {
+                                    txtNode.detach();
+                                }
+
+                            }
                             return toret;
                         }
                     }
@@ -269,6 +317,13 @@ plugin.documentExtension.documentNode.transformations = {
             }
         });
         return toret;
+    },
+    insertNewNode: function () {
+        var node = this;
+        var newElement = this.document.createDocumentNode({tagName: 'div', attrs: {class: 'p'}});
+        node.after(newElement);
+        newElement.append({text: ''});
+        return newElement;
     }
 };
 
@@ -475,6 +530,7 @@ var createWrapTextAction = function(createParams) {
 };
 
 
+
 var createLinkFromSelection = function(callback, params) {
     var doc = params.fragment.document,
         dialog = Dialog.create({
@@ -485,7 +541,8 @@ var createLinkFromSelection = function(callback, params) {
                 {label: gettext('Link'), name: 'href', type: 'input',
                 prePasteHandler: function(text) {
                                     return params.fragment.document.getLinkForUrl(text);
-                                }.bind(this)
+                                }.bind(this),
+                description: '<a href="#-" class="attachment-library">' + gettext('attachment library') + '</a>'
                 }
             ]
         }),
@@ -511,6 +568,9 @@ var createLinkFromSelection = function(callback, params) {
         });
     });
     dialog.show();
+    $(".attachment-library", dialog.$el).on('click', function() {
+        attachments.select(function(v) {$("input", dialog.$el).val(v);});
+    });
 };
 
 var editLink = function(callback, params) {
@@ -580,6 +640,7 @@ var linkAction = {
     }
 };
 
+
 var metadataParams = {};
 
 plugin.actions = [