helpdesk-search-base.component.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://www.pwm-project.org
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2018 The PWM Project
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. import {IPeopleService} from '../services/people.service';
  23. import SearchResult from '../models/search-result.model';
  24. import {isArray, isString, IPromise, IQService, IScope} from 'angular';
  25. import {IPerson} from '../models/person.model';
  26. import {IHelpDeskConfigService} from '../services/helpdesk-config.service';
  27. import LocalStorageService from '../services/local-storage.service';
  28. let verificationsDialogTemplateUrl = require('./verifications-dialog.template.html');
  29. let recentVerificationsDialogTemplateUrl = require('./recent-verifications-dialog.template.html');
  30. export default abstract class HelpDeskSearchBaseComponent {
  31. columnConfiguration: any;
  32. protected pendingRequests: IPromise<any>[] = [];
  33. photosEnabled: boolean;
  34. query: string;
  35. searchResult: SearchResult;
  36. searchTextLocalStorageKey: string;
  37. searchViewLocalStorageKey: string;
  38. verificationsEnabled: boolean;
  39. view: string;
  40. constructor(protected $q: IQService,
  41. protected $scope: IScope,
  42. protected $stateParams: angular.ui.IStateParamsService,
  43. protected configService: IHelpDeskConfigService,
  44. protected IasDialogService: any,
  45. protected localStorageService: LocalStorageService,
  46. protected peopleService: IPeopleService) {
  47. this.searchTextLocalStorageKey = this.localStorageService.keys.HELPDESK_SEARCH_TEXT;
  48. this.searchViewLocalStorageKey = this.localStorageService.keys.HELPDESK_SEARCH_VIEW;
  49. $scope.$watch('$ctrl.query', (newValue: string, oldValue: string) => {
  50. this.onSearchTextChange(newValue, oldValue);
  51. });
  52. }
  53. protected initialize(): void {
  54. this.query = this.getSearchText();
  55. this.configService.verificationsEnabled().then((verificationsEnabled: boolean) => {
  56. this.verificationsEnabled = verificationsEnabled;
  57. });
  58. }
  59. private getSearchText(): string {
  60. let param: string = this.$stateParams['query'];
  61. // If multiple query parameters are defined, use the first one
  62. if (isArray(param)) {
  63. param = param[0].trim();
  64. }
  65. else if (isString(param)) {
  66. param = param.trim();
  67. }
  68. return param || this.localStorageService.getItem(this.searchTextLocalStorageKey);
  69. }
  70. abstract fetchData(): void;
  71. protected fetchSearchData(): IPromise<void | SearchResult> {
  72. // this.abortPendingRequests();
  73. this.searchResult = null;
  74. if (!this.query) {
  75. // this.clearSearch();
  76. return null;
  77. }
  78. let promise = this.peopleService.search(this.query);
  79. this.pendingRequests.push(promise);
  80. return promise
  81. .then(
  82. (searchResult: SearchResult) => {
  83. // self.clearErrorMessage();
  84. // self.clearSearchMessage();
  85. // Aborted request
  86. if (!searchResult) {
  87. return;
  88. }
  89. // Too many results returned
  90. // if (searchResult.sizeExceeded) {
  91. // self.setSearchMessage('Display_SearchResultsExceeded');
  92. // }
  93. // No results returned. Not an else if statement so that the more important message is presented
  94. // if (!searchResult.people.length) {
  95. // self.setSearchMessage('Display_SearchResultsNone');
  96. // }
  97. return this.$q.resolve(searchResult);
  98. },
  99. (error) => {
  100. /*self.setErrorMessage(error);
  101. self.clearSearchMessage();*/
  102. })
  103. .finally(() => {
  104. // self.removePendingRequest(promise);
  105. });
  106. }
  107. private onSearchTextChange(newValue: string, oldValue: string): void {
  108. if (newValue === oldValue) {
  109. return;
  110. }
  111. this.storeSearchText();
  112. // this.clearSearchMessage();
  113. // this.clearErrorMessage();
  114. this.fetchData();
  115. }
  116. protected selectPerson(person: IPerson): void {
  117. this.IasDialogService
  118. .open({
  119. controller: 'VerificationsDialogController as $ctrl',
  120. templateUrl: verificationsDialogTemplateUrl,
  121. locals: {
  122. personUserKey: person.userKey,
  123. search: true
  124. }
  125. });
  126. }
  127. protected showVerifications(): void {
  128. this.IasDialogService
  129. .open({
  130. controller: 'RecentVerificationsDialogController as $ctrl',
  131. templateUrl: recentVerificationsDialogTemplateUrl
  132. });
  133. }
  134. protected storeSearchText(): void {
  135. this.localStorageService.setItem(this.searchTextLocalStorageKey, this.query || '');
  136. }
  137. }