might be a nice idea to make the add to + delete from favorites with ajax functionality - good for the user and less reloads?

Comments

Dabitch’s picture

Wish I knew more about Ajax because yes, that sounds awesome.

mr.andrey’s picture

Yes, this would be great to get going with jquery - so it works like the faves in flickr.

A.

z.stolar’s picture

I need to implement exactly that in a project. Let's hope a patch will evolve out of that.

bobdalob’s picture

#2 only just tried the flickr 'faves' - yes, that's the idea!

ilmaestro’s picture

i'm actually checking how i can implement this same thing. i can get something working using jquery's ajax, but it doesn't actually use the favorite_nodes/add/123 menu path that is built into the favorite nodes module. In general, I don't know if drupal supports ajax calls to URLs this way.

Still researching...

ilmaestro’s picture

Ok guys, I got it working and thought I'd post my results here. I found inspiration with the fasttoggle module, and just went from there:

The following stuff you can add to the favorite_nodes_menu function. Be sure to empty your cache_menu table after making this change.

function favorite_nodes_menu($may_cache) {
	$items = array();

	if ($may_cache) {
		$items[] = array(
			'path'     => 'favoriteNodes/add',
			'callback' => 'favorite_nodes_favorites',
			'type'     => MENU_CALLBACK,
			'access'   => user_access(FAVORITE_NODES_PERM_ADD),
		);

		$items[] = array(
			'path'     => 'favoriteNodes/delete',
			'callback' => 'favorite_nodes_favorites',
			'type'     => MENU_CALLBACK,
			'access'   => user_access(FAVORITE_NODES_PERM_ADD),
		);
	}
}

Next is the new funciton that is called by the two new menu URLs above:

// Wraps the add favorite nodes function
function favorite_nodes_favorites() {
	$op = arg(1);
	$nid = arg(2);
	
	if (($op != "add" && $op != "delete") || !is_numeric($nid)) {
		return;
	}
	
	// The action is confirmed: either via form submit or via AJAX/POST
	if (isset($_POST['confirm']) && $_POST['confirm']) {
		if ($op == "add") {
			$newTitle = "Remove from favorites";
			$newHref = "favoriteNodes/delete/$nid";
			favorite_nodes_add($nid);
		} else {
			$newTitle = "Add to favorites";
			$newHref = "favoriteNodes/add/$nid";
			favorite_nodes_delete($nid);
		}

		// Output the new status for the updated link text on AJAX changes
		if (isset($_POST['javascript']) && $_POST['javascript']) {
			drupal_set_header('Content-Type: text/javascript; charset=utf-8');
			echo drupal_to_js(array(
				"text" => $newTitle,
				"callback" => "favorites",
				"href" => $newHref)
			);
			exit;
		}
		else {
			drupal_goto('node/'. $nid);
		}
	}
}

Finally we have some javascript stuff that kicks off the ajax request to our new menu url handler, and handles the callback:

Drupal.toggle = {
	"favorites": function(data) {
		$(this).html(data.text);
		$(this).attr("href", data.href);
	}
};

if (Drupal.jsEnabled) {
	$(function() {
		$("#toggleFavorites").click(function() {
			var link = $(this).addClass('throbbing');

			jQuery.post(this.href, { confirm: true, javascript: true }, function(data) {
				data = Drupal.parseJson(data);
				if (data) {
					$("#toggleFavorites").removeClass('throbbing');

					// Execute callback
					if (data.callback && Drupal.toggle[data.callback]) {
						Drupal.toggle[data.callback].call(link[0], data);
					}
				}
			});

			// Skip href
			return false;
		});
	});
}

To use the new functionality, just add a link to your page to add to favorites, something like this:

<a nid="106" id="toggleFavorites" href="favoriteNodes/add/106" class="">Add to favorites</a>

Note, the javascript also adds a "throbbing" css class to your link. You can take this stuff out if you want, or download the fasttoggle module to get the CSS and the image for that.

Cheers.

mokargas’s picture

Tried that code ilmaestro, but it doesn't work? At least not for deleting

MagicMatze’s picture

same to me - not working. Maybe we made some mistakes by editing your code?

Maybe you could implement this feature in next release?

ilmaestro’s picture

You say it didn't work for deleting - did it work for adding? The URL for deleting a node from the favorites would of course be different. Example, to delete nid 106:

<a id="toggleFavorites" href="favoriteNodes/delete/106" class="">Delete from favorites</a>
bobdalob’s picture

Solved.....elsewhere. There are so many modules now that I don't often go and see what's out there. Yesterday I had a browse and found "Views Bookmarks", which does the job. I wasn't sure when I saw the name, but it's worth a look.

I'll leave this request as active for those wishing to see this functionality appear in this module.

lainel’s picture

Title: ajax add to/delete from favorites » Where...

Where did you insert these codes?
I'm thinking you inserted them into the fasttoggle file.

kbahey’s picture

Title: Where... » ajax add to/delete from favorites

Please do not change issue titles without a real need for that ....

liquidcms’s picture

further to #10 above it sounds like Views Bookmarks is not being ported to D6 but is replaced with an alternate to this module: http://drupal.org/project/flag which seems to have numerous more features including ajax.