main_settings_view.js 3.31 KB
Newer Older
Don Mitchell committed
1 2 3 4
CMS.Views.Settings.Main = Backbone.View.extend({
	// Model class is CMS.Models.Settings.CourseSettings
	// allow navigation between the tabs
	events: {
5 6
		'click .settings-page-menu a': "showSettingsTab",
		'blur input' : 'updateModel' 
Don Mitchell committed
7
	},
8 9 10 11
	
	currentTab: null, 
	subviews: {},	# indexed by tab name

Don Mitchell committed
12 13
	initialize: function() {
		// load templates
14 15 16 17 18 19 20 21 22
		this.currentTab = this.$el.find('.settings-page-menu .is-shown').attr('data-section');
		// create the initial subview
		this.subviews[this.currentTab] = this.createSubview();
			
		// fill in fields
		this.$el.find("#course-name-input").val(this.model.get('courseLocation').get('name'));
		this.$el.find("#course-organization-input").val(this.model.get('courseLocation').get('org'));
		this.$el.find("#course-number-input").val(this.model.get('courseLocation').get('course'));
		this.render();
Don Mitchell committed
23
	},
24
	
Don Mitchell committed
25
	render: function() {
26
		
Don Mitchell committed
27
		// create any necessary subviews and put them onto the page
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		if (!this.model.has(this.currentTab)) {
			// TODO disable screen until fetch completes?
			this.model.retrieve(this.currentTab, function() {
				this.subviews[this.currentTab] = this.createSubview();
				this.subviews[this.currentTab].render();
			});
			}
		}
		else this.callRenderFunction();
		
		return this;
	},
	
	createSubview: function() {
		switch (this.currentTab) {
		case 'details':
			return new CMS.Views.Settings.Details({
				el: this.$el.find('.settings-' + this.currentTab),
				model: this.model.get(this.currentTab);
			});
			break;
		case 'faculty':
			break;
		case 'grading':
			break;
		case 'problems':
			break;
		case 'discussions':
			break;
		}
Don Mitchell committed
58 59 60 61 62 63 64 65 66
	},
	
	showSettingsTab: function(e) {
		this.currentTab = $(e.target).attr('data-section');
		$('.settings-page-section > section').hide();
		$('.settings-' + this.currentTab).show();
		$('.settings-page-menu .is-shown').removeClass('is-shown');
		$(e.target).addClass('is-shown');
		// fetch model for the tab if not loaded already
67
		this.render();
Don Mitchell committed
68 69
	}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
});

CMS.Views.Settings.Details = Backbone.View.extend({
	// Model class is CMS.Models.Settings.CourseDetails
	events : {
		"blur input" : "updateModel"
	},
	render: function() {
		if (this.model.has('start_date')) this.$el.find('#course-start-date-input').datepicker('setDate', this.model.get('start_date'));
		if (this.model.has('end_date')) this.$el.find('#course-end-date-input').datepicker('setDate', this.model.get('end_date'));
		if (this.model.has('enrollment_start')) this.$el.find('#course-enrollment-start-input').datepicker('setDate', this.model.get('enrollment_start'));
		if (this.model.has('enrollment_end')) this.$el.find('#course-enrollment-end-input').datepicker('setDate', this.model.get('enrollment_end'));
	},
	updateModel: function(event) {
		// figure out which field
		switch (event.currentTarget.id) {
		case 'course-start-date-input':
			this.model.set('start_date', $(event.currentTarget).datepicker('getDate'));
			break;
		case 'course-end-date-input':
			this.model.set('end_date', $(event.currentTarget).datepicker('getDate'));
			break;
		case 'course-enrollment-start-date-input':
			this.model.set('enrollment_start', $(event.currentTarget).datepicker('getDate'));
			break;
		case 'course-enrollment-end-date-input':
			this.model.set('enrollment_end', $(event.currentTarget).datepicker('getDate'));
			break;

		default:
			break;
		}
		// save the updated model
		this.model.save();
	}
});