From 43dc4da7e2b449cedcfc76d2ddf36757e3714b80 Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Wed, 19 Feb 2014 14:55:30 -0500 Subject: [PATCH] abstract mobile interactions js DRY up repeated code and simplify logic. --- core/client/mobile-interactions.js | 42 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/core/client/mobile-interactions.js b/core/client/mobile-interactions.js index 2509abc81c..853c9e3328 100644 --- a/core/client/mobile-interactions.js +++ b/core/client/mobile-interactions.js @@ -7,54 +7,56 @@ FastClick.attach(document.body); + // ### general wrapper to handle conditional screen size actions + function responsiveAction(event, mediaCondition, cb) { + if (!window.matchMedia(mediaCondition).matches) { + return; + } + + event.preventDefault(); + event.stopPropagation(); + cb(); + } + // ### Show content preview when swiping left on content list $('.manage').on('click', '.content-list ol li', function (event) { - if (window.matchMedia('(max-width: 800px)').matches) { - event.preventDefault(); - event.stopPropagation(); + responsiveAction(event, '(max-width: 800px)', function () { $('.content-list').animate({right: '100%', left: '-100%', 'margin-right': '15px'}, 300); $('.content-preview').animate({right: '0', left: '0', 'margin-left': '0'}, 300); - } + }); }); // ### Hide content preview $('.manage').on('click', '.content-preview .button-back', function (event) { - if (window.matchMedia('(max-width: 800px)').matches) { - event.preventDefault(); - event.stopPropagation(); + responsiveAction(event, '(max-width: 800px)', function () { $('.content-list').animate({right: '0', left: '0', 'margin-right': '0'}, 300); $('.content-preview').animate({right: '-100%', left: '100%', 'margin-left': '15px'}, 300); - } + }); }); // ### Show settings options page when swiping left on settings menu link $('.settings').on('click', '.settings-menu li', function (event) { - if (window.matchMedia('(max-width: 800px)').matches) { - event.preventDefault(); - event.stopPropagation(); + responsiveAction(event, '(max-width: 800px)', function () { $('.settings-sidebar').animate({right: '100%', left: '-102%', 'margin-right': '15px'}, 300); $('.settings-content').animate({right: '0', left: '0', 'margin-left': '0'}, 300); $('.settings-content .button-back, .settings-content .button-save').css('display', 'inline-block'); - } + }); }); // ### Hide settings options page $('.settings').on('click', '.settings-content .button-back', function (event) { - if (window.matchMedia('(max-width: 800px)').matches) { - event.preventDefault(); - event.stopPropagation(); + responsiveAction(event, '(max-width: 800px)', function () { $('.settings-sidebar').animate({right: '0', left: '0', 'margin-right': '0'}, 300); $('.settings-content').animate({right: '-100%', left: '100%', 'margin-left': '15'}, 300); $('.settings-content .button-back, .settings-content .button-save').css('display', 'none'); - } + }); }); // ### Toggle the sidebar menu $('[data-off-canvas]').on('click', function (event) { - if (window.matchMedia('(max-width: 650px)').matches) { - event.preventDefault(); + responsiveAction(event, '(max-width: 650px)', function () { $('body').toggleClass('off-canvas'); - } + }); }); -}()); \ No newline at end of file +}());