The attached patch adds a little jQuery magic to bookmark nodes through AJAX. When you click the link, the link label changes immediately, and a POST request is done in the background (so there is no visual delay at all).

This in fact is much better than the GET request that the non-JS version uses, as you should not do any state changes through a GET request, on principle :P.

The patch adds a .js file and reuses the existing menu handler for the JS callback.

CommentFileSizeAuthor
#12 views_bookmark-ajax.patch1.63 KBnedjo
bookmark_js.patch4.61 KBSteven

Comments

moshe weitzman’s picture

Anyone available to review this? If not, I might do so. Would be nice to provide some feedback to users after marking a node.

Steven’s picture

I like the current minimalist interface. Since the change is immediate, I think it is fairly obvious.

The better solution would be to replace the current link with a more graphical indicator/button that better signals the state (e.g. plus-sign / check-mark) and separates it from the action (add / remove from bookmarks).

merlinofchaos’s picture

Status: Needs review » Fixed

I'm pretty happy with this. I had to make a couple of minor changes to make it work with other changes I made, but nothing too concerning. Commited to -dev. Thanks!

moshe weitzman’s picture

Category: feature » bug
Status: Fixed » Active

the js file was omitted from the commit.

Steven’s picture

Status: Active » Reviewed & tested by the community

That means it's still RTBC...

moshe weitzman’s picture

Status: Reviewed & tested by the community » Needs work

I just committed the js file and everything works except after i bookmarl or unbookmark, the node link's text flips state twice, and thus mirepsents the bookmarked status. the bookmark is saved/unsaved file, just the text is changed and then changed back. i guess some event is firing twice or something ... setting to needs work.

moshe weitzman’s picture

i meant to say that the bookmark/unbook action works properly, but the text is wrong.

merlinofchaos’s picture

Whoops. There was a fix I had to make to the JS to get that to work right. I just hadn't gotten back to this to commit the file.

m3avrck’s picture

Any updates to this one? Seeing lots of *missing* JS files :-)

bohtho’s picture

I also see the link which is flipping text twice as Moshe said. Any updates ?

nedjo’s picture

Status: Needs work » Fixed

The .js file is in the DRUPAL-5 branch. Looks like the last commit on the .js file fixed the flipping issue. Works for me.

nedjo’s picture

Status: Fixed » Needs review
StatusFileSize
new1.63 KB

Fails without clean URLs. This seems to be due to the way the extra argument to trigger javascript is appended.

Attached is a patch that removes this extra argument and instead tests for a header sent by jQuery to distinguish between a regular and a jQuery/js request.

KentBye’s picture

I can confirm that ajax bookmarking doesn't work with clean URLs turned off, and that nedjo's patch fixes the issue after Steven's patch is applied. Great patch. Would love to see it committed.

Steven’s picture

Relying on HTTP headers, which are known to be filtered by overzealous proxies and firewalls, seems like a bad idea.

KentBye’s picture

I personally will be using this with clean URLs enabled, and so Steven's original patch works fine for me.

The only addition to Steven's script that I'd love to see is some sort of CSS class or id added to the mark and unmark links so that button images could be easily added. I came up with the following way to directly insert the button images, but I'm sure that there's a better way to do it via CSS theming.

I replaced

      function flipLabel(e) {
        $(e).html(flip[$(e).html()]);
      }

with

      function flipLabel(e) {
        if($(e).children().attr('src').match('list-remove')) {
          $(e).children().remove();
          $(e).html(flip[$(e).html()]);
          $(e).prepend("<img src='http://x/sites/all/modules/views_bookmark/images/list-add.png'> ");
        } else {
          $(e).children().remove();
          $(e).html(flip[$(e).html()]);
          $(e).prepend("<img src='http://x/sites/all/modules/views_bookmark/images/list-remove.png'> ");
        }
      }
      var x = $('a.links_bookmark_1')[i-1];
      if (x) {
        if ($(x).attr('href').match(/\/mark\//)) {
          $(x).prepend("<img src='http://x/sites/all/modules/views_bookmark/images/list-add.png'> ");
        }
        else {
          $(x).prepend("<img src='http://x/sites/all/modules/views_bookmark/images/list-remove.png'> ");
        }
      }

The FlipLabel conditional is keyed off of the name of the image name which is created on the page load, and I'm sure there's a cleaner way to do this as well.

quicksketch’s picture

Status: Needs review » Fixed

I took a good long look at the ajax implementation and made some fairly sweeping changes, but while keeping the original idea intact.

- Fixed several bugs: clean urls, duplicate javascript settings being added, setting proper response headers and making the request expect a json response, rather than manually running Drupal.parseJson.
- The entire link is now replaced, rather than updated. Class attributes and the link title weren't being updated before. I figured it's silly to update all the attributes and content individually, replacing it makes it simpler and more easily themable from the PHP side.
- The default attributes of the javascript links now include helpful classes before and after they have been bookmarked.
- The javascript behavior can now be over ridden with a well-placed hook_link_alter in another module. As well as the classes applied to the bookmark link.

Let's call this one done and open new issues for any new problems. Thanks!

Anonymous’s picture

Status: Fixed » Closed (fixed)
physiotek’s picture

is this included in the official stable release for D5? because when i click on the link it immediatly change the bookmark/unbookmark link but the page refresh. is this patch goal is to avoid the page refresh??
thanks!
pht3k

mooffie’s picture

@pht3k: Please open a new issue. It's best if you provide a link to a faulty page on your website.

(Yes, this feature is included in the 'official' release, but you should use the 'dev' one, since it fixes a bunch of misc bugs.)