Ghost/core/client/toggle.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2013-05-11 20:44:25 +04:00
// # Toggle Support
/*global document, $, Ghost */
(function () {
'use strict';
2013-05-11 20:44:25 +04:00
Ghost.temporary.hideToggles = function () {
$('[data-toggle]').each(function () {
var toggle = $(this).data('toggle');
2014-01-08 01:19:52 +04:00
$(this).parent().children(toggle + ':visible').fadeOut(150);
});
// Toggle active classes on menu headers
$('[data-toggle].active').removeClass('active');
};
Ghost.temporary.initToggles = function ($el) {
$el.find('[data-toggle]').each(function () {
2013-05-11 20:44:25 +04:00
var toggle = $(this).data('toggle');
$(this).parent().children(toggle).hide();
});
$el.find('[data-toggle]').on('click', function (e) {
2013-05-11 20:44:25 +04:00
e.preventDefault();
e.stopPropagation();
var $this = $(this),
toggle = $this.data('toggle'),
isAlreadyActive = $this.is('.active');
// Close all the other open toggle menus
Ghost.temporary.hideToggles();
if (!isAlreadyActive) {
$this.toggleClass('active');
2014-01-08 01:19:52 +04:00
$(this).parent().children(toggle).toggleClass('open').fadeToggle(150);
}
2013-05-11 20:44:25 +04:00
});
};
$(document).ready(function () {
// ## Toggle Up In Your Grill
// Allows for toggling via data-attributes.
// ### Usage
// <nav>
// <a href="#" data-toggle=".toggle-me">Toggle</a>
// <ul class="toggle-me">
// <li>Toggled yo</li>
// </ul>
// </nav>
Ghost.temporary.initToggles($(document));
2013-05-11 20:44:25 +04:00
});
}());