Justified-Gallery icon indicating copy to clipboard operation
Justified-Gallery copied to clipboard

Replace image in justified gallery.

Open plugowski opened this issue 9 years ago • 11 comments

Hi,

My problem is that I display justified gallery, but user has possibility to manipulate image in gallery (crop, rotate). Unfortunately after that operation, when I destroy object and initialize it again, DOM remebered original size of container and it looks terible.

Any suggestions?

var $photos = $('div.photos');
$.post('/crop_photo/' + $imageId + '.json', {crop: cropData}, function(data){

if (typeof data.photo_url != 'undefined') {
    $photos.justifiedGallery('destroy');
    $relatedTarget.closest('.image').find('img').attr('src', data.thumb_url);
    $relatedTarget.closest('.image').find('[data-action="crop"]').attr('data-image', data.photo_url);
    $photos.justifiedGallery(justifiedGalleryOptions);
}
});

data.thumb_url and data.photo_url contains url with timestamp to prevent browser to get it from cache.

plugowski avatar Oct 12 '15 17:10 plugowski

Maybe JustifiedGallery.prototype.rewind()?

biziclop avatar Oct 12 '15 17:10 biziclop

rewind didn't work in that case... or, can you tell me how to use it?

plugowski avatar Oct 12 '15 17:10 plugowski

JustifiedGallery.prototype.init sets

              $entry.data('jg.width', width);
              $entry.data('jg.height', height);

but JustifiedGallery.prototype.destroy doesn't seem to remove them. so maybe running

$('div.photos > photo').data({
  'jg.width': undefined,
  'jg.height': undefined
});

before rebuilding the gallery helps.

Also:

/* If we have the height and the width, we don't wait that the image is loaded, but we start directly
 * with the justification */
          if (that.settings.waitThumbnailsLoad === false) {
            var width = parseInt($image.attr('width'), 10);
            var height = parseInt($image.attr('height'), 10);

Do you have width/height attr on your images?

OK, I shut up now, I'm just browsing the source.

biziclop avatar Oct 12 '15 18:10 biziclop

I tried to:

  • setting waitThumbnailsLoad: false
  • add to destroy method: $entry.data('jg.width', undefined); $entry.data('jg.height', undefined);
  • $relatedTarget.closest('.image').data({ 'jg.width': undefined, 'jg.height': undefined });

Nothing works as I want to..

plugowski avatar Oct 12 '15 18:10 plugowski

What if you do this unspeakable horror (just for debugging):

var html = $photos.html();
$photos.empty().append( $.parseHTML( html ));

biziclop avatar Oct 12 '15 18:10 biziclop

Well, that works :P

plugowski avatar Oct 12 '15 18:10 plugowski

Hmm, you change img src: $relatedTarget.closest('.image').find('img').attr('src', data.thumb_url); Then you immediately rebuild the gallery. Maybe poor img tag still remembers the old size. Maybe you should replace just the img to a fresh one. Maybe you could img.width = undefined; img.height = undefined

biziclop avatar Oct 12 '15 18:10 biziclop

I tried also to remove whole img tag and put the new one with no result... Maybe jg puts something to cache, local storage or something (i'm not advanced js developer so that is not obvious for me).

plugowski avatar Oct 12 '15 18:10 plugowski

I'm out of ideas. JG seems to clean up everything nicely. Could you put it online, maybe a simplified version? Also, you could ask it on StackOverflow too.

biziclop avatar Oct 12 '15 18:10 biziclop

I'll try put it tomorrow :)

plugowski avatar Oct 12 '15 19:10 plugowski

Dear all!

JG can't clean up just everything, it resets the old values, so it is normal if they are changed in the meanwhile.

Seems more that you just need to call justifiedGallery again, also without any option, because it remembers them. Something like:

if (typeof data.photo_url != 'undefined') {
    $relatedTarget.closest('.image').find('img').attr('src', data.thumb_url);
    $relatedTarget.closest('.image').find('[data-action="crop"]').attr('data-image', data.photo_url);
    $photos.justifiedGallery();
}

Have you tried that?

As @biziclop have said, if you provide a working example (send me an email if it is a kind of private), I can debug that better.

Thank you

miromannino avatar Oct 13 '15 10:10 miromannino