openaire-library/claims/claim-utils/claimContextSearchForm.comp...

514 lines
24 KiB
TypeScript

import {Component, Input,Output, EventEmitter, ViewChild} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import { Router} from '@angular/router';
import {ContextsService} from './service/contexts.service';
import {ClaimContext} from './claimEntities.class';
import { StaticAutoCompleteComponent } from '../../utils/staticAutoComplete/staticAutoComplete.component';
declare var UIkit:any;
import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
import{EnvProperties} from '../../utils/properties/env-properties';
@Component({
// moduleId: module.id,
selector: 'claim-contexts-search-form',
template: `
<div [class]="((inlineClaim)?'':' uk-card uk-card-default uk-padding uk-padding-medium ')+ 'uk-animation' " >
<!--div class="uk-text-lead">Select community:</div>
<div clas="uk-margin-left">
<div *ngIf="selectedCommunityId" >
<div class="uk-text-bold">My community:</div>
<div class="uk-margin-left">
<span>
{{selectedCommunityLabel}}
</span>
<a *ngIf="!isSelected(selectedCommunityId)" (click)="addNewContext(selectedCommunityLabel,null,{'id':selectedCommunityId, 'label':selectedCommunityLabel})" class="uk-icon-button icon-button-small uk-icon">
<span class="uk-icon">
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="0.8"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg>
</span> </a>
<span *ngIf="isSelected(selectedCommunityId)" class="uk-label uk-label-success uk-margin-small-top">Added</span>
</div>
</div>
<hr *ngIf="selectedCommunityId">
<div *ngIf="selectedCommunityId" class="uk-text-bold">Other communities:</div>
<div class="uk-child-width-1-3@m uk-margin-left uk-grid-match uk-grid">
<ng-container *ngFor="let community of communities">
<div *ngIf="community.id != selectedCommunityId">
<div>
<span>
{{community.label}}
</span>
<a *ngIf="!isSelected(community.id)" (click)="addNewContext(community.label,null,{'id':community.id, 'label':community.label})" class="uk-icon-button icon-button-small uk-icon">
<span class="uk-icon">
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="0.8"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg>
</span> </a>
<span *ngIf="isSelected(community.id)" class="uk-label uk-label-success uk-margin-small-top">Added</span>
</div>
</div>
</ng-container >
</div>
</div-->
<div class="uk-grid uk-margin-top uk-animation-fade">
<div class="uk-width-expand">
<!--div class="uk-text-lead">More options:</div-->
<div class="uk-text-large">Select a community <helper div="link-context-form" tooltip=true></helper> </div>
<select [(ngModel)]="selectedCommunityId" (ngModelChange)="communityChanged()" class="uk-margin-large-bottom uk-select uk-width-medium " name="select_funder" >
<option value="0" >Select community</option>
<option *ngFor="let community of communities" [value]="community.id" >{{community.label}}</option>
</select>
<div class="uk-text-large">and then</div>
<div class=" uk-margin-left uk-margin-left">
<div class=" uk-text-bold">search for community concepts</div>
<table class="uk-margin-remove uk-width-1-1 uk-table uk-table-responsive" ><tr>
<tr>
<td *ngIf="selectedCommunityId != '0' && categories.length > 0" class="uk-width-medium">
<select [(ngModel)]="selectedCategoryId" (ngModelChange)="categoryChanged()" class="uk-select uk-width-medium " name="select_funder" >
<option value="0" >Select category</option>
<option *ngFor="let category of categories" [value]="category.id" >{{category.label}}</option>
</select>
</td><td >
<static-autocomplete [(properties)]=properties [(list)] = concepts [allowDuplicates]=true [showSelected]=false [placeHolderMessage] = "'Concept name'" title = "Concepts" [multipleSelections]=true (addItem) = "select($event)" > </static-autocomplete>
</td></tr>
</table>
</div>
<div *ngIf="loading" class="uk-alert uk-alert-primary" role="alert">Loading communities information...</div>
<div *ngIf="error" class="uk-alert uk-alert-warning" role="alert">An error occured while loading communities...</div>
<div class="uk-text-large ">or</div>
<div class=" uk-text-bold uk-margin-bottom uk-margin-left">browse through community categories:
<button uk-toggle="target: #browse; animation: uk-animation-fade" class="uk-button portal-button"> Browse </button>
</div>
<div class="uk-margin-left">
<div id="browse" class=" uk-margin-small">
<!--a uk-toggle="target: #browse; animation: uk-animation-fade" class="uk-float-right"><span class="uk-icon">
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="close" ratio="1"><path fill="none" stroke="#000" stroke-width="1.06" d="M16,16 L4,4"></path><path fill="none" stroke="#000" stroke-width="1.06" d="M16,4 L4,16"></path></svg>
</span></a-->
<div *ngIf="selectedCommunityId =='0'" class="uk-alert uk-alert-warning" >Please select community first...</div>
<div *ngIf="selectedCommunityId !='0'" class="uk-panel uk-panel-scrollable">
<ul class="uk-list ">
<li>
<span (click)="displaySubcategory(selectedCommunityId)" >
<span *ngIf="!conceptsClassDisplay[selectedCommunityId]" class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="8 5 13 10 8 15"></polygon></svg></span>
<span *ngIf="conceptsClassDisplay[selectedCommunityId]" class="uk-icon">
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="5 7 15 7 10 12"></polygon></svg></span>{{selectedCommunityLabel}}
</span>
<a *ngIf="!isSelected(selectedCommunityId)" (click)="addNewContext(selectedCommunityLabel,null,{'id':selectedCommunityId, 'label':selectedCommunityLabel})" class="uk-icon-button icon-button-small uk-icon">
<span class="uk-icon">
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="0.8"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg>
</span> </a>
<span *ngIf="isSelected(selectedCommunityId)" class="uk-label uk-label-success uk-margin-small-top">Added</span>
<ul *ngIf="conceptsClassDisplay[selectedCommunityId]" class="uk-animation-fade" >
<li *ngFor="let category of categories" >
<span (click)="browseConcepts(category.id)" >
<span *ngIf="!conceptsClassDisplay[category.id]" class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="8 5 13 10 8 15"></polygon></svg></span>
<span *ngIf="conceptsClassDisplay[category.id]" class="uk-icon">
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="5 7 15 7 10 12"></polygon></svg></span>
{{category.label}}</span><a *ngIf="!isSelected(category.id)" (click)="addNewContext(selectedCommunityLabel,category.label,{'id':category.id, 'label':category.label})" class="uk-icon-button icon-button-small" >
<span class="uk-icon">
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="0.8"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg>
</span></a>
<span *ngIf="isSelected(category.id)" class="uk-label uk-label-success uk-margin-small-top">Added</span>
<div *ngIf="conceptsCategoryLoading[category.id]" class="uk-alert uk-alert-primary" role="alert">Loading category information...</div>
<ul *ngIf="conceptsClass[category.id] && conceptsClassDisplay[category.id]" class="uk-list uk-animation-fade" id="ul_{{category.id}}" >
<li *ngFor="let concept1 of conceptsClass[category.id]" >
<span (click)="displaySubcategory(concept1.id)" >
<span *ngIf="!conceptsClassDisplay[concept1.id] && concept1.concepts" class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="8 5 13 10 8 15"></polygon></svg></span>
<span *ngIf="conceptsClassDisplay[concept1.id] && concept1.concepts" class="uk-icon">
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="5 7 15 7 10 12"></polygon></svg></span>
{{concept1.label}}
</span>
<a *ngIf="!isSelected(concept1.id)" (click)="addNewContext(selectedCommunityLabel,category.label,concept1)" class="uk-icon-button icon-button-small"><span class="uk-icon">
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="0.8"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg>
</span></a>
<span *ngIf="isSelected(concept1.id)" class="uk-label uk-label-success uk-margin-small-top">Added</span>
<ul *ngIf="concept1.concepts && conceptsClassDisplay[concept1.id] " class="uk-animation-fade" >
<li *ngFor="let concept2 of concept1.concepts"><span (click)="displaySubcategory(concept2.id)" >
<span *ngIf="!conceptsClassDisplay[concept2.id] && concept2.concepts" class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="8 5 13 10 8 15"></polygon></svg></span>
<span *ngIf="conceptsClassDisplay[concept2.id] && concept2.concepts" class="uk-icon">
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="5 7 15 7 10 12"></polygon></svg></span>
{{concept2.label}}</span>
<a *ngIf="!isSelected(concept2.id)" (click)="addNewContext(selectedCommunityLabel,category.label,concept2)" class="uk-icon-button icon-button-small"><span class="uk-icon">
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="0.8"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg>
</span></a>
<span *ngIf="isSelected(concept2.id)" class="uk-label uk-label-success uk-margin-small-top">Added</span>
<ul *ngIf="concept2.concepts && conceptsClassDisplay[concept2.id] " class="uk-animation-fade" >
<li *ngFor="let concept3 of concept2.concepts">{{concept3.label}}
<a *ngIf="!isSelected(concept3.id)" (click)="addNewContext(selectedCommunityLabel,category.label,concept3)" class="uk-icon-button icon-button-small"><span class="uk-icon">
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="0.8"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg>
</span></a>
<span *ngIf="isSelected(concept3.id)" class="uk-label uk-label-success uk-margin-small-top">Added</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</ul>
</div>
</div>
</div>
<div *ngIf="warningMessage.length > 0" class="uk-alert uk-alert-warning" role="alert">{{warningMessage}}</div>
<div *ngIf="infoMessage.length > 0" class="uk-alert uk-alert-primary" role="alert">{{infoMessage}}</div>
</div>
</div>
<!--a *ngIf="simpleView" class="uk-float-right " (click)="simpleView=!simpleView">More options <span class="uk-icon">
<svg height="20" icon="chevron-right" ratio="1" viewBox="0 0 20 20" width="20" xmlns="http://www.w3.org/2000/svg"><polyline fill="none" points="7 4 13 10 7 16" stroke="#000" stroke-width="1.03"></polyline></svg>
</span>
</a>
<a *ngIf="!simpleView" class="uk-float-right " (click)="simpleView=!simpleView">Less options <span class="uk-icon">
<svg height="20" icon="chevron-right" ratio="1" viewBox="0 0 20 20" width="20" xmlns="http://www.w3.org/2000/svg"><polyline fill="none" points="7 4 13 10 7 16" stroke="#000" stroke-width="1.03"></polyline></svg>
</span>
</a-->
</div>
`
})
export class ClaimContextSearchFormComponent {
// @Input() public inline:boolean = false ; // for claimed started from landing pages
public showComponent:boolean = true ; // for claimed started from landing pages
@Input() public selectedList;
//The following need to be kept in case we have to save the current state
@Input() public projects;
@Input() public results;
@Input() public inlineEntity;
@Input() public properties:EnvProperties;
@Input() communityId:string= null;
@Input() public inlineClaim:boolean=false;
public selectedCommunityId:string = "0";
public selectedCategoryId:string ="0";
// @Output() contextSelected = new EventEmitter();
@ViewChild (StaticAutoCompleteComponent) autocomplete : StaticAutoCompleteComponent ;
public query = '';
public filteredList = [];
public communities:any;
public selectedCommunityLabel:string = "Community:";
public categories:any;
public selectedCategoryLabel:string ="Category:";
public concepts = [];
public conceptsClass = [];
public conceptsClassDisplay = [];
public conceptsCategoryLoading = [];
public warningMessage = "";
public infoMessage = "";
public loading:boolean = false;
public error:boolean = false;
public simpleView:boolean = true;
@Input() localStoragePrefix:string = "";
ngOnInit() {
this.getCommunities();
}
constructor(private _contextService: ContextsService,private router: Router) {
}
select($event){
var item = $event.value;
this.addNewContext( this.selectedCommunityLabel, this.selectedCategoryLabel, item);
}
isSelected(id):boolean{
for (var _i = 0; _i < this.selectedList.length; _i++) {
let item = this.selectedList[_i];
if(item.concept.id == id){
return true;
// this.warningMessage = "Concept already in selected list";
}
}
return false;
}
addNewContext(community,category,concept, notify=true){
if(this.selectedList.length > 50){
UIkit.notification({
message : 'Your basket exceeds the number of allowed concepts (50)',
status : 'warning',
timeout : 1500,
pos : 'top-center'
});
return;
}
var context: ClaimContext= { community: community, category: category, concept: concept };
var found:boolean = false;
this.warningMessage = "";
if (!this.isSelected(context.concept.id)) {
this.selectedList.push(context);
if(notify){
UIkit.notification({
message : 'A new concept added in your basket.',
status : 'primary',
timeout : 1500,
pos : 'top-center'
});
}
localStorage.setItem(this.localStoragePrefix + "contexts", JSON.stringify(this.selectedList));
}else{
if(notify){
UIkit.notification({
message : 'The concept is already in your basket.',
status : 'warning',
timeout : 1500,
pos : 'top-center'
});
}
}
}
getCommunities () {
if(!Session.isLoggedIn()){
this.saveStateAndRedirectLogin();
}else{
this.loading = true;
this._contextService.getCommunities(this.properties.contextsAPI).subscribe(
data => {
this.communities = data;
if(this.communities.length > 0){
this.communities.sort((n1,n2)=> n1.label > n2.label);
}
this.loading = false;
if(this.communityId != null){
//preselect community
this.selectedCommunityId = this.communityId;
this.communityChanged()
this.addNewContext(this.selectedCommunityLabel,null,{'id':this.selectedCommunityId, 'label':this.selectedCommunityLabel}, false)
}
},
err => {
//console.log(err);
this.handleError("Error getting communities", err);
this.loading = false;
this.error = true;
}
);
}
}
getCategories () {
this.loading = true;
this.categories=[];
if(this.selectedCommunityId != '0'){
if(!Session.isLoggedIn()){
this.saveStateAndRedirectLogin();
}else{
this._contextService.getCategories(this.selectedCommunityId,this.properties.contextsAPI).subscribe(
data => {
this.categories =data;
this.concepts = [];
this.addCommunityInConcepts();
this.filteredList = [];
if (this.query !== ""){
var event = {value: ""};
event.value = this.query;
}
this.loading = false;
},
err => {
//console.log(err);
this.handleError("Error getting categories for community with id: "+this.selectedCommunityId, err);
this.loading = false;
}
);
}
}
}
getConcepts () {
this.loading = true;
if(this.selectedCategoryId != '0'){
if(!Session.isLoggedIn()){
this.saveStateAndRedirectLogin();
}else{
this.concepts = [];
this._contextService.getConcepts(this.selectedCategoryId, "",true, this.properties.contextsAPI).subscribe(
data => {
this.concepts =data;
for(var i =0 ; i<data.length; i++){
if(data[i].hasSubConcept == true){
this.getSubConcepts(data[i].id);
}
}
this.addCommunityInConcepts();
if (this.query !== ""){
var event = {value: ""};
event.value = this.query;
// this.filter(event);
}
this.loading = false;
},
err => {
//console.log(err);
this.handleError("Error getting concepts for category with id: "+this.selectedCategoryId, err);
this.loading = false;
}
);
}
}else{
this.concepts=[];
this.loading = false;
}
}
getSubConcepts (conceptId) {
this.loading = true;
if(this.selectedCategoryId != '0'){
if(!Session.isLoggedIn()){
this.saveStateAndRedirectLogin();
}else{
this._contextService.getSubConcepts(conceptId, "",true, this.properties.contextsAPI).subscribe(
data => {
for(var i =0 ; i<data.length; i++){
this.concepts.push(data[i]);
}
if (this.query !== ""){
var event = {value: ""};
event.value = this.query;
// this.filter(event);
}
this.loading = false;
},
err => {
//console.log(err);
this.handleError("Error getting subconcepts for concept with id: "+conceptId, err);
this.loading = false;
}
);
}
}else{
this.concepts=[];
this.loading = false;
}
}
displaySubcategory(id) {
if(this.conceptsClassDisplay[id] != null){
this.conceptsClassDisplay[id] = !this.conceptsClassDisplay[id];
}else{
this.conceptsClassDisplay[id] = true;
}
}
browseConcepts (categoryId) {
if(!Session.isLoggedIn()){
this.saveStateAndRedirectLogin();
}else{
if(this.conceptsClass[categoryId] != null){
this.conceptsClassDisplay[categoryId] = !this.conceptsClassDisplay[categoryId];
return;
}else{
this.conceptsClassDisplay[categoryId] = true;
}
this.conceptsClass[categoryId] = [];
this.conceptsCategoryLoading[categoryId] = true;
this._contextService.getConcepts(categoryId, "",false, this.properties.contextsAPI).subscribe(
data => {
var concepts = data;
this.conceptsClass[categoryId] = [];
for(var i =0 ; i<data.length; i++){
if(data[i].hasSubConcept == true){
this.browseSubConcepts(categoryId, data[i].id);
}else{
this.conceptsClass[categoryId].push(data[i]);
}
}
this.conceptsCategoryLoading[categoryId] = false;
},
err => {
//console.log(err);
this.handleError("Error getting concepts for category with id: "+this.selectedCategoryId, err);
this.conceptsCategoryLoading[categoryId] = false;
}
);
}
}
browseSubConcepts (categoryId, conceptId) {
this.conceptsCategoryLoading[categoryId] = true;
this._contextService.getSubConcepts(conceptId, "",false, this.properties.contextsAPI).subscribe(
data => {
var concepts = data[0];
this.conceptsClass[categoryId].push(concepts)
this.conceptsCategoryLoading[categoryId] = false;
},
err => {
//console.log(err);
this.handleError("Error getting subconcepts for concept with id: "+conceptId, err);
this.conceptsCategoryLoading[categoryId] = false;
}
);
}
communityChanged(){
this.warningMessage = "";
this.infoMessage = "";
for(var i = 0; i< this.communities.length; i++){
if(this.communities[i].id==this.selectedCommunityId){
this.selectedCommunityLabel = this.communities[i].label;
break;
}
}
this.selectedCategoryId = "0";
this.selectedCategoryLabel="Select Category:";
if(this.selectedCommunityId != "0"){
this.getCategories();
}
}
categoryChanged(){
this.warningMessage = "";
this.infoMessage = "";
for(var i = 0; i< this.categories.length; i++){
if(this.categories[i].id==this.selectedCategoryId){
this.selectedCategoryLabel = this.categories[i].label;
break;
}
}
this.getConcepts();
}
addCommunityInConcepts(){
this.concepts.push({"id":this.selectedCommunityId, "label":this.selectedCommunityLabel});
if(this.autocomplete){
this.autocomplete.updateList(this.concepts);
}
}
saveStateAndRedirectLogin(){
if(this.projects != null){
localStorage.setItem(this.localStoragePrefix + "projects", JSON.stringify(this.projects));
}
localStorage.setItem(this.localStoragePrefix + "contexts", JSON.stringify(this.selectedList));
if(this.results != null){
localStorage.setItem(this.localStoragePrefix + "results", JSON.stringify(this.results));
}
if(this.inlineEntity != null){
localStorage.setItem(this.localStoragePrefix + "inlineEntity", JSON.stringify(this.inlineEntity));
}
this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this.router.url } });
}
private handleError(message: string, error) {
console.error("Claim context search form (component): "+message, error);
}
}