// ------------------------------------------------------------------------------------------- // toggle shortcode javascript // ------------------------------------------------------------------------------------------- (function($) { "use strict"; $.fn.avia_sc_toggle = function(options) { var defaults = { single: '.single_toggle', heading: '.toggler', content: '.toggle_wrap', sortcontainer:'.taglist' }; var win = $(window), options = $.extend(defaults, options); return this.each(function() { var container = $(this).addclass('enable_toggles'), toggles = $(options.single, container), heading = $(options.heading, container), activestyle = $(container).attr('data-currentstyle'), allcontent = $(options.content, container), sortlinks = $(options.sortcontainer + " a", container); heading.each(function(i) { var thisheading = $(this), content = thisheading.next(options.content, container), headingstyle = thisheading.attr('style'), hoverstyle = thisheading.data( 'hoverstyle' ); if( 'undefined' == typeof headingstyle ) { headingstyle = ''; } if( 'undefined' == typeof hoverstyle ) { hoverstyle = ''; } function scroll_to_viewport() { //check if toggle title is in viewport. if not scroll up var el_offset = content.offset().top, scoll_target = el_offset - 50 - parseint($('html').css('margin-top'),10); if(win.scrolltop() > el_offset) { $('html:not(:animated),body:not(:animated)').animate({scrolltop: scoll_target},200); } } if(content.css('visibility') != "hidden") { thisheading.addclass('activetitle').attr('style',activestyle); } thisheading.on('keydown', function(objevent) { if (objevent.keycode === 13) { // if user presses 'enter' thisheading.trigger('click'); } }); thisheading.on( 'click', function() { if(content.css('visibility') != "hidden") { content.slideup(200, function() { content.removeclass('active_tc').attr({style:''}); win.trigger('av-height-change'); win.trigger('av-content-el-height-changed', this ); location.replace(thisheading.data('fake-id') + "-closed"); }); thisheading.removeclass('activetitle').attr('style',headingstyle); } else { if(container.is('.toggle_close_all')) { allcontent.not(content).slideup(200, function() { $(this).removeclass('active_tc').attr({style:''}); scroll_to_viewport(); }); heading.removeclass('activetitle').attr('style',headingstyle); } content.addclass('active_tc'); settimeout(function(){ content.slidedown(200, function() { if(!container.is('.toggle_close_all')) { scroll_to_viewport(); } win.trigger('av-height-change'); win.trigger('av-content-el-height-changed', this ); } ); }, 1); thisheading.addclass('activetitle').attr('style',activestyle); location.replace(thisheading.data('fake-id')); } }); /* change style on hover */ if( hoverstyle ) { thisheading.hover( function() { if ( ! thisheading.hasclass('activetitle') ) { $(this).attr( 'style', hoverstyle ); } }, function() { if ( ! thisheading.hasclass('activetitle') ) { $(this).attr( 'style', headingstyle ); } } ); } }); sortlinks.on( 'click', function(e){ e.preventdefault(); var show = toggles.filter('[data-tags~="'+$(this).data('tag')+'"]'), hide = toggles.not('[data-tags~="'+$(this).data('tag')+'"]'); sortlinks.removeclass('activefilter'); $(this).addclass('activefilter'); heading.filter('.activetitle').trigger('click'); show.slidedown(); hide.slideup(); }); function trigger_default_open(hash) { if(!hash && window.location.hash) hash = window.location.hash; if(!hash) return; var open = heading.filter('[data-fake-id="'+hash+'"]'); if(open.length) { if(!open.is('.activetitle')) open.trigger('click'); window.scrollto(0, container.offset().top - 70); } } trigger_default_open(false); $('a').on( 'click',function(){ var hash = $(this).attr('href'); if(typeof hash != "undefined" && hash) { hash = hash.replace(/^.*?#/,''); trigger_default_open('#'+hash); } }); }); }; }(jquery));