-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Friedman <[email protected]>
- Loading branch information
Showing
1 changed file
with
56 additions
and
20 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,33 +1,69 @@ | ||
(function($) { // Avoid conflicts with other libraries | ||
document.addEventListener('DOMContentLoaded', function() { | ||
|
||
'use strict'; | ||
'use strict'; | ||
|
||
$(function() { | ||
// Helper function to get elements by selector | ||
const $ = selector => document.querySelectorAll(selector); | ||
|
||
$.fn.toggleBoxes = function(target) { | ||
$(this).on('click', function() { | ||
if ($(this).is(':checked')) { | ||
$(target).prop('checked', false); | ||
// Function to toggle checkboxes | ||
function toggleBoxes(elements, target) { | ||
elements.forEach(element => { | ||
element.addEventListener('click', () => { | ||
if (element.checked) { | ||
$(target).forEach(box => { | ||
box.checked = false; | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Call toggleBoxes for .html-on and .html-off elements | ||
toggleBoxes($('.html-on'), '.html-off'); | ||
toggleBoxes($('.html-off'), '.html-on'); | ||
|
||
// Page route autofill when empty using Page title | ||
const pageTitle = document.getElementById('page_title'); | ||
const pageRoute = document.getElementById('page_route'); | ||
|
||
pageTitle.addEventListener('blur', () => { | ||
const title = pageTitle.value; | ||
const route = pageRoute.value; | ||
|
||
pageRoute.value = route ? route : slugify(title); | ||
}); | ||
|
||
// Slugify a string to be url friendly | ||
function slugify(title) { | ||
return title | ||
.toLowerCase() | ||
.replace(/[^a-z\d\-_\s]/g, '') | ||
.replace(/\s+/g, '-') | ||
.trim(); | ||
} | ||
|
||
// Page Icon Previews | ||
const updateClasses = (element, newClass) => { | ||
|
||
const classesToKeep = ['o-icon', 'o-icon-font', 'fa-fw', 'fas', 'acp-icon']; | ||
|
||
element.classList.forEach(className => { | ||
if (!classesToKeep.includes(className)) { | ||
element.classList.remove(className); | ||
} | ||
}); | ||
|
||
element.classList.add(`fa-${newClass}`); | ||
}; | ||
|
||
$('.html-on').toggleBoxes('.html-off'); | ||
$('.html-off').toggleBoxes('.html-on'); | ||
const pageIconFont = document.getElementById('page_icon_font'); | ||
|
||
$('#page_title').on('blur', function() { | ||
var title = $(this).val(); | ||
$('#page_route').val(function(event, route) { | ||
return (route) ? route : title.toLowerCase().replace(/[^a-z\d\-_\s]/gi, '').trim().replace(/\s+/g, '-'); | ||
}); | ||
pageIconFont.addEventListener('keyup', function() { | ||
updateClasses(this.nextElementSibling, this.value); | ||
}); | ||
|
||
$('#page_icon_font').on('keyup blur', function() { | ||
var input = $(this).val(); | ||
var $icon = $(this).next('i'); | ||
$icon.attr('class', 'o-icon o-icon-font acp-icon fa-' + input); | ||
pageIconFont.addEventListener('blur', function() { | ||
updateClasses(this.nextElementSibling, this.value); | ||
}); | ||
|
||
}); | ||
|
||
})(jQuery); // Avoid conflicts with other libraries |