All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3 KiB

  1. export default class EditJsonConfig extends FormApplication {
  2. constructor(config, callback) {
  3. super({}, {});
  4. this.config = config;
  5. this.callback = callback;
  6. }
  7. static get defaultOptions() {
  8. return mergeObject(super.defaultOptions, {
  9. id: 'token-variants-config-json-edit',
  10. classes: ['sheet'],
  11. template: 'modules/token-variants/templates/configJsonEdit.html',
  12. resizable: true,
  13. minimizable: false,
  14. title: 'Edit Token Configuration',
  15. width: 400,
  16. height: 380,
  17. });
  18. }
  19. async getData(options) {
  20. const data = super.getData(options);
  21. data.hasConfig = this.config != null && Object.keys(this.config).length !== 0;
  22. data.config = JSON.stringify(data.hasConfig ? this.config : {}, null, 2);
  23. return data;
  24. }
  25. /**
  26. * @param {JQuery} html
  27. */
  28. activateListeners(html) {
  29. super.activateListeners(html);
  30. html.on('input', '.command textarea', this._validateJSON.bind(this));
  31. // Override 'Tab' key to insert spaces
  32. html.on('keydown', '.command textarea', function (e) {
  33. if (e.key === 'Tab' && !e.shiftKey) {
  34. e.preventDefault();
  35. var start = this.selectionStart;
  36. var end = this.selectionEnd;
  37. this.value = this.value.substring(0, start) + ' ' + this.value.substring(end);
  38. this.selectionStart = this.selectionEnd = start + 2;
  39. return false;
  40. }
  41. });
  42. html.find('.remove').click(this._onRemove.bind(this));
  43. html.find('.format').click(this._onFormat.bind(this));
  44. }
  45. async _validateJSON(event) {
  46. const controls = $(event.target).closest('form').find('button[type="submit"], button.format');
  47. try {
  48. this.config = JSON.parse(event.target.value);
  49. this.config = expandObject(this.config);
  50. this.flag = this.config.flag;
  51. controls.prop('disabled', false);
  52. } catch (e) {
  53. controls.prop('disabled', true);
  54. }
  55. }
  56. async _onRemove(event) {
  57. this.config = {};
  58. this.submit();
  59. }
  60. async _onFormat(event) {
  61. $(event.target)
  62. .closest('form')
  63. .find('textarea[name="config"]')
  64. .val(JSON.stringify(this.config, null, 2));
  65. }
  66. /**
  67. * @param {Event} event
  68. * @param {Object} formData
  69. */
  70. async _updateObject(event, formData) {
  71. if (this.callback) this.callback(this.config);
  72. }
  73. }