/* * Mazama_databrowser.js * * Jonathan Callahan * http://mazamascience.com * * Default behavior for Mazama Science databrowsers. * * 1) Any change to an element in the form should trigger a plot request so that the * current state of the UI always reflects the parameters used to generate the plot. * * 2) Each successful response contains a url for the plot and an array of values * for potential use in this javascript code. * */ /**** GLOBAL VARIABLES ********************************************************/ // All global variables begin with 'G_' var G_myGlobalVariable = 1; var G_maxDate = new Date(2024, 04-1, 12); var G_maxHour = "00"; /**** UTILITY FUNCTIONS *******************************************************/ // Display text as an error message function displayError(errorText) { alert(errorText); } /**** EVENT HANDLERS **********************************************************/ // One layer of abstraction before sending the request allows us to take UI // specific actions, e.g. setting hidden parameters or disabling some elements, // before sending the request. function updatePlot() { // Set any hidden parameter values based on UI state. // ('#hiddenParameter').val(1.0) var userDate = $('#datepicker').datepicker("getDate"); var userZ = $('#modelZ').val(); // Override user choice of 12Z when only 00Z is available for G_maxDate // NOTE: Cannot use '==' comparison with Date objects. if (userDate.toString() == G_maxDate.toString()) { if (G_maxHour == '00' && userZ == '12') { $('#modelZ').val('00'); userZ = '00'; alert("The 12Z model run is not available yet. Resetting to 00Z."); } } dateString = $.datepicker.formatDate('yymmdd',userDate) + userZ; $('#metStart').val(dateString); // // //$('#datepickerText').text(dateString); prePlotActions(); sendRequest(); } // Set styles, disable elements, etc. function prePlotActions() { $('#plotButton').hide(); $('#spinner').fadeIn(1000); } // Reset styles, enable disabled elements, etc. function postPlotActions(JSONResponse) { $('#spinner').hide(); } // Show the "Update" button function showPlotButton() { $('#plotButton').show(); } /**** REQUEST HANDLERS ********************************************************/ function handleJSONResponse(JSONResponse) { if (JSONResponse.status == 'ERROR') { displayError(JSONResponse.error_text); } else { $('#plot').attr('src',JSONResponse.img_url); returnValues = JSONResponse.return_values; // return_values have many potential uses but are not used in the example databrowser } postPlotActions(); } function sendRequest() { var url = '/cgi-bin/trajectories.cgi'; var data = $('#controls_form').serialize(); var fullUrl = url + '?' + data; // Cross browser fix from -- http://tosbourn.com/2013/08/javascript/a-fix-for-window-location-origin-in-internet-explorer/ if (!window.location.origin) { window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: ''); } var requestUrl = window.location.origin + fullUrl var requestUrl = window.location.origin + fullUrl $('#requestUrl').attr('href',requestUrl); if ( ($('#plotType').val() == 'csv') || ($('#plotType').val() == 'kmz') ) { // NOTE: Using window.location.href attempts a reload in the same window. // NOTE: As long as the CGI script is using Content-Disposition: filname=... // NOTE: this will cause a file download without opening a new window. window.location.href = fullUrl; $('#spinner').hide(); $('#requestUrl').show(); } else { $('#requestUrl').hide(); $.getJSON(url, data, handleJSONResponse); } } /**** INITIALIZATION **********************************************************/ $(function() { // Hide the spinner, plotButton and requestUrl $('#spinner').hide(); $('#plotButton').hide(); $('#requestUrl').hide(); // Attach behavior to UI elements $('#plotButton').click(updatePlot); $('#plotType').click(updatePlot); $('#modelZ').click(updatePlot); // Attach behavior to typein fields $('#trajLocationsTable input:text').change(showPlotButton); // Activate tooltips $('span.tooltip').cluetip({width: '500px', attribute: 'id', hoverClass: 'highlight'}); // Set up datepicker var dateString = $.datepicker.formatDate('yy-mm-dd',G_maxDate); $("#datepicker").val(dateString); // NOTE: Date object months start with 0, hence the '9-1' notation for September options = { minDate: new Date(2012,9-1,1), maxDate: G_maxDate, defaultDate: G_maxDate, onSelect: updatePlot, } $('#datepicker').datepicker(options); /* // Use PHP determination of most recent available data var language = $('#language').val(); if (language == "en") { var gfsDate = new Date(); } else { var gfsDate = new Date(); } var dateString = $.datepicker.formatDate('yy-mm-dd',gfsDate); $("#datepicker").val(dateString); // NOTE: Date object months start with 0, hence the '7-1' notation for July (July=7, 7-1=6) options = { minDate: new Date(2012,9-1,1), maxDate: gfsDate, dateFormat: "yy-mm-dd", defaultDate: gfsDate, onSelect: loadNewImages } $("#datepicker").datepicker(options); */ // Generate inital plot updatePlot(); });