Skip to content

Commit

Permalink
Merge pull request #7 from rndmerle/resize-listener
Browse files Browse the repository at this point in the history
fix: store state (and event-listener) on element
  • Loading branch information
alexandremoore authored Oct 9, 2018
2 parents 5be2c82 + 0f9b6e5 commit c35c56d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 38 deletions.
53 changes: 34 additions & 19 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,42 @@ export function setDefaults(options) {
defaults = _extends({}, defaults, options);
}

function setup(el, clampValue) {
tearDown(el);

const resizeListener = () => {
clampElement(el, clampValue);
};

el.__VueClampy = {
clampValue,
resizeListener
};

// Re-clamp on element resize
// resizeDetector.listenTo(el, () => {
// clampElement(el, clampValue);
// });

// Also re-clamp on window resize
window.addEventListener('resize', resizeListener);

clampElement(el, clampValue);
}

function tearDown(el) {
if (!el || !el.__VueClampy) return;
// Remove all listeners
// resizeDetector.removeAllListeners(el);
window.removeEventListener('resize', el.__VueClampy.resizeListener);
}

function setInitialContent(el) {
if (el.clampInitialContent === undefined) {
el.clampInitialContent = el.innerHTML.trim();
}
}

function clampOnResize(el, clampValue) {
clampElement(el, clampValue);
}

function clampElement(el, clamp) {
// We use element-resize-detector to trigger the ellipsis.
// Element-resize-detector adds an inner div to monitor
Expand Down Expand Up @@ -77,27 +103,16 @@ function clampElement(el, clamp) {
export default {
inserted(el, binding, vnode) {
clampValue = binding.value;
// Re-clamp on element resize
// resizeDetector.listenTo(el, () => {
// clampElement(el, clampValue);
// });

// Also re-clamp on window resize
window.addEventListener('resize', clampOnResize);

clampElement(el, clampValue);
setup(el, clampValue);
},

update(el, binding, vnode) {
clampValue = binding.value;
clampElement(el, clampValue);
setup(el, clampValue);
},

unbind(el, binding, vnode) {
clampValue = binding.value;

// Remove all listeners
// resizeDetector.removeAllListeners(el);
window.removeEventListener('resize', clampOnResize);
tearDown(el);
delete el.__VueClampy;
}
};
53 changes: 34 additions & 19 deletions vue-clampy.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,42 @@ function setDefaults(options) {
defaults = _extends({}, defaults, options);
}

function setup(el, clampValue) {
tearDown(el);

var resizeListener = function resizeListener() {
clampElement(el, clampValue);
};

el.__VueClampy = {
clampValue: clampValue,
resizeListener: resizeListener
};

// Re-clamp on element resize
// resizeDetector.listenTo(el, () => {
// clampElement(el, clampValue);
// });

// Also re-clamp on window resize
window.addEventListener('resize', resizeListener);

clampElement(el, clampValue);
}

function tearDown(el) {
if (!el || !el.__VueClampy) return;
// Remove all listeners
// resizeDetector.removeAllListeners(el);
window.removeEventListener('resize', el.__VueClampy.resizeListener);
}

function setInitialContent(el) {
if (el.clampInitialContent === undefined) {
el.clampInitialContent = el.innerHTML.trim();
}
}

function clampOnResize(el, clampValue) {
clampElement(el, clampValue);
}

function clampElement(el, clamp) {
// We use element-resize-detector to trigger the ellipsis.
// Element-resize-detector adds an inner div to monitor
Expand Down Expand Up @@ -364,26 +390,15 @@ function clampElement(el, clamp) {
var VueClampy$1 = {
inserted: function inserted(el, binding, vnode) {
clampValue = binding.value;
// Re-clamp on element resize
// resizeDetector.listenTo(el, () => {
// clampElement(el, clampValue);
// });

// Also re-clamp on window resize
window.addEventListener('resize', clampOnResize);

clampElement(el, clampValue);
setup(el, clampValue);
},
update: function update(el, binding, vnode) {
clampValue = binding.value;
clampElement(el, clampValue);
setup(el, clampValue);
},
unbind: function unbind(el, binding, vnode) {
clampValue = binding.value;

// Remove all listeners
// resizeDetector.removeAllListeners(el);
window.removeEventListener('resize', clampOnResize);
tearDown(el);
delete el.__VueClampy;
}
};

Expand Down

0 comments on commit c35c56d

Please sign in to comment.