forked from h5p/h5p-text-input-field
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-input-field.js
185 lines (158 loc) · 5.79 KB
/
text-input-field.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
179
180
181
182
183
184
185
var H5P = H5P || {};
/**
* Text Input Field module
* @external {jQuery} $ H5P.jQuery
*/
H5P.TextInputField = (function ($) {
// CSS Classes:
var MAIN_CONTAINER = 'h5p-text-input-field';
var INPUT_LABEL = 'h5p-text-input-field-label';
var INPUT_FIELD = 'h5p-text-input-field-textfield';
var WRAPPER_MESSAGE = 'h5p-text-input-field-message-wrapper';
var CHAR_MESSAGE = 'h5p-text-input-field-message-char';
var ariaId = 0;
/**
* Initialize module.
* @param {Object} params Behavior settings
* @param {Number} id Content identification
* @returns {Object} TextInputField TextInputField instance
*/
function TextInputField(params, id, contentData) {
this.$ = $(this);
this.id = id;
this.contentData = contentData;
// Set default behavior.
this.params = $.extend({}, {
taskDescription: 'Input field',
placeholderText: '',
inputFieldSize: '1',
requiredField: false
}, params);
// Sanitize the task description as it comes in HTML
this.XAPIGenerator = new H5P.TextInputField.XAPIGenerator(this.params.taskDescription.replace(/^\s+|\s+$/g, '').replace(/(<p>|<\/p>)/img, ""));
// Set the maximum length for the textarea
this.maxTextLength = (typeof this.params.maximumLength === 'undefined') ? '' : parseInt(this.params.maximumLength, 10);
// Get previous state
if (this.contentData !== undefined && this.contentData.previousState !== undefined) {
this.previousState = this.contentData.previousState;
}
ariaId++;
}
/**
* Attach function called by H5P framework to insert H5P content into page.
*
* @param {jQuery} $container The container which will be appended to.
*/
TextInputField.prototype.attach = function ($container) {
var self = this;
this.$inner = $container.addClass(MAIN_CONTAINER);
this.$taskDescription = $('<div>', {
id: ariaId,
'class': INPUT_LABEL + (this.params.requiredField ? ' required' : ''),
'html': self.params.taskDescription
}).appendTo(self.$inner);
this.$inputField = $('<textarea>', {
'class': INPUT_FIELD,
'rows': parseInt(self.params.inputFieldSize, 10),
'maxlength': self.maxTextLength,
'placeholder': self.params.placeholderText,
'tabindex': '0',
'aria-required': this.params.requiredField,
'aria-labelledby': ariaId
}).appendTo(self.$inner);
// set state from previous one
this.setState(this.previousState);
var $wrapperMessage = $('<div>', {'class': WRAPPER_MESSAGE}).appendTo(self.$inner);
this.$charMessage = $('<div>', {'class': CHAR_MESSAGE}).appendTo($wrapperMessage);
this.$inputField.on('change keyup paste', function() {
if (self.params.maximumLength !== undefined) {
self.$charMessage.html(self.params.remainingChars.replace(/@chars/g, self.computeRemainingChars()));
}
});
this.$inputField.blur(function() {
var xApiTemplate = self.createXAPIEventTemplate('interacted');
var xApiEvent = self.XAPIGenerator.generateXApi(xApiTemplate, self.$inputField.val());
self.trigger(xApiEvent);
});
this.$inputField.trigger('change');
};
/**
* Returns true if input field is not required or non-empty
* @returns {boolean} True if input field is filled or not required
*/
TextInputField.prototype.isRequiredInputFilled = function () {
if (!this.params.requiredField) {
return true;
}
if (this.params.requiredField && this.$inputField.val().length) {
return true;
}
return false;
};
/**
* Retrieves the text input field
* @returns {description:string, value:string} Returns input field
*/
TextInputField.prototype.getInput = function () {
// Strip away HTML from description:
var descriptionDoc = new DOMParser().parseFromString(this.params.taskDescription, 'text/html');
var description = descriptionDoc.body.textContent || "";
// Remove trailing newlines
return {
description: description.replace(/^\s+|\s+$/g, '').replace(/(<p>|<\/p>)/img, ""),
value: this.$inputField.val()
};
};
/**
* Get current state for H5P.Question.
* @return {object} Current state.
*/
TextInputField.prototype.getCurrentState = function () {
// We could have just uses a string, but you never know when you need to store more parameters
return {
'inputField': this.$inputField.val()
};
};
/**
* Set state from previous state.
* @param {object} previousState - PreviousState.
*/
TextInputField.prototype.setState = function (previousState) {
var self = this;
if (previousState === undefined) {
return;
}
if (typeof previousState === 'object' && !Array.isArray(previousState)) {
self.$inputField.html(previousState.inputField || '');
}
};
/**
* Compute the remaining number of characters
* @returns {number} Returns number of characters left
*/
TextInputField.prototype.computeRemainingChars = function () {
return this.maxTextLength - this.$inputField.val().length;
};
/**
* Triggers an 'answered' xAPI event for all inputs
*/
TextInputField.prototype.triggerAnsweredEvent = function () {
var xApiTemplate = this.createXAPIEventTemplate('answered');
var xApiEvent = this.XAPIGenerator.generateXApi(xApiTemplate, this.getCurrentState().inputField);
this.trigger(xApiEvent);
};
/**
* Get xAPI data.
* Contract used by report rendering engine.
*
* @see contract at {@link https://h5p.org/documentation/developers/contracts#guides-header-6}
*/
TextInputField.prototype.getXAPIData = function () {
var xApiTemplate = this.createXAPIEventTemplate('answered');
var xApiEvent = this.XAPIGenerator.generateXApi(xApiTemplate, this.getCurrentState().inputField);
return {
statement: xApiEvent.data.statement
};
};
return TextInputField;
}(H5P.jQuery));