PageSourceSearch

https://izawa-law.com/wp-content/themes/genova_tpl/js/matchHeight.js

js izawa-law.com collected 2026-10-02 22:29:10 UTC 11,869 bytes, 391 lines download raw bytes

vendor: 11,869 bytes, lines 1-391
1(function($) {
2/****************************************************
3                   matchHeight
4****************************************************/
5
6/**
7* jquery-match-height master by @liabru
8* http://brm.io/jquery-match-height/
9* License: MIT
10*/
11
12;(function(factory) { // eslint-disable-line no-extra-semi
13    'use strict';
14    if (typeof define === 'function' && define.amd) {
15        // AMD
16        define(['jquery'], factory);
17    } else if (typeof module !== 'undefined' && module.exports) {
18        // CommonJS
19        module.exports = factory(require('jquery'));
20    } else {
21        // Global
22        factory(jQuery);
23    }
24})(function($) {
25    /*
26    *  internal
27    */
28
29    var _previousResizeWidth = -1,
30        _updateTimeout = -1;
31
32    /*
33    *  _parse
34    *  value parse utility function
35    */
36
37    var _parse = function(value) {
38        // parse value and convert NaN to 0
39        return parseFloat(value) || 0;
40    };
41
42    /*
43    *  _rows
44    *  utility function returns array of jQuery selections representing each row
45    *  (as displayed after float wrapping applied by browser)
46    */
47
48    var _rows = function(elements) {
49        var tolerance = 1,
50            $elements = $(elements),
51            lastTop = null,
52            rows = [];
53
54        // group elements by their top position
55        $elements.each(function(){
56            var $that = $(this),
57                top = $that.offset().top - _parse($that.css('margin-top')),
58                lastRow = rows.length > 0 ? rows[rows.length - 1] : null;
59
60            if (lastRow === null) {
61                // first item on the row, so just push it
62                rows.push($that);
63            } else {
64                // if the row top is the same, add to the row group
65                if (Math.floor(Math.abs(lastTop - top)) <= tolerance) {
66                    rows[rows.length - 1] = lastRow.add($that);
67                } else {
68                    // otherwise start a new row group
69                    rows.push($that);
70                }
71            }
72
73            // keep track of the last row top
74            lastTop = top;
75        });
76
77        return rows;
78    };
79
80    /*
81    *  _parseOptions
82    *  handle plugin options
83    */
84
85    var _parseOptions = function(options) {
86        var opts = {
87            byRow: true,
88            property: 'height',
89            target: null,
90            remove: false
91        };
92
93        if (typeof options === 'object') {
94            return $.extend(opts, options);
95        }
96
97        if (typeof options === 'boolean') {
98            opts.byRow = options;
99        } else if (options === 'remove') {
100            opts.remove = true;
101        }
102
103        return opts;
104    };
105
106    /*
107    *  matchHeight
108    *  plugin definition
109    */
110
111    var matchHeight = $.fn.matchHeight = function(options) {
112        var opts = _parseOptions(options);
113
114        // handle remove
115        if (opts.remove) {
116            var that = this;
117
118            // remove fixed height from all selected elements
119            this.css(opts.property, '');
120
121            // remove selected elements from all groups
122            $.each(matchHeight._groups, function(key, group) {
123                group.elements = group.elements.not(that);
124            });
125
126            // TODO: cleanup empty groups
127
128            return this;
129        }
130
131        if (this.length <= 1 && !opts.target) {
132            return this;
133        }
134
135        // keep track of this group so we can re-apply later on load and resize events
136        matchHeight._groups.push({
137            elements: this,
138            options: opts
139        });
140
141        // match each element's height to the tallest element in the selection
142        matchHeight._apply(this, opts);
143
144        return this;
145    };
146
147    /*
148    *  plugin global options
149    */
150
151    matchHeight.version = 'master';
152    matchHeight._groups = [];
153    matchHeight._throttle = 80;
154    matchHeight._maintainScroll = false;
155    matchHeight._beforeUpdate = null;
156    matchHeight._afterUpdate = null;
157    matchHeight._rows = _rows;
158    matchHeight._parse = _parse;
159    matchHeight._parseOptions = _parseOptions;
160
161    /*
162    *  matchHeight._apply
163    *  apply matchHeight to given elements
164    */
165
166    matchHeight._apply = function(elements, options) {
167        var opts = _parseOptions(options),
168            $elements = $(elements),
169            rows = [$elements];
170
171        // take note of scroll position
172        var scrollTop = $(window).scrollTop(),
173            htmlHeight = $('html').outerHeight(true);
174
175        // get hidden parents
176        var $hiddenParents = $elements.parents().filter(':hidden');
177
178        // cache the original inline style
179        $hiddenParents.each(function() {
180            var $that = $(this);
181            $that.data('style-cache', $that.attr('style'));
182        });
183
184        // temporarily must force hidden parents visible
185        $hiddenParents.css('display', 'block');
186
187        // get rows if using byRow, otherwise assume one row
188        if (opts.byRow && !opts.target) {
189
190            // must first force an arbitrary equal height so floating elements break evenly
191            $elements.each(function() {
192                var $that = $(this),
193                    display = $that.css('display');
194
195                // temporarily force a usable display value
196                if (display !== 'inline-block' && display !== 'flex' && display !== 'inline-flex') {
197                    display = 'block';
198                }
199
200                // cache the original inline style
201                $that.data('style-cache', $that.attr('style'));
202
203                $that.css({
204                    'display': display,
205                    'padding-top': '0',
206                    'padding-bottom': '0',
207                    'margin-top': '0',
208                    'margin-bottom': '0',
209                    'border-top-width': '0',
210                    'border-bottom-width': '0',
211                    'height': '100px',
212                    'overflow': 'hidden'
213                });
214            });
215
216            // get the array of rows (based on element top position)
217            rows = _rows($elements);
218
219            // revert original inline styles
220            $elements.each(function() {
221                var $that = $(this);
222                $that.attr('style', $that.data('style-cache') || '');
223            });
224        }
225
226        $.each(rows, function(key, row) {
227            var $row = $(row),
228                targetHeight = 0;
229
230            if (!opts.target) {
231                // skip apply to rows with only one item
232                if (opts.byRow && $row.length <= 1) {
233                    $row.css(opts.property, '');
234                    return;
235                }
236
237                // iterate the row and find the max height
238                $row.each(function(){
239                    var $that = $(this),
240                        style = $that.attr('style'),
241                        display = $that.css('display');
242
243                    // temporarily force a usable display value
244                    if (display !== 'inline-block' && display !== 'flex' && display !== 'inline-flex') {
245                        display = 'block';
246                    }
247
248                    // ensure we get the correct actual height (and not a previously set height value)
249                    var css = { 'display': display };
250                    css[opts.property] = '';
251                    $that.css(css);
252
253                    // find the max height (including padding, but not margin)
254                    if ($that.outerHeight(false) > targetHeight) {
255                        targetHeight = $that.outerHeight(false);
256                    }
257
258                    // revert styles
259                    if (style) {
260                        $that.attr('style', style);
261                    } else {
262                        $that.css('display', '');
263                    }
264                });
265            } else {
266                // if target set, use the height of the target element
267                targetHeight = opts.target.outerHeight(false);
268            }
269
270            // iterate the row and apply the height to all elements
271            $row.each(function(){
272                var $that = $(this),
273                    verticalPadding = 0;
274
275                // don't apply to a target
276                if (opts.target && $that.is(opts.target)) {
277                    return;
278                }
279
280                // handle padding and border correctly (required when not using border-box)
281                if ($that.css('box-sizing') !== 'border-box') {
282                    verticalPadding += _parse($that.css('border-top-width')) + _parse($that.css('border-bottom-width'));
283                    verticalPadding += _parse($that.css('padding-top')) + _parse($that.css('padding-bottom'));
284                }
285
286                // set the height (accounting for padding and border)
287                $that.css(opts.property, (targetHeight - verticalPadding) + 'px');
288            });
289        });
290
291        // revert hidden parents
292        $hiddenParents.each(function() {
293            var $that = $(this);
294            $that.attr('style', $that.data('style-cache') || null);
295        });
296
297        // restore scroll position if enabled
298        if (matchHeight._maintainScroll) {
299            $(window).scrollTop((scrollTop / htmlHeight) * $('html').outerHeight(true));
300        }
301
302        return this;
303    };
304
305    /*
306    *  matchHeight._applyDataApi
307    *  applies matchHeight to all elements with a data-match-height attribute
308    */
309
310    matchHeight._applyDataApi = function() {
311        var groups = {};
312
313        // generate groups by their groupId set by elements using data-match-height
314        $('[data-match-height], [data-mh]').each(function() {
315            var $this = $(this),
316                groupId = $this.attr('data-mh') || $this.attr('data-match-height');
317
318            if (groupId in groups) {
319                groups[groupId] = groups[groupId].add($this);
320            } else {
321                groups[groupId] = $this;
322            }
323        });
324
325        // apply matchHeight to each group
326        $.each(groups, function() {
327            this.matchHeight(true);
328        });
329    };
330
331    /*
332    *  matchHeight._update
333    *  updates matchHeight on all current groups with their correct options
334    */
335
336    var _update = function(event) {
337        if (matchHeight._beforeUpdate) {
338            matchHeight._beforeUpdate(event, matchHeight._groups);
339        }
340
341        $.each(matchHeight._groups, function() {
342            matchHeight._apply(this.elements, this.options);
343        });
344
345        if (matchHeight._afterUpdate) {
346            matchHeight._afterUpdate(event, matchHeight._groups);
347        }
348    };
349
350    matchHeight._update = function(throttle, event) {
351        // prevent update if fired from a resize event
352        // where the viewport width hasn't actually changed
353        // fixes an event looping bug in IE8
354        if (event && event.type === 'resize') {
355            var windowWidth = $(window).width();
356            if (windowWidth === _previousResizeWidth) {
357                return;
358            }
359            _previousResizeWidth = windowWidth;
360        }
361
362        // throttle updates
363        if (!throttle) {
364            _update(event);
365        } else if (_updateTimeout === -1) {
366            _updateTimeout = setTimeout(function() {
367                _update(event);
368                _updateTimeout = -1;
369            }, matchHeight._throttle);
370        }
371    };
372
373    /*
374    *  bind events
375    */
376
377    // apply on DOM ready event
378    $(matchHeight._applyDataApi);
379
380    // update heights on load and resize events
381    $(window).bind('load', function(event) {
382        matchHeight._update(false, event);
383    });
384
385    // throttled update heights on resize events
386    $(window).bind('resize orientationchange', function(event) {
387        matchHeight._update(true, event);
388    });
389
390});
391})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.