Giter Club home page Giter Club logo

angular-page-visibility's Introduction

Angular Page Visibility

Getting started

First, install it.

npm install --save angular-page-visibility@latest

Then, import it into your @NgModule :

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularPageVisibilityModule } from 'angular-page-visibility';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, AngularPageVisibilityModule],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Finally, decorate your component :

import { Component, OnInit, OnDestroy } from '@angular/core';
import {
  OnPageVisible, OnPageHidden,
  OnPageVisibilityChange,
  AngularPageVisibilityStateEnum,
  OnPagePrerender, OnPageUnloaded} from 'angular-page-visibility';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
export class AppComponent implements OnInit, OnDestroy {
  ...
  @OnPageVisible()
  logWhenPageVisible (): void {
    console.log( 'OnPageVisible => visible' );
  }

  @OnPageHidden()
  logWhenPageHidden (): void {
    console.log( 'OnPageHidden => hidden' );
  }

  @OnPagePrerender()
  logWhenPagePrerender (): void {
    console.log( 'OnPagePrerender => prerender' );
  }

  @OnPageUnloaded()
  logWhenPageUnloaded (): void {
    console.log( 'OnPageUnloaded => unloaded' );
  }

  @OnPageVisibilityChange()
  logWhenPageVisibilityChange ( visibilityState: AngularPageVisibilityStateEnum ): void {
    if ( AngularPageVisibilityStateEnum[visibilityState]
      === AngularPageVisibilityStateEnum[AngularPageVisibilityStateEnum.VISIBLE]) {
      console.log( 'OnPageVisibilityChange => visible' );
    } else if (AngularPageVisibilityStateEnum[visibilityState]
      === AngularPageVisibilityStateEnum[AngularPageVisibilityStateEnum.HIDDEN]) {
      console.log( 'OnPageVisibilityChange => hidden' );
    } else if (AngularPageVisibilityStateEnum[visibilityState]
      === AngularPageVisibilityStateEnum[AngularPageVisibilityStateEnum.PRERENDER]) {
      console.log( 'OnPageVisibilityChange => prerender' );
    } else if (AngularPageVisibilityStateEnum[visibilityState]
      === AngularPageVisibilityStateEnum[AngularPageVisibilityStateEnum.UNLOADED]) {
      console.log( 'OnPageVisibilityChange => unloaded' );
    }
  }
  ...
}

Angular support

Supports Angular 11 and old versions.

Features and API

For any questions, suggestions, or feature requests

Please file an issue!

License

License under the MIT License (MIT)

Copyright © 2017-2020 Olivier LIN-SI-CHENG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

angular-page-visibility's People

Contributors

anderstornkvist avatar dependabot[bot] avatar olivierlsc avatar tuffant21 avatar

Stargazers

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

angular-page-visibility's Issues

Decorators doesnt work with Injectables

It works for members of a component but not for injected services.

export class TestComponent{
 ...
    @OnPageVisible()   
        pageVisible(): void {   
            this.isPageVisible = true;   
        }   

    @OnPageHidden()   
        pageHidden(): void {   
            this.testService.doSomthing().subscribe()   
        }   
}

The OnPageVisible() works but OnPageHidden() throws this.testService is undefined.

Angular 6 Compatibility

When upgrading my project to Angular 6, I'm getting the following compile error:

ERROR in Error during template compile of 'AngularPageVisibilityModule'
Function calls are not supported in decorators but 'ɵmakeDecorator' was called in 'NgModule'
'NgModule' calls 'ɵmakeDecorator'.

Any ideas for how I can address this? Thanks!

AoT compatibility

First off, awesome lib you made!! Many thanks!! Just a quick question / freature request; do you intent supporting AoT? If not, feel free to close the issue, if so, I'm looking forward to it :)

Ivy support?

I have Ivy enabled in my ng10 project and the lib seems to work fine - am I missing something or is the readme file maybe outdated?

And disable Ivy in tsconfig.json :

{
...
"angularCompilerOptions": {
...
"enableIvy": false
}

Angular Universal Support

Hi,

Angular page visibility does not work with SSR (universal) rendering.
At runtime we get

Error: document is not defined: Error during instantiation of AngularPageVisibilityService!. caused by: document is not defined

Thanks

Build error with aot support

When building with aot option, following error occurs. ng build --prod

ERROR in Error during template compile of 'AngularPageVisibilityModule'
  Function calls are not supported in decorators but 'ɵmakeDecorator' was called in 'NgModule'
    'NgModule' calls 'ɵmakeDecorator'.

typo in readme

this :

import { AngularPageVisibilityModule } from 'ngx-page-visibility';

should be :

import { NgxPageVisibilityModule } from 'ngx-page-visibility';

Angular 7 support

It would be great to let the library support Angular 7. Currently it's not supported because of the peer dependencies: you just get errors:

ERROR in node_modules/angular-page-visibility/lib/angular-page-visibility.service.d.ts(1,10): error TS2305: Module '"C:/Projects/NGP/Frontend/node_modules/rxjs/Observable"' has no exported member 'Observable'.
node_modules/rxjs/Observable.d.ts(1,15): error TS2307: Cannot find module 'rxjs-compat/Observable'.

rxjs error

hi do you have any idea about this error !?

ERROR in node_modules/angular-page-visibility/lib/angular-page-visibility.service.d.ts(1,10): error TS2305: Module '"/home/thunder/projects/pwa/pilapet-website/node_modules/rxjs/Observable"' has no exported member 'Observable'.
[ng] node_modules/rxjs/Observable.d.ts(1,15): error TS2307: Cannot find module 'rxjs-compat/Observable'.

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.