-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize.js
89 lines (74 loc) · 1.67 KB
/
resize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(function() {
'use strict';
angular
.module('resize', []);
angular
.module('resize')
.factory('resize', resize);
/**
* Usage:
*
* angular.module('myApp', ['resize']);
*
* var rs = resize.init({
* scope: $scope,
* resizedFn: function() { //window was resized! },
* debounce: 200
* });
*/
resize.$inject = ['$window', '$timeout'];
/**
* Resize factory
*
* @param $window
* @param $timeout
* @returns {object}
*/
function resize($window, $timeout) {
/**
* Resize constructor object
* @type {{buildInstance: buildInstance}}
*/
var Resize = {
buildInstance: buildInstance
};
function buildInstance(options) {
var _$scope = options['scope'];
var _oDebounce = options['debounce'];
var _$win = angular.element($window);
var _debounceSpeed = !!_oDebounce || _oDebounce === 0 ? _oDebounce : 100;
var _resizedFn = typeof options.resizedFn === 'function' ? options.resizedFn : null;
var _debounceResize;
/**
* Bind resize event to window
*/
function _resizeHandler() {
$timeout.cancel(_debounceResize);
_debounceResize = $timeout(_resizedFn, _debounceSpeed);
}
// run initial resize
_resizeHandler();
// bind height calculation to window resize
_$win.bind('resize', _resizeHandler);
if (_$scope) {
_$scope.$on('$destroy', function() {
_$win.unbind('resize', _resizeHandler);
});
}
}
/**
* Return constructor to assign and use
*
* @param options {object}
* @returns {object} Resize
*/
function init(options) {
var RS = Object.create(Resize);
RS.buildInstance(options);
return RS;
}
return {
init: init
}
}
})();