2014-01-22 01:21:22 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Represents a UI element.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
class UiElement {
|
2014-01-22 01:21:22 +00:00
|
|
|
/**
|
2024-05-21 20:12:08 +00:00
|
|
|
* @param {jQuery} $container
|
2014-01-22 01:21:22 +00:00
|
|
|
*/
|
2024-05-21 20:12:08 +00:00
|
|
|
constructor( $container ) {
|
|
|
|
OO.EventEmitter.call( this );
|
2014-04-07 19:19:07 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/** @property {jQuery} $container The element that contains the UI element. */
|
|
|
|
this.$container = $container;
|
2015-09-25 19:55:00 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/** @property {Object.<string, string[]>} eventsRegistered Events that this element has registered with the DOM. */
|
|
|
|
this.eventsRegistered = {};
|
|
|
|
}
|
2014-01-22 01:21:22 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the data for the element.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
set() { }
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Empties the element.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
empty() { }
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Registers listeners.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
attach() { }
|
2014-01-22 01:21:22 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Clears listeners.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
unattach() {
|
|
|
|
this.clearEvents();
|
|
|
|
}
|
2014-02-07 14:47:00 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
handleEvent( name, handler ) {
|
|
|
|
if ( this.eventsRegistered[ name ] === undefined ) {
|
|
|
|
this.eventsRegistered[ name ] = [];
|
2014-02-07 14:47:00 +00:00
|
|
|
}
|
2024-05-21 20:12:08 +00:00
|
|
|
this.eventsRegistered[ name ].push( handler );
|
|
|
|
$( document ).on( name, handler );
|
|
|
|
}
|
2014-04-02 00:07:51 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Remove all events that have been registered on this element.
|
|
|
|
*
|
|
|
|
* TODO: Unit tests
|
|
|
|
*/
|
|
|
|
clearEvents() {
|
|
|
|
for ( const ev in this.eventsRegistered ) {
|
|
|
|
const handlers = this.eventsRegistered[ ev ];
|
|
|
|
while ( handlers.length > 0 ) {
|
|
|
|
$( document ).off( ev, handlers.pop() );
|
2023-05-20 12:38:59 +00:00
|
|
|
}
|
2014-04-02 00:07:51 +00:00
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
}
|
2024-05-21 20:12:08 +00:00
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
OO.mixinClass( UiElement, OO.EventEmitter );
|
2014-10-03 09:15:28 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
module.exports = UiElement;
|