Skip to content

Commit e6990b1

Browse files
committed
Only change loading states when an action method is called, not every time the event gets fired.
1 parent adddc13 commit e6990b1

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

django_unicorn/static/js/element.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@ export class Element {
217217
return parentElement;
218218
}
219219

220+
/**
221+
* Handle loading for the element.
222+
*/
223+
handleLoading() {
224+
if (this.loading) {
225+
if (this.loading.attr) {
226+
this.el[this.loading.attr] = this.loading.attr;
227+
} else if (this.loading.class) {
228+
this.el.classList.add(this.loading.class);
229+
} else if (this.loading.removeClass) {
230+
this.el.classList.remove(this.loading.removeClass);
231+
}
232+
}
233+
}
234+
220235
/**
221236
* Check if another `Element` is the same as this `Element`.
222237
* @param {Element} other

django_unicorn/static/js/eventListeners.js

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@ import {
88
} from "./utils.js";
99
import { Element } from "./element.js";
1010

11+
/**
12+
* Handles loading elements in the component.
13+
* @param {Component} component Component.
14+
* @param {Element} targetElement Targetted element.
15+
*/
16+
function handleLoading(component, targetElement) {
17+
targetElement.handleLoading();
18+
19+
// Look at all elements with a loading attribute
20+
component.loadingEls.forEach((loadingElement) => {
21+
if (loadingElement.target) {
22+
let targetedEl = $(`#${loadingElement.target}`, component.root);
23+
24+
if (!targetedEl) {
25+
component.keyEls.forEach((keyElement) => {
26+
if (!targetedEl && keyElement.key === loadingElement.target) {
27+
targetedEl = keyElement.el;
28+
}
29+
});
30+
}
31+
32+
if (targetedEl) {
33+
if (targetElement.el.isSameNode(targetedEl)) {
34+
if (loadingElement.loading.hide) {
35+
loadingElement.hide();
36+
} else if (loadingElement.loading.show) {
37+
loadingElement.show();
38+
}
39+
}
40+
}
41+
} else if (loadingElement.loading.hide) {
42+
loadingElement.hide();
43+
} else if (loadingElement.loading.show) {
44+
loadingElement.show();
45+
}
46+
});
47+
}
48+
1149
/**
1250
* Adds an action event listener to the document for each type of event (e.g. click, keyup, etc).
1351
* Added at the document level because validation errors would sometimes remove the
@@ -137,53 +175,13 @@ export function addActionEventListener(component, eventType) {
137175
}
138176
});
139177

140-
if (targetElement.loading) {
141-
if (targetElement.loading.attr) {
142-
targetElement.el[targetElement.loading.attr] =
143-
targetElement.loading.attr;
144-
} else if (targetElement.loading.class) {
145-
targetElement.el.classList.add(targetElement.loading.class);
146-
} else if (targetElement.loading.removeClass) {
147-
targetElement.el.classList.remove(
148-
targetElement.loading.removeClass
149-
);
150-
}
151-
}
152-
153-
// Look at all elements with a loading attribute
154-
component.loadingEls.forEach((loadingElement) => {
155-
if (loadingElement.target) {
156-
let targetedEl = $(`#${loadingElement.target}`, component.root);
157-
158-
if (!targetedEl) {
159-
component.keyEls.forEach((keyElement) => {
160-
if (!targetedEl && keyElement.key === loadingElement.target) {
161-
targetedEl = keyElement.el;
162-
}
163-
});
164-
}
165-
166-
if (targetedEl) {
167-
if (targetElement.el.isSameNode(targetedEl)) {
168-
if (loadingElement.loading.hide) {
169-
loadingElement.hide();
170-
} else if (loadingElement.loading.show) {
171-
loadingElement.show();
172-
}
173-
}
174-
}
175-
} else if (loadingElement.loading.hide) {
176-
loadingElement.hide();
177-
} else if (loadingElement.loading.show) {
178-
loadingElement.show();
179-
}
180-
});
181-
182178
if (action.key) {
183179
if (action.key === toKebabCase(event.key)) {
180+
handleLoading(component, targetElement);
184181
component.callMethod(action.name);
185182
}
186183
} else {
184+
handleLoading(component, targetElement);
187185
component.callMethod(action.name);
188186
}
189187
}

0 commit comments

Comments
 (0)