Merge "Corrected grammatical error."
[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 () {
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 {boolean} [showImages] Whether to attempt to show images
20 * @cfg {string} [imageUrl] Thumbnail image URL with URL encoding
21 * @cfg {string} [description] Page description
22 * @cfg {boolean} [missing] Page doesn't exist
23 * @cfg {boolean} [redirect] Page is a redirect
24 * @cfg {boolean} [disambiguation] Page is a disambiguation page
25 * @cfg {string} [query] Matching query string to highlight
26 * @cfg {string} [compare] String comparison function for query highlighting
27 */
28 mw.widgets.TitleOptionWidget = function MwWidgetsTitleOptionWidget( config ) {
29 var icon;
30
31 if ( !config.showImages ) {
32 icon = null;
33 } else if ( config.missing ) {
34 icon = 'articleNotFound';
35 } else if ( config.redirect ) {
36 icon = 'articleRedirect';
37 } else if ( config.disambiguation ) {
38 icon = 'articleDisambiguation';
39 } else {
40 icon = 'article';
41 }
42
43 // Config initialization
44 config = $.extend( {
45 icon: icon,
46 label: config.data,
47 autoFitLabel: false,
48 $label: $( '<a>' )
49 }, config );
50
51 // Parent constructor
52 mw.widgets.TitleOptionWidget.parent.call( this, config );
53
54 // Remove check icon
55 this.checkIcon.$element.remove();
56
57 // Initialization
58 this.$label.attr( 'href', config.url );
59 this.$element.addClass( 'mw-widget-titleOptionWidget' );
60
61 // OOUI OptionWidgets make an effort to not be tab accessible, but
62 // adding a link inside them would undo that. So, explicitly make it
63 // not tabbable.
64 this.$label.attr( 'tabindex', '-1' );
65
66 // Allow opening the link in new tab, but not regular navigation.
67 this.$label.on( 'click', function ( e ) {
68 // Don't interfere with special clicks (e.g. to open in new tab)
69 if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
70 e.preventDefault();
71 }
72 } );
73
74 // Highlight matching parts of link suggestion
75 if ( config.query ) {
76 this.setHighlightedQuery( config.data, config.query, config.compare );
77 }
78 this.$label.attr( 'title', config.data );
79
80 if ( config.missing ) {
81 this.$label.addClass( 'new' );
82 } else if ( config.redirect ) {
83 this.$label.addClass( 'mw-redirect' );
84 } else if ( config.disambiguation ) {
85 this.$label.addClass( 'mw-disambig' );
86 }
87
88 if ( config.showImages && config.imageUrl ) {
89 this.$icon
90 .addClass( 'mw-widget-titleOptionWidget-hasImage' )
91 .css( 'background-image', 'url(' + config.imageUrl + ')' );
92 }
93
94 if ( config.description ) {
95 this.$element.append(
96 $( '<span>' )
97 .addClass( 'mw-widget-titleOptionWidget-description' )
98 .text( config.description )
99 .attr( 'title', config.description )
100 );
101 }
102 };
103
104 /* Setup */
105
106 OO.inheritClass( mw.widgets.TitleOptionWidget, OO.ui.MenuOptionWidget );
107
108 }() );