comparison js/foundation/foundation.equalizer.js @ 0:7abe02bf29ec

initial commit
author Alex Krolick <whokilledtheelectricmonk@gmail.com>
date Sat, 07 Nov 2015 18:04:42 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7abe02bf29ec
1 ;(function ($, window, document, undefined) {
2 'use strict';
3
4 Foundation.libs.equalizer = {
5 name : 'equalizer',
6
7 version : '5.5.3',
8
9 settings : {
10 use_tallest : true,
11 before_height_change : $.noop,
12 after_height_change : $.noop,
13 equalize_on_stack : false,
14 act_on_hidden_el: false
15 },
16
17 init : function (scope, method, options) {
18 Foundation.inherit(this, 'image_loaded');
19 this.bindings(method, options);
20 this.reflow();
21 },
22
23 events : function () {
24 this.S(window).off('.equalizer').on('resize.fndtn.equalizer', function (e) {
25 this.reflow();
26 }.bind(this));
27 },
28
29 equalize : function (equalizer) {
30 var isStacked = false,
31 group = equalizer.data('equalizer'),
32 settings = equalizer.data(this.attr_name(true)+'-init') || this.settings,
33 vals,
34 firstTopOffset;
35
36 if (settings.act_on_hidden_el) {
37 vals = group ? equalizer.find('['+this.attr_name()+'-watch="'+group+'"]') : equalizer.find('['+this.attr_name()+'-watch]');
38 }
39 else {
40 vals = group ? equalizer.find('['+this.attr_name()+'-watch="'+group+'"]:visible') : equalizer.find('['+this.attr_name()+'-watch]:visible');
41 }
42
43 if (vals.length === 0) {
44 return;
45 }
46
47 settings.before_height_change();
48 equalizer.trigger('before-height-change.fndth.equalizer');
49 vals.height('inherit');
50
51 if (settings.equalize_on_stack === false) {
52 firstTopOffset = vals.first().offset().top;
53 vals.each(function () {
54 if ($(this).offset().top !== firstTopOffset) {
55 isStacked = true;
56 return false;
57 }
58 });
59 if (isStacked) {
60 return;
61 }
62 }
63
64 var heights = vals.map(function () { return $(this).outerHeight(false) }).get();
65
66 if (settings.use_tallest) {
67 var max = Math.max.apply(null, heights);
68 vals.css('height', max);
69 } else {
70 var min = Math.min.apply(null, heights);
71 vals.css('height', min);
72 }
73
74 settings.after_height_change();
75 equalizer.trigger('after-height-change.fndtn.equalizer');
76 },
77
78 reflow : function () {
79 var self = this;
80
81 this.S('[' + this.attr_name() + ']', this.scope).each(function () {
82 var $eq_target = $(this),
83 media_query = $eq_target.data('equalizer-mq'),
84 ignore_media_query = true;
85
86 if (media_query) {
87 media_query = 'is_' + media_query.replace(/-/g, '_');
88 if (Foundation.utils.hasOwnProperty(media_query)) {
89 ignore_media_query = false;
90 }
91 }
92
93 self.image_loaded(self.S('img', this), function () {
94 if (ignore_media_query || Foundation.utils[media_query]()) {
95 self.equalize($eq_target)
96 } else {
97 var vals = $eq_target.find('[' + self.attr_name() + '-watch]:visible');
98 vals.css('height', 'auto');
99 }
100 });
101 });
102 }
103 };
104 })(jQuery, window, window.document);