3 * Used to toggle the AdvAgg Bypass Cookie client side.
6 /* global Drupal:false */
9 * Test to see if the given string contains unicode.
13 * @param int granularity
15 * @param string langcode
16 * Language used in translation.
19 * true if string contains non ASCII characters.
20 * false if string only contains ASCII characters.
22 Drupal.formatInterval = function (interval, granularity, langcode) {
24 granularity = typeof granularity !== 'undefined' ? granularity : 2;
27 while (granularity > 0) {
29 if (interval >= 31536000) {
31 output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 year', '@count years');
33 else if (interval >= 2592000) {
35 output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 month', '@count months');
37 else if (interval >= 604800) {
39 output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 week', '@count weeks');
41 else if (interval >= 86400) {
43 output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 day', '@count days');
45 else if (interval >= 3600) {
47 output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 hour', '@count hours');
49 else if (interval >= 60) {
51 output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 min', '@count min');
53 else if (interval >= 1) {
55 output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 sec', '@count sec');
62 return output.length ? output : Drupal.t('0 sec', {});
66 * Test to see if the given string contains unicode.
72 * true if string contains non ASCII characters.
73 * false if string only contains ASCII characters.
75 function advagg_is_unicode(str) {
77 for (var i = 0, n = str.length; i < n; i++) {
78 if (str.charCodeAt(i) > 255) {
86 * Toggle the advagg cookie.
89 * true if hostname contains unicode.
90 * false so the form does not get submitted.
92 function advagg_toggle_cookie() {
94 // Fallback to submitting the form for Unicode domains like ".рф".
95 if (advagg_is_unicode(document.location.hostname)) {
99 var cookie_name = 'AdvAggDisabled';
101 // See if the cookie exists.
102 var cookie_pos = document.cookie.indexOf(cookie_name + '=' + drupalSettings.advagg.key);
104 // If the cookie does exist then remove it.
105 if (cookie_pos !== -1) {
106 document.cookie = cookie_name + '=;'
107 + 'expires=Thu, 01 Jan 1970 00:00:00 GMT;'
108 + ' path=' + drupalSettings.path.baseUrl + ';'
109 + ' domain=.' + document.location.hostname + ';';
110 alert(Drupal.t('AdvAgg Bypass Cookie Removed'));
112 // If the cookie does not exist then set it.
114 var bypass_length = document.getElementById('edit-timespan').value, expire_date = new Date(new Date().getTime() + bypass_length * 1000);
116 document.cookie = cookie_name + '=' + drupalSettings.advagg.key + ';'
117 + ' expires=' + expire_date.toGMTString() + ';'
118 + ' path=' + drupalSettings.path.baseUrl + ';'
119 + ' domain=.' + document.location.hostname + ';';
120 alert(Drupal.t('AdvAgg Bypass Cookie Set for @time.', {'@time' : Drupal.formatInterval(bypass_length)}));
123 // Must return false, if returning true then form gets submitted.