提交 8527286c 编写于 作者: K kohsuke

added ability to inject CSS from ConsoleNotes.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@34212 71c3de6d-444a-0410-be80-ed276b4c234a
上级 6d492940
......@@ -1218,16 +1218,22 @@ public class Functions {
* Generate a series of &lt;script> tags to include <tt>script.js</tt>
* from {@link ConsoleAnnotatorFactory}s and {@link ConsoleAnnotationDescriptor}s.
*/
public static String generateConsoleAnnotationScript() {
public static String generateConsoleAnnotationScriptAndStylesheet() {
String cp = Stapler.getCurrentRequest().getContextPath();
StringBuilder buf = new StringBuilder();
for (ConsoleAnnotatorFactory f : ConsoleAnnotatorFactory.all()) {
String path = cp + "/extensionList/" + ConsoleAnnotatorFactory.class.getName() + "/" + f.getClass().getName();
if (f.hasScript())
buf.append("<script src='"+cp+"/extensionList/"+ConsoleAnnotatorFactory.class.getName()+"/"+f.getClass().getName()+"/script.js'></script>");
buf.append("<script src='"+path+"/script.js'></script>");
if (f.hasStylesheet())
buf.append("<link rel='stylesheet' type='text/css' href='"+path+"/style.css' />");
}
for (ConsoleAnnotationDescriptor d : ConsoleAnnotationDescriptor.all()) {
String path = cp+"/descriptor/"+d.clazz.getName();
if (d.hasScript())
buf.append("<script src='"+cp+"/descriptor/"+d.clazz.getName()+"/script.js'></script>");
buf.append("<script src='"+path+"/script.js'></script>");
if (d.hasStylesheet())
buf.append("<link rel='stylesheet' type='text/css' href='"+path+"/style.css' />");
}
return buf.toString();
}
......
......@@ -61,16 +61,28 @@ public abstract class ConsoleAnnotationDescriptor extends Descriptor<ConsoleNote
* Returns true if this descriptor has a JavaScript to be inserted on applicable console page.
*/
public boolean hasScript() {
return getScriptJs()!=null;
return hasResource("/script.js") !=null;
}
private URL getScriptJs() {
return clazz.getClassLoader().getResource(clazz.getName().replace('.','/').replace('$','/')+"/script.js");
/**
* Returns true if this descriptor has a stylesheet to be inserted on applicable console page.
*/
public boolean hasStylesheet() {
return hasResource("/style.css") !=null;
}
private URL hasResource(String name) {
return clazz.getClassLoader().getResource(clazz.getName().replace('.','/').replace('$','/')+ name);
}
@WebMethod(name="script.js")
public void doScriptJs(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveFile(req, getScriptJs(), TimeUnit2.DAYS.toMillis(1));
rsp.serveFile(req, hasResource("/script.js"), TimeUnit2.DAYS.toMillis(1));
}
@WebMethod(name="style.css")
public void doStyleCss(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveFile(req, hasResource("/style.css"), TimeUnit2.DAYS.toMillis(1));
}
/**
......
......@@ -57,6 +57,13 @@ import java.net.URL;
* <p>
* To register, put @{@link Extension} on your {@link ConsoleAnnotatorFactory} subtype.
*
* <h2>Behaviour, JavaScript, and CSS</h2>
* <p>
* {@link ConsoleNote} can have associated <tt>script.js</tt> and <tt>style.css</tt> (put them
* in the same resource directory that you normally put Jelly scripts), which will be loaded into
* the HTML page whenever the console notes are used. This allows you to use minimal markup in
* code generation, and do the styling in CSS and perform the rest of the interesting work as a CSS behaviour/JavaScript.
*
* @author Kohsuke Kawaguchi
* @since 1.349
*/
......@@ -91,12 +98,16 @@ public abstract class ConsoleAnnotatorFactory<T> implements ExtensionPoint {
* Returns true if this descriptor has a JavaScript to be inserted on applicable console page.
*/
public boolean hasScript() {
return getScriptJs() !=null;
return getResource("/script.js") !=null;
}
public boolean hasStylesheet() {
return getResource("/style.css") !=null;
}
private URL getScriptJs() {
private URL getResource(String fileName) {
Class c = getClass();
return c.getClassLoader().getResource(c.getName().replace('.','/').replace('$','/')+"/script.js");
return c.getClassLoader().getResource(c.getName().replace('.','/').replace('$','/')+ fileName);
}
/**
......@@ -104,7 +115,12 @@ public abstract class ConsoleAnnotatorFactory<T> implements ExtensionPoint {
*/
@WebMethod(name="script.js")
public void doScriptJs(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveFile(req,getScriptJs(), TimeUnit2.DAYS.toMillis(1));
rsp.serveFile(req, getResource("/script.js"), TimeUnit2.DAYS.toMillis(1));
}
@WebMethod(name="style.css")
public void doStyleCss(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveFile(req, getResource("/style.css"), TimeUnit2.DAYS.toMillis(1));
}
/**
......
......@@ -106,6 +106,13 @@ import java.util.zip.GZIPOutputStream;
* is also important, although {@link ConsoleNote}s that failed to deserialize will be simply ignored, so the
* worst thing that can happen is that you just lose some notes.
*
* <h2>Behaviour, JavaScript, and CSS</h2>
* <p>
* {@link ConsoleNote} can have associated <tt>script.js</tt> and <tt>style.css</tt> (put them
* in the same resource directory that you normally put Jelly scripts), which will be loaded into
* the HTML page whenever the console notes are used. This allows you to use minimal markup in
* code generation, and do the styling in CSS and perform the rest of the interesting work as a CSS behaviour/JavaScript.
*
* @param <T>
* Contextual model object that this console is associated with, such as {@link Run}.
*
......
......@@ -49,7 +49,7 @@ THE SOFTWARE.
</j:otherwise>
</j:choose>
<j:out value="${h.generateConsoleAnnotationScript()}"/>
<j:out value="${h.generateConsoleAnnotationScriptAndStylesheet()}"/>
<j:choose>
<!-- Do progressive console output -->
......
.ant-outcome-failure {
font-weight: bold;
color: red;
}
.ant-outcome-success {
color: #204A87;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册