001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.acacia.client.widgets.code;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.util.CmsDomUtil;
032import org.opencms.gwt.client.util.CmsScriptCallbackHelper;
033import org.opencms.gwt.client.util.CmsStylesheetLoader;
034
035import java.util.Arrays;
036import java.util.Random;
037
038/**
039 * Helper class that asynchronously loads all the necessary scripts and stylesheets for the CodeMirror editor widget, and
040 * executes a callback at the end.
041 *
042 * <p>Scripts are loaded (unless they've already been loaded before) with the async=false option, which causes them to be executed in order, and a special dummy
043 * script is loaded at the end to execute the callback.
044 */
045public class CmsCodeMirrorScriptLoader {
046
047    /** Random number generator for generating the random parameters for the dummy script. */
048    private static Random random = new Random();
049
050    /** The base URI.*/
051    private String m_baseUri;
052
053    /**
054     * Creates a new instance.
055     */
056    public CmsCodeMirrorScriptLoader() {
057
058        m_baseUri = CmsCoreProvider.get().getWorkplaceResourcesPrefix() + "/editors/codemirror";
059
060    }
061
062    /**
063     * Asynchronously loads all scripts / stylesheets and calls the given callback at the end.
064     *
065     * @param callback the callback to execute after script loading
066     */
067    public void load(Runnable callback) {
068
069        CmsStylesheetLoader cssLoader = new CmsStylesheetLoader(
070            Arrays.asList(
071                m_baseUri + "/dist/lib/codemirror.css",
072                m_baseUri + "/dist/theme/eclipse.css",
073                m_baseUri + "/dist/addon/dialog/dialog.css",
074                m_baseUri + "/dist/addon/hint/show-hint.css",
075                m_baseUri + "/codemirror-ocms.css"),
076
077            () -> {
078                load("/js/lang-en.js");
079                load("/dist/lib/codemirror.js");
080                load("/dist/addon/dialog/dialog.js");
081                load("/dist/addon/search/searchcursor.js");
082                load("/dist/addon/search/search.js");
083                load("/dist/addon/search/jump-to-line.js");
084                load("/dist/addon/edit/closebrackets.js");
085                load("/dist/addon/edit/closetag.js");
086                load("/dist/addon/edit/matchbrackets.js");
087                load("/dist/addon/edit/trailingspace.js");
088                load("/dist/addon/hint/show-hint.js");
089                load("/dist/addon/hint/html-hint.js");
090                load("/dist/addon/hint/javascript-hint.js");
091                load("/dist/addon/hint/xml-hint.js");
092                load("/dist/addon/fold/foldcode.js");
093                load("/dist/addon/fold/brace-fold.js");
094                load("/dist/addon/fold/xml-fold.js");
095                load("/dist/addon/comment/comment.js");
096                load("/dist/addon/selection/active-line.js");
097                load("/dist/mode/css/css.js");
098                load("/dist/mode/xml/xml.js");
099                load("/dist/mode/clike/clike.js");
100                load("/dist/mode/javascript/javascript.js");
101                load("/dist/mode/htmlmixed/htmlmixed.js");
102                load("/js/htmlembedded_modified.js");
103                CmsScriptCallbackHelper helper = new CmsScriptCallbackHelper() {
104
105                    @Override
106                    public void run() {
107
108                        callback.run();
109                    }
110                };
111                // use a random number to avoid caching and ensure that the onload handler
112                // is always executed
113                CmsDomUtil.ensureJavaScriptIncluded(
114                    m_baseUri + "/dummy.js?r=" + random.nextInt(100000000),
115                    false,
116                    helper.createCallback());
117
118            });
119        cssLoader.loadWithTimeout(10000);
120    }
121
122    /**
123     * Loads the script from the given path (which is treated as relative to the CodeMirror base URI.
124     *
125     * @param path the path to load the script from
126     */
127    public void load(String path) {
128
129        // ensure scripts are executed in insertion order with async=false
130        CmsDomUtil.ensureJavaScriptIncluded(m_baseUri + path, /*async=*/false);
131    }
132
133}