From d7da1562c3b3c7544e3899e7bc49f7b32c2d2d19 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 30 Aug 2012 23:02:54 +0100 Subject: [PATCH 1/1] Get rid of the sidebar's onclose global state --- app/assets/javascripts/browse.js | 6 ++-- app/assets/javascripts/export.js | 6 ++-- app/assets/javascripts/sidebar.js | 46 ++++++++++--------------------- app/views/site/_key.html.erb | 13 ++++----- 4 files changed, 26 insertions(+), 45 deletions(-) diff --git a/app/assets/javascripts/browse.js b/app/assets/javascripts/browse.js index b7e9c01d5..769cc112e 100644 --- a/app/assets/javascripts/browse.js +++ b/app/assets/javascripts/browse.js @@ -17,7 +17,7 @@ function startBrowse(sidebarHtml) { $("#sidebar_title").html(I18n.t('browse.start_rjs.data_frame_title')); $("#sidebar_content").html(sidebarHtml); - openSidebar({ onclose: stopBrowse }); + openSidebar(); var vectors = new OpenLayers.Layer.Vector(); @@ -53,7 +53,7 @@ function startBrowse(sidebarHtml) { } } - function stopBrowse() { + $("#sidebar").one("closed", function () { if (map.dataLayer.active) { map.dataLayer.active = false; @@ -80,7 +80,7 @@ function startBrowse(sidebarHtml) { map.dataLayer.setVisibility(false); map.events.unregister("moveend", map, updateData); } - } + }); function startDrag() { $("#browse_select_box").html(I18n.t('browse.start_rjs.drag_a_box')); diff --git a/app/assets/javascripts/export.js b/app/assets/javascripts/export.js index a744d0a22..5debb1b07 100644 --- a/app/assets/javascripts/export.js +++ b/app/assets/javascripts/export.js @@ -46,7 +46,7 @@ function startExport(sidebarHtml) { $("#mapnik_scale").change(mapnikSizeChanged); - openSidebar({ onclose: stopExport }); + openSidebar(); if (map.baseLayer.name == "Mapnik") { $("#format_mapnik").prop("checked", true); @@ -58,7 +58,7 @@ function startExport(sidebarHtml) { $("#viewanchor").removeClass("active"); $("#exportanchor").addClass("active"); - function stopExport() { + $("#sidebar").one("closed", function () { $("#viewanchor").addClass("active"); $("#exportanchor").removeClass("active"); @@ -67,7 +67,7 @@ function startExport(sidebarHtml) { map.events.unregister("moveend", map, mapMoved); map.events.unregister("changebaselayer", map, htmlUrlChanged); map.removeLayer(vectors); - } + }); function getMercatorBounds() { var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), diff --git a/app/assets/javascripts/sidebar.js b/app/assets/javascripts/sidebar.js index 4ef379bf5..a5d906980 100644 --- a/app/assets/javascripts/sidebar.js +++ b/app/assets/javascripts/sidebar.js @@ -1,40 +1,24 @@ -var openSidebar; +function openSidebar(options) { + options = options || {}; -(function () { - var onclose; + $("#sidebar").trigger("closed"); - openSidebar = function(options) { - options = options || {}; + if (options.title) { $("#sidebar_title").html(options.title); } - if (onclose) { - onclose(); - onclose = null; - } + if (options.width) { $("#sidebar").width(options.width); } + else { $("#sidebar").width("30%"); } - if (options.title) { $("#sidebar_title").html(options.title); } + $("#sidebar").css("display", "block"); - if (options.width) { $("#sidebar").width(options.width); } - else { $("#sidebar").width("30%"); } + $("#sidebar").trigger("opened"); +}; - $("#sidebar").css("display", "block"); +$(document).ready(function () { + $(".sidebar_close").click(function (e) { + $("#sidebar").css("display", "none"); - $("#sidebar").trigger("opened"); + $("#sidebar").trigger("closed"); - onclose = options.onclose; - }; - - $(document).ready(function () { - $(".sidebar_close").click(function (e) { - $("#sidebar").css("display", "none"); - - $("#sidebar").trigger("closed"); - - if (onclose) { - onclose(); - onclose = null; - } - - e.preventDefault(); - }); + e.preventDefault(); }); -})(); +}); diff --git a/app/views/site/_key.html.erb b/app/views/site/_key.html.erb index 15f64275c..b596692ee 100644 --- a/app/views/site/_key.html.erb +++ b/app/views/site/_key.html.erb @@ -7,17 +7,14 @@ $(document).ready(function () { }); } - var closeMapKey = function () { - map.events.unregister("zoomend", map, updateMapKey); - map.events.unregister("changelayer", map, updateMapKey); - } - $("#open_map_key").click(function () { updateMapKey(); - openSidebar({ - title: "<%= t('site.key.map_key') %>", - onclose: closeMapKey + openSidebar({ title: "<%= t('site.key.map_key') %>" }); + + $("#sidebar").one("closed", function () { + map.events.unregister("zoomend", map, updateMapKey); + map.events.unregister("changelayer", map, updateMapKey); }); map.events.register("zoomend", map, updateMapKey); -- 2.39.5