-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize-dimension.js
106 lines (83 loc) · 2.64 KB
/
resize-dimension.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(function (root, factory) {
var moduleName = 'ResizeDimension';
if (typeof define === 'function' && define.amd) {
define(['jquery'], function ($) {
return (root[moduleName] = factory($));
});
} else {
root[moduleName] = factory(root.$);
}
}(this, function ($) {
var $window = $(window);
var ResizeDimension = function ($el, dimension, handler, options) {
if (! (this instanceof ResizeDimension)) {
return new ResizeDimension($el, dimension, handler, options);
}
this.$el = $el;
this.init(dimension, handler, options);
return this;
};
/**
* Stub - overridden on #init()
*/
ResizeDimension.prototype.onResize = function () {};
ResizeDimension.bound = {};
ResizeDimension.bind = function (dimension, options) {
if (ResizeDimension.bound[dimension]) return;
ResizeDimension.bound[dimension] = true;
$window.resizeDimension(dimension, function () {
$window.trigger('resize-' + dimension);
}, options);
};
ResizeDimension.prototype.init = function (dimension, handler, options) {
if (typeof dimension === 'object') {
options = dimension;
dimension = options.dimension;
handler = options.handler;
}
options = $.extend({}, options);
options.dimension = dimension;
options.handler = handler;
this.options = options;
if ($.isFunction(options.changed)) {
this.changed = options.changed;
}
this.dimension = this.normalize(options.dimension);
this.handler = options.handler;
this.previousValue = this.value();
var proxied = $.proxy(this.handle, this);
if (options.throttler) {
this.onResize = options.throttler(proxied);
}
else {
this.onResize = proxied;
}
};
ResizeDimension.prototype.normalize = function (dimension) {
return dimension;
};
ResizeDimension.prototype.changed = function (previous, current) {
return previous !== current;
};
ResizeDimension.prototype.value = function (e) {
return this.$el[this.dimension]();
};
ResizeDimension.prototype.handle = function (e) {
var currentValue = this.value();
if (this.changed(this.previousValue, currentValue)) {
this.previousValue = currentValue;
return this.handler.call(this.$el, e);
}
};
var $resizeDimension = function () {
var args = Array.prototype.slice.call(arguments);
return this.each( function() {
var $el = $(this);
args = [$el].concat(args);
var instance = ResizeDimension.apply(null, args);
$el.on('resize', $.proxy(instance.onResize, instance));
});
};
$.fn.resizeDimension = $resizeDimension;
return ResizeDimension;
}));