mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
migrate library directives from ACA (#6861)
This commit is contained in:
107
lib/core/directives/library-favorite.directive.ts
Normal file
107
lib/core/directives/library-favorite.directive.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2019 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Directive, HostListener, Input, OnChanges, Output, EventEmitter } from '@angular/core';
|
||||
import { SiteBody, FavoriteBody, FavoriteEntry, Site } from '@alfresco/js-api';
|
||||
import { AlfrescoApiService } from '../services/alfresco-api.service';
|
||||
|
||||
export interface LibraryEntity {
|
||||
entry: Site;
|
||||
isLibrary: boolean;
|
||||
isFavorite: boolean;
|
||||
}
|
||||
|
||||
@Directive({
|
||||
selector: '[adf-favorite-library]',
|
||||
exportAs: 'favoriteLibrary'
|
||||
})
|
||||
export class LibraryFavoriteDirective implements OnChanges {
|
||||
@Input('adf-favorite-library')
|
||||
library: LibraryEntity = null;
|
||||
|
||||
@Output() toggle = new EventEmitter<any>();
|
||||
// tslint:disable-next-line: no-output-native
|
||||
@Output() error = new EventEmitter<any>();
|
||||
|
||||
private targetLibrary = null;
|
||||
|
||||
@HostListener('click')
|
||||
onClick() {
|
||||
const guid = this.targetLibrary.entry.guid;
|
||||
|
||||
if (this.targetLibrary.isFavorite) {
|
||||
this.removeFavorite(guid);
|
||||
} else {
|
||||
this.addFavorite({
|
||||
target: {
|
||||
site: {
|
||||
guid
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
constructor(private alfrescoApiService: AlfrescoApiService) {}
|
||||
|
||||
ngOnChanges(changes) {
|
||||
if (!changes.library.currentValue) {
|
||||
this.targetLibrary = null;
|
||||
return;
|
||||
}
|
||||
|
||||
this.targetLibrary = changes.library.currentValue;
|
||||
this.markFavoriteLibrary(changes.library.currentValue);
|
||||
}
|
||||
|
||||
isFavorite(): boolean {
|
||||
return this.targetLibrary && this.targetLibrary.isFavorite;
|
||||
}
|
||||
|
||||
private async markFavoriteLibrary(library: LibraryEntity) {
|
||||
if (this.targetLibrary.isFavorite === undefined) {
|
||||
try {
|
||||
await this.alfrescoApiService.peopleApi.getFavoriteSite('-me-', library.entry.id);
|
||||
this.targetLibrary.isFavorite = true;
|
||||
} catch {
|
||||
this.targetLibrary.isFavorite = false;
|
||||
}
|
||||
} else {
|
||||
this.targetLibrary = library;
|
||||
}
|
||||
}
|
||||
|
||||
private addFavorite(favoriteBody: FavoriteBody) {
|
||||
this.alfrescoApiService.peopleApi
|
||||
.addFavorite('-me-', favoriteBody)
|
||||
.then((libraryEntry: FavoriteEntry) => {
|
||||
this.targetLibrary.isFavorite = true;
|
||||
this.toggle.emit(libraryEntry);
|
||||
})
|
||||
.catch((error) => this.error.emit(error));
|
||||
}
|
||||
|
||||
private removeFavorite(favoriteId: string) {
|
||||
this.alfrescoApiService.favoritesApi
|
||||
.removeFavoriteSite('-me-', favoriteId)
|
||||
.then((libraryBody: SiteBody) => {
|
||||
this.targetLibrary.isFavorite = false;
|
||||
this.toggle.emit(libraryBody);
|
||||
})
|
||||
.catch((error) => this.error.emit(error));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user