hue_popup = new Object(); hue_popup.trigger_sel = '.hue-popup'; hue_popup.close_sel = '.hue-popup-close'; hue_popup.noclose_sel = '.hue-popup-noclose'; hue_popup.source_sel = '.hue-popup-source'; hue_popup.target_sel = '.hue-popup-target'; hue_popup.title_prefix = ''; hue_popup.title_postfix = ''; hue_popup.cb_trigger = 'popupTriggered'; hue_popup.cb_content_loaded = 'popupContentLoaded'; hue_popup.cb_open = 'popupOpen'; hue_popup.cb_close = 'popupClosed'; //hue_popup.url = ''; hue_popup.base_url = ''; hue_popup.base_title = ''; hue_popup.is_open = false; hue_popup.content_loading = false; // set to true while content is being loaded $ = jQuery; $(function() { // Prepare var History = window.History; // Note: We are using a capital H instead of a lower h // Bind to StateChange Event History.Adapter.bind(window, 'statechange', function(e) { // Note: We are using statechange instead of popstate var State = History.getState(); // Note: We are using History.getState() instead of event.state //History.log(State.data, State.title, State.url); if (!State.data.popup) { // going back to index site (no popup) // close popup close_popup(); } else { // going to a popup site // going to popup site load_content(State.url); if (!hue_popup.is_open) { open_popup(); } } }); /* open popup. block background scrolling fade in overlay fade in content */ function open_popup() { // callback //console.log('invoke callback: open'); call_by_string(hue_popup.cb_open); // let the user handle this (via callback) //$(hue_popup.target_sel).fadeIn(hue_overlay.fadein); //$(hue_popup.content_sel).delay(hue_overlay.content_delay).fadeIn(hue_overlay.content_fadein); hue_popup.is_open = true; return false; } /* close popup. hide overlay unblock scrolling */ function close_popup() { //console.log('close overlay'); // let the user handle this (via callback) // $(hue_popup.content_sel).hide(); // instantly hide content box // $(hue_popup.target_sel).fadeOut(hue_overlay.fadeout); // fade out overlay // callback //console.log('invoke callback: close'); call_by_string(hue_popup.cb_close); hue_popup.is_open = false; return false; } /* load content into the popup. optionally display loading animation until the content is ready */ function load_content(url) { hue_popup.content_loading = true; // get content via ajax //target.load(url + ' ' + hue_popup.source_sel , '', content_loaded); // use custom get function to append rather then replace the target $.get(url, function(data, textStatus, jqXHR) { // invoke callback function $('.hue-popup-overlay').scrollTop(0); content_loaded(data, textStatus, jqXHR); hue_popup.content_loading = false; }); } /* ajax content loaded event */ function content_loaded(data, textStatus, jqXHR) { //console.log('content loaded: ' + textStatus) // inject content into target var content = $(data).find(hue_popup.source_sel); // filter data //console.log(content.find("h1.title").text()); $(hue_popup.source_sel).remove(); $(hue_popup.target_sel).append(content); // place content at the end of target element /* add triggers */ //don't close when clicking in designated areas $(hue_popup.noclose_sel).on('click', function(e) { //console.log(e.target); e.stopPropagation(); // allows links, but won't close overlay }); // add triggers for in-popup navigation $(hue_popup.target_sel + ' ' + hue_popup.trigger_sel).click(function(e) { // callback //console.log("callback: trigger"); call_by_string(hue_popup.cb_trigger, [e]); var url = $(this).attr('href'); //console.log("in-popup: " + url); History.pushState({popup: true}, hue_popup.title_prefix + ' ' + $(this).attr('title') + ' ' + hue_popup.title_postfix, url); return false; }); /* history manipulation */ $(hue_popup.target_sel + ' ' + hue_popup.close_sel).click(function() { /*if ( huePopupHistory > 1 ) { window.history.go(-1); huePopupHistory--; return false; } History.pushState({popup: false}, hue_popup.base_title, hue_popup.base_url); return false;*/ }); //$(hue_popup.content_sel).hide(); // invoke callback function //console.log('callback: content loaded'); call_by_string(hue_popup.cb_content_loaded, [data, textStatus, jqXHR]); } /* call a global function by string, optionally with an array of arguments */ function call_by_string(string, argumentsArray) { var fn = window[string]; if (typeof fn === 'function') { if (!argumentsArray) fn(); else fn.apply(null, argumentsArray); } } /* trigger */ /*$(hue_popup.trigger_sel).click(function(e) { // callback //console.log("callback: trigger"); call_by_string(hue_popup.cb_trigger, [e]); // save base url and title when triggering the popup for the first time hue_popup.base_url = $(location).attr('href'); hue_popup.base_title = $(document).attr('title'); //get content url var url = $(this).attr('href'); //console.log("popup: " + url); History.pushState({popup: true}, hue_popup.title_prefix + ' ' + $(this).attr('title') + ' ' + hue_popup.title_postfix, url); return false; });*/ $("body").on("click", hue_popup.trigger_sel, function(e) { // callback //console.log("callback: trigger"); if ( viewportSize.getWidth() >= 1024 ) { call_by_string(hue_popup.cb_trigger, [e]); // save base url and title when triggering the popup for the first time if ( hue_popup.base_url == "" ) hue_popup.base_url = $(location).attr('href'); //console.log("base: " + hue_popup.base_url); hue_popup.base_title = $(document).attr('title'); //get content url var url = $(this).attr('href'); //console.log("popup: " + url); History.pushState({popup: true}, hue_popup.title_prefix + ' ' + $(this).attr('title') + ' ' + hue_popup.title_postfix, url); return false; } }); // close trigger $(hue_popup.close_sel).click(function(e) { //History.pushState({popup: false}, hue_popup.base_title, hue_popup.base_url); window.history.go(-1); }); });