|
| 1 | +import Script, { ERROR_LEVEL } from '@core/script.js'; |
| 2 | +import Popup from '@components/popup.js'; |
| 3 | +import { CreateElement } from '@utils/helpers.js'; |
| 4 | + |
| 5 | +export default class LabelPopup { |
| 6 | + #casket; |
| 7 | + #inventory; |
| 8 | + |
| 9 | + constructor(casket, inventory) { |
| 10 | + this.#casket = casket; |
| 11 | + this.#inventory = inventory; |
| 12 | + } |
| 13 | + |
| 14 | + Show() { |
| 15 | + let prompt; |
| 16 | + let placeholder; |
| 17 | + |
| 18 | + if (this.#casket.attributes["custom name attr"]) { |
| 19 | + prompt = `Enter a new descriptive label for this Storage Unit.`; |
| 20 | + placeholder = this.#casket.attributes["custom name attr"]; |
| 21 | + } else { |
| 22 | + prompt = "Enter a descriptive label for your personal Storage Unit and start using it for storing items."; |
| 23 | + placeholder = "New name" |
| 24 | + } |
| 25 | + |
| 26 | + const renameForm = CreateElement("form", { |
| 27 | + class: "cs2s_settings_form", |
| 28 | + html: /*html*/` |
| 29 | + <div class="cs2s_settings_form_group_item"> |
| 30 | + <label for="storage_unit_name"> |
| 31 | + ${prompt} |
| 32 | + </label> |
| 33 | + <input type="text" name="storage_unit_name" placeholder="${placeholder}" maxlength="20"> |
| 34 | + </div> |
| 35 | + |
| 36 | + <div class="cs2s_settings_form_submit_group"> |
| 37 | + <button class="cs2s_blue_long_button" type="submit">Personalize</button> |
| 38 | + <button class="cs2s_grey_long_button" id="form_cancel" type="button">Cancel</button> |
| 39 | + </div> |
| 40 | + `, |
| 41 | + onsubmit: async (event) => { |
| 42 | + event.preventDefault(); |
| 43 | + |
| 44 | + let name = renameForm.elements["storage_unit_name"].value || renameForm.elements["storage_unit_name"].placeholder; |
| 45 | + |
| 46 | + if (!Script.Bot?.Plugin?.Connected) { |
| 47 | + Script.ShowStartInterfacePrompt({ |
| 48 | + message: "Interface must running to personalize storage units", |
| 49 | + autoClose: true, |
| 50 | + popoverMode: true, |
| 51 | + fade: false, |
| 52 | + onconnected: () => { |
| 53 | + popup.Hide(); |
| 54 | + this.#LabelItem(name); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + popup.Hide(); |
| 62 | + this.#LabelItem(name); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + const popup = new Popup({ |
| 67 | + title: "Personalize Your Storage Unit", |
| 68 | + fade: false, |
| 69 | + body: [CreateElement("div", { |
| 70 | + class: "cs2s_action_body", |
| 71 | + children: [ |
| 72 | + renameForm |
| 73 | + ] |
| 74 | + })] |
| 75 | + }); |
| 76 | + |
| 77 | + renameForm.querySelector("#form_cancel").onclick = () => { popup.Hide(); }; |
| 78 | + |
| 79 | + popup.Show(); |
| 80 | + } |
| 81 | + |
| 82 | + async #LabelItem(name) { |
| 83 | + const loadingBody = CreateElement("div", { |
| 84 | + class: "cs2s_action_body", |
| 85 | + children: [ |
| 86 | + CreateElement("div", { |
| 87 | + class: "cs2s_action_spinner" |
| 88 | + }) |
| 89 | + ] |
| 90 | + }); |
| 91 | + |
| 92 | + const successButton = CreateElement("div", { |
| 93 | + class: "cs2s_blue_long_button", |
| 94 | + text: "OK" |
| 95 | + }); |
| 96 | + |
| 97 | + const successBody = CreateElement("div", { |
| 98 | + class: "cs2s_action_body", |
| 99 | + children: [ |
| 100 | + CreateElement("div", { |
| 101 | + class: "cs2s_action_message cs2s_action_message_tall", |
| 102 | + text: "Storage Unit successfully labeled" |
| 103 | + }), |
| 104 | + successButton |
| 105 | + ] |
| 106 | + }); |
| 107 | + |
| 108 | + let storageUnitRenamed = false; |
| 109 | + |
| 110 | + const progressPopup = new Popup({ |
| 111 | + simpleMode: true, |
| 112 | + disableClose: true, |
| 113 | + fade: false, |
| 114 | + title: "Labeling Storage Unit", |
| 115 | + body: [ |
| 116 | + loadingBody, |
| 117 | + successBody |
| 118 | + ], |
| 119 | + onclose: () => { |
| 120 | + if (storageUnitRenamed) { |
| 121 | + window.location.reload(); |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + successBody.hide(); |
| 127 | + successButton.onclick = () => { progressPopup.Hide(); }; |
| 128 | + |
| 129 | + progressPopup.Show(); |
| 130 | + |
| 131 | + try { |
| 132 | + await this.#inventory.LabelStorageUnit(this.#casket, name); |
| 133 | + storageUnitRenamed = true; |
| 134 | + } catch (e) { |
| 135 | + progressPopup.Hide(); |
| 136 | + Script.ShowError({ level: ERROR_LEVEL.HIGH }, e, new Error(`Failed to label storage unit.`)); |
| 137 | + |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + loadingBody.hide(); |
| 142 | + successBody.show(); |
| 143 | + } |
| 144 | +} |
0 commit comments