pardus-options-library 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +323 -0
  2. package/eslint.config.js +38 -0
  3. package/package.json +32 -0
  4. package/src/classes/abstract/abstract-button.js +116 -0
  5. package/src/classes/abstract/abstract-toggle-button.js +145 -0
  6. package/src/classes/contents-area.js +22 -0
  7. package/src/classes/description-element.js +80 -0
  8. package/src/classes/disableable-html-element.js +48 -0
  9. package/src/classes/html-element.js +189 -0
  10. package/src/classes/info-element.js +56 -0
  11. package/src/classes/options/abstract-array-option.js +38 -0
  12. package/src/classes/options/abstract-option.js +189 -0
  13. package/src/classes/options/boolean-option.js +24 -0
  14. package/src/classes/options/grouped-options.js +61 -0
  15. package/src/classes/options/key-down-disable-button.js +20 -0
  16. package/src/classes/options/key-down-option.js +178 -0
  17. package/src/classes/options/key-down-set-key-button.js +18 -0
  18. package/src/classes/options/numeric-option.js +40 -0
  19. package/src/classes/options/select-option.js +74 -0
  20. package/src/classes/options/text-area-option.js +39 -0
  21. package/src/classes/options-box.js +200 -0
  22. package/src/classes/options-content.js +178 -0
  23. package/src/classes/options-group.js +114 -0
  24. package/src/classes/pardus-options-utility.js +139 -0
  25. package/src/classes/pardus-options.js +144 -0
  26. package/src/classes/save-button-row/load-button.js +21 -0
  27. package/src/classes/save-button-row/preset-label.js +95 -0
  28. package/src/classes/save-button-row/preset-row.js +106 -0
  29. package/src/classes/save-button-row/presets.js +57 -0
  30. package/src/classes/save-button-row/reset-button.js +21 -0
  31. package/src/classes/save-button-row/save-button-row.js +69 -0
  32. package/src/classes/save-button-row/save-button.js +21 -0
  33. package/src/classes/tabs/sub-tab.js +66 -0
  34. package/src/classes/tabs/tab-content.js +149 -0
  35. package/src/classes/tabs/tab-label.js +45 -0
  36. package/src/classes/tabs/tab.js +66 -0
  37. package/src/classes/tabs/tabs-element.js +25 -0
  38. package/src/classes/tabs/tabs-row.js +44 -0
  39. package/src/classes/tip-box.js +69 -0
  40. package/src/classes/version-row.js +14 -0
  41. package/src/index.js +13 -0
  42. package/webpack.config.cjs +16 -0
