{"version":3,"file":"pfe-datetime.min.js","sources":["../_temp/pfe-datetime.js"],"sourcesContent":["/*!\n * PatternFly Elements: PfeDatetime 1.12.3\n * @license\n * Copyright 2021 Red Hat, Inc.\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n * \n*/\n\nimport PFElement from \"../../pfelement/dist/pfelement.js\";\n\nclass PfeDatetime extends PFElement {\n\n // Injected at build-time\n static get version() {\n return \"1.12.3\";\n }\n\n // Injected at build-time\n get html() {\n return `\n\n`;\n }\n\n static get tag() {\n return \"pfe-datetime\";\n }\n\n get styleUrl() {\n return \"pfe-datetime.scss\";\n }\n\n get templateUrl() {\n return \"pfe-datetime.html\";\n }\n\n get _dateTimeType() {\n return this.format || this.type || PfeDatetime.properties.format.default;\n }\n\n static get properties() {\n return {\n format: {\n title: \"Format\",\n type: String,\n values: [\"local\", \"relative\"],\n default: \"local\",\n },\n oldType: {\n alias: \"format\",\n attr: \"type\",\n },\n datetime: {\n title: \"Date and time\",\n type: String,\n observer: \"_datetimeChanged\",\n prefix: false,\n },\n timestamp: {\n title: \"Timestamp\",\n type: String,\n observer: \"_timestampChanged\",\n prefix: false,\n },\n };\n }\n\n constructor() {\n super(PfeDatetime);\n }\n\n _datetimeChanged(oldVal, newVal) {\n if (!Date.parse(newVal)) {\n return;\n }\n\n if (Date.parse(newVal) && this._datetime === Date.parse(newVal)) {\n return;\n }\n\n this.setDate(Date.parse(newVal));\n }\n\n _timestampChanged(oldVal, newVal) {\n if (this._timestamp === newVal) {\n this.log(\"early return\");\n return;\n }\n\n this._timestamp = newVal;\n this.setDate(new Date(newVal * 1000));\n }\n\n setDate(date) {\n this._datetime = date;\n this.shadowRoot.querySelector(\"span\").innerText = window.Intl ? this._getTypeString() : date.toLocaleString();\n }\n\n _getOptions() {\n const props = {\n weekday: {\n short: \"short\",\n long: \"long\",\n },\n day: {\n numeric: \"numeric\",\n \"2-digit\": \"2-digit\",\n },\n month: {\n short: \"short\",\n long: \"long\",\n },\n year: {\n numeric: \"numeric\",\n \"2-digit\": \"2-digit\",\n },\n hour: {\n numeric: \"numeric\",\n \"2-digit\": \"2-digit\",\n },\n minute: {\n numeric: \"numeric\",\n \"2-digit\": \"2-digit\",\n },\n second: {\n numeric: \"numeric\",\n \"2-digit\": \"2-digit\",\n },\n timeZoneName: {\n short: \"short\",\n long: \"long\",\n },\n };\n\n let options = {};\n\n for (const prop in props) {\n // converting the prop name from camel case to\n // hyphenated so it matches the attribute.\n // for example: timeZoneName to time-zone-name\n let attributeName = prop\n .replace(/[\\w]([A-Z])/g, (match) => {\n return match[0] + \"-\" + match[1];\n })\n .toLowerCase();\n\n const value = props[prop][this.getAttribute(attributeName)];\n if (value) {\n options[prop] = value;\n }\n }\n\n if (this.getAttribute(\"time-zone\")) {\n options.timeZone = this.getAttribute(\"time-zone\");\n }\n\n return options;\n }\n\n _getTypeString() {\n const options = this._getOptions();\n const locale = this.getAttribute(\"locale\") || navigator.language;\n let dt = \"\";\n switch (this._dateTimeType) {\n case \"local\":\n dt = new Intl.DateTimeFormat(locale, options).format(this._datetime);\n break;\n case \"relative\":\n dt = this._getTimeRelative(this._datetime - Date.now());\n break;\n default:\n dt = this._datetime;\n }\n return dt;\n }\n\n _getTimeRelative(ms) {\n const tense = ms > 0 ? \"until\" : \"ago\";\n let str = \"just now\";\n // Based off of Github Relative Time\n // https://github.com/github/time-elements/blob/master/src/relative-time.js\n const s = Math.round(Math.abs(ms) / 1000);\n const min = Math.round(s / 60);\n const h = Math.round(min / 60);\n const d = Math.round(h / 24);\n const m = Math.round(d / 30);\n const y = Math.round(m / 12);\n if (m >= 18) {\n str = y + \" years\";\n } else if (m >= 12) {\n str = \"a year\";\n } else if (d >= 45) {\n str = m + \" months\";\n } else if (d >= 30) {\n str = \"a month\";\n } else if (h >= 36) {\n str = d + \" days\";\n } else if (h >= 24) {\n str = \"a day\";\n } else if (min >= 90) {\n str = h + \" hours\";\n } else if (min >= 45) {\n str = \"an hour\";\n } else if (s >= 90) {\n str = min + \" minutes\";\n } else if (s >= 45) {\n str = \"a minute\";\n } else if (s >= 10) {\n str = s + \" seconds\";\n }\n return str !== \"just now\" ? `${str} ${tense}` : str;\n }\n}\n\nPFElement.create(PfeDatetime);\n\nexport default PfeDatetime;\n"],"names":["PfeDatetime","PFElement","version","html","tag","styleUrl","templateUrl","_dateTimeType","this","format","type","properties","default","title","String","values","oldType","alias","attr","datetime","observer","prefix","timestamp","[object Object]","super","oldVal","newVal","Date","parse","_datetime","setDate","_timestamp","log","date","shadowRoot","querySelector","innerText","window","Intl","_getTypeString","toLocaleString","props","weekday","short","long","day","numeric","2-digit","month","year","hour","minute","second","timeZoneName","options","prop","attributeName","replace","match","toLowerCase","value","getAttribute","timeZone","_getOptions","locale","navigator","language","dt","DateTimeFormat","_getTimeRelative","now","ms","tense","str","s","Math","round","abs","min","h","d","m","y","create"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;EA2BA,MAAMA,UAAoBC,EAGxBC,qBACE,MAAO,SAITC,WACE,MAAO,yGAKTC,iBACE,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