| 1 |
{# |
| 2 |
template for html fragment injected by auto_redirect extension |
| 3 |
|
| 4 |
args |
| 5 |
---- |
| 6 |
redirector -- RedirectSettings instance |
| 7 |
.subject |
| 8 |
.domain_url |
| 9 |
.domain_root |
| 10 |
.timeout |
| 11 |
|
| 12 |
NOTE: the html fragment is designed to layout properly, and provide a minimally functional |
| 13 |
redirect, even if the JS fails. |
| 14 |
#} |
| 15 |
|
| 16 |
<div class="admonition warning without-title c-redirect-warning"> |
| 17 |
<p class="c-redirect-warning__header"> |
| 18 |
{{ redirector.domain_subject }} has moved to |
| 19 |
<a class="c-redirect-warning__link" href="{{ redirector.domain_url }}">{{ redirector.domain_url }}</a> |
| 20 |
</p> |
| 21 |
<p class="c-redirect-warning__auto" style="display: none"> |
| 22 |
You will be redirected to the new location in |
| 23 |
<span class="c-redirect-warning__count">{{ redirector.timeout }}</span> seconds. |
| 24 |
</p> |
| 25 |
{% if redirector.domain_footer %} |
| 26 |
<p class="c-redirect-warning__footer"> |
| 27 |
{{ redirector.domain_footer|safe }} |
| 28 |
</p> |
| 29 |
{% endif %} |
| 30 |
</div> |
| 31 |
|
| 32 |
<script type="text/javascript"> |
| 33 |
$(function (){ |
| 34 |
// locate components & init state |
| 35 |
var $overlay = $('<div class="c-redirect-overlay" />'), |
| 36 |
$warning = $(".c-redirect-warning"), |
| 37 |
$link = $(".c-redirect-warning__link"), |
| 38 |
$auto = $(".c-redirect-warning__auto"), |
| 39 |
$count = $(".c-redirect-warning__count"), |
| 40 |
timeout = +new Date + {{ redirector.timeout }} * 1000, |
| 41 |
pending = null, |
| 42 |
target = $link.attr("href"), |
| 43 |
domain_root = "{{ redirector.domain_root }}", |
| 44 |
url = document.location.href, |
| 45 |
root_url = CST.hosturl + CST.rootpath, |
| 46 |
getSubUrl = CST.utils.getSubUrl; |
| 47 |
|
| 48 |
// make redirect target point to equivalent page, not just document root |
| 49 |
// XXX: could just pull from link[rel=canonical] instead... |
| 50 |
var sub_url = getSubUrl(url, root_url); |
| 51 |
if(domain_root && sub_url !== undefined){ |
| 52 |
target = domain_root + sub_url; |
| 53 |
$link.attr("href", target); |
| 54 |
} |
| 55 |
|
| 56 |
// make warning clicks act like they clicked link |
| 57 |
$warning.click(function (evt){ |
| 58 |
if(evt.which == 1) { window.location = target; } |
| 59 |
evt.stopPropagation(); |
| 60 |
}).addClass("is-clickable"); |
| 61 |
|
| 62 |
// if came from within document, dont' show overlay or do auto redirect |
| 63 |
if(getSubUrl(document.referrer || '', root_url) !== undefined){ |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
// mute everything but warning |
| 68 |
$overlay.appendTo("body").append($warning); |
| 69 |
|
| 70 |
// helper update counter & redirect after timeout |
| 71 |
function update(){ |
| 72 |
var delta = timeout - new Date; |
| 73 |
if(delta < 0){ |
| 74 |
window.location = target; |
| 75 |
setTimeout(function (){ $auto.hide(); }, 250); |
| 76 |
} else { |
| 77 |
$count.html(Math.ceil(delta / 1000)); |
| 78 |
pending = setTimeout(update, 250); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// start counter |
| 83 |
{% if redirector.timeout > 0 %} |
| 84 |
update(); |
| 85 |
$auto.show(); |
| 86 |
{% endif %} |
| 87 |
|
| 88 |
// stop auto-redirect if they click outside of warning |
| 89 |
$(window).one("click", function (evt){ |
| 90 |
$auto.hide(); |
| 91 |
clearTimeout(pending); |
| 92 |
$warning.prependTo(".body"); |
| 93 |
$overlay.remove(); |
| 94 |
}); |
| 95 |
}); |
| 96 |
</script> |