This repository was archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 865
/
Copy pathmatrix-input.js
371 lines (301 loc) · 11.5 KB
/
matrix-input.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* TODO(csilvers): fix these lint errors (http://eslint.org/docs/rules): */
/* eslint-disable comma-dangle, indent, no-undef, no-var */
/* To fix, remove an entry above, run ka-lint, and fix errors. */
/**
* Allows for intuitive matrix input for matrix exercises.
*
* See `matrix_transpose.html` for an example.
*
* To use in an exercise:
*
* 1. Add "matrix matrix-input" to data-require.
*
* 2. Use `matrixPad()` to pad the solution matrix with empty string values
* and assign to a `var` named `PADDED_SOLN_MAT`:
*
* Ex: <var id="PADDED_SOLN_MAT">matrixPad(SOLN_MAT, 3, 3)</var>
*
* 3. Use the following HTML for the
* solution markup:
*
* <div class="solution" data-type="multiple">
* <div data-each="PADDED_SOLN_MAT as row" class="row">
* <div data-each="row as elem">
* <div data-if="elem !== ''" class="sol">
* <var>elem</var>
* </div>
* <div data-else data-type="text" class="sol">
* <var>elem</var>
* </div>
* </div>
* </div>
* </div>
*
*/
define(function(require) {
require("../third_party/jquery.cursor-position.js");
$.extend(KhanUtil, {
matrixInput: {
eventsAttached: false,
eventNamespace: "matrix-input",
containerEl: null,
bracketEls: null,
cells: null,
LEFT_ARROW: 37,
UP_ARROW: 38,
RIGHT_ARROW: 39,
DOWN_ARROW: 40,
ENTER_KEY: 13,
ROWS: 3,
COLS: 3,
maxRow: 0,
maxCol: 0,
contentMaxRow: 0,
contentMaxCol: 0,
init: function() {
var self = this;
this.initContainer();
var inputs = $(".matrix-row .sol input[type='text']");
this.cells = _.map(inputs, function(input, i) {
return {
el: input,
index: i,
row: self.indexToRow(i),
col: self.indexToCol(i),
val: function() {
return $.trim($(this.el).val());
},
clearVal: function() {
$(this.el).val("");
}
};
});
this.addBrackets();
this.bindInputEvents();
this.resetAllMaxVals();
this.render();
},
initContainer: function() {
this.containerEl = $("#solutionarea .matrix-input");
if (!this.containerEl[0]) {
this.containerEl = $("#solutionarea").addClass("matrix-input");
}
},
addBrackets: function(i) {
var left = $("<div>").addClass("matrix-bracket bracket-left");
var right = $("<div>").addClass("matrix-bracket bracket-right");
this.containerEl.prepend(left, right);
this.bracketEls = [left, right];
},
removeBrackets: function() {
_.each(this.bracketEls, function(bracketEl) {
$(bracketEl).remove();
});
},
indexToRow: function(i) {
return Math.floor(i / this.COLS);
},
indexToCol: function(i) {
return i % this.COLS;
},
coordToIndex: function(row, col) {
return this.COLS * row + col;
},
bindInputEvents: function() {
// We reevaluate the highlighted area after:
// 1) clicking on some element besides the cells, or
// 2) tabbing to a new cell in the solution area
// This is sufficient since these are the only ways
// the user will get to change the value.
var self = this;
// Track whether or not a click originated in an input to avoid
// calling the global matrix sizing reset that we append to <body>
var clickedInput = false;
// case #1
$("body").on("click." + self.eventNamespace, function() {
if (!clickedInput) {
self.resetMaxToContentMax();
self.render();
}
clickedInput = false;
});
_.each(this.cells, function(cell) {
$(cell.el).on({
// case #2
focus: function(e) {
self.setMaxVals(cell);
self.render();
},
blur: function(e) {
self.setMaxVals(cell);
},
// case #1 (track whether or not a click originated in an
// input to avoid calling the click event bound to <body>)
click: function(e) {
clickedInput = true;
},
keydown: function(e) {
var LAST_ROW = self.ROWS - 1;
var LAST_INDEX = self.cells.length - 1;
var nextIndex = null;
var nextRow;
// cursor position only does something when you
// are at the start of the input, moving left, or
// at the end of the input, moving right
if (e.which === self.LEFT_ARROW) {
// don't do anything if at the first cell
// or if the cursor is not at the start
if (cell.index === 0 || !$(this).isCursorFirst()) {
return;
}
nextIndex = cell.index - 1;
} else if (e.which === self.RIGHT_ARROW) {
// don't do anything if at the last cell
// or if the cursor is not at the end of the input
// text
if (cell.index === LAST_INDEX ||
!$(this).isCursorLast()) {
return;
}
nextIndex = cell.index + 1;
} else if (e.which === self.UP_ARROW) {
// if already on first row, don't do anything
if (cell.row === 0) {
return;
}
nextRow = cell.row - 1;
nextIndex = self.coordToIndex(nextRow, cell.col);
} else if (e.which === self.DOWN_ARROW) {
// if on last row, don't do anything
if (cell.row === LAST_ROW) {
return;
}
nextRow = cell.row + 1;
nextIndex = self.coordToIndex(nextRow, cell.col);
// when submitting via enter key, make sure max vals
// are set properly
} else if (e.which === self.ENTER_KEY) {
self.setMaxVals(cell);
}
// let default behavior take place if we don't do
// anything
if (nextIndex === null) {
return;
}
// change focus to next input
$(self.cells[nextIndex].el).focus();
// don't let event bubble
e.preventDefault();
}
});
});
},
setContentMaxRow: function(val) {
this.contentMaxRow = Math.max(val, this.contentMaxRow);
},
setContentMaxCol: function(val) {
this.contentMaxCol = Math.max(val, this.contentMaxCol);
},
// maxRow/maxCol is the max of the currently selected element and the
// content max element
setMaxRow: function(val) {
this.maxRow = Math.max(val, this.contentMaxRow);
},
setMaxCol: function(val) {
this.maxCol = Math.max(val, this.contentMaxCol);
},
resetMaxToContentMax: function() {
this.maxRow = this.contentMaxRow;
this.maxCol = this.contentMaxCol;
},
resetAllMaxVals: function() {
this.maxRow = 0;
this.maxCol = 0;
this.contentMaxRow = 0;
this.contentMaxCol = 0;
},
setMaxValsFromScratch: function() {
// initialize to 0, since we want to start from scratch
this.resetAllMaxVals();
var self = this;
_.each(this.cells, function(cell) {
if (cell.val()) {
self.setContentMaxRow(cell.row);
self.setContentMaxCol(cell.col);
}
});
this.resetMaxToContentMax();
},
setMaxVals: function(cell) {
var val = cell.val();
// cell is nonempty
if (val) {
// only nonempty cell can be used to set content max values
// unless (see case below)
this.setContentMaxRow(cell.row);
this.setContentMaxCol(cell.col);
// cell is empty
} else {
// reset the contents of the cell when it's just spaces
cell.clearVal();
// if it was the cell responsible for a content max val(s),
// we need to find the new content max val(s)...
if (this.contentMaxRow === cell.row ||
this.contentMaxCol === cell.col) {
this.setMaxValsFromScratch();
}
}
// both nonempty and empty cells can set absolute max values
this.setMaxRow(cell.row);
this.setMaxCol(cell.col);
},
// position matrix brackets based on bounds
positionBrackets: function() {
var cell = $(this.cells[0].el);
var bracketWidth = this.bracketEls[0].width();
var rows = this.maxRow + 1;
var cols = this.maxCol + 1;
var height = cell.outerHeight(true) * rows;
var marginLeft = cell.outerWidth(true) * cols - bracketWidth;
_.each(this.bracketEls, function($el) {
$el.css({
"height": height
});
});
// right bracket
this.bracketEls[1].css({
"margin-left": marginLeft
});
},
render: function() {
this.positionBrackets();
},
cleanup: function() {
$("body").off("." + this.eventNamespace);
this.removeBrackets();
}
}
});
$.fn["matrix-inputLoad"] = function() {
if (KhanUtil.matrixInput.eventsAttached) {
return;
}
$(Exercises).on("newProblem.matrix-input", function() {
KhanUtil.matrixInput.init();
});
$(Khan).on("showGuess.matrix-input", function() {
KhanUtil.matrixInput.setMaxValsFromScratch();
KhanUtil.matrixInput.render();
});
KhanUtil.matrixInput.eventsAttached = true;
};
$.fn["matrix-inputCleanup"] = function() {
if (!KhanUtil.matrixInput.eventsAttached) {
return;
}
KhanUtil.matrixInput.cleanup();
$(Exercises).off("newProblem.matrix-input");
$(Khan).off("showGuess.matrix-input");
KhanUtil.matrixInput.eventsAttached = false;
};
});