@scalable.software/pin 0.2.0 → 0.2.1

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.
@@ -1,329 +1,318 @@
1
- /**
2
- * @module Component
3
- */
4
- import { Component, Template } from "@scalable.software/component";
5
- import { type Configuration } from "@scalable.software/component";
6
-
7
- import { Tag, Attribute, Visible, Status, Event, Gesture } from "./Pin.meta.js";
8
- import {
9
- type Attributes,
10
- type Visibility,
11
- type Statuses,
12
- type Events,
13
- type Handler,
14
- } from "./Pin.meta.js";
15
-
16
-
17
-
18
- /**
19
- * Configuration required for components with custom layout and style
20
- * @category Configuration
21
- */
22
- export const configuration: Configuration = {
23
- url: import.meta.url,
24
- template: {
25
- id: Tag,
26
- },
27
- css: {
28
- name: "Pin.style.css",
29
- },
30
- } as const;
31
-
32
- /**
33
- * A pin button that can be:
34
- * 1. pinned and unpinned
35
- * 2. hidden and shown
36
- * @category Components
37
- */
38
- export class Pin extends Component {
39
- /**
40
- * The tag name of the component
41
- * @category Configuration
42
- */
43
- public static get Tag() {
44
- return Tag;
45
- }
46
-
47
- /**
48
- * Only attributes defined the Attributes object will be observed in DOM
49
- * @category Configuration
50
- */
51
- public static get Attributes(): Attributes {
52
- return Attribute;
53
- }
54
-
55
- /**
56
- * Wait for the component to be defined before returning a collection of the component in the DOM
57
- * @category Utilities
58
- */
59
- public static get = async () =>
60
- (await customElements.whenDefined(Pin.Tag)) &&
61
- (Array.from(document.querySelectorAll(Pin.Tag)) as Pin[]);
62
-
63
- /**
64
- * Helper function to load the component template into DOM
65
- * @category Utilities
66
- */
67
- public static Template = new Template(import.meta.url);
68
-
69
- /**
70
- * Cache element references to improve performance
71
- * @category State
72
- * @hidden
73
- */
74
- protected elements: { icon: HTMLDivElement | null } = { icon: null };
75
-
76
- /**
77
- * Internal Visibility state of the component
78
- * @category State
79
- * @default Visible.YES
80
- * @hidden
81
- */
82
- private _visible: Visibility = Visible.YES;
83
-
84
- /**
85
- * Internal Status state of the component
86
- * @category State
87
- * @default
88
- * @hidden
89
- */
90
- private _status: Statuses = Status.UNPINNED;
91
-
92
- /**
93
- * on triggered when any event is triggered
94
- * @category Events
95
- * @hidden
96
- */
97
- private _on: Handler = null;
98
-
99
- /**
100
- * onhide triggered when pin visibility changes to hidden
101
- * @category Events
102
- * @hidden
103
- */
104
- private _onhide: Handler = null;
105
-
106
- /**
107
- * onshow triggered when pin visibility changes to visible
108
- * @category Events
109
- * @hidden
110
- */
111
- private _onshow: Handler = null;
112
-
113
- /**
114
- * onpin triggered when pin status changes to pinned
115
- * @category Events
116
- * @hidden
117
- */
118
- private _onpin: Handler = null;
119
-
120
- /**
121
- * onunpin triggered when pin state changes to unpinned
122
- * @category Events
123
- * @hidden
124
- */
125
- private _onunpin: Handler = null;
126
-
127
- constructor() {
128
- super(configuration);
129
- }
130
-
131
- /**
132
- * Get and Sets the visibility of the pin button
133
- * @category State
134
- */
135
- public get visible(): Visibility {
136
- return this.hasAttribute(Attribute.VISIBLE)
137
- ? (this.getAttribute(Attribute.VISIBLE) as Visibility)
138
- : this._visible;
139
- }
140
- public set visible(visible: Visibility) {
141
- visible = visible || Visible.YES;
142
- if (this._visible !== visible) {
143
- this._visible = visible;
144
- visible == Visible.YES && this.removeAttribute(Attribute.VISIBLE);
145
- visible == Visible.NO && this.setAttribute(Attribute.VISIBLE, visible);
146
-
147
- visible == Visible.NO &&
148
- this._dispatchEvent(Event.ON_HIDE, { detail: { visible } });
149
- visible == Visible.YES &&
150
- this._dispatchEvent(Event.ON_SHOW, { detail: { visible } });
151
- }
152
- }
153
-
154
- /**
155
- * Get and Sets the status of the pin button
156
- * @category State
157
- */
158
- public get status(): Statuses {
159
- return this.hasAttribute(Attribute.STATUS)
160
- ? (this.getAttribute(Attribute.STATUS) as Statuses)
161
- : this._status;
162
- }
163
- public set status(status: Statuses) {
164
- if (this._status !== status) {
165
- this._status = status;
166
- this.setAttribute(Attribute.STATUS, status);
167
-
168
- status == Status.PINNED &&
169
- this._dispatchEvent(Event.ON_PIN, { detail: { status } });
170
- status == Status.UNPINNED &&
171
- this._dispatchEvent(Event.ON_UNPIN, { detail: { status } });
172
- }
173
- }
174
-
175
- /**
176
- * Triggered on any event
177
- * @event
178
- * @category Events
179
- */
180
- public set on(handler: Handler) {
181
- Object
182
- .values(Event)
183
- .filter((event): event is Events => event !== Event.ON)
184
- .forEach((event: Events) => {
185
- this._on && this.removeEventListener(event, this._on);
186
- this._on = handler;
187
- this._on && this.addEventListener(event, this._on);
188
- });
189
- }
190
-
191
- /**
192
- * Triggered via `.hide()`
193
- * @event
194
- * @category Events
195
- */
196
- public set onhide(handler: Handler) {
197
- this._onhide && this.removeEventListener(Event.ON_HIDE, this._onhide);
198
- this._onhide = handler;
199
- this._onhide && this.addEventListener(Event.ON_HIDE, this._onhide);
200
- }
201
-
202
- /**
203
- * Triggered via `.show()`
204
- * @event
205
- * @category Events
206
- */
207
- public set onshow(handler: Handler) {
208
- this._onshow && this.removeEventListener(Event.ON_SHOW, this._onshow);
209
- this._onshow = handler;
210
- this._onshow && this.addEventListener(Event.ON_SHOW, this._onshow);
211
- }
212
-
213
- /**
214
- * Triggered via `.pin()`
215
- * @event
216
- * @category Events
217
- */
218
- public set onpin(handler: Handler) {
219
- this._onpin && this.removeEventListener(Event.ON_PIN, this._onpin);
220
- this._onpin = handler;
221
- this._onpin && this.addEventListener(Event.ON_PIN, this._onpin);
222
- }
223
-
224
- /**
225
- * Triggered via `.unpin()`
226
- * @event
227
- * @category Events
228
- */
229
- public set onunpin(handler: Handler) {
230
- this._onunpin && this.removeEventListener(Event.ON_UNPIN, this._onunpin);
231
- this._onunpin = handler;
232
- this._onunpin && this.addEventListener(Event.ON_UNPIN, this._onunpin);
233
- }
234
-
235
- /**
236
- * Hide the pin button when it is visible
237
- * @category Operations
238
- */
239
- public hide = () => (this.visible = Visible.NO);
240
-
241
- /**
242
- * Show the pin button when it is hidden
243
- * @category Operations
244
- */
245
- public show = () => (this.visible = Visible.YES);
246
-
247
- /**
248
- * Pin the pin button when it is unpinned
249
- * @category Operations
250
- */
251
- public pin = () => (this.status = Status.PINNED);
252
-
253
- /**
254
- * Unpin the pin button when it is pinned
255
- * @category Operations
256
- */
257
- public unpin = () => (this.status = Status.UNPINNED);
258
-
259
- /**
260
- * Toggle the pin button between pinned and unpinned
261
- * @category Operations
262
- */
263
- public toggle = () =>
264
- (this.status =
265
- this.status === Status.PINNED ? Status.UNPINNED : Status.PINNED);
266
-
267
- /**
268
- * List operations to perform for selected attributes being observed in the DOM.
269
- * @category Configuration
270
- * @hidden
271
- */
272
- protected _attributeHandlers = {
273
- [Attribute.VISIBLE]: (value) => (this.visible = value),
274
- [Attribute.STATUS]: (value) => (this.status = value),
275
- };
276
-
277
- /**
278
- * Initialize component attributes with default values
279
- * @category Configuration
280
- * @hidden
281
- */
282
- protected _initialize = () => this._initializeState();
283
-
284
- /**
285
- * Cache element references to improve performance
286
- * @category Configuration
287
- * @hidden
288
- */
289
- protected _cacheElements = () => this._cacheIcon();
290
-
291
- /**
292
- * Called by the connectedCallback prototypical method
293
- * @category Configuration
294
- * @hidden
295
- */
296
- protected _addEventListeners = () =>
297
- this.elements.icon.addEventListener(Gesture.CLICK, this._handleClick);
298
-
299
- /**
300
- * Called by the disconnectedCallback prototypical method
301
- * @category Configuration
302
- * @hidden
303
- */
304
- protected _removeEventListeners = () =>
305
- this.elements.icon.removeEventListener(Gesture.CLICK, this._handleClick);
306
-
307
- /**
308
- * Initialize component state with default values
309
- * @category State
310
- * @hidden
311
- */
312
- private _initializeState = () =>
313
- this.setAttribute(Attribute.STATUS, this._status);
314
-
315
- /**
316
- * Cache icon element reference to improve performance
317
- * @category State
318
- * @hidden
319
- */
320
- private _cacheIcon = () =>
321
- (this.elements.icon = this.root.querySelector(".icon"));
322
-
323
- /**
324
- * Handles the click event
325
- * @category Gesture
326
- * @hidden
327
- */
328
- private _handleClick = (event: MouseEvent | TouchEvent) => this.toggle();
329
- }
1
+ /**
2
+ * @module Component
3
+ */
4
+ import { Component, Template } from "@scalable.software/component";
5
+ import { type Configuration, type Handler } from "@scalable.software/component";
6
+
7
+ import {
8
+ Tag,
9
+ Attributes,
10
+ Visible,
11
+ Status,
12
+ Event,
13
+ Gesture,
14
+ } from "./pin.meta.js";
15
+
16
+ /**
17
+ * Configuration required for components with custom layout and style
18
+ * @category Configuration
19
+ */
20
+ export const configuration: Configuration = {
21
+ url: import.meta.url,
22
+ template: {
23
+ id: Tag,
24
+ },
25
+ css: {
26
+ name: "pin.style.css",
27
+ },
28
+ } as const;
29
+
30
+ /**
31
+ * A pin button that can be:
32
+ * 1. pinned and unpinned
33
+ * 2. hidden and shown
34
+ * @category Components
35
+ */
36
+ export class Pin extends Component {
37
+ /**
38
+ * The tag name of the component
39
+ * @category Configuration
40
+ */
41
+ public static get Tag() {
42
+ return Tag;
43
+ }
44
+
45
+ /**
46
+ * Only attributes defined the Attributes object will be observed in DOM
47
+ * @category Configuration
48
+ */
49
+ public static get Attributes(): Attributes {
50
+ return Attributes;
51
+ }
52
+
53
+ /**
54
+ * Helper function to load the component template into DOM
55
+ * @category Utility
56
+ */
57
+ public static Template = new Template(import.meta.url);
58
+
59
+ /**
60
+ * Cache element references to improve performance
61
+ * @category State
62
+ * @hidden
63
+ */
64
+ protected elements: { icon: HTMLDivElement | null } = { icon: null };
65
+
66
+ /**
67
+ * Internal Visibility state of the component
68
+ * @category State
69
+ * @default Visible.YES
70
+ * @hidden
71
+ */
72
+ private _visible: Visible = Visible.YES;
73
+
74
+ /**
75
+ * Internal Status state of the component
76
+ * @category State
77
+ * @default
78
+ * @hidden
79
+ */
80
+ private _status: Status = Status.UNPINNED;
81
+
82
+ /**
83
+ * on triggered when any event is triggered
84
+ * @category Events
85
+ * @hidden
86
+ */
87
+ private _on: Handler = null;
88
+
89
+ /**
90
+ * onhide triggered when pin visibility changes to hidden
91
+ * @category Events
92
+ * @hidden
93
+ */
94
+ private _onhide: Handler = null;
95
+
96
+ /**
97
+ * onshow triggered when pin visibility changes to visible
98
+ * @category Events
99
+ * @hidden
100
+ */
101
+ private _onshow: Handler = null;
102
+
103
+ /**
104
+ * onpin triggered when pin status changes to pinned
105
+ * @category Events
106
+ * @hidden
107
+ */
108
+ private _onpin: Handler = null;
109
+
110
+ /**
111
+ * onunpin triggered when pin state changes to unpinned
112
+ * @category Events
113
+ * @hidden
114
+ */
115
+ private _onunpin: Handler = null;
116
+
117
+ constructor() {
118
+ super(configuration);
119
+ }
120
+
121
+ /**
122
+ * Get and Sets the visibility of the pin button
123
+ * @category State
124
+ */
125
+ public get visible(): Visible {
126
+ return this.hasAttribute(Attributes.VISIBLE)
127
+ ? (this.getAttribute(Attributes.VISIBLE) as Visible)
128
+ : this._visible;
129
+ }
130
+ public set visible(visible: Visible) {
131
+ visible = visible || Visible.YES;
132
+ if (this._visible !== visible) {
133
+ this._visible = visible;
134
+ visible == Visible.YES && this.removeAttribute(Attributes.VISIBLE);
135
+ visible == Visible.NO && this.setAttribute(Attributes.VISIBLE, visible);
136
+
137
+ visible == Visible.NO &&
138
+ this._dispatchEvent(Event.ON_HIDE, { detail: { visible } });
139
+ visible == Visible.YES &&
140
+ this._dispatchEvent(Event.ON_SHOW, { detail: { visible } });
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Get and Sets the status of the pin button
146
+ * @category State
147
+ */
148
+ public get status(): Status {
149
+ return this.hasAttribute(Attributes.STATUS)
150
+ ? (this.getAttribute(Attributes.STATUS) as Status)
151
+ : this._status;
152
+ }
153
+ public set status(status: Status) {
154
+ if (this._status !== status) {
155
+ this._status = status;
156
+ this.setAttribute(Attributes.STATUS, status);
157
+
158
+ status == Status.PINNED &&
159
+ this._dispatchEvent(Event.ON_PIN, { detail: { status } });
160
+ status == Status.UNPINNED &&
161
+ this._dispatchEvent(Event.ON_UNPIN, { detail: { status } });
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Triggered on any event
167
+ * @event
168
+ * @category Events
169
+ */
170
+ public set on(handler: Handler) {
171
+ Object.values(Event)
172
+ .filter((event): event is Event => event !== Event.ON)
173
+ .forEach((event: Event) => {
174
+ this._on && this.removeEventListener(event, this._on);
175
+ this._on = handler;
176
+ this._on && this.addEventListener(event, this._on);
177
+ });
178
+ }
179
+
180
+ /**
181
+ * Triggered via `.hide()`
182
+ * @event
183
+ * @category Events
184
+ */
185
+ public set onhide(handler: Handler) {
186
+ this._onhide && this.removeEventListener(Event.ON_HIDE, this._onhide);
187
+ this._onhide = handler;
188
+ this._onhide && this.addEventListener(Event.ON_HIDE, this._onhide);
189
+ }
190
+
191
+ /**
192
+ * Triggered via `.show()`
193
+ * @event
194
+ * @category Events
195
+ */
196
+ public set onshow(handler: Handler) {
197
+ this._onshow && this.removeEventListener(Event.ON_SHOW, this._onshow);
198
+ this._onshow = handler;
199
+ this._onshow && this.addEventListener(Event.ON_SHOW, this._onshow);
200
+ }
201
+
202
+ /**
203
+ * Triggered via `.pin()`
204
+ * @event
205
+ * @category Events
206
+ */
207
+ public set onpin(handler: Handler) {
208
+ this._onpin && this.removeEventListener(Event.ON_PIN, this._onpin);
209
+ this._onpin = handler;
210
+ this._onpin && this.addEventListener(Event.ON_PIN, this._onpin);
211
+ }
212
+
213
+ /**
214
+ * Triggered via `.unpin()`
215
+ * @event
216
+ * @category Events
217
+ */
218
+ public set onunpin(handler: Handler) {
219
+ this._onunpin && this.removeEventListener(Event.ON_UNPIN, this._onunpin);
220
+ this._onunpin = handler;
221
+ this._onunpin && this.addEventListener(Event.ON_UNPIN, this._onunpin);
222
+ }
223
+
224
+ /**
225
+ * Hide the pin button when it is visible
226
+ * @category Operations
227
+ */
228
+ public hide = () => (this.visible = Visible.NO);
229
+
230
+ /**
231
+ * Show the pin button when it is hidden
232
+ * @category Operations
233
+ */
234
+ public show = () => (this.visible = Visible.YES);
235
+
236
+ /**
237
+ * Pin the pin button when it is unpinned
238
+ * @category Operations
239
+ */
240
+ public pin = () => (this.status = Status.PINNED);
241
+
242
+ /**
243
+ * Unpin the pin button when it is pinned
244
+ * @category Operations
245
+ */
246
+ public unpin = () => (this.status = Status.UNPINNED);
247
+
248
+ /**
249
+ * Toggle the pin button between pinned and unpinned
250
+ * @category Operations
251
+ */
252
+ public toggle = () =>
253
+ (this.status =
254
+ this.status === Status.PINNED ? Status.UNPINNED : Status.PINNED);
255
+
256
+ /**
257
+ * List operations to perform for selected attributes being observed in the DOM.
258
+ * @category Configuration
259
+ * @hidden
260
+ */
261
+ protected _attributeHandlers = {
262
+ [Attributes.VISIBLE]: (value) => (this.visible = value),
263
+ [Attributes.STATUS]: (value) => (this.status = value),
264
+ };
265
+
266
+ /**
267
+ * Initialize component attributes with default values
268
+ * @category Configuration
269
+ * @hidden
270
+ */
271
+ protected _initialize = () => this._initializeState();
272
+
273
+ /**
274
+ * Cache element references to improve performance
275
+ * @category Configuration
276
+ * @hidden
277
+ */
278
+ protected _cache = () => this._cacheIcon();
279
+
280
+ /**
281
+ * Called by the connectedCallback prototypical method
282
+ * @category Configuration
283
+ * @hidden
284
+ */
285
+ protected _addEventListeners = () =>
286
+ this.elements.icon.addEventListener(Gesture.CLICK, this._handleClick);
287
+
288
+ /**
289
+ * Called by the disconnectedCallback prototypical method
290
+ * @category Configuration
291
+ * @hidden
292
+ */
293
+ protected _removeEventListeners = () =>
294
+ this.elements.icon.removeEventListener(Gesture.CLICK, this._handleClick);
295
+
296
+ /**
297
+ * Initialize component state with default values
298
+ * @category State
299
+ * @hidden
300
+ */
301
+ private _initializeState = () =>
302
+ this.setAttribute(Attributes.STATUS, this._status);
303
+
304
+ /**
305
+ * Cache icon element reference to improve performance
306
+ * @category State
307
+ * @hidden
308
+ */
309
+ private _cacheIcon = () =>
310
+ (this.elements.icon = this.root.querySelector(".icon"));
311
+
312
+ /**
313
+ * Handles the click event
314
+ * @category Gesture
315
+ * @hidden
316
+ */
317
+ private _handleClick = (event: MouseEvent | TouchEvent) => this.toggle();
318
+ }
package/dist/Index.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export { type Configuration } from "@scalable.software/component";
2
- export { Tag, Attribute, State, Visible, Status, Operation, Event, Gesture, } from "./Pin.meta.js";
3
- export { type Attributes, type States, type Visibility, type Statuses, type Operations, type Events, type Gestures, type Handler, } from "./Pin.meta.js";
4
- export { Pin } from "./Pin.js";
package/dist/Index.js DELETED
@@ -1,2 +0,0 @@
1
- export { Tag, Attribute, State, Visible, Status, Operation, Event, Gesture, } from "./Pin.meta.js";
2
- export { Pin } from "./Pin.js";