| 1 |
// $Id: ajax_vote_up_down.js,v 1.5 2006/11/24 13:18:35 frjo Exp $
|
| 2 |
|
| 3 |
Drupal.voteUpDownAutoAttach = function() {
|
| 4 |
var vdb = [];
|
| 5 |
$('span.vote-up-inact, span.vote-down-inact, span.vote-up-act, span.vote-down-act').each(function () {
|
| 6 |
// Read in the path to the PHP handler
|
| 7 |
var uri = $(this).attr('title');
|
| 8 |
// Get title from a tag.
|
| 9 |
var atitle = $(this).children('a').attr('title');
|
| 10 |
$(this).attr('title', atitle);
|
| 11 |
// remove href link
|
| 12 |
$(this).html('');
|
| 13 |
// Create an object with this uri. Because
|
| 14 |
// we feed in the span as an argument, we'll be able
|
| 15 |
// to attach events to this element.
|
| 16 |
if (!vdb[uri]) {
|
| 17 |
vdb[uri] = new Drupal.VDB(this, uri);
|
| 18 |
}
|
| 19 |
});
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* A Vote DataBase object
|
| 24 |
*/
|
| 25 |
Drupal.VDB = function(elt, uri) {
|
| 26 |
var db = this;
|
| 27 |
// By making the span element a property of this object,
|
| 28 |
// we get the ability to attach behaviours to that element.
|
| 29 |
this.elt = elt;
|
| 30 |
this.uri = uri;
|
| 31 |
this.id = $(elt).attr('id');
|
| 32 |
this.type = this.id.indexOf('node') > -1 ? 'node' : 'comment';
|
| 33 |
this.dir1 = this.id.indexOf('vote_up') > -1 ? 'up' : 'down';
|
| 34 |
this.dir2 = this.dir1 == 'up' ? 'down' : 'up';
|
| 35 |
// Extract the cid so we can change other elements for the same cid
|
| 36 |
this.cid = db.id.match(/[0-9]+$/);
|
| 37 |
$(elt).click(function() {
|
| 38 |
// Ajax GET request for vote data
|
| 39 |
$.ajax({
|
| 40 |
type: "GET",
|
| 41 |
url: db.uri,
|
| 42 |
success: function (data) {
|
| 43 |
//update the voting arrows
|
| 44 |
$('#' + db.id + '.vote-' + db.dir1 + '-inact')
|
| 45 |
.removeClass('vote-' + db.dir1 + '-inact')
|
| 46 |
.addClass('vote-' + db.dir1 + '-act');
|
| 47 |
$('#vote_' + db.dir2 + '_' + db.type + '_' + db.cid)
|
| 48 |
.removeClass('vote-' + db.dir2 + '-act')
|
| 49 |
.addClass('vote-' + db.dir2 + '-inact');
|
| 50 |
// update the points
|
| 51 |
$('#vote_points_' + db.type + '_' + db.cid).html(data);
|
| 52 |
},
|
| 53 |
error: function (xmlhttp) {
|
| 54 |
alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ db.uri);
|
| 55 |
}
|
| 56 |
});
|
| 57 |
});
|
| 58 |
}
|
| 59 |
|
| 60 |
// Global killswitch
|
| 61 |
if (Drupal.jsEnabled) {
|
| 62 |
$(document).ready(Drupal.voteUpDownAutoAttach);
|
| 63 |
}
|