summaryrefslogtreecommitdiffstats
path: root/js/foundation/foundation.equalizer.js
blob: 687e84627e5fd5df6b293d45da5a6d6bcae7214d (plain)
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
;
(function ($, window, document, undefined) {
    'use strict';

    Foundation.libs.equalizer = {
        name: 'equalizer',

        version: '5.5.0',

        settings: {
            use_tallest: true,
            before_height_change: $.noop,
            after_height_change: $.noop,
            equalize_on_stack: false
        },

        init: function (scope, method, options) {
            Foundation.inherit(this, 'image_loaded');
            this.bindings(method, options);
            this.reflow();
        },

        events: function () {
            this.S(window).off('.equalizer').on('resize.fndtn.equalizer', function (e) {
                this.reflow();
            }.bind(this));
        },

        equalize: function (equalizer) {
            var isStacked = false,
                vals = equalizer.find('[' + this.attr_name() + '-watch]:visible'),
                settings = equalizer.data(this.attr_name(true) + '-init');

            if (vals.length === 0) return;
            var firstTopOffset = vals.first().offset().top;
            settings.before_height_change();
            equalizer.trigger('before-height-change').trigger('before-height-change.fndth.equalizer');
            vals.height('inherit');
            vals.each(function () {
                var el = $(this);
                if (el.offset().top !== firstTopOffset) {
                    isStacked = true;
                }
            });

            if (settings.equalize_on_stack === false) {
                if (isStacked) return;
            }
            ;

            var heights = vals.map(function () {
                return $(this).outerHeight(false)
            }).get();

            if (settings.use_tallest) {
                var max = Math.max.apply(null, heights);
                vals.css('height', max);
            } else {
                var min = Math.min.apply(null, heights);
                vals.css('height', min);
            }
            settings.after_height_change();
            equalizer.trigger('after-height-change').trigger('after-height-change.fndtn.equalizer');
        },

        reflow: function () {
            var self = this;

            this.S('[' + this.attr_name() + ']', this.scope).each(function () {
                var $eq_target = $(this);
                self.image_loaded(self.S('img', this), function () {
                    self.equalize($eq_target)
                });
            });
        }
    };
})(jQuery, window, window.document);