@tutor/exercise-editor
Version:
The exercise editor for tutor.
85 lines (75 loc) • 2.52 kB
JavaScript
var _,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
_ = require('lodash');
module.exports = function(ko) {
var ExercisePageViewModel, TaskViewModel;
if (ko.mapping == null) {
throw Error("You need to load knockout-mapping for the exercise editor.");
}
TaskViewModel = (function() {
function TaskViewModel(data) {
if (data != null) {
ko.mapping.fromJS(data, {}, this);
}
this.hasTitle = ko.computed((function(_this) {
return function() {
return (_this.title() != null) && _this.title().trim() !== '';
};
})(this));
if (this.solution == null) {
this.solution = ko.observable('');
}
}
return TaskViewModel;
})();
ExercisePageViewModel = (function() {
function ExercisePageViewModel(params) {
this.init = bind(this.init, this);
this.submit = bind(this.submit, this);
this.number = ko.observable();
this.tasks = ko.observableArray();
this.exerciseNotFound = ko.observable(false);
this.isOld = ko.observable(true);
this.getExercise(params.id, (function(_this) {
return function(exercise, status, error) {
var tasks;
if (exercise != null) {
_this.exercise = exercise;
_this.number(exercise.number);
tasks = _.map(exercise.tasks, function(t) {
return new TaskViewModel(t);
});
_.each(tasks, function(t) {
t.solution.subscribe(function() {
return _this.submit(t);
});
return _this.tasks.push(t);
});
return _this.isOld(Date.parse(exercise.dueDate) < Date.now());
} else if (status === 404) {
return _this.exerciseNotFound(true);
} else {
}
};
})(this));
}
ExercisePageViewModel.prototype.submit = function(task) {
if (task != null) {
return this.saveTask(task.id, ko.mapping.toJS(task), function(status) {
return console.log('Submit: ' + status);
});
} else {
return this.saveExercise(exercise.id, ko.mapping.toJS(this.tasks), function(status) {
return console.log('Submit: ' + status);
});
}
};
ExercisePageViewModel.prototype.init = function() {
return $(".popupinfo").popup();
};
return ExercisePageViewModel;
})();
return {
ExercisePageViewModel: ExercisePageViewModel
};
};