Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { AlertController, LoadingController } from 'ionic-angular';
  3. import { SignonService, IServiceCallback } from 'e1-service';
  4. import { signonPrompt } from '../misc/prompts';
  5.  
  6. export interface IServiceCall {
  7. call(callback: IServiceCallback);
  8. }
  9. /*
  10. Helper functions for e1-service.
  11. Usage:
  12. Use the call function with the service to use and callback parameters.
  13. If no token, then the call is directed to callWithSignonPrompt.
  14. If call returns error, then try signon and call the service again.
  15. callWithSignonPrompt: Display signon
  16. */
  17. @Injectable()
  18. export class E1HelperService {
  19. callWithSignonInProgress: boolean = false;
  20. callWithSignonPromptInProgress: boolean = false;
  21. callWithSignonPrompt(service: IServiceCall, callback: IServiceCallback = {}) {
  22. if (this.callWithSignonPromptInProgress) {
  23. return;
  24. }
  25. this.callWithSignonPromptInProgress = true;
  26. let prompt = this.promptCtrl.create(signonPrompt(
  27. cred => {
  28. if (cred.username && cred.password) {
  29. let loading = this.loadCtrl.create({
  30. content: 'Signing in..'
  31. });
  32. loading.present();
  33. this.signon.username = cred.username;
  34. this.signon.password = cred.password;
  35. this.signon.authenticate({
  36. success: () => {
  37. service.call(callback);
  38. prompt.dismiss();
  39. this.callWithSignonPromptInProgress = false;
  40. },
  41. done: () => {
  42. console.log('Signon done!');
  43. loading.dismiss();
  44. }
  45. });
  46. }
  47. return false;
  48. },
  49. () => {
  50. this.callWithSignonPromptInProgress = false;
  51. }
  52. ));
  53. prompt.present();
  54. }
  55. callWithSignon(service: IServiceCall, callback: IServiceCallback = {}) {
  56. if (this.callWithSignonInProgress) {
  57. return;
  58. }
  59. this.callWithSignonInProgress = true;
  60. this.signon.authenticate({
  61. success: () => service.call(callback),
  62. error: () => this.callWithSignonPrompt(service, callback),
  63. done: this.callWithSignonInProgress = false
  64. });
  65. }
  66. call(service: IServiceCall, callback: IServiceCallback = {}) {
  67. let cb = Object.assign({}, callback, {
  68. error: msg => this.callWithSignon(service, callback)
  69. });
  70. if (this.signon.hasToken) {
  71. service.call(cb)
  72. } else {
  73. this.callWithSignonPrompt(service, callback);
  74. }
  75. }
  76. constructor(
  77. public promptCtrl: AlertController,
  78. public loadCtrl: LoadingController,
  79. public signon: SignonService
  80. ) {
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement