-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_movie.js
133 lines (118 loc) · 4.4 KB
/
add_movie.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// AJAX activity indicators
Ajax.Responders.register({
onCreate: function() {
if($('loading') && Ajax.activeRequestCount > 0)
Effect.Appear('loading', {duration: 0.4, queue: 'end'})
},
onComplete: function() {
if($('loading') && Ajax.activeRequestCount == 0)
Effect.Fade('loading', {duration: 0.4, queue: 'end'})
}
})
// Cross-browser AddEvent function
function addEvent(obj, evType, fn) {
if (obj.addEventListener) {
obj.addEventListener(evType, fn, false)
return true
} else if (obj.attachEvent) {
var r = obj.attachEvent('on' + evType, fn)
return r
} else {
return false
}
}
// Clear query parameters in imdb links (added by imdb.com while searching for titles there)
function beautify_imdb_uri(url) {
var i = url.indexOf('?')
if (i > 0) {
return url.substring(0, i)
} else {
return url
}
}
// Attach separate AJAX call to each star
function add_behaviour() {
// get <a href> stars
var elements = $A( $('rating').getElementsByTagName('a') )
elements.each(function(node) {
node.addEventListener('click', function () {
var message = $('message')
if ($F('url').match(/^https?:\/\/.*imdb\.com\/title\/tt([0-9]{7,12})(\/){0,1}.*$/i)) {
var rating = parseInt(this.id.substr(6))
// make selected rating 'stuck'
$A( $('rating').getElementsByTagName('a') ).each( function(el) {
var id = parseInt(el.id.substr(6))
if (rating >= id) el.style.background="url(star_rating.gif) bottom left"
else el.style.backgroundPosition = "top left"
})
// execute AJAX call
Effect.Fade('message', {duration: 0.4, queue: 'end'})
var pars = 'action=add&rating=' + rating + '&url=' + escape(beautify_imdb_uri($F('url'))) + '&review=' + encodeURIComponent($F('review'))
new Ajax.Request('../../../wp-admin/tools.php?page=wp_movie_ratings_management', { method: 'post', parameters: pars, onComplete: show_response })
} else {
message.setAttribute('class', 'error')
message.innerHTML = '<p><strong>Error: wrong imdb link.</strong></p>'
Effect.Appear('message', {duration: 0.4, queue: 'end'})
$('url').focus()
}
}, false)
})
}
// Focus window and appropriate input field
function initial_focus() {
// focus window
if (window.focus) window.focus()
// focus input -> review, if we have imdb link already or url in another case
var url = $('url')
var review = $('review')
if (url.value.match(/imdb\.com/)) review.focus()
else url.focus()
}
// Parse url and paste it into <input type="text" name="url"> if it's imdb.com page
function parse_uri() {
// No url, nothing to parse
if (location.href.indexOf('?url=') === -1) return
// Parse and set as url if it is imdb movie page
var url = unescape(location.href.substring(location.href.indexOf('?url=') + 5))
if (url.match(/^https?:\/\/.*imdb\.com\/title\/tt([0-9]{7,12})(\/){0,1}.*$/i)) {
$('url').value = beautify_imdb_uri(url)
}
}
// Show AJAX response
function show_response(originalRequest) {
var message = $('message')
var response = unescape(originalRequest.responseText)
var matches = response.match(/<div id="message" class="(.+?)">(.*?)<\/div>/i)
// Not logged in
if (!matches) {
message.setAttribute('class', 'error')
message.innerHTML = '<p><strong>Error: movie rating not added. Perhaps you are not logged in?</strong></p>'
} else {
// Valid response
if (matches.length == 3) {
message.setAttribute('class', matches[1])
message.innerHTML = matches[2]
} else {
// Unknown error
message.setAttribute('class', 'error')
message.innerHTML = '<p><strong>Error: unrecognized AJAX response.</strong></p>'
}
}
// Show message
Effect.Appear('message', {duration: 0.4, queue: 'end'})
// Close the pop-up window on successful update
if (matches && (matches[1] == 'updated fade')) {
// Close the window (after a little over 1s delay, so you can actually read the message)
setTimeout("close_window()", 1200);
}
}
// Empty effects queue and close the window
function close_window() {
// We clear the effects queue because the browser crashes if you issue a window.close when effects are still running
Effect.Queues.get('global').each(function (e) { e.cancel(); })
window.close()
}
// Add onLoad events
addEvent(window, 'load', add_behaviour)
addEvent(window, 'load', parse_uri)
addEvent(window, 'load', initial_focus)