Skip to content

Commit 870f6dc

Browse files
committed
maint(pat-collapsible): Modernize code.
1 parent f0d8d5b commit 870f6dc

File tree

2 files changed

+73
-83
lines changed

2 files changed

+73
-83
lines changed

src/pat/collapsible/collapsible.js

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/**
2-
* Patterns collapsible - Collapsible content
3-
*
4-
* Copyright 2012-2013 Florian Friesdorf
5-
* Copyright 2012-2013 Simplon B.V. - Wichert Akkerman
6-
* Copyright 2012 Markus Maier
7-
* Copyright 2013 Peter Lamut
8-
* Copyright 2012 Jonas Hoersch
9-
*/
10-
111
import $ from "jquery";
122
import "../../core/jquery-ext";
133
import inject from "../inject/inject";
@@ -54,8 +44,8 @@ export default Base.extend({
5444
"slide-horizontal": { closed: "slideOut", open: "slideIn" },
5545
},
5646

57-
init: function ($el, opts) {
58-
var $content, state, storage;
47+
init($el, opts) {
48+
let $content;
5949
this.options = store.updateOptions($el[0], parser.parse($el, opts));
6050

6151
if (this.options.trigger === "::first") {
@@ -81,11 +71,11 @@ export default Base.extend({
8171
}
8272
}
8373

84-
state = this.options.closed || $el.hasClass("closed") ? "closed" : "open";
74+
let state = this.options.closed || $el.hasClass("closed") ? "closed" : "open";
8575
if (this.options.store !== "none") {
86-
storage = (this.options.store === "local" ? store.local : store.session)(
87-
this.name
88-
);
76+
const storage = (
77+
this.options.store === "local" ? store.local : store.session
78+
)(this.name);
8979
state = storage.get($el.attr("id")) || state;
9080
}
9181

@@ -122,42 +112,42 @@ export default Base.extend({
122112
return $el;
123113
},
124114

125-
open: function () {
115+
open() {
126116
if (!this.$el.hasClass("open")) this.toggle();
127117
return this.$el;
128118
},
129119

130-
close: function () {
120+
close() {
131121
if (!this.$el.hasClass("closed")) this.toggle();
132122
return this.$el;
133123
},
134124

135-
_onClick: function (event) {
125+
_onClick(event) {
136126
this.toggle(event.data);
137127
},
138128

139-
_onKeyPress: function (event) {
140-
var keycode = event.keyCode ? event.keyCode : event.which;
129+
_onKeyPress(event) {
130+
const keycode = event.keyCode ? event.keyCode : event.which;
141131
if (keycode === 13) this.toggle();
142132
},
143133

144-
_loadContent: function ($el, url, $target) {
145-
var components = url.split("#"),
146-
base_url = components[0],
147-
id = components[1] ? "#" + components[1] : "body",
148-
opts = [
149-
{
150-
url: base_url,
151-
source: id,
152-
$target: $target,
153-
dataType: "html",
154-
},
155-
];
134+
_loadContent($el, url, $target) {
135+
const components = url.split("#");
136+
const base_url = components[0];
137+
const id = components[1] ? "#" + components[1] : "body";
138+
const opts = [
139+
{
140+
url: base_url,
141+
source: id,
142+
$target: $target,
143+
dataType: "html",
144+
},
145+
];
156146
inject.execute(opts, $el);
157147
},
158148

159149
// jQuery method to force loading of content.
160-
loadContent: function ($el) {
150+
loadContent($el) {
161151
return $el.each(
162152
function (idx, el) {
163153
if (this.options.loadContent)
@@ -166,12 +156,12 @@ export default Base.extend({
166156
);
167157
},
168158

169-
toggle: function () {
170-
var new_state = this.$el.hasClass("closed") ? "open" : "closed";
159+
toggle() {
160+
const new_state = this.$el.hasClass("closed") ? "open" : "closed";
171161
if (this.options.store !== "none") {
172-
var storage = (this.options.store === "local" ? store.local : store.session)(
173-
this.name
174-
);
162+
const storage = (
163+
this.options.store === "local" ? store.local : store.session
164+
)(this.name);
175165
storage.set(this.$el.attr("id"), new_state);
176166
}
177167
if (new_state === "open") {
@@ -198,11 +188,11 @@ export default Base.extend({
198188
}
199189
},
200190

201-
_transit: async function ($el, from_cls, to_cls) {
191+
async _transit($el, from_cls, to_cls) {
202192
if (to_cls === "open" && this.options.loadContent) {
203193
this._loadContent($el, this.options.loadContent, this.$panel);
204194
}
205-
var duration =
195+
const duration =
206196
this.options.transition === "css" || this.options.transition === "none"
207197
? null
208198
: this.options.effect.duration;
@@ -215,7 +205,7 @@ export default Base.extend({
215205
transition: "complete",
216206
});
217207
} else {
218-
var t = this.transitions[this.options.transition];
208+
const t = this.transitions[this.options.transition];
219209
$el.addClass("in-progress").trigger("pat-update", {
220210
pattern: "collapsible",
221211
transition: "start",

src/pat/collapsible/collapsible.test.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,81 +10,81 @@ describe("pat-collapsible", function () {
1010
jest.restoreAllMocks();
1111
});
1212

13-
it("wraps the collapsible within a div.panel-content", function () {
13+
it("1- wraps the collapsible within a div.panel-content", function () {
1414
document.body.innerHTML = `
1515
<div class="pat-collapsible">
1616
<h3>Trigger header</h3>
1717
<p>Collapsible content</p>
1818
</div>
1919
`;
2020

21-
var $collapsible = $(".pat-collapsible");
21+
const $collapsible = $(".pat-collapsible");
2222
pattern.init($collapsible);
2323
expect($collapsible.find(".panel-content").length).toBe(1);
2424
});
2525

26-
it("is open by default", function () {
26+
it("2 - is open by default", function () {
2727
document.body.innerHTML = `
2828
<div class="pat-collapsible">
2929
<h3>Trigger header</h3>
3030
<p>Collapsible content</p>
3131
</div>
3232
`;
3333

34-
var $collapsible = $(".pat-collapsible");
34+
const $collapsible = $(".pat-collapsible");
3535
pattern.init($collapsible);
3636
expect($collapsible.hasClass("open")).toBeTruthy();
3737
});
3838

39-
it("can be explicitly closed by adding the class 'closed'", function () {
39+
it("3 - can be explicitly closed by adding the class 'closed'", function () {
4040
document.body.innerHTML = `
4141
<div class="pat-collapsible closed">
4242
<h3>Trigger header</h3>
4343
<p>Collapsible content</p>
4444
</div>"
4545
`;
4646

47-
var $collapsible = $(".pat-collapsible");
47+
const $collapsible = $(".pat-collapsible");
4848
pattern.init($collapsible);
4949
expect($collapsible.hasClass("open")).toBeFalsy();
5050
});
5151

52-
it("can be toggled closed if it's open", function () {
52+
it("4 - can be toggled closed if it's open", function () {
5353
document.body.innerHTML = `
5454
<div class="pat-collapsible">
5555
<h3>Trigger header</h3>
5656
<p>Collapsible content</p>
5757
</div>
5858
`;
5959

60-
var $collapsible = $(".pat-collapsible");
61-
var pat = pattern.init($collapsible, { transition: "none" });
60+
const $collapsible = $(".pat-collapsible");
61+
const pat = pattern.init($collapsible, { transition: "none" });
6262
pat.toggle($collapsible);
6363
expect($collapsible.hasClass("open")).toBe(false);
6464
expect($collapsible.hasClass("closed")).toBe(true);
65-
var $trigger = $("h3");
65+
const $trigger = $("h3");
6666
expect($trigger.hasClass("collapsible-open")).toBe(false);
6767
expect($trigger.hasClass("collapsible-closed")).toBe(true);
6868
});
6969

70-
it("can be toggled open if it's closed", function () {
70+
it("5 - can be toggled open if it's closed", function () {
7171
document.body.innerHTML = `
7272
<div class="pat-collapsible closed">
7373
<h3>Trigger header</h3>
7474
<p>Collapsible content</p>
7575
</div>
7676
`;
77-
var $collapsible = $(".pat-collapsible");
78-
var pat = pattern.init($collapsible, { transition: "none" });
77+
const $collapsible = $(".pat-collapsible");
78+
const pat = pattern.init($collapsible, { transition: "none" });
7979
pat.toggle($collapsible);
8080
expect($collapsible.hasClass("open")).toBe(true);
8181
expect($collapsible.hasClass("closed")).toBe(false);
82-
var $trigger = $("h3");
82+
const $trigger = $("h3");
8383
expect($trigger.hasClass("collapsible-open")).toBe(true);
8484
expect($trigger.hasClass("collapsible-closed")).toBe(false);
8585
});
8686

87-
it("can be configured to have trigger which only opens it", async function () {
87+
it("6 - can be configured to have trigger which only opens it", async function () {
8888
document.body.innerHTML = `
8989
<div class="closed pat-collapsible" data-pat-collapsible="open-trigger: #open">
9090
<button>toggle</button>
@@ -102,7 +102,7 @@ describe("pat-collapsible", function () {
102102
expect($collapsible.hasClass("closed")).toBe(false);
103103
});
104104

105-
it("can be configured to have trigger which only closes it", async function () {
105+
it("7 - can be configured to have trigger which only closes it", async function () {
106106
document.body.innerHTML = `
107107
<div class="pat-collapsible" data-pat-collapsible="close-trigger: #close">
108108
<button>toggle</button>
@@ -120,8 +120,8 @@ describe("pat-collapsible", function () {
120120
expect($collapsible.hasClass("open")).toBe(false);
121121
});
122122

123-
describe("scrolling", function () {
124-
it("can scroll to itself when opened.", async function () {
123+
describe("8 - scrolling", function () {
124+
it("8.1 - can scroll to itself when opened.", async function () {
125125
document.body.innerHTML = `
126126
<div class="pat-collapsible closed" data-pat-collapsible="scroll-selector: self">
127127
<p>Collapsible content</p>
@@ -137,7 +137,7 @@ describe("pat-collapsible", function () {
137137
expect(spy_scroll).toHaveBeenCalledTimes(1);
138138
});
139139

140-
it("does not scroll when being closed.", async function () {
140+
it("8.2 - does not scroll when being closed.", async function () {
141141
document.body.innerHTML = `
142142
<div class="pat-collapsible opened" data-pat-collapsible="scroll-selector: self">
143143
<p>Collapsible content</p>
@@ -153,19 +153,19 @@ describe("pat-collapsible", function () {
153153
expect(spy_scroll).not.toHaveBeenCalled();
154154
});
155155

156-
it("only scrolls once even if multiple collapsible are opened at once.", async function () {
156+
it("8.3 - only scrolls once even if multiple collapsible are opened at once.", async function () {
157157
document.body.innerHTML = `
158-
<button class="pat-button" id="open">Open All</button>
159-
<div class="pat-collapsible closed c1" data-pat-collapsible="scroll-selector: self; open-trigger: #open; transition: none">
160-
<p>Collapsible content</p>
161-
</div>
162-
<div class="pat-collapsible closed c2" data-pat-collapsible="scroll-selector: self; open-trigger: #open; transition: none">
163-
<p>Collapsible content</p>
164-
</div>
165-
<div class="pat-collapsible closed c3" data-pat-collapsible="scroll-selector: self; open-trigger: #open; transition: none">
166-
<p>Collapsible content</p>
167-
</div>
168-
`;
158+
<button class="pat-button" id="open">Open All</button>
159+
<div class="pat-collapsible closed c1" data-pat-collapsible="scroll-selector: self; open-trigger: #open; transition: none">
160+
<p>Collapsible content</p>
161+
</div>
162+
<div class="pat-collapsible closed c2" data-pat-collapsible="scroll-selector: self; open-trigger: #open; transition: none">
163+
<p>Collapsible content</p>
164+
</div>
165+
<div class="pat-collapsible closed c3" data-pat-collapsible="scroll-selector: self; open-trigger: #open; transition: none">
166+
<p>Collapsible content</p>
167+
</div>
168+
`;
169169

170170
registry.scan(document.body);
171171
const spy_animate = jest.spyOn($.fn, "animate");
@@ -181,12 +181,12 @@ describe("pat-collapsible", function () {
181181
expect(spy_animate).toHaveBeenCalledTimes(1);
182182
});
183183

184-
it("can scroll to itself when opened with an offset.", async function () {
184+
it("8.4 - can scroll to itself when opened with an offset.", async function () {
185185
document.body.innerHTML = `
186-
<div class="pat-collapsible closed" data-pat-collapsible="scroll-selector: self; scroll-offset: 40; transition: none">
187-
<p>Collapsible content</p>
188-
</div>
189-
`;
186+
<div class="pat-collapsible closed" data-pat-collapsible="scroll-selector: self; scroll-offset: 40; transition: none">
187+
<p>Collapsible content</p>
188+
</div>
189+
`;
190190
const collapsible = document.querySelector(".pat-collapsible");
191191
const pat = pattern.init(collapsible);
192192
const spy_animate = jest.spyOn($.fn, "animate");
@@ -198,12 +198,12 @@ describe("pat-collapsible", function () {
198198
expect(arg_1.scrollTop).toBe(-40); // the offset is substracted from the scroll position to stop BEFORE the target position.
199199
});
200200

201-
it("can scroll to itself when opened with a negative offset.", async function () {
201+
it("8.5 - can scroll to itself when opened with a negative offset.", async function () {
202202
document.body.innerHTML = `
203-
<div class="pat-collapsible closed" data-pat-collapsible="scroll-selector: self; scroll-offset: -40; transition: none">
204-
<p>Collapsible content</p>
205-
</div>
206-
`;
203+
<div class="pat-collapsible closed" data-pat-collapsible="scroll-selector: self; scroll-offset: -40; transition: none">
204+
<p>Collapsible content</p>
205+
</div>
206+
`;
207207
const collapsible = document.querySelector(".pat-collapsible");
208208
const pat = pattern.init(collapsible);
209209
const spy_animate = jest.spyOn($.fn, "animate");

0 commit comments

Comments
 (0)