var $search_form = $(".search__form"); var $input = $(".search__input"); /***************** * search opened *****************/ $('.js-expandable-search__trigger').on("click", function(e){ e.preventDefault(); }); /***************** * Expandable search gets wider * triggered by focus on .js-expandable-search__trigger *****************/ var cancelSearch = false; var lastFocusedElement = null; var isClick = false; var closing = false; $('.js-expandable-search__trigger').mousedown(function(e) { //see if the form is submitted if($(this).hasClass('search__button--submit') && $input.val() != '') { $search_form.submit(); } isClick = true; //console.log("clicked: true"); //console.log("cancel search: " + cancelSearch); if(cancelSearch) { //console.log("clicked cause search to blur"); $(this).blur(); $input.blur(); closing = true; } else { //console.log("click caused search to show"); $(".js-expandable-search").addClass("is-expanded"); $(".js-expandable-search__nudge").addClass("is-nudged"); $(this).addClass('search__button--submit'); cancelSearch = true; closing = false; } }).focus(function(e){ //console.log("focused: true"); //console.log("cancel search: " + cancelSearch); if(!isClick) { if(cancelSearch) { //console.log("focus caused search to hide"); $(this).blur(); $input.blur(); cancelSearch = false; } else { //console.log("focus caused search to show"); $(".js-expandable-search").addClass("is-expanded"); $(".js-expandable-search__nudge").addClass("is-nudged"); $(this).addClass('search__button--submit'); $input.focus(); cancelSearch = true; } } else { if(!closing) { $input.focus(); } else { $(this).blur(); } } isClick = false; }); $(document).on('blur',".search__input",function() { //console.log("blur caused search to close"); $(".js-expandable-search").removeClass("is-expanded"); $('.js-expandable-search__trigger').removeClass('search__button--submit'); $(".js-expandable-search__nudge").removeClass("is-nudged"); cancelSearch = false; }); /***************** * button dropdown ********************/ $(".js-button-dropdown").addClass("is-enabled"); $("[data-dropdown-trigger]").addClass("is-collapsed").click(function(e){ e.preventDefault(); var $trigger = $(this), id = $trigger.attr("data-dropdown-trigger"), $target = $("[data-dropdown-target="+id+"]"); if($trigger.is(".is-collapsed")) { $trigger.removeClass("is-collapsed"); $target.slideDown(150); } else { $trigger.addClass("is-collapsed"); $target.slideUp(150); } }); $("[data-dropdown-target],[data-dropdown-trigger]").click(function(e){ e.stopPropagation(); }); $("body").click(function(e){ $("[data-dropdown-trigger]").not(".is-collapsed").each(function(){ var id = $(this).attr("data-dropdown-trigger"); $("[data-dropdown-target="+id+"]").slideUp(200); $(this).addClass("is-collapsed"); }); }); $('.js-open-subscribe-dropdown').click(function(e) { e.preventDefault(); e.stopPropagation(); toggleSubscribe(); }); $('.js-open-login-dropdown').click(function(e) { e.preventDefault(); e.stopPropagation(); toggleLogin(); }); /* stop propogation in nav-dropdown to avoid closing */ $('.subscribe-dropdown').click(function(e){ e.stopPropagation(); }); $('.login-dropdown').click(function(e){ e.stopPropagation(); }); $(document).click(function(e){ if($(".subscribe-dropdown").is(".is-open")) {toggleSubscribe();} }); $(document).click(function(e){ if($(".login-dropdown").is(".is-open")) {toggleLogin();} }); function toggleSubscribe() { if($(".login-dropdown").is(".is-open")) { toggleLogin(); } $('.js-open-subscribe-dropdown').toggleClass('active'); $('.subscribe-dropdown').toggleClass('is-open'); } function toggleLogin() { if($(".subscribe-dropdown").is(".is-open")) { toggleSubscribe(); } $('.js-open-login-dropdown').toggleClass('active'); $('.login-dropdown').toggleClass('is-open'); } /* Banner reposition */ function placeBanner() { if($(window).width() < 769) { $(".banner.mobile").show(); if($(".banner.mobile").length == 0) { $(".banner").hide(); $(".article--home.article--first").before(""); } } else { $(".banner").show(); $(".banner.mobile").hide(); } } placeBanner(); $(window).resize($.throttle( 250, function() { placeBanner(); })); // only youtube videos less than 450px if($('iframe[src*="youtube.com"]').attr("width") < 450) { // wrap all youtube videos in a .youtube-video container $('iframe[src*="youtube.com"]').wrap("
"); } //toggle site nav on mobile $(".js-site-header__nav-toggle").click(function(e) { e.preventDefault(); $(".site-nav").toggleClass("is-open"); }); // generate republish markup var articleMarkup = ""; articleMarkup += $(".article__title").parent().html(); articleMarkup += $(".article-content").html(); articleMarkup = articleMarkup.replace(/src=\"\//g, 'src="proxy.php?url=https%3A%2F%2Fsciencenode.org%2F"); articleMarkup = articleMarkup.replace(/src=\"\.\.\//g, 'src="proxy.php?url=https%3A%2F%2Fsciencenode.org%2F"); articleMarkup = articleMarkup.replace(/href=\"\//g, 'href="proxy.php?url=https%3A%2F%2Fsciencenode.org%2F"); articleMarkup = articleMarkup.replace(/href=\"\.\.\//g, 'href="proxy.php?url=https%3A%2F%2Fsciencenode.org%2F"); $(".republish__markup").html(articleMarkup.trim()); $(".republish__markup").append('<p>This article was originally published on <a href="proxy.php?url=https%3A%2F%2Fsciencenode.org" target="_blank">ScienceNode.org</a>. Read the <a href="'+$('meta[property="og:url"]').attr('content')+'" target="_blank">original article</a>.</p>'); $(".republish__markup").append('<script type="text/javascript">(function(e,t,n,r,i,s,o){e["GoogleAnalyticsObject"]=i;e[i]=e[i]||function(){(e[i].q=e[i].q||[]).push(arguments)},e[i].l=1*new Date;s=t.createElement(n),o=t.getElementsByTagName(n)[0];s.async=1;s.src=r;o.parentNode.insertBefore(s,o)})(window,document,"script","//www.google-analytics.com/ga.js","ga");var _gaq=_gaq||[];var _gaq=_gaq||[];_gaq.push(["_setAccount","UA-1781270-1"],["_trackEvent","article interaction","republished",null,true])</script>'); $(".republish__markup").focus(function() { var $this = $(this); $this.select(); // Work around Chrome's little problem $this.mouseup(function() { // Prevent further mouseup intervention $this.unbind("mouseup"); return false; }); }); // Add captions to images using the alt text var altText = false; var floatDir = "right"; var htmlCode = ""; var width = "420"; $('.article-content img, .article-content .youtube-video').each(function() { altText = $(this).attr("alt"); if(altText && !$(this).parents('.caption').length) { $(this).removeAttr("align"); $(this).removeAttr("style"); $(this).wrap("
"); htmlCode = htmlUnescape("
"+altText+"
"); $(this).after(htmlCode); if(floatDir == "right") { floatDir = "left"; } else { floatDir = "right"; } } if($(this).hasClass("youtube-video") && !$(this).parent().hasClass("override")) { width = $('iframe[src*="youtube.com"]').attr("width"); if($(this).parents('.caption').length) { if($(this).parents('.caption--full').length == 0) { $(this).parent().attr("style","padding: 1em 2em; float: " + floatDir + "; max-width:" + width + "px"); } else { $(this).parent().attr("style","padding: 1em 2em; float: " + floatDir); } } else { $(this).attr("style","padding: 1em 2em; float: " + floatDir + "; max-width:" + width + "px"); } if(floatDir == "right") { floatDir = "left"; } else { floatDir = "right"; } } }); // replace http:// version of videos $(window).load(function() { $('.article-content iframe').each(function() { if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) { // image was broken, replace with your new image this.src = this.src.replace('http://', 'https://'); return true; } }); }); $(window).load(function() { $('.article-content img').each(function() { if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) { // image was broken, replace with your new image imgError(this); } }); }); function htmlUnescape(value){ return String(value) .replace(/"/g, '"') .replace(/'/g, "'") .replace(/</g, '<') .replace(/>/g, '>') .replace(/&/g, '&'); } // fix some of the images that have &20(1) at the end function imgError(image) { image.onerror = ""; image.src = image.src.replace('%20(1)', ''); image.src = image.src.replace('(', ''); image.src = image.src.replace(')', ''); image.src = image.src.replace('%26', 'and'); image.src = image.src.replace(".%20", ' '); image.src = image.src.replace("._", '_'); return true; } // Fancybox $(document).ready(function() { if($(".fancybox").length) { $(".fancybox").fancybox({ afterLoad: function(){ this.title = $(this.element).find('img').attr('alt').replace(/\<\;/g,"<").replace(/\>\;/g,">"); }, helpers: { title : { type : 'inside' }, overlay: { locked: false } }, 'titleFromAlt':true }); } }); // subscriber $("form[name='subscribeForm']").submit(function(e) { // this code prevents form from actually being submitted //e.preventDefault(); //e.returnValue = false; var subscriberEmail = $(".subscribe-dropdown__email").val(); var subscriberLocation = $(".subscribe-dropdown__location").val(); // this is the important part. you want to submit // the form but only after the ajax call is completed $.ajax({ type: 'POST', url: "/subscribe/subscribe.php", async:false, data: { email: subscriberEmail, location: subscriberLocation }, success: function() { // your success handler }, error: function() { // your error handler }, complete: function() { // make sure that you are no longer handling the submit event; clear handler //this.off('submit'); // actually submit the form this.submit(); } }); });