abstract mobile interactions js

DRY up repeated code and simplify logic.
This commit is contained in:
Sam Saccone 2014-02-19 14:55:30 -05:00
parent 6af7d8fa3f
commit 43dc4da7e2

View File

@ -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');
}
});
});
}());
}());