| 1 |
efrain |
1 |
/* global ns */
|
|
|
2 |
/**
|
|
|
3 |
* Creates a boolean field for the editor.
|
|
|
4 |
*
|
|
|
5 |
* @param {mixed} parent
|
|
|
6 |
* @param {object} field
|
|
|
7 |
* @param {mixed} params
|
|
|
8 |
* @param {function} setValue
|
|
|
9 |
* @returns {ns.Boolean}
|
|
|
10 |
*/
|
|
|
11 |
ns.Boolean = function (parent, field, params, setValue) {
|
|
|
12 |
if (params === undefined) {
|
|
|
13 |
this.value = false;
|
|
|
14 |
setValue(field, this.value);
|
|
|
15 |
}
|
|
|
16 |
else {
|
|
|
17 |
this.value = params;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
this.field = field;
|
|
|
21 |
this.setValue = setValue;
|
|
|
22 |
|
|
|
23 |
// Setup event dispatching on change
|
|
|
24 |
this.changes = [];
|
|
|
25 |
this.triggerListeners = function (value) {
|
|
|
26 |
// Run callbacks
|
|
|
27 |
for (var i = 0; i < this.changes.length; i++) {
|
|
|
28 |
this.changes[i](value);
|
|
|
29 |
}
|
|
|
30 |
};
|
|
|
31 |
};
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Create HTML for the boolean field.
|
|
|
35 |
*/
|
|
|
36 |
ns.Boolean.prototype.createHtml = function () {
|
|
|
37 |
const id = ns.getNextFieldId(this.field);
|
|
|
38 |
const checked = (this.value !== undefined && this.value) ? ' checked' : '';
|
|
|
39 |
let content = '<input id="' + id + '" type="checkbox"';
|
|
|
40 |
if (this.field.description !== undefined) {
|
|
|
41 |
content += ' aria-describedby="' + ns.getDescriptionId(id) + '"';
|
|
|
42 |
}
|
|
|
43 |
if (this.value !== undefined && this.value) {
|
|
|
44 |
content += ' checked';
|
|
|
45 |
}
|
|
|
46 |
content += '/>';
|
|
|
47 |
return ns.createBooleanFieldMarkup(this.field, content, id);
|
|
|
48 |
};
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* "Validate" the current boolean field.
|
|
|
52 |
*/
|
|
|
53 |
ns.Boolean.prototype.validate = function () {
|
|
|
54 |
return true;
|
|
|
55 |
};
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Append the boolean field to the given wrapper.
|
|
|
59 |
*
|
|
|
60 |
* @param {jQuery} $wrapper
|
|
|
61 |
* @returns {undefined}
|
|
|
62 |
*/
|
|
|
63 |
ns.Boolean.prototype.appendTo = function ($wrapper) {
|
|
|
64 |
var that = this;
|
|
|
65 |
|
|
|
66 |
this.$item = ns.$(this.createHtml()).appendTo($wrapper);
|
|
|
67 |
this.$input = this.$item.find('input');
|
|
|
68 |
this.$errors = this.$item.find('.h5p-errors');
|
|
|
69 |
|
|
|
70 |
this.$input.change(function () {
|
|
|
71 |
// Validate
|
|
|
72 |
that.value = that.$input.is(':checked');
|
|
|
73 |
that.setValue(that.field, that.value);
|
|
|
74 |
that.triggerListeners(that.value);
|
|
|
75 |
});
|
|
|
76 |
};
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Remove this item.
|
|
|
80 |
*/
|
|
|
81 |
ns.Boolean.prototype.remove = function () {
|
|
|
82 |
this.$item.remove();
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
// Tell the editor what widget we are.
|
|
|
86 |
ns.widgets['boolean'] = ns.Boolean;
|