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/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-05-31 00:18:12 +00:00
|
|
|
var EP,
|
|
|
|
cachedRTL;
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2014-01-22 01:21:22 +00:00
|
|
|
/**
|
2014-02-19 02:27:30 +00:00
|
|
|
* Represents a UI element.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-01-22 01:21:22 +00:00
|
|
|
* @class mw.mmv.ui.Element
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
function Element( $container ) {
|
2018-11-12 16:33:24 +00:00
|
|
|
OO.EventEmitter.call( this );
|
2015-09-25 19:55:00 +00:00
|
|
|
|
2014-01-22 01:21:22 +00:00
|
|
|
/** @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-04-02 00:07:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 = [];
|
2014-04-07 19:19:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores named timeouts. See setTimer().
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @private
|
2014-04-07 19:19:07 +00:00
|
|
|
* @property {Object.<string, {timeout: Object, handler: function(), delay: number}>}
|
|
|
|
*/
|
|
|
|
this.timers = {};
|
2014-01-22 01:21:22 +00:00
|
|
|
}
|
2015-09-25 19:55:00 +00:00
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
OO.mixinClass( Element, OO.EventEmitter );
|
2015-09-25 19:55:00 +00:00
|
|
|
|
2014-03-17 08:07:53 +00:00
|
|
|
EP = Element.prototype;
|
2014-01-22 01:21:22 +00:00
|
|
|
|
2014-05-31 00:18:12 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the document is RTL. Assumes it doesn't change.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {boolean}
|
2014-05-31 00:18:12 +00:00
|
|
|
*/
|
|
|
|
EP.isRTL = function () {
|
|
|
|
if ( cachedRTL === undefined ) {
|
|
|
|
cachedRTL = $( document.body ).hasClass( 'rtl' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedRTL;
|
|
|
|
};
|
|
|
|
|
2014-01-22 01:21:22 +00:00
|
|
|
/**
|
|
|
|
* Sets the data for the element.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @abstract
|
2014-01-22 01:21:22 +00:00
|
|
|
*/
|
2014-03-17 08:07:53 +00:00
|
|
|
EP.set = function () {};
|
2014-01-22 01:21:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Empties the element.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @abstract
|
2014-01-22 01:21:22 +00:00
|
|
|
*/
|
2014-03-17 08:07:53 +00:00
|
|
|
EP.empty = function () {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers listeners.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @abstract
|
2014-03-17 08:07:53 +00:00
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.attach = function () {};
|
2014-03-17 08:07:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears listeners.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @abstract
|
2014-03-17 08:07:53 +00:00
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.unattach = function () {
|
2014-03-17 08:07:53 +00:00
|
|
|
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
|
2014-03-17 08:07:53 +00:00
|
|
|
*
|
|
|
|
* TODO: Unit tests
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @param {string} name Name of event, like 'keydown'
|
|
|
|
* @param {Function} handler Callback for the event
|
2014-02-07 14:47:00 +00:00
|
|
|
*/
|
2014-03-17 08:07:53 +00:00
|
|
|
EP.handleEvent = function ( name, handler ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
if ( this.eventsRegistered[ name ] === undefined ) {
|
|
|
|
this.eventsRegistered[ name ] = [];
|
2014-02-07 14:47:00 +00:00
|
|
|
}
|
2016-07-18 13:49:27 +00:00
|
|
|
this.eventsRegistered[ name ].push( handler );
|
2014-02-07 14:47:00 +00:00
|
|
|
$( 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++ ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
thisevent = events[ i ];
|
|
|
|
handlers = this.eventsRegistered[ thisevent ];
|
2014-02-07 14:47:00 +00:00
|
|
|
while ( handlers.length > 0 ) {
|
|
|
|
$( document ).off( thisevent, handlers.pop() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-02 00:07:51 +00:00
|
|
|
/**
|
|
|
|
* Manipulate CSS directly. This is needed to set styles for pseudo-classes and pseudo-elements.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-04-02 00:07:51 +00:00
|
|
|
* @param {string} key some name to identify the style
|
|
|
|
* @param {string|null} style a CSS snippet (set to null to delete the given style)
|
|
|
|
*/
|
2014-04-04 22:02:16 +00:00
|
|
|
EP.setInlineStyle = function ( key, style ) {
|
2014-04-02 00:07:51 +00:00
|
|
|
|
|
|
|
if ( !this.$inlineStyles ) {
|
|
|
|
this.$inlineStyles = [];
|
|
|
|
}
|
|
|
|
|
2016-07-18 13:49:27 +00:00
|
|
|
if ( !this.$inlineStyles[ key ] ) {
|
2014-04-02 00:07:51 +00:00
|
|
|
if ( !style ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-06 01:56:53 +00:00
|
|
|
this.$inlineStyles[ key ] = $( '<style>' ).attr( 'type', 'text/css' ).appendTo( 'head' );
|
2014-04-02 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 13:49:27 +00:00
|
|
|
this.$inlineStyles[ key ].html( style || '' );
|
2014-04-02 00:07:51 +00:00
|
|
|
};
|
|
|
|
|
2014-04-07 19:19:07 +00:00
|
|
|
/**
|
|
|
|
* 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().
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-04-07 19:19:07 +00:00
|
|
|
* @param {string} name
|
|
|
|
* @param {function()} callback
|
|
|
|
* @param {number} delay delay in milliseconds
|
|
|
|
*/
|
|
|
|
EP.setTimer = function ( name, callback, delay ) {
|
|
|
|
var element = this;
|
|
|
|
|
|
|
|
this.clearTimer( name );
|
2016-07-18 13:49:27 +00:00
|
|
|
this.timers[ name ] = {
|
2014-04-07 19:19:07 +00:00
|
|
|
timeout: null,
|
|
|
|
handler: callback,
|
|
|
|
delay: delay
|
|
|
|
};
|
2016-07-18 13:49:27 +00:00
|
|
|
this.timers[ name ].timeout = setTimeout( function () {
|
|
|
|
delete element.timers[ name ];
|
2014-04-07 19:19:07 +00:00
|
|
|
callback.call( element );
|
|
|
|
}, delay );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears a timer. See setTimer().
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-04-07 19:19:07 +00:00
|
|
|
* @param {string} name
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.clearTimer = function ( name ) {
|
2014-04-07 19:19:07 +00:00
|
|
|
if ( name in this.timers ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
clearTimeout( this.timers[ name ].timeout );
|
|
|
|
delete this.timers[ name ];
|
2014-04-07 19:19:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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().
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-04-07 19:19:07 +00:00
|
|
|
* @param {string} name
|
|
|
|
* @param {number} [delay] delay in milliseconds
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.resetTimer = function ( name, delay ) {
|
2014-04-07 19:19:07 +00:00
|
|
|
if ( name in this.timers ) {
|
|
|
|
if ( delay === undefined ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
delay = this.timers[ name ].delay;
|
2014-04-07 19:19:07 +00:00
|
|
|
}
|
2016-07-18 13:49:27 +00:00
|
|
|
this.setTimer( name, this.timers[ name ].handler, delay );
|
2014-04-07 19:19:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-22 17:44:44 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-03 09:15:28 +00:00
|
|
|
/**
|
|
|
|
* Flips E (east) and W (west) directions in RTL documents.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-10-03 09:15:28 +00:00
|
|
|
* @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;
|
|
|
|
};
|
|
|
|
|
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;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|