1/** 2 * Open tickets badge Template Script 3 * ------------------------------------------------------------------------------ 4 * Displays a number of open tickets 5 * 6 */ 7 8const OpenTicketsBadge = (function () { 9 const LOCAL_STORAGE_KEY = 'lt:open_tickets'; 10 const TTL = 60; // Time to live in seconds. 11 const elements = document.querySelectorAll('.js-open-tickets-badge'); 12 13 function getDefaultRequestConfig() { 14 return JSON.parse( 15 JSON.stringify({ 16 credentials: 'same-origin', 17 headers: { 18 'X-Requested-With': 'XMLHttpRequest', 19 'Content-Type': 'application/json;', 20 }, 21 }), 22 ); 23 } 24 25 function fetchJSON(url, config) { 26 return fetch(url, config).then((response) => { 27 if (!response.ok) { 28 throw response; 29 } 30 return response.json(); 31 }); 32 } 33 34 function getTickets() { 35 return fetchJSON('/api/v2/requests.json?status=new,open', getDefaultRequestConfig()); 36 } 37 38 function getCount() { 39 return getTickets().then((response) => response.count); 40 } 41 42 /** 43 * @param {string} key - A key to identify the value. 44 * @param {any} value - A value associated with the key. 45 * @param {number} ttl - Time to live in seconds. 46 */ 47 function setWithExpiry(key, value, ttl) { 48 // `item` is an object which contains the original value 49 // as well as the time when it's supposed to expire 50 const item = { 51 value, 52 ttl: Date.now() + ttl * 1000, 53 }; 54 localStorage.setItem(key, JSON.stringify(item)); 55 } 56 57 /** 58 * @param {string} key - A key to identify the data. 59 * @returns {any|null} returns the value associated with the key if its exists and is not expired. 60 */ 61 function getWithExpiry(key) { 62 const itemStr = localStorage.getItem(key); 63 // if the item doesn't exist, return null 64 if (!itemStr) { 65 return null; 66 } 67 const item = JSON.parse(itemStr); 68 // compare the expiry time of the item with the current time 69 if (Date.now() > item.ttl) { 70 // If the item is expired, delete the item from storage 71 // and return null 72 localStorage.removeItem(key); 73 return null; 74 } 75 return item.value; 76 } 77 78 /** 79 * @param {number} count - A number of opened tickets. 80 */ 81 function render(count) {
82 [...elements].forEach((element) => { 83 if (elements.length && count > 0) { 84 const currentEl = element; 85 currentEl.innerText = count; 86 currentEl.classList.add('is-active'); 87 } 88 }); 89 if (count > 0) { 90 const { title } = document; 91 document.title = `(${count}) ${title}`; 92 } 93 } 94 95 return { 96 init() { 97 const storedCount = getWithExpiry(LOCAL_STORAGE_KEY); 98 if (window.localStorage && storedCount) { 99 render(storedCount); 100 } else { 101 getCount().then((count) => { 102 render(count); 103 if (window.localStorage) { 104 setWithExpiry(LOCAL_STORAGE_KEY, count, TTL); 105 } 106 }); 107 } 108 }, 109 }; 110}()); 111 112if (LotusConfig.signedIn) { 113 OpenTicketsBadge.init(); 114}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.