Commit 8ac82950 authored by ibuler's avatar ibuler

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

parent 7c357ae5
...@@ -48,6 +48,7 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy { ...@@ -48,6 +48,7 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
isLoadTreeAsync: boolean; isLoadTreeAsync: boolean;
filterAssetCancel$: Subject<boolean> = new Subject(); filterAssetCancel$: Subject<boolean> = new Subject();
loading = true; loading = true;
favoriteAssets = [];
constructor(private _appSvc: AppService, constructor(private _appSvc: AppService,
private _treeFilterSvc: TreeFilterService, private _treeFilterSvc: TreeFilterService,
...@@ -105,6 +106,10 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy { ...@@ -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.loading = true;
this._http.getMyGrantedNodes(this.isLoadTreeAsync, refresh).subscribe(resp => { this._http.getMyGrantedNodes(this.isLoadTreeAsync, refresh).subscribe(resp => {
this.loading = false; this.loading = false;
...@@ -206,6 +211,15 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy { ...@@ -206,6 +211,15 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
return findSSHProtocol; 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() { get RMenuList() {
const menuList = [{ const menuList = [{
'id': 'new-connection', 'id': 'new-connection',
...@@ -219,6 +233,18 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy { ...@@ -219,6 +233,18 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
'fa': 'fa-file', 'fa': 'fa-file',
'hide': !this.nodeSupportSSH(), 'hide': !this.nodeSupportSSH(),
'click': this.connectFileManager.bind(this) '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) { if (!this.rightClickSelectNode) {
return []; return [];
...@@ -265,6 +291,24 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy { ...@@ -265,6 +291,24 @@ export class ElementAssetTreeComponent implements OnInit, OnDestroy {
window.open(url, '_blank'); 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) { filterAssets(keyword) {
if (this.isLoadTreeAsync) { if (this.isLoadTreeAsync) {
this._logger.debug('Filter assets server'); this._logger.debug('Filter assets server');
......
...@@ -3,6 +3,7 @@ import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; ...@@ -3,6 +3,7 @@ import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {Browser, DataStore} from '@app/globals'; import {Browser, DataStore} from '@app/globals';
import {GuacObjAddResp, SystemUser, TreeNode, User as _User} from '@app/model'; import {GuacObjAddResp, SystemUser, TreeNode, User as _User} from '@app/model';
import {SettingService} from './setting'; import {SettingService} from './setting';
import {getCookie} from '@app/utils/common';
@Injectable() @Injectable()
...@@ -12,23 +13,43 @@ export class HttpService { ...@@ -12,23 +13,43 @@ export class HttpService {
constructor(private http: HttpClient, private settingSrv: SettingService) { 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) { get(url: string, options?: any) {
return this.http.get(url, options); return this.http.get(url, options);
} }
post(url: string, options?: any) { post(url: string, body: any, options?: any) {
return this.http.post(url, options); options = this.setOptionsCSRFToken(options);
return this.http.post(url, body, options);
} }
put(url: string, options?: any) { put(url: string, options?: any) {
options = this.setOptionsCSRFToken(options);
return this.http.put(url, options); return this.http.put(url, options);
} }
delete(url: string, options?: any) { delete(url: string, options?: any) {
options = this.setOptionsCSRFToken(options);
return this.http.delete(url, options); return this.http.delete(url, options);
} }
patch(url: string, options?: any) { patch(url: string, options?: any) {
options = this.setOptionsCSRFToken(options);
return this.http.patch(url, options); return this.http.patch(url, options);
} }
...@@ -88,6 +109,25 @@ export class HttpService { ...@@ -88,6 +109,25 @@ export class HttpService {
return this.http.get<Array<SystemUser>>(url); 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) { getGuacamoleToken(user_id: string, authToken: string) {
const body = new HttpParams() const body = new HttpParams()
.set('username', user_id) .set('username', user_id)
......
...@@ -31,3 +31,19 @@ export function newTerminal(fontSize?: number) { ...@@ -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 @@ ...@@ -76,5 +76,7 @@
"yes": "是", "yes": "是",
"no": "否", "no": "否",
"cols": "列数", "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