Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.FilterTagMultiselectWidget.js
1 ( function ( mw ) {
2 /**
3 * List displaying all filter groups
4 *
5 * @class
6 * @extends OO.ui.MenuTagMultiselectWidget
7 * @mixins OO.ui.mixin.PendingElement
8 *
9 * @constructor
10 * @param {mw.rcfilters.Controller} controller Controller
11 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
12 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
13 * @param {Object} config Configuration object
14 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
15 */
16 mw.rcfilters.ui.FilterTagMultiselectWidget = function MwRcfiltersUiFilterTagMultiselectWidget( controller, model, savedQueriesModel, config ) {
17 var rcFiltersRow,
18 title = new OO.ui.LabelWidget( {
19 label: mw.msg( 'rcfilters-activefilters' ),
20 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
21 } ),
22 $contentWrapper = $( '<div>' )
23 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
24
25 config = config || {};
26
27 this.controller = controller;
28 this.model = model;
29 this.queriesModel = savedQueriesModel;
30 this.$overlay = config.$overlay || this.$element;
31 this.matchingQuery = null;
32 this.currentView = this.model.getCurrentView();
33
34 // Parent
35 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.call( this, $.extend( true, {
36 label: mw.msg( 'rcfilters-filterlist-title' ),
37 placeholder: mw.msg( 'rcfilters-empty-filter' ),
38 inputPosition: 'outline',
39 allowArbitrary: false,
40 allowDisplayInvalidTags: false,
41 allowReordering: false,
42 $overlay: this.$overlay,
43 menu: {
44 // Our filtering is done through the model
45 filterFromInput: false,
46 hideWhenOutOfView: false,
47 hideOnChoose: false,
48 width: 650,
49 footers: [
50 {
51 name: 'viewSelect',
52 sticky: false,
53 // View select menu, appears on default view only
54 $element: $( '<div>' )
55 .append( new mw.rcfilters.ui.ViewSwitchWidget( this.controller, this.model ).$element ),
56 views: [ 'default' ]
57 },
58 {
59 name: 'feedback',
60 // Feedback footer, appears on all views
61 $element: $( '<div>' )
62 .append(
63 new OO.ui.ButtonWidget( {
64 framed: false,
65 icon: 'feedback',
66 flags: [ 'progressive' ],
67 label: mw.msg( 'rcfilters-filterlist-feedbacklink' ),
68 href: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
69 } ).$element
70 )
71 }
72 ]
73 },
74 input: {
75 icon: 'menu',
76 placeholder: mw.msg( 'rcfilters-search-placeholder' )
77 }
78 }, config ) );
79
80 this.savedQueryTitle = new OO.ui.LabelWidget( {
81 label: '',
82 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
83 } );
84
85 this.resetButton = new OO.ui.ButtonWidget( {
86 framed: false,
87 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
88 } );
89
90 if ( !mw.user.isAnon() ) {
91 this.saveQueryButton = new mw.rcfilters.ui.SaveFiltersPopupButtonWidget(
92 this.controller,
93 this.queriesModel,
94 {
95 $overlay: this.$overlay
96 }
97 );
98
99 this.saveQueryButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
100
101 this.saveQueryButton.connect( this, {
102 click: 'onSaveQueryButtonClick',
103 saveCurrent: 'setSavedQueryVisibility'
104 } );
105 this.queriesModel.connect( this, {
106 itemUpdate: 'onSavedQueriesItemUpdate',
107 initialize: 'onSavedQueriesInitialize',
108 'default': 'reevaluateResetRestoreState'
109 } );
110 }
111
112 this.emptyFilterMessage = new OO.ui.LabelWidget( {
113 label: mw.msg( 'rcfilters-empty-filter' ),
114 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
115 } );
116 this.$content.append( this.emptyFilterMessage.$element );
117
118 // Events
119 this.resetButton.connect( this, { click: 'onResetButtonClick' } );
120 // Stop propagation for mousedown, so that the widget doesn't
121 // trigger the focus on the input and scrolls up when we click the reset button
122 this.resetButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
123 this.model.connect( this, {
124 initialize: 'onModelInitialize',
125 update: 'onModelUpdate',
126 searchChange: 'onModelSearchChange',
127 itemUpdate: 'onModelItemUpdate',
128 highlightChange: 'onModelHighlightChange'
129 } );
130 this.input.connect( this, { change: 'onInputChange' } );
131
132 // The filter list and button should appear side by side regardless of how
133 // wide the button is; the button also changes its width depending
134 // on language and its state, so the safest way to present both side
135 // by side is with a table layout
136 rcFiltersRow = $( '<div>' )
137 .addClass( 'mw-rcfilters-ui-row' )
138 .append(
139 this.$content
140 .addClass( 'mw-rcfilters-ui-cell' )
141 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
142 );
143
144 if ( !mw.user.isAnon() ) {
145 rcFiltersRow.append(
146 $( '<div>' )
147 .addClass( 'mw-rcfilters-ui-cell' )
148 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
149 .append( this.saveQueryButton.$element )
150 );
151 }
152
153 // Add a selector at the right of the input
154 this.viewsSelectWidget = new OO.ui.ButtonSelectWidget( {
155 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
156 items: [
157 new OO.ui.ButtonOptionWidget( {
158 framed: false,
159 data: 'namespaces',
160 icon: 'article',
161 label: mw.msg( 'namespaces' ),
162 title: mw.msg( 'rcfilters-view-namespaces-tooltip' )
163 } ),
164 new OO.ui.ButtonOptionWidget( {
165 framed: false,
166 data: 'tags',
167 icon: 'tag',
168 label: mw.msg( 'tags-title' ),
169 title: mw.msg( 'rcfilters-view-tags-tooltip' )
170 } )
171 ]
172 } );
173
174 // Rearrange the UI so the select widget is at the right of the input
175 this.$element.append(
176 $( '<div>' )
177 .addClass( 'mw-rcfilters-ui-table' )
178 .append(
179 $( '<div>' )
180 .addClass( 'mw-rcfilters-ui-row' )
181 .append(
182 $( '<div>' )
183 .addClass( 'mw-rcfilters-ui-cell' )
184 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
185 .append( this.input.$element ),
186 $( '<div>' )
187 .addClass( 'mw-rcfilters-ui-cell' )
188 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
189 .append( this.viewsSelectWidget.$element )
190 )
191 )
192 );
193
194 // Event
195 this.viewsSelectWidget.connect( this, { choose: 'onViewsSelectWidgetChoose' } );
196
197 rcFiltersRow.append(
198 $( '<div>' )
199 .addClass( 'mw-rcfilters-ui-cell' )
200 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
201 .append( this.resetButton.$element )
202 );
203
204 // Build the content
205 $contentWrapper.append(
206 title.$element,
207 this.savedQueryTitle.$element,
208 $( '<div>' )
209 .addClass( 'mw-rcfilters-ui-table' )
210 .append(
211 rcFiltersRow
212 )
213 );
214
215 // Initialize
216 this.$handle.append( $contentWrapper );
217 this.emptyFilterMessage.toggle( this.isEmpty() );
218 this.savedQueryTitle.toggle( false );
219
220 this.$element
221 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
222
223 this.reevaluateResetRestoreState();
224 };
225
226 /* Initialization */
227
228 OO.inheritClass( mw.rcfilters.ui.FilterTagMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
229
230 /* Methods */
231
232 /**
233 * Respond to view select widget choose event
234 *
235 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
236 */
237 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget ) {
238 this.controller.switchView( buttonOptionWidget.getData() );
239 this.viewsSelectWidget.selectItem( null );
240 this.focus();
241 };
242
243 /**
244 * Respond to model search change event
245 *
246 * @param {string} value Search value
247 */
248 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelSearchChange = function ( value ) {
249 this.input.setValue( value );
250 };
251
252 /**
253 * Respond to input change event
254 *
255 * @param {string} value Value of the input
256 */
257 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputChange = function ( value ) {
258 this.controller.setSearch( value );
259 };
260
261 /**
262 * Respond to query button click
263 */
264 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSaveQueryButtonClick = function () {
265 this.getMenu().toggle( false );
266 };
267
268 /**
269 * Respond to save query model initialization
270 */
271 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSavedQueriesInitialize = function () {
272 this.setSavedQueryVisibility();
273 };
274
275 /**
276 * Respond to save query item change. Mainly this is done to update the label in case
277 * a query item has been edited
278 *
279 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
280 */
281 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSavedQueriesItemUpdate = function ( item ) {
282 if ( this.matchingQuery === item ) {
283 // This means we just edited the item that is currently matched
284 this.savedQueryTitle.setLabel( item.getLabel() );
285 }
286 };
287
288 /**
289 * Respond to menu toggle
290 *
291 * @param {boolean} isVisible Menu is visible
292 */
293 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
294 // Parent
295 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onMenuToggle.call( this );
296
297 if ( isVisible ) {
298 this.focus();
299
300 mw.hook( 'RcFilters.popup.open' ).fire();
301
302 if ( !this.getMenu().findSelectedItem() ) {
303 // If there are no selected items, scroll menu to top
304 // This has to be in a setTimeout so the menu has time
305 // to be positioned and fixed
306 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
307 }
308 } else {
309 this.blur();
310
311 // Clear selection
312 this.selectTag( null );
313
314 // Clear the search
315 this.controller.setSearch( '' );
316
317 // Log filter grouping
318 this.controller.trackFilterGroupings( 'filtermenu' );
319 }
320
321 this.input.setIcon( isVisible ? 'search' : 'menu' );
322 };
323
324 /**
325 * @inheritdoc
326 */
327 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputFocus = function () {
328 // Parent
329 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
330
331 // Only scroll to top of the viewport if:
332 // - The widget is more than 20px from the top
333 // - The widget is not above the top of the viewport (do not scroll downwards)
334 // (This isn't represented because >20 is, anyways and always, bigger than 0)
335 this.scrollToTop( this.$element, 0, { min: 20, max: Infinity } );
336 };
337
338 /**
339 * @inheritdoc
340 */
341 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.doInputEscape = function () {
342 // Parent
343 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.doInputEscape.call( this );
344
345 // Blur the input
346 this.input.$input.blur();
347 };
348
349 /**
350 * @inheritdoc
351 */
352 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMouseDown = function ( e ) {
353 if ( !this.isDisabled() && e.which === OO.ui.MouseButtons.LEFT ) {
354 this.menu.toggle();
355
356 return false;
357 }
358 };
359
360 /**
361 * @inheritdoc
362 */
363 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onChangeTags = function () {
364 // Parent method
365 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
366
367 this.emptyFilterMessage.toggle( this.isEmpty() );
368 };
369
370 /**
371 * Respond to model initialize event
372 */
373 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
374 this.setSavedQueryVisibility();
375 };
376
377 /**
378 * Respond to model update event
379 */
380 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
381 this.updateElementsForView();
382 };
383
384 /**
385 * Update the elements in the widget to the current view
386 */
387 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
388 var view = this.model.getCurrentView(),
389 inputValue = this.input.getValue().trim(),
390 inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
391
392 if ( inputView !== 'default' ) {
393 // We have a prefix already, remove it
394 inputValue = inputValue.substr( 1 );
395 }
396
397 if ( inputView !== view ) {
398 // Add the correct prefix
399 inputValue = this.model.getViewTrigger( view ) + inputValue;
400 }
401
402 // Update input
403 this.input.setValue( inputValue );
404
405 if ( this.currentView !== view ) {
406 this.scrollToTop( this.$element );
407 this.currentView = view;
408 }
409 };
410
411 /**
412 * Set the visibility of the saved query button
413 */
414 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
415 if ( mw.user.isAnon() ) {
416 return;
417 }
418
419 this.matchingQuery = this.controller.findQueryMatchingCurrentState();
420
421 this.savedQueryTitle.setLabel(
422 this.matchingQuery ? this.matchingQuery.getLabel() : ''
423 );
424 this.savedQueryTitle.toggle( !!this.matchingQuery );
425 this.saveQueryButton.setDisabled( !!this.matchingQuery );
426 this.saveQueryButton.setTitle( !this.matchingQuery ?
427 mw.msg( 'rcfilters-savedqueries-add-new-title' ) :
428 mw.msg( 'rcfilters-savedqueries-already-saved' ) );
429
430 if ( this.matchingQuery ) {
431 this.emphasize();
432 }
433 };
434
435 /**
436 * Respond to model itemUpdate event
437 * fixme: when a new state is applied to the model this function is called 60+ times in a row
438 *
439 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
440 */
441 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
442 if ( !item.getGroupModel().isHidden() ) {
443 if (
444 item.isSelected() ||
445 (
446 this.model.isHighlightEnabled() &&
447 item.getHighlightColor()
448 )
449 ) {
450 this.addTag( item.getName(), item.getLabel() );
451 } else {
452 this.removeTagByData( item.getName() );
453 }
454 }
455
456 this.setSavedQueryVisibility();
457
458 // Re-evaluate reset state
459 this.reevaluateResetRestoreState();
460 };
461
462 /**
463 * @inheritdoc
464 */
465 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
466 return (
467 this.model.getItemByName( data ) &&
468 !this.isDuplicateData( data )
469 );
470 };
471
472 /**
473 * @inheritdoc
474 */
475 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
476 this.controller.toggleFilterSelect( item.model.getName() );
477
478 // Select the tag if it exists, or reset selection otherwise
479 this.selectTag( this.findItemFromData( item.model.getName() ) );
480
481 this.focus();
482 };
483
484 /**
485 * Respond to highlightChange event
486 *
487 * @param {boolean} isHighlightEnabled Highlight is enabled
488 */
489 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
490 var highlightedItems = this.model.getHighlightedItems();
491
492 if ( isHighlightEnabled ) {
493 // Add capsule widgets
494 highlightedItems.forEach( function ( filterItem ) {
495 this.addTag( filterItem.getName(), filterItem.getLabel() );
496 }.bind( this ) );
497 } else {
498 // Remove capsule widgets if they're not selected
499 highlightedItems.forEach( function ( filterItem ) {
500 if ( !filterItem.isSelected() ) {
501 this.removeTagByData( filterItem.getName() );
502 }
503 }.bind( this ) );
504 }
505
506 this.setSavedQueryVisibility();
507 };
508
509 /**
510 * @inheritdoc
511 */
512 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
513 var menuOption = this.menu.getItemFromModel( tagItem.getModel() );
514
515 this.menu.setUserSelecting( true );
516 // Parent method
517 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
518
519 // Switch view
520 this.controller.resetSearchForView( tagItem.getView() );
521
522 this.selectTag( tagItem );
523 this.scrollToTop( menuOption.$element );
524
525 this.menu.setUserSelecting( false );
526 };
527
528 /**
529 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
530 * If no items are given, reset selection from all.
531 *
532 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
533 * omit to deselect all
534 */
535 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
536 var i, len, selected;
537
538 for ( i = 0, len = this.items.length; i < len; i++ ) {
539 selected = this.items[ i ] === item;
540 if ( this.items[ i ].isSelected() !== selected ) {
541 this.items[ i ].toggleSelected( selected );
542 }
543 }
544 };
545 /**
546 * @inheritdoc
547 */
548 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
549 // Parent method
550 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
551
552 this.controller.clearFilter( tagItem.getName() );
553
554 tagItem.destroy();
555 };
556
557 /**
558 * Respond to click event on the reset button
559 */
560 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
561 if ( this.model.areVisibleFiltersEmpty() ) {
562 // Reset to default filters
563 this.controller.resetToDefaults();
564 } else {
565 // Reset to have no filters
566 this.controller.emptyFilters();
567 }
568 };
569
570 /**
571 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
572 */
573 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
574 var defaultsAreEmpty = this.controller.areDefaultsEmpty(),
575 currFiltersAreEmpty = this.model.areVisibleFiltersEmpty(),
576 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
577
578 this.resetButton.setIcon(
579 currFiltersAreEmpty ? 'history' : 'trash'
580 );
581
582 this.resetButton.setLabel(
583 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
584 );
585 this.resetButton.setTitle(
586 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
587 );
588
589 this.resetButton.toggle( !hideResetButton );
590 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
591 };
592
593 /**
594 * @inheritdoc
595 */
596 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
597 return new mw.rcfilters.ui.MenuSelectWidget(
598 this.controller,
599 this.model,
600 menuConfig
601 );
602 };
603
604 /**
605 * @inheritdoc
606 */
607 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
608 var filterItem = this.model.getItemByName( data );
609
610 if ( filterItem ) {
611 return new mw.rcfilters.ui.FilterTagItemWidget(
612 this.controller,
613 this.model,
614 this.model.getInvertModel(),
615 filterItem,
616 {
617 $overlay: this.$overlay
618 }
619 );
620 }
621 };
622
623 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.emphasize = function () {
624 if (
625 !this.$handle.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
626 ) {
627 this.$handle
628 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
629 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
630
631 setTimeout( function () {
632 this.$handle
633 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
634
635 setTimeout( function () {
636 this.$handle
637 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
638 }.bind( this ), 1000 );
639 }.bind( this ), 500 );
640
641 }
642 };
643 /**
644 * Scroll the element to top within its container
645 *
646 * @private
647 * @param {jQuery} $element Element to position
648 * @param {number} [marginFromTop=0] When scrolling the entire widget to the top, leave this
649 * much space (in pixels) above the widget.
650 * @param {Object} [threshold] Minimum distance from the top of the element to scroll at all
651 * @param {number} [threshold.min] Minimum distance above the element
652 * @param {number} [threshold.max] Minimum distance below the element
653 */
654 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop, threshold ) {
655 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
656 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
657 containerScrollTop = $( container ).scrollTop(),
658 effectiveScrollTop = $( container ).is( 'body, html' ) ? 0 : containerScrollTop,
659 newScrollTop = effectiveScrollTop + pos.top - ( marginFromTop || 0 );
660
661 // Scroll to item
662 if (
663 threshold === undefined ||
664 (
665 (
666 threshold.min === undefined ||
667 newScrollTop - containerScrollTop >= threshold.min
668 ) &&
669 (
670 threshold.max === undefined ||
671 newScrollTop - containerScrollTop <= threshold.max
672 )
673 )
674 ) {
675 $( container ).animate( {
676 scrollTop: newScrollTop
677 } );
678 }
679 };
680 }( mediaWiki ) );