Last active
August 29, 2015 14:04
-
-
Save tkh44/b977f5d29799280c8346 to your computer and use it in GitHub Desktop.
Simple pull to refresh for angular. Only works on touch devices. Not very good android support.
This file contains 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
<div class="pull-to-refresh"> | |
<h1 class="title" ng-bind="text[status]"></h1> | |
</div> | |
<div ng-transclude></div> |
This file contains 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
(function (window, document, undefined) { | |
var pullToRefresh = angular.module('iOffice.pullToRefresh', []); | |
pullToRefresh.constant('pullToRefreshConfig', { | |
threshold: 60, | |
debounce: 400, | |
parentSelector: '', | |
onRefresh: function(){}, | |
text: { | |
pull: 'Pull down to refresh', | |
release: 'Release to refresh', | |
loading: 'Loading...' | |
}, | |
icon: { | |
pull: 'ion-ios7-arrow-thin-down', | |
release: 'ion-ios7-arrow-thin-up', | |
loading: 'ion-ios7-reloading' | |
} | |
}); | |
pullToRefresh.directive('pullToRefresh', function($timeout, $q, pullToRefreshConfig) { | |
return { | |
restrict: 'A', | |
transclude: true, | |
templateUrl: 'pullToRefresh.html', | |
scope: { | |
pullToRefreshOptions: '=' | |
}, | |
link: function($scope, $element, attrs) { | |
var config = angular.extend({}, pullToRefreshConfig, $scope.pullToRefreshOptions || {}); | |
var shouldReload = false; | |
// The pull to refresh indicator element | |
var ptrElement = $element.children()[0]; | |
// The element that can scroll | |
var $scrollElement = config.parentSelector.length ? | |
angular.element(document.querySelector(config.parentSelector)) : | |
$element.parent(); | |
var setStatus = function(status) { | |
shouldReload = status === 'release'; | |
$scope.$apply(function() { | |
$scope.status = status; | |
}); | |
}; | |
$scope.text = config.text; | |
$scope.icon = config.icon; | |
$scope.status = 'pull'; | |
var onTouchMove = function(e) { | |
var top = $scrollElement[0].scrollTop; | |
if (top < -config.threshold && !shouldReload) { | |
setStatus('release'); | |
} else if (top > -config.threshold && shouldReload) { | |
setStatus('pull'); | |
} | |
}; | |
var onTouchEnd = function(e) { | |
if (!shouldReload) | |
return; | |
ptrElement.style.webkitTransitionDuration = '400ms'; | |
ptrElement.style.margin = '0 auto'; | |
setStatus('loading'); | |
var start = +new Date(); | |
config.onRefresh().then(function () { | |
var elapsed = +new Date() - start; | |
$timeout(function () { | |
ptrElement.style.margin = ''; | |
ptrElement.style.webkitTransitionDuration = '300ms'; | |
$scope.status = 'pull'; | |
}, elapsed < config.debounce ? config.debounce - elapsed : 0); | |
}); | |
}; | |
window.touchEnd = function() { | |
shouldReload = true; | |
onTouchEnd(); | |
}; | |
$element.bind('touchmove', onTouchMove); | |
$element.bind('touchend', onTouchEnd); | |
$scope.$on('$destroy', function () { | |
$element.unbind('touchmove', onTouchMove); | |
$element.unbind('touchend', onTouchEnd); | |
}); | |
} | |
}; | |
}); | |
}(window, document)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment