提交 30bb3fde 编写于 作者: K Kohsuke Kawaguchi

adding the server side code to generate context menu

上级 7c58bb48
......@@ -792,7 +792,8 @@ public class Functions {
*/
public static String getIconFilePath(Action a) {
String name = a.getIconFileName();
if(name.startsWith("/"))
if (name==null) return null;
if (name.startsWith("/"))
return name.substring(1);
else
return "images/24x24/"+name;
......
......@@ -23,11 +23,20 @@
*/
package hudson.model;
import hudson.model.queue.Tasks;
import jenkins.model.ModelObjectWithContextMenu;
import org.apache.commons.jelly.Script;
import org.apache.commons.jelly.XMLOutput;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.WebApp;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.jelly.JellyClassTearOff;
import org.kohsuke.stapler.jelly.JellyFacet;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
......@@ -38,7 +47,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
* @author Kohsuke Kawaguchi
*/
@ExportedBean
public abstract class Actionable extends AbstractModelObject {
public abstract class Actionable extends AbstractModelObject implements ModelObjectWithContextMenu {
/**
* Actions contributed to this model object.
*
......@@ -119,4 +128,22 @@ public abstract class Actionable extends AbstractModelObject {
}
return null;
}
public ContextMenu doContextMenu(StaplerRequest request, StaplerResponse response) throws Exception {
ContextMenu c = new ContextMenu();
WebApp webApp = WebApp.getCurrent();
Script s = webApp.getMetaClass(this).getTearOff(JellyClassTearOff.class).findScript("sidepanel");
if (s==null) {
// fallback
c.addAll(getActions());
} else {
JellyFacet facet = webApp.getFacet(JellyFacet.class);
request.setAttribute("taskTags",c);
request.setAttribute("mode","side-panel");
facet.scriptInvoker.invokeScript(request,response,s,this,new XMLOutput(new DefaultHandler()));
}
return c;
}
}
......@@ -293,7 +293,7 @@ import java.util.regex.Pattern;
* @author Kohsuke Kawaguchi
*/
@ExportedBean
public class Jenkins extends AbstractCIBase implements ModifiableItemGroup<TopLevelItem>, StaplerProxy, StaplerFallback, ViewGroup, AccessControlled, DescriptorByNameOwner {
public class Jenkins extends AbstractCIBase implements ModifiableItemGroup<TopLevelItem>, StaplerProxy, StaplerFallback, ViewGroup, AccessControlled, DescriptorByNameOwner, ModelObjectWithContextMenu {
private transient final Queue queue;
/**
......@@ -2894,6 +2894,10 @@ public class Jenkins extends AbstractCIBase implements ModifiableItemGroup<TopLe
rsp.getWriter().println("GCed");
}
public ContextMenu doContextMenu(StaplerRequest request, StaplerResponse response) {
return new ContextMenu().addAll(getActions());
}
/**
* Obtains the heap dump.
*/
......
package jenkins.model;
import hudson.Functions;
import hudson.model.Action;
import hudson.model.ModelObject;
import org.apache.commons.jelly.JellyException;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.export.Flavor;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* {@link ModelObject} that has context menu in the breadcrumb
*
* This is just a marker interface. It needs
* @author Kohsuke Kawaguchi
*/
public interface ModelObjectWithContextMenu extends ModelObject {
public ContextMenu doContextMenu(StaplerRequest request, StaplerResponse response) throws Exception;
@ExportedBean
public class ContextMenu implements HttpResponse {
@Exported(inline=true)
public final List<MenuItem> items = new ArrayList<MenuItem>();
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object o) throws IOException, ServletException {
rsp.serveExposedBean(req,this,Flavor.JSON);
}
public ContextMenu add(String url, String text) {
items.add(new MenuItem(url,text));
return this;
}
public ContextMenu addAll(Collection<? extends Action> actions) {
for (Action a : actions)
add(a);
return this;
}
public ContextMenu add(Action a) {
StaplerRequest req = Stapler.getCurrentRequest();
String text = a.getDisplayName();
String icon = Functions.getIconFilePath(a);
String url = Functions.getActionUrl(req.findAncestor(ModelObject.class).getUrl(),a);
return add(url,icon,text);
}
public ContextMenu add(String url, String icon, String text) {
if (text != null && icon != null && url != null)
items.add(new MenuItem(url,"<img src='"+url+"'> "+text));
return this;
}
}
@ExportedBean
public class MenuItem {
@Exported
public String url;
@Exported
public String text;
public MenuItem(String url, String text) {
this.url = url;
this.text = text;
}
}
}
......@@ -94,8 +94,12 @@ THE SOFTWARE.
</j:when>
<j:otherwise>
<div class="task">
<j:set var="icon" value="${rootURL}${icon.startsWith('images/') ? h.resourcePath : ''}/${icon}"/>
${taskTags!=null ? taskTags.add(href,icon,title) : null}
${taskTags}
<a href="${href}" onclick="${attrs.onclick}">
<img width="24" height="24" style="margin: 2px;" alt="" src="${rootURL}${icon.startsWith('images/') ? h.resourcePath : ''}/${icon}"/>
<img width="24" height="24" style="margin: 2px;" alt="" src="${icon}"/>
</a>
<st:nbsp />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册