-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-shave.js
84 lines (65 loc) · 2.04 KB
/
vue-shave.js
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
78
79
80
81
82
83
84
import shave from 'shave/dist/shave';
import throttle from 'just-throttle';
// Where we keep our shaver functions
let shavers = [];
// Global defaults
const defaults = {
height: 100,
throttle: 300,
spaces: false,
character: '...',
};
// Vue plugin install function
function install( Vue, options ) {
// Merge settings with defaults
const globalSettings = { ...defaults, ...options };
// Our throttled run function
const runShaversThrottled = throttle( runShavers, globalSettings.throttle );
if ( window ) {
window.addEventListener( 'load', () => runShavers() );
}
// Add the shave directive
Vue.directive( 'shave', {
bind( el, binding ) {
// Setup settings
const directiveSettings = binding.value || {};
const settings = { ...globalSettings, ...directiveSettings };
// Create the function to run on window resize
// Bound to the given shaver settings
const shaveFn = (( height, character, spaces ) => {
shave( el, height, { character, spaces });
}).bind( null, settings.height, settings.character, settings.spaces );
// Add the shaver to the list
shavers.push({ el, shaveFn });
// If this is the first shaver, add the resize event listener
if ( shavers.length === 1 ) {
window.addEventListener( 'resize', runShaversThrottled );
}
},
unbind( el ) {
// Remove the shaver from the list
shavers = shavers.filter( shaver => shaver.el !== el );
// If there are no shavers, remove the resize listener
if ( shavers.length === 0 ) {
window.removeEventListener( 'resize', runShaversThrottled );
}
},
// Run shaver on inserted
inserted: runShaver,
// Run shaver on updated
componentUpdated: runShaver,
});
};
function runShavers() {
shavers.forEach( shaver => shaver.shaveFn() );
};
function runShaver( el ) {
// Get the shaver for the current element
const found = shavers.filter( shaver => shaver.el === el );
const shaver = found.length ? found[ 0 ] : null;
// Run the shaver function
if ( shaver && shaver.shaveFn ) {
shaver.shaveFn();
}
};
export default { install };