Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.TitleOptionWidget.js
blob84533aa957dc43b7daf9e8a06afc9bb7a34e3779
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 ) {
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
25          */
26         mw.widgets.TitleOptionWidget = function MwWidgetsTitleOptionWidget( config ) {
27                 var icon;
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                 }
39                 // Config initialization
40                 config = $.extend( {
41                         icon: icon,
42                         label: config.data,
43                         autoFitLabel: false,
44                         $label: $( '<a>' )
45                 }, config );
47                 // Parent constructor
48                 mw.widgets.TitleOptionWidget.parent.call( this, config );
50                 // Initialization
51                 this.$label.attr( 'href', config.url );
52                 this.$element.addClass( 'mw-widget-titleOptionWidget' );
54                 // Allow opening the link in new tab, but not regular navigation.
55                 this.$label.on( 'click', function ( e ) {
56                         // Don't interfere with special clicks (e.g. to open in new tab)
57                         if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
58                                 e.preventDefault();
59                         }
60                 } );
62                 // Highlight matching parts of link suggestion
63                 this.$label.autoEllipsis( { hasSpan: false, tooltip: true, matchText: config.query } );
65                 if ( config.missing ) {
66                         this.$label.addClass( 'new' );
67                 }
69                 if ( config.imageUrl ) {
70                         this.$icon
71                                 .addClass( 'mw-widget-titleOptionWidget-hasImage' )
72                                 .css( 'background-image', 'url(' + config.imageUrl + ')' );
73                 }
75                 if ( config.description ) {
76                         this.$element.append(
77                                 $( '<span>' )
78                                         .addClass( 'mw-widget-titleOptionWidget-description' )
79                                         .text( config.description )
80                                         .attr( 'title', config.description )
81                         );
82                 }
83         };
85         /* Setup */
87         OO.inheritClass( mw.widgets.TitleOptionWidget, OO.ui.MenuOptionWidget );
89 }( jQuery, mediaWiki ) );