001package gwt.material.design.client; 002 003/* 004 * #%L 005 * GwtMaterialDesign 006 * %% 007 * Copyright (C) 2015 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.ScriptInjector; 024import com.google.gwt.dom.client.StyleInjector; 025import com.google.gwt.resources.client.TextResource; 026import gwt.material.design.client.resources.MaterialResources; 027 028import java.util.ArrayList; 029import java.util.List; 030 031public class MaterialDesignBase { 032 033 static class FutureResource { 034 TextResource resource; 035 boolean removeTag, sourceUrl; 036 037 public FutureResource(TextResource resource, boolean removeTag, boolean sourceUrl) { 038 this.resource = resource; 039 this.removeTag = removeTag; 040 this.sourceUrl = sourceUrl; 041 } 042 } 043 044 static TextResource jqueryResource; 045 static List<FutureResource> futureResources; 046 047 protected void load() { 048 checkJQuery(false); 049 injectJs(MaterialResources.INSTANCE.materializeJs()); 050 injectJs(MaterialResources.INSTANCE.animationJs()); 051 injectJs(MaterialResources.INSTANCE.shrinkJs()); 052 onModuleLoaded(); 053 } 054 055 protected void onModuleLoaded() { 056 if(futureResources != null) { 057 for (FutureResource res : futureResources) { 058 injectJs(res.resource, res.removeTag, res.sourceUrl); 059 } 060 } 061 } 062 063 public static void injectJs(TextResource resource) { 064 injectJs(resource, true, false); 065 } 066 067 public static void injectDebugJs(TextResource resource) { 068 injectJs(resource, false, true); 069 } 070 071 public static void injectJs(TextResource resource, boolean removeTag, boolean sourceUrl) { 072 if(!resource.getName().contains("jQuery")) { 073 if(!checkJQuery(sourceUrl)) { 074 // We need to wait for jQuery to load 075 if(futureResources == null) { 076 futureResources = new ArrayList<>(); 077 } 078 futureResources.add(new FutureResource(resource, removeTag, sourceUrl)); 079 } 080 } 081 String text = resource.getText() + 082 (sourceUrl ? "//# sourceURL=" + resource.getName() + ".js" : ""); 083 084 // Inject the script resource 085 ScriptInjector.fromString(text) 086 .setWindow(ScriptInjector.TOP_WINDOW) 087 .setRemoveTag(removeTag) 088 .inject(); 089 } 090 091 public static void injectCss(TextResource resource) { 092 StyleInjector.inject(resource.getText()); 093 } 094 095 protected static boolean checkJQuery(boolean debug) { 096 if(!isjQueryLoaded()) { 097 if(jqueryResource != null) { 098 if(debug) { 099 injectDebugJs(jqueryResource); 100 } else { 101 injectJs(jqueryResource); 102 } 103 } else { 104 return false; 105 } 106 } 107 return true; 108 } 109 110 /** 111 * Check to see if jQuery is loaded already 112 * 113 * @return true is jQuery is loaded, false otherwise 114 */ 115 public static native boolean isjQueryLoaded() /*-{ 116 return (typeof $wnd['jQuery'] !== 'undefined'); 117 }-*/; 118}