| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
( function () { |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
mw.widgets.ExpiryWidget = function ( config ) { |
| 21 |
const RFC2822 = 'ddd, DD MMM YYYY HH:mm:ss [GMT]'; |
| 22 |
|
| 23 |
|
| 24 |
config = Object.assign( {}, config ); |
| 25 |
|
| 26 |
this.relativeField = new config.RelativeInputClass( config.relativeInput ); |
| 27 |
this.relativeField.$element.addClass( 'mw-widget-ExpiryWidget-relative' ); |
| 28 |
|
| 29 |
|
| 30 |
mw.widgets.ExpiryWidget.super.call( this, config ); |
| 31 |
|
| 32 |
|
| 33 |
this.inputSwitch = new OO.ui.ButtonSelectWidget( { |
| 34 |
tabIndex: -1, |
| 35 |
items: [ |
| 36 |
new OO.ui.ButtonOptionWidget( { |
| 37 |
data: 'relative', |
| 38 |
icon: 'edit' |
| 39 |
} ), |
| 40 |
new OO.ui.ButtonOptionWidget( { |
| 41 |
data: 'date', |
| 42 |
icon: 'calendar' |
| 43 |
} ) |
| 44 |
] |
| 45 |
} ); |
| 46 |
this.dateTimeField = new mw.widgets.datetime.DateTimeInputWidget( { |
| 47 |
min: new Date(), |
| 48 |
required: config.required |
| 49 |
} ); |
| 50 |
|
| 51 |
|
| 52 |
this.dateTimeField.toggle( false ); |
| 53 |
|
| 54 |
this.inputSwitch.selectItemByData( 'relative' ); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
this.inputSwitch.on( 'choose', ( event ) => { |
| 60 |
switch ( event.getData() ) { |
| 61 |
case 'date': |
| 62 |
this.dateTimeField.toggle( true ); |
| 63 |
this.relativeField.toggle( false ); |
| 64 |
break; |
| 65 |
case 'relative': |
| 66 |
this.dateTimeField.toggle( false ); |
| 67 |
this.relativeField.toggle( true ); |
| 68 |
break; |
| 69 |
} |
| 70 |
} ); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
this.dateTimeField.on( 'change', ( value ) => { |
| 75 |
|
| 76 |
if ( this.relativeField.isVisible() ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
if ( !value ) { |
| 82 |
this.relativeField.setValue( value ); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
const datetime = moment( value ); |
| 87 |
|
| 88 |
|
| 89 |
if ( !datetime.isValid() ) { |
| 90 |
this.relativeField.setValue( undefined ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
this.relativeField.setValue( datetime.utc().locale( 'en' ).format( RFC2822 ) ); |
| 95 |
} ); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
this.relativeField.on( 'change', ( event ) => { |
| 100 |
|
| 101 |
this.emit( 'change', event ); |
| 102 |
|
| 103 |
|
| 104 |
if ( this.dateTimeField.isVisible() ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
const datetime = moment( event ); |
| 111 |
|
| 112 |
if ( datetime.isValid() ) { |
| 113 |
this.dateTimeField.setValue( datetime.utc().toISOString() ); |
| 114 |
} else { |
| 115 |
this.dateTimeField.setValue( undefined ); |
| 116 |
} |
| 117 |
} ); |
| 118 |
|
| 119 |
|
| 120 |
this.$element |
| 121 |
.addClass( 'mw-widget-ExpiryWidget' ) |
| 122 |
.addClass( 'mw-widget-ExpiryWidget-hasDatePicker' ) |
| 123 |
.append( |
| 124 |
this.inputSwitch.$element, |
| 125 |
this.dateTimeField.$element, |
| 126 |
this.relativeField.$element |
| 127 |
); |
| 128 |
|
| 129 |
|
| 130 |
this.relativeField.emit( 'change', this.relativeField.getValue() ); |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
OO.inheritClass( mw.widgets.ExpiryWidget, OO.ui.Widget ); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
mw.widgets.ExpiryWidget.static.reusePreInfuseDOM = function ( node, config ) { |
| 141 |
const $relativeElement = $( node ).find( '.mw-widget-ExpiryWidget-relative' ); |
| 142 |
|
| 143 |
config = mw.widgets.ExpiryWidget.super.static.reusePreInfuseDOM( node, config ); |
| 144 |
|
| 145 |
|
| 146 |
if ( $relativeElement.hasClass( 'oo-ui-textInputWidget' ) ) { |
| 147 |
config.RelativeInputClass = OO.ui.TextInputWidget; |
| 148 |
|
| 149 |
} else if ( $relativeElement.hasClass( 'mw-widget-selectWithInputWidget' ) ) { |
| 150 |
config.RelativeInputClass = mw.widgets.SelectWithInputWidget; |
| 151 |
} |
| 152 |
|
| 153 |
config.relativeInput = config.RelativeInputClass.static.reusePreInfuseDOM( |
| 154 |
$relativeElement, |
| 155 |
config.relativeInput |
| 156 |
); |
| 157 |
|
| 158 |
return config; |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
mw.widgets.ExpiryWidget.static.gatherPreInfuseState = function ( node, config ) { |
| 165 |
const state = mw.widgets.ExpiryWidget.super.static.gatherPreInfuseState( node, config ); |
| 166 |
|
| 167 |
state.relativeInput = config.RelativeInputClass.static.gatherPreInfuseState( |
| 168 |
$( node ).find( '.mw-widget-ExpiryWidget-relative' ), |
| 169 |
config.relativeInput |
| 170 |
); |
| 171 |
|
| 172 |
return state; |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
mw.widgets.ExpiryWidget.prototype.restorePreInfuseState = function ( state ) { |
| 179 |
mw.widgets.ExpiryWidget.super.prototype.restorePreInfuseState.call( this, state ); |
| 180 |
this.relativeField.restorePreInfuseState( state.relativeInput ); |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
mw.widgets.ExpiryWidget.prototype.setDisabled = function ( disabled ) { |
| 187 |
mw.widgets.ExpiryWidget.super.prototype.setDisabled.call( this, disabled ); |
| 188 |
this.relativeField.setDisabled( disabled ); |
| 189 |
|
| 190 |
if ( this.inputSwitch ) { |
| 191 |
this.inputSwitch.setDisabled( disabled ); |
| 192 |
} |
| 193 |
|
| 194 |
if ( this.dateTimeField ) { |
| 195 |
this.dateTimeField.setDisabled( disabled ); |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
mw.widgets.ExpiryWidget.prototype.getValue = function () { |
| 205 |
return this.relativeField.getValue(); |
| 206 |
}; |
| 207 |
|
| 208 |
}() ); |