salvattore icon indicating copy to clipboard operation
salvattore copied to clipboard

"Balanced columns" algorithm

Open MoOx opened this issue 10 years ago • 67 comments

This commit include a new algorithm that handle elements height during the repartition of the items. The previous algorithm was pretty simple (just balance same amount of items in each columns), and this can create visual issue if lots of items have different heights. I currently have an implementation that can use more than 5000 items, & with this amount the differences are too big. Checkout an ![commented example](http://cl.ly/image/1c0N2o181932/Screen%20Shot%202014-03-20%20at %2021.53.23.png)

Here is a new algorithm that include a small process to retrieve correct height (by appending them in a additional column showing/retrieving value/removing ). Using a wrapper to append & remove from the dom make this very fast. I don’t get any lag with lots of items on my current board.

Should close #4 (& so close #52 )

A 2nd (simple) commit include fix for latest Chrome (Close #69 & close #40 )

It jshint ok (I've used default grunt) & it should be ok with your coding style. I hope so :)

It not really better on your example (in the repo) since the images doesn't have the appropriate height during loading, but in my case it give something really great (fyi I use https://github.com/suitcss/flex-embed/ to prepare my css to avoid issue like that, very nice for rwd).

This algorithm part should be easily skipped if wrapper in a conditional part (that can be enabled with an option).

MoOx avatar Mar 20 '14 21:03 MoOx

FYI, here is a screen with the new algorithm & a lots of results

screen shot 2014-03-20 at 22 28 28

MoOx avatar Mar 20 '14 21:03 MoOx

Thank you @MoOx, this is fantastic. What if we execute the balance algorithm once the window is completely loaded though? That way we don't need to predict the image heights.

rnmp avatar Mar 21 '14 02:03 rnmp

Also would like to see a live example + any additional instructions to put it on the website. This is gonna be great!

rnmp avatar Mar 21 '14 02:03 rnmp

The problem to wait until window load is that can be long. For me you example pages take like 20secondes to load, so initiating or updating salvatorre will be really weird after that delay. I don't think it should be the default option. But you can still make recreate_columns() public to allow people to call it on window load (a snippet in in README should be enough for that). Like I said, if there is no this new algorithm is the default behavior, so you don't need to change something to make it happen. That being said, you will see here that I only use this new method if we have height data available. If there is no values, we fallback to your original method to found column index for new elements. So this PR should be safe to merge without adding anything for now.

MoOx avatar Mar 21 '14 04:03 MoOx

The lack of this feature has kept me from using Salvattore, so this is really interesting. But I can't get it to run smoothly, do you have a live demo? It randomly fails for me in Chrome and Firefox. And it dosen't work at all in Safari.

michaelnie avatar Mar 23 '14 14:03 michaelnie

@michaelnie Grab the zip of my branch (https://github.com/MoOx/salvattore/archive/master.zip) & just try to open the examples/salvatorre.html. It the default behavior here. But since images are not really big & slow to dowload, the example is not really relevant. We should use examples without images to get something very sexy (or images with specified ratio so salvatorre can get relevant values).

MoOx avatar Mar 23 '14 17:03 MoOx

Would just like to drop in and say I'd love this to be merged with master! :smile: On Tumblr/WordPress/etc. themes with posts that can vary wildly in height, this would improve how Salvattore presents posts dramatically. So, yeah, +1.

ara303 avatar Mar 24 '14 00:03 ara303

Can we expose the balance algorithm so we can call it at will?

Iirc, window.load isn't going to be helpful is we are appending new content (via ajax) to the page. Ideally we should be adding our own x is loaded, do y ?

mikedidthis avatar Mar 24 '14 13:03 mikedidthis

@mikedidthis if you use my branch (bower install git://github.com/MoOx/salvatorre.git) you will bet this algorithm. Also if you use append_elements(), you will the same algorithm used.

MoOx avatar Mar 24 '14 13:03 MoOx

:thumbsup: Thanks @MoOx

It would still be nice to open up the API to allow us to use init or recalculate the grid when we need it.

Happy to do it if @rnmp likes the idea.

mikedidthis avatar Mar 24 '14 14:03 mikedidthis

I totally agree.

MoOx avatar Mar 24 '14 14:03 MoOx

Sorry for the delay! Was on vacation.

I will merge this in the coming days and yes @mikedidthis, that would be very much appreciated! Thank you.

rnmp avatar Mar 26 '14 02:03 rnmp

Sorry to bother @MoOx, but I cannot make this algorithm work locally after downloading the .zip of your repo as instructed (in fact, it wouldn't even load at all in my Safari browser). I need to see a live example working in order to merge, please.

rnmp avatar Mar 28 '14 03:03 rnmp

k I will take a look.

MoOx avatar Mar 28 '14 05:03 MoOx

@rnmp Cool. Once the merge is sorted, I will expose the methods.

mikedidthis avatar Apr 07 '14 09:04 mikedidthis

@rnmp Just pushed a fix for Safari \o/

MoOx avatar Apr 08 '14 06:04 MoOx

Exciting! Will review soon.

rnmp avatar Apr 09 '14 14:04 rnmp

Is there any news on this PR?

scarfacedeb avatar Oct 14 '14 08:10 scarfacedeb

let me rebase on latest change so @rnmp can merge that.

MoOx avatar Oct 14 '14 08:10 MoOx

Rebase, so this should be ok now.

MoOx avatar Oct 14 '14 08:10 MoOx

This works well for me. The PR has stalled a bit, and I'd like to use it. So in the meantime, I've added salvattore-moox to the Bower registry as v1.0.8. Would <3 a review and merge though.

ngokevin avatar Nov 10 '14 21:11 ngokevin

Any news on this?

jonagoldman avatar Nov 27 '14 12:11 jonagoldman

@MoOx @rnmp what's the status on this. Would love to see this merged in...

phillip-haydon avatar Dec 08 '14 16:12 phillip-haydon

I'm tired of rebasing all the time, so @rnmp should just tell me when he is open to use a "less bad" algorithm...

MoOx avatar Dec 08 '14 16:12 MoOx

Sorry @MoOx :( I do appreciate the effort you put in tho.

I'm using your repo at the moment. It's almost perfect for me. Recreation works great when the images have loaded but on initial load because the images haven't loaded yet it falls back to the old way. Looking at changing up your code a bit to add a data-height to work out the calculation.

phillip-haydon avatar Dec 08 '14 16:12 phillip-haydon

I would be more inclined to use / expose the init event. Pair that with the imagesLoaded callback to init the plugin.

Appreciate all the efforts! :+1:

mikedidthis avatar Dec 08 '14 16:12 mikedidthis

@mikedidthis I thought about that but then I would end up with a long column of images which then flickers after all images are loaded :(

I figured knowing the height I could work out up front the column images go into.

phillip-haydon avatar Dec 08 '14 16:12 phillip-haydon

Yeah, I see the issue. From a selfish point of view, I don't think salvattore should worry about images, embeds etc.

I believe this is a decision for @rnmp

mikedidthis avatar Dec 08 '14 16:12 mikedidthis

For what it's worth I added a temp solution:

    <script>
    (function() {
        var imgs = document.querySelectorAll('#gallery a>img'),
            i;

        for (i = 0 ; i < imgs.length ; i++) {
            imgs[i].addEventListener('load', function () {
                salvattore.resort();
            }, true);
        }
    }(salvattore));
    </script>
return {
  append_elements: self.append_elements,
  prepend_elements: self.prepend_elements,
  register_grid: self.register_grid,
  resort: function() {
      Array.prototype.forEach.call(grids, self.recreate_columns);
  }
};

Working fine :) On 100 images it works great.

phillip-haydon avatar Dec 08 '14 17:12 phillip-haydon

What's the status here? Or, why is this not merged? To be honest, without balanced columns this plugin covers not very much use cases.

witrin avatar Mar 02 '15 11:03 witrin