Merge "resources: Strip '$' and 'mw' from file closures"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.SaveFiltersPopupButtonWidget.js
1 ( function () {
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 checkBoxLayout,
18 $popupContent = $( '<div>' );
19
20 config = config || {};
21
22 this.controller = controller;
23 this.model = model;
24
25 // Parent
26 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.parent.call( this, $.extend( {
27 framed: false,
28 icon: 'bookmark',
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: 'bookmark' } ) ).$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.setAsDefaultCheckbox = new OO.ui.CheckboxInputWidget();
50 checkBoxLayout = new OO.ui.FieldLayout( this.setAsDefaultCheckbox, {
51 label: mw.msg( 'rcfilters-savedqueries-setdefault' ),
52 align: 'inline'
53 } );
54
55 this.applyButton = new OO.ui.ButtonWidget( {
56 label: mw.msg( 'rcfilters-savedqueries-apply-label' ),
57 classes: [ 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons-apply' ],
58 flags: [ 'primary', 'progressive' ]
59 } );
60 this.cancelButton = new OO.ui.ButtonWidget( {
61 label: mw.msg( 'rcfilters-savedqueries-cancel-label' ),
62 classes: [ 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons-cancel' ]
63 } );
64
65 $popupContent
66 .append(
67 $( '<div>' )
68 .addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-layout' )
69 .append( layout.$element ),
70 $( '<div>' )
71 .addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-options' )
72 .append( checkBoxLayout.$element ),
73 $( '<div>' )
74 .addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons' )
75 .append(
76 this.cancelButton.$element,
77 this.applyButton.$element
78 )
79 );
80
81 // Events
82 this.popup.connect( this, {
83 ready: 'onPopupReady'
84 } );
85 this.input.connect( this, {
86 change: 'onInputChange',
87 enter: 'onInputEnter'
88 } );
89 this.input.$input.on( {
90 keyup: this.onInputKeyup.bind( this )
91 } );
92 this.setAsDefaultCheckbox.connect( this, { change: 'onSetAsDefaultChange' } );
93 this.cancelButton.connect( this, { click: 'onCancelButtonClick' } );
94 this.applyButton.connect( this, { click: 'onApplyButtonClick' } );
95
96 // Initialize
97 this.applyButton.setDisabled( !this.input.getValue() );
98 this.$element
99 .addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget' );
100 };
101
102 /* Initialization */
103 OO.inheritClass( mw.rcfilters.ui.SaveFiltersPopupButtonWidget, OO.ui.PopupButtonWidget );
104
105 /**
106 * Respond to input enter event
107 */
108 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputEnter = function () {
109 this.apply();
110 };
111
112 /**
113 * Respond to input change event
114 *
115 * @param {string} value Input value
116 */
117 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputChange = function ( value ) {
118 value = value.trim();
119
120 this.applyButton.setDisabled( !value );
121 };
122
123 /**
124 * Respond to input keyup event, this is the way to intercept 'escape' key
125 *
126 * @param {jQuery.Event} e Event data
127 * @return {boolean} false
128 */
129 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputKeyup = function ( e ) {
130 if ( e.which === OO.ui.Keys.ESCAPE ) {
131 this.popup.toggle( false );
132 return false;
133 }
134 };
135
136 /**
137 * Respond to popup ready event
138 */
139 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onPopupReady = function () {
140 this.input.focus();
141 };
142
143 /**
144 * Respond to "set as default" checkbox change
145 * @param {boolean} checked State of the checkbox
146 */
147 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onSetAsDefaultChange = function ( checked ) {
148 var messageKey = checked ?
149 'rcfilters-savedqueries-apply-and-setdefault-label' :
150 'rcfilters-savedqueries-apply-label';
151
152 this.applyButton
153 .setIcon( checked ? 'pushPin' : null )
154 .setLabel( mw.msg( messageKey ) );
155 };
156
157 /**
158 * Respond to cancel button click event
159 */
160 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onCancelButtonClick = function () {
161 this.popup.toggle( false );
162 };
163
164 /**
165 * Respond to apply button click event
166 */
167 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onApplyButtonClick = function () {
168 this.apply();
169 };
170
171 /**
172 * Apply and add the new quick link
173 */
174 mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.apply = function () {
175 var label = this.input.getValue().trim();
176
177 // This condition is more for sanity-check, since the
178 // apply button should be disabled if the label is empty
179 if ( label ) {
180 this.controller.saveCurrentQuery( label, this.setAsDefaultCheckbox.isSelected() );
181 this.input.setValue( '' );
182 this.setAsDefaultCheckbox.setSelected( false );
183 this.popup.toggle( false );
184
185 this.emit( 'saveCurrent' );
186 }
187 };
188 }() );