all files / component/editor/ deja-editor.service.ts

100% Statements 22/22
62.5% Branches 5/8
100% Functions 5/5
100% Lines 19/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                     
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
import { Injectable } from '@angular/core';
 
/**
 * Monaco Editor Service
 *
 * Service used by Monaco Editor Component to load only once instance of the Monaco Editor Library
 */
@Injectable()
export class DejaEditorService {
 
    private _loading: boolean;
    private _loader: Promise<any>;
 
    /**
     * Load the CKEditor Editor Library
     *
     * @return Resolved promise when the library is loaded
     */
    public initDejaEditorLib(): Promise<any> {
        Eif (!this._loading) {
            this.init();
        }
 
        return this._loader;
    }
 
    private init() {
        this._loader = new Promise((resolve) => {
            this._loading = true;
 
            // Load AMD loader if necessary
            Eif (!(<any>window).ckeditor) {
                const baseElement = document.getElementsByTagName('base')[0] || {} as HTMLBaseElement;
                const baseHref = baseElement.href;
                const basePath = (<any>window).CKEDITOR_BASEPATH || `${baseHref}assets/ckeditor/`;
                const loaderScript = document.createElement('script');
                document.head.appendChild(loaderScript);
                loaderScript.type = 'text/javascript';
                loaderScript.src = `${basePath}ckeditor.js`;
                loaderScript.addEventListener('load', resolve);
            }
        });
    }
}