Skip to content

Commit f67302d

Browse files
committed
Add attr, class, remove class loading.
1 parent b4c1453 commit f67302d

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

django_unicorn/static/js/attribute.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class Attribute {
1212
this.isModel = false;
1313
this.isField = false;
1414
this.isPoll = false;
15+
this.isLoading = false;
1516
this.isKey = false;
1617
this.isPK = false;
1718
this.isError = false;
@@ -37,6 +38,8 @@ export class Attribute {
3738
this.isDb = true;
3839
} else if (contains(this.name, ":poll")) {
3940
this.isPoll = true;
41+
} else if (contains(this.name, ":loading")) {
42+
this.isLoading = true;
4043
} else if (this.name === "unicorn:key" || this.name === "u:key") {
4144
this.isKey = true;
4245
} else if (this.name === "unicorn:pk" || this.name === "u:pk") {

django_unicorn/static/js/element.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class Element {
2626

2727
this.model = {};
2828
this.poll = {};
29+
this.loading = {};
2930
this.actions = [];
3031
this.db = {};
3132
this.field = {};
@@ -74,6 +75,14 @@ export class Element {
7475
if (pollArgs.length > 0) {
7576
this.poll.timing = parseInt(pollArgs[0], 10) || 2000;
7677
}
78+
} else if (attribute.isLoading) {
79+
if (attribute.modifiers.attr) {
80+
this.loading.attr = attribute.value;
81+
} else if (attribute.modifiers.class && attribute.modifiers.remove) {
82+
this.loading.removeClass = attribute.value;
83+
} else if (attribute.modifiers.class) {
84+
this.loading.class = attribute.value;
85+
}
7786
} else if (attribute.eventType) {
7887
const action = {};
7988
action.name = attribute.value;

django_unicorn/static/js/eventListeners.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ export function addActionEventListener(component, eventType) {
123123
}
124124
});
125125

126+
if (targetElement.loading) {
127+
if (targetElement.loading.attr) {
128+
targetElement.el[targetElement.loading.attr] = targetElement.loading.attr;
129+
}
130+
131+
if (targetElement.loading.class) {
132+
targetElement.el.classList.add(targetElement.loading.class);
133+
}
134+
135+
if (targetElement.loading.removeClass) {
136+
targetElement.el.classList.remove(targetElement.loading.removeClass);
137+
}
138+
}
139+
126140
if (action.key) {
127141
if (action.key === toKebabCase(event.key)) {
128142
component.callMethod(action.name);

tests/js/element/loading.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import test from "ava";
2+
import { getElement } from "../utils.js";
3+
4+
test("loading class", (t) => {
5+
const html = "<div u:click='update()' u:loading.class='loading'></div>";
6+
const element = getElement(html);
7+
8+
t.is(element.loading.class, "loading");
9+
});
10+
11+
test("loading remove class", (t) => {
12+
const html = "<div u:click='update()' u:loading.class.remove='unloading'></div>";
13+
const element = getElement(html);
14+
15+
t.is(element.loading.removeClass, "unloading");
16+
});
17+
18+
test("loading attr", (t) => {
19+
const html = "<div u:click='update()' u:loading.attr='disabled'></div>";
20+
const element = getElement(html);
21+
22+
t.is(element.loading.attr, "disabled");
23+
});

0 commit comments

Comments
 (0)