-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate.js
66 lines (54 loc) · 2.15 KB
/
rate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(function ($) {
Drupal.behaviors.rate = {
attach: function(context) {
$('.rate-widget:not(.rate-processed)', context).addClass('rate-processed').each(function () {
var widget = $(this);
// as we use drupal_html_id() to generate unique ids
// we have to truncate the '--<id>'
var ids = widget.attr('id').split('--');
ids = ids[0].match(/^rate\-([a-z]+)\-([0-9]+)\-([0-9]+)\-([0-9])$/);
var data = {
content_type: ids[1],
content_id: ids[2],
widget_id: ids[3],
widget_mode: ids[4]
};
$('a.rate-button', widget).click(function() {
var token = this.getAttribute('href').match(/rate\=([a-zA-Z0-9\-_]{32,64})/)[1];
return Drupal.rateVote(widget, data, token);
});
});
}
};
Drupal.rateVote = function(widget, data, token) {
// Invoke JavaScript hook.
widget.trigger('eventBeforeRate', [data]);
$(".rate-info", widget).text(Drupal.t('Saving vote...'));
// Random number to prevent caching, see http://drupal.org/node/1042216#comment-4046618
var random = Math.floor(Math.random() * 99999);
var q = (Drupal.settings.rate.basePath.match(/\?/) ? '&' : '?') + 'widget_id=' + data.widget_id + '&content_type=' + data.content_type + '&content_id=' + data.content_id + '&widget_mode=' + data.widget_mode + '&token=' + token + '&destination=' + encodeURIComponent(Drupal.settings.rate.destination) + '&r=' + random;
if (data.value) {
q = q + '&value=' + data.value;
}
// fetch all widgets with this id as class
widget = $('.' + widget.attr('id'));
$.get(Drupal.settings.rate.basePath + q, function(response) {
if (response.match(/^https?\:\/\/[^\/]+\/(.*)$/)) {
// We got a redirect.
document.location = response;
}
else {
// get parent object
var p = widget.parent();
// Invoke JavaScript hook.
widget.trigger('eventAfterRate', [data]);
widget.before(response);
// remove widget
widget.remove();
widget = undefined;
Drupal.attachBehaviors(p);
}
});
return false;
}
})(jQuery);