summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.SaveFiltersPopupButtonWidget.js
blob: 89ad38283f403a8534dee32948459e3d871f1aee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
( function ( mw ) {
	/**
	 * Save filters widget. This widget is displayed in the tag area
	 * and allows the user to save the current state of the system
	 * as a new saved filter query they can later load or set as
	 * default.
	 *
	 * @extends OO.ui.PopupButtonWidget
	 *
	 * @constructor
	 * @param {mw.rcfilters.Controller} controller Controller
	 * @param {mw.rcfilters.dm.SavedQueriesModel} model View model
	 * @param {Object} [config] Configuration object
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget = function MwRcfiltersUiSaveFiltersPopupButtonWidget( controller, model, config ) {
		var layout,
			checkBoxLayout,
			$popupContent = $( '<div>' );

		config = config || {};

		this.controller = controller;
		this.model = model;

		// Parent
		mw.rcfilters.ui.SaveFiltersPopupButtonWidget.parent.call( this, $.extend( {
			framed: false,
			icon: 'bookmark',
			title: mw.msg( 'rcfilters-savedqueries-add-new-title' ),
			popup: {
				classes: [ 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup' ],
				padded: true,
				head: true,
				label: mw.msg( 'rcfilters-savedqueries-add-new-title' ),
				$content: $popupContent
			}
		}, config ) );
		// // HACK: Add an icon to the popup head label
		this.popup.$head.prepend( ( new OO.ui.IconWidget( { icon: 'bookmark' } ) ).$element );

		this.input = new OO.ui.TextInputWidget( {
			placeholder: mw.msg( 'rcfilters-savedqueries-new-name-placeholder' )
		} );
		layout = new OO.ui.FieldLayout( this.input, {
			label: mw.msg( 'rcfilters-savedqueries-new-name-label' ),
			align: 'top'
		} );

		this.setAsDefaultCheckbox = new OO.ui.CheckboxInputWidget();
		checkBoxLayout = new OO.ui.FieldLayout( this.setAsDefaultCheckbox, {
			label: mw.msg( 'rcfilters-savedqueries-setdefault' ),
			align: 'inline'
		} );

		this.applyButton = new OO.ui.ButtonWidget( {
			label: mw.msg( 'rcfilters-savedqueries-apply-label' ),
			classes: [ 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons-apply' ],
			flags: [ 'primary', 'progressive' ]
		} );
		this.cancelButton = new OO.ui.ButtonWidget( {
			label: mw.msg( 'rcfilters-savedqueries-cancel-label' ),
			classes: [ 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons-cancel' ]
		} );

		$popupContent
			.append(
				$( '<div>' )
					.addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-layout' )
					.append( layout.$element ),
				$( '<div>' )
					.addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-options' )
					.append( checkBoxLayout.$element ),
				$( '<div>' )
					.addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget-popup-buttons' )
					.append(
						this.cancelButton.$element,
						this.applyButton.$element
					)
			);

		// Events
		this.popup.connect( this, {
			ready: 'onPopupReady'
		} );
		this.input.connect( this, {
			change: 'onInputChange',
			enter: 'onInputEnter'
		} );
		this.input.$input.on( {
			keyup: this.onInputKeyup.bind( this )
		} );
		this.setAsDefaultCheckbox.connect( this, { change: 'onSetAsDefaultChange' } );
		this.cancelButton.connect( this, { click: 'onCancelButtonClick' } );
		this.applyButton.connect( this, { click: 'onApplyButtonClick' } );

		// Initialize
		this.applyButton.setDisabled( !this.input.getValue() );
		this.$element
			.addClass( 'mw-rcfilters-ui-saveFiltersPopupButtonWidget' );
	};

	/* Initialization */
	OO.inheritClass( mw.rcfilters.ui.SaveFiltersPopupButtonWidget, OO.ui.PopupButtonWidget );

	/**
	 * Respond to input enter event
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputEnter = function () {
		this.apply();
	};

	/**
	 * Respond to input change event
	 *
	 * @param {string} value Input value
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputChange = function ( value ) {
		value = value.trim();

		this.applyButton.setDisabled( !value );
	};

	/**
	 * Respond to input keyup event, this is the way to intercept 'escape' key
	 *
	 * @param {jQuery.Event} e Event data
	 * @return {boolean} false
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onInputKeyup = function ( e ) {
		if ( e.which === OO.ui.Keys.ESCAPE ) {
			this.popup.toggle( false );
			return false;
		}
	};

	/**
	 * Respond to popup ready event
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onPopupReady = function () {
		this.input.focus();
	};

	/**
	 * Respond to "set as default" checkbox change
	 * @param {boolean} checked State of the checkbox
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onSetAsDefaultChange = function ( checked ) {
		var messageKey = checked ?
			'rcfilters-savedqueries-apply-and-setdefault-label' :
			'rcfilters-savedqueries-apply-label';

		this.applyButton
			.setIcon( checked ? 'pushPin' : null )
			.setLabel( mw.msg( messageKey ) );
	};

	/**
	 * Respond to cancel button click event
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onCancelButtonClick = function () {
		this.popup.toggle( false );
	};

	/**
	 * Respond to apply button click event
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.onApplyButtonClick = function () {
		this.apply();
	};

	/**
	 * Apply and add the new quick link
	 */
	mw.rcfilters.ui.SaveFiltersPopupButtonWidget.prototype.apply = function () {
		var label = this.input.getValue().trim();

		// This condition is more for sanity-check, since the
		// apply button should be disabled if the label is empty
		if ( label ) {
			this.controller.saveCurrentQuery( label, this.setAsDefaultCheckbox.isSelected() );
			this.input.setValue( '' );
			this.setAsDefaultCheckbox.setSelected( false );
			this.popup.toggle( false );

			this.emit( 'saveCurrent' );
		}
	};
}( mediaWiki ) );