summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.js
blob: e498e8034c5b681e25f31419b6cc642a129791e1 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * This file is part of the MediaWiki extension MultimediaViewer.
 *
 * MultimediaViewer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * MultimediaViewer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MultimediaViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, $, oo ) {
	var EP,
		cachedRTL;

	/**
	 * Represents a UI element.
	 *
	 * @class mw.mmv.ui.Element
	 * @abstract
	 * @constructor
	 * @param {jQuery} $container
	 */
	function Element( $container ) {
		oo.EventEmitter.call( this );

		/** @property {jQuery} $container The element that contains the UI element. */
		this.$container = $container;

		/** @property {Object.<string, string[]>} eventsRegistered Events that this element has registered with the DOM. */
		this.eventsRegistered = {};

		/**
		 * @property {Object.<string, jQuery>} $inlineStyles a list of `<style>` elements in the head
		 *  which we use to manipulate pseudo-classes and pseudo-elements.
		 */
		this.$inlineStyles = [];

		/**
		 * Stores named timeouts. See setTimer().
		 *
		 * @private
		 * @property {Object.<string, {timeout: Object, handler: function(), delay: number}>}
		 */
		this.timers = {};
	}

	oo.mixinClass( Element, oo.EventEmitter );

	EP = Element.prototype;

	/**
	 * Checks whether the document is RTL. Assumes it doesn't change.
	 *
	 * @return {boolean}
	 */
	EP.isRTL = function () {
		if ( cachedRTL === undefined ) {
			cachedRTL = $( document.body ).hasClass( 'rtl' );
		}

		return cachedRTL;
	};

	/**
	 * Sets the data for the element.
	 *
	 * @abstract
	 */
	EP.set = function () {};

	/**
	 * Empties the element.
	 *
	 * @abstract
	 */
	EP.empty = function () {};

	/**
	 * Registers listeners.
	 *
	 * @abstract
	 */
	EP.attach = function () {};

	/**
	 * Clears listeners.
	 *
	 * @abstract
	 */
	EP.unattach = function () {
		this.clearEvents();
	};

	/**
	 * Add event handler in a way that will be auto-cleared on lightbox close
	 *
	 * TODO: Unit tests
	 *
	 * @param {string} name Name of event, like 'keydown'
	 * @param {Function} handler Callback for the event
	 */
	EP.handleEvent = function ( name, handler ) {
		if ( this.eventsRegistered[ name ] === undefined ) {
			this.eventsRegistered[ name ] = [];
		}
		this.eventsRegistered[ name ].push( handler );
		$( document ).on( name, handler );
	};

	/**
	 * Remove all events that have been registered on this element.
	 *
	 * TODO: Unit tests
	 */
	EP.clearEvents = function () {
		var i, handlers, thisevent,
			events = Object.keys( this.eventsRegistered );

		for ( i = 0; i < events.length; i++ ) {
			thisevent = events[ i ];
			handlers = this.eventsRegistered[ thisevent ];
			while ( handlers.length > 0 ) {
				$( document ).off( thisevent, handlers.pop() );
			}
		}
	};

	/**
	 * Manipulate CSS directly. This is needed to set styles for pseudo-classes and pseudo-elements.
	 *
	 * @param {string} key some name to identify the style
	 * @param {string|null} style a CSS snippet (set to null to delete the given style)
	 */
	EP.setInlineStyle = function ( key, style ) {

		if ( !this.$inlineStyles ) {
			this.$inlineStyles = [];
		}

		if ( !this.$inlineStyles[ key ] ) {
			if ( !style ) {
				return;
			}

			this.$inlineStyles[ key ] = $( '<style type="text/css" />' ).appendTo( 'head' );
		}

		this.$inlineStyles[ key ].html( style || '' );
	};

	/**
	 * Sets a timer. This is a shortcut to using the native setTimout and then storing
	 * the reference, with some small differences for convenience:
	 * - setting the same timer again clears the old one
	 * - callbacks have the element as their context
	 * Timers are local to the element.
	 * See also clearTimer() and resetTimer().
	 *
	 * @param {string} name
	 * @param {function()} callback
	 * @param {number} delay delay in milliseconds
	 */
	EP.setTimer = function ( name, callback, delay ) {
		var element = this;

		this.clearTimer( name );
		this.timers[ name ] = {
			timeout: null,
			handler: callback,
			delay: delay
		};
		this.timers[ name ].timeout = setTimeout( function () {
			delete element.timers[ name ];
			callback.call( element );
		}, delay );
	};

	/**
	 * Clears a timer. See setTimer().
	 *
	 * @param {string} name
	 */
	EP.clearTimer = function ( name ) {
		if ( name in this.timers ) {
			clearTimeout( this.timers[ name ].timeout );
			delete this.timers[ name ];
		}
	};

	/**
	 * Resets a timer, so that its delay will be relative to when resetTimer() was called, not when
	 * the timer was created. Optionally changes the delay as well.
	 * Resetting a timer that does not exist or has already fired has no effect.
	 * See setTimer().
	 *
	 * @param {string} name
	 * @param {number} [delay] delay in milliseconds
	 */
	EP.resetTimer = function ( name, delay ) {
		if ( name in this.timers ) {
			if ( delay === undefined ) {
				delay = this.timers[ name ].delay;
			}
			this.setTimer( name, this.timers[ name ].handler, delay );
		}
	};

	/**
	 * Makes the entire input/textarea selected when focused.
	 * Invoked with that input/textarea as context.
	 */
	EP.selectAllOnEvent = function () {
		try {
			this.select();
		} catch ( e ) {
			// IE doesn't like when select() is called during the onfocus handler
		}
	};

	/**
	 * Reduces the action of clicks to solely focusing the input/textarea.
	 * Essentialy disables clicking inside the text to select a portion of it.
	 * Invoked with that input/textarea as context.
	 *
	 * @param {jQuery.Event} e
	 * @return {boolean} False to prevent default event
	 */
	EP.onlyFocus = function ( e ) {
		this.focus();
		e.preventDefault();
		return false;
	};

	/**
	 * Flips E (east) and W (west) directions in RTL documents.
	 *
	 * @param {string} keyword a keyword where the first 'e' or 'w' character means a direction (such as a
	 *  tipsy gravity parameter)
	 * @return {string}
	 */
	EP.correctEW = function ( keyword ) {
		if ( this.isRTL() ) {
			keyword = keyword.replace( /[ew]/i, function ( dir ) {
				if ( dir === 'e' ) {
					return 'w';
				} else if ( dir === 'E' ) {
					return 'W';
				} else if ( dir === 'w' ) {
					return 'e';
				} else if ( dir === 'W' ) {
					return 'E';
				}
			} );
		}
		return keyword;
	};

	mw.mmv.ui = {};
	mw.mmv.ui.reuse = {};
	mw.mmv.ui.Element = Element;
}( mediaWiki, jQuery, OO ) );