{"version":3,"file":"main.CPLu21.js","sources":["theme-164/virtual:theme","theme-164/discourse/components/poll-banner.gjs","theme-164/discourse/connectors/above-main-container/poll-banner-connector.gjs","theme-164/virtual:entrypoint:main"],"sourcesContent":["import { getObjectForTheme } from \"discourse/lib/theme-settings-store\";\n\nexport const settings = getObjectForTheme(164);\n\nexport function themePrefix(key) {\n  return `theme_translations.164.${key}`;\n}\n","import Component from \"@glimmer/component\";\nimport { tracked } from \"@glimmer/tracking\";\nimport { on } from \"@ember/modifier\";\nimport { action } from \"@ember/object\";\nimport didInsert from \"@ember/render-modifiers/modifiers/did-insert\";\nimport { service } from \"@ember/service\";\nimport { htmlSafe } from \"@ember/template\";\nimport DButton from \"discourse/components/d-button\";\nimport { ajax } from \"discourse/lib/ajax\";\nimport { popupAjaxError } from \"discourse/lib/ajax-error\";\nimport KeyValueStore from \"discourse/lib/key-value-store\";\n\nexport default class PollBanner extends Component {\n  @service currentUser;\n\n  @tracked cooked;\n  @tracked postId;\n  @tracked isVisible = false;\n\n  pollStore = new KeyValueStore(\"poll-banner_\");\n\n  get shouldShowPoll() {\n    const topicId = settings.topic_id;\n    return (\n      topicId &&\n      topicId !== 0 &&\n      this.currentUser &&\n      !this.pollStore.get(`${topicId}_closed`)\n    );\n  }\n\n  getCachedPollData() {\n    return this.pollStore.getObject(String(settings.topic_id));\n  }\n\n  cachePollData(cooked, postId) {\n    const pollData = {\n      cooked,\n      postId,\n      timestamp: Date.now(),\n    };\n    this.pollStore.setObject({\n      key: String(settings.topic_id),\n      value: pollData,\n    });\n    return pollData;\n  }\n\n  async loadPollFromTopic() {\n    const topicId = settings.topic_id;\n    try {\n      const response = await ajax(`/t/${topicId}.json`);\n      const firstPost = response.post_stream.posts[0];\n      const pollData = this.cachePollData(firstPost.cooked, firstPost.id);\n      this.cooked = pollData.cooked;\n      this.postId = pollData.postId;\n    } catch (error) {\n      this.isVisible = false;\n      popupAjaxError(error);\n    }\n  }\n\n  loadPollFromCache(cachedData) {\n    this.cooked = cachedData.cooked;\n    this.postId = cachedData.postId;\n    return Date.now() - cachedData.timestamp;\n  }\n\n  shouldDisplayBanner(timeElapsed) {\n    const showTime = 60000 * settings.show_after;\n    const stopTime = 60000 * settings.stop_after;\n    return timeElapsed >= showTime && timeElapsed <= stopTime;\n  }\n\n  @action\n  async setupPoll() {\n    if (!this.shouldShowPoll) {\n      return;\n    }\n\n    const cachedData = this.getCachedPollData();\n    let timeElapsed = 0;\n\n    if (cachedData) {\n      timeElapsed = this.loadPollFromCache(cachedData);\n    } else {\n      await this.loadPollFromTopic();\n    }\n\n    this.isVisible = this.shouldDisplayBanner(timeElapsed);\n  }\n\n  @action\n  handleVote(event) {\n    const voteOptionId = event.target.getAttribute(\"data-poll-option-id\");\n\n    if (!voteOptionId) {\n      return;\n    }\n\n    const pollContainer = event.currentTarget;\n    pollContainer\n      .querySelectorAll(\"[data-poll-option-id]\")\n      .forEach((option) => option.classList.remove(\"selected-vote\"));\n    event.target.classList.add(\"selected-vote\");\n\n    ajax(\"/polls/vote\", {\n      type: \"PUT\",\n      data: {\n        post_id: this.postId,\n        poll_name: settings.poll_name,\n        options: [voteOptionId],\n      },\n    })\n      .then((result) => {\n        if (result.vote[0].length > 0) {\n          setTimeout(() => {\n            this.closePoll();\n          }, 1000);\n        }\n      })\n      .catch(popupAjaxError);\n  }\n\n  @action\n  closePoll() {\n    this.pollStore.set({\n      key: `${settings.topic_id}_closed`,\n      value: \"true\",\n    });\n    this.isVisible = false;\n  }\n\n  <template>\n    <div\n      class=\"above-main-container-outlet poll-banner-connector\n        {{if this.isVisible 'visible-poll'}}\"\n      {{didInsert this.setupPoll}}\n    >\n      {{! template-lint-disable no-invalid-interactive }}\n      <div\n        class=\"poll-banner poll-banner-content\"\n        {{on \"click\" this.handleVote}}\n      >\n        {{htmlSafe this.cooked}}\n        <DButton\n          class=\"btn-flat\"\n          @action={{this.closePoll}}\n          @icon=\"xmark\"\n          @label=\"share.close\"\n        />\n        <div class=\"poll-banner-key\">{{settings.poll_key}}</div>\n      </div>\n    </div>\n  </template>\n}\n","import Component from \"@glimmer/component\";\nimport { service } from \"@ember/service\";\nimport PollBanner from \"../../components/poll-banner\";\n\nexport default class PollBannerConnector extends Component {\n  @service currentUser;\n\n  <template>\n    {{#if this.currentUser}}\n      <PollBanner />\n    {{/if}}\n  </template>\n}\n","import * as Mod1 from \"./discourse/components/poll-banner\";\nimport * as Mod2 from \"./discourse/connectors/above-main-container/poll-banner-connector.gjs\";\nconst compatModules = {\n  \"discourse/components/poll-banner\": Mod1,\n  \"discourse/connectors/above-main-container/poll-banner-connector\": Mod2,\n};\nexport default compatModules;\n"],"names":["settings","getObjectForTheme","PollBanner","Component","dt7948","g","this","prototype","service","currentUser","i","tracked","cooked","postId","isVisible","pollStore","KeyValueStore","shouldShowPoll","topicId","topic_id","get","getCachedPollData","getObject","String","cachePollData","pollData","timestamp","Date","now","setObject","key","value","loadPollFromTopic","firstPost","ajax","post_stream","posts","id","error","popupAjaxError","loadPollFromCache","cachedData","shouldDisplayBanner","timeElapsed","showTime","show_after","stopTime","stop_after","setupPoll","n","action","handleVote","event","voteOptionId","target","getAttribute","currentTarget","querySelectorAll","forEach","option","classList","remove","add","type","data","post_id","poll_name","options","then","result","vote","length","setTimeout","closePoll","catch","set","setComponentTemplate","createTemplateFactory","block","moduleName","scope","didInsert","on","htmlSafe","DButton","isStrictMode","PollBannerConnector","compatModules","Mod1"],"mappings":"w7BAEaA,EAAWC,EAAkB;ACU3B,MAAMC,UAAmBC,SAAAC,OAAAC,EAAAC,KAAAC,yBACrCC,GAAA,CAAAC,QAAAL,OAAAM,EAAAJ,KAAA;OAAAF,OAAAC,EAAAC,KAAAC,oBAEAI,GAAA,CAAAC,QAAAR,OAAAM,EAAAJ,KAAA;OAAAF,OAAAC,EAAAC,KAAAC,oBACAI,GAAA,CAAAE,QAAAT,OAAAM,EAAAJ,KAAA;OAAAF,OAAAC,EAAAC,KAAAC,uBACAI,GAAA,WAAA,OAAoB,CAAA,EAAA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