<commit_before>import {
  Component,
  Input,
  OnInit,
  ViewChild,
  ElementRef
} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Space } from 'ngx-fabric8-wit';
import { WorkItemUI, WorkItem } from './../../models/work-item';
import { WorkItemLinkUI } from './../../models/link';
import { LinkTypeUI } from './../../models/link-type';
import { Observable } from 'rxjs/Observable';

import { WorkItemService } from '../../services/work-item.service';

//ngrx stuff
import { AppState } from './../../states/app.state';
import { Store } from '@ngrx/store';
import * as LinkTypeActions from './../../actions/link-type.actions';
import * as WorkItemLinkActions from './../../actions/work-item-link.actions';

@Component({
  selector: 'work-item-link',
  templateUrl: './work-item-link.component.html',
  styleUrls: ['./work-item-link.component.less']
})

export class WorkItemLinkComponent implements OnInit {
  @Input() loggedIn: Boolean;
  @Input() detailContext: string; // It should be detail or preview
  @ViewChild('searchResultList') searchResultList: any;
  @ViewChild('linkTypeSelector') linkTypeSelector: ElementRef;
  @ViewChild('wiSearchBox') wiSearchBox: ElementRef;

  @Input('workItem') set workItemSetter(workItem: WorkItemUI) {
    this.workItem = workItem;
    this.store.dispatch(
      new WorkItemLinkActions.Get(this.workItem.link+'/relationships/links')
    );
  }

  private workItem: WorkItemUI = null;
  private eventListeners: any[] = [];
  private linkTypes: LinkTypeUI[] = [];
  private workItemLinks: WorkItemLinkUI[] = [];
  private showLinkComponent: Boolean = false;
  private selectedTab: string | null = null;
  private showLinkView: Boolean = false;
  private showLinkCreator: Boolean = true;
  private selectedLinkType: LinkTypeUI = null;
  searchWorkItems: WorkItem[] = [];
  searchNotAllowedIds: string[] = [];
  selectedWorkItemId: string;
  selectedValue: string = '';

  private spaceSource = this.store
    .select('listPage')
    .select('space')
    .filter(s => !!s);
  private linkTypeSource = this.store
    .select('detailPage')
    .select('linkType')
    .filter(lt => !!lt.length);
  private workItemLinkSource = this.store
    .select('detailPage')
    .select('workItemLink');

  constructor(
    private store: Store<AppState>,
    private workItemService: WorkItemService
  ){}

  ngOnInit() {
    this.eventListeners.push(
      this.spaceSource.take(1)
        .subscribe((space: Space) => {
          this.store.dispatch(new LinkTypeActions.Get());
        })
    );

    Observable.combineLatest(
      this.spaceSource,
      this.linkTypeSource
    ).take(1).subscribe(([
      spaceSource,
      linkTypeSource
    ]) => {
      this.linkTypes = [...linkTypeSource];
      this.workItemLinkSource.subscribe(workItemLinks => {
        this.workItemLinks = [...workItemLinks];
        if (this.linkTypeSelector && this.wiSearchBox) {
          this.wiSearchBox.nativeElement.value = '';
        }
      });
    })
  }

  toggleLinkComponent(onlyOpen: Boolean = false) {
    if (this.loggedIn) {
      if (onlyOpen) {
        this.showLinkComponent = true;
      } else {
        this.showLinkComponent = !this.showLinkComponent;
      }
    }
    if (!this.showLinkComponent) {
      this.selectedTab = null;
    } else {
      if (!this.selectedTab) {
        this.selectedTab = 'all';
      }
    }
  }

  selectTab(linkTypeName: string | null = null) {
    this.selectedTab = linkTypeName;
    this.toggleLinkComponent(true);
  }

  toggleLinkView() {
    this.showLinkView = !this.showLinkView;
  }

  toggleLinkCreator() {
    this.showLinkCreator = !this.showLinkCreator;
  }

  onSelectRelation(relation: any): void{
    //clear the search box and reset values related to search
    this.wiSearchBox.nativeElement.value = '';
    this.searchWorkItems = [];
    this.selectedWorkItemId = null;
    this.selectedLinkType = relation;
  }

  selectSearchResult(id: string, number: number, title: string){
    this.selectedWorkItemId = id;
    this.selectedValue = number + ' - ' + title;
    this.searchWorkItems = [];
  }

