@idevs/corelib 0.0.39 → 0.0.40

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.
@@ -0,0 +1,49 @@
1
+ /// <reference types="jquery" />
2
+ export type DropdownToolButtonOptions = {
3
+ title?: string;
4
+ cssClass?: string;
5
+ icon?: string;
6
+ disabled?: boolean;
7
+ dropdownMenuPosition?: 'right';
8
+ isDropUp?: boolean;
9
+ };
10
+ export type DropdownToolButtonItem = {
11
+ key: string;
12
+ title?: string;
13
+ hint?: string;
14
+ cssClass?: string;
15
+ icon?: string;
16
+ onClick?: (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => void;
17
+ isSeparator?: boolean;
18
+ disabled?: boolean;
19
+ isDropdownHeader?: boolean;
20
+ dropdownHeaderTitle?: string;
21
+ };
22
+ export type ToolDropdownSideButtonItem = {
23
+ key: string;
24
+ title?: string;
25
+ hint?: string;
26
+ cssClass?: string;
27
+ icon?: string;
28
+ onClick?: (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => void;
29
+ disabled?: boolean;
30
+ };
31
+ export declare class DropdownToolButton {
32
+ element: JQuery;
33
+ private isDisabled;
34
+ private itemDisablingState;
35
+ private options;
36
+ constructor(container: JQuery, buttons: DropdownToolButtonItem[], opt?: DropdownToolButtonOptions);
37
+ private getDisablingStateItem;
38
+ private setDisablingStateItem;
39
+ private removeDisablingStateItem;
40
+ private buildBaseDropdown;
41
+ addDropdownItems(buttons: DropdownToolButtonItem[]): void;
42
+ addDropdownItem(button: DropdownToolButtonItem, idx?: number): void;
43
+ enableDropdown(enable: boolean): void;
44
+ enableDropdownItemByKey(key: string, enable: boolean): void;
45
+ enableSideButtonByKey(key: string, enable: boolean): void;
46
+ removeDropdownItem(key: string): void;
47
+ removeSideButtonItem(key: string): void;
48
+ addSideButtonItem(button: ToolDropdownSideButtonItem, idx?: number): void;
49
+ }
@@ -0,0 +1,230 @@
1
+ /// <reference types="bootstrap" />
2
+ import { coalesce, first, isEmptyOrNull, tryFirst } from '@serenity-is/corelib/q';
3
+ export class DropdownToolButton {
4
+ constructor(container, buttons, opt) {
5
+ this.element = null;
6
+ this.isDisabled = false;
7
+ this.itemDisablingState = [];
8
+ this.options = opt || {};
9
+ this.isDisabled = this.options.disabled || false;
10
+ this.element = this.buildBaseDropdown();
11
+ this.addDropdownItems(buttons);
12
+ this.element.on('click', () => {
13
+ $('.dropdown-toggle', this.element).dropdown('toggle');
14
+ });
15
+ container.append(this.element);
16
+ }
17
+ getDisablingStateItem(key) {
18
+ if (tryFirst(this.itemDisablingState, x => x.key == key) != null) {
19
+ return first(this.itemDisablingState, x => x.key == key).disabled || false;
20
+ }
21
+ return false;
22
+ }
23
+ setDisablingStateItem(key, value) {
24
+ if (tryFirst(this.itemDisablingState, x => x.key == key) != null) {
25
+ first(this.itemDisablingState, x => x.key == key).disabled = value || false;
26
+ return;
27
+ }
28
+ this.itemDisablingState.push({ key: key, disabled: value || false });
29
+ }
30
+ removeDisablingStateItem(key) {
31
+ this.itemDisablingState.some((item, idx) => {
32
+ if (item.key == key) {
33
+ this.itemDisablingState.splice(idx, 1);
34
+ return true;
35
+ }
36
+ });
37
+ }
38
+ buildBaseDropdown() {
39
+ const dropdownTemplate = `<div class="buttons-inner dropdown" style="overflow: visible">
40
+ <div class="idevs-tool-dropdown-button tool-button icon-tool-button ${coalesce(this.options.cssClass, '')} ${this.options.isDropUp ? 'dropup' : ''} ${this.isDisabled ? 'disabled' : ''}" style="cursor: unset;">
41
+ <div class="button-outer dropdown-toggle ${this.isDisabled ? 'disabled' : ''}"
42
+ data-bs-toggle="dropdown"
43
+ style="cursor: pointer;">
44
+ <span class="button-inner">
45
+ <i class="${this.options.icon}"></i>
46
+ ${coalesce(this.options.title, '')}
47
+ </span>
48
+ <i class="caret"></i>
49
+ </div>
50
+ <ul class="dropdown-menu ${this.options.dropdownMenuPosition == 'right' ? 'dropdown-menu-right' : ''}"></ul>
51
+ </div>
52
+ </div>`;
53
+ return $(dropdownTemplate);
54
+ }
55
+ addDropdownItems(buttons) {
56
+ if (buttons && buttons.length > 0) {
57
+ buttons.forEach(button => {
58
+ this.addDropdownItem(button);
59
+ });
60
+ }
61
+ }
62
+ addDropdownItem(button, idx) {
63
+ if (!isEmptyOrNull(button.key)) {
64
+ if (this.itemDisablingState.map(x => x.key).indexOf(button.key) > -1) {
65
+ alert(`Dropdown has existed key: ${button.key}`);
66
+ return;
67
+ }
68
+ this.setDisablingStateItem(button.key, button.disabled || false);
69
+ }
70
+ let dropdownItemElement;
71
+ if (button.isDropdownHeader && !isEmptyOrNull(button.dropdownHeaderTitle)) {
72
+ dropdownItemElement = $(`<li class="dropdown-header ${coalesce(button.cssClass, '')}">${button.dropdownHeaderTitle}</li>`);
73
+ }
74
+ else {
75
+ if (button.isSeparator) {
76
+ dropdownItemElement = $(`<li class="dropdown-divider"></li>`);
77
+ }
78
+ else {
79
+ dropdownItemElement = $(`<li class="${button.disabled ? 'disabled' : ''}"
80
+ title="${coalesce(button.hint, '')}"
81
+ data-idevs-key="${coalesce(button.key, '')}">
82
+ <a href="#" class="${coalesce(button.cssClass, 'dropdown-item')}">
83
+ <i class="${coalesce(button.icon, '')}"></i>
84
+ ${button.title}
85
+ </a>
86
+ </li>`);
87
+ dropdownItemElement.on('click', (e) => {
88
+ e.preventDefault();
89
+ if (this.isDisabled) {
90
+ return;
91
+ }
92
+ let buttonIsDisabled = button.disabled;
93
+ if (!isEmptyOrNull(button.key)) {
94
+ buttonIsDisabled = this.getDisablingStateItem(button.key);
95
+ }
96
+ if (buttonIsDisabled) {
97
+ return;
98
+ }
99
+ button.onClick(e);
100
+ });
101
+ }
102
+ }
103
+ if (idx === null || typeof idx === 'undefined') {
104
+ this.element.find('.dropdown-menu').append(dropdownItemElement);
105
+ return;
106
+ }
107
+ if (idx <= 0) {
108
+ this.element.find('.dropdown-menu').prepend(dropdownItemElement);
109
+ return;
110
+ }
111
+ const nbrOfButtons = this.element.find(`.dropdown-menu > li`).length;
112
+ if (idx > nbrOfButtons) {
113
+ idx = nbrOfButtons;
114
+ }
115
+ this.element.find(`.dropdown-menu > li:nth-child(${idx})`).after(dropdownItemElement);
116
+ }
117
+ enableDropdown(enable) {
118
+ const drd = this.element.find('.dropdown').first();
119
+ if (drd) {
120
+ if (enable) {
121
+ if (drd.hasClass('disabled')) {
122
+ drd.removeClass('disabled');
123
+ }
124
+ }
125
+ else {
126
+ if (!drd.hasClass('disabled')) {
127
+ drd.addClass('disabled');
128
+ }
129
+ }
130
+ }
131
+ const drdToggle = this.element.find('.dropdown-toggle').first();
132
+ if (drdToggle) {
133
+ if (enable) {
134
+ if (drdToggle.hasClass('disabled')) {
135
+ drd.removeClass('disabled');
136
+ }
137
+ else {
138
+ if (!drdToggle.hasClass('disabled')) {
139
+ drdToggle.addClass('disabled');
140
+ }
141
+ }
142
+ }
143
+ }
144
+ this.isDisabled = !enable;
145
+ }
146
+ enableDropdownItemByKey(key, enable) {
147
+ const drdItem = this.element.find(`.dropdown-menu li[data-idevs-key="${key}"]`).first();
148
+ if (drdItem) {
149
+ if (enable) {
150
+ if (drdItem.hasClass('disabled')) {
151
+ drdItem.removeClass('disabled');
152
+ }
153
+ }
154
+ else {
155
+ if (!drdItem.hasClass('disabled')) {
156
+ drdItem.addClass('disabled');
157
+ }
158
+ }
159
+ }
160
+ this.setDisablingStateItem(key, !enable);
161
+ }
162
+ enableSideButtonByKey(key, enable) {
163
+ const tButton = this.element.find(`.tool-button[data-idevs-key="${key}"]`).first();
164
+ if (tButton) {
165
+ if (enable) {
166
+ if (tButton.hasClass('disabled')) {
167
+ tButton.removeClass('disabled');
168
+ }
169
+ }
170
+ else {
171
+ if (!tButton.hasClass('disabled')) {
172
+ tButton.addClass('disabled');
173
+ }
174
+ }
175
+ }
176
+ this.setDisablingStateItem(key, !enable);
177
+ }
178
+ removeDropdownItem(key) {
179
+ this.element.find(`.dropdown-menu li[data-idevs-key="${key}"]`).remove();
180
+ this.removeDisablingStateItem(key);
181
+ }
182
+ removeSideButtonItem(key) {
183
+ this.element.find(`.tool-button[data-idevs-key="${key}"]`).remove();
184
+ this.removeDisablingStateItem(key);
185
+ }
186
+ addSideButtonItem(button, idx) {
187
+ if (!isEmptyOrNull(button.key)) {
188
+ if (this.itemDisablingState.map(x => x.key).indexOf(button.key) > -1) {
189
+ alert(`Dropdown has existed key: ${button.key}`);
190
+ return;
191
+ }
192
+ this.setDisablingStateItem(button.key, button.disabled || false);
193
+ }
194
+ const sideButtonTemplate = `<div class="tool-button add-button icon-tool-button ${coalesce(button.cssClass, '')} ${button.disabled ? 'disabled' : ''}"
195
+ data-idevs-key="${coalesce(button.key, '')}"
196
+ title="${coalesce(button.title, '')}">
197
+ <div class="button-outer">
198
+ <span class="button-inner">
199
+ <i class="${coalesce(button.icon, '')}"></i>
200
+ </span>
201
+ </div>
202
+ </div>`;
203
+ const sideButton = $(sideButtonTemplate);
204
+ sideButton.on('click', (e) => {
205
+ e.preventDefault();
206
+ let buttonIsDisabled = button.disabled;
207
+ if (!isEmptyOrNull(button.key)) {
208
+ buttonIsDisabled = this.getDisablingStateItem(button.key);
209
+ }
210
+ if (buttonIsDisabled) {
211
+ return;
212
+ }
213
+ button.onClick(e);
214
+ });
215
+ if (idx === null || typeof idx === 'undefined') {
216
+ this.element.append(sideButton);
217
+ return;
218
+ }
219
+ if (idx <= 0) {
220
+ this.element.prepend(sideButton);
221
+ return;
222
+ }
223
+ const nbrOfButtons = this.element.find(`div.tool-button`).length;
224
+ if (idx > nbrOfButtons) {
225
+ idx = nbrOfButtons;
226
+ }
227
+ this.element.find(`div.tool-button:nth-child(${idx})`).after(sideButton);
228
+ }
229
+ }
230
+ //# sourceMappingURL=DropdownToolButton.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DropdownToolButton.js","sourceRoot":"","sources":["../../src/ui/DropdownToolButton.ts"],"names":[],"mappings":"AAAA,mCAAmC;AAEnC,OAAO,EAAE,QAAQ,EAAE,KAAK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GAAG,KAAK,WAAW,EAAE;YAC9C,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;YAC/B,OAAM;SACP;QAED,IAAI,GAAG,IAAI,CAAC,EAAE;YACZ,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;YAChC,OAAM;SACP;QAED,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAAA;QAEhE,IAAI,GAAG,GAAG,YAAY,EAAE;YACtB,GAAG,GAAG,YAAY,CAAA;SACnB;QAED,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,6BAA6B,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;IAC1E,CAAC;CACF","sourcesContent":["/// <reference types=\"bootstrap\" />\n\nimport { coalesce, first, isEmptyOrNull, tryFirst } from '@serenity-is/corelib/q'\n\nexport type DropdownToolButtonOptions = {\n title?: string\n cssClass?: string\n icon?: string\n disabled?: boolean\n dropdownMenuPosition?: 'right'\n isDropUp?: boolean\n}\n\nexport type DropdownToolButtonItem = {\n key: string\n title?: string\n hint?: string\n cssClass?: string\n icon?: string\n onClick?: (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => void\n isSeparator?: boolean\n disabled?: boolean\n isDropdownHeader?: boolean\n dropdownHeaderTitle?: string\n}\n\nexport type ToolDropdownSideButtonItem = {\n key: string\n title?: string\n hint?: string\n cssClass?: string\n icon?: string\n onClick?: (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => void\n disabled?: boolean\n}\n\nexport class DropdownToolButton {\n public element: JQuery = null\n private isDisabled = false\n private itemDisablingState: { key: string; disabled: boolean }[] = []\n private options: DropdownToolButtonOptions\n\n public constructor(container: JQuery, buttons: DropdownToolButtonItem[], opt?: DropdownToolButtonOptions) {\n this.options = opt || {}\n this.isDisabled = this.options.disabled || false\n this.element = this.buildBaseDropdown()\n this.addDropdownItems(buttons)\n\n this.element.on('click', () => {\n $('.dropdown-toggle', this.element).dropdown('toggle')\n })\n\n container.append(this.element)\n }\n\n private getDisablingStateItem(key: string): boolean {\n if (tryFirst(this.itemDisablingState, x => x.key == key) != null) {\n return first(this.itemDisablingState, x => x.key == key).disabled || false\n }\n\n return false\n }\n\n private setDisablingStateItem(key: string, value: boolean) {\n if (tryFirst(this.itemDisablingState, x => x.key == key) != null) {\n first(this.itemDisablingState, x => x.key == key).disabled = value || false\n return\n }\n\n this.itemDisablingState.push({ key: key, disabled: value || false })\n }\n\n private removeDisablingStateItem(key: string) {\n this.itemDisablingState.some((item, idx) => {\n if (item.key == key) {\n this.itemDisablingState.splice(idx, 1)\n\n return true\n }\n })\n }\n\n private buildBaseDropdown(): JQuery {\n const dropdownTemplate = `<div class=\"buttons-inner dropdown\" style=\"overflow: visible\">\n <div class=\"idevs-tool-dropdown-button tool-button icon-tool-button ${coalesce(this.options.cssClass, '')} ${\n this.options.isDropUp ? 'dropup' : ''\n } ${this.isDisabled ? 'disabled' : ''}\" style=\"cursor: unset;\">\n <div class=\"button-outer dropdown-toggle ${this.isDisabled ? 'disabled' : ''}\"\n data-bs-toggle=\"dropdown\"\n style=\"cursor: pointer;\">\n <span class=\"button-inner\">\n <i class=\"${this.options.icon}\"></i>\n ${coalesce(this.options.title, '')}\n </span>\n <i class=\"caret\"></i>\n </div>\n <ul class=\"dropdown-menu ${this.options.dropdownMenuPosition == 'right' ? 'dropdown-menu-right' : ''}\"></ul>\n </div>\n</div>`\n\n return $(dropdownTemplate)\n }\n\n public addDropdownItems(buttons: DropdownToolButtonItem[]) {\n if (buttons && buttons.length > 0) {\n buttons.forEach(button => {\n this.addDropdownItem(button)\n })\n }\n }\n\n public addDropdownItem(button: DropdownToolButtonItem, idx?: number) {\n if (!isEmptyOrNull(button.key)) {\n if (this.itemDisablingState.map(x => x.key).indexOf(button.key) > -1) {\n alert(`Dropdown has existed key: ${button.key}`)\n return\n }\n\n this.setDisablingStateItem(button.key, button.disabled || false)\n }\n\n let dropdownItemElement: JQuery\n\n if (button.isDropdownHeader && !isEmptyOrNull(button.dropdownHeaderTitle)) {\n dropdownItemElement = $(\n `<li class=\"dropdown-header ${coalesce(button.cssClass, '')}\">${button.dropdownHeaderTitle}</li>`,\n )\n } else {\n if (button.isSeparator) {\n dropdownItemElement = $(`<li class=\"dropdown-divider\"></li>`)\n } else {\n dropdownItemElement = $(`<li class=\"${button.disabled ? 'disabled' : ''}\"\n title=\"${coalesce(button.hint, '')}\"\n data-idevs-key=\"${coalesce(button.key, '')}\">\n <a href=\"#\" class=\"${coalesce(button.cssClass, 'dropdown-item')}\">\n <i class=\"${coalesce(button.icon, '')}\"></i>\n ${button.title}\n </a>\n </li>`)\n\n dropdownItemElement.on('click', (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => {\n e.preventDefault()\n\n if (this.isDisabled) {\n return\n }\n\n let buttonIsDisabled = button.disabled\n\n if (!isEmptyOrNull(button.key)) {\n buttonIsDisabled = this.getDisablingStateItem(button.key)\n }\n if (buttonIsDisabled) {\n return\n }\n\n button.onClick(e)\n })\n }\n }\n\n if (idx === null || typeof idx === 'undefined') {\n this.element.find('.dropdown-menu').append(dropdownItemElement)\n return\n }\n\n if (idx <= 0) {\n this.element.find('.dropdown-menu').prepend(dropdownItemElement)\n return\n }\n\n const nbrOfButtons = this.element.find(`.dropdown-menu > li`).length\n\n if (idx > nbrOfButtons) {\n idx = nbrOfButtons\n }\n\n this.element.find(`.dropdown-menu > li:nth-child(${idx})`).after(dropdownItemElement)\n }\n\n public enableDropdown(enable: boolean) {\n const drd = this.element.find('.dropdown').first()\n if (drd) {\n if (enable) {\n if (drd.hasClass('disabled')) {\n drd.removeClass('disabled')\n }\n } else {\n if (!drd.hasClass('disabled')) {\n drd.addClass('disabled')\n }\n }\n }\n\n const drdToggle = this.element.find('.dropdown-toggle').first()\n if (drdToggle) {\n if (enable) {\n if (drdToggle.hasClass('disabled')) {\n drd.removeClass('disabled')\n } else {\n if (!drdToggle.hasClass('disabled')) {\n drdToggle.addClass('disabled')\n }\n }\n }\n }\n\n this.isDisabled = !enable\n }\n\n public enableDropdownItemByKey(key: string, enable: boolean) {\n const drdItem = this.element.find(`.dropdown-menu li[data-idevs-key=\"${key}\"]`).first()\n if (drdItem) {\n if (enable) {\n if (drdItem.hasClass('disabled')) {\n drdItem.removeClass('disabled')\n }\n } else {\n if (!drdItem.hasClass('disabled')) {\n drdItem.addClass('disabled')\n }\n }\n }\n\n this.setDisablingStateItem(key, !enable)\n }\n\n public enableSideButtonByKey(key: string, enable: boolean) {\n const tButton = this.element.find(`.tool-button[data-idevs-key=\"${key}\"]`).first()\n if (tButton) {\n if (enable) {\n if (tButton.hasClass('disabled')) {\n tButton.removeClass('disabled')\n }\n } else {\n if (!tButton.hasClass('disabled')) {\n tButton.addClass('disabled')\n }\n }\n }\n\n this.setDisablingStateItem(key, !enable)\n }\n\n public removeDropdownItem(key: string) {\n this.element.find(`.dropdown-menu li[data-idevs-key=\"${key}\"]`).remove()\n this.removeDisablingStateItem(key)\n }\n\n public removeSideButtonItem(key: string) {\n this.element.find(`.tool-button[data-idevs-key=\"${key}\"]`).remove()\n this.removeDisablingStateItem(key)\n }\n\n public addSideButtonItem(button: ToolDropdownSideButtonItem, idx?: number) {\n if (!isEmptyOrNull(button.key)) {\n if (this.itemDisablingState.map(x => x.key).indexOf(button.key) > -1) {\n alert(`Dropdown has existed key: ${button.key}`)\n return\n }\n\n this.setDisablingStateItem(button.key, button.disabled || false)\n }\n\n const sideButtonTemplate = `<div class=\"tool-button add-button icon-tool-button ${coalesce(button.cssClass, '')} ${\n button.disabled ? 'disabled' : ''\n }\"\n data-idevs-key=\"${coalesce(button.key, '')}\"\n title=\"${coalesce(button.title, '')}\">\n <div class=\"button-outer\">\n <span class=\"button-inner\">\n <i class=\"${coalesce(button.icon, '')}\"></i>\n </span>\n </div>\n </div>`\n\n const sideButton = $(sideButtonTemplate)\n\n sideButton.on('click', (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => {\n e.preventDefault()\n\n let buttonIsDisabled = button.disabled\n\n if (!isEmptyOrNull(button.key)) {\n buttonIsDisabled = this.getDisablingStateItem(button.key)\n }\n\n if (buttonIsDisabled) {\n return\n }\n\n button.onClick(e)\n })\n\n if (idx === null || typeof idx === 'undefined') {\n this.element.append(sideButton)\n return\n }\n\n if (idx <= 0) {\n this.element.prepend(sideButton)\n return\n }\n\n const nbrOfButtons = this.element.find(`div.tool-button`).length\n\n if (idx > nbrOfButtons) {\n idx = nbrOfButtons\n }\n\n this.element.find(`div.tool-button:nth-child(${idx})`).after(sideButton)\n }\n}\n"]}
@@ -0,0 +1,18 @@
1
+ /// <reference types="jquery" />
2
+ export type ToggleToolButtonOptions = {
3
+ title?: string;
4
+ hint?: string;
5
+ cssClass?: string;
6
+ altCssClass?: string;
7
+ icon?: string;
8
+ altIcon?: string;
9
+ onClick?: (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => void;
10
+ disabled?: boolean;
11
+ };
12
+ export declare class ToggleToolButton {
13
+ element: JQuery;
14
+ private isDisabled;
15
+ private options;
16
+ constructor(container: JQuery, opt?: ToggleToolButtonOptions);
17
+ private buildBaseButton;
18
+ }
@@ -0,0 +1,53 @@
1
+ import { coalesce } from '@serenity-is/corelib/q';
2
+ import { neededTarget } from '../globals';
3
+ export class ToggleToolButton {
4
+ constructor(container, opt) {
5
+ this.element = null;
6
+ this.isDisabled = false;
7
+ this.options = opt || {};
8
+ this.isDisabled = this.options.disabled || false;
9
+ this.element = this.buildBaseButton();
10
+ this.element.on('click', e => {
11
+ if (this.isDisabled)
12
+ return;
13
+ const target = neededTarget(e.target, '.idevs-toggle-button');
14
+ if (this.options.altIcon) {
15
+ const icon = target.querySelector(`i`);
16
+ if (icon.className == this.options.icon) {
17
+ icon.className = this.options.altIcon;
18
+ }
19
+ else {
20
+ icon.className = this.options.icon;
21
+ }
22
+ }
23
+ if (this.options.cssClass && this.options.altCssClass) {
24
+ if (target.className.includes(this.options.cssClass)) {
25
+ target.className = target.className.replace(this.options.cssClass, this.options.altCssClass);
26
+ }
27
+ else {
28
+ target.className = target.className.replace(this.options.altCssClass, this.options.cssClass);
29
+ }
30
+ }
31
+ if (this.options.onClick) {
32
+ this.options.onClick(e);
33
+ }
34
+ });
35
+ container.append(this.element);
36
+ }
37
+ buildBaseButton() {
38
+ const buttonTemplate = `<div class="buttons-inner" style="overflow: visible">
39
+ <div class="idevs-toggle-button tool-button icon-tool-button ${coalesce(this.options.cssClass, '')} ${this.isDisabled ? 'disabled' : ''}" style="cursor: unset;">
40
+ <div class="button-outer ${this.isDisabled ? 'disabled' : ''}"
41
+ style="cursor: pointer;">
42
+ <span class="button-inner">
43
+ <i class="${this.options.icon}"></i>
44
+ ${coalesce(this.options.title, '')}
45
+ </span>
46
+ <i class="caret"></i>
47
+ </div>
48
+ </div>
49
+ </div>`;
50
+ return $(buttonTemplate);
51
+ }
52
+ }
53
+ //# sourceMappingURL=ToggleToolButton.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ToggleToolButton.js","sourceRoot":"","sources":["../../src/ui/ToggleToolButton.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAazC,MAAM,OAAO,gBAAgB;IAK3B,YAAmB,SAAiB,EAAE,GAA6B;QAJ5D,YAAO,GAAW,IAAI,CAAA;QACrB,eAAU,GAAG,KAAK,CAAA;QAIxB,IAAI,CAAC,OAAO,GAAG,GAAG,IAAI,EAAE,CAAA;QACxB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,IAAI,KAAK,CAAA;QAChD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,eAAe,EAAE,CAAA;QAErC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,OAA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AAM,cAAc,GAAG;mEACwC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,IAChG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EACjC;mCAC+B,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE;;;4BAGxC,IAAI,CAAC,OAAO,CAAC,IAAI;kBAC3B,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;;;;;OAK3C,CAAA;QAEH,OAAO,CAAC,CAAC,cAAc,CAAC,CAAA;IAC1B,CAAC;CACF","sourcesContent":["import { coalesce } from '@serenity-is/corelib/q'\nimport { neededTarget } from '../globals'\n\nexport type ToggleToolButtonOptions = {\n title?: string\n hint?: string\n cssClass?: string\n altCssClass?: string\n icon?: string\n altIcon?: string\n onClick?: (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => void\n disabled?: boolean\n}\n\nexport class ToggleToolButton {\n public element: JQuery = null\n private isDisabled = false\n private options: ToggleToolButtonOptions\n\n public constructor(container: JQuery, opt?: ToggleToolButtonOptions) {\n this.options = opt || {}\n this.isDisabled = this.options.disabled || false\n this.element = this.buildBaseButton()\n\n this.element.on('click', e => {\n if (this.isDisabled) return\n\n const target = neededTarget(e.target as HTMLElement, '.idevs-toggle-button')\n if (this.options.altIcon) {\n const icon = target.querySelector(`i`)\n if (icon.className == this.options.icon) {\n icon.className = this.options.altIcon\n } else {\n icon.className = this.options.icon\n }\n }\n\n if (this.options.cssClass && this.options.altCssClass) {\n if (target.className.includes(this.options.cssClass)) {\n target.className = target.className.replace(this.options.cssClass, this.options.altCssClass)\n } else {\n target.className = target.className.replace(this.options.altCssClass, this.options.cssClass)\n }\n }\n\n if (this.options.onClick) {\n this.options.onClick(e)\n }\n })\n\n container.append(this.element)\n }\n\n private buildBaseButton(): JQuery {\n const buttonTemplate = `<div class=\"buttons-inner\" style=\"overflow: visible\">\n <div class=\"idevs-toggle-button tool-button icon-tool-button ${coalesce(this.options.cssClass, '')} ${\n this.isDisabled ? 'disabled' : ''\n }\" style=\"cursor: unset;\">\n <div class=\"button-outer ${this.isDisabled ? 'disabled' : ''}\"\n style=\"cursor: pointer;\">\n <span class=\"button-inner\">\n <i class=\"${this.options.icon}\"></i>\n ${coalesce(this.options.title, '')}\n </span>\n <i class=\"caret\"></i>\n </div>\n </div>\n</div>`\n\n return $(buttonTemplate)\n }\n}\n"]}
@@ -1 +1,2 @@
1
- export * from './ToolDropdownButton';
1
+ export * from './DropdownToolButton';
2
+ export * from './ToggleToolButton';
package/dist/ui/index.js CHANGED
@@ -1,2 +1,3 @@
1
- export * from './ToolDropdownButton';
1
+ export * from './DropdownToolButton';
2
+ export * from './ToggleToolButton';
2
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ui/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA","sourcesContent":["export * from './ToolDropdownButton'\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ui/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,oBAAoB,CAAA","sourcesContent":["export * from './DropdownToolButton'\nexport * from './ToggleToolButton'\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idevs/corelib",
3
- "version": "0.0.39",
3
+ "version": "0.0.40",
4
4
  "description": "Extended library for Serenity Framework",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { coalesce, first, isEmptyOrNull, tryFirst } from '@serenity-is/corelib/q'
4
4
 
5
- export type ToolDropdownButtonOptions = {
5
+ export type DropdownToolButtonOptions = {
6
6
  title?: string
7
7
  cssClass?: string
8
8
  icon?: string
@@ -11,7 +11,7 @@ export type ToolDropdownButtonOptions = {
11
11
  isDropUp?: boolean
12
12
  }
13
13
 
14
- export type ToolDropdownButtonItem = {
14
+ export type DropdownToolButtonItem = {
15
15
  key: string
16
16
  title?: string
17
17
  hint?: string
@@ -34,13 +34,13 @@ export type ToolDropdownSideButtonItem = {
34
34
  disabled?: boolean
35
35
  }
36
36
 
37
- export class ToolDropdownButton {
37
+ export class DropdownToolButton {
38
38
  public element: JQuery = null
39
39
  private isDisabled = false
40
40
  private itemDisablingState: { key: string; disabled: boolean }[] = []
41
- private options: ToolDropdownButtonOptions
41
+ private options: DropdownToolButtonOptions
42
42
 
43
- public constructor(container: JQuery, buttons: ToolDropdownButtonItem[], opt?: ToolDropdownButtonOptions) {
43
+ public constructor(container: JQuery, buttons: DropdownToolButtonItem[], opt?: DropdownToolButtonOptions) {
44
44
  this.options = opt || {}
45
45
  this.isDisabled = this.options.disabled || false
46
46
  this.element = this.buildBaseDropdown()
@@ -101,7 +101,7 @@ export class ToolDropdownButton {
101
101
  return $(dropdownTemplate)
102
102
  }
103
103
 
104
- public addDropdownItems(buttons: ToolDropdownButtonItem[]) {
104
+ public addDropdownItems(buttons: DropdownToolButtonItem[]) {
105
105
  if (buttons && buttons.length > 0) {
106
106
  buttons.forEach(button => {
107
107
  this.addDropdownItem(button)
@@ -109,7 +109,7 @@ export class ToolDropdownButton {
109
109
  }
110
110
  }
111
111
 
112
- public addDropdownItem(button: ToolDropdownButtonItem, idx?: number) {
112
+ public addDropdownItem(button: DropdownToolButtonItem, idx?: number) {
113
113
  if (!isEmptyOrNull(button.key)) {
114
114
  if (this.itemDisablingState.map(x => x.key).indexOf(button.key) > -1) {
115
115
  alert(`Dropdown has existed key: ${button.key}`)
@@ -0,0 +1,72 @@
1
+ import { coalesce } from '@serenity-is/corelib/q'
2
+ import { neededTarget } from '../globals'
3
+
4
+ export type ToggleToolButtonOptions = {
5
+ title?: string
6
+ hint?: string
7
+ cssClass?: string
8
+ altCssClass?: string
9
+ icon?: string
10
+ altIcon?: string
11
+ onClick?: (e: JQuery.ClickEvent<HTMLElement, null, HTMLElement, HTMLElement>) => void
12
+ disabled?: boolean
13
+ }
14
+
15
+ export class ToggleToolButton {
16
+ public element: JQuery = null
17
+ private isDisabled = false
18
+ private options: ToggleToolButtonOptions
19
+
20
+ public constructor(container: JQuery, opt?: ToggleToolButtonOptions) {
21
+ this.options = opt || {}
22
+ this.isDisabled = this.options.disabled || false
23
+ this.element = this.buildBaseButton()
24
+
25
+ this.element.on('click', e => {
26
+ if (this.isDisabled) return
27
+
28
+ const target = neededTarget(e.target as HTMLElement, '.idevs-toggle-button')
29
+ if (this.options.altIcon) {
30
+ const icon = target.querySelector(`i`)
31
+ if (icon.className == this.options.icon) {
32
+ icon.className = this.options.altIcon
33
+ } else {
34
+ icon.className = this.options.icon
35
+ }
36
+ }
37
+
38
+ if (this.options.cssClass && this.options.altCssClass) {
39
+ if (target.className.includes(this.options.cssClass)) {
40
+ target.className = target.className.replace(this.options.cssClass, this.options.altCssClass)
41
+ } else {
42
+ target.className = target.className.replace(this.options.altCssClass, this.options.cssClass)
43
+ }
44
+ }
45
+
46
+ if (this.options.onClick) {
47
+ this.options.onClick(e)
48
+ }
49
+ })
50
+
51
+ container.append(this.element)
52
+ }
53
+
54
+ private buildBaseButton(): JQuery {
55
+ const buttonTemplate = `<div class="buttons-inner" style="overflow: visible">
56
+ <div class="idevs-toggle-button tool-button icon-tool-button ${coalesce(this.options.cssClass, '')} ${
57
+ this.isDisabled ? 'disabled' : ''
58
+ }" style="cursor: unset;">
59
+ <div class="button-outer ${this.isDisabled ? 'disabled' : ''}"
60
+ style="cursor: pointer;">
61
+ <span class="button-inner">
62
+ <i class="${this.options.icon}"></i>
63
+ ${coalesce(this.options.title, '')}
64
+ </span>
65
+ <i class="caret"></i>
66
+ </div>
67
+ </div>
68
+ </div>`
69
+
70
+ return $(buttonTemplate)
71
+ }
72
+ }
package/src/ui/index.ts CHANGED
@@ -1 +1,2 @@
1
- export * from './ToolDropdownButton'
1
+ export * from './DropdownToolButton'
2
+ export * from './ToggleToolButton'