Commit 8ac82950 authored by ibuler's avatar ibuler

[Update] luna tree 增加收藏取消收藏

parent 7c357ae5
......@@ -48,6 +48,7 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
isLoadTreeAsync: boolean;
filterAssetCancel$: Subject<boolean> = new Subject();
loading = true;
favoriteAssets = [];
constructor(private _appSvc: AppService,
private _treeFilterSvc: TreeFilterService,
......@@ -105,6 +106,10 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
};
}
this._http.getFavoriteAssets().subscribe(resp => {
this.favoriteAssets = resp.map(i => i.asset);
});
this.loading = true;
this._http.getMyGrantedNodes(this.isLoadTreeAsync, refresh).subscribe(resp => {
this.loading = false;
......@@ -206,6 +211,15 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
return findSSHProtocol;
}
isAssetFavorite() {
const host = this.rightClickSelectNode.meta.asset;
if (!host) {
return false;
}
const assetId = host.id;
return this.favoriteAssets.indexOf(assetId) !== -1;
}
get RMenuList() {
const menuList = [{
'id': 'new-connection',
......@@ -219,6 +233,18 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
'fa': 'fa-file',
'hide': !this.nodeSupportSSH(),
'click': this.connectFileManager.bind(this)
}, {
'id': 'favorite',
'name': 'Favorite',
'fa': 'fa-star-o',
'hide': this.isAssetFavorite(),
'click': this.favoriteAsset.bind(this)
}, {
'id': 'disfavor',
'name': 'Disfavor',
'fa': 'fa-star',
'hide': !this.isAssetFavorite(),
'click': this.favoriteAsset.bind(this)
}];
if (!this.rightClickSelectNode) {
return [];
......@@ -265,6 +291,24 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
window.open(url, '_blank');
}
favoriteAsset() {
const host = this.rightClickSelectNode.meta.asset;
if (!host) {
return false;
}
const assetId = host.id;
if (this.isAssetFavorite()) {
this._http.favoriteAsset(assetId, false).subscribe(() => {
const i = this.favoriteAssets.indexOf(assetId);
this.favoriteAssets.splice(i, 1);
});
} else {
this._http.favoriteAsset(assetId, true).subscribe(() => {
this.favoriteAssets.push(assetId);
});
}
}
filterAssets(keyword) {
if (this.isLoadTreeAsync) {
this._logger.debug('Filter assets server');
......
......@@ -3,6 +3,7 @@ import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {Browser, DataStore} from '@app/globals';
import {GuacObjAddResp, SystemUser, TreeNode, User as _User} from '@app/model';
import {SettingService} from './setting';
import {getCookie} from '@app/utils/common';
@Injectable()
......@@ -12,23 +13,43 @@ export class HttpService {
constructor(private http: HttpClient, private settingSrv: SettingService) {
}
setOptionsCSRFToken(options) {
const csrfToken = getCookie('csrftoken');
let headers;
if (!options) {
options = {};
}
if (!options.headers) {
headers = new HttpHeaders();
} else {
headers = options.headers;
}
headers = headers.set('X-CSRFToken', csrfToken);
options.headers = headers;
return options;
}
get(url: string, options?: any) {
return this.http.get(url, options);
}
post(url: string, options?: any) {
return this.http.post(url, options);
post(url: string, body: any, options?: any) {
options = this.setOptionsCSRFToken(options);
return this.http.post(url, body, options);
}
put(url: string, options?: any) {
options = this.setOptionsCSRFToken(options);
return this.http.put(url, options);
}
delete(url: string, options?: any) {
options = this.setOptionsCSRFToken(options);
return this.http.delete(url, options);
}
patch(url: string, options?: any) {
options = this.setOptionsCSRFToken(options);
return this.http.patch(url, options);
}
......@@ -88,6 +109,25 @@ export class HttpService {
return this.http.get<Array<SystemUser>>(url);
}
favoriteAsset(assetId: string, favorite: boolean) {
let url: string;
if (favorite) {
url = `/api/v1/assets/favorite-assets/`;
const data = {
asset: assetId
};
return this.post(url, data);
} else {
url = `/api/v1/assets/favorite-assets/?asset=${assetId}`;
return this.delete(url);
}
}
getFavoriteAssets() {
const url = '/api/v1/assets/favorite-assets/';
return this.http.get<Array<any>>(url);
}
getGuacamoleToken(user_id: string, authToken: string) {
const body = new HttpParams()
.set('username', user_id)
......
......@@ -31,3 +31,19 @@ export function newTerminal(fontSize?: number) {
}
});
}
export function getCookie(name: string): string {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
......@@ -76,5 +76,7 @@
"yes": "是",
"no": "否",
"cols": "列数",
"rows": "行数"
"rows": "行数",
"favorite": "收藏",
"disfavor": "取消收藏"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment