Settings.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, watch } from "vue";
  3. import Toast from "toasters";
  4. import { storeToRefs } from "pinia";
  5. import validation from "@/validation";
  6. import { useWebsocketsStore } from "@/stores/websockets";
  7. import { useManageStationStore } from "@/stores/manageStation";
  8. import { useForm } from "@/composables/useForm";
  9. const InfoIcon = defineAsyncComponent(
  10. () => import("@/components/InfoIcon.vue")
  11. );
  12. const props = defineProps({
  13. modalUuid: { type: String, required: true }
  14. });
  15. const { socket } = useWebsocketsStore();
  16. const manageStationStore = useManageStationStore({
  17. modalUuid: props.modalUuid
  18. });
  19. const { station } = storeToRefs(manageStationStore);
  20. const { editStation } = manageStationStore;
  21. const { inputs, save, setOriginalValue } = useForm(
  22. {
  23. name: {
  24. value: station.value.name,
  25. validate: value => {
  26. if (!validation.isLength(value, 2, 16))
  27. return "Name must have between 2 and 16 characters.";
  28. if (!validation.regex.az09_.test(value))
  29. return "Invalid name format. Allowed characters: a-z, 0-9 and _.";
  30. return true;
  31. }
  32. },
  33. displayName: {
  34. value: station.value.displayName,
  35. validate: value => {
  36. if (!validation.isLength(value, 2, 32))
  37. return "Display name must have between 2 and 32 characters.";
  38. if (!validation.regex.ascii.test(value))
  39. return "Invalid display name format. Only ASCII characters are allowed.";
  40. return true;
  41. }
  42. },
  43. description: {
  44. value: station.value.description,
  45. validate: value => {
  46. if (
  47. value
  48. .split("")
  49. .filter(character => character.charCodeAt(0) === 21328)
  50. .length !== 0
  51. )
  52. return "Invalid description format.";
  53. return true;
  54. }
  55. },
  56. theme: station.value.theme,
  57. privacy: station.value.privacy,
  58. skipVoteThreshold: station.value.skipVoteThreshold,
  59. requestsEnabled: station.value.requests.enabled,
  60. requestsAccess: station.value.requests.access,
  61. requestsLimit: station.value.requests.limit,
  62. autofillEnabled: station.value.autofill.enabled,
  63. autofillLimit: station.value.autofill.limit,
  64. autofillMode: station.value.autofill.mode
  65. },
  66. ({ status, messages, values }, resolve, reject) => {
  67. if (status === "success") {
  68. const oldStation = JSON.parse(JSON.stringify(station.value));
  69. const updatedStation = {
  70. ...oldStation,
  71. name: values.name,
  72. displayName: values.displayName,
  73. description: values.description,
  74. theme: values.theme,
  75. privacy: values.privacy,
  76. skipVoteThreshold: values.skipVoteThreshold,
  77. requests: {
  78. ...oldStation.requests,
  79. enabled: values.requestsEnabled,
  80. access: values.requestsAccess,
  81. limit: values.requestsLimit
  82. },
  83. autofill: {
  84. ...oldStation.autofill,
  85. enabled: values.autofillEnabled,
  86. limit: values.autofillLimit,
  87. mode: values.autofillMode
  88. }
  89. };
  90. socket.dispatch(
  91. "stations.update",
  92. station.value._id,
  93. updatedStation,
  94. res => {
  95. new Toast(res.message);
  96. if (res.status === "success") {
  97. editStation(updatedStation);
  98. resolve();
  99. } else reject(new Error(res.message));
  100. }
  101. );
  102. } else {
  103. Object.values(messages).forEach(message => {
  104. new Toast({ content: message, timeout: 8000 });
  105. });
  106. resolve();
  107. }
  108. },
  109. {
  110. modalUuid: props.modalUuid
  111. }
  112. );
  113. watch(station, value => {
  114. setOriginalValue({
  115. name: value.name,
  116. displayName: value.displayName,
  117. description: value.description,
  118. theme: value.theme,
  119. privacy: value.privacy,
  120. skipVoteThreshold: value.skipVoteThreshold,
  121. requestsEnabled: value.requests.enabled,
  122. requestsAccess: value.requests.access,
  123. requestsLimit: value.requests.limit,
  124. autofillEnabled: value.autofill.enabled,
  125. autofillLimit: value.autofill.limit,
  126. autofillMode: value.autofill.mode
  127. });
  128. });
  129. </script>
  130. <template>
  131. <div class="station-settings">
  132. <label class="label">Name</label>
  133. <div class="control is-expanded">
  134. <input class="input" type="text" v-model="inputs['name'].value" />
  135. </div>
  136. <label class="label">Display Name</label>
  137. <div class="control is-expanded">
  138. <input
  139. class="input"
  140. type="text"
  141. v-model="inputs['displayName'].value"
  142. />
  143. </div>
  144. <label class="label">Description</label>
  145. <div class="control is-expanded">
  146. <input
  147. class="input"
  148. type="text"
  149. v-model="inputs['description'].value"
  150. />
  151. </div>
  152. <div class="settings-buttons">
  153. <div class="small-section">
  154. <label class="label">Theme</label>
  155. <div class="control is-expanded select">
  156. <select v-model="inputs['theme'].value">
  157. <option value="blue" selected>Blue</option>
  158. <option value="purple">Purple</option>
  159. <option value="teal">Teal</option>
  160. <option value="orange">Orange</option>
  161. <option value="red">Red</option>
  162. </select>
  163. </div>
  164. </div>
  165. <div class="small-section">
  166. <label class="label">Privacy</label>
  167. <div class="control is-expanded select">
  168. <select v-model="inputs['privacy'].value">
  169. <option value="public">Public</option>
  170. <option value="unlisted">Unlisted</option>
  171. <option value="private" selected>Private</option>
  172. </select>
  173. </div>
  174. </div>
  175. <div class="small-section">
  176. <label class="label">
  177. Skip Vote Threshold
  178. <info-icon
  179. tooltip="The % of logged-in station users required to vote to skip a song"
  180. />
  181. </label>
  182. <div class="control is-expanded input-slider">
  183. <input
  184. v-model="inputs['skipVoteThreshold'].value"
  185. type="range"
  186. min="0"
  187. max="100"
  188. />
  189. <span>{{ inputs["skipVoteThreshold"].value }}%</span>
  190. </div>
  191. </div>
  192. <div
  193. class="requests-settings"
  194. :class="{ enabled: inputs['requestsEnabled'].value }"
  195. >
  196. <div class="toggle-row">
  197. <label class="label">
  198. Requests
  199. <info-icon
  200. tooltip="Allow users to add songs to the queue"
  201. />
  202. </label>
  203. <p class="is-expanded checkbox-control">
  204. <label class="switch">
  205. <input
  206. type="checkbox"
  207. id="toggle-requests"
  208. v-model="inputs['requestsEnabled'].value"
  209. />
  210. <span class="slider round"></span>
  211. </label>
  212. <label for="toggle-requests">
  213. <p>
  214. {{
  215. inputs["requestsEnabled"].value
  216. ? "Enabled"
  217. : "Disabled"
  218. }}
  219. </p>
  220. </label>
  221. </p>
  222. </div>
  223. <div
  224. v-if="inputs['requestsEnabled'].value"
  225. class="small-section"
  226. >
  227. <label class="label">Minimum access</label>
  228. <div class="control is-expanded select">
  229. <select v-model="inputs['requestsAccess'].value">
  230. <option value="owner" selected>Owner</option>
  231. <option value="user">User</option>
  232. </select>
  233. </div>
  234. </div>
  235. <div
  236. v-if="inputs['requestsEnabled'].value"
  237. class="small-section"
  238. >
  239. <label class="label">Per user request limit</label>
  240. <div class="control is-expanded">
  241. <input
  242. class="input"
  243. type="number"
  244. min="1"
  245. max="50"
  246. v-model="inputs['requestsLimit'].value"
  247. />
  248. </div>
  249. </div>
  250. </div>
  251. <div
  252. class="autofill-settings"
  253. :class="{ enabled: inputs['autofillEnabled'].value }"
  254. >
  255. <div class="toggle-row">
  256. <label class="label">
  257. Autofill
  258. <info-icon
  259. tooltip="Automatically fill the queue with songs"
  260. />
  261. </label>
  262. <p class="is-expanded checkbox-control">
  263. <label class="switch">
  264. <input
  265. type="checkbox"
  266. id="toggle-autofill"
  267. v-model="inputs['autofillEnabled'].value"
  268. />
  269. <span class="slider round"></span>
  270. </label>
  271. <label for="toggle-autofill">
  272. <p>
  273. {{
  274. inputs["autofillEnabled"].value
  275. ? "Enabled"
  276. : "Disabled"
  277. }}
  278. </p>
  279. </label>
  280. </p>
  281. </div>
  282. <div
  283. v-if="inputs['autofillEnabled'].value"
  284. class="small-section"
  285. >
  286. <label class="label">Song limit</label>
  287. <div class="control is-expanded">
  288. <input
  289. class="input"
  290. type="number"
  291. min="1"
  292. max="50"
  293. v-model="inputs['autofillLimit'].value"
  294. />
  295. </div>
  296. </div>
  297. <div
  298. v-if="inputs['autofillEnabled'].value"
  299. class="small-section"
  300. >
  301. <label class="label">Play mode</label>
  302. <div class="control is-expanded select">
  303. <select v-model="inputs['autofillMode'].value">
  304. <option value="random" selected>Random</option>
  305. <option value="sequential">Sequential</option>
  306. </select>
  307. </div>
  308. </div>
  309. </div>
  310. </div>
  311. <button class="control is-expanded button is-primary" @click="save()">
  312. Save Changes
  313. </button>
  314. </div>
  315. </template>
  316. <style lang="less" scoped>
  317. .night-mode {
  318. .requests-settings,
  319. .autofill-settings {
  320. background-color: var(--dark-grey-2) !important;
  321. }
  322. }
  323. .station-settings {
  324. .settings-buttons {
  325. display: flex;
  326. justify-content: center;
  327. flex-wrap: wrap;
  328. .small-section {
  329. width: calc(50% - 10px);
  330. min-width: 150px;
  331. margin: 5px auto;
  332. &:nth-child(odd) {
  333. margin-left: 0;
  334. }
  335. &:nth-child(even) {
  336. margin-right: 0;
  337. }
  338. }
  339. }
  340. .input-slider {
  341. display: flex;
  342. input[type="range"] {
  343. -webkit-appearance: none;
  344. margin: 0;
  345. padding: 0;
  346. width: 100%;
  347. min-width: 100px;
  348. background: transparent;
  349. }
  350. input[type="range"]:focus {
  351. outline: none;
  352. }
  353. input[type="range"]::-webkit-slider-runnable-track {
  354. width: 100%;
  355. height: 5.2px;
  356. cursor: pointer;
  357. box-shadow: 0;
  358. background: var(--light-grey-3);
  359. border-radius: @border-radius;
  360. border: 0;
  361. }
  362. input[type="range"]::-webkit-slider-thumb {
  363. box-shadow: 0;
  364. border: 0;
  365. height: 19px;
  366. width: 19px;
  367. border-radius: 100%;
  368. background: var(--primary-color);
  369. cursor: pointer;
  370. -webkit-appearance: none;
  371. margin-top: -6.5px;
  372. }
  373. input[type="range"]::-moz-range-track {
  374. width: 100%;
  375. height: 5.2px;
  376. cursor: pointer;
  377. box-shadow: 0;
  378. background: var(--light-grey-3);
  379. border-radius: @border-radius;
  380. border: 0;
  381. }
  382. input[type="range"]::-moz-range-thumb {
  383. box-shadow: 0;
  384. border: 0;
  385. height: 19px;
  386. width: 19px;
  387. border-radius: 100%;
  388. background: var(--primary-color);
  389. cursor: pointer;
  390. -webkit-appearance: none;
  391. margin-top: -6.5px;
  392. }
  393. input[type="range"]::-ms-track {
  394. width: 100%;
  395. height: 5.2px;
  396. cursor: pointer;
  397. box-shadow: 0;
  398. background: var(--light-grey-3);
  399. border-radius: @border-radius;
  400. }
  401. input[type="range"]::-ms-fill-lower {
  402. background: var(--light-grey-3);
  403. border: 0;
  404. border-radius: 0;
  405. box-shadow: 0;
  406. }
  407. input[type="range"]::-ms-fill-upper {
  408. background: var(--light-grey-3);
  409. border: 0;
  410. border-radius: 0;
  411. box-shadow: 0;
  412. }
  413. input[type="range"]::-ms-thumb {
  414. box-shadow: 0;
  415. border: 0;
  416. height: 15px;
  417. width: 15px;
  418. border-radius: 100%;
  419. background: var(--primary-color);
  420. cursor: pointer;
  421. -webkit-appearance: none;
  422. margin-top: 1.5px;
  423. }
  424. & > span {
  425. min-width: 40px;
  426. margin-left: 10px;
  427. text-align: center;
  428. }
  429. }
  430. .requests-settings,
  431. .autofill-settings {
  432. display: flex;
  433. flex-wrap: wrap;
  434. width: 100%;
  435. margin: 10px 0;
  436. padding: 10px;
  437. border-radius: @border-radius;
  438. box-shadow: @box-shadow;
  439. .toggle-row {
  440. display: flex;
  441. width: 100%;
  442. line-height: 36px;
  443. .label {
  444. font-size: 18px;
  445. margin: 0;
  446. }
  447. }
  448. .label {
  449. display: flex;
  450. flex-grow: 1;
  451. }
  452. .checkbox-control {
  453. justify-content: end;
  454. }
  455. .small-section {
  456. &:nth-child(even) {
  457. margin-left: 0;
  458. margin-right: auto;
  459. }
  460. &:nth-child(odd) {
  461. margin-left: auto;
  462. margin-right: 0;
  463. }
  464. }
  465. }
  466. }
  467. </style>