-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.matchWindow.js
More file actions
93 lines (81 loc) · 2.9 KB
/
jquery.matchWindow.js
File metadata and controls
93 lines (81 loc) · 2.9 KB
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
//it's safe to use window & document throughout your code, but scoping the variable lets you take advantage of some really useful features of minifiers/uglifiers.
;
(function ($, window, document, undefined) {
var pluginName = "matchWindow",
/**
* This MatchWindowOptions object can be overridden during initialization
* @type {{live: boolean, minHeight: number, offset: number}}
*/
defaults = {
live: false,
minHeight: 0,
offset: 0
},
calculated = {
height: null//this allows us to store the height so we're not constantly querying for computed styles
};
/**
* This plugin sets the height of the selected element to the height of the window.
* By default this plugin only sets height once, but you can enable an option to set the height on resize, too.
* Version 1.3.0 added support for willSetHeight event to support canceling the set height
*
* @author Abishai Gray <agray@mindgruve.com>
* @version 1.3.0
*
* @param element
* @param options
* @constructor
*/
function MatchWindow(element, options) {
if (element) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
}
//Static Method
MatchWindow.clearCachedHeight = function () {
calculated.height = null;
};
MatchWindow.prototype = {
init: function () {
var _this = this;
this.setHeight();
if (this.options.live) {
$(window).on('resize.match-window', function () {
calculated.height = null;//this forces the height to be re-calculated
_this.setHeight();
});
}
},
setHeight: function () {
var setHeightEvent = $.Event('willSetHeight');
$(this.element).trigger(setHeightEvent);
if (setHeightEvent.isDefaultPrevented()) {
return;
}
if (!calculated.height) {
calculated.height = $(window).height() - (this.options.offset || 0);
}
$(this.element).height(Math.max(calculated.height, this.options.minHeight));
$(this.element).trigger('didSetHeight');
}
};
//setup $().pluginName
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName,
new MatchWindow(this, options));
}
});
};
//add support for requirejs
if (typeof define === "function" && define.amd) {
define(function () {
return MatchWindow;
});
}
})(jQuery, window, document);