001/* 002 * Copyright 2012 GWT-Bootstrap 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.github.gwtbootstrap.client.ui.resources; 017 018import com.github.gwtbootstrap.client.ui.config.Configurator; 019import com.github.gwtbootstrap.client.ui.resources.internal.InternalResourceInjector; 020import com.google.gwt.core.client.GWT; 021import com.google.gwt.dom.client.Document; 022import com.google.gwt.dom.client.Element; 023import com.google.gwt.dom.client.HeadElement; 024import com.google.gwt.dom.client.LinkElement; 025import com.google.gwt.dom.client.StyleInjector; 026import com.google.gwt.resources.client.TextResource; 027 028/** 029 * Utility class to inject our resources into modules page. Use it to inject 030 * JavaScript and CSS files. 031 * 032 * @since 2.0.4.0 033 * 034 * @author Carlos Alexandro Becker 035 */ 036public class ResourceInjector { 037 038 private static final Configurator ADAPTER = GWT.create(Configurator.class); 039 040 private static final InternalResourceInjector INJECTOR = GWT.create(InternalResourceInjector.class); 041 042 private static HeadElement head; 043 044 /** 045 * Injects the required CSS styles and JavaScript files into the document header. 046 * <pre> 047 * It's for NoStyle Module. 048 * </pre> 049 */ 050 public static void configureWithCssFile() { 051 052 injectResourceCssAsFile("bootstrap.min.css"); 053 injectResourceCssAsFile("gwt-bootstrap.css"); 054 injectResourceCssAsFile("font-awesome.css"); 055 056 configure(); 057 058 } 059 060 /** 061 * Injects the required CSS styles and JavaScript files into the document 062 * header. 063 */ 064 public static void configure() { 065 066 Resources res = ADAPTER.getResources(); 067 if (ADAPTER.hasResponsiveDesign()) 068 activateResponsiveDesign(res); 069 070 if(isNotLoadedJquery()) 071 injectJs(res.jquery()); 072 073 injectJs(res.bootstrapJs()); 074 075 INJECTOR.configure(); 076 } 077 078 private native static boolean isNotLoadedJquery() /*-{ 079 return !$wnd['jQuery'] || (typeof $wnd['jQuery'] !== 'function'); 080 }-*/; 081 082 private static void injectCss(TextResource r) { 083 StyleInjector.inject(r.getText()); 084 } 085 086 /** 087 * Inject public resource css file as a file. 088 * @param filename inject file name 089 */ 090 public static void injectResourceCssAsFile(String filename) { 091 LinkElement link = Document.get().createLinkElement(); 092 link.setType("text/css"); 093 link.setRel("stylesheet"); 094 link.setHref(GWT.getModuleName() + "/css/" + filename); 095 getHead().appendChild(link); 096 } 097 098 private static HeadElement getHead() { 099 if (head == null) { 100 Element elt = Document.get().getElementsByTagName("head").getItem(0); 101 assert elt != null : "The host HTML page does not have a <head> element" 102 + " which is required by StyleInjector"; 103 head = HeadElement.as(elt); 104 } 105 return head; 106 } 107 108 private static void injectJs(TextResource r) { 109 JavaScriptInjector.inject(r.getText()); 110 } 111 112 private static void activateResponsiveDesign(Resources res) { 113 injectCss(res.bootstrapResponsiveCss()); 114 MetaInjector 115 .inject("viewport", "width=device-width, initial-scale=1.0"); 116 } 117 118}