<commit_before>import {Component, AfterViewChecked} from '@angular/core';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {Location} from '@angular/common';
import {IdaiFieldDocument, IdaiFieldGeometry} from 'idai-components-2/idai-field-model';
import {Query} from 'idai-components-2/datastore';
import {Document, Action} from 'idai-components-2/core';
import {DocumentEditChangeMonitor} from 'idai-components-2/documents';
import {Messages} from 'idai-components-2/messages';
import {ConfigLoader, ViewDefinition} from 'idai-components-2/configuration';
import {IdaiFieldDatastore} from '../datastore/idai-field-datastore';
import {Observable} from 'rxjs/Observable';
import {SettingsService} from '../settings/settings-service';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
import {DoceditComponent} from '../docedit/docedit.component';
import {ViewUtility} from '../util/view-utility';


@Component({
    moduleId: module.id,
    templateUrl: './resources.html'
})

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
export class ResourcesComponent implements AfterViewChecked {

    public view: ViewDefinition;
    public mainTypeLabel: string;
    public mode: string = 'map';
    public editGeometry: boolean = false;
    public query: Query = {q: '', type: 'resource', prefix: true};

    public documents: Array<Document>;
    public selectedDocument: Document;

    public mainTypeDocuments: Array<IdaiFieldDocument>;
    public selectedMainTypeDocument: IdaiFieldDocument;

    public projectDocument: IdaiFieldDocument;

    private ready: boolean = false;
    private newDocumentsFromRemote: Array<Document> = [];
    private scrollTarget: IdaiFieldDocument;
    private observers: Array<any> = [];
    private mainTypeObservers: Array<any> = [];

    private mainTypeHistory = {};

    constructor(private route: ActivatedRoute,
                private router: Router,
                private location: Location,
                private datastore: IdaiFieldDatastore,
                private settingsService: SettingsService,
                private modalService: NgbModal,
                private documentEditChangeMonitor: DocumentEditChangeMonitor,
                private messages: Messages,
                private configLoader: ConfigLoader,
                private viewUtility: ViewUtility
    ) {
        this.route.params.subscribe(params => {
            if (this.selectedMainTypeDocument != undefined && this.view != undefined) {
                this.mainTypeHistory[this.view.name] = this.selectedMainTypeDocument;
            }

            this.selectedDocument = undefined;
            this.selectedMainTypeDocument = undefined;
            this.mainTypeDocuments = undefined;
            this.ready = false;

            this.parseParams(params)
                .then(() => this.initialize())
                .catch(msgWithParams => {
                    if (msgWithParams) this.messages.add(msgWithParams)
                });
        });

        const self = this;
        datastore.documentChangesNotifications().subscribe(result => {
            self.handleChange(result);
        });
    }

    ngAfterViewChecked() {

        if (this.scrollTarget) {
            this.scrollToDocument(this.scrollTarget);
            this.scrollTarget = undefined;
        }
    }

    private parseParams(params: Params): Promise<any> {

        let viewName: string = params['view'];
        let resourceId: string = params['id'];
        let tab: string = params['tab'];

        if (resourceId) this.selectDocumentFromParams(resourceId, tab);

        this.location.replaceState('resources/' + viewName);

        if (!this.view || viewName != this.view.name) {
            return this.initializeView(viewName);
        } else {
            return Promise.resolve();
        }
    }

    public initialize(): Promise<any> {

        return this.fetchProjectDocument()
            .then(() => this.fetchMainTypeDocuments())
            .then(() => this.fetchDocuments())
            .then(() => {
                this.ready = true;
                if (this.view.mainType == 'project') this.setMode('map');
                this.notifyMainTypeObservers();
            });
    }

    private initializeView(viewName: string): Promise<any> {
<commit_msg>don't switch to map when opening project view<commit_after>import {Component, AfterViewChecked} from '@angular/core';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {Location} from '@angular/common';
import {IdaiFieldDocument, IdaiFieldGeometry} from 'idai-components-2/idai-field-model';
import {Query} from 'idai-components-2/datastore';
import {Document, Action} from 'idai-components-2/core';
import {DocumentEditChangeMonitor} from 'idai-components-2/documents';
import {Messages} from 'idai-components-2/messages';
import {ConfigLoader, ViewDefinition} from 'idai-components-2/configuration';
import {IdaiFieldDatastore} from '../datastore/idai-field-datastore';
import {Observable} from 'rxjs/Observable';
import {SettingsService} from '../settings/settings-service';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
import {DoceditComponent} from '../docedit/docedit.component';
import {ViewUtility} from '../util/view-utility';


@Component({
    moduleId: module.id,
    templateUrl: './resources.html'
})

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
export class ResourcesComponent implements AfterViewChecked {

    public view: ViewDefinition;
    public mainTypeLabel: string;
    public mode: string = 'map';
    public editGeometry: boolean = false;
    public query: Query = {q: '', type: 'resource', prefix: true};

    public documents: Array<Document>;
    public selectedDocument: Document;

    public mainTypeDocuments: Array<IdaiFieldDocument>;
    public selectedMainTypeDocument: IdaiFieldDocument;

    public projectDocument: IdaiFieldDocument;

    private ready: boolean = false;
    private newDocumentsFromRemote: Array<Document> = [];
    private scrollTarget: IdaiFieldDocument;
    private observers: Array<any> = [];
    private mainTypeObservers: Array<any> = [];

    private mainTypeHistory = {};

    constructor(private route: ActivatedRoute,
                private router: Router,
                private location: Location,
                private datastore: IdaiFieldDatastore,
                private settingsService: SettingsService,
                private modalService: NgbModal,
                private documentEditChangeMonitor: DocumentEditChangeMonitor,
                private messages: Messages,
                private configLoader: ConfigLoader,
                private viewUtility: ViewUtility
    ) {
        this.route.params.subscribe(params => {
            if (this.selectedMainTypeDocument != undefined && this.view != undefined) {
                this.mainTypeHistory[this.view.name] = this.selectedMainTypeDocument;
            }

            this.selectedDocument = undefined;
            this.selectedMainTypeDocument = undefined;
            this.mainTypeDocuments = undefined;
            this.ready = false;

            this.parseParams(params)
                .then(() => this.initialize())
                .catch(msgWithParams => {
                    if (msgWithParams) this.messages.add(msgWithParams)
                });
        });

        const self = this;
        datastore.documentChangesNotifications().subscribe(result => {
            self.handleChange(result);
        });
    }

    ngAfterViewChecked() {

        if (this.scrollTarget) {
            this.scrollToDocument(this.scrollTarget);
            this.scrollTarget = undefined;
        }
    }

    private parseParams(params: Params): Promise<any> {

        let viewName: string = params['view'];
        let resourceId: string = params['id'];
        let tab: string = params['tab'];

        if (resourceId) this.selectDocumentFromParams(resourceId, tab);

        this.location.replaceState('resources/' + viewName);

        if (!this.view || viewName != this.view.name) {
            return this.initializeView(viewName);
        } else {
            return Promise.resolve();
        }
    }

    public initialize(): Promise<any> {

        return this.fetchProjectDocument()
            .then(() => this.fetchMainTypeDocuments())
            .then(() => this.fetchDocuments())
            .then(() => {
                this.ready = true;
                this.notifyMainTypeObservers();
            });
    }

    private initializeView(viewName: string): Promise<any> {

{'fraction_non_alphanumeric': 0.11514378478664193, 'fraction_numerical': 0.0013914656771799629, 'mean_word_length': 2.7337662337662336, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 41, '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': '111590', 'n_tokens_mistral': 2278, 'n_tokens_neox': 2213, 'n_words': 513}