NIHVIVO-1384 Change script and stylesheet template objects to take the entire tag as a parameter, rather than just the src/href attribute.

This commit is contained in:
rjy7 2011-03-08 15:44:05 +00:00
parent b33b6bc21e
commit 8b89cad590
32 changed files with 108 additions and 213 deletions

View file

@ -36,9 +36,8 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.Tem
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
import edu.cornell.mannlib.vitro.webapp.web.BreadCrumbsUtil;
import edu.cornell.mannlib.vitro.webapp.web.PortalWebUtil;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.Tags;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.User;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.files.Scripts;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.files.Stylesheets;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MainMenu;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.TabMenu;
import freemarker.ext.beans.BeansWrapper;
@ -290,35 +289,21 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
return wrapper;
}
private TemplateModel getStylesheetList(String themeDir) {
private TemplateModel getTagList() {
// For script and stylesheet lists, use an object wrapper that exposes write methods,
// instead of the configuration's object wrapper, which doesn't. The templates can
// add stylesheets and scripts to the lists by calling their add() methods.
BeansWrapper wrapper = getNonDefaultBeansWrapper(BeansWrapper.EXPOSE_SAFE);
try {
// Here themeDir SHOULD NOT have the context path already added to it.
return wrapper.wrap(new Stylesheets(themeDir));
return wrapper.wrap(new Tags());
} catch (TemplateModelException e) {
log.error("Error creating stylesheet TemplateModel");
log.error("Error creating Tags template model");
return null;
}
}
private TemplateModel getScriptList(String themeDir) {
// For script and stylesheet lists, use an object wrapper that exposes write methods,
// instead of the configuration's object wrapper, which doesn't. The templates can
// add stylesheets and scripts to the lists by calling their add() methods.
BeansWrapper wrapper = getNonDefaultBeansWrapper(BeansWrapper.EXPOSE_SAFE);
try {
return wrapper.wrap(new Scripts(themeDir));
} catch (TemplateModelException e) {
log.error("Error creating script TemplateModel");
return null;
}
}
/**
* Add any Java directives the templates should have access to.
* This is public and static so that these may be used by other classes during
@ -368,9 +353,9 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
map.put("themeDir", themeDir);
map.put("currentTheme", themeDir.substring(themeDir.lastIndexOf('/')+1));
map.put("stylesheets", getStylesheetList(themeDir));
map.put("scripts", getScriptList(themeDir));
map.put("headScripts", getScriptList(themeDir));
map.put("stylesheets", getTagList());
map.put("scripts", getTagList());
map.put("headScripts", getTagList());
map.putAll(getDirectives());
map.putAll(getMethods());

View file

@ -0,0 +1,38 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels;
import java.util.LinkedHashSet;
public class Tags extends BaseTemplateModel {
protected LinkedHashSet<String> tags = null;
public Tags() {
this.tags = new LinkedHashSet<String>();
}
public Tags(LinkedHashSet<String> tags) {
this.tags = tags;
}
public void add(String... tags) {
for (String tag : tags) {
add(tag);
}
}
public void add(String tag) {
tags.add(tag);
}
public String getList() {
String tagList = "";
for (String tag : tags) {
tagList += tag;
}
return tagList;
}
}

View file

@ -1,84 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.files;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
public abstract class Files extends BaseTemplateModel {
protected LinkedHashSet<String> list = null;
private String themeDir = null;
@SuppressWarnings("serial")
private static final Set<String> allowedExternalUrlPatterns = new HashSet<String>() {{
add("http://");
add("https://");
}};
public Files() {
this.list = new LinkedHashSet<String>();
}
public Files(String themeDir) {
this();
this.themeDir = themeDir;
}
public Files(LinkedHashSet<String> list) {
this.list = list;
}
public void add(String path) {
// Allow for an external url
for (String currentPattern : allowedExternalUrlPatterns) {
if (path.startsWith(currentPattern)) {
list.add(path);
return;
}
}
// If an external url pattern was not found.
list.add(getUrl(path));
}
public void add(String... paths) {
for (String path : paths) {
add(path);
}
}
public void addFromTheme(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
path = themeDir + path;
add(path);
}
public void addFromTheme(String... paths) {
for (String path : paths) {
addFromTheme(path);
}
}
public String getTags() {
String tags = "";
for (String file : list) {
tags += getTag(file);
}
return tags;
}
public String dump() {
return list.toString();
}
protected abstract String getTag(String url);
}

View file

@ -1,16 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.files;
public class Scripts extends Files {
public Scripts() { }
public Scripts(String themeDir) {
super(themeDir);
}
protected String getTag(String url) {
return "<script type=\"text/javascript\" src=\"" + url + "\"></script>\n";
}
}

View file

@ -1,17 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.files;
public class Stylesheets extends Files {
public Stylesheets() { }
public Stylesheets(String themeDir) {
super(themeDir);
}
protected String getTag(String url) {
return "<link rel=\"stylesheet\" href=\"" + url + "\" />\n";
}
}

View file

@ -41,9 +41,12 @@ public abstract class Widget {
Map<String, Object> map = new HashMap<String, Object>();
try {
// Once we remove portals, we can define these as Configuration shared variables. Then the
// templates will automatically get these and we don't have to add them to the data model.
map.put("stylesheets", dataModel.get("stylesheets"));
map.put("scripts", dataModel.get("scripts"));
map.put("headScripts", dataModel.get("headScripts"));
map.put("urls", dataModel.get("urls"));
} catch (TemplateModelException e) {
log.error("Error getting asset values from data model.");
}

View file

@ -54,4 +54,4 @@
</div>
${scripts.add("/js/commentForm.js")}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/commentForm.js"></script>')}

View file

@ -2,14 +2,12 @@
<#-- Crop the replacement main image for an Individual, to produce a thumbnail. -->
${scripts.add("/js/jquery.js")}
${scripts.add("/js/jquery_plugins/jcrop/jquery.Jcrop.js")}
${scripts.add("/js/imageUpload/cropImage.js")}
${stylesheets.add("/css/uploadImages.css")}
${stylesheets.add("/js/jquery_plugins/jcrop/jquery.Jcrop.css")}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/jquery.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/jquery_plugins/jcrop/jquery.Jcrop.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/imageUpload/cropImage.js"></script>')}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/uploadImages.css" />',
'<link rel="stylesheet" href="${urls.base}/js/jquery_plugins/jcrop/jquery.Jcrop.css" />')}
<#--Reduce original image to fit in the page layout
If the width of the image is bigger or equal to 500 pixels,

View file

@ -2,10 +2,10 @@
<#-- Upload a replacement main image for an Individual. -->
${scripts.add("/js/jquery.js")}
${scripts.add("/js/imageUpload/imageUploadUtils.js")}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/jquery.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/imageUpload/imageUploadUtils.js"></script>')}
${stylesheets.add("/css/uploadImages.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/uploadImages.css" />')}
<section id="photoUploadContainer" role="region">
<h2>Photo Upload</h2>

View file

@ -2,10 +2,10 @@
<#-- Upload a replacement main image for an Individual. -->
${scripts.add("/js/jquery.js")}
${scripts.add("/js/imageUpload/imageUploadUtils.js")}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/jquery.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/imageUpload/imageUploadUtils.js"></script>')}
${stylesheets.add("/css/uploadImages.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/uploadImages.css" />')}
<section id="photoUpload" role="region">
<h2>Photo Upload</h2>

View file

@ -70,14 +70,13 @@
<#-- Ontology properties -->
<#include "individual-properties.ftl">
${stylesheets.add("/css/individual/individual.css")}
<#-- RY Figure out which of these scripts really need to go into the head, and which are needed at all (e.g., tinyMCE??) -->
${headScripts.add("/js/jquery_plugins/getURLParam.js",
"/js/jquery_plugins/colorAnimations.js",
"/js/jquery_plugins/jquery.form.js",
"/js/tiny_mce/tiny_mce.js",
"/js/controls.js",
"/js/toggle.js")}
${scripts.add("/js/imageUpload/imageUploadUtils.js")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/individual/individual.css" />')}
${headScripts.add('<script type="text/javascript" src="${urls.base}/js/jquery_plugins/getURLParam.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/jquery_plugins/colorAnimations.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/jquery_plugins/jquery.form.js.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/tiny_mce/tiny_mce.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/controls.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/toggle.js"></script>')}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/imageUpload/imageUploadUtils.js"></script>')}

View file

@ -3,7 +3,8 @@
<#-- List individuals in the requested class. -->
<#import "lib-list.ftl" as l>
${stylesheets.add("/css/browseIndex.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/browseIndex.css" />')}
<section class="individualList">
<h2>${title} <span class="rdfLink"><a class="icon-rdf" href="${redirecturl}" title="View the ${title} list in RDF format">RDF</a></span></h2>

View file

@ -9,7 +9,7 @@
<#include "menupage-browse.ftl">
${stylesheets.add("/css/menupage/menupage.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/menupage/menupage.css" />')}
<#include "menupage-scripts.ftl">
<#else>

View file

@ -2,7 +2,7 @@
<#-- Browse class groups on the home page. Could potentially become a widget -->
${stylesheets.add("/css/browseClassGroups.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/browseClassGroups.css" />')}
<#macro allClassGroups classGroups>
<#-- Loop through classGroups first so we can account for situations when all class groups are empty -->
@ -72,8 +72,8 @@ ${stylesheets.add("/css/browseClassGroups.css")}
defaultBrowseClassGroupCount: '${firstPopulatedClassGroup.individualCount!}'
};
</script>
${scripts.add('<script type="text/javascript" src="${urls.base}/js/browseClassGroups.js"></script>')}
${scripts.add("/js/browseClassGroups.js")}
<#else>
<#-- Would be nice to update classgroups-checkForData.ftl with macro so it could be used here as well -->
<#-- <#include "classgroups-checkForData.ftl"> -->
@ -104,5 +104,7 @@ ${stylesheets.add("/css/browseClassGroups.css")}
<#-- Will be populated dynamically via AJAX request -->
</section>
${scripts.add("/js/raphael/raphael.js", "/js/raphael/g.raphael.js", "/js/raphael/g.bar.js")}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/raphael/raphael.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/raphael/g.raphael.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/raphael/g.bar.js"></script>')}
</#macro>

View file

@ -10,4 +10,4 @@
</#list>
</div>
${stylesheets.add("/css/dump.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/dump.css" />')}

View file

@ -9,5 +9,5 @@
</div>
<#if stylesheets??>
${stylesheets.add("/css/dump.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/dump.css" />')}
</#if>

View file

@ -34,5 +34,5 @@
</div>
<#if stylesheets??>
${stylesheets.add("/css/dump.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/dump.css" />')}
</#if>

View file

@ -8,4 +8,4 @@
${help}
</div>
${stylesheets.add("/css/dump.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/dump.css" />')}

View file

@ -25,4 +25,5 @@
</script>
<#-- Script to enable browsing individuals within a class -->
${scripts.add("/js/jquery_plugins/jquery.scrollTo-min.js", "/js/menupage/browseByVClass.js")}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/jquery_plugins/jquery.scrollTo-min.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/menupage/browseByVClass.js"></script>')}

View file

@ -2,7 +2,7 @@
<#-- Template for the Revision Information page. -->
${stylesheets.add("/css/revision.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/revision.css" />')}
<section role="region">
<h2>Revision Information</h2>

View file

@ -129,9 +129,9 @@ ${s2} => ${str.unCamelCase(s2)}<br />
<@dump var="zoo1" />
<@dump var="pojo" />
${stylesheets.addFromTheme("/css/sstest.css", "/css/sstest2.css")}
${scripts.addFromTheme("/js/jstest.js")}
${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/script1.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/script2.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/script3.js"></script>')}
<@dumpAll />

View file

@ -16,4 +16,4 @@
</div>
${stylesheets.add("/css/search.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/search.css" />')}

View file

@ -56,4 +56,4 @@
</div> <!-- end contentsBrowseGroup -->
${stylesheets.add("/css/search.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/search.css" />')}

View file

@ -2,7 +2,7 @@
<#-- Template for the main Site Administration page -->
${stylesheets.add("/css/admin.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/admin.css" />')}
<div class="tab">
<h2>Site Administration</h2>

View file

@ -41,21 +41,3 @@
</body>
</html>
<#--
Three ways to add a stylesheet:
A. In theme directory:
${stylesheets.addFromTheme("/css/sample.css")}
${stylesheets.add(themeDir + "/css/sample.css")}
B. Any location
${stylesheets.add("/edit/forms/css/sample.css)"}
To add a script:
A. In theme directory:
${scripts.addFromTheme("/css/sample.js")}
B. Any location
${scripts("/edit/forms/js/sample.js)"}
-->

View file

@ -10,7 +10,7 @@
<script type="text/javascript" src="${urls.base}/js/html5.js"></script>
<![endif]-->
${headScripts.tags}
${headScripts.list}
<!--[if lt IE 7]>
<script type="text/javascript" src="${urls.base}/js/jquery_plugins/supersleight.js"></script>

View file

@ -2,7 +2,7 @@
<#-- Template for scripts loaded at the end of the body element -->
${scripts.tags}
${scripts.list}
<#include "googleAnalytics.ftl">

View file

@ -1,8 +1,11 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
<#-- Template for loading stylesheets in the head -->
${stylesheets.add("/css/edit.css")} <#--temporary until edit controller can include this when needed -->
<!-- vitro base styles (application-wide) -->
<link rel="stylesheet" href="${urls.base}/css/vitro.css" />
${stylesheets.tags}
${stylesheets.list}
<#--temporary until edit controller can include this when needed -->
<link rel="stylesheet" href="${urls.base}/css/edit.css" />

View file

@ -4,8 +4,8 @@
<#macro assets>
<#--
${stylesheets.add("/css/something.css")}
${scripts.add("/js/somejavascript.js")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/something.css" />')}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/somejavascript.js"></script>')}
-->
</#macro>

View file

@ -5,8 +5,8 @@
<#macro assets>
<#--
Are there stylesheets or scripts needed?
${stylesheets.add("/css/browse.css")}
${scripts.add("/js/browse.js")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/browse.css" />')}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/browse.js"></script>'}
-->
</#macro>

View file

@ -8,9 +8,9 @@
because we don't have the user in the template data model when we generate the assets. This can also be fixed by
NIHVIVO-1357.
<#if ! user.loggedIn> -->
${stylesheets.add("/css/login.css")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/login.css" />')}
<#-- ${scripts.add("")} -->
${headScripts.add("/js/login/loginUtils.js")}
${headScripts.add('<script type="text/javascript" src="${urls.base}/js/login/loginUtils.js"></script>')}
<#-- </#if> -->
</#macro>

View file

@ -3,9 +3,9 @@
<#-- Test widget -->
<#macro assets>
${stylesheets.add("/css/test.css")}
${scripts.add("/js/testscript.js")}
${headScripts.add("/js/testheadscript.js")}
${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/test.css" />')}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/testscript.js"></script>')}
${headScripts.add('<script type="text/javascript" src="${urls.base}/js/testheadscript.js"></script>')}
</#macro>
<#macro loggedIn>