@@ -0,0 +1,20 @@
1
+ import AbstractToggleButton from '../abstract/abstract-toggle-button.js';
2
+
3
+ export default class DisableButton extends AbstractToggleButton {
4
+ constructor({
5
+ id,
6
+ premium = false,
7
+ disabled = false,
8
+ toggled = false,
9
+ }) {
10
+ super({
11
+ id,
12
+ premium,
13
+ disabled,
14
+ toggled,
15
+ actionText: 'Disable',
16
+ toggleText: 'Enable',
17
+ styleExtra: 'width:58px;',
18
+ });
19
+ }
20
+ }
@@ -0,0 +1,178 @@
1
+ import AbstractOption from './abstract-option.js';
2
+ import SetKeyButton from './key-down-set-key-button.js';
3
+ import DisableButton from './key-down-disable-button.js';
4
+ import PardusOptionsUtility from '../pardus-options-utility.js';
5
+
6
+ export default class KeyDownOption extends AbstractOption {
7
+ constructor({
8
+ id,
9
+ variable,
10
+ description = '',
11
+ defaultValue = false,
12
+ saveFunction = PardusOptionsUtility.defaultSaveFunction,
13
+ getFunction = PardusOptionsUtility.defaultGetFunction,
14
+ shallow = false,
15
+ reverse = false,
16
+ info = null,
17
+ }) {
18
+ const currValue = getFunction(`${PardusOptionsUtility.getVariableName(variable)}`, defaultValue);
19
+ const currDisabled = Object.hasOwn(currValue, 'disabled') && currValue.disabled;
20
+ super({
21
+ id,
22
+ variable,
23
+ description,
24
+ defaultValue,
25
+ saveFunction,
26
+ getFunction,
27
+ shallow,
28
+ reverse,
29
+ info,
30
+ disabled: currDisabled,
31
+ styleExtra: 'width: 20px;padding: 2px;text-align: center;margin: 2px 7px 2px;',
32
+ });
33
+ this.setKeyButton = new SetKeyButton({
34
+ id: `${this.id}-setkey`,
35
+ disabled: currDisabled,
36
+ });
37
+ this.disableButton = new DisableButton({
38
+ id: `${this.id}-disable`,
39
+ toggled: currDisabled,
40
+ });
41
+ this.boundCaptureKeyListener = this.captureKeyListener.bind(this);
42
+ this.setKeyButton.addClickListener(() => {
43
+ document.getElementById(`${this.inputId}-key`).value = '_';
44
+ document.getElementById(`${this.inputId}-key`).style.color = 'lime';
45
+ document.addEventListener('keydown', this.boundCaptureKeyListener, {
46
+ once: true,
47
+ ephemeral: true,
48
+ });
49
+ this.setKeyButton.toggle();
50
+ this.disableButton.disable();
51
+ });
52
+ this.setKeyButton.addToggleClickListener(() => {
53
+ document.removeEventListener('keydown', this.boundCaptureKeyListener);
54
+ this.setKeyButton.toggle();
55
+ this.disableButton.enable();
56
+ this.refreshElement();
57
+ });
58
+ this.disableButton.addClickListener(() => {
59
+ this.disable();
60
+ this.disableButton.toggle();
61
+ });
62
+ this.disableButton.addToggleClickListener(() => {
63
+ this.enable();
64
+ this.disableButton.toggle();
65
+ });
66
+ this.addAfterRefreshHook(() => {
67
+ this.setKeyButton.afterRefreshElement();
68
+ this.disableButton.afterRefreshElement();
69
+ });
70
+ }
71
+
72
+ captureKeyListener(event) {
73
+ this.getInputElement().value = JSON.stringify({
74
+ code: event.keyCode,
75
+ key: event.code,
76
+ description: event.key,
77
+ });
78
+ document.getElementById(`${this.inputId}-key`).value = this.getCurrentKeyDescription();
79
+ document.getElementById(`${this.inputId}-key`).style.color = '#D0D1D9';
80
+ this.disableButton.enable();
81
+ this.setKeyButton.toggle();
82
+ }
83
+
84
+ getInnerHTML() {
85
+ let keyPressHtml = `<input id='${this.inputId}' type='text' hidden value='${JSON.stringify(this.getValue())}'>`;
86
+ keyPressHtml += `<table width='100%' style='border-collapse: collapse;'><tbody><tr><td align="left"><input id='${this.inputId}-key' type='text' cols='1' maxlength='1' readonly value="${this.getKeyDescription()}" style="${this.style}" ${this.disabled ? 'disabled' : ''}/></td><td align="right">${this.setKeyButton}</td><td align="right" style='padding-right: 0px;'>${this.disableButton}</td></tr></tbody></table>`;
87
+ return keyPressHtml;
88
+ }
89
+
90
+ /**
91
+ * Disables the input element
92
+ * @function AbstractOption#disable
93
+ */
94
+ disable() {
95
+ this.setDisabled(true);
96
+ document.getElementById(`${this.inputId}-key`).removeAttribute('disabled');
97
+ document.getElementById(`${this.inputId}-key`).setAttribute('style', this.style);
98
+
99
+ this.getInputElement().value = JSON.stringify({
100
+ ...this.getCurrentValue(),
101
+ disabled: true,
102
+ });
103
+ }
104
+
105
+ /**
106
+ * Enables the input element
107
+ * @function AbstractOption#enable
108
+ */
109
+ enable() {
110
+ this.setDisabled(false);
111
+ document.getElementById(`${this.inputId}-key`).removeAttribute('disabled');
112
+ document.getElementById(`${this.inputId}-key`).setAttribute('style', this.style);
113
+
114
+ const newValue = this.getCurrentValue();
115
+ delete newValue.disabled;
116
+ this.getInputElement().value = JSON.stringify(newValue);
117
+ }
118
+
119
+ setDisabled(disabled = false) {
120
+ this.disabled = disabled;
121
+ if (this.disabled) {
122
+ this.colour = '#B5B5B5';
123
+ this.backgroundColour = '#CCCCCC';
124
+ this.setKeyButton.disable();
125
+ } else {
126
+ this.colour = '#D0D1D9';
127
+ this.backgroundColour = '#00001C';
128
+ this.setKeyButton.enable();
129
+ }
130
+ this.style = `color: ${this.colour};background-color: ${this.backgroundColour};${this.styleExtra}`;
131
+ }
132
+
133
+ saveValue(overrideSaveFunction = null) {
134
+ if (overrideSaveFunction && typeof overrideSaveFunction === 'function') {
135
+ overrideSaveFunction(`${PardusOptionsUtility.getVariableName(this.variable)}`, this.getCurrentValue());
136
+ } else {
137
+ this.saveFunction(`${PardusOptionsUtility.getVariableName(this.variable)}`, this.getCurrentValue());
138
+ }
139
+ }
140
+
141
+ getKey() {
142
+ return this.getValue().key;
143
+ }
144
+
145
+ getKeyDescription() {
146
+ return this.getValue().description;
147
+ }
148
+
149
+ getKeyCode() {
150
+ return this.getValue().code;
151
+ }
152
+
153
+ isDisabled() {
154
+ const value = this.getValue();
155
+
156
+ if (Object.hasOwn(value, 'disabled') && value.disabled) {
157
+ return true;
158
+ }
159
+
160
+ return false;
161
+ }
162
+
163
+ getCurrentKey() {
164
+ return this.getCurrentValue().key;
165
+ }
166
+
167
+ getCurrentKeyDescription() {
168
+ return this.getCurrentValue().description;
169
+ }
170
+
171
+ getCurrentCode() {
172
+ return this.getCurrentValue().code;
173
+ }
174
+
175
+ getCurrentValue() {
176
+ return JSON.parse(this.getInputElement().value);
177
+ }
178
+ }
@@ -0,0 +1,18 @@
1
+ import AbstractToggleButton from '../abstract/abstract-toggle-button.js';
2
+
3
+ export default class SetKeyButton extends AbstractToggleButton {
4
+ constructor({
5
+ id,
6
+ premium = false,
7
+ disabled = false,
8
+ }) {
9
+ super({
10
+ id,
11
+ premium,
12
+ disabled,
13
+ actionText: 'Set Key',
14
+ toggleText: 'Cancel',
15
+ styleExtra: 'width:58px;',
16
+ });
17
+ }
18
+ }
@@ -0,0 +1,40 @@
1
+ import AbstractOption from './abstract-option.js';
2
+ import PardusOptionsUtility from '../pardus-options-utility.js';
3
+
4
+ export default class NumericOption extends AbstractOption {
5
+ constructor({
6
+ id,
7
+ variable,
8
+ description,
9
+ defaultValue = 0,
10
+ saveFunction = PardusOptionsUtility.defaultSaveFunction,
11
+ getFunction = PardusOptionsUtility.defaultGetFunction,
12
+ disabled = false,
13
+ min = 0,
14
+ max = 0,
15
+ step = 1,
16
+ info = null,
17
+ }) {
18
+ super({
19
+ id,
20
+ variable,
21
+ description,
22
+ defaultValue,
23
+ saveFunction,
24
+ getFunction,
25
+ info,
26
+ disabled,
27
+ });
28
+ this.minValue = min;
29
+ this.maxValue = max;
30
+ this.stepValue = step;
31
+ }
32
+
33
+ getInnerHTML() {
34
+ return `<input id="${this.inputId}" type="number" min="${this.minValue}" max="${this.maxValue}" step="${this.stepValue}" value="${this.getValue()}" style="${this.style}" ${this.disabled ? 'disabled' : ''}>`;
35
+ }
36
+
37
+ getCurrentValue() {
38
+ return this.getInputElement().value;
39
+ }
40
+ }
@@ -0,0 +1,74 @@
1
+ import AbstractOption from './abstract-option.js';
2
+ import PardusOptionsUtility from '../pardus-options-utility.js';
3
+
4
+ export default class SelectOption extends AbstractOption {
5
+ constructor({
6
+ id,
7
+ variable,
8
+ description,
9
+ defaultValue = null,
10
+ saveFunction = PardusOptionsUtility.defaultSaveFunction,
11
+ getFunction = PardusOptionsUtility.defaultGetFunction,
12
+ disabled = false,
13
+ info = null,
14
+ inheritStyle = false,
15
+ options = [],
16
+ }) {
17
+ super({
18
+ id,
19
+ variable,
20
+ description,
21
+ defaultValue,
22
+ saveFunction,
23
+ getFunction,
24
+ info,
25
+ disabled,
26
+ align: 'right',
27
+ });
28
+ this.options = options;
29
+
30
+ if (inheritStyle) {
31
+ this.addEventListener('change', () => {
32
+ this.updateSelectStyle();
33
+ });
34
+ }
35
+ }
36
+
37
+ getInnerHTML() {
38
+ let selectHtml = '';
39
+ const savedValue = this.getValue();
40
+ let hasSelected = false;
41
+ let selectStyle = '';
42
+ for (const option of this.options) {
43
+ const style = (option.style) ? ` style="${option.style}"` : '';
44
+ if (!hasSelected && (option.value === savedValue || (option.default && option.default === true && !savedValue))) {
45
+ selectHtml += `<option value=${option.value}${style} selected>${option.text}</option>`;
46
+ hasSelected = true;
47
+ selectStyle = (option.style) ? ` style="${option.style}"` : '';
48
+ } else {
49
+ selectHtml += `<option value=${option.value}${style}>${option.text}</option>`;
50
+ }
51
+ }
52
+
53
+ selectHtml = `<select id="${this.inputId}"${selectStyle} style="${this.style}" ${this.disabled ? 'disabled' : ''}>${selectHtml}`;
54
+
55
+ return selectHtml;
56
+ }
57
+
58
+ updateSelectStyle() {
59
+ const currentStyle = this.getInputElement().selectedOptions[0].getAttribute('style');
60
+ this.getInputElement().setAttribute('style', currentStyle);
61
+ }
62
+
63
+ getOptions() {
64
+ return this.options;
65
+ }
66
+
67
+ setOptions(options = []) {
68
+ this.options = options;
69
+ }
70
+
71
+ getCurrentValue() {
72
+ return this.getInputElement().value;
73
+ }
74
+ }
@@ -0,0 +1,39 @@
1
+ import AbstractOption from './abstract-option.js';
2
+ import PardusOptionsUtility from '../pardus-options-utility.js';
3
+
4
+ export default class TextAreaOption extends AbstractOption {
5
+ constructor({
6
+ id,
7
+ variable,
8
+ description,
9
+ defaultValue = 0,
10
+ saveFunction = PardusOptionsUtility.defaultSaveFunction,
11
+ getFunction = PardusOptionsUtility.defaultGetFunction,
12
+ disabled = false,
13
+ info = null,
14
+ rows = 3,
15
+ cols = 65,
16
+ }) {
17
+ super({
18
+ id,
19
+ variable,
20
+ description,
21
+ defaultValue,
22
+ saveFunction,
23
+ getFunction,
24
+ info,
25
+ disabled,
26
+ styleExtra: 'font-family: Helvetica, Arial, sans-serif;font-size:11px;',
27
+ });
28
+ this.rows = rows;
29
+ this.cols = cols;
30
+ }
31
+
32
+ getInnerHTML() {
33
+ return `<textarea id="${this.inputId}" width="100%" autocomplete="off" autocorrect="off" spellcheck="false" ${(this.rows === 0) ? '' : `rows="${this.rows}"`} ${(this.cols === 0) ? '' : `cols="${this.cols}"`} style="${this.style}" ${this.disabled ? 'disabled' : ''}>${this.getValue()}</textarea>`;
34
+ }
35
+
36
+ getCurrentValue() {
37
+ return this.getInputElement().value;
38
+ }
39
+ }
@@ -0,0 +1,200 @@
1
+ import DisableableHtmlElement from './disableable-html-element.js';
2
+ import PardusOptionsUtility from './pardus-options-utility.js';
3
+ import SaveButtonRow from './save-button-row/save-button-row.js';
4
+ import Presets from './save-button-row/presets.js';
5
+ import OptionsGroup from './options-group.js';
6
+ import DescriptionElement from './description-element.js';
7
+
8
+ export default class OptionsBox extends DisableableHtmlElement {
9
+ constructor({
10
+ id,
11
+ heading,
12
+ premium = false,
13
+ description = '',
14
+ imageLeft = '',
15
+ imageRight = '',
16
+ saveFunction = PardusOptionsUtility.defaultSaveFunction,
17
+ getFunction = PardusOptionsUtility.defaultGetFunction,
18
+ refresh = true,
19
+ resetButton = false,
20
+ presets = 0,
21
+ disabled = false,
22
+ }) {
23
+ super({
24
+ id,
25
+ disabled,
26
+ });
27
+ this.heading = heading;
28
+ this.premium = premium;
29
+ this.saveFunction = saveFunction;
30
+ this.getFunction = getFunction;
31
+ this.refresh = refresh;
32
+ this.resetButton = resetButton;
33
+ this.disabled = disabled;
34
+
35
+ const headerHtml = (premium) ? '<th class="premium">' : '<th>';
36
+ this.frontContainer = `<form id="${this.id}" action="none"><table style="background:url(${PardusOptionsUtility.getImagePackUrl()}bgd.gif)" width="100%" cellpadding="3" align="center"><tbody><tr>${headerHtml}${heading}</th></tr>`;
37
+ this.backContainer = '</tbody></table></form>';
38
+ this.innerHtml = '';
39
+ this.description = new DescriptionElement({
40
+ id: `${this.id}-description`,
41
+ description,
42
+ imageLeft,
43
+ imageRight,
44
+ });
45
+ this.optionsGroup = new OptionsGroup({
46
+ id: `${this.id}-options-group`,
47
+ premium,
48
+ saveFunction,
49
+ getFunction,
50
+ disabled,
51
+ });
52
+ this.saveButtonRow = new SaveButtonRow({
53
+ id: `${this.id}-save`,
54
+ premium,
55
+ resetButton,
56
+ disabled,
57
+ });
58
+ this.presets = new Presets({
59
+ id: `${this.id}-presets`,
60
+ premium,
61
+ presets,
62
+ disabled,
63
+ });
64
+ this.addAfterRefreshHook((opts) => {
65
+ if (opts.maintainRefreshStatus) {
66
+ return;
67
+ }
68
+ this.refresh = true;
69
+ });
70
+ this.addAfterRefreshHook(() => {
71
+ this.optionsGroup.afterRefreshElement();
72
+ this.saveButtonRow.afterRefreshElement();
73
+ });
74
+ this.addAfterRefreshHook(() => {
75
+ this.setFunctions();
76
+ });
77
+ }
78
+
79
+ toString() {
80
+ if (this.optionsGroup.options.length === 0) {
81
+ return this.frontContainer + this.description + this.innerHtml + this.optionsGroup + this.backContainer;
82
+ }
83
+ return this.frontContainer + this.description + this.innerHtml + this.optionsGroup + this.saveButtonRow + this.presets + this.backContainer;
84
+ }
85
+
86
+ setFunctions() {
87
+ if (this.optionsGroup.options.length !== 0) {
88
+ this.saveButtonRow.addSaveEventListener(() => {
89
+ for (const option of this.optionsGroup.options) {
90
+ option.saveValue();
91
+ }
92
+ this.saveButtonRow.displaySaved();
93
+
94
+ const event = new Event('save');
95
+ this.getElement().dispatchEvent(event);
96
+ });
97
+ this.saveButtonRow.addResetEventListener(() => {
98
+ for (const option of this.optionsGroup.options) {
99
+ option.resetValue();
100
+ }
101
+ this.saveButtonRow.displayReset();
102
+ this.optionsGroup.refreshElement();
103
+
104
+ const event = new Event('reset');
105
+ this.getElement().dispatchEvent(event);
106
+ });
107
+ }
108
+
109
+ this.presets.setFunctions(this.optionsGroup.options);
110
+ }
111
+
112
+ /**
113
+ * Allows disabling or enabling this element and all nested elements without refreshing
114
+ * @function OptionsBox#setDisabled
115
+ */
116
+ setDisabled(disabled = false) {
117
+ this.disabled = disabled;
118
+ this.optionsGroup.setDisabled(disabled);
119
+ this.saveButtonRow.setDisabled(disabled);
120
+ this.presets.setDisabled(disabled);
121
+ }
122
+
123
+ addSaveEventListener(func) {
124
+ return this.saveButtonRow.addSaveEventListener(func);
125
+ }
126
+
127
+ addBooleanOption({
128
+ refresh = this.refresh,
129
+ ...args
130
+ }) {
131
+ const newOption = this.optionsGroup.addBooleanOption(args);
132
+ if (refresh) {
133
+ this.refreshElement();
134
+ }
135
+ return newOption;
136
+ }
137
+
138
+ addTextAreaOption({
139
+ refresh = this.refresh,
140
+ ...args
141
+ }) {
142
+ const newOption = this.optionsGroup.addTextAreaOption(args);
143
+ if (refresh) {
144
+ this.refreshElement();
145
+ }
146
+ return newOption;
147
+ }
148
+
149
+ addNumericOption({
150
+ refresh = this.refresh,
151
+ ...args
152
+ }) {
153
+ const newOption = this.optionsGroup.addNumericOption(args);
154
+ if (refresh) {
155
+ this.refreshElement();
156
+ }
157
+ return newOption;
158
+ }
159
+
160
+ addKeyDownOption({
161
+ refresh = this.refresh,
162
+ ...args
163
+ }) {
164
+ const newOption = this.optionsGroup.addKeyDownOption(args);
165
+ if (refresh) {
166
+ this.refreshElement();
167
+ }
168
+ return newOption;
169
+ }
170
+
171
+ addSelectOption({
172
+ refresh = this.refresh,
173
+ ...args
174
+ }) {
175
+ const newOption = this.optionsGroup.addSelectOption(args);
176
+ if (refresh) {
177
+ this.refreshElement();
178
+ }
179
+ return newOption;
180
+ }
181
+
182
+ addGroupedOption({
183
+ description,
184
+ saveFunction = this.saveFunction,
185
+ getFunction = this.getFunction,
186
+ refresh = this.refresh,
187
+ }) {
188
+ const groupedOptions = {
189
+ description,
190
+ saveFunction,
191
+ getFunction,
192
+ };
193
+
194
+ const newOption = this.optionsGroup.addGroupedOption(groupedOptions);
195
+ if (refresh) {
196
+ this.refreshElement();
197
+ }
198
+ return newOption;
199
+ }
200
+ }