  searchWorkItem(term, event) {
    event.stopPropagation();
    //console.log(this.searchResultList.nativeElement.children.length);
    if (event.keyCode == 40 || event.keyCode == 38){
      let lis = this.searchResultList.nativeElement.children;<commit_msg>fix(link): work item can not be self linked. (#2604)<commit_after>import {
  Component,
  Input,
  OnInit,
  ViewChild,
  ElementRef
} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Space } from 'ngx-fabric8-wit';
import { WorkItemUI, WorkItem } from './../../models/work-item';
import { WorkItemLinkUI } from './../../models/link';
import { LinkTypeUI } from './../../models/link-type';
import { Observable } from 'rxjs/Observable';

import { WorkItemService } from '../../services/work-item.service';

//ngrx stuff
import { AppState } from './../../states/app.state';
import { Store } from '@ngrx/store';
import * as LinkTypeActions from './../../actions/link-type.actions';
import * as WorkItemLinkActions from './../../actions/work-item-link.actions';

@Component({
  selector: 'work-item-link',
  templateUrl: './work-item-link.component.html',
  styleUrls: ['./work-item-link.component.less']
})

export class WorkItemLinkComponent implements OnInit {
  @Input() loggedIn: Boolean;
  @Input() detailContext: string; // It should be detail or preview
  @ViewChild('searchResultList') searchResultList: any;
  @ViewChild('linkTypeSelector') linkTypeSelector: ElementRef;
  @ViewChild('wiSearchBox') wiSearchBox: ElementRef;

  @Input('workItem') set workItemSetter(workItem: WorkItemUI) {
    this.workItem = workItem;
    this.store.dispatch(
      new WorkItemLinkActions.Get(this.workItem.link+'/relationships/links')
    );
    this.setSearchNotAllowedIds();
  }

  private workItem: WorkItemUI = null;
  private eventListeners: any[] = [];
  private linkTypes: LinkTypeUI[] = [];
  private workItemLinks: WorkItemLinkUI[] = [];
  private showLinkComponent: Boolean = false;
  private selectedTab: string | null = null;
  private showLinkView: Boolean = false;
  private showLinkCreator: Boolean = true;
  private selectedLinkType: LinkTypeUI = null;
  searchWorkItems: WorkItem[] = [];
  searchNotAllowedIds: string[] = [];
  selectedWorkItemId: string;
  selectedValue: string = '';

  private spaceSource = this.store
    .select('listPage')
    .select('space')
    .filter(s => !!s);
  private linkTypeSource = this.store
    .select('detailPage')
    .select('linkType')
    .filter(lt => !!lt.length);
  private workItemLinkSource = this.store
    .select('detailPage')
    .select('workItemLink');

  constructor(
    private store: Store<AppState>,
    private workItemService: WorkItemService
  ){}

  ngOnInit() {
    this.eventListeners.push(
      this.spaceSource.take(1)
        .subscribe((space: Space) => {
          this.store.dispatch(new LinkTypeActions.Get());
        })
    );

    Observable.combineLatest(
      this.spaceSource,
      this.linkTypeSource
    ).take(1).subscribe(([
      spaceSource,
      linkTypeSource
    ]) => {
      this.linkTypes = [...linkTypeSource];
      this.workItemLinkSource.subscribe(workItemLinks => {
        this.workItemLinks = [...workItemLinks];
        if (this.linkTypeSelector && this.wiSearchBox) {
          this.wiSearchBox.nativeElement.value = '';
        }
      });
    })
  }

  toggleLinkComponent(onlyOpen: Boolean = false) {
    if (this.loggedIn) {
      if (onlyOpen) {
        this.showLinkComponent = true;
      } else {
        this.showLinkComponent = !this.showLinkComponent;
      }
    }
    if (!this.showLinkComponent) {
      this.selectedTab = null;
    } else {
      if (!this.selectedTab) {
        this.selectedTab = 'all';
      }
    }
  }

  selectTab(linkTypeName: string | null = null) {
    this.selectedTab = linkTypeName;
    this.toggleLinkComponent(true);
  }

  toggleLinkView() {
    this.showLinkView = !this.showLinkView;
  }

  toggleLinkCreator() {
    this.showLinkCreator = !this.showLinkCreator;
  }

  onSelectRelation(relation: any): void{
    //clear the search box and reset values related to search
    this.wiSearchBox.nativeElement.value = '';
    this.searchWorkItems = [];
    this.selectedWorkItemId = null;
    this.selectedLinkType = relation;
  }

  setSearchNotAllowedIds() {
    this.searchNotAllowedIds.push(this.workItem.id);
  }

  selectSearchResult(id: string, number: number, title: string){
    this.selectedWorkItemId = id;
    this.selectedValue = number + ' - ' + title;
    this.searchWorkItems = [];
  }

  searchWorkItem(term, event) {
    event.stopPropagation();
    //console.log(this.searchResultList.nativeElement.children.length);
    if (event.keyCode == 40 || event.keyCode == 38){
      let lis = this.searchResultList.nativeElement.children;
{'fraction_non_alphanumeric': 0.14194559498488749, 'fraction_numerical': 0.0020150005597223777, 'mean_word_length': 3.2972582972582973, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3020279', 'n_tokens_mistral': 2738, 'n_tokens_neox': 2712, 'n_words': 544}