2014-03-17 08:07:53 +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/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-19 20:26:45 +00:00
|
|
|
const Dialog = require( './mmv.ui.dialog.js' );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-03-17 08:07:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the file reuse dialog and the link to open it.
|
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
class ReuseDialog extends Dialog {
|
2014-06-25 02:15:56 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* @param {jQuery} $container the element to which the dialog will be appended
|
|
|
|
* @param {jQuery} $openButton the button which opens the dialog. Only used for positioning.
|
|
|
|
* @param {Config} config
|
2014-06-25 02:15:56 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
constructor( $container, $openButton, config ) {
|
|
|
|
super( $container, $openButton, config );
|
2014-09-16 20:33:05 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* @property {Object.<string, UiElement>} tabs List of tab ui objects.
|
|
|
|
*/
|
|
|
|
this.tabs = null;
|
2014-09-16 20:33:05 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* @property {Object.<string, OO.ui.MenuOptionWidget>} ooTabs List of tab OOUI objects.
|
|
|
|
*/
|
|
|
|
this.ooTabs = null;
|
2014-09-16 20:33:05 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.loadDependencies.push( 'mmv.ui.reuse.shareembed' );
|
2014-09-16 20:33:05 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.$dialog.addClass( 'mw-mmv-reuse-dialog' );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.eventPrefix = 'use-this-file';
|
|
|
|
}
|
2024-02-13 00:44:51 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// FIXME this should happen outside the dialog and the tabs, but we need to improve
|
|
|
|
initTabs() {
|
|
|
|
const makeTab = ( type ) => new OO.ui.MenuOptionWidget( {
|
2014-11-22 00:01:29 +00:00
|
|
|
data: type,
|
2020-06-26 10:45:20 +00:00
|
|
|
// The following messages are used here:
|
|
|
|
// * multimediaviewer-embed-tab
|
|
|
|
// * multimediaviewer-share-tab
|
2024-02-13 00:44:51 +00:00
|
|
|
label: mw.message( `multimediaviewer-${ type }-tab` ).text()
|
2014-06-25 02:15:56 +00:00
|
|
|
} );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.reuseTabs = new OO.ui.MenuSelectWidget( {
|
|
|
|
autoHide: false,
|
|
|
|
classes: [ 'mw-mmv-reuse-tabs' ]
|
|
|
|
} );
|
|
|
|
this.reuseTabs.$element.appendTo( this.$dialog );
|
2014-07-29 15:56:32 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
const { Embed, Share } = require( 'mmv.ui.reuse.shareembed' );
|
|
|
|
this.tabs = {
|
|
|
|
share: new Share( this.$dialog ),
|
|
|
|
embed: new Embed( this.$dialog )
|
|
|
|
};
|
2014-04-11 14:05:58 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.ooTabs = {
|
|
|
|
share: makeTab( 'share' ),
|
|
|
|
embed: makeTab( 'embed' )
|
|
|
|
};
|
2014-04-11 14:05:58 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.reuseTabs.addItems( [
|
|
|
|
this.ooTabs.share,
|
|
|
|
this.ooTabs.embed
|
|
|
|
] );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// MenuSelectWidget has a nasty tendency to hide itself, maybe we're not using it right?
|
|
|
|
this.reuseTabs.toggle( true );
|
|
|
|
this.reuseTabs.toggle = () => { };
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.selectedTab = this.getLastUsedTab();
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// In case nothing is saved in localStorage or it contains junk
|
|
|
|
if ( !Object.prototype.hasOwnProperty.call( this.tabs, this.selectedTab ) ) {
|
|
|
|
this.selectedTab = 'share';
|
|
|
|
}
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.reuseTabs.selectItem( this.ooTabs[ this.selectedTab ] );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( this.dependenciesNeedToBeAttached ) {
|
|
|
|
this.attachDependencies();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.tabsSetValues ) {
|
|
|
|
// This is a delayed set() for the elements we've just created on demand
|
|
|
|
this.tabs.share.set.apply( this.tabs.share, this.tabsSetValues.share );
|
|
|
|
this.tabs.embed.set.apply( this.tabs.embed, this.tabsSetValues.embed );
|
|
|
|
this.showImageWarnings( this.tabsSetValues.share[ 0 ] );
|
|
|
|
this.tabsSetValues = undefined;
|
2014-03-17 08:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-13 00:44:51 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
toggleDialog() {
|
|
|
|
if ( this.tabs === null ) {
|
|
|
|
this.initTabs();
|
|
|
|
}
|
2014-06-25 02:15:56 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
super.toggleDialog();
|
|
|
|
}
|
2014-06-25 02:15:56 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Handles tab selection.
|
|
|
|
*
|
|
|
|
* @param {OO.ui.MenuOptionWidget} option
|
|
|
|
*/
|
|
|
|
handleTabSelection( option ) {
|
|
|
|
this.selectedTab = option.getData();
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-10-23 19:28:57 +00:00
|
|
|
for ( const tab in this.tabs ) {
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( tab === this.selectedTab ) {
|
|
|
|
this.tabs[ tab ].show();
|
|
|
|
} else {
|
|
|
|
this.tabs[ tab ].hide();
|
|
|
|
}
|
|
|
|
}
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.config.setInLocalStorage( 'mmv-lastUsedTab', this.selectedTab );
|
|
|
|
}
|
2014-04-11 14:05:58 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* @return {string} Last used tab
|
|
|
|
*/
|
|
|
|
getLastUsedTab() {
|
|
|
|
return this.config.getFromLocalStorage( 'mmv-lastUsedTab' );
|
|
|
|
}
|
2014-04-11 14:05:58 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Registers listeners.
|
|
|
|
*/
|
|
|
|
attach() {
|
|
|
|
this.handleEvent( 'mmv-reuse-open', this.handleOpenCloseClick.bind( this ) );
|
2014-04-11 14:05:58 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.handleEvent( 'mmv-download-open', this.closeDialog.bind( this ) );
|
|
|
|
this.handleEvent( 'mmv-options-open', this.closeDialog.bind( this ) );
|
2014-04-11 14:05:58 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.attachDependencies();
|
2014-03-17 08:07:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Registers listeners for dependencies loaded on demand
|
|
|
|
*/
|
|
|
|
attachDependencies() {
|
|
|
|
if ( this.reuseTabs && this.tabs ) {
|
|
|
|
// This is a delayed attach() for the elements we've just created on demand
|
|
|
|
this.reuseTabs.on( 'select', this.handleTabSelection.bind( this ) );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
for ( const tab in this.tabs ) {
|
|
|
|
this.tabs[ tab ].attach();
|
|
|
|
}
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.dependenciesNeedToBeAttached = false;
|
|
|
|
} else {
|
|
|
|
this.dependenciesNeedToBeAttached = true;
|
|
|
|
}
|
2014-04-11 14:05:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Clears listeners.
|
|
|
|
*/
|
|
|
|
unattach() {
|
|
|
|
super.unattach();
|
|
|
|
|
|
|
|
if ( this.reuseTabs ) {
|
|
|
|
this.reuseTabs.off( 'select' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.tabs ) {
|
2023-10-23 19:28:57 +00:00
|
|
|
for ( const tab in this.tabs ) {
|
2023-05-20 12:38:59 +00:00
|
|
|
this.tabs[ tab ].unattach();
|
|
|
|
}
|
2014-04-11 14:05:58 +00:00
|
|
|
}
|
2014-03-17 08:07:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Sets data needed by contained tabs and makes dialog launch link visible.
|
|
|
|
*
|
|
|
|
* @param {Image} image
|
|
|
|
* @param {Repo} repo
|
|
|
|
* @param {string} caption
|
|
|
|
* @param {string} alt
|
|
|
|
*/
|
|
|
|
set( image, repo, caption, alt ) {
|
|
|
|
if ( this.tabs !== null ) {
|
|
|
|
this.tabs.share.set( image );
|
|
|
|
this.tabs.embed.set( image, repo, caption, alt );
|
|
|
|
this.tabs.embed.set( image, repo, caption );
|
|
|
|
this.showImageWarnings( image );
|
|
|
|
} else {
|
|
|
|
this.tabsSetValues = {
|
|
|
|
share: [ image ],
|
|
|
|
embed: [ image, repo, caption, alt ]
|
|
|
|
};
|
|
|
|
}
|
2014-04-11 14:05:58 +00:00
|
|
|
}
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
empty() {
|
|
|
|
super.empty();
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-10-23 19:28:57 +00:00
|
|
|
for ( const tab in this.tabs ) {
|
2023-05-20 12:38:59 +00:00
|
|
|
this.tabs[ tab ].empty();
|
|
|
|
}
|
2014-03-17 08:07:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Fired when the dialog is opened.
|
2023-06-27 20:10:10 +00:00
|
|
|
*
|
2023-05-20 13:02:55 +00:00
|
|
|
* @event ReuseDialog#mmv-reuse-opened
|
2023-05-20 12:38:59 +00:00
|
|
|
*/
|
2023-05-20 13:02:55 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Opens a dialog with information about file reuse.
|
|
|
|
*/
|
|
|
|
openDialog() {
|
|
|
|
super.openDialog();
|
2014-09-05 12:52:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// move warnings after the tabs
|
|
|
|
this.$warning.insertAfter( this.reuseTabs.$element );
|
2014-12-07 08:09:04 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.tabs[ this.selectedTab ].show();
|
2014-09-12 18:05:13 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
$( document ).trigger( 'mmv-reuse-opened' );
|
|
|
|
}
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Fired when the dialog is closed.
|
2023-06-27 20:10:10 +00:00
|
|
|
*
|
2023-05-20 13:02:55 +00:00
|
|
|
* @event ReuseDialog#mmv-reuse-closed
|
2023-05-20 12:38:59 +00:00
|
|
|
*/
|
2023-05-20 13:02:55 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Closes the reuse dialog.
|
|
|
|
*/
|
|
|
|
closeDialog() {
|
|
|
|
super.closeDialog();
|
2014-09-05 12:52:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
$( document ).trigger( 'mmv-reuse-closed' );
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 01:58:41 +00:00
|
|
|
|
2023-05-19 20:26:45 +00:00
|
|
|
module.exports = ReuseDialog;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|