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.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | flag_better_ajax_really.patch | 10.2 KB | quicksketch |
| #13 | better_ajax_really.diff | 10.46 KB | mooffie |
| #10 | flag_better_ajax.patch | 7.4 KB | quicksketch |
| #5 | ajax_with_throbber.diff | 9.33 KB | mooffie |
| #5 | flag-throbber.gif | 2.14 KB | mooffie |
Comments
Comment #1
quicksketchThis 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.
Comment #2
mooffie commentedI was aware of this, but I didn't consider this a showstopper.
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.
Comment #3
mooffie commented(And thanks for the review. You're quite a discerning person, noticing that digg "minor issue", and that the huge array gets *updated* :-)
Comment #4
quicksketchThis 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.
Comment #5
mooffie commentedNate, 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.)
Comment #6
mooffie commentedBTW, 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.
Comment #7
mooffie commented(I won't be here for the following two days. It's a holiday here. Good and happy new year, Nate :-)
Comment #8
quicksketchThe 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.
Comment #9
mooffie commentedNate, you forgot to attach the patch.
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.)
Me too.
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.
Comment #10
quicksketchShoot, sorry. Here's the patch.
Comment #11
mooffie commentedIt'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.
Comment #12
quicksketchSigh, well crap. It seems to be missing entirely. Let's cleanup your patch in #5 again and go from there.
Comment #13
mooffie commentedHere'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.
Comment #14
quicksketchThis 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.
Comment #15
quicksketchI committed these changes after a (rather painful) port to Drupal 5. The caveat I ran into with Drupal 5's jQuery was this line:
In jQuery 1.0.4, the find() method permanently affects the original object, so $newLink became just the
a.flagthat was found (this can be undone by calling$newLink.end()).My workaround was just to avoid using find at all with this line:
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.
Comment #16
mooffie commentedNathan, thanks for all the work! The module is much healthier now.
Removing the indentation was very useful.
Comment #17
mooffie commentedI added a page to the handbook:
How to implement Digg-like links
Comment #18
quicksketchRock!
Comment #19
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.