| 1 |
const util = require( 'mediawiki.util' ); |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
HtmlformChecker.prototype.attach = function ( $extraElements ) { |
| 41 |
let $e = this.$element; |
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
HtmlformChecker.prototype.validate = function () { |
| 61 |
let currentRequestInternal; |
| 62 |
const that = this; |
| 63 |
const value = this.$element.val(); |
| 64 |
|
| 65 |
|
| 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 |
|
| 81 |
|
| 82 |
if ( that.currentRequest !== currentRequestInternal ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
HtmlformChecker.prototype.setErrors = function ( valid, errors, forceReplacement ) { |
| 112 |
let replace; |
| 113 |
let $errorBox = this.$errorBox; |
| 114 |
|
| 115 |
if ( errors.length === 0 ) { |
| 116 |
|
| 117 |
|
| 118 |
$errorBox.slideUp( () => { |
| 119 |
$errorBox |
| 120 |
.removeAttr( 'class' ) |
| 121 |
.empty(); |
| 122 |
} ); |
| 123 |
} else { |
| 124 |
let $error; |
| 125 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 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 |
|
| 164 |
|
| 165 |
$errorBox.slideDown(); |
| 166 |
}; |
| 167 |
if ( |
| 168 |
$oldErrorBox !== $errorBox && |
| 169 |
( oldErrorType === 'error' || oldErrorType === 'warning' ) |
| 170 |
) { |
| 171 |
|
| 172 |
$oldErrorBox.slideUp( showFunc ); |
| 173 |
} else { |
| 174 |
showFunc(); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return this; |
| 179 |
}; |
| 180 |
|
| 181 |
module.exports = HtmlformChecker; |