1/** 2 * Author and copyright: Stefan Haack (https://shaack.com) 3 * Repository: https://github.com/shaack/bootstrap-cookie-banner 4 * License: MIT, see file 'LICENSE' 5 */ 6 7function BootstrapCookieConsentSettings(props) { 8 var modalId = "bcb-modal" 9 var self = this 10 var detailedSettingsShown = false 11 this.props = { 12 autoShowDialog: true, // disable autoShowModal on the privacy policy and legal notice pages, to make these pages readable 13 lang: navigator.language, // the language, in which the modal is shown 14 languages: ["en", "de"], // supported languages (in ./content/), defaults to first in array 15 contentURL: "./content/", // this URL must contain the dialogs content in the needed languages 16 cookieName: "cookie-consent-settings", // the name of the cookie in which the configuration is stored as JSON 17 cookieStorageDays: 365 // the duration the cookie configuration is stored on the client 18 } 19 for (var property in props) { 20 // noinspection JSUnfilteredForInLoop 21 this.props[property] = props[property] 22 } 23 this.lang = this.props.lang 24 if (this.lang.indexOf("-") !== -1) { 25 this.lang = this.lang.split("-")[0] 26 } 27 if (!this.props.languages.includes(this.lang)) { 28 this.lang = this.props.languages[0] // fallback 29 } 30 console.log("bootstrap-cookie-consent-settings,", "settings language: " + this.lang) 31 var Cookie = { 32 set: function (name, value, days) { 33 var expires = "" 34 if (days) { 35 var date = new Date() 36 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)) 37 expires = "; expires=" + date.toUTCString() 38 } 39 document.cookie = name + "=" + (value || "") + expires + "; Path=/; SameSite=Strict;" 40 }, 41 get: function (name) { 42 var nameEQ = name + "=" 43 var ca = document.cookie.split(';') 44 for (var i = 0; i < ca.length; i++) { 45 var c = ca[i] 46 while (c.charAt(0) === ' ') { 47 c = c.substring(1, c.length) 48 } 49 if (c.indexOf(nameEQ) === 0) { 50 return c.substring(nameEQ.length, c.length) 51 } 52 } 53 return undefined 54 }, 55 remove: function (name) { 56 document.cookie = name + '=; Path=/; SameSite=Strict; Expires=Thu, 01 Jan 1970 00:00:01 GMT;' 57 } 58 } 59 var Util = { 60 documentReady: function (fn) { 61 if (document.readyState !== 'loading') { 62 fn() 63 } else { 64 document.addEventListener('DOMContentLoaded', fn) 65 } 66 } 67 } 68 69 function showDialog() { 70 Util.documentReady(function () { 71 this.modal = document.getElementById(modalId) 72 if (!self.modal) { 73 self.modal = document.createElement("div") 74 self.modal.id = modalId 75 self.modal.setAttribute("class", "modal fade") 76 self.modal.setAttribute("tabindex", "-1") 77 self.modal.setAttribute("role", "dialog") 78 self.modal.setAttribute("aria-labelledby", modalId) 79 document.body.append(self.modal) 80 self.$modal = $(self.modal) 81 // load content 82 var templateUrl = self.props.contentURL + self.lang + ".html" 83 $.get(templateUrl) 84 .done(function (data) { 85 self.modal.innerHTML = data 86 $(self.modal).modal({ 87 backdrop: "static", 88 keyboard: false 89 }) 90 self.$buttonDoNotAgree = $("#bcb-buttonDoNotAgree") 91 self.$buttonAgree = $("#bcb-buttonAgree") 92 self.$buttonSave = $("#bcb-buttonSave") 93 self.$buttonAgreeAll = $("#bcb-buttonAgreeAll") 94 updateButtons() 95 updateOptionsFromCookie() 96 $("#bcb-options").on("hide.bs.collapse", function () { 97 detailedSettingsShown = false 98 updateButtons() 99 }).on("show.bs.collapse", function () { 100 detailedSettingsShown = true 101 updateButtons() 102 }) 103 self.$buttonDoNotAgree.click(function () { 104 doNotAgree() 105 }) 106 self.$buttonAgree.click(function () { 107 agreeAll() 108 }) 109 self.$buttonSave.click(function () { 110 saveSettings() 111 }) 112 self.$buttonAgreeAll.click(function () { 113 agreeAll() 114 }) 115 }) 116 .fail(function (data) { 117 console.error("bootstrap-cookie-banner, request of \"" + templateUrl + "\" failed, statusCode: " + data.status) 118 console.error("see documentation at https://github.com/shaack/bootstrap-cookie-banner") 119 }) 120 } else { 121 self.$modal.modal("show") 122 } 123 }.bind(this)) 124 } 125 126 function updateOptionsFromCookie() { 127 var settings = self.getSettings() 128 if (settings) { 129 for (var setting in settings) { 130 var $checkbox = self.$modal.find("#bcb-options .bcb-option[data-name='" + setting + "'] input[type='checkbox']") 131 // noinspection JSUnfilteredForInLoop 132 $checkbox.prop("checked", settings[setting]) 133 } 134 } 135 } 136 137 function updateButtons() { 138 if (detailedSettingsShown) { 139 self.$buttonDoNotAgree.hide() 140 self.$buttonAgree.hide() 141 self.$buttonSave.show() 142 self.$buttonAgreeAll.show() 143 } else { 144 self.$buttonDoNotAgree.show() 145 self.$buttonAgree.show() 146 self.$buttonSave.hide() 147 self.$buttonAgreeAll.hide() 148 } 149 } 150 151 function gatherOptions(setAllExceptNecessary) { 152 var $options = self.$modal.find("#bcb-options .bcb-option") 153 var options = {} 154 for (var i = 0; i < $options.length; i++) { 155 var option = $options[i] 156 var name = option.getAttribute("data-name") 157 if (name === "necessary") { 158 options[name] = true 159 } else if (setAllExceptNecessary === undefined) { 160 var $checkbox = $(option).find("input[type='checkbox']") 161 options[name] = $checkbox.prop("checked") 162 } else { 163 options[name] = !!setAllExceptNecessary 164 } 165 } 166 return options 167 } 168 169 function agreeAll() { 170 Cookie.set(self.props.cookieName, JSON.stringify(gatherOptions(true)), self.props.cookieStorageDays) 171 self.$modal.modal("hide") 172 self.$modal.on("hidden.bs.modal", function () { 173 location.reload() 174 }) 175 } 176 177 function doNotAgree() { 178 Cookie.set(self.props.cookieName, JSON.stringify(gatherOptions(false)), self.props.cookieStorageDays) 179 self.$modal.modal("hide") 180 self.$modal.on("hidden.bs.modal", function () { 181 location.reload() 182 }) 183 } 184 185 function saveSettings() { 186 Cookie.set(self.props.cookieName, JSON.stringify(gatherOptions()), self.props.cookieStorageDays) 187 self.$modal.modal("hide") 188 self.$modal.on("hidden.bs.modal", function () { 189 location.reload() 190 }) 191 } 192 193 // init 194 195 if (Cookie.get(this.props.cookieName) === undefined && this.props.autoShowDialog) { 196 showDialog() 197 } 198 199 // API 200 201 this.showDialog = function () { 202 showDialog() 203 }
204 this.getSettings = function (optionName) { 205 var cookie = Cookie.get(self.props.cookieName) 206 if (cookie) { 207 var settings = JSON.parse(Cookie.get(self.props.cookieName)) 208 if (optionName === undefined) { 209 return settings 210 } else { 211 if (settings) { 212 return settings[optionName] 213 } else { 214 return false 215 } 216 } 217 } else { 218 return undefined 219 } 220 } 221}
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.