1 |
efrain |
1 |
/* global ns */
|
|
|
2 |
/**
|
|
|
3 |
* Create a text field for the form.
|
|
|
4 |
*
|
|
|
5 |
* @param {mixed} parent
|
|
|
6 |
* @param {Object} field
|
|
|
7 |
* @param {mixed} params
|
|
|
8 |
* @param {function} setValue
|
|
|
9 |
* @returns {ns.Text}
|
|
|
10 |
*/
|
|
|
11 |
ns.Text = function (parent, field, params, setValue) {
|
|
|
12 |
this.field = field;
|
|
|
13 |
this.value = params;
|
|
|
14 |
this.setValue = setValue;
|
|
|
15 |
this.changeCallbacks = [];
|
|
|
16 |
};
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Append field to wrapper.
|
|
|
20 |
*
|
|
|
21 |
* @param {type} $wrapper
|
|
|
22 |
* @returns {undefined}
|
|
|
23 |
*/
|
|
|
24 |
ns.Text.prototype.appendTo = function ($wrapper) {
|
|
|
25 |
var that = this;
|
|
|
26 |
|
|
|
27 |
this.$item = ns.$(this.createHtml()).appendTo($wrapper);
|
|
|
28 |
this.$input = this.$item.find('input');
|
|
|
29 |
this.$errors = this.$item.children('.h5p-errors');
|
|
|
30 |
|
|
|
31 |
this.$input.change(function () {
|
|
|
32 |
// Validate
|
|
|
33 |
var value = that.validate();
|
|
|
34 |
|
|
|
35 |
if (value !== false) {
|
|
|
36 |
// Set param
|
|
|
37 |
if (H5P.trim(value) === '') {
|
|
|
38 |
// Avoid storing empty strings. (will be valid if field is optional)
|
|
|
39 |
delete that.value;
|
|
|
40 |
that.setValue(that.field);
|
|
|
41 |
}
|
|
|
42 |
else {
|
|
|
43 |
that.value = value;
|
|
|
44 |
that.setValue(that.field, ns.htmlspecialchars(value));
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
for (var i = 0; i < that.changeCallbacks.length; i++) {
|
|
|
48 |
that.changeCallbacks[i](value);
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
});
|
|
|
52 |
};
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Run callback when value changes.
|
|
|
56 |
*
|
|
|
57 |
* @param {function} callback
|
|
|
58 |
* @returns {Number|@pro;length@this.changeCallbacks}
|
|
|
59 |
*/
|
|
|
60 |
ns.Text.prototype.change = function (callback) {
|
|
|
61 |
this.changeCallbacks.push(callback);
|
|
|
62 |
callback();
|
|
|
63 |
|
|
|
64 |
return this.changeCallbacks.length - 1;
|
|
|
65 |
};
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Create HTML for the text field.
|
|
|
69 |
*/
|
|
|
70 |
ns.Text.prototype.createHtml = function () {
|
|
|
71 |
const id = ns.getNextFieldId(this.field);
|
|
|
72 |
const descriptionId = (this.field.description !== undefined ? ns.getDescriptionId(id) : undefined)
|
|
|
73 |
var input = ns.createText(this.value, this.field.maxLength, this.field.placeholder, id, descriptionId);
|
|
|
74 |
return ns.createFieldMarkup(this.field, input, id);
|
|
|
75 |
};
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Validate the current text field.
|
|
|
79 |
*/
|
|
|
80 |
ns.Text.prototype.validate = function () {
|
|
|
81 |
var that = this;
|
|
|
82 |
|
|
|
83 |
var value = H5P.trim(this.$input.val());
|
|
|
84 |
var valid = true;
|
|
|
85 |
|
|
|
86 |
// Clear errors before showing new ones
|
|
|
87 |
this.$errors.html('');
|
|
|
88 |
|
|
|
89 |
if ((that.field.optional === undefined || !that.field.optional) && !value.length) {
|
|
|
90 |
this.$errors.append(ns.createError(ns.t('core', 'requiredProperty', {':property': ns.t('core', 'textField')})));
|
|
|
91 |
valid = false;
|
|
|
92 |
}
|
|
|
93 |
else if (value.length > this.field.maxLength) {
|
|
|
94 |
this.$errors.append(ns.createError(ns.t('core', 'tooLong', {':max': this.field.maxLength})));
|
|
|
95 |
valid = false;
|
|
|
96 |
}
|
|
|
97 |
else if (this.field.regexp !== undefined && value.length && !value.match(new RegExp(this.field.regexp.pattern, this.field.regexp.modifiers))) {
|
|
|
98 |
this.$errors.append(ns.createError(ns.t('core', 'invalidFormat')));
|
|
|
99 |
valid = false;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
this.$input.toggleClass('error', !valid);
|
|
|
103 |
|
|
|
104 |
return ns.checkErrors(this.$errors, this.$input, value);
|
|
|
105 |
};
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Remove this item.
|
|
|
109 |
*/
|
|
|
110 |
ns.Text.prototype.remove = function () {
|
|
|
111 |
this.$item.remove();
|
|
|
112 |
};
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* When someone from the outside wants to set a value.
|
|
|
116 |
*
|
|
|
117 |
* @param {string} value
|
|
|
118 |
*/
|
|
|
119 |
ns.Text.prototype.forceValue = function (value) {
|
|
|
120 |
this.$input.val(value).change();
|
|
|
121 |
};
|
|
|
122 |
|
|
|
123 |
// Tell the editor what widget we are.
|
|
|
124 |
ns.widgets.text = ns.Text;
|