Merge "Revert "RCFilters: Don't apply/clear highlights 66 times""
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.TitleOptionWidget.js
1 /*!
2 * MediaWiki Widgets - TitleOptionWidget class.
3 *
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7 ( function ( $, mw ) {
8
9 /**
10 * Creates a mw.widgets.TitleOptionWidget object.
11 *
12 * @class
13 * @extends OO.ui.MenuOptionWidget
14 *
15 * @constructor
16 * @param {Object} config Configuration options
17 * @cfg {string} data Label to display
18 * @cfg {string} url URL of page
19 * @cfg {string} [imageUrl] Thumbnail image URL with URL encoding
20 * @cfg {string} [description] Page description
21 * @cfg {boolean} [missing] Page doesn't exist
22 * @cfg {boolean} [redirect] Page is a redirect
23 * @cfg {boolean} [disambiguation] Page is a disambiguation page
24 * @cfg {string} [query] Matching query string to highlight
25 */
26 mw.widgets.TitleOptionWidget = function MwWidgetsTitleOptionWidget( config ) {
27 var icon;
28
29 if ( config.missing ) {
30 icon = 'page-not-found';
31 } else if ( config.redirect ) {
32 icon = 'page-redirect';
33 } else if ( config.disambiguation ) {
34 icon = 'page-disambiguation';
35 } else {
36 icon = 'page-existing';
37 }
38
39 // Config initialization
40 config = $.extend( {
41 icon: icon,
42 label: config.data,
43 autoFitLabel: false,
44 $label: $( '<a>' )
45 }, config );
46
47 // Parent constructor
48 mw.widgets.TitleOptionWidget.parent.call( this, config );
49
50 // Initialization
51 this.$label.attr( 'href', config.url );
52 this.$element.addClass( 'mw-widget-titleOptionWidget' );
53
54 // OOUI OptionWidgets make an effort to not be tab accessible, but
55 // adding a link inside them would undo that. So, explicitly make it
56 // not tabbable.
57 this.$label.attr( 'tabindex', '-1' );
58
59 // Allow opening the link in new tab, but not regular navigation.
60 this.$label.on( 'click', function ( e ) {
61 // Don't interfere with special clicks (e.g. to open in new tab)
62 if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
63 e.preventDefault();
64 }
65 } );
66
67 // Highlight matching parts of link suggestion
68 if ( config.query ) {
69 this.setHighlightedQuery( config.data, config.query );
70 }
71 this.$label.attr( 'title', config.data );
72
73 if ( config.missing ) {
74 this.$label.addClass( 'new' );
75 } else if ( config.redirect ) {
76 this.$label.addClass( 'mw-redirect' );
77 } else if ( config.disambiguation ) {
78 this.$label.addClass( 'mw-disambig' );
79 }
80
81 if ( config.imageUrl ) {
82 this.$icon
83 .addClass( 'mw-widget-titleOptionWidget-hasImage' )
84 .css( 'background-image', 'url(' + config.imageUrl + ')' );
85 }
86
87 if ( config.description ) {
88 this.$element.append(
89 $( '<span>' )
90 .addClass( 'mw-widget-titleOptionWidget-description' )
91 .text( config.description )
92 .attr( 'title', config.description )
93 );
94 }
95 };
96
97 /* Setup */
98
99 OO.inheritClass( mw.widgets.TitleOptionWidget, OO.ui.MenuOptionWidget );
100
101 }( jQuery, mediaWiki ) );