forked from h5p/h5p-editor-drag-question
-
Notifications
You must be signed in to change notification settings - Fork 0
/
H5PEditor.DynamicCheckboxes.js
178 lines (160 loc) · 4.72 KB
/
H5PEditor.DynamicCheckboxes.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
var H5PEditor = H5PEditor || {};
/**
* Editor widget module for dynamic value checkboxes.
*
* Displays a list of checkboxes, and the list is regenerated each time the
* field is set as active unlike H5PEditor.select where the options are
* generated when the field is initialized, and after that stays the same.
*
* Other fields may change the options in dynamicCheckboxes
*/
H5PEditor.widgets.dynamicCheckboxes = H5PEditor.DynamicCheckboxes = (function ($) {
/**
* Initialize widget.
*
* @param {Object} parent
* @param {Object} field
* @param {Object} params
* @param {function} setValue
* @returns {_L8.C}
*/
function C(parent, field, params, setValue) {
this.parent = parent;
this.field = field;
this.setValue = setValue;
if (params === undefined) {
if (this.field.multiple) {
this.params = [];
}
else {
this.params = '';
}
setValue(field, this.params);
}
else {
this.params = params;
}
}
/**
* Append widget to form.
*
* @param {jQuery} $wrapper
* @returns {undefined}
*/
C.prototype.appendTo = function ($wrapper) {
this.$item = $(H5PEditor.createFieldMarkup(this.field)).appendTo($wrapper);
this.$errors = this.$item.children('.h5p-errors');
};
/**
* The widget is set as active.
* (re)Generate options.
*
* @returns {undefined}
*/
C.prototype.setActive = function () {
var that = this,
html = '',
i, j, option, selected;
for (i = 0; i < this.field.options.length; i++) {
option = this.field.options[i];
selected = false;
if (this.field.multiple) {
// Check if selected
for (j = 0; j < this.params.length; j++) {
if (this.params[j] === option.value) {
selected = true;
break;
}
}
html += '<li><label class="h5p-editor-label"><input type="checkbox" value="' + option.value + '"' + (selected ? ' checked="checked"' : '') + '/><div class="h5p-label-text">' + option.label + '</div></label></li>';
}
else {
// Check if selected
if (this.params === option.value) {
selected = true;
}
html += '<li><label class="h5p-editor-label"><input type="radio" name="dynboxradio1" value="' + option.value + '"' + (selected ? ' checked="checked"' : '') + '/>' + option.label + '</label></li>';
}
}
this.$item.html(html ? '<div class="h5peditor-label">' + this.field.label + '</div>' + (this.field.multiple ? '<a href="#" class="h5p-selectall">' + H5PEditor.t('H5PEditor.DragQuestion', 'selectAll') + '</a>' : '') + '<ul class="h5peditor-dynamiccheckboxes-select">' + html + '</ul>' : '');
var updateSelectAll, $a, $checkboxes = this.$item.find('input').change(function () {
that.change($(this));
// If all is checked change select all button.
updateSelectAll();
});
$a = this.$item.find('.h5p-selectall').click(function () {
if ($a.hasClass('h5p-deselectall')) {
$checkboxes.each(function () {
var $this = $(this);
if ($this.is(':checked')) {
$this.prop('checked', false).change();
}
});
}
else {
$checkboxes.each(function () {
var $this = $(this);
if (!$this.is(':checked')) {
$this.prop('checked', true).change();
}
});
}
return false;
});
updateSelectAll = function () {
if ($checkboxes.length) {
if ($checkboxes.length === $checkboxes.filter(':checked').length) {
$a.addClass('h5p-deselectall').text(H5PEditor.t('H5PEditor.DragQuestion', 'deselectAll'));
}
else {
$a.removeClass('h5p-deselectall').text(H5PEditor.t('H5PEditor.DragQuestion', 'selectAll'));
}
}
};
updateSelectAll();
};
/**
* Update params with changes to checkbox.
*
* @param {jQuery} $input
* @returns {undefined}
*/
C.prototype.change = function ($input) {
var i, value = $input.val();
if (this.field.multiple) {
if ($input.is(':checked')) {
this.params.push(value);
}
else {
for (i = 0; i < this.params.length; i++) {
if (this.params[i] === value) {
this.params.splice(i, 1);
}
}
}
}
else {
if ($input.is(':checked')) {
this.params = value;
this.setValue(this.field, value);
}
}
};
/**
* Validate the current field.
*
* @returns {Boolean}
*/
C.prototype.validate = function () {
return true;
};
/**
*
* @returns {undefined}
*/
C.prototype.remove = function () {
this.$item.remove();
};
return C;
})(H5P.jQuery);
// Get translations from H5PEditor.DragQuestion