-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSXQuestion.js
More file actions
478 lines (420 loc) · 15.8 KB
/
JSXQuestion.js
File metadata and controls
478 lines (420 loc) · 15.8 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* MIT License
* Copyright (c) 2020 JSXGraph
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* This is a plugin to enable function plotting and dynamic geometry constructions with JSXGraph
* within a formulas question of Moodle platform.
*
* JSXGraph is a cross-browser JavaScript library for interactive geometry,
* function plotting, charting, and data visualization in the web browser.
* JSXGraph is implemented in pure JavaScript and does not rely on any other
* library. Special care has been taken to optimize the performance.
*
* @package filter_jsxgraph
* @copyright 2023 JSXGraph team - Center for Mobile Learning with Digital Technology – Universität Bayreuth
* Andreas Walter <andreas.walter@uni-bayreuth.de>,
* Alfred Wassermann <alfred.wassermann@uni-bayreuth.de>
* based on work by Tim Kos and Marc Bernat
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
'use strict';
// For code prechecks:
var JXG = JXG || {};
/**
* @param {String|String[]} boardIDs ID of the HTML element containing the JSXGraph board. Has to be set with local const BOARDID0.
* If more than one board is used, an array of BOARDIDS must be given.
* @param {Function} jsxGraphCode JavaScript function containing the construction code.
* @param {Boolean} [allowInputEntry=false] Should the original inputs from formulas be displayed and linked to the construction?
* @param {Number} [decimalPrecision=2] Number of digits to round to.
*/
var JSXQuestion = function(boardIDs, jsxGraphCode, allowInputEntry, decimalPrecision) {
var that = this,
topEl;
if (allowInputEntry === undefined || allowInputEntry === null) {
allowInputEntry = false;
}
if (decimalPrecision === undefined || decimalPrecision === null) {
decimalPrecision = 2;
}
/**
* Array with the IDs of the boards.
* @type {String[]}
*/
this.BOARDIDS = boardIDs;
if (!JXG.isArray(boardIDs)) {
this.BOARDIDS = [boardIDs];
}
/**
* ID of the first board.
* @type {String}
*/
this.firstBOARDID = this.BOARDIDS[0];
/**
* ID of the *first* board.
* @type {String}
* @deprecated use array {@see BOARDIDS} instead
*/
this.BOARDID = this.firstBOARDID;
/**
* HTML element containing the board.
* @deprecated
* @type {HTMLElement}
*/
this.elm = document.getElementById(this.firstBOARDID);
// Get the first ancestor of the board having class ".formulaspart" (should be a div).
// ATTENTION!!! The class used here depends on formulas and can make the extension useless when updating formulas!
topEl = this.elm.closest('.formulaspart');
/**
* Stores the input tags from the formulas question.
* @type {HTMLElement[]}
*/
this.inputs = topEl.querySelectorAll('input');
// Hide the input elements
if (allowInputEntry) {
this.inputs.forEach(function(el) {
el.addEventListener('input', function() {
if (!that.board.inUpdate) {
that.update();
}
});
});
this.inputs.forEach(function(el) {
el.addEventListener('change', function() {
if (!that.board.inUpdate) {
that.update();
}
});
});
} else {
this.inputs.forEach(function(el) {
el.style.display = 'none';
});
}
if (Object.defineProperties) {
var boards = [];
Object.defineProperties(this, {
boards: {
'default': [],
get: function() {
return boards;
},
set: function(value) {
if (JXG.isArray(value)) {
boards = value;
} else {
boards = [value];
}
},
},
firstBoard: {
'default': null,
get: function() {
return boards[0];
},
set: function(value) {
boards = [value];
},
},
board: {
'default': null,
get: function() {
return boards[0];
},
set: function(value) {
boards = [value];
},
},
brd: {
'default': null,
get: function() {
return boards[0];
},
set: function(value) {
boards = [value];
},
},
});
} else {
/**
* Array with the stored JSXGraph boards.
* @type {JXG.Board[]}
*/
this.boards = [];
/**
* Stored *first* JSXGraph board.
* @type {JXG.Board}
*/
this.firstBoard = null;
/**
* Stored *first* JSXGraph board.
* @type {JXG.Board}
*/
this.board = this.firstBoard;
/**
* @type {JXG.Board}
* @deprecated use attribute {@see board} instead
*/
this.brd = this.firstBoard;
}
/**
* Alias for function initBoards and initAndAddBoard. Returns only the first board (for backward compatibility).
* @see JSXQuestion.initBoards
* @see JSXQuestion.initAndAddBoard
*
* @param {String|Object|Object[]} firstParam Attributes for function JXG.JSXGraph.initBoard(...).
* Can also be an array. Otherwise each board gets the same attributes.
* @param {Object|Object[]} secondParam Guarantees compatibility with the function JXG.JSXGraph.initBoard(...).
* The ID that was then passed in the first parameter is ignored!
* Can also be an array. Otherwise each board gets the same attributes.
*
* @returns {JXG.Board} JSXGraph board
*/
this.initBoard = function(firstParam, secondParam) {
if (JXG.isString(firstParam)) { /* Parameter firstParam is id. */
return that.initAndAddBoard(firstParam, secondParam);
} else /* Parameter firstParam are attributes. */ if (!JXG.isArray(firstParam)) {
return that.initAndAddBoard(that.BOARDIDS[that.boards.length], firstParam);
} else {
return that.initBoards(firstParam, secondParam)[0];
}
};
/**
* Initializes the board(s), saves it/them in the attributes of JSXQuestion and returns an array of boards.
*
* @param {Object|Object[]} [attributes={}] Attributes for function JXG.JSXGraph.initBoard(...).
* Can also be an array. Otherwise each board gets the same attributes.
* @param {Object|Object[]} [attributesIfBoxIsGiven={}] Guarantees compatibility with the function JXG.JSXGraph.initBoard(...).
* The ID that was then passed in the first parameter is ignored!
* Can also be an array. Otherwise each board gets the same attributes.
*
* @returns {JXG.Board[]} JSXGraph board
*/
this.initBoards = function(attributes, attributesIfBoxIsGiven) {
var board, attr, i;
if (attributes === undefined || attributes === null) {
attributes = {};
}
if (attributesIfBoxIsGiven === undefined || attributesIfBoxIsGiven === null) {
attributesIfBoxIsGiven = {};
}
if (JXG.isString(attributes)) { // Backward compatibility!
attributes = attributesIfBoxIsGiven;
}
if (!JXG.isArray(attributes)) {
attributes = [attributes];
}
// From here attributes is an array.
for (i = 0; i < that.boards.length; i++) {
JXG.JSXGraph.freeBoard(that.boards[i]);
}
that.boards = [];
for (i = 0; i < that.BOARDIDS.length; i++) {
attr = attributes[i] || attributes[0]; // First attributes are default.
attr.id = attr.id ?? that.BOARDIDS[i] + '_board';
board = JXG.JSXGraph.initBoard(that.BOARDIDS[i], attr);
that.boards.push(board);
}
return that.boards;
};
/**
* Initialize and add one board to the boards array. Returns the just added board.
*
* @param {String} id Id of the board.
* @param {Object} [attributes={}] Attributes for function JXG.JSXGraph.initBoard(...).
*
* @returns {JXG.Board} JSXGraph board
*/
this.initAndAddBoard = function(id, attributes) {
var index = that.BOARDIDS.indexOf(id);
if (index === -1) {
throw new Error('The id "' + id + '" was not passed in Initialization of JSXQuestion.');
}
if (attributes === undefined || attributes === null) {
attributes = {};
}
if (JXG.exists(that.boards[index])) {
JXG.JSXGraph.freeBoard(that.boards[index]);
}
attributes.id = attributes.id ?? that.BOARDIDS[index] + '_board';
that.boards[index] = JXG.JSXGraph.initBoard(that.BOARDIDS[index], attributes);
return that.boards[index];
};
/**
* Calls the function addChild ascending for each board.
* After this function boards[0] is child of boards[1], boards[1] is child of boards[2] etc.
*/
this.addChildsAsc = function() {
var i;
for (i = that.boards.length - 1; i > 0; i--) {
that.boards[i].addChild(that.boards[i - 1]);
}
};
/**
* Calls the function addChild descending for each board.
* After this function boards[0] is parent of boards[1], boards[1] is parent of boards[2] etc.
*/
this.addChildsDesc = function() {
var i;
for (i = 0; i < that.boards.length - 1; i++) {
that.boards[i].addChild(that.boards[i + 1]);
}
};
/**
* Links the board to the inputs. If a change has been made in the board,
* the input with the number inputNumber is assigned the value that the function valueFunction returns.
*
* @param {Number} inputNumber
* @param {Function} valueFunction
*/
this.bindInput = function(inputNumber, valueFunction) {
var i;
for (i = 0; i < that.boards.length; i++) {
that.boards[i].on('up', function() {
that.set(inputNumber, valueFunction());
});
that.boards[i].update();
that.boards[i].triggerEventHandlers(['up'], []);
}
};
/**
* Indicator if the question has been solved.
* @type {Boolean}
*/
this.isSolved = false;
if (this.inputs && this.inputs[0]) {
this.isSolved = this.inputs[0].readOnly;
}
/**
* @deprecated use attribute {@see isSolved} instead
* @type {Boolean}
*/
this.solved = this.isSolved;
/**
* Fill input element of index inputNumber of the formulas question with value.
*
* @param {Number} inputNumber Index of the input element, starting at 0.
* @param {Number} value Number to be set.
*/
this.set = function(inputNumber, value) {
if (!that.isSolved && that.inputs && that.inputs[inputNumber]) {
that.inputs[inputNumber].value = Math.round(value * Math.pow(10, decimalPrecision)) / Math.pow(10, decimalPrecision);
}
};
/**
* Set values for all formulas input fields
*
* @param {Number[]} values Array containing the numbers to be set.
*/
this.setAllValues = function(values) {
var inputNumber,
len = values.length;
for (inputNumber = 0; inputNumber < len; inputNumber++) {
that.set(inputNumber, values[inputNumber]);
}
};
/**
* Get the content of input element of index inputNumber of the formulas question.
*
* @param {Number} inputNumber Index of the input form, starting at 0.
* @param {Number} [defaultValue=0] Number that is returned if the value of the input could not be read or is not a number.
*
* @returns {Number} Entry of the formulas input field.
*/
this.get = function(inputNumber, defaultValue) {
var n;
if (defaultValue === undefined || defaultValue === null) {
defaultValue = 0;
}
if (that.inputs && that.inputs[inputNumber]) {
n = parseFloat(that.inputs[inputNumber].value);
if (!isNaN(n)) {
return Math.round(n * Math.pow(10, decimalPrecision)) / Math.pow(10, decimalPrecision);
}
}
return defaultValue;
};
/**
* Fetch all values from the formulas input fields.
*
* @param {Number|Number[]} [defaultValues=0] Default values if the fields are empty.
*
* @returns {Number[]} Array containing the entries of all associated formulas input fields.
*/
this.getAllValues = function(defaultValues) {
var inputNumber,
len = that.inputs.length,
values = [],
defaultValue;
if (defaultValues === undefined || defaultValues === null) {
defaultValues = 0;
}
if (Array.isArray(defaultValues)) {
if (defaultValues.length !== len) {
return null;
}
} else {
if (isNaN(defaultValues)) {
return null;
} else {
defaultValue = defaultValues;
}
}
for (inputNumber = 0; inputNumber < len; inputNumber++) {
values.push(
that.get(inputNumber, defaultValue || defaultValues[inputNumber])
);
}
return values;
};
/**
* Reload the construction.
*/
this.reload = this.update = function() {
var i;
for (i = 0; i < that.boards.length; i++) {
that.boards[i].update();
}
jsxGraphCode(that);
};
// Execute the JSXGraph JavaScript code.
this.update();
};
JSXQuestion.toString = function() {
return JSXQuestion.name;
};
// Polyfill for element.closest:
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector;
}
if (!Element.prototype.closest) {
Element.prototype.closest = function(s) {
var el = this;
do {
if (Element.prototype.matches.call(el, s)) {
return el;
}
el = el.parentElement || el.parentNode;
} while (el !== null && el.nodeType === 1);
return null;
};
}