Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * A wrapper for a widget that provides an enable/disable button
3 *
4 * @class
5 * @private
6 * @constructor
7 * @param {OO.ui.Widget} widget
8 * @param {Object} [config] Configuration options
9 */
10 function OptionalParamWidget( widget, config ) {
11 let k;
12
13 config = config || {};
14
15 this.widget = widget;
16 this.$cover = config.$cover ||
17 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-cover' );
18 this.checkbox = new OO.ui.CheckboxInputWidget( config.checkbox )
19 .on( 'change', this.onCheckboxChange, [], this );
20
21 OptionalParamWidget.super.call( this, config );
22
23 // Forward most methods for convenience
24 for ( k in this.widget ) {
25 if ( typeof this.widget[ k ] === 'function' && !this[ k ] ) {
26 this[ k ] = this.widget[ k ].bind( this.widget );
27 }
28 }
29
30 widget.connect( this, {
31 change: [ this.emit, 'change' ]
32 } );
33
34 this.$cover.on( 'click', this.onOverlayClick.bind( this ) );
35
36 this.$element
37 .addClass( 'mw-apisandbox-optionalWidget' )
38 .append(
39 this.$cover,
40 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
41 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
42 widget.$element
43 ),
44 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
45 this.checkbox.$element
46 )
47 )
48 );
49
50 this.setDisabled( widget.isDisabled() );
51 }
52
53 OO.inheritClass( OptionalParamWidget, OO.ui.Widget );
54
55 /**
56 * @param {boolean} checked
57 */
58 OptionalParamWidget.prototype.onCheckboxChange = function ( checked ) {
59 this.setDisabled( !checked );
60 };
61
62 OptionalParamWidget.prototype.onOverlayClick = function () {
63 this.setDisabled( false );
64 if ( typeof this.widget.focus === 'function' ) {
65 this.widget.focus();
66 }
67 };
68
69 /**
70 * @param {boolean} disabled
71 * @return {OptionalParamWidget}
72 * @chainable
73 */
74 OptionalParamWidget.prototype.setDisabled = function ( disabled ) {
75 OptionalParamWidget.super.prototype.setDisabled.call( this, disabled );
76 this.widget.setDisabled( this.isDisabled() );
77 this.checkbox.setSelected( !this.isDisabled() );
78 this.$cover.toggle( this.isDisabled() );
79 this.emit( 'change' );
80 return this;
81 };
82
83 /**
84 * @return {any|undefined}
85 */
86 OptionalParamWidget.prototype.getApiValue = function () {
87 return this.isDisabled() ? undefined : this.widget.getApiValue();
88 };
89
90 /**
91 * @param {any|undefined} newValue
92 */
93 OptionalParamWidget.prototype.setApiValue = function ( newValue ) {
94 this.setDisabled( newValue === undefined );
95 this.widget.setApiValue( newValue );
96 };
97
98 /**
99 * @return {jQuery.Promise}
100 */
101 OptionalParamWidget.prototype.apiCheckValid = function () {
102 if ( this.isDisabled() ) {
103 return $.Deferred().resolve( true ).promise();
104 } else {
105 return this.widget.apiCheckValid();
106 }
107 };
108
109 module.exports = OptionalParamWidget;