-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert FX switcher to vanilla JavaScript (from jQuery)
- Loading branch information
Showing
1 changed file
with
45 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,56 @@ | ||
jQuery(document).ready(function ($) { | ||
/* Click Tab */ | ||
$(document.body).on('click', '#fxb-switcher a.nav-tab', function (e) { | ||
e.preventDefault(); | ||
document.addEventListener('DOMContentLoaded', function () { | ||
// Click Tab | ||
document.body.addEventListener('click', function (e) { | ||
const target = e.target; | ||
|
||
/* Bail */ | ||
if ($(this).hasClass('nav-tab-active')) { | ||
return false; | ||
} | ||
// Check if the clicked element is a nav-tab link | ||
if (target.matches('#fxb-switcher a.nav-tab')) { | ||
e.preventDefault(); | ||
|
||
/* Confirm ? */ | ||
if (!$(this).hasClass('switch-confirmed')) { | ||
if (true !== confirm($(this).data('confirm'))) { | ||
// Bail if the clicked tab is already active | ||
if (target.classList.contains('nav-tab-active')) { | ||
return false; | ||
} | ||
} | ||
|
||
/* Add Confirmed Class */ | ||
$(this).addClass('switch-confirmed'); | ||
// Confirm switch | ||
if (!target.classList.contains('switch-confirmed')) { | ||
const confirmationMessage = target.getAttribute('data-confirm'); | ||
if (!confirm(confirmationMessage)) { | ||
return false; | ||
} | ||
} | ||
|
||
// Add Confirmed Class | ||
target.classList.add('switch-confirmed'); | ||
|
||
/* Force Switch to Visual Editor */ | ||
$.fn.fxB_switchEditor("fxb_editor"); | ||
// Force Switch to Visual Editor | ||
if (typeof window.fxB_switchEditor === 'function') { | ||
window.fxB_switchEditor('fxb_editor'); | ||
} | ||
|
||
var this_data = $(this).data('fxb-switcher'); | ||
const switcherData = target.getAttribute('data-fxb-switcher'); | ||
|
||
/* Clicking "Editor" */ | ||
if ('editor' == this_data) { | ||
$('html').removeClass('fx_builder_active'); | ||
$('input[name="_fxb_active"]').val(''); | ||
$(this).addClass('nav-tab-active'); | ||
$(this).siblings('.nav-tab').removeClass('nav-tab-active'); | ||
} else if ('builder' == this_data) { | ||
$('html').addClass('fx_builder_active'); | ||
$('input[name="_fxb_active"]').val('1'); | ||
$(this).addClass('nav-tab-active'); | ||
$(this).siblings('.nav-tab').removeClass('nav-tab-active'); | ||
if (switcherData === 'editor') { | ||
document.documentElement.classList.remove('fx_builder_active'); | ||
document.querySelector('input[name="_fxb_active"]').value = ''; | ||
target.classList.add('nav-tab-active'); | ||
const siblings = target.parentElement.querySelectorAll('.nav-tab'); | ||
siblings.forEach(function (sibling) { | ||
if (sibling !== target) { | ||
sibling.classList.remove('nav-tab-active'); | ||
} | ||
}); | ||
} else if (switcherData === 'builder') { | ||
document.documentElement.classList.add('fx_builder_active'); | ||
document.querySelector('input[name="_fxb_active"]').value = '1'; | ||
target.classList.add('nav-tab-active'); | ||
const siblings = target.parentElement.querySelectorAll('.nav-tab'); | ||
siblings.forEach(function (sibling) { | ||
if (sibling !== target) { | ||
sibling.classList.remove('nav-tab-active'); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
}); |