GithubHelp home page GithubHelp logo

idle-vue's Introduction

idle-vue Build Status

idle-vue is a Vue.js plug-in, that detects when the user hasn't interacted with your app for a while. idle-vue is meant to be used with Vue, Vuex and either Webpack or Browserify.

idle-vue is based on idle-js.

๐ŸŒ Installation

npm install --save idle-vue

๐Ÿ‘‹ Usage

At the root of your project, just before creating your Vue application, import the idle-vue plug-in, and add it to the Vue global with the following code:

import Vue from 'vue'
import IdleVue from 'idle-vue'

const options = { ... }

Vue.use(IdleVue, options)

Vue.use is a Vue method that installs the given plugin (here, IdleVue), and passes it the given options.

The above code does two things:

  • Add two hooks onIdle and onActive to all Vue objects

  • Add a computed value isAppIdle to all Vue objects

Hooks

The plug-in adds two hooks to Vue: onIdle and onActive; those functions may be defined in any Vue object (components included), and will be called by the plug-in when the window respectively starts and stops idling.

These hooks are not methods; they should be added directly at the Root of your component. These hooks will not be called if the options object has no eventEmitter field.

Example - main.js

import Vue from 'vue'
import IdleVue from 'idle-vue'

const eventsHub = new Vue()

Vue.use(IdleVue, {
  eventEmitter: eventsHub,
  idleTime: 10000
})

const vm = new Vue({
  el: '#app',
  data () {
    return {
      messageStr: 'Hello'
    }
  },
  onIdle() {
    this.messageStr = 'ZZZ'
  },
  onActive() {
    this.messageStr = 'Hello'
  }
})

Example - index.html

<div id=app>
  <p>
    {{ messageStr }}
  </p>
</div>

isAppIdle

The plug-in adds a computed value isAppIdle to every Vue object.

It's a shorthand for the current value of store.state.idleVue.isIdle; this value will always be undefined if the options object has no store field.

Note that using isAppIdle or using the hooks onIdle and onActive are both different, valid ways of doing the same thing: detecting when your app is idle. You can use either or both of them depending on your needs.

Example - main.js

import Vue from 'vue'
import IdleVue from 'idle-vue'
import Vuex from 'vuex'

const store = new Vuex.Store({
  // ...
})

Vue.use(IdleVue, { store })

const vm = new Vue({
  el: '#app',
  store,
  computed: {
    messageStr() {
      return this.isAppIdle ? 'ZZZ' : 'Hello'
    }
  }
})

Example - index.html

<div id=app>
  <p>
    {{ messageStr }}
  </p>
</div>

IdleView

The package comes with an example component named IdleView (or idle-view).

idle-view is not automatically included with the plugin. It can be imported as a global component or a dependency within your own component, however it serves best as a working example from which to base your own implementation.

This component is a default idle overlay with a small "touch the screen" sprite; it has no props and no slots. You may create your own idle overlay by exploiting isAppIdle.

Example - main.js

import IdleVue from 'idle-vue'
import IdleVueComponent from 'idle-vue/src/components/Idle.vue'
import Vuex from 'vuex'

const eventsHub = new Vue()
const store = new Vuex.Store({
  // ...
})

Vue.use(IdleVue, { eventEmitter: eventsHub, store })
Vue.component('idle-view', IdleVueComponent) // Required only to use idle-view component

const vm = new Vue({
  el: '#app',
  store,
  // ...
})

Example - index.html

<div id=app>
  <p>
    Hello world!
    ...
  </p>
  <idle-view />
</div>

Options

idle-vue accepts the following options when loaded; all of them are facultative, except store or eventEmitter; they cannot be both omitted:

  • eventEmitter: The Vue instance through which the idle-vue plugin is to send events. The plugin will send idleVue_onIdle and idleVue_onActive events to this instance; all Vue objects created after the plugin is loaded will run their onIdle and onActive hooks when idleVue_onIdle and idleVue_onActive events are sent.

  • store: The Vuex instance which stores the state of the app (idle or active); this store has a state idleVue.isIdle and a mutation idleVue/IDLE_CHANGED(isIdle).

  • idleTime: The time (in ms) without input before the program is considered idle. For instance, with idleTime: 40000, the module will emit idle events after 40 seconds of inactivity. Default value: 60000 (one minute).

  • events: Events that "break" idleness. Default value: ['mousemove', 'keydown', 'mousedown', 'touchstart']

  • keepTracking: Whether you want to track more than once. Default value: true.

  • startAtIdle: Start in idle state. Default value: true.

  • moduleName: The name of the vuex module (if store is defined), and the prefix of the emitted events (if eventEmitter is defined). Default value: idleVue.

โค๏ธ Contribute

Thanks for helping us!

Please follow the following standards when submitting a pull request:

idle-vue's People

Contributors

afonso avatar agnivade avatar gabrielstuff avatar gregpeden avatar hugohil avatar poignardazur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

