Skip to content

Commit a6b8a7a

Browse files
committed
pat-modal: Modernize code
1 parent 6110184 commit a6b8a7a

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

src/pat/modal/modal.js

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default Base.extend({
2222
// links, forms and subforms inject modals
2323
trigger:
2424
"div.pat-modal, a.pat-modal, form.pat-modal, .pat-modal.pat-subform",
25-
init: function ($el, opts, trigger) {
25+
init($el, opts, trigger) {
2626
this.options = parser.parse(this.$el, opts);
2727
if (trigger && trigger.type === "injection") {
2828
$.extend(
@@ -37,8 +37,8 @@ export default Base.extend({
3737
}
3838
},
3939

40-
_init_inject1: function () {
41-
var opts = {
40+
_init_inject1() {
41+
const opts = {
4242
target: "#pat-modal",
4343
class:
4444
"pat-modal" +
@@ -55,17 +55,14 @@ export default Base.extend({
5555
if (!this.$el.closest("#pat-modal")) {
5656
$("#pat-modal").detach();
5757
}
58-
this.$el.on(
59-
"pat-inject-missingSource pat-inject-missingTarget",
60-
function () {
61-
$("#pat-modal").detach();
62-
}
63-
);
58+
this.$el.on("pat-inject-missingSource pat-inject-missingTarget", () => {
59+
$("#pat-modal").detach();
60+
});
6461
inject.init(this.$el, opts);
6562
},
6663

67-
_init_div1: function () {
68-
var $header = $("<div class='header' />");
64+
_init_div1() {
65+
const $header = $("<div class='header' />");
6966
if (this.options.closing.indexOf("close-button") !== -1) {
7067
$(
7168
"<button type='button' class='close-panel'>" +
@@ -104,7 +101,7 @@ export default Base.extend({
104101
$("body").addClass("modal-active");
105102
},
106103

107-
_init_handlers: function () {
104+
_init_handlers() {
108105
$(document).on(
109106
"click.pat-modal",
110107
"#pat-modal .close-panel[type!=submit]",
@@ -153,45 +150,44 @@ export default Base.extend({
153150
);
154151
},
155152

156-
_onPossibleOutsideClick: function (ev) {
153+
_onPossibleOutsideClick(ev) {
157154
if (this.$el.has(ev.target)) {
158155
this.destroy();
159156
}
160157
},
161158

162-
_onKeyUp: function (ev) {
159+
_onKeyUp(ev) {
163160
if (ev.which === 27) {
164161
this.destroy();
165162
}
166163
},
167164

168-
getTallestChild: function () {
169-
var $tallest_child;
170-
$("*", this.$el).each(function () {
171-
var $child = $(this);
165+
getTallestChild() {
166+
let $tallest_child;
167+
for (const child of $("*", this.$el)) {
168+
const $child = $(child);
172169
if (typeof $tallest_child === "undefined") {
173170
$tallest_child = $child;
174171
} else if (
175172
$child.outerHeight(true) > $tallest_child.outerHeight(true)
176173
) {
177174
$tallest_child = $child;
178175
}
179-
});
176+
}
180177
return $tallest_child;
181178
},
182179

183-
setPosition: function () {
180+
setPosition() {
184181
this.$el.css("top", ($(window).innerHeight() - this.$el.height()) / 2);
185182
},
186183

187-
resize: function () {
184+
resize() {
188185
// reset the height before setting a new one
189186
this.$el.removeClass("max-height").css("height", "");
190187

191-
var panel_content_elem = this.$el.find(".panel-content");
192-
var header_elem = this.$el.find(".header");
193-
194-
var modal_height =
188+
const panel_content_elem = this.$el.find(".panel-content");
189+
const header_elem = this.$el.find(".header");
190+
const modal_height =
195191
panel_content_elem.outerHeight(true) +
196192
header_elem.outerHeight(true);
197193
if (this.$el.height() < modal_height) {
@@ -210,22 +206,20 @@ export default Base.extend({
210206
utils.redraw(this.$el.find(".panel-body"));
211207
}
212208
},
213-
destroy: function () {
214-
var $el = this.$el;
209+
destroy() {
215210
// if working without injection, destroy right away.
216211
$(document).off(".pat-modal");
217-
$el.remove();
212+
this.$el.remove();
218213
$("body").removeClass("modal-active");
219214
$("body").removeClass("modal-panel");
220215
},
221-
destroy_inject: function () {
222-
var $el = this.$el;
223-
if ($el.find("form").hasClass("pat-inject")) {
216+
destroy_inject() {
217+
if (this.$el.find("form").hasClass("pat-inject")) {
224218
// if pat-inject in modal form, listen to patterns-inject-triggered and destroy first
225219
// once that has been triggered
226220
let destroy_handler = () => {
227221
$(document).off(".pat-modal");
228-
$el.remove();
222+
this.$el.remove();
229223
$("body").removeClass("modal-active");
230224
$("body").removeClass("modal-panel");
231225
$("body").off("patterns-inject-triggered", destroy_handler);
@@ -234,7 +228,7 @@ export default Base.extend({
234228
} else {
235229
// if working without injection, destroy right away.
236230
$(document).off(".pat-modal");
237-
$el.remove();
231+
this.$el.remove();
238232
$("body").removeClass("modal-active");
239233
$("body").removeClass("modal-panel");
240234
}

0 commit comments

Comments
 (0)