}.bind(this));
+ this.view.on('shuffleItems', function () {
+ this.wlxmlNode.document.transaction(function() {
+ this.shuffleItems();
+ }.bind(this), {
+ metadata: {
+ description: gettext('Set random order')
+ }
+ });
+ }.bind(this));
+
var exerciseNode = this.wlxmlNode;
this.createContainer(this.wlxmlNode.object.getDescription(), {
resetBackground: true,
var node = event.meta.node;
if(node.is('item.answer') && event.meta.parent && event.meta.parent.is('list.orderable')) {
this.reloadView();
+ this.updateNumbers();
}
},
reloadView: function() {
this.view.addItem(item);
}.bind(this));
},
+ updateNumbers: function () {
+ var answer = 1;
+ this.view.sortedItemViews().forEach(function (itemView) {
+ itemView.item.setAnswer(answer);
+ answer++;
+ })
+ },
+ shuffleItems: function () {
+ var items = this.wlxmlNode.object.getItems();
+ var currentIndex = items.length, randomIndex;
+ while (0 !== currentIndex) {
+ randomIndex = Math.floor(Math.random() * currentIndex);
+ currentIndex -= 1;
+ items[currentIndex].node.after(items[randomIndex].node);
+ }
+ this.reloadView();
+ },
getVerticallyFirstTextElement: function() {
var toret;
this.containers.some(function(container) {
this.modePills = this.dom.find('.modePills');
this.list = this.dom.find('ol');
this.description = this.dom.find('.description');
+ this.shuffleButton = this.dom.find('.shuffle');
this.itemViews = [];
this.modePills.find('a').on('click', function(e) {
this.setMode($(e.target).parent().attr('mode'));
}.bind(this));
+ this.shuffleButton.on('click', function(e) {
+ e.stopPropagation();
+ e.preventDefault();
+ this.trigger('shuffleItems');
+ }.bind(this));
+
this.mode = 'initial';
var dropTargets = this.dom.find('.placeholder-top');
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;
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');
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) {