<commit_before>import {Component, Input, OnChanges} from '@angular/core';
import {IdaiFieldDocument} from 'idai-components-2/idai-field-model';
import {IdaiType, ProjectConfiguration} from 'idai-components-2/configuration';
import {ResourcesComponent} from '../resources.component';
import {Node} from './node';
import {Loading} from '../../../widgets/loading';
import {ViewFacade} from '../view/view-facade';
import {IdaiFieldDocumentDatastore} from '../../../core/datastore/idai-field-document-datastore';
import {TreeBuilder} from './tree-builder';
import {FoldState} from './fold-state';

@Component({
    selector: 'list',
    moduleId: module.id,
    templateUrl: './list.html'
})
/**
 * A hierarchical view of resources
 *
 * @author <NAME>.
 * @author <NAME>
 */
export class ListComponent implements OnChanges {

    @Input() ready: boolean;
    @Input() documents: IdaiFieldDocument[];

    private docs: IdaiFieldDocument[] = [];

    public typesMap: { [type: string]: IdaiType };

    public treeBuilder: TreeBuilder;
    public tree: Node[] = [];

    constructor(
        private datastore: IdaiFieldDocumentDatastore,
        public resourcesComponent: ResourcesComponent,
        private loading: Loading,
        projectConfiguration: ProjectConfiguration,
        public viewFacade: ViewFacade,
        private foldState: FoldState
    ) {
        this.typesMap = projectConfiguration.getTypesMap();
        this.treeBuilder = new TreeBuilder(datastore, foldState);
    }


    ngOnChanges() {

        if (!this.ready) return;
        this.loading.start();

        // The timeout is necessary to make the loading icon appear
        setTimeout(async () => {
            if (this.viewFacade.getDocuments()) {
                if (!this.resourcesComponent.getIsRecordedInTarget()) return Promise.resolve();
                this.docs = this.viewFacade.getDocuments() as IdaiFieldDocument[];
                this.tree = await this.treeBuilder.from(this.viewFacade.getDocuments() as IdaiFieldDocument[]);
            }
            this.loading.stop();
        }, 1);
    }


    public documentsInclude(doc: IdaiFieldDocument): boolean {

        return this.docs.some(d => d.resource.id == doc.resource.id );
    }


    public showPlusButton() {

        if (this.resourcesComponent.ready && !this.loading.showIcons && this.viewFacade.getQuery().q == '') {
            if (this.viewFacade.isInOverview()) return true;
            if (this.viewFacade.getSelectedMainTypeDocument()) return true;
        }

        return false;
    }


    public async createNewDocument(newDoc: IdaiFieldDocument) {

        const docs: Array<IdaiFieldDocument> = this.viewFacade.getDocuments() as IdaiFieldDocument[];
        
        for (let doc of docs) {
            if (!doc.resource.id) {
                docs.splice(docs.indexOf(doc),1);
                break;
            }
        }
        docs.push(newDoc);

        // if newDoc as parent, ensure that it's children are shown 
        if (newDoc.resource.relations['liesWithin']) {
            const parentDocId = newDoc.resource.relations['liesWithin'][0];
            if (parentDocId && this.foldState.childrenShownForIds.indexOf(parentDocId) == -1) {
                this.foldState.childrenShownForIds.push(parentDocId);
            }
        }

        this.docs = docs;
        this.tree = await this.treeBuilder.from(docs);
    }
}<commit_msg>clear fold state in list component on search<commit_after>import {Component, Input, OnChanges} from '@angular/core';
import {IdaiFieldDocument} from 'idai-components-2/idai-field-model';
import {IdaiType, ProjectConfiguration} from 'idai-components-2/configuration';
import {ResourcesComponent} from '../resources.component';
import {Node} from './node';
import {Loading} from '../../../widgets/loading';
import {ViewFacade} from '../view/view-facade';
import {IdaiFieldDocumentDatastore} from '../../../core/datastore/idai-field-document-datastore';
import {TreeBuilder} from './tree-builder';
import {FoldState} from './fold-state';

@Component({
    selector: 'list',
    moduleId: module.id,
    templateUrl: './list.html'
})
/**
 * A hierarchical view of resources
 *
 * @author <NAME>.
 * @author <NAME>
 */
export class ListComponent implements OnChanges {

    @Input() ready: boolean;
    @Input() documents: IdaiFieldDocument[];

    public typesMap: { [type: string]: IdaiType };

    public treeBuilder: TreeBuilder;
    public tree: Node[] = [];

    private docs: IdaiFieldDocument[] = [];

    private newResourceCreated: boolean = false;


    constructor(
        private datastore: IdaiFieldDocumentDatastore,
        public resourcesComponent: ResourcesComponent,
        private loading: Loading,
        projectConfiguration: ProjectConfiguration,
        public viewFacade: ViewFacade,
        private foldState: FoldState
    ) {
        this.typesMap = projectConfiguration.getTypesMap();
        this.treeBuilder = new TreeBuilder(datastore, foldState);
    }


    ngOnChanges() {

        if (!this.ready) return;
        this.loading.start();

        // The timeout is necessary to make the loading icon appear
        setTimeout(async () => {
            this.clearFoldStateIfNecessary();

            if (this.viewFacade.getDocuments()) {
                if (!this.resourcesComponent.getIsRecordedInTarget()) return Promise.resolve();
                this.docs = this.viewFacade.getDocuments() as IdaiFieldDocument[];
                this.tree = await this.treeBuilder.from(this.viewFacade.getDocuments() as IdaiFieldDocument[]);
            }
            this.loading.stop();
        }, 1);
    }


    public documentsInclude(doc: IdaiFieldDocument): boolean {

        return this.docs.some(d => d.resource.id == doc.resource.id );
    }


    public showPlusButton() {

        if (this.resourcesComponent.ready && !this.loading.showIcons && this.viewFacade.getQuery().q == '') {
            if (this.viewFacade.isInOverview()) return true;
            if (this.viewFacade.getSelectedMainTypeDocument()) return true;
        }

        return false;
    }


    public async createNewDocument(newDoc: IdaiFieldDocument) {

        const docs: Array<IdaiFieldDocument> = this.viewFacade.getDocuments() as IdaiFieldDocument[];
        
        for (let doc of docs) {
            if (!doc.resource.id) {
                docs.splice(docs.indexOf(doc),1);
                break;
            }
        }
        docs.push(newDoc);

        // if newDoc as parent, ensure that it's children are shown 
        if (newDoc.resource.relations['liesWithin']) {
            const parentDocId = newDoc.resource.relations['liesWithin'][0];
            if (parentDocId && this.foldState.childrenShownForIds.indexOf(parentDocId) == -1) {
                this.foldState.childrenShownForIds.push(parentDocId);
            }
        }

        this.docs = docs;
        this.newResourceCreated = true;
        this.tree = await this.treeBuilder.from(docs);
    }


    private clearFoldStateIfNecessary() {

        if (!this.newResourceCreated) {
            this.foldState.clear();
        } else {
            this.newResourceCreated = false;
        }
    }
}
{'fraction_non_alphanumeric': 0.1243047830923248, 'fraction_numerical': 0.0016685205784204673, 'mean_word_length': 2.6182092555331993, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '245944', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1986, 'n_words': 442}