From ea2f430c0093c5c3baeb4865e4eeed613a9e6a10 Mon Sep 17 00:00:00 2001 From: jonsowman Date: Thu, 13 Jan 2011 16:54:37 +0000 Subject: [PATCH] Exported all the cookie related functions from the main .js include file to their own one --- predict/index.php | 1 + predict/js/pred-cookie.js | 132 ++++++++++++++++++++++++++++++++++++++ predict/js/pred.js | 112 ++------------------------------ 3 files changed, 138 insertions(+), 107 deletions(-) create mode 100644 predict/js/pred-cookie.js diff --git a/predict/index.php b/predict/index.php index 2ddacf1..87d6a58 100644 --- a/predict/index.php +++ b/predict/index.php @@ -41,6 +41,7 @@ google.load("jqueryui", "1.8.1"); + diff --git a/predict/js/pred-cookie.js b/predict/js/pred-cookie.js new file mode 100644 index 0000000..0a65dc1 --- /dev/null +++ b/predict/js/pred-cookie.js @@ -0,0 +1,132 @@ +/* + * CUSF Landing Prediction Version 2 + * Jon Sowman 2010 + * jon@hexoc.com + * http://www.hexoc.com + * + * http://github.com/jonsowman/cusf-standalone-predictor + * + * This file contains all the cookie-related functions for the landing + * predictor + * + */ + +function saveLocationToCookie() { + // Get the variables from the form + var req_lat = $("#req_lat").val(); + var req_lon = $("#req_lon").val(); + var req_alt = $("#req_alt").val(); + var req_name = $("#req_name").val(); + var cookie_name = "cusf_predictor"; + var locations_limit = 5; + var name_limit = 20; + + // Check the length of the name + if ( req_name.length > name_limit ) { + req_name = req_name.substr(0, name_limit); + } + + // Now let's init the cookie + $.Jookie.Initialise(cookie_name, 99999999); + if ( !$.Jookie.Get(cookie_name, "idx") ) { + $.Jookie.Set(cookie_name, "idx", 0); + var idx = 0; + } else { + var idx = $.Jookie.Get(cookie_name, "idx"); + } + + if ( $.Jookie.Get(cookie_name, "idx") >= locations_limit ) { + $("#location_save").fadeOut(); + throwError("You may only save " + locations_limit + + " locations - please delete some."); + } else { + // Find the next free index we can use + var i=1; + while ( $.Jookie.Get(cookie_name, i+"_name") && i<=locations_limit ) { + i++; + } + + // We will use this idx for the next location + $.Jookie.Set(cookie_name, i+"_lat", req_lat); + $.Jookie.Set(cookie_name, i+"_lon", req_lon); + $.Jookie.Set(cookie_name, i+"_alt", req_alt); + $.Jookie.Set(cookie_name, i+"_name", req_name); + + // Increase the index + idx++; + $.Jookie.Set(cookie_name, "idx", idx); + + // Close dialog and let the user know it worked + $("#location_save").hide(); + appendDebug("Successfully saved the location to cookie " + cookie_name); + } + +} + +// For when the user clicks the "Custom" link for Launch Site +// Construct and display a table of their custom saved locations stored +// in a cookie, and display it +function constructCookieLocationsTable(cookie_name) { +var t = ""; +t += ""; + +$.Jookie.Initialise(cookie_name, 99999999); +if ( !$.Jookie.Get(cookie_name, "idx") || $.Jookie.Get(cookie_name, "idx") == 0 ) { + throwError("You haven't saved any locations yet. Please click Save Location to do so."); + return false; +} else { + idx = $.Jookie.Get(cookie_name, "idx"); + t += ""; + var i=1; + var j=0; + while ( j"; + t += ""; + j++; + } + i++; + } + t += "
NameUseDelete
"; + t += "Use"; + t += ""; + t += "Delete"; + t += "
"; + $("#locations_table").html(t); + return true; +} +} + +// Given a cookie name and an location index, fill the launch card fields +// before hiding the Custom locations window and plotting the new launch +// site +function setCookieLatLng(cookie_name, idx) { + var req_lat = $.Jookie.Get(cookie_name, idx+"_lat"); + var req_lon= $.Jookie.Get(cookie_name, idx+"_lon"); + var req_alt = $.Jookie.Get(cookie_name, idx+"_alt"); + $("#lat").val(req_lat); + $("#lon").val(req_lon); + $("#initial_alt").val(req_alt); + // Now hide the window + $("#location_save_local").fadeOut(); + SetSiteOther(); + plotClick(); +} + +// Given a cookie name and a location index, delete the custom location that +// is in the cookie +function deleteCookieLocation(cookie_name, idx) { + // Delete the location in the cookie + $.Jookie.Unset(cookie_name, idx+"_lat"); + $.Jookie.Unset(cookie_name, idx+"_lon"); + $.Jookie.Unset(cookie_name, idx+"_alt"); + $.Jookie.Unset(cookie_name, idx+"_name"); + // Decrease quantity in cookie by one + var qty = $.Jookie.Get(cookie_name, "idx"); + qty -= 1; + $.Jookie.Set(cookie_name, "idx", qty); + // Now hide the window + $("#location_save_local").fadeOut(); +} + diff --git a/predict/js/pred.js b/predict/js/pred.js index 9fd4dcf..a4430ba 100644 --- a/predict/js/pred.js +++ b/predict/js/pred.js @@ -549,59 +549,17 @@ function setupEventHandlers() { } } }); + + // Activate the "Save" button in the "Save Location to Cookie" window $("#req_sub_btn").click(function() { - // Get the variables from the form - var req_lat = $("#req_lat").val(); - var req_lon = $("#req_lon").val(); - var req_alt = $("#req_alt").val(); - var req_name = $("#req_name").val(); - var cookie_name = "cusf_predictor"; - var locations_limit = 5; - var name_limit = 20; - - // Check the length of the name - if ( req_name.length > name_limit ) req_name = req_name.substr(0, name_limit); - - // Now let's init the cookie - $.Jookie.Initialise(cookie_name, 99999999); - if ( !$.Jookie.Get(cookie_name, "idx") ) { - $.Jookie.Set(cookie_name, "idx", 0); - var idx = 0; - } else { - var idx = $.Jookie.Get(cookie_name, "idx"); - } - - if ( $.Jookie.Get(cookie_name, "idx") >= locations_limit ) { - $("#location_save").fadeOut(); - throwError("You may only save "+locations_limit+" locations - please delete some."); - } else { - // Find the next free index we can use - var i=1; - while ( $.Jookie.Get(cookie_name, i+"_name") && i<=locations_limit ) { - i++; - } - // We will use this idx for the next location - $.Jookie.Set(cookie_name, i+"_lat", req_lat); - $.Jookie.Set(cookie_name, i+"_lon", req_lon); - $.Jookie.Set(cookie_name, i+"_alt", req_alt); - $.Jookie.Set(cookie_name, i+"_name", req_name); - - // Increase the index - idx++; - $.Jookie.Set(cookie_name, "idx", idx); - - // Close dialog and let the user know it worked - $("#location_save").hide(); - appendDebug("Successfully saved the location to cookie " + cookie_name); - } - + saveLocationToCookie(); }); - // activate the "Set with Map" link + // Activate the "Set with Map" link $("#setWithClick").click(function() { setLatLonByClick(true); }); - // activate the "use burst calc" links + // Activate the "use burst calc" links $("#burst-calc-show").click(function() { $("#burst-calc-wrapper").show(); }); @@ -694,8 +652,6 @@ function setupEventHandlers() { $("#req_lat").val(lat); $("#req_lon").val(lon); $("#req_alt").val($("#initial_alt").val()); - // this is bad, use a geocoder to guess it - $("#req_name").val("Unnamed"); appendDebug("Trying to reverse geo-code the launch point"); rvGeocode(lat, lon, "req_name"); $("#location_save").fadeIn(); @@ -723,64 +679,6 @@ function rvGeocode(lat, lon, fillField) { }); } -function constructCookieLocationsTable(cookie_name) { - var t = ""; - t += ""; - - $.Jookie.Initialise(cookie_name, 99999999); - if ( !$.Jookie.Get(cookie_name, "idx") || $.Jookie.Get(cookie_name, "idx") == 0 ) { - throwError("You haven't saved any locations yet. Please click Save Location to do so."); - return false; - } else { - idx = $.Jookie.Get(cookie_name, "idx"); - t += ""; - var i=1; - var j=0; - while ( j"; - t += ""; - j++; - } - i++; - } - t += "
NameUseDelete
"; - t += "Use"; - t += ""; - t += "Delete"; - t += "
"; - $("#locations_table").html(t); - return true; - } -} - -function setCookieLatLng(cookie_name, idx) { - var req_lat = $.Jookie.Get(cookie_name, idx+"_lat"); - var req_lon= $.Jookie.Get(cookie_name, idx+"_lon"); - var req_alt = $.Jookie.Get(cookie_name, idx+"_alt"); - $("#lat").val(req_lat); - $("#lon").val(req_lon); - $("#initial_alt").val(req_alt); - // now hide the window - $("#location_save_local").fadeOut(); - SetSiteOther(); - plotClick(); -} - -function deleteCookieLocation(cookie_name, idx) { - // Delete the location in the cookie - $.Jookie.Unset(cookie_name, idx+"_lat"); - $.Jookie.Unset(cookie_name, idx+"_lon"); - $.Jookie.Unset(cookie_name, idx+"_alt"); - $.Jookie.Unset(cookie_name, idx+"_name"); - // Decrease quantity in cookie by one - var qty = $.Jookie.Get(cookie_name, "idx"); - qty -= 1; - $.Jookie.Set(cookie_name, "idx", qty); - $("#location_save_local").fadeOut(); -} - function POSIXtoHM(timestamp, format) { // using JS port of PHP's date() var ts = new Date();