UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 7.93 kB
import*as Common from"../../core/common/common.js";import*as i18n from"../../core/i18n/i18n.js";import*as UI from"../../ui/legacy/legacy.js";import locationsSettingsTabStyles from"./locationsSettingsTab.css.js";let locationsSettingsTabInstance;const UIStrings={customLocations:"Custom locations",locationName:"Location name",lat:"Lat",long:"Long",timezoneId:"Timezone ID",locale:"Locale",latitude:"Latitude",longitude:"Longitude",locationNameCannotBeEmpty:"Location name cannot be empty",locationNameMustBeLessThanS:"Location name must be less than {PH1} characters",latitudeMustBeANumber:"Latitude must be a number",latitudeMustBeGreaterThanOrEqual:"Latitude must be greater than or equal to {PH1}",latitudeMustBeLessThanOrEqualToS:"Latitude must be less than or equal to {PH1}",longitudeMustBeANumber:"Longitude must be a number",longitudeMustBeGreaterThanOr:"Longitude must be greater than or equal to {PH1}",longitudeMustBeLessThanOrEqualTo:"Longitude must be less than or equal to {PH1}",timezoneIdMustContainAlphabetic:"Timezone ID must contain alphabetic characters",localeMustContainAlphabetic:"Locale must contain alphabetic characters",addLocation:"Add location..."},str_=i18n.i18n.registerUIStrings("panels/sensors/LocationsSettingsTab.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class LocationsSettingsTab extends UI.Widget.VBox{list;customSetting;editor;constructor(){super(!0),this.contentElement.createChild("div","header").textContent=i18nString(UIStrings.customLocations);const t=UI.UIUtils.createTextButton(i18nString(UIStrings.addLocation),this.addButtonClicked.bind(this),"add-locations-button");this.contentElement.appendChild(t),this.list=new UI.ListWidget.ListWidget(this),this.list.element.classList.add("locations-list"),this.list.show(this.contentElement),this.customSetting=Common.Settings.Settings.instance().moduleSetting("emulation.locations");const e=this.customSetting.get().map((t=>function(t,e){if(!t.title){const i=e.find((e=>e.lat===t.lat&&e.long===t.long&&e.timezoneId===t.timezoneId&&e.locale===t.locale));if(i)return i;console.error("Could not determine a location setting title")}return t}(t,this.customSetting.defaultValue)));this.customSetting.set(e),this.customSetting.addChangeListener(this.locationsUpdated,this),this.setDefaultFocusedElement(t)}static instance(){return locationsSettingsTabInstance||(locationsSettingsTabInstance=new LocationsSettingsTab),locationsSettingsTabInstance}wasShown(){super.wasShown(),this.registerCSSFiles([locationsSettingsTabStyles]),this.list.registerCSSFiles([locationsSettingsTabStyles]),this.locationsUpdated()}locationsUpdated(){this.list.clear();const t=this.customSetting.get();for(const e of t)this.list.appendItem(e,!0);this.list.appendSeparator()}addButtonClicked(){this.list.addNewItem(this.customSetting.get().length,{title:"",lat:0,long:0,timezoneId:"",locale:""})}renderItem(t,e){const i=document.createElement("div");i.classList.add("locations-list-item");const n=i.createChild("div","locations-list-text locations-list-title").createChild("div","locations-list-title-text");return n.textContent=t.title,UI.Tooltip.Tooltip.install(n,t.title),i.createChild("div","locations-list-separator"),i.createChild("div","locations-list-text").textContent=String(t.lat),i.createChild("div","locations-list-separator"),i.createChild("div","locations-list-text").textContent=String(t.long),i.createChild("div","locations-list-separator"),i.createChild("div","locations-list-text").textContent=t.timezoneId,i.createChild("div","locations-list-separator"),i.createChild("div","locations-list-text").textContent=t.locale,i}removeItemRequested(t,e){const i=this.customSetting.get();i.splice(e,1),this.customSetting.set(i)}commitEdit(t,e,i){t.title=e.control("title").value.trim();const n=e.control("lat").value.trim();t.lat=n?parseFloat(n):0;const o=e.control("long").value.trim();t.long=o?parseFloat(o):0;const s=e.control("timezoneId").value.trim();t.timezoneId=s;const a=e.control("locale").value.trim();t.locale=a;const l=this.customSetting.get();i&&l.push(t),this.customSetting.set(l)}beginEdit(t){const e=this.createEditor();return e.control("title").value=t.title,e.control("lat").value=String(t.lat),e.control("long").value=String(t.long),e.control("timezoneId").value=t.timezoneId,e.control("locale").value=t.locale,e}createEditor(){if(this.editor)return this.editor;const t=new UI.ListWidget.Editor;this.editor=t;const e=t.contentElement(),i=e.createChild("div","locations-edit-row");i.createChild("div","locations-list-text locations-list-title").textContent=i18nString(UIStrings.locationName),i.createChild("div","locations-list-separator locations-list-separator-invisible"),i.createChild("div","locations-list-text").textContent=i18nString(UIStrings.lat),i.createChild("div","locations-list-separator locations-list-separator-invisible"),i.createChild("div","locations-list-text").textContent=i18nString(UIStrings.long),i.createChild("div","locations-list-separator locations-list-separator-invisible"),i.createChild("div","locations-list-text").textContent=i18nString(UIStrings.timezoneId),i.createChild("div","locations-list-separator locations-list-separator-invisible"),i.createChild("div","locations-list-text").textContent=i18nString(UIStrings.locale);const n=e.createChild("div","locations-edit-row");n.createChild("div","locations-list-text locations-list-title locations-input-container").appendChild(t.createInput("title","text",i18nString(UIStrings.locationName),(function(t,e,i){const n=i.value.trim();let o;n.length?n.length>50&&(o=i18nString(UIStrings.locationNameMustBeLessThanS,{PH1:50})):o=i18nString(UIStrings.locationNameCannotBeEmpty);if(o)return{valid:!1,errorMessage:o};return{valid:!0,errorMessage:void 0}}))),n.createChild("div","locations-list-separator locations-list-separator-invisible");let o=n.createChild("div","locations-list-text locations-input-container");return o.appendChild(t.createInput("lat","text",i18nString(UIStrings.latitude),(function(t,e,i){const n=i.value.trim(),o=Number(n);if(!n)return{valid:!0,errorMessage:void 0};let s;Number.isNaN(o)?s=i18nString(UIStrings.latitudeMustBeANumber):parseFloat(n)<-90?s=i18nString(UIStrings.latitudeMustBeGreaterThanOrEqual,{PH1:-90}):parseFloat(n)>90&&(s=i18nString(UIStrings.latitudeMustBeLessThanOrEqualToS,{PH1:90}));if(s)return{valid:!1,errorMessage:s};return{valid:!0,errorMessage:void 0}}))),n.createChild("div","locations-list-separator locations-list-separator-invisible"),o=n.createChild("div","locations-list-text locations-list-text-longitude locations-input-container"),o.appendChild(t.createInput("long","text",i18nString(UIStrings.longitude),(function(t,e,i){const n=i.value.trim(),o=Number(n);if(!n)return{valid:!0,errorMessage:void 0};let s;Number.isNaN(o)?s=i18nString(UIStrings.longitudeMustBeANumber):parseFloat(n)<-180?s=i18nString(UIStrings.longitudeMustBeGreaterThanOr,{PH1:-180}):parseFloat(n)>180&&(s=i18nString(UIStrings.longitudeMustBeLessThanOrEqualTo,{PH1:180}));if(s)return{valid:!1,errorMessage:s};return{valid:!0,errorMessage:void 0}}))),n.createChild("div","locations-list-separator locations-list-separator-invisible"),o=n.createChild("div","locations-list-text locations-input-container"),o.appendChild(t.createInput("timezoneId","text",i18nString(UIStrings.timezoneId),(function(t,e,i){const n=i.value.trim();if(""===n||/[a-zA-Z]/.test(n))return{valid:!0,errorMessage:void 0};return{valid:!1,errorMessage:i18nString(UIStrings.timezoneIdMustContainAlphabetic)}}))),n.createChild("div","locations-list-separator locations-list-separator-invisible"),o=n.createChild("div","locations-list-text locations-input-container"),o.appendChild(t.createInput("locale","text",i18nString(UIStrings.locale),(function(t,e,i){const n=i.value.trim();if(""===n||/[a-zA-Z]{2}/.test(n))return{valid:!0,errorMessage:void 0};return{valid:!1,errorMessage:i18nString(UIStrings.localeMustContainAlphabetic)}}))),t}}