azkaban-developers
Changes
src/java/azkaban/flow/Flow.java 8(+8 -0)
src/java/azkaban/flow/Node.java 15(+14 -1)
src/web/css/azkaban.css 72(+71 -1)
src/web/css/jquery.contextMenu.css 62(+62 -0)
src/web/css/jquery.contextMenu.custom.css 64(+64 -0)
src/web/js/azkaban.flow.view.js 30(+29 -1)
src/web/js/azkaban.main.view.js 89(+86 -3)
src/web/js/azkaban.project.view.js 11(+8 -3)
src/web/js/jquery.contextMenu.js 211(+211 -0)
src/web/js/jquery.contextMenu.js~ 1585(+1585 -0)
Details
src/java/azkaban/flow/Flow.java 8(+8 -0)
diff --git a/src/java/azkaban/flow/Flow.java b/src/java/azkaban/flow/Flow.java
index a4ce3ba..86cd159 100644
--- a/src/java/azkaban/flow/Flow.java
+++ b/src/java/azkaban/flow/Flow.java
@@ -73,6 +73,10 @@ public class Flow {
}
}
+ public Node getNode(String nodeId) {
+ return nodes.get(nodeId);
+ }
+
public int getNumLevels() {
return numLevels;
}
@@ -296,4 +300,8 @@ public class Flow {
public Map<String, Set<Edge>> getInEdgeMap() {
return inEdges;
}
+
+ public FlowProps getFlowProps(String propSource) {
+ return flowProps.get(propSource);
+ }
}
\ No newline at end of file
src/java/azkaban/flow/Node.java 15(+14 -1)
diff --git a/src/java/azkaban/flow/Node.java b/src/java/azkaban/flow/Node.java
index bf37599..4775757 100644
--- a/src/java/azkaban/flow/Node.java
+++ b/src/java/azkaban/flow/Node.java
@@ -19,7 +19,8 @@ public class Node {
private Point2D position = null;
private int level;
private int expectedRunTimeSec = 1;
-
+ private String type;
+
public Node(String id) {
this.id = id;
}
@@ -43,6 +44,14 @@ public class Node {
return state;
}
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
public void setState(State state) {
this.state = state;
}
@@ -99,8 +108,11 @@ public class Node {
Node node = new Node(id);
String jobSource = (String)mapObj.get("job.source");
String propSource = (String)mapObj.get("prop.source");
+ String typeSource = (String)mapObj.get("job.type");
+
node.setJobSource(jobSource);
node.setPropsSource(propSource);
+ node.setType(typeSource);
Integer expectedRuntime = (Integer)mapObj.get("expectedRuntime");
if (expectedRuntime != null) {
@@ -146,6 +158,7 @@ public class Node {
objMap.put("id", id);
objMap.put("job.source", jobSource);
objMap.put("prop.source", propsSource);
+ objMap.put("job.type", type);
objMap.put("expectedRuntime", expectedRunTimeSec);
objMap.put("state", state.toString());
diff --git a/src/java/azkaban/project/FileProjectManager.java b/src/java/azkaban/project/FileProjectManager.java
index bbbbc38..c0aba7e 100644
--- a/src/java/azkaban/project/FileProjectManager.java
+++ b/src/java/azkaban/project/FileProjectManager.java
@@ -11,11 +11,18 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
+import net.sf.ehcache.config.CacheConfiguration;
+import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
+
import org.apache.log4j.Logger;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import azkaban.flow.Flow;
+import azkaban.flow.Node;
import azkaban.flow.layout.BlockFlowLayout;
import azkaban.flow.layout.LayeredFlowLayout;
import azkaban.user.Permission;
@@ -37,15 +44,33 @@ public class FileProjectManager implements ProjectManager {
private static final String PROJECT_DIRECTORY = "src";
private static final String FLOW_EXTENSION = ".flow";
private static final Logger logger = Logger.getLogger(FileProjectManager.class);
+ private static final int IDLE_SECONDS = 120;
private ConcurrentHashMap<String, Project> projects = new ConcurrentHashMap<String, Project>();
-
+ private CacheManager manager = CacheManager.create();
+ private Cache sourceCache;
+
private File projectDirectory;
public FileProjectManager(Props props) {
setupDirectories(props);
loadAllProjects();
+ setupCache();
}
+ private void setupCache() {
+ CacheConfiguration cacheConfig =
+ new CacheConfiguration("propsCache", 2000)
+ .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
+ .overflowToDisk(false)
+ .eternal(false)
+ .timeToIdleSeconds(IDLE_SECONDS)
+ .diskPersistent(false)
+ .diskExpiryThreadIntervalSeconds(0);
+
+ sourceCache = new Cache(cacheConfig);
+ manager.addCache(sourceCache);
+ }
+
private void setupDirectories(Props props) {
String projectDir = props.getString(DIRECTORY_PARAM);
logger.info("Using directory " + projectDir + " as the project directory.");
@@ -263,6 +288,9 @@ public class FileProjectManager implements ProjectManager {
else if (creator == null) {
throw new ProjectManagerException("Valid creator user must be set.");
}
+ else if (!projectName.matches("[a-zA-Z][a-zA-Z_0-9|-]*")){
+ throw new ProjectManagerException("Project names must start with a letter, followed by any number of letters, digits, '-' or '_'.");
+ }
if (projects.contains(projectName)) {
throw new ProjectManagerException("Project already exists.");
@@ -304,11 +332,11 @@ public class FileProjectManager implements ProjectManager {
private synchronized void writeProjectFile(File directory, Project project) throws IOException {
Object object = project.toObject();
File tmpFile = File.createTempFile("project-",".json", directory);
-
+
if (tmpFile.exists()) {
tmpFile.delete();
}
-
+
logger.info("Writing project file " + tmpFile);
String output = JSONUtils.toJSON(object, true);
@@ -323,7 +351,7 @@ public class FileProjectManager implements ProjectManager {
throw e;
}
writer.close();
-
+
File projectFile = new File(directory, PROPERTIES_FILENAME);
File swapFile = new File(directory, PROPERTIES_FILENAME + "_old");
@@ -362,7 +390,36 @@ public class FileProjectManager implements ProjectManager {
oldOutputFile.delete();
}
}
+
+ public Props getProperties(String projectName, String source, User user) throws ProjectManagerException {
+ Project project = projects.get(projectName);
+ if (project == null) {
+ throw new ProjectManagerException("Project " + project + " cannot be found.");
+ }
+ if (!project.hasPermission(user, Type.READ)) {
+ throw new AccessControlException("Permission denied. Do not have read access.");
+ }
+
+ String mySource = projectName + File.separatorChar + project.getSource() + File.separatorChar + "src" + File.separatorChar + source;
+ Element sourceElement = sourceCache.get(mySource);
+
+ if (sourceElement != null) {
+ return Props.clone((Props)sourceElement.getObjectValue());
+ }
+
+ File file = new File(projectDirectory, mySource);
+ if (!file.exists()) {
+ throw new ProjectManagerException("Source file " + file.getAbsolutePath() + " doesn't exist.");
+ }
+ try {
+ Props props = new Props((Props)null, file);
+ return props;
+ } catch (IOException e) {
+ throw new ProjectManagerException("Error loading file " + file.getPath(), e);
+ }
+ }
+
@Override
public synchronized Project removeProject(String projectName, User user) {
return null;
@@ -382,4 +439,6 @@ public class FileProjectManager implements ProjectManager {
return pathname.isFile() && !pathname.isHidden() && name.length() > suffix.length() && name.endsWith(suffix);
}
}
+
+
}
\ No newline at end of file
diff --git a/src/java/azkaban/project/ProjectManager.java b/src/java/azkaban/project/ProjectManager.java
index 6d673c0..e6e9046 100644
--- a/src/java/azkaban/project/ProjectManager.java
+++ b/src/java/azkaban/project/ProjectManager.java
@@ -1,11 +1,8 @@
package azkaban.project;
import java.io.File;
-import java.io.IOException;
-import java.security.AccessControlException;
import java.util.List;
-import azkaban.flow.Flow;
import azkaban.user.User;
import azkaban.utils.Props;
@@ -22,4 +19,6 @@ public interface ProjectManager {
public Project createProject(String projectName, String description, User creator) throws ProjectManagerException;
public Project removeProject(String projectName, User user) throws ProjectManagerException;
+
+ public Props getProperties(String projectName, String source, User user) throws ProjectManagerException;
}
\ No newline at end of file
diff --git a/src/java/azkaban/utils/DirectoryFlowLoader.java b/src/java/azkaban/utils/DirectoryFlowLoader.java
index 0415994..acc0b9e 100644
--- a/src/java/azkaban/utils/DirectoryFlowLoader.java
+++ b/src/java/azkaban/utils/DirectoryFlowLoader.java
@@ -99,6 +99,10 @@ public class DirectoryFlowLoader {
prop.setSource(relative);
Node node = new Node(jobName);
+ String type = prop.getString("type", "none");
+ errors.add("Job doesn't have type set '" + jobName + "'.");
+ node.setType(type);
+
node.setJobSource(relative);
if (parent != null) {
node.setPropsSource(parent.getSource());
diff --git a/src/java/azkaban/webapp/servlet/ProjectManagerServlet.java b/src/java/azkaban/webapp/servlet/ProjectManagerServlet.java
index b389b08..fcacdc1 100644
--- a/src/java/azkaban/webapp/servlet/ProjectManagerServlet.java
+++ b/src/java/azkaban/webapp/servlet/ProjectManagerServlet.java
@@ -15,6 +15,7 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.zip.ZipFile;
@@ -31,12 +32,15 @@ import org.apache.log4j.Logger;
import azkaban.flow.Edge;
import azkaban.flow.Flow;
+import azkaban.flow.FlowProps;
import azkaban.flow.Node;
import azkaban.project.Project;
import azkaban.project.ProjectManager;
import azkaban.project.ProjectManagerException;
import azkaban.user.Permission.Type;
import azkaban.user.User;
+import azkaban.utils.Pair;
+import azkaban.utils.Props;
import azkaban.utils.Utils;
import azkaban.webapp.session.Session;
import azkaban.webapp.servlet.MultipartParser;
@@ -71,6 +75,9 @@ public class ProjectManagerServlet extends LoginAbstractAzkabanServlet {
if (hasParam(req, "json")) {
handleJSONAction(req, resp, session);
}
+ else if (hasParam(req, "job")) {
+ handleJobPage(req, resp, session);
+ }
else if (hasParam(req, "flow")) {
handleFlowPage(req, resp, session);
}
@@ -122,16 +129,30 @@ public class ProjectManagerServlet extends LoginAbstractAzkabanServlet {
}
String jsonName = getParam(req, "json");
- if (jsonName.equals("fetchflowlist")) {
+ if (jsonName.equals("fetchflowjobs")) {
jsonFetchFlow(project, ret, req, resp);
}
else if (jsonName.equals("fetchflowgraph")) {
jsonFetchFlowGraph(project, ret, req, resp);
}
+ else if (jsonName.equals("fetchprojectflows")) {
+ jsonFetchProjectFlows(project, ret, req, resp);
+ }
this.writeJSON(resp, ret);
}
+ private void jsonFetchProjectFlows(Project project, HashMap<String, Object> ret, HttpServletRequest req, HttpServletResponse resp) throws ServletException {
+ ArrayList<Map<String,Object>> flowList = new ArrayList<Map<String,Object>>();
+ for (Flow flow: project.getFlows()) {
+ HashMap<String, Object> flowObj = new HashMap<String, Object>();
+ flowObj.put("flowId", flow.getId());
+ flowList.add(flowObj);
+ }
+
+ ret.put("flows", flowList);
+ }
+
private void jsonFetchFlowGraph(Project project, HashMap<String, Object> ret, HttpServletRequest req, HttpServletResponse resp) throws ServletException {
String flowId = getParam(req, "flow");
Flow flow = project.getFlow(flowId);
@@ -219,6 +240,86 @@ public class ProjectManagerServlet extends LoginAbstractAzkabanServlet {
ret.put("nodes", nodeList);
}
+ private void handleJobPage(HttpServletRequest req, HttpServletResponse resp, Session session) throws ServletException {
+ Page page = newPage(req, resp, session, "azkaban/webapp/servlet/velocity/jobpage.vm");
+ String projectName = getParam(req, "project");
+ String flowName = getParam(req, "flow");
+ String jobName = getParam(req, "job");
+
+ User user = session.getUser();
+ Project project = null;
+ Flow flow = null;
+ try {
+ project = manager.getProject(projectName, user);
+ if (project == null) {
+ page.add("errorMsg", "Project " + projectName + " not found.");
+ }
+ else {
+ page.add("project", project);
+
+ flow = project.getFlow(flowName);
+ if (flow == null) {
+ page.add("errorMsg", "Flow " + flowName + " not found.");
+ }
+ else {
+ page.add("flowid", flow.getId());
+
+ Node node = flow.getNode(jobName);
+
+ if (node == null) {
+ page.add("errorMsg", "Job " + jobName + " not found.");
+ }
+ else {
+ Props prop = manager.getProperties(projectName, node.getJobSource(), user);
+ page.add("jobid", node.getId());
+ page.add("jobtype", node.getType());
+
+ ArrayList<String> dependencies = new ArrayList<String>();
+ Set<Edge> inEdges = flow.getInEdges(node.getId());
+ if (inEdges != null) {
+ for ( Edge dependency: inEdges ) {
+ dependencies.add(dependency.getSourceId());
+ }
+ }
+ if (!dependencies.isEmpty()) {
+ page.add("dependencies", dependencies);
+ }
+
+ ArrayList<String> dependents = new ArrayList<String>();
+ Set<Edge> outEdges = flow.getOutEdges(node.getId());
+ if (outEdges != null) {
+ for ( Edge dependent: outEdges ) {
+ dependents.add(dependent.getTargetId());
+ }
+ }
+ if (!dependents.isEmpty()) {
+ page.add("dependents", dependents);
+ }
+
+ // Resolve property dependencies
+ String source = node.getPropsSource();
+ page.add("properties", source);
+
+ ArrayList<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>();
+ // Parameter
+ for (String key : prop.getKeySet()) {
+ String value = prop.get(key);
+ parameters.add(new Pair<String,String>(key, value));
+ }
+
+ page.add("parameters", parameters);
+ }
+ }
+ }
+ }
+ catch (AccessControlException e) {
+ page.add("errorMsg", e.getMessage());
+ } catch (ProjectManagerException e) {
+ page.add("errorMsg", e.getMessage());
+ }
+
+ page.render();
+ }
private void handleFlowPage(HttpServletRequest req, HttpServletResponse resp, Session session) throws ServletException {
Page page = newPage(req, resp, session, "azkaban/webapp/servlet/velocity/flowpage.vm");
@@ -243,8 +344,6 @@ public class ProjectManagerServlet extends LoginAbstractAzkabanServlet {
page.add("flowid", flow.getId());
}
-
-
}
catch (AccessControlException e) {
page.add("errorMsg", e.getMessage());
diff --git a/src/java/azkaban/webapp/servlet/velocity/flowpage.vm b/src/java/azkaban/webapp/servlet/velocity/flowpage.vm
index 75d7122..2ff14e9 100644
--- a/src/java/azkaban/webapp/servlet/velocity/flowpage.vm
+++ b/src/java/azkaban/webapp/servlet/velocity/flowpage.vm
@@ -8,6 +8,7 @@
<script type="text/javascript" src="${context}/js/underscore-1.2.1-min.js"></script>
<script type="text/javascript" src="${context}/js/backbone-0.5.3-min.js"></script>
<script type="text/javascript" src="${context}/js/jquery.simplemodal.js"></script>
+ <script type="text/javascript" src="${context}/js/jquery.contextMenu.js"></script>
<script type="text/javascript" src="${context}/js/azkaban.nav.js"></script>
<script type="text/javascript" src="${context}/js/azkaban.flow.view.js"></script>
<script type="text/javascript" src="${context}/js/svgNavigate.js"></script>
@@ -21,7 +22,7 @@
var projectName = "${project.name}";
var flowName = "${flowid}";
</script>
-
+ <link rel="stylesheet" type="text/css" href="${context}/css/jquery.contextMenu.custom.css" />
</head>
<body>
#set($current_page="all")
@@ -39,8 +40,10 @@
<div id="all-jobs-content">
<div class="section-hd">
- <h2><a href="${context}/manager?project=${project.name}">Project <span>$project.name</span></a></h2>
- <h3><a href="${context}/manager?project=${project.name}&flow=${flowid}">Flow <span>$flowid</span></a></h3>
+ <h2><a href="${context}/manager?project=${project.name}&flow=${flowid}">Flow <span>$flowid</span></a></h2>
+ <div class="section-sub-hd">
+ <h4><a href="${context}/manager?project=${project.name}">Project <span>$project.name</span></a></h4>
+ </div>
</div>
<div id="headertabs" class="headertabs">
@@ -71,6 +74,11 @@
</div>
</div>
#end
+ <ul id="jobMenu" class="contextMenu">
+ <li class="open"><a href="#open">Open...</a></li>
+ <li class="openwindow"><a href="#openwindow">Open in New Window...</a></li>
+ </ul>
+
</div>
</body>
</html>
diff --git a/src/java/azkaban/webapp/servlet/velocity/index.vm b/src/java/azkaban/webapp/servlet/velocity/index.vm
index e8a19e1..f2e82ef 100644
--- a/src/java/azkaban/webapp/servlet/velocity/index.vm
+++ b/src/java/azkaban/webapp/servlet/velocity/index.vm
@@ -8,7 +8,7 @@
<script type="text/javascript" src="${context}/js/backbone-0.5.3-min.js"></script>
<script type="text/javascript" src="${context}/js/jquery.simplemodal.js"></script>
<script type="text/javascript" src="${context}/js/azkaban.nav.js"></script>
- <script type="text/javascript" src="${context}/js/azkaban.job.view.js"></script>
+ <script type="text/javascript" src="${context}/js/azkaban.main.view.js"></script>
<script type="text/javascript">
var contextURL = "${context}";
var currentTime = ${currentTime};
@@ -50,7 +50,7 @@
#foreach($project in $projects)
<tr class="row">
<td class="tb-name">
- <div class="jobfolder expand" onclick="expandFlow(this)" id="$folderName">
+ <div class="jobfolder expand" id="${project.name}">
<span class="state-icon"></span>
<a href="${context}/manager?project=${project.name}">$project.name</a>
</div>
@@ -59,6 +59,17 @@
<td class="tb-owner">$project.lastModifiedUser</td>
<td class="tb-action last"></td>
</tr>
+ <tr class="childrow" id="${project.name}-child" style="display: none;">
+ <td class="expandedFlow">
+ <table class="innerTable">
+ <thead>
+ <tr><th class="tb-name">Flows</th></tr>
+ </thead>
+ <tbody id="${project.name}-tbody">
+ </tbody>
+ </table>
+ </td>
+ </tr>
#end
#else
<tr><td class="last">No viewable projects found.</td></tr>
diff --git a/src/java/azkaban/webapp/servlet/velocity/jobpage.vm b/src/java/azkaban/webapp/servlet/velocity/jobpage.vm
new file mode 100644
index 0000000..5404daf
--- /dev/null
+++ b/src/java/azkaban/webapp/servlet/velocity/jobpage.vm
@@ -0,0 +1,97 @@
+<!DOCTYPE html>
+<html>
+ <head>
+#parse( "azkaban/webapp/servlet/velocity/style.vm" )
+ <script type="text/javascript" src="${context}/js/jquery/jquery.js"></script>
+ <script type="text/javascript" src="${context}/js/jqueryui/jquery-ui.custom.min.js"></script>
+ <script type="text/javascript" src="${context}/js/namespace.js"></script>
+ <script type="text/javascript" src="${context}/js/underscore-1.2.1-min.js"></script>
+ <script type="text/javascript" src="${context}/js/backbone-0.5.3-min.js"></script>
+ <script type="text/javascript" src="${context}/js/jquery.simplemodal.js"></script>
+ <script type="text/javascript" src="${context}/js/azkaban.nav.js"></script>
+ <script type="text/javascript">
+ var contextURL = "${context}";
+ var currentTime = ${currentTime};
+ var timezone = "${timezone}";
+ var errorMessage = "${error_message}";
+ var successMessage = "${success_message}";
+
+ var projectId = "$project.name";
+ </script>
+ </head>
+ <body>
+#set($current_page="all")
+#parse( "azkaban/webapp/servlet/velocity/nav.vm" )
+ <div class="messaging"><p id="messageClose">X</p><p id="message"></p></div>
+
+#if($errorMsg)
+ <div class="box-error-message">$errorMsg</div>
+#else
+ <div class="content">
+ #if($error_message != "null")
+ <div class="box-error-message">$error_message</div>
+ #elseif($success_message != "null")
+ <div class="box-success-message">$success_message</div>
+ #end
+ <div id="all-jobs-content">
+ <div class="section-hd">
+ <h2><a href="${context}/manager?project=${project.name}&flow=${flowid}&job=${jobid}">Job <span>$jobid</span></a></h2>
+ <div class="section-sub-hd">
+ <h4><a href="${context}/manager?project=${project.name}">Project <span>$project.name</span></a></h4>
+ <h4 class="separator">></h4>
+ <h4><a href="${context}/manager?project=${project.name}&flow=${flowid}">Flow <span>$flowid</span></a></h4>
+ </div>
+ </div>
+ </div>
+
+ <div id="job-summary">
+ <table class="summary-table">
+ <tr><td class="first">Type:</td><td>$jobtype</td></tr>
+ <tr><td class="first">Dependencies:</td><td>
+#if ($dependencies)
+#foreach($dependency in $dependencies)
+ <a href="${context}/manager?project=${project.name}&flow=${flowid}&job=$dependency">$dependency</a>
+#end
+#else
+ <span>No Dependencies</span>
+#end
+ </td></tr>
+ <tr><td class="first">Dependents:</td><td>
+#if ($dependents)
+#foreach($dependent in $dependents)
+ <a href="${context}/manager?project=${project.name}&flow=${flowid}&job=$dependent">$dependent</a><span>,</span>
+#end
+#else
+ <span>No Dependencies</span>
+#end
+ </td></tr>
+ <tr><td class="first">Properties:</td><td>
+#if ($properties)
+ <a>$properties</a>
+#else
+ <span>No Inherited Properties</span>
+#end
+ </td></tr>
+ </table>
+ </div>
+
+ <table id="all-jobs" class="all-jobs job-table parameters">
+ <thead>
+ <tr>
+ <th class="tb-pname">Parameter Name</th>
+ <th class="tb-pvalue">Value</th>
+ </tr>
+ </thead>
+ <tbody>
+#foreach($parameter in $parameters)
+ <tr>
+ <td class="first">$parameter.first</td><td>$parameter.second</td>
+ </tr>
+#end
+ </tbody>
+ </table>
+ </div>
+
+#end
+ </body>
+</html>
\ No newline at end of file
src/web/css/azkaban.css 72(+71 -1)
diff --git a/src/web/css/azkaban.css b/src/web/css/azkaban.css
index 65fa053..43ffd2e 100644
--- a/src/web/css/azkaban.css
+++ b/src/web/css/azkaban.css
@@ -114,7 +114,7 @@ textarea {
background-color: #E0E0E0;
border: 1px solid #cdcdcd;
margin: 0 50px 10px;
- min-height: 240px;
+ min-height: 150px;
}
.section-ft {
@@ -128,6 +128,7 @@ textarea {
}
.section-hd h2 {
+ clear: both;
float: left;
font-size: 125%;
font-weight: bold;
@@ -168,6 +169,39 @@ textarea {
font-size: 90%;
}
+.section-sub-hd {
+ clear: both;
+}
+
+.section-hd h4 {
+ float: left;
+ font-weight: bold;
+ font-size:11pt;
+ margin-right: 5px;
+}
+
+.section-hd h4 a {
+ text-decoration:none;
+ color: #888;
+}
+
+.section-hd h4 a:hover {
+ color: #009FC9;
+}
+
+.section-hd h4 span {
+ padding-left: 5px;
+ font-weight: normal;
+ font-size: 10pt;
+}
+
+.section-hd h4.separator {
+ color: #AAA;
+ padding-left: 5px;
+ font-weight: normal;
+ font-size: 10pt;
+}
+
table {
background-color: #fff;
font-family: Arial;
@@ -225,6 +259,14 @@ tr:hover td {
width: 10%;
}
+.all-jobs .tb-pname {
+
+}
+
+.all-jobs .tb-pvalue {
+
+}
+
/* messaging */
.messaging {
color: #fff;
@@ -795,6 +837,26 @@ tr:hover td {
width: 30%;
}
+#job-summary {
+ margin-left: 30px;
+ margin-bottom: 10px;
+ width: 60%;
+}
+
+#job-summary table tr td a {
+ text-decoration: none;
+ color: #555;
+}
+
+#job-summary table tr td span {
+ color: #555;
+}
+
+#job-summary table tr td a:hover {
+ text-decoration: none;
+ color: #009FC9;
+}
+
.summary-table {
font-size: 12px;
background: none;
@@ -1044,6 +1106,14 @@ tr:hover td {
color: black;
}
+table.parameters tr td.first {
+ font-weight: bold;
+}
+
+table.parameters tr td {
+ font-size: 11pt;
+ padding-left: 10px;
+}
/* old styles */
src/web/css/jquery.contextMenu.css 62(+62 -0)
diff --git a/src/web/css/jquery.contextMenu.css b/src/web/css/jquery.contextMenu.css
new file mode 100644
index 0000000..5b2dd90
--- /dev/null
+++ b/src/web/css/jquery.contextMenu.css
@@ -0,0 +1,62 @@
+/* Generic context menu styles */
+.contextMenu {
+ position: absolute;
+ width: 120px;
+ z-index: 99999;
+ border: solid 1px #CCC;
+ background: #EEE;
+ padding: 0px;
+ margin: 0px;
+ display: none;
+}
+
+.contextMenu LI {
+ list-style: none;
+ padding: 0px;
+ margin: 0px;
+}
+
+.contextMenu A {
+ color: #333;
+ text-decoration: none;
+ display: block;
+ line-height: 20px;
+ height: 20px;
+ background-position: 6px center;
+ background-repeat: no-repeat;
+ outline: none;
+ padding: 1px 5px;
+ padding-left: 28px;
+}
+
+.contextMenu LI.hover A {
+ color: #FFF;
+ background-color: #3399FF;
+}
+
+.contextMenu LI.disabled A {
+ color: #AAA;
+ cursor: default;
+}
+
+.contextMenu LI.hover.disabled A {
+ background-color: transparent;
+}
+
+.contextMenu LI.separator {
+ border-top: solid 1px #CCC;
+}
+
+/*
+ Adding Icons
+
+ You can add icons to the context menu by adding
+ classes to the respective LI element(s)
+*/
+
+.contextMenu LI.edit A { background-image: url(images/page_white_edit.png); }
+.contextMenu LI.cut A { background-image: url(images/cut.png); }
+.contextMenu LI.copy A { background-image: url(images/page_white_copy.png); }
+.contextMenu LI.paste A { background-image: url(images/page_white_paste.png); }
+.contextMenu LI.delete A { background-image: url(images/page_white_delete.png); }
+.contextMenu LI.quit A { background-image: url(images/door.png); }
src/web/css/jquery.contextMenu.custom.css 64(+64 -0)
diff --git a/src/web/css/jquery.contextMenu.custom.css b/src/web/css/jquery.contextMenu.custom.css
new file mode 100644
index 0000000..07aa950
--- /dev/null
+++ b/src/web/css/jquery.contextMenu.custom.css
@@ -0,0 +1,64 @@
+/* Generic context menu styles */
+.contextMenu {
+ position: absolute;
+ width: 200px;
+ z-index: 99999;
+ border: solid 1px #CCC;
+ background: #EEE;
+ padding: 0px;
+ margin: 0px;
+ display: none;
+}
+
+.contextMenu LI {
+ list-style: none;
+ padding: 0px;
+ margin: 0px;
+}
+
+.contextMenu A {
+ color: #333;
+ text-decoration: none;
+ display: block;
+ line-height: 20px;
+ height: 20px;
+ background-position: 6px center;
+ background-repeat: no-repeat;
+ outline: none;
+ padding: 1px 5px;
+ padding-left: 28px;
+
+ font-size: 10pt;
+}
+
+.contextMenu LI.hover A {
+ color: #FFF;
+ background-color: #3399FF;
+}
+
+.contextMenu LI.disabled A {
+ color: #AAA;
+ cursor: default;
+}
+
+.contextMenu LI.hover.disabled A {
+ background-color: transparent;
+}
+
+.contextMenu LI.separator {
+ border-top: solid 1px #CCC;
+}
+
+/*
+ Adding Icons
+
+ You can add icons to the context menu by adding
+ classes to the respective LI element(s)
+*/
+
+.contextMenu LI.edit A { background-image: url(images/page_white_edit.png); }
+.contextMenu LI.cut A { background-image: url(images/cut.png); }
+.contextMenu LI.copy A { background-image: url(images/page_white_copy.png); }
+.contextMenu LI.paste A { background-image: url(images/page_white_paste.png); }
+.contextMenu LI.delete A { background-image: url(images/page_white_delete.png); }
+.contextMenu LI.quit A { background-image: url(images/door.png); }
src/web/js/azkaban.flow.view.js 30(+29 -1)
diff --git a/src/web/js/azkaban.flow.view.js b/src/web/js/azkaban.flow.view.js
index 871a6d0..36dd514 100644
--- a/src/web/js/azkaban.flow.view.js
+++ b/src/web/js/azkaban.flow.view.js
@@ -1,5 +1,17 @@
$.namespace('azkaban');
+var handleJobMenuClick = function(action, el, pos) {
+ var jobid = el[0].jobid;
+ var requestURL = contextURL + "/manager?project=" + projectName + "&flow=" + flowName + "&job=" + jobid;
+ if (action == "open") {
+ window.location.href = requestURL;
+ }
+ else if(action == "openwindow") {
+ window.open(requestURL);
+ }
+
+}
+
var flowTabView;
azkaban.FlowTabView= Backbone.View.extend({
events : {
@@ -58,6 +70,7 @@ azkaban.JobListView = Backbone.View.extend({
function(){
var a = $(this).find("a");
$(a).html(this.jobid);
+ $(this).show();
}
);
}
@@ -72,6 +85,7 @@ azkaban.JobListView = Backbone.View.extend({
function(){
var a = $(this).find("a");
$(a).html(this.jobid);
+ $(this).show();
}
);
}
@@ -132,6 +146,12 @@ azkaban.JobListView = Backbone.View.extend({
ul.appendChild(li);
li.jobid=nodeArray[i].id;
+ $(li).contextMenu({
+ menu: 'jobMenu'
+ },
+ handleJobMenuClick
+ );
+
this.listNodes[nodeArray[i].id] = li;
}
@@ -302,7 +322,7 @@ azkaban.SvgGraphView = Backbone.View.extend({
line.setAttributeNS(null, "x2", endNode.sx);
line.setAttributeNS(null, "y2", endNode.sy);
line.setAttributeNS(null, "style", "stroke:rgb(255,0,0);stroke-width:2");
-
+
self.mainG.appendChild(line);
}
},
@@ -315,6 +335,7 @@ azkaban.SvgGraphView = Backbone.View.extend({
var yOffset = 10;
var nodeG = document.createElementNS(svgns, "g");
+ nodeG.setAttributeNS(null, "class", "jobnode");
nodeG.setAttributeNS(null, "id", node.id);
nodeG.setAttributeNS(null, "font-family", "helvetica");
nodeG.setAttributeNS(null, "transform", "translate(" + node.sx + "," + node.sy + ")");
@@ -344,6 +365,13 @@ azkaban.SvgGraphView = Backbone.View.extend({
innerG.appendChild(text);
nodeG.appendChild(innerG);
self.mainG.appendChild(nodeG);
+
+ nodeG.jobid=node.id;
+ $(nodeG).contextMenu({
+ menu: 'jobMenu'
+ },
+ handleJobMenuClick
+ );
this.nodes[node.id] = node;
},
src/web/js/azkaban.project.view.js 11(+8 -3)
diff --git a/src/web/js/azkaban.project.view.js b/src/web/js/azkaban.project.view.js
index 76dee8d..298eaec 100644
--- a/src/web/js/azkaban.project.view.js
+++ b/src/web/js/azkaban.project.view.js
@@ -53,6 +53,10 @@ azkaban.FlowTableView= Backbone.View.extend({
_.bindAll(this, 'createJobListTable');
},
expandFlowProject : function(evt) {
+ if (evt.target.tagName!="SPAN") {
+ return;
+ }
+
var target = evt.currentTarget;
var targetId = target.id;
var requestURL = contextURL + "/manager";
@@ -82,7 +86,7 @@ azkaban.FlowTableView= Backbone.View.extend({
$.get(
requestURL,
- {"project": projectId, "json":"fetchflowlist", "flow":targetId},
+ {"project": projectId, "json":"fetchflowjobs", "flow":targetId},
function(data) {
console.log("Success");
target.loaded = true;
@@ -100,7 +104,7 @@ azkaban.FlowTableView= Backbone.View.extend({
createJobListTable : function(data, innerTable) {
var nodes = data.nodes;
var flowId = data.flowId;
-
+ var requestURL = contextURL + "/manager?project=" + data.project + "&flow=" + data.flowId + "&job=";
for (var i = 0; i < nodes.length; i++) {
var job = nodes[i];
var name = job.id;
@@ -118,6 +122,7 @@ azkaban.FlowTableView= Backbone.View.extend({
$(ida).text(name);
$(ida).attr("id", nodeId);
$(ida).css("margin-left", level * 20);
+ $(ida).attr("href", requestURL + name);
$(idtd).append(ida);
$(tr).append(idtd);
@@ -150,7 +155,7 @@ azkaban.FlowTableView= Backbone.View.extend({
});
$(function() {
- projectView = new azkaban.ProjectView({el:$( '#all-jobs-content'), successMsg: successMessage, errorMsg: errorMessage });
+ projectView = new azkaban.ProjectView({el:$('#all-jobs-content')});
uploadView = new azkaban.UploadProjectView({el:$('#upload-project')});
flowTableView = new azkaban.FlowTableView({el:$('#flow-tabs')});
// Setting up the project tabs
src/web/js/jquery.contextMenu.js 211(+211 -0)
diff --git a/src/web/js/jquery.contextMenu.js b/src/web/js/jquery.contextMenu.js
new file mode 100644
index 0000000..59c1737
--- /dev/null
+++ b/src/web/js/jquery.contextMenu.js
@@ -0,0 +1,211 @@
+// jQuery Context Menu Plugin
+//
+// Version 1.01
+//
+// Cory S.N. LaViska
+// A Beautiful Site (http://abeautifulsite.net/)
+//
+// More info: http://abeautifulsite.net/2008/09/jquery-context-menu-plugin/
+//
+// Terms of Use
+//
+// This plugin is dual-licensed under the GNU General Public License
+// and the MIT License and is copyright A Beautiful Site, LLC.
+//
+if(jQuery)( function() {
+ $.extend($.fn, {
+
+ contextMenu: function(o, callback) {
+ // Defaults
+ if( o.menu == undefined ) return false;
+ if( o.inSpeed == undefined ) o.inSpeed = 150;
+ if( o.outSpeed == undefined ) o.outSpeed = 75;
+ // 0 needs to be -1 for expected results (no fade)
+ if( o.inSpeed == 0 ) o.inSpeed = -1;
+ if( o.outSpeed == 0 ) o.outSpeed = -1;
+ // Loop each context menu
+ $(this).each( function() {
+ var el = $(this);
+ var offset = $(el).offset();
+ // Add contextMenu class
+ $('#' + o.menu).addClass('contextMenu');
+ // Simulate a true right click
+ $(this).mousedown( function(e) {
+ var evt = e;
+ evt.stopPropagation();
+ $(this).mouseup( function(e) {
+ e.stopPropagation();
+ var srcElement = $(this);
+ $(this).unbind('mouseup');
+ if( evt.button == 2 ) {
+ // Hide context menus that may be showing
+ $(".contextMenu").hide();
+ // Get this context menu
+ var menu = $('#' + o.menu);
+
+ if( $(el).hasClass('disabled') ) return false;
+
+ // Detect mouse position
+ var d = {}, x, y;
+ if( self.innerHeight ) {
+ d.pageYOffset = self.pageYOffset;
+ d.pageXOffset = self.pageXOffset;
+ d.innerHeight = self.innerHeight;
+ d.innerWidth = self.innerWidth;
+ } else if( document.documentElement &&
+ document.documentElement.clientHeight ) {
+ d.pageYOffset = document.documentElement.scrollTop;
+ d.pageXOffset = document.documentElement.scrollLeft;
+ d.innerHeight = document.documentElement.clientHeight;
+ d.innerWidth = document.documentElement.clientWidth;
+ } else if( document.body ) {
+ d.pageYOffset = document.body.scrollTop;
+ d.pageXOffset = document.body.scrollLeft;
+ d.innerHeight = document.body.clientHeight;
+ d.innerWidth = document.body.clientWidth;
+ }
+ (e.pageX) ? x = e.pageX : x = e.clientX + d.scrollLeft;
+ (e.pageY) ? y = e.pageY : y = e.clientY + d.scrollTop;
+
+ // Show the menu
+ $(document).unbind('click');
+ $(menu).css({ top: y, left: x }).fadeIn(o.inSpeed);
+ // Hover events
+ $(menu).find('A').mouseover( function() {
+ $(menu).find('LI.hover').removeClass('hover');
+ $(this).parent().addClass('hover');
+ }).mouseout( function() {
+ $(menu).find('LI.hover').removeClass('hover');
+ });
+
+ // Keyboard
+ $(document).keypress( function(e) {
+ switch( e.keyCode ) {
+ case 38: // up
+ if( $(menu).find('LI.hover').size() == 0 ) {
+ $(menu).find('LI:last').addClass('hover');
+ } else {
+ $(menu).find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');
+ if( $(menu).find('LI.hover').size() == 0 ) $(menu).find('LI:last').addClass('hover');
+ }
+ break;
+ case 40: // down
+ if( $(menu).find('LI.hover').size() == 0 ) {
+ $(menu).find('LI:first').addClass('hover');
+ } else {
+ $(menu).find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');
+ if( $(menu).find('LI.hover').size() == 0 ) $(menu).find('LI:first').addClass('hover');
+ }
+ break;
+ case 13: // enter
+ $(menu).find('LI.hover A').trigger('click');
+ break;
+ case 27: // esc
+ $(document).trigger('click');
+ break
+ }
+ });
+
+ // When items are selected
+ $('#' + o.menu).find('A').unbind('click');
+ $('#' + o.menu).find('LI:not(.disabled) A').click( function() {
+ $(document).unbind('click').unbind('keypress');
+ $(".contextMenu").hide();
+ // Callback
+ if( callback ) callback( $(this).attr('href').substr(1), $(srcElement), {x: x - offset.left, y: y - offset.top, docX: x, docY: y} );
+ return false;
+ });
+
+ // Hide bindings
+ setTimeout( function() { // Delay for Mozilla
+ $(document).click( function() {
+ $(document).unbind('click').unbind('keypress');
+ $(menu).fadeOut(o.outSpeed);
+ return false;
+ });
+ }, 0);
+ }
+ });
+ });
+
+ // Disable text selection
+ if( $.browser.mozilla ) {
+ $('#' + o.menu).each( function() { $(this).css({ 'MozUserSelect' : 'none' }); });
+ } else if( $.browser.msie ) {
+ $('#' + o.menu).each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
+ } else {
+ $('#' + o.menu).each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
+ }
+ // Disable browser context menu (requires both selectors to work in IE/Safari + FF/Chrome)
+ $(el).add($('UL.contextMenu')).bind('contextmenu', function() { return false; });
+
+ });
+ return $(this);
+ },
+
+ // Disable context menu items on the fly
+ disableContextMenuItems: function(o) {
+ if( o == undefined ) {
+ // Disable all
+ $(this).find('LI').addClass('disabled');
+ return( $(this) );
+ }
+ $(this).each( function() {
+ if( o != undefined ) {
+ var d = o.split(',');
+ for( var i = 0; i < d.length; i++ ) {
+ $(this).find('A[href="' + d[i] + '"]').parent().addClass('disabled');
+
+ }
+ }
+ });
+ return( $(this) );
+ },
+
+ // Enable context menu items on the fly
+ enableContextMenuItems: function(o) {
+ if( o == undefined ) {
+ // Enable all
+ $(this).find('LI.disabled').removeClass('disabled');
+ return( $(this) );
+ }
+ $(this).each( function() {
+ if( o != undefined ) {
+ var d = o.split(',');
+ for( var i = 0; i < d.length; i++ ) {
+ $(this).find('A[href="' + d[i] + '"]').parent().removeClass('disabled');
+
+ }
+ }
+ });
+ return( $(this) );
+ },
+
+ // Disable context menu(s)
+ disableContextMenu: function() {
+ $(this).each( function() {
+ $(this).addClass('disabled');
+ });
+ return( $(this) );
+ },
+
+ // Enable context menu(s)
+ enableContextMenu: function() {
+ $(this).each( function() {
+ $(this).removeClass('disabled');
+ });
+ return( $(this) );
+ },
+
+ // Destroy context menu(s)
+ destroyContextMenu: function() {
+ // Destroy specified context menus
+ $(this).each( function() {
+ // Disable action
+ $(this).unbind('mousedown').unbind('mouseup');
+ });
+ return( $(this) );
+ }
+
+ });
+})(jQuery);
\ No newline at end of file
src/web/js/jquery.contextMenu.js~ 1585(+1585 -0)
diff --git a/src/web/js/jquery.contextMenu.js~ b/src/web/js/jquery.contextMenu.js~
new file mode 100644
index 0000000..26acefc
--- /dev/null
+++ b/src/web/js/jquery.contextMenu.js~
@@ -0,0 +1,1585 @@
+/*!
+ * jQuery contextMenu - Plugin for simple contextMenu handling
+ *
+ * Version: 1.5.22
+ *
+ * Authors: Rodney Rehm, Addy Osmani (patches for FF)
+ * Web: http://medialize.github.com/jQuery-contextMenu/
+ *
+ * Licensed under
+ * MIT License http://www.opensource.org/licenses/mit-license
+ * GPL v3 http://opensource.org/licenses/GPL-3.0
+ *
+ */
+
+(function($, undefined){
+
+ // TODO: -
+ // ARIA stuff: menuitem, menuitemcheckbox und menuitemradio
+ // create <menu> structure if $.support[htmlCommand || htmlMenuitem] and !opt.disableNative
+
+// determine html5 compatibility
+$.support.htmlMenuitem = ('HTMLMenuItemElement' in window);
+$.support.htmlCommand = ('HTMLCommandElement' in window);
+$.support.eventSelectstart = ("onselectstart" in document.documentElement);
+/* // should the need arise, test for css user-select
+$.support.cssUserSelect = (function(){
+ var t = false,
+ e = document.createElement('div');
+
+ $.each('Moz|Webkit|Khtml|O|ms|Icab|'.split('|'), function(i, prefix) {
+ var propCC = prefix + (prefix ? 'U' : 'u') + 'serSelect',
+ prop = (prefix ? ('-' + prefix.toLowerCase() + '-') : '') + 'user-select';
+
+ e.style.cssText = prop + ': text;';
+ if (e.style[propCC] == 'text') {
+ t = true;
+ return false;
+ }
+
+ return true;
+ });
+
+ return t;
+})();
+*/
+
+var // currently active contextMenu trigger
+ $currentTrigger = null,
+ // is contextMenu initialized with at least one menu?
+ initialized = false,
+ // window handle
+ $win = $(window),
+ // number of registered menus
+ counter = 0,
+ // mapping selector to namespace
+ namespaces = {},
+ // mapping namespace to options
+ menus = {},
+ // custom command type handlers
+ types = {},
+ // default values
+ defaults = {
+ // selector of contextMenu trigger
+ selector: null,
+ // where to append the menu to
+ appendTo: null,
+ // method to trigger context menu ["right", "left", "hover"]
+ trigger: "right",
+ // hide menu when mouse leaves trigger / menu elements
+ autoHide: false,
+ // ms to wait before showing a hover-triggered context menu
+ delay: 200,
+ // determine position to show menu at
+ determinePosition: function($menu) {
+ // position to the lower middle of the trigger element
+ if ($.ui && $.ui.position) {
+ // .position() is provided as a jQuery UI utility
+ // (...and it won't work on hidden elements)
+ $menu.css('display', 'block').position({
+ my: "center top",
+ at: "center bottom",
+ of: this,
+ offset: "0 5",
+ collision: "fit"
+ }).css('display', 'none');
+ } else {
+ // determine contextMenu position
+ var offset = this.offset();
+ offset.top += this.outerHeight();
+ offset.left += this.outerWidth() / 2 - $menu.outerWidth() / 2;
+ $menu.css(offset);
+ }
+ },
+ // position menu
+ position: function(opt, x, y) {
+ var $this = this,
+ offset;
+ // determine contextMenu position
+ if (!x && !y) {
+ opt.determinePosition.call(this, opt.$menu);
+ return;
+ } else if (x === "maintain" && y === "maintain") {
+ // x and y must not be changed (after re-show on command click)
+ offset = opt.$menu.position();
+ } else {
+ // x and y are given (by mouse event)
+ var triggerIsFixed = opt.$trigger.parents().andSelf()
+ .filter(function() {
+ return $(this).css('position') == "fixed";
+ }).length;
+
+ if (triggerIsFixed) {
+ y -= $win.scrollTop();
+ x -= $win.scrollLeft();
+ }
+ offset = {top: y, left: x};
+ }
+
+ // correct offset if viewport demands it
+ var bottom = $win.scrollTop() + $win.height(),
+ right = $win.scrollLeft() + $win.width(),
+ height = opt.$menu.height(),
+ width = opt.$menu.width();
+
+ if (offset.top + height > bottom) {
+ offset.top -= height;
+ }
+
+ if (offset.left + width > right) {
+ offset.left -= width;
+ }
+
+ opt.$menu.css(offset);
+ },
+ // position the sub-menu
+ positionSubmenu: function($menu) {
+ if ($.ui && $.ui.position) {
+ // .position() is provided as a jQuery UI utility
+ // (...and it won't work on hidden elements)
+ $menu.css('display', 'block').position({
+ my: "left top",
+ at: "right top",
+ of: this,
+ collision: "fit"
+ }).css('display', '');
+ } else {
+ // determine contextMenu position
+ var offset = {
+ top: 0,
+ left: this.outerWidth()
+ };
+ $menu.css(offset);
+ }
+ },
+ // offset to add to zIndex
+ zIndex: 1,
+ // show hide animation settings
+ animation: {
+ duration: 50,
+ show: 'slideDown',
+ hide: 'slideUp'
+ },
+ // events
+ events: {
+ show: $.noop,
+ hide: $.noop
+ },
+ // default callback
+ callback: null,
+ // list of contextMenu items
+ items: {}
+ },
+ // mouse position for hover activation
+ hoveract = {
+ timer: null,
+ pageX: null,
+ pageY: null
+ },
+ // determine zIndex
+ zindex = function($t) {
+ var zin = 0,
+ $tt = $t;
+
+ while (true) {
+ zin = Math.max(zin, parseInt($tt.css('z-index'), 10) || 0);
+ $tt = $tt.parent();
+ if (!$tt || !$tt.length || "html body".indexOf($tt.prop('nodeName').toLowerCase()) > -1 ) {
+ break;
+ }
+ }
+
+ return zin;
+ },
+ // event handlers
+ handle = {
+ // abort anything
+ abortevent: function(e){
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ },
+
+ // contextmenu show dispatcher
+ contextmenu: function(e) {
+ var $this = $(this);
+
+ // disable actual context-menu
+ e.preventDefault();
+ e.stopImmediatePropagation();
+
+ // abort native-triggered events unless we're triggering on right click
+ if (e.data.trigger != 'right' && e.originalEvent) {
+ return;
+ }
+
+ if (!$this.hasClass('context-menu-disabled')) {
+ // theoretically need to fire a show event at <menu>
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#context-menus
+ // var evt = jQuery.Event("show", { data: data, pageX: e.pageX, pageY: e.pageY, relatedTarget: this });
+ // e.data.$menu.trigger(evt);
+
+ $currentTrigger = $this;
+ if (e.data.build) {
+ var built = e.data.build($currentTrigger, e);
+ // abort if build() returned false
+ if (built === false) {
+ return;
+ }
+
+ // dynamically build menu on invocation
+ e.data = $.extend(true, {}, defaults, e.data, built || {});
+
+ // abort if there are no items to display
+ if (!e.data.items || $.isEmptyObject(e.data.items)) {
+ // Note: jQuery captures and ignores errors from event handlers
+ if (window.console) {
+ (console.error || console.log)("No items specified to show in contextMenu");
+ }
+
+ throw new Error('No Items sepcified');
+ }
+
+ // backreference for custom command type creation
+ e.data.$trigger = $currentTrigger;
+
+ op.create(e.data);
+ }
+ // show menu
+ op.show.call($this, e.data, e.pageX, e.pageY);
+ }
+ },
+ // contextMenu left-click trigger
+ click: function(e) {
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ $(this).trigger(jQuery.Event("contextmenu", { data: e.data, pageX: e.pageX, pageY: e.pageY }));
+ },
+ // contextMenu right-click trigger
+ mousedown: function(e) {
+ // register mouse down
+ var $this = $(this);
+
+ // hide any previous menus
+ if ($currentTrigger && $currentTrigger.length && !$currentTrigger.is($this)) {
+ $currentTrigger.data('contextMenu').$menu.trigger('contextmenu:hide');
+ }
+
+ // activate on right click
+ if (e.button == 2) {
+ $currentTrigger = $this.data('contextMenuActive', true);
+ }
+ },
+ // contextMenu right-click trigger
+ mouseup: function(e) {
+ // show menu
+ var $this = $(this);
+ if ($this.data('contextMenuActive') && $currentTrigger && $currentTrigger.length && $currentTrigger.is($this) && !$this.hasClass('context-menu-disabled')) {
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ $currentTrigger = $this;
+ $this.trigger(jQuery.Event("contextmenu", { data: e.data, pageX: e.pageX, pageY: e.pageY }));
+ }
+
+ $this.removeData('contextMenuActive');
+ },
+ // contextMenu hover trigger
+ mouseenter: function(e) {
+ var $this = $(this),
+ $related = $(e.relatedTarget),
+ $document = $(document);
+
+ // abort if we're coming from a menu
+ if ($related.is('.context-menu-list') || $related.closest('.context-menu-list').length) {
+ return;
+ }
+
+ // abort if a menu is shown
+ if ($currentTrigger && $currentTrigger.length) {
+ return;
+ }
+
+ hoveract.pageX = e.pageX;
+ hoveract.pageY = e.pageY;
+ hoveract.data = e.data;
+ $document.on('mousemove.contextMenuShow', handle.mousemove);
+ hoveract.timer = setTimeout(function() {
+ hoveract.timer = null;
+ $document.off('mousemove.contextMenuShow');
+ $currentTrigger = $this;
+ $this.trigger(jQuery.Event("contextmenu", { data: hoveract.data, pageX: hoveract.pageX, pageY: hoveract.pageY }));
+ }, e.data.delay );
+ },
+ // contextMenu hover trigger
+ mousemove: function(e) {
+ hoveract.pageX = e.pageX;
+ hoveract.pageY = e.pageY;
+ },
+ // contextMenu hover trigger
+ mouseleave: function(e) {
+ // abort if we're leaving for a menu
+ var $related = $(e.relatedTarget);
+ if ($related.is('.context-menu-list') || $related.closest('.context-menu-list').length) {
+ return;
+ }
+
+ try {
+ clearTimeout(hoveract.timer);
+ } catch(e) {}
+
+ hoveract.timer = null;
+ },
+
+ // click on layer to hide contextMenu
+ layerClick: function(e) {
+ var $this = $(this),
+ root = $this.data('contextMenuRoot'),
+ mouseup = false,
+ button = e.button,
+ x = e.pageX,
+ y = e.pageY,
+ target,
+ offset,
+ selectors;
+
+ e.preventDefault();
+ e.stopImmediatePropagation();
+
+ // This hack looks about as ugly as it is
+ // Firefox 12 (at least) fires the contextmenu event directly "after" mousedown
+ // for some reason `root.$layer.hide(); document.elementFromPoint()` causes this
+ // contextmenu event to be triggered on the uncovered element instead of on the
+ // layer (where every other sane browser, including Firefox nightly at the time)
+ // triggers the event. This workaround might be obsolete by September 2012.
+ $this.on('mouseup', function() {
+ mouseup = true;
+ });
+ setTimeout(function() {
+ var $window, hideshow;
+
+ // test if we need to reposition the menu
+ if ((root.trigger == 'left' && button == 0) || (root.trigger == 'right' && button == 2)) {
+ if (document.elementFromPoint) {
+ root.$layer.hide();
+ target = document.elementFromPoint(x, y);
+ root.$layer.show();
+
+ selectors = [];
+ for (var s in namespaces) {
+ selectors.push(s);
+ }
+
+ target = $(target).closest(selectors.join(', '));
+
+ if (target.length) {
+ if (target.is(root.$trigger[0])) {
+ root.position.call(root.$trigger, root, x, y);
+ return;
+ }
+ }
+ } else {
+ offset = root.$trigger.offset();
+ $window = $(window);
+ // while this looks kinda awful, it's the best way to avoid
+ // unnecessarily calculating any positions
+ offset.top += $window.scrollTop();
+ if (offset.top <= e.pageY) {
+ offset.left += $window.scrollLeft();
+ if (offset.left <= e.pageX) {
+ offset.bottom = offset.top + root.$trigger.outerHeight();
+ if (offset.bottom >= e.pageY) {
+ offset.right = offset.left + root.$trigger.outerWidth();
+ if (offset.right >= e.pageX) {
+ // reposition
+ root.position.call(root.$trigger, root, x, y);
+ return;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ hideshow = function(e) {
+ if (e) {
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ }
+
+ root.$menu.trigger('contextmenu:hide');
+ if (target && target.length) {
+ setTimeout(function() {
+ target.contextMenu({x: x, y: y});
+ }, 50);
+ }
+ };
+
+ if (mouseup) {
+ // mouseup has already happened
+ hideshow();
+ } else {
+ // remove only after mouseup has completed
+ $this.on('mouseup', hideshow);
+ }
+ }, 50);
+ },
+ // key handled :hover
+ keyStop: function(e, opt) {
+ if (!opt.isInput) {
+ e.preventDefault();
+ }
+
+ e.stopPropagation();
+ },
+ key: function(e) {
+ var opt = $currentTrigger.data('contextMenu') || {},
+ $children = opt.$menu.children(),
+ $round;
+
+ switch (e.keyCode) {
+ case 9:
+ case 38: // up
+ handle.keyStop(e, opt);
+ // if keyCode is [38 (up)] or [9 (tab) with shift]
+ if (opt.isInput) {
+ if (e.keyCode == 9 && e.shiftKey) {
+ e.preventDefault();
+ opt.$selected && opt.$selected.find('input, textarea, select').blur();
+ opt.$menu.trigger('prevcommand');
+ return;
+ } else if (e.keyCode == 38 && opt.$selected.find('input, textarea, select').prop('type') == 'checkbox') {
+ // checkboxes don't capture this key
+ e.preventDefault();
+ return;
+ }
+ } else if (e.keyCode != 9 || e.shiftKey) {
+ opt.$menu.trigger('prevcommand');
+ return;
+ }
+
+ case 9: // tab
+ case 40: // down
+ handle.keyStop(e, opt);
+ if (opt.isInput) {
+ if (e.keyCode == 9) {
+ e.preventDefault();
+ opt.$selected && opt.$selected.find('input, textarea, select').blur();
+ opt.$menu.trigger('nextcommand');
+ return;
+ } else if (e.keyCode == 40 && opt.$selected.find('input, textarea, select').prop('type') == 'checkbox') {
+ // checkboxes don't capture this key
+ e.preventDefault();
+ return;
+ }
+ } else {
+ opt.$menu.trigger('nextcommand');
+ return;
+ }
+ break;
+
+ case 37: // left
+ handle.keyStop(e, opt);
+ if (opt.isInput || !opt.$selected || !opt.$selected.length) {
+ break;
+ }
+
+ if (!opt.$selected.parent().hasClass('context-menu-root')) {
+ var $parent = opt.$selected.parent().parent();
+ opt.$selected.trigger('contextmenu:blur');
+ opt.$selected = $parent;
+ return;
+ }
+ break;
+
+ case 39: // right
+ handle.keyStop(e, opt);
+ if (opt.isInput || !opt.$selected || !opt.$selected.length) {
+ break;
+ }
+
+ var itemdata = opt.$selected.data('contextMenu') || {};
+ if (itemdata.$menu && opt.$selected.hasClass('context-menu-submenu')) {
+ opt.$selected = null;
+ itemdata.$selected = null;
+ itemdata.$menu.trigger('nextcommand');
+ return;
+ }
+ break;
+
+ case 35: // end
+ case 36: // home
+ if (opt.$selected && opt.$selected.find('input, textarea, select').length) {
+ return;
+ } else {
+ (opt.$selected && opt.$selected.parent() || opt.$menu)
+ .children(':not(.disabled, .not-selectable)')[e.keyCode == 36 ? 'first' : 'last']()
+ .trigger('contextmenu:focus');
+ e.preventDefault();
+ return;
+ }
+ break;
+
+ case 13: // enter
+ handle.keyStop(e, opt);
+ if (opt.isInput) {
+ if (opt.$selected && !opt.$selected.is('textarea, select')) {
+ e.preventDefault();
+ return;
+ }
+ break;
+ }
+ opt.$selected && opt.$selected.trigger('mouseup');
+ return;
+
+ case 32: // space
+ case 33: // page up
+ case 34: // page down
+ // prevent browser from scrolling down while menu is visible
+ handle.keyStop(e, opt);
+ return;
+
+ case 27: // esc
+ handle.keyStop(e, opt);
+ opt.$menu.trigger('contextmenu:hide');
+ return;
+
+ default: // 0-9, a-z
+ var k = (String.fromCharCode(e.keyCode)).toUpperCase();
+ if (opt.accesskeys[k]) {
+ // according to the specs accesskeys must be invoked immediately
+ opt.accesskeys[k].$node.trigger(opt.accesskeys[k].$menu
+ ? 'contextmenu:focus'
+ : 'mouseup'
+ );
+ return;
+ }
+ break;
+ }
+ // pass event to selected item,
+ // stop propagation to avoid endless recursion
+ e.stopPropagation();
+ opt.$selected && opt.$selected.trigger(e);
+ },
+
+ // select previous possible command in menu
+ prevItem: function(e) {
+ e.stopPropagation();
+ var opt = $(this).data('contextMenu') || {};
+
+ // obtain currently selected menu
+ if (opt.$selected) {
+ var $s = opt.$selected;
+ opt = opt.$selected.parent().data('contextMenu') || {};
+ opt.$selected = $s;
+ }
+
+ var $children = opt.$menu.children(),
+ $prev = !opt.$selected || !opt.$selected.prev().length ? $children.last() : opt.$selected.prev(),
+ $round = $prev;
+
+ // skip disabled
+ while ($prev.hasClass('disabled') || $prev.hasClass('not-selectable')) {
+ if ($prev.prev().length) {
+ $prev = $prev.prev();
+ } else {
+ $prev = $children.last();
+ }
+ if ($prev.is($round)) {
+ // break endless loop
+ return;
+ }
+ }
+
+ // leave current
+ if (opt.$selected) {
+ handle.itemMouseleave.call(opt.$selected.get(0), e);
+ }
+
+ // activate next
+ handle.itemMouseenter.call($prev.get(0), e);
+
+ // focus input
+ var $input = $prev.find('input, textarea, select');
+ if ($input.length) {
+ $input.focus();
+ }
+ },
+ // select next possible command in menu
+ nextItem: function(e) {
+ e.stopPropagation();
+ var opt = $(this).data('contextMenu') || {};
+
+ // obtain currently selected menu
+ if (opt.$selected) {
+ var $s = opt.$selected;
+ opt = opt.$selected.parent().data('contextMenu') || {};
+ opt.$selected = $s;
+ }
+
+ var $children = opt.$menu.children(),
+ $next = !opt.$selected || !opt.$selected.next().length ? $children.first() : opt.$selected.next(),
+ $round = $next;
+
+ // skip disabled
+ while ($next.hasClass('disabled') || $next.hasClass('not-selectable')) {
+ if ($next.next().length) {
+ $next = $next.next();
+ } else {
+ $next = $children.first();
+ }
+ if ($next.is($round)) {
+ // break endless loop
+ return;
+ }
+ }
+
+ // leave current
+ if (opt.$selected) {
+ handle.itemMouseleave.call(opt.$selected.get(0), e);
+ }
+
+ // activate next
+ handle.itemMouseenter.call($next.get(0), e);
+
+ // focus input
+ var $input = $next.find('input, textarea, select');
+ if ($input.length) {
+ $input.focus();
+ }
+ },
+
+ // flag that we're inside an input so the key handler can act accordingly
+ focusInput: function(e) {
+ var $this = $(this).closest('.context-menu-item'),
+ data = $this.data(),
+ opt = data.contextMenu,
+ root = data.contextMenuRoot;
+
+ root.$selected = opt.$selected = $this;
+ root.isInput = opt.isInput = true;
+ },
+ // flag that we're inside an input so the key handler can act accordingly
+ blurInput: function(e) {
+ var $this = $(this).closest('.context-menu-item'),
+ data = $this.data(),
+ opt = data.contextMenu,
+ root = data.contextMenuRoot;
+
+ root.isInput = opt.isInput = false;
+ },
+
+ // :hover on menu
+ menuMouseenter: function(e) {
+ var root = $(this).data().contextMenuRoot;
+ root.hovering = true;
+ },
+ // :hover on menu
+ menuMouseleave: function(e) {
+ var root = $(this).data().contextMenuRoot;
+ if (root.$layer && root.$layer.is(e.relatedTarget)) {
+ root.hovering = false;
+ }
+ },
+
+ // :hover done manually so key handling is possible
+ itemMouseenter: function(e) {
+ var $this = $(this),
+ data = $this.data(),
+ opt = data.contextMenu,
+ root = data.contextMenuRoot;
+
+ root.hovering = true;
+
+ // abort if we're re-entering
+ if (e && root.$layer && root.$layer.is(e.relatedTarget)) {
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ }
+
+ // make sure only one item is selected
+ (opt.$menu ? opt : root).$menu
+ .children('.hover').trigger('contextmenu:blur');
+
+ if ($this.hasClass('disabled') || $this.hasClass('not-selectable')) {
+ opt.$selected = null;
+ return;
+ }
+
+ $this.trigger('contextmenu:focus');
+ },
+ // :hover done manually so key handling is possible
+ itemMouseleave: function(e) {
+ var $this = $(this),
+ data = $this.data(),
+ opt = data.contextMenu,
+ root = data.contextMenuRoot;
+
+ if (root !== opt && root.$layer && root.$layer.is(e.relatedTarget)) {
+ root.$selected && root.$selected.trigger('contextmenu:blur');
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ root.$selected = opt.$selected = opt.$node;
+ return;
+ }
+
+ $this.trigger('contextmenu:blur');
+ },
+ // contextMenu item click
+ itemClick: function(e) {
+ var $this = $(this),
+ data = $this.data(),
+ opt = data.contextMenu,
+ root = data.contextMenuRoot,
+ key = data.contextMenuKey,
+ callback;
+
+ // abort if the key is unknown or disabled or is a menu
+ if (!opt.items[key] || $this.hasClass('disabled') || $this.hasClass('context-menu-submenu')) {
+ return;
+ }
+
+ e.preventDefault();
+ e.stopImmediatePropagation();
+
+ if ($.isFunction(root.callbacks[key])) {
+ // item-specific callback
+ callback = root.callbacks[key];
+ } else if ($.isFunction(root.callback)) {
+ // default callback
+ callback = root.callback;
+ } else {
+ // no callback, no action
+ return;
+ }
+
+ // hide menu if callback doesn't stop that
+ if (callback.call(root.$trigger, key, root) !== false) {
+ root.$menu.trigger('contextmenu:hide');
+ } else if (root.$menu.parent().length) {
+ op.update.call(root.$trigger, root);
+ }
+ },
+ // ignore click events on input elements
+ inputClick: function(e) {
+ e.stopImmediatePropagation();
+ },
+
+ // hide <menu>
+ hideMenu: function(e, data) {
+ var root = $(this).data('contextMenuRoot');
+ op.hide.call(root.$trigger, root, data && data.force);
+ },
+ // focus <command>
+ focusItem: function(e) {
+ e.stopPropagation();
+ var $this = $(this),
+ data = $this.data(),
+ opt = data.contextMenu,
+ root = data.contextMenuRoot;
+
+ $this.addClass('hover')
+ .siblings('.hover').trigger('contextmenu:blur');
+
+ // remember selected
+ opt.$selected = root.$selected = $this;
+
+ // position sub-menu - do after show so dumb $.ui.position can keep up
+ if (opt.$node) {
+ root.positionSubmenu.call(opt.$node, opt.$menu);
+ }
+ },
+ // blur <command>
+ blurItem: function(e) {
+ e.stopPropagation();
+ var $this = $(this),
+ data = $this.data(),
+ opt = data.contextMenu,
+ root = data.contextMenuRoot;
+
+ $this.removeClass('hover');
+ opt.$selected = null;
+ }
+ },
+ // operations
+ op = {
+ show: function(opt, x, y) {
+ var $this = $(this),
+ offset,
+ css = {};
+
+ // hide any open menus
+ $('#context-menu-layer').trigger('mousedown');
+
+ // backreference for callbacks
+ opt.$trigger = $this;
+
+ // show event
+ if (opt.events.show.call($this, opt) === false) {
+ $currentTrigger = null;
+ return;
+ }
+
+ // create or update context menu
+ op.update.call($this, opt);
+
+ // position menu
+ opt.position.call($this, opt, x, y);
+
+ // make sure we're in front
+ if (opt.zIndex) {
+ css.zIndex = zindex($this) + opt.zIndex;
+ }
+
+ // add layer
+ op.layer.call(opt.$menu, opt, css.zIndex);
+
+ // adjust sub-menu zIndexes
+ opt.$menu.find('ul').css('zIndex', css.zIndex + 1);
+
+ // position and show context menu
+ opt.$menu.css( css )[opt.animation.show](opt.animation.duration);
+ // make options available
+ $this.data('contextMenu', opt);
+ // register key handler
+ $(document).off('keydown.contextMenu').on('keydown.contextMenu', handle.key);
+ // register autoHide handler
+ if (opt.autoHide) {
+ // trigger element coordinates
+ var pos = $this.position();
+ pos.right = pos.left + $this.outerWidth();
+ pos.bottom = pos.top + this.outerHeight();
+ // mouse position handler
+ $(document).on('mousemove.contextMenuAutoHide', function(e) {
+ if (opt.$layer && !opt.hovering && (!(e.pageX >= pos.left && e.pageX <= pos.right) || !(e.pageY >= pos.top && e.pageY <= pos.bottom))) {
+ // if mouse in menu...
+ opt.$menu.trigger('contextmenu:hide');
+ }
+ });
+ }
+ },
+ hide: function(opt, force) {
+ var $this = $(this);
+ if (!opt) {
+ opt = $this.data('contextMenu') || {};
+ }
+
+ // hide event
+ if (!force && opt.events && opt.events.hide.call($this, opt) === false) {
+ return;
+ }
+
+ if (opt.$layer) {
+ // keep layer for a bit so the contextmenu event can be aborted properly by opera
+ setTimeout((function($layer){ return function(){
+ $layer.remove();
+ };
+ })(opt.$layer), 10);
+
+ try {
+ delete opt.$layer;
+ } catch(e) {
+ opt.$layer = null;
+ }
+ }
+
+ // remove handle
+ $currentTrigger = null;
+ // remove selected
+ opt.$menu.find('.hover').trigger('contextmenu:blur');
+ opt.$selected = null;
+ // unregister key and mouse handlers
+ //$(document).off('.contextMenuAutoHide keydown.contextMenu'); // http://bugs.jquery.com/ticket/10705
+ $(document).off('.contextMenuAutoHide').off('keydown.contextMenu');
+ // hide menu
+ opt.$menu && opt.$menu[opt.animation.hide](opt.animation.duration, function (){
+ // tear down dynamically built menu after animation is completed.
+ if (opt.build) {
+ opt.$menu.remove();
+ $.each(opt, function(key, value) {
+ switch (key) {
+ case 'ns':
+ case 'selector':
+ case 'build':
+ case 'trigger':
+ return true;
+
+ default:
+ opt[key] = undefined;
+ try {
+ delete opt[key];
+ } catch (e) {}
+ return true;
+ }
+ });
+ }
+ });
+ },
+ create: function(opt, root) {
+ if (root === undefined) {
+ root = opt;
+ }
+ // create contextMenu
+ opt.$menu = $('<ul class="context-menu-list ' + (opt.className || "") + '"></ul>').data({
+ 'contextMenu': opt,
+ 'contextMenuRoot': root
+ });
+
+ $.each(['callbacks', 'commands', 'inputs'], function(i,k){
+ opt[k] = {};
+ if (!root[k]) {
+ root[k] = {};
+ }
+ });
+
+ root.accesskeys || (root.accesskeys = {});
+
+ // create contextMenu items
+ $.each(opt.items, function(key, item){
+ var $t = $('<li class="context-menu-item ' + (item.className || "") +'"></li>'),
+ $label = null,
+ $input = null;
+
+ item.$node = $t.data({
+ 'contextMenu': opt,
+ 'contextMenuRoot': root,
+ 'contextMenuKey': key
+ });
+
+ // register accesskey
+ // NOTE: the accesskey attribute should be applicable to any element, but Safari5 and Chrome13 still can't do that
+ if (item.accesskey) {
+ var aks = splitAccesskey(item.accesskey);
+ for (var i=0, ak; ak = aks[i]; i++) {
+ if (!root.accesskeys[ak]) {
+ root.accesskeys[ak] = item;
+ item._name = item.name.replace(new RegExp('(' + ak + ')', 'i'), '<span class="context-menu-accesskey">$1</span>');
+ break;
+ }
+ }
+ }
+
+ if (typeof item == "string") {
+ $t.addClass('context-menu-separator not-selectable');
+ } else if (item.type && types[item.type]) {
+ // run custom type handler
+ types[item.type].call($t, item, opt, root);
+ // register commands
+ $.each([opt, root], function(i,k){
+ k.commands[key] = item;
+ if ($.isFunction(item.callback)) {
+ k.callbacks[key] = item.callback;
+ }
+ });
+ } else {
+ // add label for input
+ if (item.type == 'html') {
+ $t.addClass('context-menu-html not-selectable');
+ } else if (item.type) {
+ $label = $('<label></label>').appendTo($t);
+ $('<span></span>').html(item._name || item.name).appendTo($label);
+ $t.addClass('context-menu-input');
+ opt.hasTypes = true;
+ $.each([opt, root], function(i,k){
+ k.commands[key] = item;
+ k.inputs[key] = item;
+ });
+ } else if (item.items) {
+ item.type = 'sub';
+ }
+
+ switch (item.type) {
+ case 'text':
+ $input = $('<input type="text" value="1" name="context-menu-input-'+ key +'" value="">')
+ .val(item.value || "").appendTo($label);
+ break;
+
+ case 'textarea':
+ $input = $('<textarea name="context-menu-input-'+ key +'"></textarea>')
+ .val(item.value || "").appendTo($label);
+
+ if (item.height) {
+ $input.height(item.height);
+ }
+ break;
+
+ case 'checkbox':
+ $input = $('<input type="checkbox" value="1" name="context-menu-input-'+ key +'" value="">')
+ .val(item.value || "").prop("checked", !!item.selected).prependTo($label);
+ break;
+
+ case 'radio':
+ $input = $('<input type="radio" value="1" name="context-menu-input-'+ item.radio +'" value="">')
+ .val(item.value || "").prop("checked", !!item.selected).prependTo($label);
+ break;
+
+ case 'select':
+ $input = $('<select name="context-menu-input-'+ key +'">').appendTo($label);
+ if (item.options) {
+ $.each(item.options, function(value, text) {
+ $('<option></option>').val(value).text(text).appendTo($input);
+ });
+ $input.val(item.selected);
+ }
+ break;
+
+ case 'sub':
+ $('<span></span>').html(item._name || item.name).appendTo($t);
+ item.appendTo = item.$node;
+ op.create(item, root);
+ $t.data('contextMenu', item).addClass('context-menu-submenu');
+ item.callback = null;
+ break;
+
+ case 'html':
+ $(item.html).appendTo($t);
+ break;
+
+ default:
+ $.each([opt, root], function(i,k){
+ k.commands[key] = item;
+ if ($.isFunction(item.callback)) {
+ k.callbacks[key] = item.callback;
+ }
+ });
+
+ $('<span></span>').html(item._name || item.name || "").appendTo($t);
+ break;
+ }
+
+ // disable key listener in <input>
+ if (item.type && item.type != 'sub' && item.type != 'html') {
+ $input
+ .on('focus', handle.focusInput)
+ .on('blur', handle.blurInput);
+
+ if (item.events) {
+ $input.on(item.events);
+ }
+ }
+
+ // add icons
+ if (item.icon) {
+ $t.addClass("icon icon-" + item.icon);
+ }
+ }
+
+ // cache contained elements
+ item.$input = $input;
+ item.$label = $label;
+
+ // attach item to menu
+ $t.appendTo(opt.$menu);
+
+ // Disable text selection
+ if (!opt.hasTypes && $.support.eventSelectstart) {
+ // browsers support user-select: none,
+ // IE has a special event for text-selection
+ // browsers supporting neither will not be preventing text-selection
+ $t.on('selectstart.disableTextSelect', handle.abortevent);
+ }
+ });
+ // attach contextMenu to <body> (to bypass any possible overflow:hidden issues on parents of the trigger element)
+ if (!opt.$node) {
+ opt.$menu.css('display', 'none').addClass('context-menu-root');
+ }
+ opt.$menu.appendTo(opt.appendTo || document.body);
+ },
+ update: function(opt, root) {
+ var $this = this;
+ if (root === undefined) {
+ root = opt;
+ // determine widths of submenus, as CSS won't grow them automatically
+ // position:absolute > position:absolute; min-width:100; max-width:200; results in width: 100;
+ // kinda sucks hard...
+ opt.$menu.find('ul').andSelf().css({position: 'static', display: 'block'}).each(function(){
+ var $this = $(this);
+ $this.width($this.css('position', 'absolute').width())
+ .css('position', 'static');
+ }).css({position: '', display: ''});
+ }
+ // re-check disabled for each item
+ opt.$menu.children().each(function(){
+ var $item = $(this),
+ key = $item.data('contextMenuKey'),
+ item = opt.items[key],
+ disabled = ($.isFunction(item.disabled) && item.disabled.call($this, key, root)) || item.disabled === true;
+
+ // dis- / enable item
+ $item[disabled ? 'addClass' : 'removeClass']('disabled');
+
+ if (item.type) {
+ // dis- / enable input elements
+ $item.find('input, select, textarea').prop('disabled', disabled);
+
+ // update input states
+ switch (item.type) {
+ case 'text':
+ case 'textarea':
+ item.$input.val(item.value || "");
+ break;
+
+ case 'checkbox':
+ case 'radio':
+ item.$input.val(item.value || "").prop('checked', !!item.selected);
+ break;
+
+ case 'select':
+ item.$input.val(item.selected || "");
+ break;
+ }
+ }
+
+ if (item.$menu) {
+ // update sub-menu
+ op.update.call($this, item, root);
+ }
+ });
+ },
+ layer: function(opt, zIndex) {
+ // add transparent layer for click area
+ // filter and background for Internet Explorer, Issue #23
+ var $layer = opt.$layer = $('<div id="context-menu-layer" style="position:fixed; z-index:' + zIndex + '; top:0; left:0; opacity: 0; filter: alpha(opacity=0); background-color: #000;"></div>')
+ .css({height: $win.height(), width: $win.width(), display: 'block'})
+ .data('contextMenuRoot', opt)
+ .insertBefore(this)
+ .on('contextmenu', handle.abortevent)
+ .on('mousedown', handle.layerClick);
+
+ // IE6 doesn't know position:fixed;
+ if (!$.support.fixedPosition) {
+ $layer.css({
+ 'position' : 'absolute',
+ 'height' : $(document).height()
+ });
+ }
+
+ return $layer;
+ }
+ };
+
+// split accesskey according to http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#assigned-access-key
+function splitAccesskey(val) {
+ var t = val.split(/\s+/),
+ keys = [];
+
+ for (var i=0, k; k = t[i]; i++) {
+ k = k[0].toUpperCase(); // first character only
+ // theoretically non-accessible characters should be ignored, but different systems, different keyboard layouts, ... screw it.
+ // a map to look up already used access keys would be nice
+ keys.push(k);
+ }
+
+ return keys;
+}
+
+// handle contextMenu triggers
+$.fn.contextMenu = function(operation) {
+ if (operation === undefined) {
+ this.first().trigger('contextmenu');
+ } else if (operation.x && operation.y) {
+ this.first().trigger(jQuery.Event("contextmenu", {pageX: operation.x, pageY: operation.y}));
+ } else if (operation === "hide") {
+ var $menu = this.data('contextMenu').$menu;
+ $menu && $menu.trigger('contextmenu:hide');
+ } else if (operation) {
+ this.removeClass('context-menu-disabled');
+ } else if (!operation) {
+ this.addClass('context-menu-disabled');
+ }
+
+ return this;
+};
+
+// manage contextMenu instances
+$.contextMenu = function(operation, options) {
+ if (typeof operation != 'string') {
+ options = operation;
+ operation = 'create';
+ }
+
+ if (typeof options == 'string') {
+ options = {selector: options};
+ } else if (options === undefined) {
+ options = {};
+ }
+
+ // merge with default options
+ var o = $.extend(true, {}, defaults, options || {}),
+ $document = $(document);
+
+ switch (operation) {
+ case 'create':
+ // no selector no joy
+ if (!o.selector) {
+ throw new Error('No selector specified');
+ }
+ // make sure internal classes are not bound to
+ if (o.selector.match(/.context-menu-(list|item|input)($|\s)/)) {
+ throw new Error('Cannot bind to selector "' + o.selector + '" as it contains a reserved className');
+ }
+ if (!o.build && (!o.items || $.isEmptyObject(o.items))) {
+ throw new Error('No Items sepcified');
+ }
+ counter ++;
+ o.ns = '.contextMenu' + counter;
+ namespaces[o.selector] = o.ns;
+ menus[o.ns] = o;
+
+ // default to right click
+ if (!o.trigger) {
+ o.trigger = 'right';
+ }
+
+ if (!initialized) {
+ // make sure item click is registered first
+ $(document)
+ .on({
+ 'contextmenu:hide.contextMenu': handle.hideMenu,
+ 'prevcommand.contextMenu': handle.prevItem,
+ 'nextcommand.contextMenu': handle.nextItem,
+ 'contextmenu.contextMenu': handle.abortevent,
+ 'mouseenter.contextMenu': handle.menuMouseenter,
+ 'mouseleave.contextMenu': handle.menuMouseleave
+ }, '.context-menu-list')
+ .on('mouseup.contextMenu', '.context-menu-input', handle.inputClick)
+ .on({
+ 'mouseup.contextMenu': handle.itemClick,
+ 'contextmenu:focus.contextMenu': handle.focusItem,
+ 'contextmenu:blur.contextMenu': handle.blurItem,
+ 'contextmenu.contextMenu': handle.abortevent,
+ 'mouseenter.contextMenu': handle.itemMouseenter,
+ 'mouseleave.contextMenu': handle.itemMouseleave
+ }, '.context-menu-item');
+
+ initialized = true;
+ }
+
+ // engage native contextmenu event
+ $document
+ .on('contextmenu' + o.ns, o.selector, o, handle.contextmenu);
+
+ switch (o.trigger) {
+ case 'hover':
+ $document
+ .on('mouseenter' + o.ns, o.selector, o, handle.mouseenter)
+ .on('mouseleave' + o.ns, o.selector, o, handle.mouseleave);
+ break;
+
+ case 'left':
+ $document.on('click' + o.ns, o.selector, o, handle.click);
+ break;
+ /*
+ default:
+ // http://www.quirksmode.org/dom/events/contextmenu.html
+ $document
+ .on('mousedown' + o.ns, o.selector, o, handle.mousedown)
+ .on('mouseup' + o.ns, o.selector, o, handle.mouseup);
+ break;
+ */
+ }
+
+ // create menu
+ if (!o.build) {
+ op.create(o);
+ }
+ break;
+
+ case 'destroy':
+ if (!o.selector) {
+ $document.off('.contextMenu .contextMenuAutoHide');
+ $.each(namespaces, function(key, value) {
+ $document.off(value);
+ });
+
+ namespaces = {};
+ menus = {};
+ counter = 0;
+ initialized = false;
+
+ $('#context-menu-layer, .context-menu-list').remove();
+ } else if (namespaces[o.selector]) {
+ var $visibleMenu = $('.context-menu-list').filter(':visible');
+ if ($visibleMenu.length && $visibleMenu.data().contextMenuRoot.$trigger.is(o.selector)) {
+ $visibleMenu.trigger('contextmenu:hide', {force: true});
+ }
+
+ try {
+ if (menus[namespaces[o.selector]].$menu) {
+ menus[namespaces[o.selector]].$menu.remove();
+ }
+
+ delete menus[namespaces[o.selector]];
+ } catch(e) {
+ menus[namespaces[o.selector]] = null;
+ }
+
+ $document.off(namespaces[o.selector]);
+ }
+ break;
+
+ case 'html5':
+ // if <command> or <menuitem> are not handled by the browser,
+ // or options was a bool true,
+ // initialize $.contextMenu for them
+ if ((!$.support.htmlCommand && !$.support.htmlMenuitem) || (typeof options == "boolean" && options)) {
+ $('menu[type="context"]').each(function() {
+ if (this.id) {
+ $.contextMenu({
+ selector: '[contextmenu=' + this.id +']',
+ items: $.contextMenu.fromMenu(this)
+ });
+ }
+ }).css('display', 'none');
+ }
+ break;
+
+ default:
+ throw new Error('Unknown operation "' + operation + '"');
+ }
+
+ return this;
+};
+
+// import values into <input> commands
+$.contextMenu.setInputValues = function(opt, data) {
+ if (data === undefined) {
+ data = {};
+ }
+
+ $.each(opt.inputs, function(key, item) {
+ switch (item.type) {
+ case 'text':
+ case 'textarea':
+ item.value = data[key] || "";
+ break;
+
+ case 'checkbox':
+ item.selected = data[key] ? true : false;
+ break;
+
+ case 'radio':
+ item.selected = (data[item.radio] || "") == item.value ? true : false;
+ break;
+
+ case 'select':
+ item.selected = data[key] || "";
+ break;
+ }
+ });
+};
+
+// export values from <input> commands
+$.contextMenu.getInputValues = function(opt, data) {
+ if (data === undefined) {
+ data = {};
+ }
+
+ $.each(opt.inputs, function(key, item) {
+ switch (item.type) {
+ case 'text':
+ case 'textarea':
+ case 'select':
+ data[key] = item.$input.val();
+ break;
+
+ case 'checkbox':
+ data[key] = item.$input.prop('checked');
+ break;
+
+ case 'radio':
+ if (item.$input.prop('checked')) {
+ data[item.radio] = item.value;
+ }
+ break;
+ }
+ });
+
+ return data;
+};
+
+// find <label for="xyz">
+function inputLabel(node) {
+ return (node.id && $('label[for="'+ node.id +'"]').val()) || node.name;
+}
+
+// convert <menu> to items object
+function menuChildren(items, $children, counter) {
+ if (!counter) {
+ counter = 0;
+ }
+
+ $children.each(function() {
+ var $node = $(this),
+ node = this,
+ nodeName = this.nodeName.toLowerCase(),
+ label,
+ item;
+
+ // extract <label><input>
+ if (nodeName == 'label' && $node.find('input, textarea, select').length) {
+ label = $node.text();
+ $node = $node.children().first();
+ node = $node.get(0);
+ nodeName = node.nodeName.toLowerCase();
+ }
+
+ /*
+ * <menu> accepts flow-content as children. that means <embed>, <canvas> and such are valid menu items.
+ * Not being the sadistic kind, $.contextMenu only accepts:
+ * <command>, <menuitem>, <hr>, <span>, <p> <input [text, radio, checkbox]>, <textarea>, <select> and of course <menu>.
+ * Everything else will be imported as an html node, which is not interfaced with contextMenu.
+ */
+
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#concept-command
+ switch (nodeName) {
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#the-menu-element
+ case 'menu':
+ item = {name: $node.attr('label'), items: {}};
+ counter = menuChildren(item.items, $node.children(), counter);
+ break;
+
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-a-element-to-define-a-command
+ case 'a':
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-button-element-to-define-a-command
+ case 'button':
+ item = {
+ name: $node.text(),
+ disabled: !!$node.attr('disabled'),
+ callback: (function(){ return function(){ $node.click(); }; })()
+ };
+ break;
+
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-command-element-to-define-a-command
+
+ case 'menuitem':
+ case 'command':
+ switch ($node.attr('type')) {
+ case undefined:
+ case 'command':
+ case 'menuitem':
+ item = {
+ name: $node.attr('label'),
+ disabled: !!$node.attr('disabled'),
+ callback: (function(){ return function(){ $node.click(); }; })()
+ };
+ break;
+
+ case 'checkbox':
+ item = {
+ type: 'checkbox',
+ disabled: !!$node.attr('disabled'),
+ name: $node.attr('label'),
+ selected: !!$node.attr('checked')
+ };
+ break;
+
+ case 'radio':
+ item = {
+ type: 'radio',
+ disabled: !!$node.attr('disabled'),
+ name: $node.attr('label'),
+ radio: $node.attr('radiogroup'),
+ value: $node.attr('id'),
+ selected: !!$node.attr('checked')
+ };
+ break;
+
+ default:
+ item = undefined;
+ }
+ break;
+
+ case 'hr':
+ item = '-------';
+ break;
+
+ case 'input':
+ switch ($node.attr('type')) {
+ case 'text':
+ item = {
+ type: 'text',
+ name: label || inputLabel(node),
+ disabled: !!$node.attr('disabled'),
+ value: $node.val()
+ };
+ break;
+
+ case 'checkbox':
+ item = {
+ type: 'checkbox',
+ name: label || inputLabel(node),
+ disabled: !!$node.attr('disabled'),
+ selected: !!$node.attr('checked')
+ };
+ break;
+
+ case 'radio':
+ item = {
+ type: 'radio',
+ name: label || inputLabel(node),
+ disabled: !!$node.attr('disabled'),
+ radio: !!$node.attr('name'),
+ value: $node.val(),
+ selected: !!$node.attr('checked')
+ };
+ break;
+
+ default:
+ item = undefined;
+ break;
+ }
+ break;
+
+ case 'select':
+ item = {
+ type: 'select',
+ name: label || inputLabel(node),
+ disabled: !!$node.attr('disabled'),
+ selected: $node.val(),
+ options: {}
+ };
+ $node.children().each(function(){
+ item.options[this.value] = $(this).text();
+ });
+ break;
+
+ case 'textarea':
+ item = {
+ type: 'textarea',
+ name: label || inputLabel(node),
+ disabled: !!$node.attr('disabled'),
+ value: $node.val()
+ };
+ break;
+
+ case 'label':
+ break;
+
+ default:
+ item = {type: 'html', html: $node.clone(true)};
+ break;
+ }
+
+ if (item) {
+ counter++;
+ items['key' + counter] = item;
+ }
+ });
+
+ return counter;
+}
+
+// convert html5 menu
+$.contextMenu.fromMenu = function(element) {
+ var $this = $(element),
+ items = {};
+
+ menuChildren(items, $this.children());
+
+ return items;
+};
+
+// make defaults accessible
+$.contextMenu.defaults = defaults;
+$.contextMenu.types = types;
+
+})(jQuery);