member_list_editor.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. import {hasFeature} from '../helpers';
  18. import {WebClientService} from '../services/webclient';
  19. const AUTOCOMPLETE_MAX_RESULTS = 20;
  20. export default [
  21. '$log', 'WebClientService',
  22. function($log: ng.ILogService, webClientService: WebClientService) {
  23. return {
  24. restrict: 'EA',
  25. scope: {},
  26. bindToController: {
  27. members: '=eeeMembers',
  28. onChange: '=eeeOnChange',
  29. placeholder: '=eeePlaceholder',
  30. },
  31. controllerAs: 'ctrl',
  32. controller: [function() {
  33. // Cache all contacts with group chat support
  34. this.allContacts = Array
  35. .from(webClientService.contacts.values())
  36. .filter((contactReceiver: threema.ContactReceiver) => hasFeature(
  37. contactReceiver,
  38. threema.ContactReceiverFeature.GROUP_CHAT,
  39. $log,
  40. )) as threema.ContactReceiver[];
  41. this.selectedItemChange = (contactReceiver: threema.ContactReceiver) => {
  42. if (contactReceiver !== undefined) {
  43. this.members.push(contactReceiver.id);
  44. this.selectedItem = null;
  45. this.onChange(this.members);
  46. }
  47. };
  48. this.querySearch = (query: string): threema.ContactReceiver[] => {
  49. if (query !== undefined && query.length <= 0) {
  50. // Do not show a result on empty entry
  51. return [];
  52. } else {
  53. // Search for contacts, do not show selected contacts
  54. const lowercaseQuery = query.toLowerCase();
  55. const hideInactiveContacts = !webClientService.appConfig.showInactiveIDs;
  56. const result = this.allContacts
  57. .filter((contactReceiver: threema.ContactReceiver) => {
  58. // Ignore already selected contacts
  59. if (this.members.filter((id: string) => id === contactReceiver.id).length !== 0) {
  60. return false;
  61. }
  62. // Potentially ignore inactive contacts
  63. if (hideInactiveContacts && contactReceiver.state === 'INACTIVE') {
  64. return false;
  65. }
  66. // Search in display name
  67. if (contactReceiver.displayName.toLowerCase().indexOf(lowercaseQuery) >= 0) {
  68. return true;
  69. }
  70. // Search in identity
  71. if (contactReceiver.id.toLowerCase().indexOf(lowercaseQuery) >= 0) {
  72. return true;
  73. }
  74. // Not found
  75. return false;
  76. });
  77. return result.length <= AUTOCOMPLETE_MAX_RESULTS ? result
  78. : result.slice(0, AUTOCOMPLETE_MAX_RESULTS);
  79. }
  80. };
  81. this.onRemoveMember = (contact: threema.ContactReceiver): boolean => {
  82. if (contact.id === webClientService.me.id) {
  83. return false;
  84. }
  85. this.members = this.members.filter(
  86. (identity: string) => identity !== contact.id,
  87. );
  88. return true;
  89. };
  90. }],
  91. template: `
  92. <ul class="member-list">
  93. <li>
  94. <md-autocomplete
  95. md-no-cache="false"
  96. md-delay="200"
  97. md-selected-item="ctrl.selectedItem"
  98. md-search-text="ctrl.searchText"
  99. md-selected-item-change="ctrl.selectedItemChange(contactReceiver)"
  100. md-items="contactReceiver in ctrl.querySearch(ctrl.searchText)"
  101. md-item-text="contactReceiver.displayName"
  102. md-min-length="0"
  103. placeholder="{{ctrl.placeholder}}"
  104. md-menu-class="autocomplete-custom-template">
  105. <md-item-template>
  106. <eee-contact-badge
  107. eee-contact="contactReceiver"
  108. eee-disable-click="true"/>
  109. </md-item-template>
  110. </md-autocomplete>
  111. </li>
  112. <li ng-repeat="identity in ctrl.members">
  113. <eee-contact-badge
  114. eee-identity="identity"
  115. eee-disable-click="true"
  116. eee-on-remove="ctrl.onRemoveMember"/>
  117. </li>
  118. </ul>
  119. `,
  120. };
  121. },
  122. ];