idle-vue's Issues

Error from uglifyJs

I added idle-vue with npm to my vue project and everything was working fine in run dev but when i tried to build my project I got the following error:

ERROR in static/js/vendor.87bf41a35bd9b3a1e693.js from UglifyJs Unexpected token: punc (() [./~/idle-vue/dist/build.js:678,0][static/js/vendor.87bf41a35bd9b3a1e693.js:12395,9]

Did anyone else experience this? I tried changing some settings in my .babelrc but I still get the error.

reference error: "window is not defined" when using in Nuxt.js

I'm trying to use this plugin in Nuxt.js.
So far I did (based on the example code):

  1. npm install --save idle-vue
  2. created /plugins/idle-vue.js with the following content:
import Vue from 'vue'
import IdleVue from 'idle-vue'

const eventsHub = new Vue()

Vue.use(IdleVue, {
  eventEmitter: eventsHub,
  idleTime: 10000
})
  1. imported the plugin in /nuxt.config.js:
plugins: [
    { src: '@/plugins/idle-vue.js'}
  ],
  1. added hooks to my Nuxt page Vue instance:
onIdle() {
  console.log('ZZZ');
},
onActive() {
  console.log('Hello');
},

This causes Nuxt to report reference error: "window is not defined"
Screenshot 2019-05-17 at 12 33 53

Multiple Idles

Is it possible to have multiple callbacks with different timings?

Vue 3 support

First of all.. Thank you for your nice work with handling of idle state.

We've encountered warnings using idle-vue with Vue 3 - regarding new naming conventions in Vue 3. Where the destroyed() functionality is now called unmounted.

Could this be changed in at new version bundle, to support Vue 3 ?

ERROR from UglifyJs Unexpected token: punc (()

Hello,

When building the project, Uglify throws this error.
ERROR in static/js/vendor.4955f0d5d2e9486bc600.js from UglifyJs Unexpected token: punc (()

As Evan explains in this discussion it's because idle-vue serves a non precompiled version of the file.

Could this be fixed? Thanks!

Typescript support

Hi Guys,

Do you have typescript support? When I try following the example it tells me that on onIdle() is not a function?

Update the value of the isIdle in vuex by calling a mutation.

I'm trying to change the boolean value of the isIdle, bypassing a boolean param to the mutation idleVue/IDLE_CHANGED.
The code:
this.$store.commit('idleVue/IDLE_CHANGED', false)
And It does change the value of the isIdle in the store, but the timer after that doesn't work.

Do you have any idea about that?

Stop idle tracking

Is there any way to stop idle tracking in a particular component and enable it after a certain interval?

Error: 'idleVue_onIdle' of undefined

I am using the latest version of the module.

This is how I am installing the module. I do not want to emit the event to all the component so I am omitting the eventHandler here.
Vue.use(IdleVue, { store, idleTime: 1*60*60*1000 });

I would expect that idleVue_onIdle event will not be called. But, in the console for each component, I am getting this error. Please see the screenshot here.
image

Just for the side note, I am able to watch the store property "isAppIdle" and it's working as expected. But, this console is making the things dirty.

Many thanks
Piyush

Event information

Gabriel,

Is it possible to get the event information directly through the evenEmitter (in isIdle() / isActive())? Currently, I am trying to switch to the active state on keyup (which works) however because IdleVue also listens for this event I get double events every time I keyup is emitted.

Idle-view component broken (PNG and clearRect errors)

Getting a 404 error touch.ba3be66.png cannot be found.
Also getting a ton of JS errors in build.js?16a9:880

Uncaught TypeError: this.ctx.clearRect is not a function
at VueComponent.spriteRender (build.js?16a9:880)
at VueComponent.boundFn [as spriteRender] (vue.esm.js?65d7:180)
at VueComponent.spriteLoop (build.js?16a9:888)
at boundFn (vue.esm.js?65d7:179)

idle-vue seems to prevent reload nuxt app

I have an application with idle-vue implemented.
After some time running on localhost and meanwhile refreshing the application it appears the nuxt app suddenly keeps loading and waiting for a process in idle-vue, keeping the screen white. This is after a manual page refresh. When I pause the debugger in DevTools, the file that is processed is always build.js in idle-vue. I don't really know how to reproduce this as it happens after a unknown period of time.

I know the package is no longer maintained but it's worth a shot as it's messing with my workflow bigtime.
Any people have experienced this or any hints on how to debug this situation?

Module not found: Error: Can't resolve './src/Idle'

I am getting this error while configuring it in main.js file. If I change the extension of the include in the index.js file from import IdleView from './src/Idle'; to import IdleView from './src/Idle.vue'; it works fine, but after changing this the build command fails. Any ideas as to why this is happening?

Inquiry about tracking the idle users

I need to listen to a specific button, I've initialized the "events" as 'none'
and when I click on the button I'm changing the value of idleVue.isIdle in the store by committing a mutation:
this.$store.commit('idleVue/IDLE_CHANGED', false)
But the issue is, it's stopping to show me that notification after I clicked on that button (reset button).

How to make the package listen to a specific button?

isIdle for All Tabs

i am facing a problem while implementing in my whole project.
isIdle works fine when my project is opened in single tab but when i open my project on second tab and no one is working on that tab isIdle value gets true whereas it should be false as user is working on the other tab of the project
How may i make it possible that if user is working on one tab of a site isIdle state must be false for all other tabs as well.
i am getting state by following
import store from './store'
Vue.use(IdleVue, { store})

const vm = new Vue({
store,
computed: {
messageStr() {
return this.isAppIdle ? 'ZZZ' : 'Hello'
}
}
})
putting condition on
$store.state.idleVue.isIdle

nuxt with idle-view ?

I try add idle-vue to my Nuxt app. But when I use idle-view to view. it is show error
Property or method "isIdle" is not defined on the instance but referenced during render

This my config:
plugins/idle-vue.js:

import IdleVue from 'idle-vue'
import Vue from 'vue'
import Vuex from 'vuex'
import IdleVueComponent from 'idle-vue/src/components/Idle.vue'

const store = new Vuex.Store({})

const eventsHub = new Vue()
Vue.component('idle-view', IdleVueComponent)
Vue.use(IdleVue, {
  eventEmitter: eventsHub,
  store,
  idleTime: 10000
})

nuxt.config.js

plugins: [
    {
      src: '~/plugins/firebase.js',
      ssr: false
    },
...
  ],

pages/index.vue

.form-group
  idle-view

I use nuxt ^2.6.2. Please help me. Thank you

[suggestion] Moving the computed property to a getter

Hey,
Awesome job with idle-vue!

I don't understand why you are injecting a computed property to every Vue object.
Why don't you create a getter isAppIdle in the store and let us subscribe to that value? That would be way more flexible & practical ;)

Thanks a lot!

Don't include idle-vue component in default build

For most people this is just bloat, most people will not use this component so you're just adding kB to their JS asset size. Just include it in the package as an example or to be extended or whatever. Anyone who wants to use this component can include it themselves.

For most people it will not work properly since it tried to request a PNG icon at a path that almost nobody will use.

Idle time option

I think idletime option does not work even If I put 500ms.
Any example of implementing this plugin as component and passing custom idle time to it?
I would be grateful
Thanks

Difficulty dynamically setting idleTime

Hi!

I'm trying to set and get the idle time from a propety we've stored in our own vuex store and we've run in to a road block.

I want to try something like this, but whenever I try, the idleTime property is not available in vuex, where is this stored?

Is it possible to change the idleTime property during runtime or is it only during mounting the App component that it can be 'hot-reloaded'?

//IdleTime setup:
import Vue from 'vue';
import IdleVue from 'idle-vue';
import store from '@/store';

Vue.use(IdleVue, {
    // idleTime: 15000, // unable to change when un-commented
    store
});

//Store - different file
const state = {
    idleVue: {
        idleTime: 5000,
        isIdle: true
    }
};

export default {
    state
};```

Can the idleTime be set after initialisation?

It would be great to be able to change the ideTime option that you can set initially in the options. In my case, I have users that want to be able to configure the idle time-out of modals. For this, the timeout setting is stored in localStorage, and I've got it fetching from there to set on the idleTime option, but I can't see a way of setting it after the initial app load. Only solution I could think of so far is to force a refresh, but I'm reluctant to do that as I believe it's bad practice in SPAs. Appreciate the help!

what is the actual timer in this? I don't understand

I'm using this. It's working great. I have it working in a Quasar Framework app. I've been through all of the code here and in idle-js and I still don't see what is actually doing the countdown. What am I missing?

Error when importing idle-vue

I want to use idle-vue in an electron app using https://github.com/SimulatedGREG/electron-vue, but when I import idle-vue in my main.js file it throws an error:

Uncaught SyntaxError: Unexpected token import
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at eval (eval at <anonymous> (http://localhost:9080/renderer.js:1959:1), <anonymous>:1:18)

The main.js looks like this:

import 'basscss/css/basscss.min.css'
import 'flexboxgrid/css/flexboxgrid.min.css'

import 'gsap'

import Vue from 'vue'
import axios from 'axios'

import App from './App'
import router from './router'
import store from './vuex/store'
import i18n from './locales/index'
import IdleVue from 'idle-vue'

import './lib/qrcode'

if (!process.env.IS_WEB) Vue.use(require('vue-electron'))
Vue.http = Vue.prototype.$http = axios
Vue.config.productionTip = false

Vue.use(IdleVue, { store })

/* eslint-disable no-new */
new Vue({
  components: { App },
  router,
  store,
  i18n,
  template: '<App/>'
}).$mount('#app')

It might happen because of a bad babel-loader configuration

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.