Skip to content

Commit 82e29e6

Browse files
author
Ryan A. Johnson
committed
feat(HXFileTileElement): add declarative functionality
* Add `dismiss()` to simulate clicking the "X" * Refactor source to better organize contents
1 parent fe8230b commit 82e29e6

File tree

2 files changed

+69
-40
lines changed

2 files changed

+69
-40
lines changed

docs/elements/hx-file-tile/index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<section>
1111
<p>
12-
The custom <code>{{page.title}}</code> element describes the states of
12+
The custom <code>{{page.title}}</code> element describes the states of
1313
a downloadable asset.
1414
</p>
1515

@@ -38,6 +38,14 @@
3838
</hx-def>
3939
</hx-dl>
4040
</section>
41+
42+
<section>
43+
<h2 id="methods">Methods</h2>
44+
<dl>
45+
<dt>dismiss()</dt>
46+
<dd>Simulates a mouse click on the dismiss button (i.e., clicking the "X")</dd>
47+
</dl>
48+
</section>
4149
{% endblock %}
4250

4351
{% block attributes %}

src/helix-ui/elements/HXFileTileElement.js

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class HXFileTileElement extends HXElement {
3232
'icon',
3333
'name',
3434
];
35-
}//$observedAttributes
35+
}
3636

3737
$onAttributeChange (attr, oldVal, newVal) {
3838
switch (attr) {
@@ -53,42 +53,48 @@ export class HXFileTileElement extends HXElement {
5353
this._elExt.innerText = this._extension || '';
5454
break;
5555
}
56-
}//$onAttributeChange
56+
}
5757

58-
// GETTERS
58+
/**
59+
* Icon to appear within the empty file icon.
60+
* @type {String}
61+
*/
5962
get icon () {
6063
return this.getAttribute('icon');
6164
}
62-
63-
get name () {
64-
return this.getAttribute('name');
65-
}
66-
67-
get href () {
68-
return this.getAttribute('href');
69-
}
70-
71-
get loading () {
72-
return this.hasAttribute('loading');
73-
}
74-
75-
get valid () {
76-
return !this.hasAttribute('invalid');
77-
}
78-
79-
// SETTERS
8065
set icon (newVal) {
8166
this.setAttribute('icon', newVal);
8267
}
8368

69+
/**
70+
* File name
71+
* @type {String}
72+
*/
73+
get name () {
74+
return this.getAttribute('name');
75+
}
8476
set name (newVal) {
8577
this.setAttribute('name', newVal);
8678
}
8779

80+
/**
81+
* URL to download the file
82+
* @type {String}
83+
*/
84+
get href () {
85+
return this.getAttribute('href');
86+
}
8887
set href (newVal) {
8988
this.setAttribute('href', newVal);
9089
}
9190

91+
/**
92+
* @default false
93+
* @type {Boolean}
94+
*/
95+
get loading () {
96+
return this.hasAttribute('loading');
97+
}
9298
set loading (newVal) {
9399
if (newVal) {
94100
this.setAttribute('loading', '');
@@ -97,6 +103,13 @@ export class HXFileTileElement extends HXElement {
97103
}
98104
}
99105

106+
/**
107+
* @default true
108+
* @type {Boolean}
109+
*/
110+
get valid () {
111+
return !this.hasAttribute('invalid');
112+
}
100113
set valid (newVal) {
101114
if (newVal) {
102115
this.removeAttribute('invalid');
@@ -105,6 +118,31 @@ export class HXFileTileElement extends HXElement {
105118
}
106119
}
107120

121+
/**
122+
* Simulates clicking "X" (i.e., the dismiss button)
123+
*/
124+
dismiss () {
125+
if (this.loading || !this.valid) {
126+
if (this.$emit('cancel')) {
127+
this.remove();
128+
}
129+
} else {
130+
if (this.$emit('delete')) {
131+
// only if event was not canceled by consumer
132+
this.remove();
133+
}
134+
}
135+
}
136+
137+
/**
138+
* https://regex101.com/r/K8XCbn/2
139+
* @private
140+
*/
141+
get _extension () {
142+
let re = /(?:\.([^.]+))?$/;
143+
return re.exec(this.name)[1];
144+
}
145+
108146
/** @private */
109147
get _btnDismiss () {
110148
return this.shadowRoot.getElementById('hxDismiss');
@@ -125,26 +163,9 @@ export class HXFileTileElement extends HXElement {
125163
return this.shadowRoot.getElementById('hxLink');
126164
}
127165

128-
/** @private */
129-
// https://regex101.com/r/K8XCbn/2
130-
get _extension () {
131-
let re = /(?:\.([^.]+))?$/;
132-
return re.exec(this.name)[1];
133-
}
134-
135166
/** @private */
136167
_onDismiss (evt) {
137168
evt.preventDefault();
138-
139-
if (this.loading || !this.valid) {
140-
if (this.$emit('cancel')) {
141-
this.remove();
142-
}
143-
} else {
144-
if (this.$emit('delete')) {
145-
// only if event was not canceled by consumer
146-
this.remove();
147-
}
148-
}
169+
this.dismiss();
149170
}
150171
}

0 commit comments

Comments
 (0)