Merge "User: Avoid deprecated Linker::link()"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.SaveFiltersPopupButtonWidget.js
1 ( function ( mw ) {
2 /**
3 * Save filters widget. This widget is displayed in the tag area
4 * and allows the user to save the current state of the system
5 * as a new saved filter query they can later load or set as
6 * default.
7 *
8 * @extends OO.ui.PopupButtonWidget
9 *
10 * @constructor
11 * @param {mw.rcfilters.Controller} controller Controller
12 * @param {mw.rcfilters.dm.SavedQueriesModel} model View model
13 * @param {Object} [config] Configuration object
14 */
15 mw.rcfilters.ui.SaveFiltersPopupButtonWidget = function MwRcfiltersUiSaveFiltersPopupButtonWidget( controller, model, config ) {
16 var layout,
17 $popupContent = $( '<div>' );
18
19 config = config || {};
20
21 this.controller = controller;
22 this.model = model;
23
24 // Parent
25 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.parent.call( this, $.extend( {
26 framed: false,
27 icon: 'unClip',
28 $overlay: this.$overlay,
29 title: mw.msg( 'rcfilters-savedqueries-add-new-title' ),
30 popup: {
31 classes: [ 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup' ],
32 padded: true,
33 head: true,
34 label: mw.msg( 'rcfilters-savedqueries-add-new-title' ),
35 $content: $popupContent
36 }
37 }, config ) );
38 // // HACK: Add an icon to the popup head label
39 this.popup.$head.prepend( ( new OO.ui.IconWidget( { icon: 'unClip' } ) ).$element );
40
41 this.input = new OO.ui.TextInputWidget( {
42 validate: /\S/
43 } );
44 layout = new OO.ui.FieldLayout( this.input, {
45 label: mw.msg( 'rcfilters-savedqueries-new-name-label' ),
46 align: 'top'
47 } );
48
49 this.applyButton = new OO.ui.ButtonWidget( {
50 label: mw.msg( 'rcfilters-savedqueries-apply-label' ),
51 classes: [ 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons-apply' ],
52 flags: [ 'primary', 'progressive' ]
53 } );
54 this.cancelButton = new OO.ui.ButtonWidget( {
55 label: mw.msg( 'rcfilters-savedqueries-cancel-label' ),
56 classes: [ 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons-cancel' ]
57 } );
58
59 $popupContent
60 .append(
61 $( '<div>' )
62 .addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-layout' )
63 .append( layout.$element ),
64 $( '<div>' )
65 .addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons' )
66 .append(
67 this.cancelButton.$element,
68 this.applyButton.$element
69 )
70 );
71
72 // Events
73 this.popup.connect( this, {
74 ready: 'onPopupReady'
75 } );
76 this.input.connect( this, { enter: 'onInputEnter' } );
77 this.input.$input.on( {
78 keyup: this.onInputKeyup.bind( this )
79 } );
80 this.cancelButton.connect( this, { click: 'onCancelButtonClick' } );
81 this.applyButton.connect( this, { click: 'onApplyButtonClick' } );
82
83 // Initialize
84 this.$element
85 .addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget' );
86 };
87
88 /* Initialization */
89 OO.inheritClass( mw.rcfilters.ui.SaveFiltersPopupButtonWidget, OO.ui.PopupButtonWidget );
90
91 /**
92 * Respond to input enter event
93 */
94 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputEnter = function () {
95 this.apply();
96 };
97
98 /**
99 * Respond to input keyup event, this is the way to intercept 'escape' key
100 *
101 * @param {jQuery.Event} e Event data
102 * @returns {boolean} false
103 */
104 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputKeyup = function ( e ) {
105 if ( e.which === OO.ui.Keys.ESCAPE ) {
106 this.popup.toggle( false );
107 return false;
108 }
109 };
110
111 /**
112 * Respond to popup ready event
113 */
114 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onPopupReady = function () {
115 this.input.focus();
116 };
117
118 /**
119 * Respond to cancel button click event
120 */
121 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onCancelButtonClick = function () {
122 this.popup.toggle( false );
123 };
124
125 /**
126 * Respond to apply button click event
127 */
128 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onApplyButtonClick = function () {
129 this.apply();
130 };
131
132 /**
133 * Apply and add the new quick link
134 */
135 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.apply = function () {
136 var widget = this,
137 label = this.input.getValue();
138
139 this.input.getValidity()
140 .done( function () {
141 widget.controller.saveCurrentQuery( label );
142 widget.input.setValue( this.input, '' );
143 widget.emit( 'saveCurrent' );
144 widget.popup.toggle( false );
145 } );
146 };
147 }( mediaWiki ) );