Skip to content

Commit cd69d9e

Browse files
author
Olivier FAURE
committed
💎 Rework package around Vue.use()
1 parent 5574cfd commit cd69d9e

File tree

4 files changed

+165
-134
lines changed

4 files changed

+165
-134
lines changed

Idle.vue

Lines changed: 0 additions & 131 deletions
This file was deleted.

index.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict'
2+
3+
import IdleJs from 'idle-js'
4+
import IdleView from "./src/Idle"
5+
6+
export default {
7+
IdleView,
8+
9+
install (Vue, options) {
10+
const {
11+
eventEmitter,
12+
store = Vue.$store,
13+
14+
idleTime = 60 * 1000,
15+
events = ['mousemove', 'keydown', 'mousedown', 'touchstart'],
16+
keepTracking = true,
17+
startAtIdle = true,
18+
moduleName = 'idle'
19+
} = options || {}
20+
21+
if (!options) {
22+
throw Error("`options` must be a valid JS object")
23+
}
24+
25+
if (!eventEmitter) {
26+
throw Error("`options.eventEmitter` must be a valid Vue instance")
27+
}
28+
if (store === undefined) {
29+
throw Error("`options.store` is undefined\n"
30+
+ "`options.store` must be null or a valid Vuex store instance")
31+
}
32+
33+
store && store.registerModule(moduleName, {
34+
state: { isIdle: false },
35+
36+
mutations: {
37+
[`${moduleName}/IDLE_CHANGED`]: function (state, isIdle) {
38+
state.isIdle = isIdle
39+
}
40+
}
41+
})
42+
43+
const idle = new IdleJs({
44+
idle: idleTime,
45+
events,
46+
keepTracking,
47+
startAtIdle,
48+
49+
onIdle: () => {
50+
eventEmitter.$emit(`${moduleName}_onIdle`)
51+
store && store.commit(`${moduleName}/IDLE_CHANGED`, true)
52+
},
53+
onActive: () => {
54+
eventEmitter && eventEmitter.$emit(`${moduleName}_onActive`)
55+
store && store.commit(`${moduleName}/IDLE_CHANGED`, false)
56+
}
57+
})
58+
idle.start()
59+
60+
Vue.component("IdleView", IdleView)
61+
62+
Vue.mixin({
63+
data () { return {
64+
idleVue_onIdle: null,
65+
idleVue_onActive: null
66+
}},
67+
created () {
68+
const options = this.$options
69+
70+
this.idleVue_onIdle = () => options.onIdle && options.onIdle()
71+
this.idleVue_onActive = () => options.onActive && options.onActive()
72+
eventEmitter.$on(`${moduleName}_onIdle`, this.idleVue_onIdle)
73+
eventEmitter.$on(`${moduleName}_onActive`, this.idleVue_onActive)
74+
},
75+
destroyed () {
76+
eventEmitter.$off(`${moduleName}_onIdle`, this.idleVue_onIdle)
77+
eventEmitter.$off(`${moduleName}_onActive`, this.idleVue_onActive)
78+
},
79+
computed: {
80+
isAppIdle () {
81+
return store.state.idle
82+
}
83+
}
84+
})
85+
}
86+
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
22
"name": "idle-vue",
3-
"version": "1.0.1",
3+
"version": "2.0.0",
44
"description": "Vue component wrapper for idle-js",
55
"repository": "https://github.com/soixantecircuits/idle-vue.git",
66
"author": "Olivier FAURE <olivier.faure@epitech.eu>",
77
"license": "MIT",
8-
"main": "Idle.vue",
8+
"main": "index.js",
99
"dependencies": {
10-
"idle-js": "^0.1.0",
10+
"idle-js": "^0.1.0"
11+
},
12+
"devDependencies": {
1113
"vue": "^2.3.3",
1214
"vuex": "^2.3.1"
1315
}

src/Idle.vue

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<template>
2+
<div class="idle-view" :class="{active: isAppIdle}">
3+
<div class="overlay" :class="{active: isAppIdle}"></div>
4+
<sprite spriteId="touch"
5+
:spriteSrc="require('./assets/touch.png')"
6+
:spriteW='180'
7+
:spriteH='215'
8+
ref='sprite'
9+
></sprite>
10+
</div>
11+
</template>
12+
13+
<script>
14+
'use strict'
15+
16+
import Sprite from './Sprite'
17+
18+
export default {
19+
components: {
20+
Sprite
21+
},
22+
onIdle () {
23+
this.$refs.sprite.play()
24+
},
25+
onActive () {
26+
this.$refs.sprite.stop()
27+
}
28+
}
29+
30+
</script>
31+
32+
<style>
33+
.idle-vue {
34+
display: none;
35+
}
36+
.idle-vue.active {
37+
pointer-events: all;
38+
display: block;
39+
}
40+
.idle-vue .sprite {
41+
top: 700px;
42+
bottom: 0px;
43+
margin: auto;
44+
position: absolute;
45+
height: 10px;
46+
width: 180px;
47+
left: 0px;
48+
right: 0px;
49+
z-index: 9999;
50+
-webkit-transform: scale(0.7);
51+
}
52+
.idle-vue .overlay {
53+
width: 100vw;
54+
height: 100vh;
55+
position: absolute;
56+
top: 0px;
57+
left: 0px;
58+
background: #000;
59+
pointer-events:none;
60+
z-index: 8888;
61+
opacity: 0;
62+
/*-webkit-animation: SlowMo 5s cubic-bezier(0.77, 0, 0.175, 1) infinite;*/
63+
-webkit-transition: opacity 800ms cubic-bezier(0.77, 0, 0.175, 1);
64+
}
65+
.idle-vue .overlay.active {
66+
pointer-events: all;
67+
opacity: 0.6;
68+
}
69+
@-webkit-keyframes SlowMo {
70+
0%{background-position:0% 50%}
71+
50%{background-position:100% 50%}
72+
100%{background-position:0% 50%}
73+
}
74+
</style>

0 commit comments

Comments
 (0)