1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* A module that enables the setting of form field values on the client side.
|
|
|
18 |
*
|
|
|
19 |
* @module mod_lti/form-field
|
|
|
20 |
* @copyright 2016 Jun Pataleta <jun@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @since 3.2
|
|
|
23 |
*/
|
|
|
24 |
define(['jquery'],
|
|
|
25 |
function($) {
|
|
|
26 |
/**
|
|
|
27 |
* Form field class.
|
|
|
28 |
*
|
|
|
29 |
* @param {string} name Field name.
|
|
|
30 |
* @param {number} type The field type.
|
|
|
31 |
* @param {boolean} resetIfUndefined Flag to reset the field to the default value if undefined in the return data.
|
|
|
32 |
* @param {string|number|boolean} defaultValue The default value to use for the field.
|
|
|
33 |
* @constructor
|
|
|
34 |
*/
|
|
|
35 |
var FormField = function(name, type, resetIfUndefined, defaultValue) {
|
|
|
36 |
this.name = name;
|
|
|
37 |
this.id = 'id_' + this.name;
|
|
|
38 |
this.selector = '#' + this.id;
|
|
|
39 |
this.type = type;
|
|
|
40 |
this.resetIfUndefined = resetIfUndefined;
|
|
|
41 |
this.defaultValue = defaultValue;
|
|
|
42 |
};
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Form field types.
|
|
|
46 |
*
|
|
|
47 |
* @type {{TEXT: number, SELECT: number, CHECKBOX: number, EDITOR: number}}
|
|
|
48 |
*/
|
|
|
49 |
FormField.TYPES = {
|
|
|
50 |
TEXT: 1,
|
|
|
51 |
SELECT: 2,
|
|
|
52 |
CHECKBOX: 3,
|
|
|
53 |
EDITOR: 4
|
|
|
54 |
};
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Sets the values for a form field.
|
|
|
58 |
*
|
|
|
59 |
* @param {string|boolean|number} value The value to be set into the field.
|
|
|
60 |
*/
|
|
|
61 |
FormField.prototype.setFieldValue = function(value) {
|
|
|
62 |
if (value === null) {
|
|
|
63 |
if (this.resetIfUndefined) {
|
|
|
64 |
value = this.defaultValue;
|
|
|
65 |
} else {
|
|
|
66 |
// No need set the field value if value is null and there's no need to reset the field.
|
|
|
67 |
return;
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
switch (this.type) {
|
|
|
72 |
case FormField.TYPES.CHECKBOX:
|
|
|
73 |
if (value) {
|
|
|
74 |
$(this.selector).prop('checked', true);
|
|
|
75 |
} else {
|
|
|
76 |
$(this.selector).prop('checked', false);
|
|
|
77 |
}
|
|
|
78 |
break;
|
|
|
79 |
case FormField.TYPES.EDITOR:
|
|
|
80 |
if (typeof value.text !== 'undefined') {
|
|
|
81 |
/* global tinyMCE:false */
|
|
|
82 |
|
|
|
83 |
// Set text in editor's editable content, if applicable.
|
|
|
84 |
// Check if it is an Atto editor.
|
|
|
85 |
var attoEditor = $(this.selector + 'editable');
|
|
|
86 |
if (attoEditor.length) {
|
|
|
87 |
attoEditor.html(value.text);
|
|
|
88 |
} else if (typeof tinyMCE !== 'undefined') {
|
|
|
89 |
// If the editor is not Atto, try to fallback to TinyMCE.
|
|
|
90 |
if (tinyMCE.majorVersion == "3") {
|
|
|
91 |
// Tiny 3.
|
|
|
92 |
tinyMCE.execInstanceCommand(this.id, 'mceInsertContent', false, value.text);
|
|
|
93 |
} else {
|
|
|
94 |
// Tiny 4+.
|
|
|
95 |
tinyMCE.get(this.id).setContent(value.text);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Set text to actual editor text area.
|
|
|
100 |
$(this.selector).val(value.text);
|
|
|
101 |
}
|
|
|
102 |
break;
|
|
|
103 |
default:
|
|
|
104 |
$(this.selector).val(value);
|
|
|
105 |
break;
|
|
|
106 |
}
|
|
|
107 |
};
|
|
|
108 |
|
|
|
109 |
return FormField;
|
|
|
110 |
}
|
|
|
111 |
);
|