Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Fix selector context #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function removeHeight (element) {
*
* @param selectors
*/
function removeHeights (selectors) {
function removeHeights (el, selectors) {
[].forEach.call(selectors, (selector) => {
const elements = document.querySelectorAll(selector);
const elements = el.querySelectorAll(selector);
[].forEach.call(elements, (element) => {
removeHeight(element);
});
Expand Down Expand Up @@ -45,8 +45,8 @@ function getMinHeight (elements) {
*
* @param selector
*/
function setHeight (selector) {
const elements = document.querySelectorAll(selector);
function setHeight (el, selector) {
const elements = el.querySelectorAll(selector);
const height = getMinHeight(elements);

[].forEach.call(elements, (element) => {
Expand Down Expand Up @@ -85,23 +85,23 @@ function shouldRun (rules) {
* @param selectors
* @param disabled
*/
function matchHeights (selectors = [], disabled = []) {
function matchHeights (el, selectors = [], disabled = []) {
// Size each selector in turn
removeHeights(selectors);
removeHeights(el, selectors);

// Check if the plugin should run
if (!shouldRun(disabled)) return false;

// Size each provided selector
selectors.forEach(setHeight);
selectors.forEach((selector) => setHeight(el, selector));
}


function plugin (Vue, options = {}) {
Vue.directive('match-heights', {
bind (el, binding) {
function matchHeightsFunc () {
matchHeights(binding.value.el, binding.value.disabled || options.disabled || []);
matchHeights(el, binding.value.el, binding.value.disabled || options.disabled || []);
}
matchHeightsFunc();
window.addEventListener('resize', matchHeightsFunc);
Expand All @@ -110,11 +110,11 @@ function plugin (Vue, options = {}) {

inserted (el, binding) {
function matchHeightsFunc () {
matchHeights(binding.value.el, binding.value.disabled || options.disabled || []);
matchHeights(el, binding.value.el, binding.value.disabled || options.disabled || []);
}

// Handle images rendering
[].forEach.call(document.querySelectorAll(binding.value.el), (el) => {
[].forEach.call(el.querySelectorAll(binding.value.el), (el) => {
[].forEach.call(el.querySelectorAll('img'), (img) => {
img.addEventListener('load', matchHeightsFunc);
});
Expand Down