Created
July 20, 2016 08:09
-
-
Save banesto/4cb0a701691dd2c69e38024a57ac581b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $button = $(button), | |
target = $button.data('target'), | |
$parent = $('.pollWrapped'); | |
// simultanious action | |
$(target).stop(false, false).toggleClass('open').slideToggle(100).siblings().removeClass('open').slideUp(100); | |
var action = ($(target).parent().find('.open').length) ? 'addClass' : 'removeClass'; | |
$parent[action]('showUpload'); | |
// Common functions for promises & deferred example | |
function animation_1(resolve) { | |
$(target).stop(false, false).slideToggle(100, resolve); | |
} | |
function animation_2(resolve) { | |
$(target).siblings().slideUp(100, resolve); | |
} | |
function shrink() { | |
if ($('.uploadsBlock').find('.fileUploadContainer:visible').length) { | |
$parent.addClass('showUpload'); | |
} else { | |
$parent.removeClass('showUpload'); | |
} | |
} | |
// PROMISES | |
function await_animation(fn) { | |
return new Promise(function(resolve, reject) { | |
fn(resolve); | |
}) | |
} | |
Promise.all([ | |
await_animation(animation_1), | |
await_animation(animation_2), | |
]) | |
.then(shrink); | |
// Deferred | |
var d1 = $.Deferred(), | |
d2 = $.Deferred(); | |
$.when(d1, d2).done(function() { | |
if ($('.uploadsBlock').find('.fileUploadContainer:visible').length) { | |
$parent.addClass('showUpload'); | |
} else { | |
$parent.removeClass('showUpload'); | |
} | |
}); | |
animation_1(d1.resolve.bind(d1)); | |
animation_2(d2.resolve.bind(d2)); | |
// Deferred advanced example | |
function animate(animation_list) { | |
return $.when | |
.apply($, animation_list.map(defer)) | |
} | |
function defer(fn) { | |
var defer = $.Deferred() | |
fn(defer.resolve.bind(defer)) | |
return defer | |
} | |
function shrink() { | |
var action = $('.uploadsBlock').find('.fileUploadContainer:visible').length ? 'addClass' : 'removeClass' | |
$parent[action]('showUpload'); | |
} | |
animate([ | |
function(resolve) { $(target).stop(false, false).slideToggle(1000, resolve) }, | |
function(resolve) { $(target).siblings().slideUp(1000, resolve) }, | |
]) | |
.done(function() { | |
console.log('done'); | |
shrink(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment