forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradingStandard.js
More file actions
355 lines (322 loc) · 13.4 KB
/
Copy pathgradingStandard.js
File metadata and controls
355 lines (322 loc) · 13.4 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import React from 'react'
import ReactDOM from 'react-dom'
import update from 'react-addons-update'
import DataRow from 'jsx/grading/dataRow'
import $ from 'jquery'
import I18n from 'i18n!external_tools'
import _ from 'underscore'
import splitAssetString from 'compiled/str/splitAssetString'
var GradingStandard = React.createClass({
getInitialState: function() {
return {
editingStandard: $.extend(true, {}, this.props.standard),
saving: false,
showAlert: false
};
},
componentWillReceiveProps: function(nextProps) {
this.setState({
editingStandard: $.extend(true, {}, this.props.standard),
saving: nextProps.saving,
showAlert: false
});
},
componentDidMount: function() {
if(this.props.justAdded) ReactDOM.findDOMNode(this.refs.title).focus();
},
componentDidUpdate: function(prevProps, prevState) {
if(this.props.editing !== prevProps.editing){
ReactDOM.findDOMNode(this.refs.title).focus();
this.setState({editingStandard: $.extend(true, {}, this.props.standard)})
}
},
triggerEditGradingStandard: function(event) {
this.props.onSetEditingStatus(this.props.uniqueId, true);
},
triggerStopEditingGradingStandard: function() {
this.props.onSetEditingStatus(this.props.uniqueId, false);
},
triggerDeleteGradingStandard: function(event) {
return this.props.onDeleteGradingStandard(event, this.props.uniqueId);
},
triggerSaveGradingStandard: function() {
if(this.standardIsValid()){
this.setState({saving: true}, function() {
this.props.onSaveGradingStandard(this.state.editingStandard);
});
}else{
this.setState({showAlert: true}, function() {
ReactDOM.findDOMNode(this.refs.invalidStandardAlert).focus();
});
}
},
assessedAssignment: function() {
return !!(this.props.standard && this.props.standard["assessed_assignment?"]);
},
deleteDataRow: function(index) {
if(this.moreThanOneDataRowRemains()){
var newEditingStandard = update(this.state.editingStandard, {data: {$splice: [[index, 1]]}});
this.setState({editingStandard: newEditingStandard});
}
},
moreThanOneDataRowRemains: function() {
return this.state.editingStandard.data.length > 1;
},
insertGradingStandardRow: function(index) {
var newEditingStandard = update(this.state.editingStandard, {data: {$splice: [[index + 1, 0, ["", ""]]]}});
this.setState({editingStandard: newEditingStandard});
},
changeTitle: function(event) {
var newEditingStandard = $.extend(true, {}, this.state.editingStandard);
newEditingStandard.title = (event.target.value);
this.setState({editingStandard: newEditingStandard});
},
changeRowMinScore: function(index, inputVal) {
var newEditingStandard = $.extend(true, {}, this.state.editingStandard);
var lastChar = inputVal.substr(inputVal.length - 1);
newEditingStandard.data[index][1] = inputVal;
this.setState({editingStandard: newEditingStandard});
},
changeRowName: function(index, newRowName) {
var newEditingStandard = $.extend(true, {}, this.state.editingStandard);
newEditingStandard.data[index][0] = newRowName;
this.setState({editingStandard: newEditingStandard});
},
hideAlert: function() {
this.setState({showAlert: false}, function(){
ReactDOM.findDOMNode(this.refs.title).focus();
});
},
standardIsValid: function() {
return this.rowDataIsValid() && this.rowNamesAreValid();
},
rowDataIsValid: function() {
if(this.state.editingStandard.data.length <= 1) return true;
var rowValues = _.map(this.state.editingStandard.data, function(dataRow){ return String(dataRow[1]).trim() });
var sanitizedRowValues = _.chain(rowValues).compact().uniq().value();
var inputsAreUniqueAndNonEmpty = (sanitizedRowValues.length === rowValues.length);
var valuesDoNotOverlap = !_.any(this.state.editingStandard.data, function(element, index, list){
if(index < 1) return false;
var thisMinScore = this.props.round(element[1]);
var aboveMinScore = this.props.round(list[index-1][1]);
return (thisMinScore >= aboveMinScore);
}, this);
return inputsAreUniqueAndNonEmpty && valuesDoNotOverlap;
},
rowNamesAreValid: function() {
var rowNames = _.map(this.state.editingStandard.data, function(dataRow){ return dataRow[0].trim() });
var sanitizedRowNames = _.chain(rowNames).compact().uniq().value();
return sanitizedRowNames.length === rowNames.length;
},
renderCannotManageMessage: function() {
if(this.props.permissions.manage && this.props.othersEditing) return null;
if(this.props.standard.context_name){
return (
<div ref="cannotManageMessage">
{I18n.t("(%{context}: %{contextName})", { context: this.props.standard.context_type.toLowerCase(), contextName: this.props.standard.context_name })}
</div>
);
}
return (
<div ref="cannotManageMessage">
{I18n.t("(%{context} level)", { context: this.props.standard.context_type.toLowerCase() })}
</div>
);
},
renderIdNames: function() {
if(this.assessedAssignment()) return "grading_standard_blank";
return "grading_standard_" + (this.props.standard ? this.props.standard.id : "blank");
},
renderTitle: function() {
if(this.props.editing){
return (
<div className="pull-left">
<input type="text" onChange={this.changeTitle}
name="grading_standard[title]" className="scheme_name" title={I18n.t("Grading standard title")}
value={this.state.editingStandard.title} ref="title"/>
</div>
);
}
return (
<div className="pull-left">
<div className="title" ref="title">
<span className="screenreader-only">{I18n.t("Grading standard title")}</span>
{this.props.standard.title}
</div>
</div>
);
},
renderDataRows: function() {
var data = this.props.editing ? this.state.editingStandard.data : this.props.standard.data;
return data.map(function(item, idx, array){
return (
<DataRow key={idx} uniqueId={idx} row={item} siblingRow={array[idx - 1]} editing={this.props.editing}
onDeleteRow={this.deleteDataRow} onInsertRow={this.insertGradingStandardRow}
onlyDataRowRemaining={!this.moreThanOneDataRowRemains()} round={this.props.round}
onRowMinScoreChange={this.changeRowMinScore} onRowNameChange={this.changeRowName}/>
);
}, this);
},
renderSaveButton: function() {
if(this.state.saving){
return (
<button type="button" ref="saveButton" className="btn btn-primary save_button" disabled="true">
{I18n.t("Saving...")}
</button>
);
}
return (
<button type="button" ref="saveButton" onClick={this.triggerSaveGradingStandard} className="btn btn-primary save_button">
{I18n.t("Save")}
</button>
);
},
renderSaveAndCancelButtons: function() {
if(this.props.editing){
return (
<div className="form-actions">
<button type="button" ref="cancelButton" onClick={this.triggerStopEditingGradingStandard} className="btn cancel_button">
{I18n.t("Cancel")}
</button>
{this.renderSaveButton()}
</div>
);
}
return null;
},
renderEditAndDeleteIcons: function() {
if(!this.props.editing){
return(
<div>
<button ref="editButton"
className={"Button Button--icon-action edit_grading_standard_button " + (this.assessedAssignment() ? "read_only" : "")}
onClick={this.triggerEditGradingStandard} type="button">
<span className="screenreader-only">{I18n.t("Edit Grading Scheme")}</span>
<i className="icon-edit"/>
</button>
<button ref="deleteButton" className="Button Button--icon-action delete_grading_standard_button"
onClick={this.triggerDeleteGradingStandard} type="button">
<span className="screenreader-only">{I18n.t("Delete Grading Scheme")}</span>
<i className="icon-trash"/>
</button>
</div>
);
}
return null;
},
renderIconsAndTitle: function() {
if(this.props.permissions.manage && !this.props.othersEditing &&
(!this.props.standard.context_code || this.props.standard.context_code == ENV.context_asset_string)){
return (
<div>
{this.renderTitle()}
<div className="links">
{this.renderEditAndDeleteIcons()}
</div>
</div>
);
}
return (
<div>
{this.renderTitle()}
{this.renderDisabledIcons()}
<div className="pull-left cannot-manage-notification">
{this.renderCannotManageMessage()}
</div>
</div>
);
},
renderDisabledIcons: function() {
if (this.props.permissions.manage &&
this.props.standard.context_code && (this.props.standard.context_code != ENV.context_asset_string)) {
var url = "/" + splitAssetString(this.props.standard.context_code).join('/') + "/grading_standards";
var titleText = I18n.t('Manage grading schemes in %{context_name}',
{context_name: this.props.standard.context_name || this.props.standard.context_type.toLowerCase()});
return (<a className='links cannot-manage-notification'
href={url}
title={titleText}
data-tooltip='left'>
<span className="screenreader-only">{titleText}</span>
<i className="icon-more standalone-icon"/>
</a>);
} else {
return (<div className="disabled-buttons" ref="disabledButtons">
<i className="icon-edit"/>
<i className="icon-trash"/>
</div>);
}
},
renderInvalidStandardMessage: function() {
var message = "Invalid grading scheme";
if (!this.rowDataIsValid()) message = "Cannot have overlapping or empty ranges. Fix the ranges and try clicking 'Save' again.";
if (!this.rowNamesAreValid()) message = "Cannot have duplicate or empty row names. Fix the names and try clicking 'Save' again.";
return (
<div id={"invalid_standard_message_" + this.props.uniqueId} className="alert-message" tabIndex="-1" ref="invalidStandardAlert">
{I18n.t("%{message}", { message: message })}
</div>
);
},
renderStandardAlert: function() {
if(!this.state.showAlert) return null;
if(this.standardIsValid()){
return (
<div id="valid_standard" className="alert alert-success">
<button aria-label="Close" className="dismiss_alert close" onClick={this.hideAlert}>×</button>
<div className="alert-message">
{I18n.t("Looks great!")}
</div>
</div>
);
}
return (
<div id="invalid_standard" className="alert alert-error">
<button aria-label="Close" className="dismiss_alert close" onClick={this.hideAlert}>×</button>
{this.renderInvalidStandardMessage()}
</div>
);
},
render: function () {
return (
<div>
<div className="grading_standard react_grading_standard pad-box-mini border border-trbl border-round"
id={this.renderIdNames()}>
{this.renderStandardAlert()}
<div>
<table>
<caption className="screenreader-only">
{I18n.t("A table containing the name of the grading scheme and buttons for editing or deleting the scheme.")}
</caption>
<thead>
<tr>
<th scope="col" className="insert_row_container" tabIndex="-1"/>
<th scope="col" colSpan="5" className="standard_title">
{this.renderIconsAndTitle()}
</th>
</tr>
<tr>
<th scope="col" className="insert_row_container"/>
<th scope="col" className="name_header">{I18n.t("Name")}</th>
<th scope="col" className="range_container" colSpan="2">
<div className="range_label">{I18n.t("Range")}</div>
<div className="clear"></div>
</th>
</tr>
</thead>
</table>
<table className="grading_standard_data">
<caption className="screenreader-only">
{I18n.t("A table that contains the grading scheme data. Each row contains a name, a maximum percentage, and a minimum percentage. In addition, each row contains a button to add a new row below, and a button to delete the current row.")}
</caption>
<thead ariaHidden="true"><tr><td></td><td></td><td></td><td></td><td></td></tr></thead>
<tbody>
{this.renderDataRows()}
</tbody>
</table>
{this.renderSaveAndCancelButtons()}
</div>
</div>
</div>
);
}
});
export default GradingStandard