unobtrusive_flash.js
2.56 KB
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
window.UnobtrusiveFlash = {};
(function() {
// Delete the cookie regardless of the domain it was set from
// Partial credit to http://stackoverflow.com/a/2959110/6678
function nukeCookie(cookieName) {
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
var hostParts = window.location.hostname.split('.').reverse();
var expireHost = hostParts.shift();
$.each(hostParts, function(i,part) {
expireHost = part + '.' + expireHost;
document.cookie = cookieName+'=; path=/;expires='+yesterday+'; domain='+expireHost;
});
document.cookie = cookieName+'=; path=/;expires='+yesterday+'; domain=';
}
// Extracts flash array stored in cookie and clears the cookie
function extractFlashFromCookies() {
var data = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
var name = 'flash';
var cookieValue = null;
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
if (cookie.substring(0, name.length + 1) == (name + '=')) {
// replace fixes problems with Rails escaping. Duh.
cookieValue = decodeURIComponent(cookie.substring(name.length + 1).replace(/\+/g,'%20'));
if (cookieValue.length > 0) break; // there might be empty "flash=" cookies
}
}
try {
data = $.parseJSON(cookieValue);
} catch(e) {
}
nukeCookie('flash');
}
return data;
}
window.UnobtrusiveFlash.showFlash = function(flashMessages) {
if (flashMessages !== null) {
$.each(flashMessages, function(_, flashMessage) {
$(window).trigger('rails:flash', {type: flashMessage[0], message: flashMessage[1]});
});
}
};
// Reads flash messages from cookies and fires corresponding events
window.UnobtrusiveFlash.showFlashFromCookies = function() {
UnobtrusiveFlash.showFlash(extractFlashFromCookies());
};
// We want to remove cookies from the flash as soon as possible, but we only want to show then after all the scripts have loaded,
// including any flash handlers
var pageCookies = extractFlashFromCookies();
$(window).on('load', function() {
UnobtrusiveFlash.showFlash(pageCookies);
});
$(document).on('page:change page:load', UnobtrusiveFlash.showFlashFromCookies); //TURBOLINK-CLASSIC
$(document).on('turbolinks:load', UnobtrusiveFlash.showFlashFromCookies); //TURBOLINK (5)
$(document).ajaxComplete(function(event,request,options) {
UnobtrusiveFlash.showFlashFromCookies();
});
})();