Debian Codegraph @

resources/src/mediawiki.special.createaccount/HtmlformChecker.js

raw symbols SCIP
1 const util = require( 'mediawiki.util' );
2
3 /**
4 * A helper class to add validation to non-OOUI HTMLForm fields.
5 *
6 * @private
7 * @class HtmlformChecker
8 *
9 * @constructor
10 * @param {jQuery} $element Form field generated by HTMLForm
11 * @param {Function} validator Validation callback
12 * @param {string} validator.value Value of the form field to be validated
13 * @param {jQuery.Promise} validator.return The promise should be resolved
14 * with an object with two properties: Boolean 'valid' to indicate success
15 * or failure of validation, and an array (containing HTML strings or
16 * jQuery collections) 'messages' to be passed to setErrors() on failure.
17 */
18 function HtmlformChecker( $element, validator ) {
19 this.validator = validator;
20 this.$element = $element;
21
22 this.$errorBox = $element.next( '.html-form-error' );
23 if ( !this.$errorBox.length ) {
24 this.$errorBox = $( '<div>' ).addClass( 'html-form-error' );
25 this.$errorBox.hide();
26 $element.after( this.$errorBox );
27 }
28
29 this.currentValue = this.$element.val();
30 }
31
32 /**
33 * Attach validation events to the form element
34 *
35 * @param {jQuery} [$extraElements] Additional elements to listen for change
36 * events on.
37 * @return {HtmlformChecker}
38 * @chainable
39 */
40 HtmlformChecker.prototype.attach = function ( $extraElements ) {
41 let $e = this.$element;
42 // We need to hook to all of these events to be sure we are
43 // notified of all changes to the value of an <input type=text>
44 // field.
45 const events = 'keyup keydown change mouseup cut paste focus blur';
46
47 if ( $extraElements ) {
48 $e = $e.add( $extraElements );
49 }
50 $e.on( events, mw.util.debounce( this.validate.bind( this ), 1000 ) );
51
52 return this;
53 };
54
55 /**
56 * Validate the form element
57 *
58 * @return {jQuery.Promise|undefined}
59 */
60 HtmlformChecker.prototype.validate = function () {
61 let currentRequestInternal;
62 const that = this;
63 const value = this.$element.val();
64
65 // Abort any pending requests.
66 if ( this.currentRequest && this.currentRequest.abort ) {
67 this.currentRequest.abort();
68 }
69
70 if ( value === '' ) {
71 this.currentValue = value;
72 this.setErrors( true, [] );
73 return;
74 }
75
76 this.currentRequest = currentRequestInternal = this.validator( value )
77 .done( ( info ) => {
78 const forceReplacement = value !== that.currentValue;
79
80 // Another request was fired in the meantime, the result we got here is no longer current.
81 // This shouldn't happen as we abort pending requests, but you never know.
82 if ( that.currentRequest !== currentRequestInternal ) {
83 return;
84 }
85 // If we're here, then the current request has finished, avoid calling .abort() needlessly.
86 that.currentRequest = undefined;
87
88 that.currentValue = value;
89
90 that.setErrors( info.valid, info.messages, forceReplacement );
91 } ).fail( () => {
92 that.currentValue = null;
93 that.setErrors( true, [] );
94 } );
95
96 return currentRequestInternal;
97 };
98
99 /**
100 * Display errors associated with the form element
101 *
102 * @param {boolean} valid Whether the input is still valid regardless of the messages
103 * @param {Array} errors A list of error messages with formatting. Each message may be
104 * a string (which will be interpreted as HTML) or a jQuery collection. They will
105 * be appended to `<div>` or `<li>`, as with jQuery.append().
106 * @param {boolean} [forceReplacement] Set true to force a visual replacement even
107 * if the errors are the same. Ignored if errors are empty.
108 * @return {HtmlformChecker}
109 * @chainable
110 */
111 HtmlformChecker.prototype.setErrors = function ( valid, errors, forceReplacement ) {
112 let replace;
113 let $errorBox = this.$errorBox;
114
115 if ( errors.length === 0 ) {
116 // FIXME: Use CSS transition
117 // eslint-disable-next-line no-jquery/no-slide
118 $errorBox.slideUp( () => {
119 $errorBox
120 .removeAttr( 'class' )
121 .empty();
122 } );
123 } else {
124 let $error;
125 // Match behavior of HTMLFormField::formatErrors()
126 if ( errors.length === 1 ) {
127 $error = $( '<div>' ).append( errors[ 0 ] );
128 } else {
129 $error = $( '<ul>' ).append(
130 errors.map( ( e ) => $( '<li>' ).append( e ) )
131 );
132 }
133
134 // Animate the replacement if told to by the caller (i.e. to make it visually
135 // obvious that the changed field value gives the same errorbox) or if
136 // the errorbox text changes (because it makes more sense than
137 // changing the text with no animation).
138 replace = forceReplacement;
139 if ( !replace && $error.text() !== $errorBox.text() ) {
140 replace = true;
141 }
142
143 const $oldErrorBox = $errorBox;
144 if ( replace ) {
145 this.$errorBox = $errorBox = $( '<div>' );
146 $errorBox.hide();
147 $oldErrorBox.after( this.$errorBox );
148 }
149
150 const oldErrorType = this.oldErrorType || 'notice';
151 const errorType = valid ? 'warning' : 'error';
152 this.oldErrorType = errorType;
153 const showFunc = function () {
154 if ( $oldErrorBox !== $errorBox ) {
155 $oldErrorBox
156 .removeAttr( 'class' )
157 .detach();
158 }
159 $errorBox.empty();
160 $errorBox.append(
161 util.messageBox( $error[ 0 ], errorType )
162 );
163 // FIXME: Use CSS transition
164 // eslint-disable-next-line no-jquery/no-slide
165 $errorBox.slideDown();
166 };
167 if (
168 $oldErrorBox !== $errorBox &&
169 ( oldErrorType === 'error' || oldErrorType === 'warning' )
170 ) {
171 // eslint-disable-next-line no-jquery/no-slide
172 $oldErrorBox.slideUp( showFunc );
173 } else {
174 showFunc();
175 }
176 }
177
178 return this;
179 };
180
181 module.exports = HtmlformChecker;