Skip to content

Commit 86d827a

Browse files
committed
Remove duplicate code to reduce the size of the build.
1 parent 2a49542 commit 86d827a

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

django_unicorn/static/js/element.js

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,17 @@ export class Element {
4848
this.isUnicorn = true;
4949
}
5050

51-
if (attribute.isModel) {
52-
this.model.name = attribute.value;
53-
this.model.eventType = attribute.modifiers.lazy ? "blur" : "input";
54-
this.model.isLazy = !!attribute.modifiers.lazy;
55-
this.model.isDefer = !!attribute.modifiers.defer;
56-
this.model.debounceTime = attribute.modifiers.debounce
57-
? parseInt(attribute.modifiers.debounce, 10) || -1
58-
: -1;
59-
} else if (attribute.isField) {
60-
this.field.name = attribute.value;
61-
this.field.eventType = attribute.modifiers.lazy ? "blur" : "input";
62-
this.field.isLazy = !!attribute.modifiers.lazy;
63-
this.field.isDefer = !!attribute.modifiers.defer;
64-
this.field.debounceTime = attribute.modifiers.debounce
51+
if (attribute.isModel || attribute.isField) {
52+
let key = "model";
53+
if (attribute.isField) {
54+
key = "field";
55+
}
56+
57+
this[key].name = attribute.value;
58+
this[key].eventType = attribute.modifiers.lazy ? "blur" : "input";
59+
this[key].isLazy = !!attribute.modifiers.lazy;
60+
this[key].isDefer = !!attribute.modifiers.defer;
61+
this[key].debounceTime = attribute.modifiers.debounce
6562
? parseInt(attribute.modifiers.debounce, 10) || -1
6663
: -1;
6764
} else if (attribute.isDb) {
@@ -80,26 +77,24 @@ export class Element {
8077
}
8178
} else if (attribute.isPollDisable) {
8279
this.poll.disableData = attribute.value;
83-
} else if (attribute.isLoading) {
80+
} else if (attribute.isLoading || attribute.isDirty) {
81+
let key = "dirty";
82+
83+
if (attribute.isLoading) {
84+
key = "loading";
85+
}
86+
8487
if (attribute.modifiers.attr) {
85-
this.loading.attr = attribute.value;
88+
this[key].attr = attribute.value;
8689
} else if (attribute.modifiers.class && attribute.modifiers.remove) {
87-
this.loading.removeClasses = attribute.value.split(" ");
90+
this[key].removeClasses = attribute.value.split(" ");
8891
} else if (attribute.modifiers.class) {
89-
this.loading.classes = attribute.value.split(" ");
90-
} else if (attribute.modifiers.remove) {
92+
this[key].classes = attribute.value.split(" ");
93+
} else if (attribute.isLoading && attribute.modifiers.remove) {
9194
this.loading.hide = true;
92-
} else {
95+
} else if (attribute.isLoading) {
9396
this.loading.show = true;
9497
}
95-
} else if (attribute.isDirty) {
96-
if (attribute.modifiers.attr) {
97-
this.dirty.attr = attribute.value;
98-
} else if (attribute.modifiers.class && attribute.modifiers.remove) {
99-
this.dirty.removeClasses = attribute.value.split(" ");
100-
} else if (attribute.modifiers.class) {
101-
this.dirty.classes = attribute.value.split(" ");
102-
}
10398
} else if (attribute.isTarget) {
10499
this.target = attribute.value;
105100
} else if (attribute.eventType) {
@@ -235,55 +230,55 @@ export class Element {
235230
* Handle loading for the element.
236231
*/
237232
handleLoading() {
238-
if (hasValue(this.loading)) {
239-
if (this.loading.attr) {
240-
this.el.setAttribute(this.loading.attr, this.loading.attr);
241-
}
242-
243-
if (this.loading.classes) {
244-
this.el.classList.add(...this.loading.classes);
245-
}
246-
247-
if (this.loading.removeClasses) {
248-
this.el.classList.remove(...this.loading.removeClasses);
249-
}
250-
}
233+
this.handleInterfacer("loading");
251234
}
252235

253236
/**
254237
* Handle dirty for the element.
255238
* @param {bool} revert Whether or not the revert the dirty class.
256239
*/
257240
handleDirty(revert) {
241+
this.handleInterfacer("dirty", revert);
242+
}
243+
244+
/**
245+
* Handle interfacers for the element.
246+
* @param {string} interfacerType The type of interfacer. Either "dirty" or "loading".
247+
* @param {bool} revert Whether or not the revert the interfacer.
248+
*/
249+
handleInterfacer(interfacerType, revert) {
258250
revert = revert || false;
259251

260-
if (hasValue(this.dirty)) {
261-
if (this.dirty.attr) {
252+
if (hasValue(this[interfacerType])) {
253+
if (this[interfacerType].attr) {
262254
if (revert) {
263-
this.el.removeAttribute(this.dirty.attr);
255+
this.el.removeAttribute(this[interfacerType].attr);
264256
} else {
265-
this.el.setAttribute(this.dirty.attr, this.dirty.attr);
257+
this.el.setAttribute(
258+
this[interfacerType].attr,
259+
this[interfacerType].attr
260+
);
266261
}
267262
}
268263

269-
if (this.dirty.classes) {
264+
if (this[interfacerType].classes) {
270265
if (revert) {
271-
this.el.classList.remove(...this.dirty.classes);
266+
this.el.classList.remove(...this[interfacerType].classes);
272267

273268
// Remove the class attribute if it's empty so that morphdom sees the node as equal
274269
if (this.el.classList.length === 0) {
275270
this.el.removeAttribute("class");
276271
}
277272
} else {
278-
this.el.classList.add(...this.dirty.classes);
273+
this.el.classList.add(...this[interfacerType].classes);
279274
}
280275
}
281276

282-
if (this.dirty.removeClasses) {
277+
if (this[interfacerType].removeClasses) {
283278
if (revert) {
284-
this.el.classList.add(...this.dirty.removeClasses);
279+
this.el.classList.add(...this[interfacerType].removeClasses);
285280
} else {
286-
this.el.classList.remove(...this.dirty.removeClasses);
281+
this.el.classList.remove(...this[interfacerType].removeClasses);
287282
}
288283
}
289284
}

0 commit comments

Comments
 (0)