001package gwt.material.design.client;
002
003/*
004 * #%L
005 * GwtMaterialDesign
006 * %%
007 * Copyright (C) 2015 - 2017 GwtMaterial
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import com.google.gwt.core.client.GWT;
024import com.google.gwt.core.client.ScriptInjector;
025import com.google.gwt.dom.client.StyleInjector;
026import com.google.gwt.resources.client.TextResource;
027import gwt.material.design.client.resources.MaterialResources;
028
029import java.util.ArrayList;
030import java.util.List;
031
032public class MaterialDesignBase {
033
034    static class FutureResource {
035        TextResource resource;
036        boolean removeTag, sourceUrl;
037
038        public FutureResource(TextResource resource, boolean removeTag, boolean sourceUrl) {
039            this.resource = resource;
040            this.removeTag = removeTag;
041            this.sourceUrl = sourceUrl;
042        }
043    }
044
045    static final JQueryProvider jQueryProvider = GWT.create(JQueryProvider.class);
046    static List<FutureResource> futureResources;
047    static boolean jqueryWarning;
048
049    protected void load() {
050        checkJQuery(false);
051        if(!isMaterializeLoaded()) {
052            injectJs(MaterialResources.INSTANCE.materializeJs());
053            injectJs(MaterialResources.INSTANCE.animationJs());
054        }
055        onModuleLoaded();
056    }
057
058    protected void onModuleLoaded() {
059        if (futureResources != null) {
060            for (FutureResource res : futureResources) {
061                injectJs(res.resource, res.removeTag, res.sourceUrl);
062            }
063        }
064    }
065
066    public static void injectJs(TextResource resource) {
067        injectJs(resource, true, false);
068    }
069
070    public static void injectDebugJs(TextResource resource) {
071        injectJs(resource, false, true);
072    }
073
074    public static void injectJs(TextResource resource, boolean removeTag, boolean sourceUrl) {
075        if (!resource.getName().equals("jQuery") && !resource.getName().equals("jQueryDebug") && !checkJQuery(sourceUrl)) {
076            // We need to wait for jQuery to load
077            if (futureResources == null) {
078                futureResources = new ArrayList<>();
079            }
080            futureResources.add(new FutureResource(resource, removeTag, sourceUrl));
081        } else {
082            directInjectJs(resource, removeTag, sourceUrl);
083        }
084    }
085
086    protected static void directInjectJs(TextResource resource) {
087        directInjectJs(resource, true, false);
088    }
089
090    protected static void directInjectDebugJs(TextResource resource) {
091        directInjectJs(resource, false, true);
092    }
093
094    protected static void directInjectJs(TextResource resource, boolean removeTag, boolean sourceUrl) {
095        String text = resource.getText() + (sourceUrl ?
096            "//# sourceURL=" + resource.getName() + ".js" : "");
097
098        // Inject the script resource
099        ScriptInjector.fromString(text)
100            .setWindow(ScriptInjector.TOP_WINDOW)
101            .setRemoveTag(removeTag)
102            .inject();
103    }
104
105    public static void injectCss(TextResource resource) {
106        StyleInjector.inject(resource.getText());
107    }
108
109    public static boolean checkJQuery(boolean debug) {
110        if (!isjQueryLoaded()) {
111            if(isProvidingJQuery()) {
112                if (debug) {
113                    directInjectDebugJs(jQueryProvider.jQuery());
114                } else {
115                    directInjectJs(jQueryProvider.jQuery());
116                }
117            } else if(!jqueryWarning) {
118                GWT.log("Warning: GWT Material is not providing JQuery. You must ensure JQuery " +
119                        "is loaded manually or use one of the GwtMaterialWithJQuery modules, failing " +
120                        "to do so will result in an endless resource loop. This message can be ignored " +
121                        "if you are doing so already (message will not appear in production).");
122                jqueryWarning = true;
123            }
124        }
125        return isjQueryLoaded();
126    }
127
128    public static boolean isProvidingJQuery() {
129        return !(jQueryProvider instanceof JQueryProvider.NoJQuery);
130    }
131
132    /**
133     * Check to see if jQuery is loaded already
134     *
135     * @return true is jQuery is loaded, false otherwise
136     */
137    public static native boolean isjQueryLoaded() /*-{
138        return (typeof $wnd['jQuery'] !== 'undefined');
139    }-*/;
140
141    public static native boolean isMaterializeLoaded() /*-{
142        return (typeof $wnd['Materialize'] !== 'undefined')
143    }-*/;
144}