/client-side/admin/src/app/service/shared.service.ts

https://bitbucket.org/AhmedElMetwally/cars-rental · TypeScript · 157 lines · 136 code · 21 blank · 0 comment · 23 complexity · bdddb1cd31b0e1459131c9d4caa9a088 MD5 · raw file

  1. import { mainErrorEnum , mainErrorEnumEn , mainErrorEnumAr } from './../../../../../shared/enums/error.enum';
  2. import { RegExpCityModel } from '../models/cityModel';
  3. import { RegExpBookingModel } from '../models/bookingModel';
  4. import { EbookingType, EbookingTypeAr, EbookingTypeEn } from '../enums/booking.enum';
  5. import { RegExpNationalityModel } from '../models/nationalityModel'
  6. import { EsignupFromAr , EsignupFromEn , EsignupFrom } from '../enums/client.enum';
  7. import { RegExpClientModel } from '../models/clientModel';
  8. import { EgearType, EgearTypeEn, EgearTypeAr } from '../enums/car.enum';
  9. import { RegExpCarModel } from '../models/carModel';
  10. import { Escreens , EscreensAr, EscreensEn } from '../enums/screens.enum';
  11. import { RegExpCarModelModel } from '../models/carModelModel';
  12. import { RegExpCarTypeModel } from '../models/carTypeModel';
  13. import { RegExpCarGroupModel } from '../models/carGroupModel';
  14. import { RegExpBranchModel } from '../models/branchModel';
  15. import { RegExpEmployeeGroupModel } from '../models/employeeGroupModel';
  16. import { RegExpEmployeeModel } from '../models/employeeModel';
  17. import { RegExpAdminModel } from '../models/adminModel';
  18. import { Injectable } from '@angular/core';
  19. import { AbstractControl } from '@angular/forms';
  20. import { EstatusAr, Estatus , EstatusEn } from '../enums/car.enum';
  21. declare let $;
  22. @Injectable({
  23. providedIn: 'root'
  24. })
  25. export class SharedService {
  26. constructor(){ };
  27. public mainErrorEnum = mainErrorEnum;
  28. public mainErrorEnumEn = mainErrorEnumEn;
  29. public mainErrorEnumAr = mainErrorEnumAr;
  30. public Escreens = Escreens;
  31. public EscreensAr = EscreensAr;
  32. public EscreensEn = EscreensEn;
  33. public Estatus = Estatus;
  34. public EstatusAr = EstatusAr;
  35. public EstatusEn = EstatusEn;
  36. public EgearType = EgearType;
  37. public EgearTypeAr = EgearTypeAr;
  38. public EgearTypeEn = EgearTypeEn;
  39. public EsignupFrom = EsignupFrom;
  40. public EsignupFromAr = EsignupFromAr;
  41. public EsignupFromEn = EsignupFromEn;
  42. public EbookingType = EbookingType;
  43. public EbookingTypeAr = EbookingTypeAr;
  44. public EbookingTypeEn = EbookingTypeEn;
  45. public mainRegExp = {
  46. RegExpEmployeeModel,
  47. RegExpAdminModel,
  48. RegExpEmployeeGroupModel,
  49. RegExpBranchModel,
  50. RegExpCarGroupModel,
  51. RegExpCarModelModel,
  52. RegExpCarTypeModel,
  53. RegExpCarModel,
  54. RegExpClientModel,
  55. RegExpNationalityModel,
  56. RegExpBookingModel,
  57. RegExpCityModel,
  58. };
  59. public clearLocalStorage(): void {
  60. localStorage.removeItem('token');
  61. localStorage.removeItem('admin');
  62. localStorage.removeItem('employee');
  63. };
  64. public setTitle(title: string): void {
  65. $('title').text(title);
  66. };
  67. public getErrorMsg(errorEnum: mainErrorEnum): string {
  68. if((window as any).lang == 'ar') {
  69. return mainErrorEnumAr[errorEnum];
  70. } else {
  71. return mainErrorEnumEn[errorEnum];
  72. };
  73. }
  74. public customValidator(requiredErrorEnum: mainErrorEnum , invalidErrorEnum: mainErrorEnum , regExp: RegExp): any {
  75. return (control: AbstractControl) => {
  76. if(Array.isArray(control.value) ) {
  77. if ( ! control.value.every( v => regExp.test(v)) ) {
  78. return {
  79. errorEnum : invalidErrorEnum
  80. };
  81. } else {
  82. return null
  83. };
  84. } else {
  85. if(control.value === '' || control.value === null){
  86. return {
  87. errorEnum : requiredErrorEnum
  88. };
  89. } else if ( !regExp.test(control.value) ) {
  90. return {
  91. errorEnum : invalidErrorEnum
  92. };
  93. } else {
  94. return null
  95. };
  96. };
  97. };
  98. };
  99. public msToTime(duration) {
  100. let seconds = parseInt((duration / 1000) % 60 + '');
  101. let minutes = parseInt((duration / (1000 * 60)) % 60 + '');
  102. let hours = parseInt((duration / (1000 * 60 * 60)) % 24 + '');
  103. let h = (hours < 10) ? "0" + hours : hours;
  104. let m = (minutes < 10) ? "0" + minutes : minutes;
  105. let s = (seconds < 10) ? "0" + seconds : seconds;
  106. return h + ":" + m
  107. };
  108. public numberEnToAr(str: string): string {
  109. const numberEnToArMap: string[] = [
  110. '٠',
  111. '١',
  112. '٢',
  113. '٣',
  114. '٤',
  115. '٥',
  116. '٦',
  117. '٧',
  118. '٨',
  119. '٩'
  120. ];
  121. return str.replace(/\d/g, function($0) { return numberEnToArMap[$0]})
  122. };
  123. public numberArToEn(str: string): string {
  124. str = str.replace(/٠/g, '0');
  125. str = str.replace(/١/g, '1');
  126. str = str.replace(/٢/g, '2');
  127. str = str.replace(/٣/g, '3');
  128. str = str.replace(/٤/g, '4');
  129. str = str.replace(/٥/g, '5');
  130. str = str.replace(/٦/g, '6');
  131. str = str.replace(/٧/g, '7');
  132. str = str.replace(/٨/g, '8');
  133. str = str.replace(/٩/g, '9');
  134. return str;
  135. };
  136. }