Commit 56dc2734 by Tim Krones

Make existing problem-builder tests pass.

parent d1f2ef6c
function StudentAnswersDashboardBlock(runtime, element) {
'use strict';
var $element = $(element);
// Pagination
var Result = Backbone.Model.extend({
initialize: function(attrs, options) {
_.each(_.zip(Result.properties, options.values), function(pair) {
this.set(pair[0], pair[1]);
}, this);
}
}, { properties: ['section', 'subsection', 'unit', 'type', 'question', 'answer', 'username'] });
var Results = Backbone.PageableCollection.extend({
model: Result,
getCurrentPage: function() {
var currentPage = this.state.currentPage;
return this.getPage(currentPage);
}
});
var ResultsView = Backbone.View.extend({
render: function() {
this._insertRecords(this.collection.getCurrentPage());
this._updateControls();
this.$('#total-pages').text(this.collection.state.totalPages);
return this;
},
_insertRecords: function(records) {
var tbody = this.$('tbody');
tbody.empty();
records.each(function(result, index) {
var row = $('<tr>');
if (index % 2 === 0) {
row.addClass('even');
}
_.each(Result.properties, function(name) {
row.append($('<td>').text(result.get(name)));
});
tbody.append(row);
}, this);
this.$('#current-page').text(this.collection.state.currentPage);
},
events: {
'click #first-page': '_firstPage',
'click #prev-page': '_prevPage',
'click #next-page': '_nextPage',
'click #last-page': '_lastPage'
},
_firstPage: function() {
this._insertRecords(this.collection.getFirstPage());
this._updateControls();
},
_prevPage: function() {
if (this.collection.hasPreviousPage()) {
this._insertRecords(this.collection.getPreviousPage());
}
this._updateControls();
},
_nextPage: function() {
if (this.collection.hasNextPage()) {
this._insertRecords(this.collection.getNextPage());
}
this._updateControls();
},
_lastPage: function() {
this._insertRecords(this.collection.getLastPage());
this._updateControls();
},
_updateControls: function() {
var currentPage = this.collection.state.currentPage,
totalPages = this.collection.state.totalPages,
firstPage = '#first-page',
prevPage = '#prev-page',
nextPage = '#next-page',
lastPage = '#last-page',
all = [firstPage, prevPage, nextPage, lastPage],
backward = [firstPage, prevPage],
forward = [nextPage, lastPage];
if (totalPages === 1) {
this._disable(all);
} else {
if (currentPage === 1) {
this._disable(backward);
this._enable(forward);
} else if (currentPage === totalPages) {
this._enable(backward);
this._disable(forward);
} else {
this._enable(all);
}
}
},
_enable: function(controls) {
_.each(controls, function(control) {
this.$(control).prop('disabled', false);
}, this);
},
_disable: function(controls) {
_.each(controls, function(control) {
this.$(control).prop('disabled', true);
}, this);
}
});
var resultsView = new ResultsView({
collection: new Results([], { mode: "client", state: { pageSize: 15 } }),
el: $element.find('#results')
});
// Set up gettext in case it isn't available in the client runtime:
if (typeof gettext == "undefined") {
window.gettext = function gettext_stub(string) { return string; };
......@@ -161,127 +285,4 @@ function StudentAnswersDashboardBlock(runtime, element) {
showSpinner();
getStatus();
// Pagination
var Result = Backbone.Model.extend({
initialize: function(attrs, options) {
_.each(_.zip(Result.properties, options.values), function(pair) {
this.set(pair[0], pair[1]);
}, this);
}
}, { properties: ['section', 'subsection', 'unit', 'type', 'question', 'answer', 'username'] });
var Results = Backbone.PageableCollection.extend({
model: Result,
getCurrentPage: function() {
var currentPage = this.state.currentPage;
return this.getPage(currentPage);
}
});
var ResultsView = Backbone.View.extend({
render: function() {
this._insertRecords(this.collection.getCurrentPage());
this._updateControls();
this.$('#total-pages').text(this.collection.state.totalPages);
return this;
},
_insertRecords: function(records) {
var tbody = this.$('tbody');
tbody.empty();
records.each(function(result, index) {
var row = $('<tr>');
if (index % 2 === 0) {
row.addClass('even');
}
_.each(Result.properties, function(name) {
row.append($('<td>').text(result.get(name)));
});
tbody.append(row);
}, this);
this.$('#current-page').text(this.collection.state.currentPage);
},
events: {
'click #first-page': '_firstPage',
'click #prev-page': '_prevPage',
'click #next-page': '_nextPage',
'click #last-page': '_lastPage'
},
_firstPage: function() {
this._insertRecords(this.collection.getFirstPage());
this._updateControls();
},
_prevPage: function() {
if (this.collection.hasPreviousPage()) {
this._insertRecords(this.collection.getPreviousPage());
}
this._updateControls();
},
_nextPage: function() {
if (this.collection.hasNextPage()) {
this._insertRecords(this.collection.getNextPage());
}
this._updateControls();
},
_lastPage: function() {
this._insertRecords(this.collection.getLastPage());
this._updateControls();
},
_updateControls: function() {
var currentPage = this.collection.state.currentPage,
totalPages = this.collection.state.totalPages,
firstPage = '#first-page',
prevPage = '#prev-page',
nextPage = '#next-page',
lastPage = '#last-page',
all = [firstPage, prevPage, nextPage, lastPage],
backward = [firstPage, prevPage],
forward = [nextPage, lastPage];
if (totalPages === 1) {
this._disable(all);
} else {
if (currentPage === 1) {
this._disable(backward);
this._enable(forward);
} else if (currentPage === totalPages) {
this._enable(backward);
this._disable(forward);
} else {
this._enable(all);
}
}
},
_enable: function(controls) {
_.each(controls, function(control) {
this.$(control).prop('disabled', false);
}, this);
},
_disable: function(controls) {
_.each(controls, function(control) {
this.$(control).prop('disabled', true);
}, this);
}
});
var resultsView = new ResultsView({
collection: new Results([], { mode: "client", state: { pageSize: 15 } }),
el: $element.find('#results')
});
}
......@@ -113,6 +113,7 @@ class StudentAnswersDashboardBlock(XBlock):
fragment.add_css_url(self.runtime.local_resource_url(self, 'public/css/student_answers_dashboard.css'))
fragment.add_javascript_url(self.runtime.local_resource_url(self, 'public/js/student_answers_dashboard.js'))
fragment.add_javascript_url(self.runtime.local_resource_url(self, 'public/js/vendor/underscore-min.js'))
fragment.add_javascript_url(self.runtime.local_resource_url(self, 'public/js/vendor/backbone-min.js'))
fragment.add_javascript_url(self.runtime.local_resource_url(self, 'public/js/vendor/backbone.paginator.min.js'))
fragment.initialize_js('StudentAnswersDashboardBlock')
return fragment
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment