editor: core plugin - edumed - first take on gaps exercise
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 1 Jul 2014 07:48:01 +0000 (09:48 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 14 Aug 2014 14:26:13 +0000 (16:26 +0200)
src/editor/modules/documentCanvas/canvas/genericElement.js
src/editor/modules/documentCanvas/nodes.less
src/editor/plugins/core/edumed/edumed.js
src/editor/plugins/core/edumed/edumed.less
src/editor/plugins/core/edumed/gaps/actions.js [new file with mode: 0644]
src/editor/plugins/core/edumed/gaps/element.js [new file with mode: 0644]
src/editor/plugins/core/edumed/gaps/gaps.less [new file with mode: 0644]
src/editor/plugins/core/edumed/gaps/view.html [new file with mode: 0644]

index 8ed08bf..fd0dfc4 100644 (file)
@@ -37,7 +37,7 @@ $.extend(generic, {
     },
     
     refresh: function() {
     },
     
     refresh: function() {
-        if(this.wlxmlNode.getTagName() === 'span') {
+        if(this.wlxmlNode.getTagName() === 'span' || this.wlxmlNode.getTagName() === 'aside') {
             if(this.containsBlock()) {
                 this.displayAsBlock();
             } else {
             if(this.containsBlock()) {
                 this.displayAsBlock();
             } else {
index 7938911..35ac7a8 100644 (file)
 [wlxml-tag="aside"] {
     margin-top: 10px;
     margin-bottom: 10px;
 [wlxml-tag="aside"] {
     margin-top: 10px;
     margin-bottom: 10px;
-
-    [wlxml-class='gap'] {
-        display: inline;
-    }
 }
 }
-
-[wlxml-class="gap"] {
-    &:before, &:after {
-        color: darken(@blue, 10%);
-        font-weight: bold;
-        //vertical-align: super;
-    }
-    &:before {
-        content: "(";
-    }
-    &:after {
-        content: ")";
-    }
-}
\ No newline at end of file
index bb64da0..25f31c9 100644 (file)
@@ -3,11 +3,13 @@ define(function(require) {
 'use strict';
 
 var actions = require('./actions'),
 'use strict';
 
 var actions = require('./actions'),
-    orderExerciseElement = require('./order/element');
+    gapsActions = require('./gaps/actions'),
+    orderExerciseElement = require('./order/element'),
+    gapsExerciseElement = require('./gaps/element');
 
 return {
 
 return {
-    actions: actions,
-    canvasElements: [orderExerciseElement]
+    actions: actions.concat(gapsActions),
+    canvasElements: [orderExerciseElement, gapsExerciseElement]
 };
 
 });
\ No newline at end of file
 };
 
 });
\ No newline at end of file
index 31a3f5b..248bedb 100644 (file)
@@ -1,4 +1,5 @@
 @import 'order/order.less';
 @import 'order/order.less';
+@import 'gaps/gaps.less';
 
 .edumed-exercise {
 
 
 .edumed-exercise {
 
diff --git a/src/editor/plugins/core/edumed/gaps/actions.js b/src/editor/plugins/core/edumed/gaps/actions.js
new file mode 100644 (file)
index 0000000..e91faa4
--- /dev/null
@@ -0,0 +1,92 @@
+define(function(require) {
+    
+'use strict';
+
+/* globals gettext */
+
+var _ = require('libs/underscore');
+
+
+var createGap = {
+    name: 'createGap',
+    params: {
+        fragment: {type: 'context', name: 'fragment'}
+    },
+    stateDefaults: {
+        icon: null,
+        label: gettext('Create a gap'),
+        execute: function(callback, params) {
+            var doc = params.fragment.document;
+
+            doc.transaction(function() {
+                var wrapper = params.fragment.startNode.parent().wrapText({
+                        _with: {tagName: 'aside', attrs: {'class': 'gap'}},
+                        offsetStart: params.fragment.startOffset,
+                        offsetEnd: params.fragment.endOffset,
+                        textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
+                    }),
+                    last = _.last(wrapper.contents());
+
+                return doc.createFragment(doc.CaretFragment, {node: last, offset: last.getText().length});
+            }, {
+                metadata: {
+                    description: gettext('Create a gap')
+                },
+                success: callback
+            });
+        }
+    },
+    getState: function(params) {
+        return {
+            allowed: params.fragment &&
+                        params.fragment.isValid() &&
+                        params.fragment instanceof params.fragment.TextRangeFragment &&
+                        params.fragment.hasSiblingBoundries() &&
+                        params.fragment.startNode.isInside('exercise.gap') &&
+                        !params.fragment.startNode.isInside({tagName: 'aside', klass: 'gap'}),
+                        
+            description: gettext('Turn selection into a gap')
+        };
+    }
+};
+
+var removeGap = {
+    name: 'removeGap',
+    params: {
+        fragment: {type: 'context', name: 'fragment'}
+    },
+    stateDefaults: {
+        icon: null,
+        label: gettext('Remove a gap'),
+        execute: function(callback, params) {
+            var doc = params.fragment.document;
+
+            doc.transaction(function() {
+                var ret = params.fragment.node.getParent('gap').unwrapContent();
+
+                return doc.createFragment(doc.CaretFragment, {node:ret.element2, offset: ret.element2.getText().length});
+            }, {
+                metadata: {
+                    description: gettext('Remove a gap')
+                },
+                success: callback
+            });
+        }
+    },
+    getState: function(params) {
+        return {
+            allowed: params.fragment &&
+                        params.fragment.isValid() &&
+                        params.fragment instanceof params.fragment.NodeFragment &&
+                        params.fragment.node.isInside('exercise.gap') &&
+                        params.fragment.node.isInside('gap'),
+                        
+            description: gettext('Remove a gap')
+        };
+    }
+};
+
+
+return [createGap, removeGap];
+
+});
\ No newline at end of file
diff --git a/src/editor/plugins/core/edumed/gaps/element.js b/src/editor/plugins/core/edumed/gaps/element.js
new file mode 100644 (file)
index 0000000..f068de9
--- /dev/null
@@ -0,0 +1,52 @@
+define(function(require) {
+    
+'use strict';
+
+
+var $ = require('libs/jquery'),
+    _ = require('libs/underscore'),
+    documentElement = require('modules/documentCanvas/canvas/documentElement'),
+    viewTemplate = require('libs/text!./view.html');
+
+var OrderExerciceElement = Object.create(documentElement.DocumentNodeElement.prototype);
+_.extend(OrderExerciceElement, {
+    init: function() {
+        documentElement.DocumentNodeElement.prototype.init.call(this);
+        var view  = $(_.template(viewTemplate)());
+        this._container().append(view);
+
+        this.createContainer(this.wlxmlNode.contents(), {
+            manages: function() {
+                return true;
+            },
+            dom: view.find('.content')
+        });
+
+        this.addToContextMenu('core.createGap');
+        this.contextMenuActions[0].on('actionExecuted', function(ret) {
+            if(ret instanceof this.wlxmlNode.document.Fragment && ret.isValid()) {
+                this.canvas.select(ret);
+            }
+        }.bind(this));
+        this.addToContextMenu('core.removeGap');
+    },
+    getVerticallyFirstTextElement: function() {
+        // doesnt container handle this?
+        var toret;
+        this.containers.some(function(container) {
+            toret = container.getVerticallyFirstTextElement();
+            return !!toret;
+        });
+        return toret;
+    }
+});
+
+return {tag: 'div', klass: 'exercise.gap', prototype: OrderExerciceElement};
+
+});
+
+
+    
+
+
+
diff --git a/src/editor/plugins/core/edumed/gaps/gaps.less b/src/editor/plugins/core/edumed/gaps/gaps.less
new file mode 100644 (file)
index 0000000..812b0e1
--- /dev/null
@@ -0,0 +1,19 @@
+[wlxml-tag="aside"] {
+    [wlxml-class='gap'] {
+        display: inline;
+    }
+}
+
+[wlxml-class="gap"] {
+    &:before, &:after {
+        color: darken(@blue, 10%);
+        font-weight: bold;
+        //vertical-align: super;
+    }
+    &:before {
+        content: "(";
+    }
+    &:after {
+        content: ")";
+    }
+}
\ No newline at end of file
diff --git a/src/editor/plugins/core/edumed/gaps/view.html b/src/editor/plugins/core/edumed/gaps/view.html
new file mode 100644 (file)
index 0000000..bd1552e
--- /dev/null
@@ -0,0 +1,4 @@
+<div class="edumed-exercise exercise-gaps">
+    <div class="header"><%= gettext('Exercise') %></div>
+    <div class="content"></div>
+</div>
\ No newline at end of file