Merge "filecache: Use current action instead of "view" only in outage mode"
[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 placeholder: mw.msg( 'rcfilters-savedqueries-new-name-placeholder' )
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, {
77 change: 'onInputChange',
78 enter: 'onInputEnter'
79 } );
80 this.input.$input.on( {
81 keyup: this.onInputKeyup.bind( this )
82 } );
83 this.cancelButton.connect( this, { click: 'onCancelButtonClick' } );
84 this.applyButton.connect( this, { click: 'onApplyButtonClick' } );
85
86 // Initialize
87 this.applyButton.setDisabled( !this.input.getValue() );
88 this.$element
89 .addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget' );
90 };
91
92 /* Initialization */
93 OO.inheritClass( mw.rcfilters.ui.SaveFiltersPopupButtonWidget, OO.ui.PopupButtonWidget );
94
95 /**
96 * Respond to input enter event
97 */
98 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputEnter = function () {
99 this.apply();
100 };
101
102 /**
103 * Respond to input change event
104 *
105 * @param {string} value Input value
106 */
107 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputChange = function ( value ) {
108 this.applyButton.setDisabled( !value );
109 };
110
111 /**
112 * Respond to input keyup event, this is the way to intercept 'escape' key
113 *
114 * @param {jQuery.Event} e Event data
115 * @returns {boolean} false
116 */
117 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputKeyup = function ( e ) {
118 if ( e.which === OO.ui.Keys.ESCAPE ) {
119 this.popup.toggle( false );
120 return false;
121 }
122 };
123
124 /**
125 * Respond to popup ready event
126 */
127 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onPopupReady = function () {
128 this.input.focus();
129 };
130
131 /**
132 * Respond to cancel button click event
133 */
134 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onCancelButtonClick = function () {
135 this.popup.toggle( false );
136 };
137
138 /**
139 * Respond to apply button click event
140 */
141 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onApplyButtonClick = function () {
142 this.apply();
143 };
144
145 /**
146 * Apply and add the new quick link
147 */
148 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.apply = function () {
149 var label = this.input.getValue();
150
151 // This condition is more for sanity-check, since the
152 // apply button should be disabled if the label is empty
153 if ( label ) {
154 this.controller.saveCurrentQuery( label );
155 this.input.setValue( '' );
156 this.popup.toggle( false );
157
158 this.emit( 'saveCurrent' );
159 }
160 };
161 }( mediaWiki ) );