Skip to content

Commit 78e4756

Browse files
committed
maint(pat switch): Modernize code.
1 parent 6be62e8 commit 78e4756

File tree

2 files changed

+179
-184
lines changed

2 files changed

+179
-184
lines changed

src/pat/switch/switch.js

Lines changed: 44 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
/**
2-
* Patterns switch - toggle classes on click
3-
*
4-
* Copyright 2013 Simplon B.V. - Wichert Akkerman
5-
* Copyright 2012 Florian Friesdorf
6-
* Copyright 2012 SYSLAB.COM GmbH
7-
*/
1+
// Patterns switch - toggle classes on click
82
import $ from "jquery";
9-
import registry from "../../core/registry";
3+
import Base from "../../core/base";
104
import logging from "../../core/logging";
115
import Parser from "../../core/parser";
126
import store from "../../core/store";
137
import utils from "../../core/utils";
8+
import dom from "../../core/dom";
149

1510
const log = logging.getLogger("pat.switch");
1611

@@ -20,78 +15,54 @@ parser.addArgument("remove");
2015
parser.addArgument("add");
2116
parser.addArgument("store", "none", ["none", "session", "local"]);
2217

23-
var switcher = {
18+
export default Base.extend({
2419
name: "switch",
2520
trigger: ".pat-switch",
26-
jquery_plugin: true,
2721

28-
init: function ($el, defaults) {
29-
return $el.each(function () {
30-
var $trigger = $(this),
31-
options = parser.parse($trigger, defaults, true);
32-
options = switcher._validateOptions(options);
33-
if (options.length) {
34-
$trigger
35-
.data("patternSwitch", options)
36-
.off(".patternSwitch")
37-
.on("click.patternSwitch", switcher._onClick);
38-
for (var i = 0; i < options.length; i++) {
39-
var option = options[i];
40-
if (option.store !== "none") {
41-
option._storage = (option.store === "local"
42-
? store.local
43-
: store.session)("switch");
44-
var state = option._storage.get(option.selector);
45-
if (
46-
state &&
47-
state.remove === option.remove &&
48-
state.add === option.add
49-
)
50-
switcher._update(option.selector, state.remove, state.add);
51-
}
52-
}
22+
init() {
23+
this.options = this._validateOptions(parser.parse(this.el, this.options, true));
24+
if (!this.options.length) {
25+
log.error("Switch without options cannot be initialized.");
26+
}
27+
28+
dom.add_event_listener(this.el, "click", "pat-switch--on-click", (e) => {
29+
if (e.tagName === "A") {
30+
e.preventDefault();
5331
}
32+
this._go();
5433
});
55-
},
5634

57-
destroy: function ($el) {
58-
return $el.each(function () {
59-
$(this).removeData("patternSwitch").off("click.patternSwitch");
60-
});
35+
for (const option of this.options) {
36+
if (option.store !== "none") {
37+
option._storage = (
38+
option.store === "local" ? store.local : store.session
39+
)("switch");
40+
const state = option._storage.get(option.selector);
41+
if (state && state.remove === option.remove && state.add === option.add)
42+
this._update(option.selector, state.remove, state.add);
43+
}
44+
}
6145
},
6246

63-
// jQuery API to toggle a switch
64-
execute: function ($el) {
65-
return $el.each(function () {
66-
switcher._go($(this));
67-
});
47+
destroy() {
48+
dom.remove_event_listener(this.el, "pat-switch--on-click");
6849
},
6950

70-
_onClick: function (ev) {
71-
if ($(ev.currentTarget).is("a")) {
72-
ev.preventDefault();
73-
}
74-
switcher._go($(this));
51+
execute() {
52+
// jQuery API to toggle a switch
53+
this._go();
7554
},
7655

77-
_go: function ($trigger) {
78-
var options = $trigger.data("patternSwitch"),
79-
option,
80-
i;
81-
if (!options) {
82-
log.error("Tried to execute a switch for an uninitialised element.");
83-
return;
84-
}
85-
for (i = 0; i < options.length; i++) {
86-
option = options[i];
87-
switcher._update(option.selector, option.remove, option.add);
56+
_go() {
57+
for (const option of this.options) {
58+
this._update(option.selector, option.remove, option.add);
8859
if (option._storage)
8960
option._storage.set(option.selector, {
9061
remove: option.remove,
9162
add: option.add,
9263
});
9364
}
94-
$trigger.trigger("resize");
65+
$(this.el).trigger("resize");
9566
},
9667

9768
_validateOptions: function (options) {
@@ -106,15 +77,17 @@ var switcher = {
10677
},
10778

10879
_update: function (selector, remove, add) {
109-
var $targets = $(selector);
80+
const targets = document.querySelectorAll(selector);
81+
for (const target of targets) {
82+
if (remove) {
83+
utils.removeWildcardClass(target, remove);
84+
}
11085

111-
if (!$targets.length) return;
86+
if (add) {
87+
target.classList.add(add);
88+
}
11289

113-
if (remove) utils.removeWildcardClass($targets, remove);
114-
if (add) $targets.addClass(add);
115-
$targets.trigger("pat-update", { pattern: "switch" });
90+
$(target).trigger("pat-update", { pattern: "switch" });
91+
}
11692
},
117-
};
118-
119-
registry.register(switcher);
120-
export default switcher;
93+
});

0 commit comments

Comments
 (0)