From 003ec7b6538c2e5f4312a803a19a45690fa2cc8b Mon Sep 17 00:00:00 2001 From: Thalia Date: Sat, 19 Nov 2016 15:51:21 +0000 Subject: [PATCH] Allow the gallery dialog to insert duplicate images initially Change I94f4fadd84cd3e prevents the gallery dialog from inserting duplicate images into the gallery dialog after one request (e.g. so double-clicking on an image in the search widget doesn't cause the image to be inserted twice). However, galleries can intentionally contain duplicates of the same image, so it is possible to make a spearate request to insert a duplicate image. When the dialog is first opened, it requests all the images in the gallery at once, so the above change was causing the duplicates in an existing gallery to be dropped. Duplicates should be allowed to be inserted following this initial request. Bug: T150894 Change-Id: I34353bc9b8db947488474c4be52292e0a1447705 --- .../ve-mw/ui/dialogs/ve.ui.MWGalleryDialog.js | 37 ++++++++++++------- .../ui/widgets/ve.ui.MWGalleryItemWidget.js | 2 +- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWGalleryDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWGalleryDialog.js index ddb0506085..d659a55a2d 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWGalleryDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWGalleryDialog.js @@ -52,7 +52,8 @@ ve.ui.MWGalleryDialog.prototype.initialize = function () { // States this.highlightedItem = null; this.searchPanelVisible = false; - this.imageCaptions = {}; + this.selectedFilenames = {}; + this.initialImageData = []; // Default settings this.defaults = mw.config.get( 'wgVisualEditorConfig' ).galleryOptions; @@ -300,7 +301,10 @@ ve.ui.MWGalleryDialog.prototype.getSetupProcess = function ( data ) { if ( title ) { titleText = title.getPrefixedText(); imageTitles.push( titleText ); - dialog.imageCaptions[ titleText ] = matches[ 3 ]; + dialog.initialImageData.push( { + title: titleText, + caption: matches[ 3 ] + } ); } } } @@ -433,14 +437,21 @@ ve.ui.MWGalleryDialog.prototype.onRequestImagesSuccess = function ( deferred, re } } - for ( title in this.imageCaptions ) { - if ( Object.prototype.hasOwnProperty.call( thumbUrls, title ) ) { - items.push( new ve.ui.MWGalleryItemWidget( { - title: title, - caption: this.imageCaptions[ title ], - thumbUrl: thumbUrls[ title ] - } ) ); - delete this.imageCaptions[ title ]; + if ( this.initialImageData.length > 0 ) { + this.initialImageData.forEach( function ( image ) { + image.thumbUrl = thumbUrls[ image.title ]; + items.push( new ve.ui.MWGalleryItemWidget( image ) ); + } ); + this.initialImageData = []; + } else { + for ( title in this.selectedFilenames ) { + if ( Object.prototype.hasOwnProperty.call( thumbUrls, title ) ) { + items.push( new ve.ui.MWGalleryItemWidget( { + title: title, + thumbUrl: thumbUrls[ title ] + } ) ); + delete this.selectedFilenames[ title ]; + } } } this.galleryGroup.addItems( items ); @@ -459,8 +470,8 @@ ve.ui.MWGalleryDialog.prototype.onRequestImagesSuccess = function ( deferred, re ve.ui.MWGalleryDialog.prototype.addNewImage = function ( title ) { var dialog = this; - // Reset this.imageCaptions, for onRequestImagesSuccess - this.imageCaptions[ title ] = ''; + // Make list of unique pending images, for onRequestImagesSuccess + this.selectedFilenames[ title ] = true; // Request image this.requestImages( { @@ -482,7 +493,7 @@ ve.ui.MWGalleryDialog.prototype.addNewImage = function ( title ) { ve.ui.MWGalleryDialog.prototype.onSearchResultsChoose = function ( item ) { var title = mw.Title.newFromText( item.getData().title ).getPrefixedText(); - if ( !Object.prototype.hasOwnProperty( this.imageCaptions, title ) ) { + if ( !Object.prototype.hasOwnProperty( this.selectedFilenames, title ) ) { this.addNewImage( title ); } }; diff --git a/modules/ve-mw/ui/widgets/ve.ui.MWGalleryItemWidget.js b/modules/ve-mw/ui/widgets/ve.ui.MWGalleryItemWidget.js index cae721565b..3df6def85c 100644 --- a/modules/ve-mw/ui/widgets/ve.ui.MWGalleryItemWidget.js +++ b/modules/ve-mw/ui/widgets/ve.ui.MWGalleryItemWidget.js @@ -19,7 +19,7 @@ ve.ui.MWGalleryItemWidget = function VeUiMWGalleryItemWidget( imageInfo, config ) { this.imageTitle = imageInfo.title; this.thumbUrl = imageInfo.thumbUrl; - this.caption = imageInfo.caption; + this.caption = imageInfo.caption || ''; this.highlighted = false; // Configuration initialization