| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
( function () { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
mw.widgets.SearchInputWidget = function MwWidgetsSearchInputWidget( config ) { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
const $form = config.$input ? config.$input.closest( 'form' ) : $(); |
| 30 |
|
| 31 |
config = Object.assign( { |
| 32 |
icon: 'search', |
| 33 |
maxLength: undefined, |
| 34 |
showPendingRequest: false, |
| 35 |
performSearchOnClick: true, |
| 36 |
dataLocation: 'header' |
| 37 |
}, config ); |
| 38 |
|
| 39 |
|
| 40 |
mw.widgets.SearchInputWidget.super.call( this, config ); |
| 41 |
|
| 42 |
|
| 43 |
this.$element.addClass( 'mw-widget-searchInputWidget' ); |
| 44 |
this.lookupMenu.$element.addClass( 'mw-widget-searchWidget-menu' ); |
| 45 |
this.lastLookupItems = []; |
| 46 |
if ( config.dataLocation ) { |
| 47 |
this.dataLocation = config.dataLocation; |
| 48 |
} |
| 49 |
if ( config.performSearchOnClick ) { |
| 50 |
this.performSearchOnClick = config.performSearchOnClick; |
| 51 |
} |
| 52 |
this.setLookupsDisabled( !this.suggestions ); |
| 53 |
|
| 54 |
$form.on( 'submit', () => { |
| 55 |
mw.track( 'mw.widgets.SearchInputWidget', { |
| 56 |
action: 'submit-form', |
| 57 |
numberOfResults: this.lastLookupItems.length, |
| 58 |
$form: $form, |
| 59 |
inputLocation: this.dataLocation || 'header', |
| 60 |
index: this.lastLookupItems.indexOf( |
| 61 |
this.$input.val() |
| 62 |
) |
| 63 |
} ); |
| 64 |
} ); |
| 65 |
|
| 66 |
this.connect( this, { |
| 67 |
change: 'onChange' |
| 68 |
} ); |
| 69 |
|
| 70 |
this.$element.addClass( 'oo-ui-textInputWidget-type-search' ); |
| 71 |
this.updateSearchIndicator(); |
| 72 |
this.connect( this, { |
| 73 |
disable: 'onDisable' |
| 74 |
} ); |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
OO.inheritClass( mw.widgets.SearchInputWidget, mw.widgets.TitleInputWidget ); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
mw.widgets.SearchInputWidget.prototype.getInputElement = function () { |
| 88 |
return $( '<input>' ).attr( 'type', 'search' ); |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
mw.widgets.SearchInputWidget.prototype.onIndicatorMouseDown = function ( e ) { |
| 95 |
if ( e.which === OO.ui.MouseButtons.LEFT ) { |
| 96 |
|
| 97 |
this.setValue( '' ); |
| 98 |
this.$input[ 0 ].focus(); |
| 99 |
return false; |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
mw.widgets.SearchInputWidget.prototype.updateSearchIndicator = function () { |
| 109 |
if ( this.getValue() === '' || this.isDisabled() || this.isReadOnly() ) { |
| 110 |
this.setIndicator( null ); |
| 111 |
} else { |
| 112 |
this.setIndicator( 'clear' ); |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
mw.widgets.SearchInputWidget.prototype.onChange = function () { |
| 120 |
this.updateSearchIndicator(); |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
mw.widgets.SearchInputWidget.prototype.onDisable = function () { |
| 130 |
this.updateSearchIndicator(); |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
mw.widgets.SearchInputWidget.prototype.setReadOnly = function ( state ) { |
| 137 |
mw.widgets.SearchInputWidget.super.prototype.setReadOnly.call( this, state ); |
| 138 |
this.updateSearchIndicator(); |
| 139 |
return this; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
mw.widgets.SearchInputWidget.prototype.getSuggestionsPromise = function () { |
| 146 |
const api = this.getApi(); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
mw.track( 'mw.widgets.SearchInputWidget', { |
| 151 |
action: 'session-start' |
| 152 |
} ); |
| 153 |
|
| 154 |
|
| 155 |
const promise = mw.searchSuggest.request( api, this.getQueryValue(), () => {}, this.limit, this.getNamespace() ); |
| 156 |
|
| 157 |
|
| 158 |
promise.done( ( data, jqXHR ) => { |
| 159 |
this.requestType = jqXHR.getResponseHeader( 'X-OpenSearch-Type' ); |
| 160 |
this.searchId = jqXHR.getResponseHeader( 'X-Search-ID' ); |
| 161 |
} ); |
| 162 |
|
| 163 |
return promise; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
mw.widgets.SearchInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) { |
| 170 |
|
| 171 |
|
| 172 |
const resp = { |
| 173 |
data: response || {}, |
| 174 |
metadata: { |
| 175 |
type: this.requestType || 'unknown', |
| 176 |
searchId: this.searchId || null, |
| 177 |
query: this.getQueryValue() |
| 178 |
} |
| 179 |
}; |
| 180 |
this.requestType = undefined; |
| 181 |
this.searchId = undefined; |
| 182 |
|
| 183 |
return resp; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
mw.widgets.SearchInputWidget.prototype.getOptionsFromData = function ( data ) { |
| 190 |
const items = [], |
| 191 |
titles = data.data[ 1 ], |
| 192 |
descriptions = data.data[ 2 ], |
| 193 |
urls = data.data[ 3 ]; |
| 194 |
|
| 195 |
|
| 196 |
$.each( titles, ( i, result ) => { |
| 197 |
items.push( new mw.widgets.TitleOptionWidget( |
| 198 |
this.getOptionWidgetData( |
| 199 |
result, |
| 200 |
|
| 201 |
|
| 202 |
{ |
| 203 |
data: result, |
| 204 |
url: urls[ i ], |
| 205 |
imageUrl: null, |
| 206 |
description: descriptions[ i ], |
| 207 |
missing: false, |
| 208 |
redirect: false, |
| 209 |
disambiguation: false |
| 210 |
} |
| 211 |
) |
| 212 |
) ); |
| 213 |
} ); |
| 214 |
|
| 215 |
mw.track( 'mw.widgets.SearchInputWidget', { |
| 216 |
action: 'impression-results', |
| 217 |
numberOfResults: items.length, |
| 218 |
resultSetType: data.metadata.type, |
| 219 |
searchId: data.metadata.searchId, |
| 220 |
query: data.metadata.query, |
| 221 |
inputLocation: this.dataLocation || 'header' |
| 222 |
} ); |
| 223 |
|
| 224 |
return items; |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
mw.widgets.SearchInputWidget.prototype.onLookupMenuChoose = function () { |
| 231 |
mw.widgets.SearchInputWidget.super.prototype.onLookupMenuChoose.apply( this, arguments ); |
| 232 |
|
| 233 |
if ( this.performSearchOnClick ) { |
| 234 |
this.$element.closest( 'form' ).trigger( 'submit' ); |
| 235 |
} |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
mw.widgets.SearchInputWidget.prototype.getLookupMenuOptionsFromData = function () { |
| 242 |
const items = mw.widgets.SearchInputWidget.super.prototype.getLookupMenuOptionsFromData.apply( |
| 243 |
this, arguments |
| 244 |
); |
| 245 |
|
| 246 |
this.lastLookupItems = items.map( ( item ) => item.data ); |
| 247 |
|
| 248 |
return items; |
| 249 |
}; |
| 250 |
|
| 251 |
}() ); |