-
Notifications
You must be signed in to change notification settings - Fork 25
/
ubcpi_edit.js
196 lines (166 loc) · 7.28 KB
/
ubcpi_edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
58
59
60
61
62
63
64
65
66
67
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
angular.module("ubcpi_edit", ['ngMessages', 'ngSanitize', 'ngCookies', 'gettext'])
.run(['$http', '$cookies', '$rootScope', '$rootElement', 'gettextCatalog', function ($http, $cookies, $rootScope, $rootElement, gettextCatalog) {
// set up CSRF Token from cookie. This is needed by all post requests
$http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
$rootScope.config = $rootElement[0].config;
// config the language
gettextCatalog.setCurrentLanguage($rootScope.config.data.lang);
}])
.directive('validateForm', ['$q', 'studioBackendService', function($q, studioBackendService) {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
scope.$watch(attrs.ngModel, function(model) {
if (model != null) {
ctrl.$validate();
}
}, true);
ctrl.$asyncValidators.validate_form = function (modelValue, viewValue) {
scope.piForm.$errors = {};
return studioBackendService.validateForm(modelValue).then(function () {
return true;
}, function (error) {
scope.piForm.$errors = error.error;
return $q.reject(error.error);
});
};
}
};
}])
.factory('studioBackendService', ['$http', '$q', '$rootScope', function ($http, $q, $rootScope) {
return {
validateForm: validateForm,
submit: studioSubmit
};
function validateForm(values) {
return $http.post($rootScope.config.urls.validate_form, values).
then(function () {
return true;
}, function (response) {
return $q.reject(response.data);
});
}
function studioSubmit(data) {
return $http.post($rootScope.config.urls.studio_submit, data).then(
function(response) {
return response.data;
},
function(error) {
return $q.reject(error.data);
}
);
}
}])
.controller('EditSettingsController', ['$scope', 'studioBackendService', 'notify', '$rootScope', 'gettext',
function ($scope, studioBackendService, notify, $rootScope, gettext) {
var self = this;
var data = $scope.config.data;
self.makeOptions = function() {
var options = [];
for (var i = 0; i < data.options.length + 1; i++) {
if(i==(data.options.length))
options.push("n/a");
else {
var option = i+1;
options.push("Option " + option);
}
}
return options;
};
self.algos = data.algos;
self.data = {};
self.data.display_name = data.display_name;
self.data.weight = data.weight;
self.data.question_text = data.question_text;
self.data.rationale_size = data.rationale_size;
self.image_position_locations = data.image_position_locations;
self.data.options = data.options;
self.data.correct_answer = data.correct_answer;
if (data.correct_rationale)
self.data.correct_rationale = data.correct_rationale;
else
self.data.correct_rationale = {"text": ""};
self.data.algo = data.algo;
self.data.seeds = data.seeds;
self.add_option = function() {
self.data.options.push(
{'text': '', 'image_url': '', 'image_position': 'below', 'image_show_fields': 0, 'image_alt': ''}
);
};
self.delete_option = function(index) {
//remove option
self.data.options.splice(index, 1);
//find seeds that match the index and remove them
for(var i=0;i<self.data.seeds.length;i++){
if(self.data.seeds[i]['answer'] == index){
self.data.seeds.splice(i,1);
i--;
}
}
//look for seeds with answer indexes that are greater than or equal to the option index and reduce
// the answer value by one to account for the removed option
for(var j=0;j<self.data.seeds.length;j++){
if(self.data.seeds[j]['answer'] >= index){
self.data.seeds[j]['answer']--;
}
}
};
self.addSeed = function() {
self.data.seeds.push({});
};
self.deleteSeed = function(index) {
self.data.seeds.splice(index, 1);
};
self.image_show_fields = function( index ) {
if ( index === false ) {
// This is just for the 'question', i.e. not an array of possibles
self.data.question_text.image_show_fields = !self.data.question_text.image_show_fields;
if ( !self.data.question_text.image_show_fields ) {
self.data.question_text.image_url = '';
}
} else {
// This is for the options
self.data.options[index].image_show_fields = !self.data.options[index].image_show_fields;
if ( !self.data.options[index].image_show_fields ) {
self.data.options[index].image_url = '';
}
}
};
self.noCorrect = function() {
if(self.data.correct_answer==data.options.length)
return false;
else
return true;
};
self.submit = function() {
notify('save', {state: 'start', message: gettext("Saving")});
return studioBackendService.submit(self.data).catch(function(errors) {
notify('error', {
'title': gettext('Error saving question'),
'message': errors.errors
});
}).finally(function() {
notify('save', {state: 'end'})
});
};
}]);
function PIEdit(runtime, element, data) {
"use strict";
// wrap element as core.js may pass a raw element or an wrapped one
var $element = $(element);
// The workbench doesn't support notifications.
var notify = $.proxy(runtime.notify, runtime) || function () {};
var urls = {
'studio_submit': runtime.handlerUrl(element, 'studio_submit'),
'validate_form': runtime.handlerUrl(element, 'validate_form')
};
// not sure why studio edit passes in array of elements,
// where student view passes in only the element
$element[0].config = {
'data': data,
'urls': urls
};
// inject xblock notification
angular.module('ubcpi_edit').value('notify', notify);
angular.bootstrap($element, ["ubcpi_edit"], {strictDi: true});
}