638f461ba783a500ee46442b8b22590c5112b21a
[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 {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 */
27 mw.widgets.TitleOptionWidget = function MwWidgetsTitleOptionWidget( config ) {
28 var icon;
29
30 if ( !config.showImages ) {
31 icon = null;
32 } else if ( config.missing ) {
33 icon = 'page-not-found';
34 } else if ( config.redirect ) {
35 icon = 'page-redirect';
36 } else if ( config.disambiguation ) {
37 icon = 'page-disambiguation';
38 } else {
39 icon = 'page-existing';
40 }
41
42 // Config initialization
43 config = $.extend( {
44 icon: icon,
45 label: config.data,
46 autoFitLabel: false,
47 $label: $( '<a>' )
48 }, config );
49
50 // Parent constructor
51 mw.widgets.TitleOptionWidget.parent.call( this, config );
52
53 // Initialization
54 this.$label.attr( 'href', config.url );
55 this.$element.addClass( 'mw-widget-titleOptionWidget' );
56
57 // OOUI OptionWidgets make an effort to not be tab accessible, but
58 // adding a link inside them would undo that. So, explicitly make it
59 // not tabbable.
60 this.$label.attr( 'tabindex', '-1' );
61
62 // Allow opening the link in new tab, but not regular navigation.
63 this.$label.on( 'click', function ( e ) {
64 // Don't interfere with special clicks (e.g. to open in new tab)
65 if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
66 e.preventDefault();
67 }
68 } );
69
70 // Highlight matching parts of link suggestion
71 if ( config.query ) {
72 this.setHighlightedQuery( config.data, config.query );
73 }
74 this.$label.attr( 'title', config.data );
75
76 if ( config.missing ) {
77 this.$label.addClass( 'new' );
78 } else if ( config.redirect ) {
79 this.$label.addClass( 'mw-redirect' );
80 } else if ( config.disambiguation ) {
81 this.$label.addClass( 'mw-disambig' );
82 }
83
84 if ( config.showImages && config.imageUrl ) {
85 this.$icon
86 .addClass( 'mw-widget-titleOptionWidget-hasImage' )
87 .css( 'background-image', 'url(' + config.imageUrl + ')' );
88 }
89
90 if ( config.description ) {
91 this.$element.append(
92 $( '<span>' )
93 .addClass( 'mw-widget-titleOptionWidget-description' )
94 .text( config.description )
95 .attr( 'title', config.description )
96 );
97 }
98 };
99
100 /* Setup */
101
102 OO.inheritClass( mw.widgets.TitleOptionWidget, OO.ui.MenuOptionWidget );
103
104 }( jQuery, mediaWiki ) );