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.min.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 INJECTOR.preConfigure(); 066 067 Resources res = ADAPTER.getResources(); 068 if(isNotLoadedJquery()) 069 injectJs(res.jquery()); 070 071 injectJs(res.bootstrapJs()); 072 073 if (ADAPTER.hasResponsiveDesign()) 074 activateResponsiveDesign(res); 075 076 INJECTOR.configure(); 077 } 078 079 private native static boolean isNotLoadedJquery() /*-{ 080 return !$wnd['jQuery'] || (typeof $wnd['jQuery'] !== 'function'); 081 }-*/; 082 083 private static void injectCss(TextResource r) { 084 StyleInjector.inject(r.getText()); 085 } 086 087 /** 088 * Inject public resource css file as a file. 089 * @param filename inject file name 090 */ 091 public static void injectResourceCssAsFile(String filename) { 092 LinkElement link = Document.get().createLinkElement(); 093 link.setType("text/css"); 094 link.setRel("stylesheet"); 095 link.setHref(GWT.getModuleBaseURL() + "css/" + filename); 096 getHead().appendChild(link); 097 } 098 099 private static HeadElement getHead() { 100 if (head == null) { 101 Element elt = Document.get().getElementsByTagName("head").getItem(0); 102 assert elt != null : "The host HTML page does not have a <head> element" 103 + " which is required by StyleInjector"; 104 head = HeadElement.as(elt); 105 } 106 return head; 107 } 108 109 private static void injectJs(TextResource r) { 110 JavaScriptInjector.inject(r.getText()); 111 } 112 113 private static void activateResponsiveDesign(Resources res) { 114 injectCss(res.bootstrapResponsiveCss()); 115 MetaInjector 116 .inject("viewport", "width=device-width, initial-scale=1.0"); 117 } 118 119}