In the past we spoke of various problems with our Ajax support.

This little patch fixes them all.

  • The PHP ajax callback now returns the new link.
  • So our Javascript now works even if the links aren't there in the huge Javascript structure. This is needed for, e.g., Views 2 ajax support.
  • People can now type "Digg this! ([flag-digg-count] diggs)" as the link label, and the counter gets updated without a page refresh. No need to program!
  • Yet, the old behavior, of caching the links in the huge structure is retained. We spoke of getting rid of this, but then there'd be a new problem, that of the "wait indicator". So I think the "old behavior" is actually a nice thing to have. We can always remove it in the future.
  • Themers can add a wait indicator (e.g. throbber) easily: there's a 'flag-waiting' class attached to the wrapper when the link is clicked.

[...] personally I don't find the immediate toggling that helpful, and it makes me feel like nothing happened.

One can simply comment out the "flag_add_extra_js(...)" line, in flag.tpl.php, to get rid of the immediate toggling.

=====

The patch is small and simple. I passed "-U 999" to diff so you could see the whole flag.js file before you.

Comments

quicksketch’s picture

Status: Needs review » Needs work
StatusFileSize
new7.01 KB

This looks great mooffie! I like the AJAX link return better than the huge array. I also like how you just update (or create) the big array as necessary. Pretty neat.

I found a small appearance bug though, when repeatedly toggling a link (2 or more times), the link gets "refreshed" twice, causing the fade effect to double. This is because the "flipLink()" function is called once immediately, then the "refreshLink()" is called after the AJAX request completes.

So this patch fixes that problem (plus some minor spacing issues), but it breaks the ability to have "Digg this! ([flag-digg-count] diggs)", because the settings are no longer updated on every request. Even without these changes, the Digg example would have some minor issues, where the link would be toggled immediately but the count would be wrong, then the AJAX request comes back and the number would be updated appropriately.

mooffie’s picture

[T]he Digg example would have some minor issues, where the link would be toggled immediately but the count would be wrong, then the AJAX request comes back and the number would be updated appropriately.

I was aware of this, but I didn't consider this a showstopper.

small appearance bug though, when repeatedly toggling a link (2 or more times), the link gets "refreshed" twice, causing the fade effect to double.

I wasn't aware of this (I'm using Opera, where that message doesn't show; I did check the patch on IE, but didn't notice something amiss).

I'll try to think of some other solution, because I think that the "digg" thing could be a useful feature.

mooffie’s picture

(And thanks for the review. You're quite a discerning person, noticing that digg "minor issue", and that the huge array gets *updated* :-)

quicksketch’s picture

Status: Needs work » Needs review
StatusFileSize
new7.4 KB

This version fixes the "Digg problem" and my double-refresh problem. I added a "settings.linkUpdated" variable, which we can check to prevent the flagged message from getting the fadeIn() effect twice.

mooffie’s picture

StatusFileSize
new2.14 KB
new9.33 KB

Nate, thanks.

While I was off-line I revisited your old idea, of getting rid of the "immediate update" feature altogether. I think we put it off because we suspected there might be aesthetic issues with the waiting indicator.

We should decide on this before continuing.

So I'm attaching a patch that gets rid of the "immediate update" and uses a throbber instead.

What are your thoughts on this? Should we pick this route?

('flag-throbber.gif', which I stole from Views, is to be placed in the 'theme' folder.)

mooffie’s picture

BTW, you may wonder why the patch mixes two different terms, "waiting" and "throbber", instead of unifying them. "throbber" is just the object in which the "waiting" state is expressed. Themers could alter the template to use some other means to express this waiting state.

mooffie’s picture

(I won't be here for the following two days. It's a holiday here. Good and happy new year, Nate :-)

quicksketch’s picture

The terminology all makes perfect sense to me. Great job. Here's a revised patch that I consider a real possibility for inclusion in the module (with all the commented out code removed).

I actually like this method more than our current method in every regard. I like the delay during the request, as it makes me feel like something is actually happening. The fade-out of the link is also a nice touch. I added one safeguard to your code that makes the click handler immediately return false if the link is already waiting for a request, this should help with those impulsive "double-clickers" out there on the internet from sending identical requests to the server.

The only downer is the matte color of white behind the throbber. This is probably more acceptable in the auto-complete textfield and in the Views interface, because it's very likely that the background will be white. However, there's only so much we can provide out-of-box before the user needs to roll up their sleeves and do some CSS.

mooffie’s picture

Nate, you forgot to attach the patch.

[...] that I consider a real possibility for inclusion in the module

I'll commit your patch. (Or you can commit it yourself, you don't really have to attach it here. OTOH, if you don't have the time to "port" it to D5, post it here and I'll gladly do the rest.)

I actually like this method more than our current method in every regard.

Me too.

The only downer is the matte color of white behind the throbber.

The image is transparent, so I guess you're refering to the antialiasing circle at the edge. I did some tests on various backgrounds and the results are acceptable in my eyes.

quicksketch’s picture

StatusFileSize
new7.4 KB

Shoot, sorry. Here's the patch.

mooffie’s picture

It's the wrong patch. It's the same as that in #4.

If you lost it, let me know if it's fine that I add the "double-click guard" myself and commit it.

quicksketch’s picture

Sigh, well crap. It seems to be missing entirely. Let's cleanup your patch in #5 again and go from there.

mooffie’s picture

StatusFileSize
new10.46 KB

Here's the new patch. Changes:

- Double-click guard.

- getLinkSettings() is gone.

- flag_add_extra_js() is gone (if, in the future, we need some global JS settings, we can inject them into the page in the theme preprocess function).

- The ajax callback can return an error messages for the JS to show (This is needed for #315881: More robust flag/unflag links as well).

- "& nbsp ;" was added inside the flag throbber SPAN or else IE won't show it. (That's what Views too does, but I didn't know the reason for this till I tested the pages under IE.) The padding in the CSS was trimmed a bit so this extra whitespace is not noticable IMHO.

quicksketch’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new10.2 KB

This looks great! You got all the same points I had, like just binding the link to flagClick directly with no wrappers, setting var element = this, link guard, etc.

Somehow a tab snuck into the JS file. Here it is without the tab.

quicksketch’s picture

Status: Reviewed & tested by the community » Fixed

I committed these changes after a (rather painful) port to Drupal 5. The caveat I ran into with Drupal 5's jQuery was this line:

var $nucleus = $newLink.is('a') ? $newLink : $newLink.find('a.flag');

In jQuery 1.0.4, the find() method permanently affects the original object, so $newLink became just the a.flag that was found (this can be undone by calling $newLink.end()).

My workaround was just to avoid using find at all with this line:

var $nucleus = $newLink.is('a') ? $newLink : $('a.flag', $newLink);

Besides this change, I also adjusted the formatting of our code, so it look like the entire JS file has been changed due to the removal of indenting. Now both the D5 and D6 versions are identical except for the last 6 lines of the file.

mooffie’s picture

Nathan, thanks for all the work! The module is much healthier now.

Removing the indentation was very useful.

mooffie’s picture

I added a page to the handbook:

How to implement Digg-like links

quicksketch’s picture

Rock!

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.