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/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
( function ( mw, $ ) {
|
2014-03-17 08:07:53 +00:00
|
|
|
var EP;
|
|
|
|
|
2014-01-22 01:21:22 +00:00
|
|
|
/**
|
2014-02-19 02:27:30 +00:00
|
|
|
* Represents a UI element.
|
2014-01-22 01:21:22 +00:00
|
|
|
* @class mw.mmv.ui.Element
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
function Element( $container ) {
|
|
|
|
/** @property {jQuery} $container The element that contains the UI element. */
|
|
|
|
this.$container = $container;
|
2014-02-07 14:47:00 +00:00
|
|
|
|
2014-02-12 22:18:13 +00:00
|
|
|
/** @property {Object.<string, string[]>} eventsRegistered Events that this element has registered with the DOM. */
|
2014-02-07 14:47:00 +00:00
|
|
|
this.eventsRegistered = {};
|
2014-01-22 01:21:22 +00:00
|
|
|
}
|
2014-03-17 08:07:53 +00:00
|
|
|
EP = Element.prototype;
|
2014-01-22 01:21:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* Sets the data for the element.
|
|
|
|
*/
|
2014-03-17 08:07:53 +00:00
|
|
|
EP.set = function () {};
|
2014-01-22 01:21:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* Empties the element.
|
|
|
|
*/
|
2014-03-17 08:07:53 +00:00
|
|
|
EP.empty = function () {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* Registers listeners.
|
|
|
|
*/
|
|
|
|
EP.attach = function() {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* Clears listeners.
|
|
|
|
*/
|
|
|
|
EP.unattach = function() {
|
|
|
|
this.clearEvents();
|
|
|
|
};
|
2014-01-22 01:21:22 +00:00
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
/**
|
|
|
|
* Add event handler in a way that will be auto-cleared on lightbox close
|
|
|
|
* @param {string} name Name of event, like 'keydown'
|
|
|
|
* @param {Function} handler Callback for the event
|
2014-03-17 08:07:53 +00:00
|
|
|
*
|
|
|
|
* TODO: Unit tests
|
2014-02-07 14:47:00 +00:00
|
|
|
*/
|
2014-03-17 08:07:53 +00:00
|
|
|
EP.handleEvent = function ( name, handler ) {
|
2014-02-07 14:47:00 +00:00
|
|
|
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.
|
2014-03-17 08:07:53 +00:00
|
|
|
*
|
|
|
|
* TODO: Unit tests
|
2014-02-07 14:47:00 +00:00
|
|
|
*/
|
2014-03-17 08:07:53 +00:00
|
|
|
EP.clearEvents = function () {
|
2014-02-07 14:47:00 +00:00
|
|
|
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() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-22 01:21:22 +00:00
|
|
|
mw.mmv.ui = {};
|
2014-03-17 08:07:53 +00:00
|
|
|
mw.mmv.ui.reuse = {};
|
2014-01-22 01:21:22 +00:00
|
|
|
mw.mmv.ui.Element = Element;
|
2014-02-07 14:47:00 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|