From 3057945b70c799b9829c377100d525231838b767 Mon Sep 17 00:00:00 2001 From: Matthew Harrison-Jones Date: Fri, 26 Jul 2013 12:32:26 +0100 Subject: [PATCH] Refactored menu fading to be a plugin for extendability. Need to find suitable place for the `.overlay` hideAway function call. --- core/client/admin-ui-temp.js | 12 ----------- core/client/assets/lib/jquery-utils.js | 28 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/core/client/admin-ui-temp.js b/core/client/admin-ui-temp.js index 85bdf797d7..40c29eee50 100644 --- a/core/client/admin-ui-temp.js +++ b/core/client/admin-ui-temp.js @@ -83,18 +83,6 @@ $(document).ready(function () { - // ## Set interactions for all menus - // This finds all visible '.overlay' elements and hides them upon clicking away from the element itself. - $("body").on('click', function (event) { - var $target = $(event.target); - if (!$target.parents().is(".overlay:visible") && !$target.is(".overlay:visible")) { - $("body").find(".overlay:visible").fadeOut(); - - // Toggle active classes on menu headers - $("[data-toggle].active").removeClass("active"); - } - }); - // LOGIN SCREEN $(window).trigger('resize'); diff --git a/core/client/assets/lib/jquery-utils.js b/core/client/assets/lib/jquery-utils.js index f82e4c3054..bb183ab0fa 100644 --- a/core/client/assets/lib/jquery-utils.js +++ b/core/client/assets/lib/jquery-utils.js @@ -54,4 +54,32 @@ } }; + /** + * Set interactions for all menus and overlays + * This finds all visible 'hideClass' elements and hides them upon clicking away from the element itself. + * A callback can be defined to customise the results. By default it will hide the element. + * @param callback + */ + $.fn.hideAway = function (callback) { + var $self = $(this); + $("body").on('click', function (event) { + var $target = $(event.target), + hideClass = $self.selector; + if (!$target.parents().is(hideClass + ":visible") && !$target.is(hideClass + ":visible")) { + if (callback) { + callback($("body").find(hideClass + ":visible")); + } else { + $("body").find(hideClass + ":visible").fadeOut(); + + // Toggle active classes on menu headers + $("[data-toggle].active").removeClass("active"); + } + } + }); + + return this; + }; + + $('.overlay').hideAway(); // TODO: Move to a more sensible global file. + }()); \ No newline at end of file