Debian Codegraph @

resources/src/mediawiki.special.apisandbox/TextParamMixin.js

raw symbols SCIP
1 /**
2 * Simple mixin for text parameters. Should only be used within
3 * the apisandbox code, and for objects that have
4 * - a getValue function
5 * - a setValue function
6 * - a getValidity function
7 * - a setIcon function
8 * - a setTitle function
9 * - this.paramInfo object set
10 *
11 * @class
12 * @private
13 * @constructor
14 */
15 function TextParamMixin() {
16 // This mixin does not manage state, nothing to do here
17 }
18
19 /**
20 * @return {any}
21 */
22 TextParamMixin.prototype.getApiValue = function () {
23 return this.getValue();
24 };
25
26 /**
27 * @param {any|undefined} newValue
28 */
29 TextParamMixin.prototype.setApiValue = function ( newValue ) {
30 if ( newValue === undefined ) {
31 newValue = this.paramInfo.default;
32 }
33 this.setValue( newValue );
34 };
35
36 /**
37 * Check if a text parameter is valid for the api, and if the result is not valid, set the icon
38 * to 'alert' and the title to a message explaining that the value is invalid. If shouldSuppressErrors
39 * is true, then the result of the validity check is always treated as valid.
40 *
41 * @param {boolean} shouldSuppressErrors
42 * @return {jQuery.Promise}
43 */
44 TextParamMixin.prototype.apiCheckValid = function ( shouldSuppressErrors ) {
45 const that = this;
46 return this.getValidity().then( () => $.Deferred().resolve( true ).promise(), () => $.Deferred().resolve( false ).promise() ).done( ( ok ) => {
47 ok = ok || shouldSuppressErrors;
48 that.setIcon( ok ? null : 'alert' );
49 that.setTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() );
50 } );
51 };
52
53 module.exports = TextParamMixin;