cedro 0.1.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.
@@ -0,0 +1,398 @@
1
+ import { WUICallback } from "../interfaces/widget.interface";
2
+ import { Button } from "./button.ui";
3
+ import { Label } from "./label.ui";
4
+ import "./styles/dialog.css";
5
+ import { Widget, WidgetAlignTypes, WidgetTypes } from "./widget.ui";
6
+
7
+ const TITLE_BAR_HEIGHT = 40;
8
+ const BUTTON_BAR_HEIGHT = 50;
9
+ const HANDLER_SIZE = 20;
10
+ const MINUMUN_WIDTH = 300;
11
+ const MINUMUN_HEIGHT = 200;
12
+
13
+ export class Dialog extends Widget {
14
+ background: Widget;
15
+
16
+ titleContainer: Widget;
17
+ contentCntainer: Widget;
18
+ buttonContainer: Widget | null;
19
+ handlerResizable: Widget | null;
20
+
21
+ titleBar: Label;
22
+ close: Button;
23
+
24
+ ok: Button | null;
25
+ cancell: Button | null;
26
+ btnSpacerLeft: Widget | null;
27
+ btnSpacerRight: Widget | null;
28
+
29
+ titleBarHeight: number;
30
+ buttonBarHeight: number;
31
+
32
+ dragDistX: number;
33
+ dragDistY: number;
34
+ draging: boolean;
35
+ resizing: boolean;
36
+
37
+ constructor(
38
+ id: string,
39
+ parent: Widget | null = null,
40
+ hasButtons: boolean = true,
41
+ resizable: boolean = false
42
+ ) {
43
+ super(id, "div", parent);
44
+
45
+ this.buttonContainer = null;
46
+ this.handlerResizable = null;
47
+
48
+ this.ok = null;
49
+ this.cancell = null;
50
+ this.btnSpacerLeft = null;
51
+ this.btnSpacerRight = null;
52
+
53
+ this.dragDistX = 0;
54
+ this.dragDistY = 0;
55
+ this.draging = false;
56
+ this.resizing = false;
57
+
58
+ this.background = new Widget(this.id + ".background", "div", null);
59
+ this.background.setType(WidgetTypes.CUSTOM);
60
+ this.background.addClass("WUIDialogBackground");
61
+ this.background.setVisible(false);
62
+
63
+ this.setWH(400, 200);
64
+
65
+ this.addClass("WUIDialog");
66
+ this.addClass("WUIDialogHide");
67
+
68
+ this.titleBarHeight = TITLE_BAR_HEIGHT;
69
+ this.buttonBarHeight = BUTTON_BAR_HEIGHT;
70
+
71
+ this.setType(WidgetTypes.CUSTOM);
72
+ this.setAlign(WidgetAlignTypes.VERTICAL);
73
+
74
+ this.titleContainer = new Widget(
75
+ this.id + ".titleContainer",
76
+ "div",
77
+ this
78
+ );
79
+ this.contentCntainer = new Widget(
80
+ this.id + ".contentCntainer",
81
+ "div",
82
+ this
83
+ );
84
+
85
+ if (hasButtons) {
86
+ this.buttonContainer = new Widget(
87
+ this.id + ".buttonContainer",
88
+ "div",
89
+ this
90
+ );
91
+
92
+ this.buttonContainer.setType(WidgetTypes.FILL);
93
+ this.buttonContainer.setAlign(WidgetAlignTypes.HORIZONTAL);
94
+ this.buttonContainer.setPadding(5);
95
+ this.buttonContainer.setFixedSize(BUTTON_BAR_HEIGHT);
96
+ }
97
+
98
+ if (resizable) {
99
+ this.handlerResizable = new Widget(
100
+ this.id + ".handlerResizable",
101
+ "div",
102
+ this
103
+ );
104
+ this.handlerResizable.setType(WidgetTypes.CUSTOM);
105
+ this.handlerResizable.getBody().innerHTML = " ";
106
+ this.handlerResizable.addClass("WUIDialogResizeHandler");
107
+ }
108
+
109
+ this.titleContainer.setType(WidgetTypes.FILL);
110
+ this.contentCntainer.setType(WidgetTypes.FILL);
111
+
112
+ this.titleContainer.setAlign(WidgetAlignTypes.HORIZONTAL);
113
+ this.contentCntainer.setAlign(WidgetAlignTypes.VERTICAL);
114
+
115
+ this.titleContainer.setPadding(5);
116
+ this.contentCntainer.setPadding(20);
117
+
118
+ this.titleContainer.setFixedSize(TITLE_BAR_HEIGHT);
119
+
120
+ this.titleBar = new Label(
121
+ this.id + ".titleBar",
122
+ "span",
123
+ this.titleContainer
124
+ );
125
+ this.titleBar.setType(WidgetTypes.FILL);
126
+ this.titleBar.setText("Title");
127
+ this.titleBar.addClass("WUITitlebar");
128
+
129
+ this.close = new Button(this.id + ".close", this.titleContainer);
130
+ this.close.setType(WidgetTypes.FILL);
131
+ this.close.setColor("primary");
132
+ this.close.setVariant("text");
133
+ this.close.setText("X");
134
+ this.close.setFixedSize(40);
135
+
136
+ this.btnSpacerLeft = new Widget(
137
+ this.id + ".btnSpacerLeft",
138
+ "div",
139
+ this.buttonContainer
140
+ );
141
+ this.btnSpacerLeft.setType(WidgetTypes.FILL);
142
+ this.btnSpacerLeft.setAlign(WidgetAlignTypes.HORIZONTAL);
143
+
144
+ if (this.buttonContainer) {
145
+ this.cancell = new Button(
146
+ this.id + ".cancell",
147
+ this.buttonContainer
148
+ );
149
+ this.cancell.setType(WidgetTypes.FILL);
150
+ this.cancell.setColor("error");
151
+ this.cancell.setText("Cancel");
152
+ this.cancell.setVariant("contained");
153
+ this.cancell.setFixedSize(100);
154
+
155
+ this.ok = new Button(this.id + ".ok", this.buttonContainer);
156
+ this.ok.setType(WidgetTypes.FILL);
157
+ this.ok.setText("OK");
158
+ this.ok.setVariant("contained");
159
+ this.ok.setColor("success");
160
+ this.ok.setFixedSize(100);
161
+
162
+ this.btnSpacerRight = new Widget(
163
+ this.id + ".btnSpacerRight",
164
+ "div",
165
+ this.buttonContainer
166
+ );
167
+ this.btnSpacerRight.setType(WidgetTypes.FILL);
168
+ this.btnSpacerRight.setAlign(WidgetAlignTypes.HORIZONTAL);
169
+
170
+ this.ok.subscribe({
171
+ event: "click",
172
+ then: () => {
173
+ this.hide();
174
+ },
175
+ });
176
+
177
+ this.cancell.subscribe({
178
+ event: "click",
179
+ then: () => {
180
+ this.hide();
181
+ },
182
+ });
183
+ }
184
+
185
+ this.raisteTop();
186
+
187
+ this.close.subscribe({
188
+ event: "click",
189
+ then: () => {
190
+ this.hide();
191
+ },
192
+ });
193
+
194
+ this.titleBar.subscribe({
195
+ event: "mousedown",
196
+ then: (e: any) => {
197
+ e.preventDefault();
198
+ const mouseX = (e as MouseEvent).clientX;
199
+ const mouseY = (e as MouseEvent).clientY;
200
+ this.dragDistX = Math.abs(this.getX() - mouseX);
201
+ this.dragDistY = Math.abs(this.getY() - mouseY);
202
+ this.draging = true;
203
+ this.background.setVisible(true);
204
+ this.background.raisteTop();
205
+ this.background.deleteClass("WUIDialogBackgroundReisizing");
206
+ this.background.addClass("WUIDialogBackgroundDragging");
207
+ },
208
+ });
209
+
210
+ this.background.subscribe({
211
+ event: "mouseup",
212
+ then: (e: any) => {
213
+ e.preventDefault();
214
+ this.draging = false;
215
+ this.resizing = false;
216
+ this.background.setVisible(false);
217
+ this.raisteTop();
218
+ },
219
+ });
220
+
221
+ this.background.subscribe({
222
+ event: "mousemove",
223
+ then: (e: any) => {
224
+ e.preventDefault();
225
+
226
+ if (this.draging) {
227
+ const mouseX = (e as MouseEvent).clientX;
228
+ const mouseY = (e as MouseEvent).clientY;
229
+ this.setX(mouseX - this.dragDistX);
230
+ this.setY(mouseY - this.dragDistY);
231
+ } else if (this.resizing) {
232
+ const mouseX = (e as MouseEvent).clientX;
233
+ const mouseY = (e as MouseEvent).clientY;
234
+
235
+ let newWidth = Math.abs(mouseX - this.getPosition().x);
236
+ let newHeight = Math.abs(mouseY - this.getPosition().y);
237
+
238
+ if (newWidth < MINUMUN_WIDTH) newWidth = MINUMUN_WIDTH;
239
+ if (newHeight < MINUMUN_HEIGHT) newHeight = MINUMUN_HEIGHT;
240
+
241
+ this.setWH(newWidth, newHeight);
242
+
243
+ this.render();
244
+ }
245
+
246
+ return;
247
+ },
248
+ });
249
+
250
+ /**RESIZE HANDLER **/
251
+ if (this.handlerResizable) {
252
+ this.handlerResizable.subscribe({
253
+ event: "mousedown",
254
+ then: (e: any) => {
255
+ e.preventDefault();
256
+ if (!this.handlerResizable) return;
257
+ const mouseX = (e as MouseEvent).clientX;
258
+ const mouseY = (e as MouseEvent).clientY;
259
+ this.dragDistX = Math.abs(
260
+ this.handlerResizable.getPosition().x - mouseX
261
+ );
262
+ this.dragDistY = Math.abs(
263
+ this.handlerResizable.getPosition().y - mouseY
264
+ );
265
+
266
+ this.resizing = true;
267
+ this.background.setVisible(true);
268
+ this.background.raisteTop();
269
+ this.background.deleteClass("WUIDialogBackgroundDragging");
270
+ this.background.addClass("WUIDialogBackgroundReisizing");
271
+ },
272
+ });
273
+ }
274
+
275
+ this.init();
276
+ }
277
+
278
+ /**
279
+ * Renders the component and its resizable handler.
280
+ *
281
+ * @return {void}
282
+ */
283
+ public render(): void {
284
+ super.render();
285
+ if (this.handlerResizable) {
286
+ this.handlerResizable.setX(this.getW() - HANDLER_SIZE);
287
+ this.handlerResizable.setY(this.getH() - HANDLER_SIZE);
288
+ this.handlerResizable.setW(HANDLER_SIZE);
289
+ this.handlerResizable.setH(HANDLER_SIZE);
290
+ this.handlerResizable.raisteTop();
291
+ this.handlerResizable.render();
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Centers the element on the screen.
297
+ *
298
+ * @param {void} - No parameters.
299
+ * @return {void} - No return value.
300
+ */
301
+ public center(): void {
302
+ const ancho = this.getW();
303
+ const alto = this.getH();
304
+
305
+ const screenWidth = window.innerWidth;
306
+ const screenHeight = window.innerHeight;
307
+
308
+ const centerX = screenWidth / 2 - ancho / 2;
309
+ const centerY = screenHeight / 2 - alto / 2;
310
+
311
+ this.setX(centerX);
312
+ this.setY(centerY);
313
+ }
314
+
315
+ /**
316
+ * Shows the dialog.
317
+ *
318
+ * @return {void} No return value.
319
+ */
320
+ public show(): void {
321
+ //this.setWH(400, 200);
322
+ this.deleteClass("WUIDialogHide");
323
+ this.addClass("WUIDialogDisplayed");
324
+ this.render();
325
+ this.center();
326
+
327
+ this.background.setVisible(true);
328
+ this.background.raisteTop();
329
+
330
+ this.raisteTop();
331
+ }
332
+
333
+ /**
334
+ * Hides the dialog.
335
+ *
336
+ * This function removes the "WUIDialogDisplayed" class from the element,
337
+ * adds the "WUIDialogHide" class to the element, sets the background element
338
+ * to be invisible, and raises the dialog element to the bottom of the z-index.
339
+ */
340
+ public hide(): void {
341
+ this.deleteClass("WUIDialogDisplayed");
342
+ this.addClass("WUIDialogHide");
343
+ this.background.setVisible(false);
344
+ this.background.raiseBottom();
345
+ this.raiseBottom();
346
+ }
347
+
348
+ init() {
349
+ super.init();
350
+ }
351
+
352
+ /**
353
+ * Sets the height of the title bar.
354
+ *
355
+ * @param {number} height - The height of the title bar.
356
+ */
357
+ setTitlebarHeight(height: number) {
358
+ this.titleBarHeight = height;
359
+ }
360
+
361
+ /**
362
+ * Set the height of the button bar.
363
+ *
364
+ * @param {number} height - The height of the button bar.
365
+ */
366
+ setButtonbarHeight(height: number) {
367
+ this.buttonBarHeight = height;
368
+ }
369
+
370
+ /**
371
+ * Retrieves the content container widget.
372
+ *
373
+ * @return {Widget} The content container widget.
374
+ */
375
+ public getContentCntainer(): Widget {
376
+ return this.contentCntainer;
377
+ }
378
+
379
+ /**
380
+ * Sets the ok callback function.
381
+ *
382
+ * @param {WUICallback} cb - The callback function to be executed when the "ok" button is clicked.
383
+ */
384
+ public setOkCallback(cb: WUICallback) {
385
+ if (!this.ok) return;
386
+ this.ok.subscribe({ event: "click", then: cb.then });
387
+ }
388
+
389
+ /**
390
+ * Sets the cancellation callback for the function.
391
+ *
392
+ * @param {WUICallback} cb - The cancellation callback to set.
393
+ */
394
+ public setCancellCallback(cb: WUICallback) {
395
+ if (!this.cancell) return;
396
+ this.cancell.subscribe({ event: "click", then: cb.then });
397
+ }
398
+ }
@@ -0,0 +1,24 @@
1
+ import { Button } from "./button.ui"
2
+ import { Widget } from "./widget.ui"
3
+ import { Dialog } from "./dialog"
4
+ import { Icon } from "./Icon.ui"
5
+ import { IconButton } from "./IconButton.ui"
6
+ import { Label } from "./label.ui"
7
+ import { Menu } from "./menu.ui"
8
+ import { Select } from "./select.ui"
9
+ import { Textbox } from "./textbox.ui"
10
+ import {createWidget} from "./widget.builder.ui"
11
+
12
+
13
+ export{
14
+ Button,
15
+ Widget,
16
+ Dialog,
17
+ Icon,
18
+ IconButton,
19
+ Label,
20
+ Menu,
21
+ Select,
22
+ Textbox,
23
+ createWidget,
24
+ }
@@ -0,0 +1,61 @@
1
+ import { Colors } from "./colors.ui";
2
+ import { Widget } from "./widget.ui";
3
+
4
+ export type LabelVariants =
5
+ | "h1"
6
+ | "h2"
7
+ | "h3"
8
+ | "h4"
9
+ | "h5"
10
+ | "h6"
11
+ | "p"
12
+ | "span";
13
+
14
+ export class Label extends Widget {
15
+ variant: LabelVariants;
16
+ color: Colors;
17
+ text: string;
18
+
19
+ constructor(
20
+ id: string,
21
+ variant: LabelVariants = "span",
22
+ parent: Widget | null = null
23
+ ) {
24
+ super(id, variant, parent);
25
+
26
+ this.variant = variant;
27
+ this.color = "primary";
28
+ this.text = "";
29
+
30
+ this.init();
31
+ }
32
+
33
+ public init(): void {
34
+ super.init();
35
+ }
36
+
37
+ setText(text: string): void {
38
+ this.text = text;
39
+ this.body.innerHTML = text;
40
+ }
41
+
42
+ setVariant(variant: LabelVariants = "span"): void {
43
+ this.variant = variant;
44
+ }
45
+
46
+ setColor(color: Colors = "primary"): void {
47
+ this.color = color;
48
+ }
49
+
50
+ getVariant(): LabelVariants {
51
+ return this.variant;
52
+ }
53
+
54
+ getColor(): Colors {
55
+ return this.color;
56
+ }
57
+
58
+ getText(): string {
59
+ return this.text;
60
+ }
61
+ }
@@ -0,0 +1,192 @@
1
+ import "./styles/menu.css";
2
+ import { IWidget } from "../interfaces/widget.interface";
3
+ import { IconButton } from "./IconButton.ui";
4
+ import { Widget, WidgetAlignTypes, WidgetTypes } from "./widget.ui";
5
+
6
+ export class Menu extends Widget {
7
+ background: Widget;
8
+ options: Map<string, IconButton>;
9
+ triggeredById: string | null;
10
+ triggeredBy: IWidget | null;
11
+ triggeredBySearchCode: any;
12
+ withCalculation: boolean;
13
+ constructor(
14
+ id: string,
15
+ trigeredById: string | null,
16
+ parent: IWidget | null = null
17
+ ) {
18
+ super(id, "div", parent);
19
+
20
+ this.background = new Widget(this.id + ".background", "div", null);
21
+ this.background.setType(WidgetTypes.CUSTOM);
22
+ this.background.addClass("WUIMenuBackground");
23
+ this.background.setVisible(false);
24
+
25
+ this.triggeredById = trigeredById;
26
+ this.setType(WidgetTypes.CUSTOM);
27
+
28
+ this.triggeredBy = null;
29
+
30
+ this.options = new Map<string, IconButton>();
31
+
32
+ this.addClass("WUIMenu");
33
+ this.addClass("WUIMenuTransparent");
34
+
35
+ this.withCalculation = false;
36
+
37
+ this.triggeredBySearchCode = setInterval(() => {
38
+ if (!this.triggeredById) {
39
+ clearInterval(this.triggeredBySearchCode);
40
+ return;
41
+ }
42
+ if (window.w.get(this.triggeredById)) {
43
+ this.triggeredBy = window.w.get(this.triggeredById) as IWidget;
44
+
45
+ this.triggeredBy.subscribe({
46
+ event: "click",
47
+ then: () => {
48
+ this.wakeUp();
49
+ },
50
+ });
51
+ clearInterval(this.triggeredBySearchCode);
52
+ }
53
+ }, 500);
54
+
55
+ this.background.subscribe({
56
+ event: "click",
57
+ then: () => {
58
+ this.close();
59
+ },
60
+ });
61
+ }
62
+
63
+ public close(): void {
64
+ this.deleteClass("WUIMenuVisible");
65
+ this.addClass("WUIMenuHidden");
66
+ this.background.setVisible(false);
67
+ }
68
+
69
+ private setFreeOptionWidth(): void {
70
+ for (const [, dataOption] of this.options) {
71
+ const option = dataOption as IconButton;
72
+ option.deleteClass("WUIMenuOptions100w");
73
+ }
74
+ }
75
+
76
+ private getMaxWidth(): number {
77
+ this.setFreeOptionWidth();
78
+ this.deleteClass("WUIMenuHidden");
79
+ this.addClass("WUIMenuTransparent");
80
+ let maxWidth = 0;
81
+ for (const [, dataOption] of this.options) {
82
+ const option = dataOption as IconButton;
83
+ const optionWidth = option.getBody().clientWidth;
84
+
85
+ if (optionWidth > maxWidth) {
86
+ maxWidth = optionWidth;
87
+ }
88
+ }
89
+ return maxWidth;
90
+ }
91
+
92
+ wakeUp(): void {
93
+ if (!this.withCalculation) {
94
+ let maxWidth = this.getMaxWidth();
95
+
96
+ for (const [, dataOption] of this.options) {
97
+ const option = dataOption as IconButton;
98
+ option.addClass("WUIMenuOptions100w");
99
+ }
100
+ this.deleteClass("WUIMenuTransparent");
101
+ this.addClass("WUIMenuHidden");
102
+ this.setW(maxWidth);
103
+ this.withCalculation = true;
104
+ }
105
+
106
+ this.background.setVisible(true);
107
+ this.background.raisteTop();
108
+
109
+ this.raisteTop();
110
+
111
+ this.deleteClass("WUIMenuHidden");
112
+ this.addClass("WUIMenuVisible");
113
+
114
+ if (this.triggeredBy) {
115
+ const position = this.triggeredBy.getPosition(false);
116
+
117
+ const triggerW = this.triggeredBy.getBody().clientWidth;
118
+ const triggerH = this.triggeredBy.getBody().clientHeight;
119
+
120
+ const screenW = window.innerWidth;
121
+ const screenH = window.innerHeight;
122
+ const menuW = this.getBody().clientWidth;
123
+ const menuH = this.getBody().clientHeight;
124
+ let positionX = position.x;
125
+ let positionY = position.y;
126
+
127
+ let openRight = true;
128
+ let openBottom = true;
129
+
130
+ if (position.x + menuW + triggerW > screenW) {
131
+ positionX = screenW - menuW - triggerW;
132
+ openRight = false;
133
+ }
134
+
135
+ if (position.y + menuH + triggerH > screenH) {
136
+ positionY = screenH - menuH - triggerH;
137
+ openBottom = false;
138
+ }
139
+
140
+ if (openRight && openBottom) {
141
+ this.setX(positionX);
142
+ this.setY(positionY + triggerH);
143
+ } else if (openRight && !openBottom) {
144
+ this.setX(positionX + triggerW);
145
+ this.setY(positionY);
146
+ } else if (!openRight && openBottom) {
147
+ /*abajo izq: Works!*/
148
+ this.setX(positionX + triggerW);
149
+ this.setY(positionY + triggerH);
150
+ } else if (!openRight && !openBottom) {
151
+ this.setX(positionX + triggerW);
152
+ this.setY(positionY - triggerH);
153
+ }
154
+
155
+ //this.setY(positionY);
156
+ }
157
+ }
158
+
159
+ public optionClicked(clickedOption: IWidget): void {
160
+ this.subscribers.forEach((callback) => {
161
+ if (callback.event == "option-clicked") {
162
+ callback.then(new Event("option-clicked"), clickedOption);
163
+ }
164
+ });
165
+ this.close();
166
+ }
167
+
168
+ public clearOptions(): void {
169
+ this.options.clear();
170
+ this.removeAllChilds();
171
+ this.withCalculation = false;
172
+ }
173
+
174
+ addOption(id: string, icon: string, label: string) {
175
+ const newOption = new IconButton(id, icon, this);
176
+ newOption.setType(WidgetTypes.FREE);
177
+ newOption.setAlign(WidgetAlignTypes.HORIZONTAL);
178
+ newOption.setText(label);
179
+ newOption.addClass("WUIMenuOptions");
180
+
181
+ newOption.subscribe({
182
+ event: "click",
183
+ then: (_e, a) => {
184
+ const option = a as IconButton;
185
+
186
+ this.optionClicked(option);
187
+ },
188
+ });
189
+
190
+ this.options.set(id, newOption);
191
+ }
192
+ }