Skip to content

Commit a278f05

Browse files
committed
Fix broken/multiple events firing due to Turbolinks
1 parent 5cebbbb commit a278f05

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

app/assets/javascripts/application.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ document.addEventListener("turbolinks:load", function(e) {
277277
LearningPaths.init();
278278

279279
$('.tess-expandable').each(function () {
280+
if (this.dataset.origHeight) {
281+
return;
282+
}
280283
var limit = this.dataset.heightLimit || 300;
281284

282285
if (this.clientHeight > limit) {
@@ -287,32 +290,10 @@ document.addEventListener("turbolinks:load", function(e) {
287290
this.style.maxHeight = '' + limit + 'px';
288291
this.classList.add('tess-expandable-closed');
289292
const btn = $('<a href="#" class="tess-expandable-btn">Show more</a>');
290-
btn[0].expandableTarget = this;
291293
btn.insertAfter($(this));
292294
}
293295
});
294296

295-
$(document).on('click', '.tess-expandable-btn', function (event) {
296-
event.preventDefault();
297-
const div = this.expandableTarget;
298-
const maxHeight = parseInt(div.dataset.origHeight) + 80;
299-
const limit = parseInt(div.dataset.heightLimit || "300");
300-
301-
if (div.classList.contains('tess-expandable-closed')) {
302-
div.classList.add('tess-expandable-open');
303-
div.classList.remove('tess-expandable-closed');
304-
div.style.maxHeight = '' + maxHeight + 'px';
305-
this.innerHTML = 'Show less';
306-
} else {
307-
div.classList.remove('tess-expandable-open');
308-
div.classList.add('tess-expandable-closed');
309-
div.style.maxHeight = '' + limit + 'px';
310-
this.innerHTML = 'Show more';
311-
}
312-
313-
return false;
314-
});
315-
316297
$('.faq .question dt').click(function () {
317298
var button = $(this).find('.expand');
318299
var sign = button.text();
@@ -381,6 +362,36 @@ $(document).on('click', '[href="#activity_log"]', function () {
381362
return false;
382363
});
383364

365+
$(document).on('click', '.tess-expandable-btn', function (event) {
366+
event.preventDefault();
367+
let div = this.previousElementSibling;
368+
369+
if (!div.classList.contains('tess-expandable')) {
370+
div = this.parentElement.querySelector('.tess-expandable');
371+
}
372+
373+
if (!div) {
374+
return false;
375+
}
376+
377+
const maxHeight = parseInt(div.dataset.origHeight) + 80;
378+
const limit = parseInt(div.dataset.heightLimit || "300");
379+
380+
if (div.classList.contains('tess-expandable-closed')) {
381+
div.classList.add('tess-expandable-open');
382+
div.classList.remove('tess-expandable-closed');
383+
div.style.maxHeight = '' + maxHeight + 'px';
384+
this.innerHTML = 'Show less';
385+
} else {
386+
div.classList.remove('tess-expandable-open');
387+
div.classList.add('tess-expandable-closed');
388+
div.style.maxHeight = '' + limit + 'px';
389+
this.innerHTML = 'Show more';
390+
}
391+
392+
return false;
393+
});
394+
384395
// sticky-navbar feature
385396
document.addEventListener('turbolinks:load', function () {
386397

0 commit comments

Comments
 (0)