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 */ 016 package com.github.gwtbootstrap.client.ui.resources; 017 018 import com.github.gwtbootstrap.client.ui.config.Configurator; 019 import com.google.gwt.core.client.GWT; 020 import com.google.gwt.dom.client.Document; 021 import com.google.gwt.dom.client.Element; 022 import com.google.gwt.dom.client.HeadElement; 023 import com.google.gwt.dom.client.LinkElement; 024 import com.google.gwt.dom.client.StyleInjector; 025 import com.google.gwt.resources.client.TextResource; 026 027 /** 028 * Utility class to inject our resources into modules page. Use it to inject 029 * JavaScript and CSS files. 030 * 031 * @since 2.0.4.0 032 * 033 * @author Carlos Alexandro Becker 034 */ 035 public class ResourceInjector { 036 037 private static final Configurator ADAPTER = GWT.create(Configurator.class); 038 039 private static HeadElement head; 040 041 /** 042 * Injects the required CSS styles and JavaScript files into the document header. 043 * <pre> 044 * It's for NoStyle Module. 045 * </pre> 046 */ 047 public static void configureWithCssFile() { 048 049 injectResourceCssAsFile("bootstrap.min.css"); 050 injectResourceCssAsFile("gwt-bootstrap.css"); 051 injectResourceCssAsFile("font-awesome.css"); 052 053 configure(); 054 055 } 056 057 /** 058 * Injects the required CSS styles and JavaScript files into the document 059 * header. 060 */ 061 public static void configure() { 062 063 Resources res = ADAPTER.getResources(); 064 if (ADAPTER.hasResponsiveDesign()) 065 activateResponsiveDesign(res); 066 067 if(isNotLoadedJquery()) 068 injectJs(res.jquery()); 069 070 injectJs(res.bootstrapJs()); 071 } 072 073 private native static boolean isNotLoadedJquery() /*-{ 074 return !$wnd['jQuery'] || (typeof $wnd['jQuery'] !== 'function'); 075 }-*/; 076 077 private static void injectCss(TextResource r) { 078 StyleInjector.inject(r.getText()); 079 } 080 081 /** 082 * Inject public resource css file as a file. 083 * @param filename inject file name 084 */ 085 public static void injectResourceCssAsFile(String filename) { 086 LinkElement link = Document.get().createLinkElement(); 087 link.setType("text/css"); 088 link.setRel("stylesheet"); 089 link.setHref(GWT.getModuleName() + "/css/" + filename); 090 getHead().appendChild(link); 091 } 092 093 private static HeadElement getHead() { 094 if (head == null) { 095 Element elt = Document.get().getElementsByTagName("head").getItem(0); 096 assert elt != null : "The host HTML page does not have a <head> element" 097 + " which is required by StyleInjector"; 098 head = HeadElement.as(elt); 099 } 100 return head; 101 } 102 103 private static void injectJs(TextResource r) { 104 JavaScriptInjector.inject(r.getText()); 105 } 106 107 private static void activateResponsiveDesign(Resources res) { 108 injectCss(res.bootstrapResponsiveCss()); 109 MetaInjector 110 .inject("viewport", "width=device-width, initial-scale=1.0"); 111 } 112 113 }