async _changeReference()

in client/client/app/shared/projectContext/index.js [1606:1677]


    async _changeReference(reference, shouldAddAnnotationTracks) {
        await this.refreshReferences();
        if (this._tracks) {
            // mapping reference track
            this._tracks = this._tracks.map(t => {
                if (t.format === 'REFERENCE' && t.isLocal) {
                    const [ref] = this._references.filter(r => r.name.toLowerCase() === t.name.toString().toLowerCase());
                    ref.projectId = t.projectId;
                    ref.isLocal = true;
                    return ref;
                }
                return t;
            });
        }
        if (reference && reference.id === undefined && reference.name !== undefined && reference.name !== null) {
            [reference] = this._references.filter(r => r.name.toLowerCase() === reference.name.toLowerCase());
        } else if (reference && reference.id !== undefined && reference.id !== null) {
            [reference] = this._references.filter(r => r.id === reference.id);
        }
        if (this._tracks && reference) {
            // mapping reference track
            const [systemReference] = this._references.filter(r => r.id === reference.id);
            // mapping annotation tracks
            if (systemReference && systemReference.annotationFiles) {
                for (let i = 0; i < systemReference.annotationFiles.length; i++) {
                    const annotationFile = systemReference.annotationFiles[i];
                    if (!shouldAddAnnotationTracks) {
                        annotationFile.selected = false;
                    }
                    const savedState = this.getTrackState((annotationFile.name || '').toLowerCase(), '');
                    this._tracks = this._tracks.map(t => {
                        if (
                            t.format === annotationFile.format &&
                            t.name.toLowerCase() === annotationFile.name.toLowerCase()
                        ) {
                            annotationFile.projectId = '';
                            annotationFile.isLocal = true;
                            if (
                                !shouldAddAnnotationTracks &&
                                (t.projectId || '').toLowerCase() === ''
                            ) {
                                annotationFile.selected = true;
                            }
                            return Object.assign(
                                {instance: t.instance},
                                {...(savedState || {})},
                                annotationFile,
                                {
                                    duplicateId: t.duplicateId,
                                    projectId: t.projectId,
                                    project: t.project,
                                    projectIdNumber: t.projectIdNumber,
                                    isLocal: t.isLocal
                                }
                            );
                        }
                        return t;
                    });
                }
            }
            this.saveAnnotationFilesState();
        }
        const newReferenceId = reference ? +reference.id : null;
        if (+this.referenceId !== newReferenceId) {
            this._reference = reference;
            this.applyAnnotationTracksState();
            this._referenceIsPromised = false;
            this._chromosomes = await this._loadChromosomesForReference(this.referenceId);
            return true;
        }
        return false;
    }