{ "version": 3, "sources": ["countdown_controller-f25fd770.js"], "sourcesContent": ["// Borrowed from here:\n// https://github.com/gorails-screencasts/423-stimulus-values-api-defaults/blob/master/app/javascript/controllers/countdown_controller.js\n\nimport { Controller } from \"@hotwired/stimulus\"\n\nexport default class extends Controller {\n static classes = [ \"hidden\", \"warning\", \"expired\" ]\n static targets = [ \"hideWhenExpired\", \"countdown\" ]\n \n static values = {\n date: String,\n warningInterval: { type: Number, default: 60 },\n refreshInterval: { type: Number, default: 1000 },\n expiredMessage: { type: String, default: \"Ending now...\" },\n message: { type: String, default: \"${minutes}m ${seconds}s\" }\n }\n\n connect() {\n this.warningClassApplied = false\n \n if (this.hasDateValue) {\n this.endTime = new Date(this.dateValue).getTime()\n\n this.update()\n this.timer = setInterval(() => {\n this.update()\n }, this.refreshIntervalValue)\n } else {\n console.error(\"Missing data-countdown-date-value attribute\", this.element)\n }\n }\n\n disconnect() {\n this.stopTimer()\n }\n\n stopTimer() {\n if (this.timer) {\n clearInterval(this.timer)\n }\n }\n\n update() {\n const difference = this.timeDifference()\n \n if (this.hasWarningClass && (difference <= this.warningIntervalValue*1000) && (difference > 0) && !this.warningClassApplied) {\n this.element.classList.add(this.warningClass)\n this.warningClassApplied = true\n }\n\n if (difference < 0) {\n if (this.warningClassApplied) {\n this.element.classList.remove(this.warningClass)\n this.warningClassApplied = false\n }\n \n if (this.hasExpiredClass) {\n this.element.classList.add(this.expiredClass)\n }\n \n this.countdownTarget.textContent = this.expiredMessageValue\n this.stopTimer()\n \n if (this.hasHideWhenExpiredTarget) {\n this.hideHideTargets()\n }\n \n return\n }\n\n const days = Math.floor(difference / (1000 * 60 * 60 * 24))\n const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));\n const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));\n const seconds = Math.floor((difference % (1000 * 60)) / 1000);\n\n this.countdownTarget.textContent = this.messageValue\n .replace(\"${days}\", days)\n .replace(\"${hours}\", hours)\n .replace(\"${minutes}\", minutes)\n .replace(\"${seconds}\", seconds)\n }\n\n timeDifference() {\n return this.endTime - new Date().getTime()\n }\n \n hideHideTargets() {\n this.hideWhenExpiredTargets.forEach((hideTarget) => {\n hideTarget.classList.add(this.hiddenClass)\n })\n }\n}"], "mappings": "wKAGA,gDAEA,qBAAO,SAAsB,EAAW,CAYtC,SAAU,CACR,KAAK,oBAAsB,GAE3B,AAAI,KAAK,aACP,MAAK,QAAU,GAAI,MAAK,KAAK,SAAS,EAAE,QAAQ,EAEhD,KAAK,OAAO,EACZ,KAAK,MAAQ,YAAY,IAAM,CAC7B,KAAK,OAAO,CACd,EAAG,KAAK,oBAAoB,GAE5B,QAAQ,MAAM,8CAA+C,KAAK,OAAO,CAE7E,CAEA,YAAa,CACX,KAAK,UAAU,CACjB,CAEA,WAAY,CACV,AAAI,KAAK,OACP,cAAc,KAAK,KAAK,CAE5B,CAEA,QAAS,CACP,KAAM,GAAa,KAAK,eAAe,EAOvC,GALI,KAAK,iBAAoB,GAAc,KAAK,qBAAqB,KAAU,EAAa,GAAM,CAAC,KAAK,qBACtG,MAAK,QAAQ,UAAU,IAAI,KAAK,YAAY,EAC5C,KAAK,oBAAsB,IAGzB,EAAa,EAAG,CAClB,AAAI,KAAK,qBACP,MAAK,QAAQ,UAAU,OAAO,KAAK,YAAY,EAC/C,KAAK,oBAAsB,IAGzB,KAAK,iBACP,KAAK,QAAQ,UAAU,IAAI,KAAK,YAAY,EAG9C,KAAK,gBAAgB,YAAc,KAAK,oBACxC,KAAK,UAAU,EAEX,KAAK,0BACP,KAAK,gBAAgB,EAGvB,MACF,CAEA,KAAM,GAAO,KAAK,MAAM,EAAc,KAAO,GAAK,GAAK,GAAG,EACpD,EAAQ,KAAK,MAAO,EAAc,KAAO,GAAK,GAAK,IAAQ,KAAO,GAAK,GAAG,EAC1E,EAAU,KAAK,MAAO,EAAc,KAAO,GAAK,IAAQ,KAAO,GAAG,EAClE,EAAU,KAAK,MAAO,EAAc,KAA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