Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /**
19 * Represents a UI element.
20 *
21 * @abstract
22 */
23 class UiElement {
24 /**
25 * @param {jQuery} $container
26 */
27 constructor( $container ) {
28 OO.EventEmitter.call( this );
29
30 /** @property {jQuery} $container The element that contains the UI element. */
31 this.$container = $container;
32
33 /** @property {Object.<string, string[]>} eventsRegistered Events that this element has registered with the DOM. */
34 this.eventsRegistered = {};
35 }
36
37 /**
38 * Sets the data for the element.
39 *
40 * @abstract
41 */
42 set() { }
43
44 /**
45 * Empties the element.
46 *
47 * @abstract
48 */
49 empty() { }
50
51 /**
52 * Registers listeners.
53 *
54 * @abstract
55 */
56 attach() { }
57
58 /**
59 * Clears listeners.
60 *
61 * @abstract
62 */
63 unattach() {
64 this.clearEvents();
65 }
66
67 /**
68 * Add event handler in a way that will be auto-cleared on lightbox close
69 *
70 * TODO: Unit tests
71 *
72 * @param {string} name Name of event, like 'keydown'
73 * @param {Function} handler Callback for the event
74 */
75 handleEvent( name, handler ) {
76 if ( this.eventsRegistered[ name ] === undefined ) {
77 this.eventsRegistered[ name ] = [];
78 }
79 this.eventsRegistered[ name ].push( handler );
80 $( document ).on( name, handler );
81 }
82
83 /**
84 * Remove all events that have been registered on this element.
85 *
86 * TODO: Unit tests
87 */
88 clearEvents() {
89 for ( const ev in this.eventsRegistered ) {
90 const handlers = this.eventsRegistered[ ev ];
91 while ( handlers.length > 0 ) {
92 $( document ).off( ev, handlers.pop() );
93 }
94 }
95 }
96 }
97
98 OO.mixinClass( UiElement, OO.EventEmitter );
99
100 module.exports = UiElement;