shuffle answers in exercise order + fix answer numbering
[fnpeditor.git] / src / editor / plugins / core / edumed / order / view.js
index 8905e7a..4b470e6 100644 (file)
@@ -14,21 +14,20 @@ var OrderExerciseView = function(element) {
     this.dom = $(_.template(viewTemplate)());
     this.modePills = this.dom.find('.modePills');
     this.list = this.dom.find('ol');
-    this.addButton = this.dom.find('button.add');
     this.description = this.dom.find('.description');
+    this.shuffleButton = this.dom.find('.shuffle');
     this.itemViews = [];
 
-    this.addButton.on('click', function(e) {
+    this.modePills.find('a').on('click', function(e) {
         e.stopPropagation();
         e.preventDefault();
-        this.trigger('newItemRequested');
-        //_.last(this.itemViews).editStart();
+        this.setMode($(e.target).parent().attr('mode'));
     }.bind(this));
 
-    this.modePills.find('a').on('click', function(e) {
+    this.shuffleButton.on('click', function(e) {
         e.stopPropagation();
         e.preventDefault();
-        this.setMode($(e.target).parent().attr('mode'));
+        this.trigger('shuffleItems');
     }.bind(this));
 
     this.mode = 'initial';
@@ -42,13 +41,7 @@ var OrderExerciseView = function(element) {
     dropTargets.on('dragenter', function() {
         var first = this.itemViews[0];
         if(this.mode === 'correct') {
-            first = this.itemViews.slice(0)
-                .sort(function(view1, view2) {
-                    if(view1.item.getAnswer() > view2.item.getAnswer()) {
-                        return 1;
-                    }
-                    return -1;
-                })[0];
+            first = this.sortedItemViews()[0];
         }
         if(!this.allowDropAt(first, true)) {
             return;
@@ -71,25 +64,17 @@ var OrderExerciseView = function(element) {
 
         var first = this.itemViews[0];
         if(this.mode === 'correct') {
-            first = this.itemViews.slice(0)
-                .sort(function(view1, view2) {
-                    if(view1.item.getAnswer() > view2.item.getAnswer()) {
-                        return 1;
-                    }
-                    return -1;
-                })[0];
+            first = this.sortedItemViews()[0];
         }
 
         this.trigger(this.mode === 'initial' ? 'moveItem' : 'moveAnswer', droppedItem.item, first.item, 'before');
         dropTargets.removeClass('active');
+        e.preventDefault();
     }.bind(this));
 };
 _.extend(OrderExerciseView.prototype, Backbone.Events, {
     addItem: function(item) {
         var view = new ItemView(item, this);
-        view.on('edit', function(text) {
-            this.trigger('itemEdited', item, text);
-        }.bind(this));
         view.on('receivedDrop', function(droppedItem) {
             this.trigger(this.mode === 'initial' ? 'moveItem' : 'moveAnswer', droppedItem.item, item, 'after');
         }.bind(this));
@@ -120,19 +105,24 @@ _.extend(OrderExerciseView.prototype, Backbone.Events, {
             this.itemViews.forEach(function(itemView) {
                 this.list.append(itemView.dom);
             }.bind(this));
+            this.shuffleButton.show();
         } else {
-            this.itemViews.slice(0)
-                .sort(function(view1, view2) {
-                    if(view1.item.getAnswer() > view2.item.getAnswer()) {
-                        return 1;
-                    }
-                    return -1;
-                })
+            this.sortedItemViews()
                 .forEach(function(itemView) {
                     this.list.append(itemView.dom);
                 }.bind(this));
+            this.shuffleButton.hide();
         }
     },
+    sortedItemViews: function () {
+        return this.itemViews.slice(0)
+            .sort(function(view1, view2) {
+                if(view1.item.getAnswer() > view2.item.getAnswer()) {
+                    return 1;
+                }
+                return -1;
+            });
+    },
     allowDropAt: function(view, up) {
         var arr = [this.draggedView.dom[0]];
         if(!up) {
@@ -150,7 +140,7 @@ var ItemView = function(item, exerciseView) {
 
 
     var dropTargets = this.dom.find('.placeholder'),
-        dragSources = this.dom.find('.wrapper');
+        dragSources = this.dom.find('.handle');
 
     dragSources.on('dragstart', function(e) {
         this.dom.addClass('dragged');