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.utils.StringUtils;
import edu.cornell.mannlib.vitro.webapp.web.BreadCrumbsUtil; import edu.cornell.mannlib.vitro.webapp.web.BreadCrumbsUtil;
import edu.cornell.mannlib.vitro.webapp.web.PortalWebUtil; 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.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.MainMenu;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.TabMenu; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.TabMenu;
import freemarker.ext.beans.BeansWrapper; import freemarker.ext.beans.BeansWrapper;
@ -290,34 +289,20 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
return wrapper; return wrapper;
} }
private TemplateModel getStylesheetList(String themeDir) { private TemplateModel getTagList() {
// For script and stylesheet lists, use an object wrapper that exposes write methods, // 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 // 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. // add stylesheets and scripts to the lists by calling their add() methods.
BeansWrapper wrapper = getNonDefaultBeansWrapper(BeansWrapper.EXPOSE_SAFE); BeansWrapper wrapper = getNonDefaultBeansWrapper(BeansWrapper.EXPOSE_SAFE);
try { try {
// Here themeDir SHOULD NOT have the context path already added to it. return wrapper.wrap(new Tags());
return wrapper.wrap(new Stylesheets(themeDir));
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
log.error("Error creating stylesheet TemplateModel"); log.error("Error creating Tags template model");
return null; 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. * Add any Java directives the templates should have access to.
@ -368,9 +353,9 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
map.put("themeDir", themeDir); map.put("themeDir", themeDir);
map.put("currentTheme", themeDir.substring(themeDir.lastIndexOf('/')+1)); map.put("currentTheme", themeDir.substring(themeDir.lastIndexOf('/')+1));
map.put("stylesheets", getStylesheetList(themeDir)); map.put("stylesheets", getTagList());
map.put("scripts", getScriptList(themeDir)); map.put("scripts", getTagList());
map.put("headScripts", getScriptList(themeDir)); map.put("headScripts", getTagList());
map.putAll(getDirectives()); map.putAll(getDirectives());
map.putAll(getMethods()); 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>(); Map<String, Object> map = new HashMap<String, Object>();
try { 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("stylesheets", dataModel.get("stylesheets"));
map.put("scripts", dataModel.get("scripts")); map.put("scripts", dataModel.get("scripts"));
map.put("headScripts", dataModel.get("headScripts")); map.put("headScripts", dataModel.get("headScripts"));
map.put("urls", dataModel.get("urls"));
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
log.error("Error getting asset values from data model."); log.error("Error getting asset values from data model.");
} }

View file

@ -54,4 +54,4 @@
</div> </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. --> <#-- Crop the replacement main image for an Individual, to produce a thumbnail. -->
${scripts.add("/js/jquery.js")} ${scripts.add('<script type="text/javascript" src="${urls.base}/js/jquery.js"></script>',
${scripts.add("/js/jquery_plugins/jcrop/jquery.Jcrop.js")} '<script type="text/javascript" src="${urls.base}/js/jquery_plugins/jcrop/jquery.Jcrop.js"></script>',
${scripts.add("/js/imageUpload/cropImage.js")} '<script type="text/javascript" src="${urls.base}/js/imageUpload/cropImage.js"></script>')}
${stylesheets.add("/css/uploadImages.css")}
${stylesheets.add("/js/jquery_plugins/jcrop/jquery.Jcrop.css")}
${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 <#--Reduce original image to fit in the page layout
If the width of the image is bigger or equal to 500 pixels, 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. --> <#-- Upload a replacement main image for an Individual. -->
${scripts.add("/js/jquery.js")} ${scripts.add('<script type="text/javascript" src="${urls.base}/js/jquery.js"></script>',
${scripts.add("/js/imageUpload/imageUploadUtils.js")} '<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"> <section id="photoUploadContainer" role="region">
<h2>Photo Upload</h2> <h2>Photo Upload</h2>

View file

@ -2,10 +2,10 @@
<#-- Upload a replacement main image for an Individual. --> <#-- Upload a replacement main image for an Individual. -->
${scripts.add("/js/jquery.js")} ${scripts.add('<script type="text/javascript" src="${urls.base}/js/jquery.js"></script>',
${scripts.add("/js/imageUpload/imageUploadUtils.js")} '<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"> <section id="photoUpload" role="region">
<h2>Photo Upload</h2> <h2>Photo Upload</h2>

View file

@ -70,14 +70,13 @@
<#-- Ontology properties --> <#-- Ontology properties -->
<#include "individual-properties.ftl"> <#include "individual-properties.ftl">
${stylesheets.add("/css/individual/individual.css")} ${stylesheets.add('<link rel="stylesheet" href="${urls.base}/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('<script type="text/javascript" src="${urls.base}/js/jquery_plugins/getURLParam.js"></script>',
${headScripts.add("/js/jquery_plugins/getURLParam.js", '<script type="text/javascript" src="${urls.base}/js/jquery_plugins/colorAnimations.js"></script>',
"/js/jquery_plugins/colorAnimations.js", '<script type="text/javascript" src="${urls.base}/js/jquery_plugins/jquery.form.js.js"></script>',
"/js/jquery_plugins/jquery.form.js", '<script type="text/javascript" src="${urls.base}/js/tiny_mce/tiny_mce.js"></script>',
"/js/tiny_mce/tiny_mce.js", '<script type="text/javascript" src="${urls.base}/js/controls.js"></script>',
"/js/controls.js", '<script type="text/javascript" src="${urls.base}/js/toggle.js"></script>')}
"/js/toggle.js")}
${scripts.add("/js/imageUpload/imageUploadUtils.js")} ${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. --> <#-- List individuals in the requested class. -->
<#import "lib-list.ftl" as l> <#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"> <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> <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"> <#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"> <#include "menupage-scripts.ftl">
<#else> <#else>

View file

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

View file

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

View file

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

View file

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

View file

@ -8,4 +8,4 @@
${help} ${help}
</div> </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>
<#-- Script to enable browsing individuals within a class --> <#-- 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. --> <#-- 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"> <section role="region">
<h2>Revision Information</h2> <h2>Revision Information</h2>

View file

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

View file

@ -16,4 +16,4 @@
</div> </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 --> </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 --> <#-- 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"> <div class="tab">
<h2>Site Administration</h2> <h2>Site Administration</h2>

View file

@ -41,21 +41,3 @@
</body> </body>
</html> </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> <script type="text/javascript" src="${urls.base}/js/html5.js"></script>
<![endif]--> <![endif]-->
${headScripts.tags} ${headScripts.list}
<!--[if lt IE 7]> <!--[if lt IE 7]>
<script type="text/javascript" src="${urls.base}/js/jquery_plugins/supersleight.js"></script> <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 --> <#-- Template for scripts loaded at the end of the body element -->
${scripts.tags} ${scripts.list}
<#include "googleAnalytics.ftl"> <#include "googleAnalytics.ftl">

View file

@ -1,8 +1,11 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> <#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
<#-- Template for loading stylesheets in the head --> <#-- 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) --> <!-- vitro base styles (application-wide) -->
<link rel="stylesheet" href="${urls.base}/css/vitro.css" /> <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> <#macro assets>
<#-- <#--
${stylesheets.add("/css/something.css")} ${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/something.css" />')}
${scripts.add("/js/somejavascript.js")} ${scripts.add('<script type="text/javascript" src="${urls.base}/js/somejavascript.js"></script>')}
--> -->
</#macro> </#macro>

View file

@ -5,8 +5,8 @@
<#macro assets> <#macro assets>
<#-- <#--
Are there stylesheets or scripts needed? Are there stylesheets or scripts needed?
${stylesheets.add("/css/browse.css")} ${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/browse.css" />')}
${scripts.add("/js/browse.js")} ${scripts.add('<script type="text/javascript" src="${urls.base}/js/browse.js"></script>'}
--> -->
</#macro> </#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 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. NIHVIVO-1357.
<#if ! user.loggedIn> --> <#if ! user.loggedIn> -->
${stylesheets.add("/css/login.css")} ${stylesheets.add('<link rel="stylesheet" href="${urls.base}/css/login.css" />')}
<#-- ${scripts.add("")} --> <#-- ${scripts.add("")} -->
${headScripts.add("/js/login/loginUtils.js")} ${headScripts.add('<script type="text/javascript" src="${urls.base}/js/login/loginUtils.js"></script>')}
<#-- </#if> --> <#-- </#if> -->
</#macro> </#macro>

View file

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