⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actions.js

📁 Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是供中,大型企业来管理其发布在互连网
💻 JS
📖 第 1 页 / 共 4 页
字号:
            }
          }
        }
      }
      src = "cocoon:/remove-shared-text";
    }

    if (doSaveDocument(src + "(" + tempFile.getCanonicalPath() + ")", uri)) 
    {
      // perform workflow action 'save'
      var resourceId = uri.substring(uriPrefix.length);
      var params = new HashMap();
      if (!workflow.invokeStandardUIAction(resourceId, "hippo.cms.save", params, uriPrefix)) {
        cocoon.log.error("actions.js: saveDocument: invokeAction() failed");
      }
    }
    else {
      cocoon.log.error("actions.js: saveDocument: put() failed");
    }
  }
  catch (e) {
    if (e instanceof Throwable) {
      e.printStackTrace();
    }
    else {
      print(e);
    }
    throw e;
  }
  finally {
    if (sr != null) {
      // TODO: handle exceptions
      sr.close();
    }
    if (fw != null) {
      // TODO: handle exceptions
      fw.close();
    }
    if (tempFile != null) {
      tempFile["delete"]();
      tempFile.deleteOnExit();
    }
	cocoon.sendPage("/workbench/blank.html");
  }
}


function doSaveDocument(src, uri)
{
  if (uri.indexOf("?") > 0)
  {
    uri = uri.substr(0, uri.indexOf("?"));
  }

  var result = handleContentModelException(function(){
    usingDocumentDo("", uri, function(document){
      usingInputStreamDo(src, function(input){
        document.setContents(input);
      });
      document.commitChanges();
    });
  });
  return result;

}


/**
 * Called after saving document to repository
 *
 * @param   status   webdav status response
 */
function post_saveDocument(status) {
  if (cocoon.log.isDebugEnabled()) {
    cocoon.log.debug("actions.js: post_saveDocument: status: " + status);
  }
}


/**
 * ...
 *
 * @param   uriPrefix    webdav files URL
 * @param   resourceId   path to the document for which to set the index, relative to uriPrefix
 */
function setIndexOnExistingDocument() {
  var uriPrefix = cocoon.parameters["uriPrefix"];
  var resourceId = cocoon.parameters["resourceId"];
  if (cocoon.log.isDebugEnabled()) {
    cocoon.log.debug("actions.js: setIndexToExistingDocument: uri: " + uriPrefix + resourceId);
  }

  var parentCollection = resourceId.substring(0, resourceId.lastIndexOf('/'));
  if (cocoon.log.isDebugEnabled()) {
    cocoon.log.debug("actions.js: setIndexToExistingDocument: parentCollection: " + parentCollection);
  }

  var result = handleContentModelException(function(){
    usingResourceDo(uriPrefix, resourceId, function(resource){
      resource.saveNewIndexProperty();
      resource.commitChanges();
    });
  });
  if (!result)
  {
    cocoon.log.error("Error setting index on " + uriPrefix + resourceId);
  }

  cocoon.sendPage("/explorer/defaultDetailSource/" + resourceId);
}


/**
 * Replaces the content of a document with an older version of that content.
 */
function replaceWithVersion(uriPrefix, resourceId, params)
{ 
  var versionAsString = params.get("version");
  var version = 0;
  try
  {
    var version = Integer.parseInt(versionAsString);
  }
  catch (e)
  {
    cocoon.log.error("replaceWith version failed because '" + versionAsString + "' is not a valid version number.");
    cocoon.log.error(e);
    return false;
  }
  
  var result = handleContentModelException(function(){
    usingDocumentDo(uriPrefix, resourceId, function(document){
      document.replaceWithVersion(version);
      document.commitChanges();
    });
  });
  return result;
}

function getAssetVersions()
{
  var resourceId = cocoon.parameters["resourceId"];
  var uriPrefix = cocoon.parameters["uriPrefix"];
  var assetUri = uriPrefix + resourceId;
  
  var doc = null;
  
  var getAssetHistoryResult = handleContentModelException(function(){
    usingAssetDo(uriPrefix, resourceId, function(asset){
      doc = asset.getVersionHistory();
      });
    });  
  cocoon.sendPage("assetVersions", {target: assetUri, doc: doc});
}


function getVersions()
{
  var resourceId = cocoon.parameters["resourceId"];
  var uriPrefix = cocoon.parameters["uriPrefix"];
  var baseUri = ContentModelUtil.getHistoryBaseUri(uriPrefix);
  var versionsUri = baseUri + resourceId;
  
  cocoon.sendPage("versions", {target: versionsUri});
}


function unlock(uriPrefix, resourceId, params)
{
  var result = handleContentModelException(function(){
    usingResourceDo(uriPrefix, resourceId, function(resource){
      resource.unlock();
      //resource.commitChanges();
    });
  });
  return result;
}


function closeDocument(uriPrefix, resourceId, params)
{
  var result = handleContentModelException(function(){
    usingDocumentDo(uriPrefix, resourceId, function(document){
      document.unlockAfterEditing();
      //document.commitChanges();
    });
  });
  return result;
}


function unlockDocumentAfterEditing()
{
  var uriPrefix = cocoon.parameters["uriPrefix"];
  var resourceId = cocoon.parameters["resourceId"];
  var result = closeDocument(uriPrefix, resourceId, null);
  cocoon.sendPage("unlock-result", {success:result, status:lastResponseStatus});
}


function moveDocument(uriPrefix, resourceId, params)
{
  return true;
}



/**
 * Sends a pipeline generating different actionlist sources depending
 * on the resource's workflowId and type.
 *
 * @param   uriPrefix    webdav files URL
 * @param   resourceId   path to the document for which to generate action sources, relative to uriPrefix
 * @param   workflowId   workflowId of the resource
 * @param   type         document type
 */
function actionSources() {

  var cache = cocoon.getComponent(Store.ROLE+"/EventAware");
  var uriPrefix = cocoon.parameters["uriPrefix"];
  var resourceId = cocoon.parameters["resourceId"];
  
  var workflowId = ""+WebDAVHelper.propfindAsString(cache,uriPrefix + resourceId, "http://hippo.nl/cms/1.0", "workflowId", authentication.getHttpState());
  var type = ""+WebDAVHelper.propfindAsString(cache,uriPrefix + resourceId, "http://hippo.nl/cms/1.0", "type", authentication.getHttpState());
  
  if (cocoon.request.getAttribute("actionSourcesCache") == null) {
    var actionSourcesCacheFile = Packages.java.io.File.createTempFile("actionSourcesCache", ".xml");
    usingBufferedFileOutputStreamDo(actionSourcesCacheFile, function(outputStream) {

  var uri = uriPrefix + resourceId;
  var liveUri = uriPrefix2liveUriPrefix(uriPrefix) + resourceId;
  
  //print("uri" + uri);
  //print("liveUri" + liveUri);
  //print("resourceId" + resourceId);
  
  switch (workflowId) {
    case "":
    case null:
      // no workflowId
      switch (type) {
        case "":
        case null:
        case "collection":
          // if collection does not exist in live repository, hide it
          if (libio.checkExists(liveUri)) {
              cocoon.processPipelineTo("collectionActions(" + resourceId + ")/addPublishFolderProperties", null, outputStream);
          } else {
              cocoon.processPipelineTo("defaultActions(" + resourceId + ")", null, outputStream);
          }
        break;
        case "asset":
          // these types should not have a workflow so send defaultActions
          cocoon.processPipelineTo("defaultActions(" + resourceId + ")", null, outputStream);
          break;
        case "binary-resource":
          //assets with worlflow
          if (startWorkflowForExistingDocument(uriPrefix, resourceId)) {
            //print("Started new workflow for new binary-resource: " + resourceId)
            renewAvailableActionsCache(resourceId);
            cocoon.processPipelineTo("binaryResourceWorkflowAndActions(" + resourceId + ")", null, outputStream);
          }
          break;
        default:
          // create new workflow for this document
          if (startWorkflowForExistingDocument(uriPrefix, resourceId)) {
            renewAvailableActionsCache(resourceId);
            cocoon.processPipelineTo("defaultAndWorkflowActions(" + resourceId + ")", null, outputStream);
          }
          else {
            cocoon.processPipelineTo("defaultActions(" + resourceId + ")", null, outputStream);
          }
          break;
      }
      break;
    default:
      if (cocoon.request.getAttribute("availableActionsCache") == null)
      {
        renewAvailableActionsCache(resourceId);
      }
      var availableActions = libio.loadDocument("file:///" + cocoon.request.getAttribute("availableActionsCache"));
      if (availableActions != null) {
        var actionNodes = availableActions.getElementsByTagName("workflow-action");
        if (actionNodes.getLength() > 0) {
          // workflow OK, send default + workflow actions
          var actionsPipeline = (type == "binary-resource") ? "binaryResourceWorkflowAndActions" : "defaultAndWorkflowActions"; 
          cocoon.processPipelineTo(actionsPipeline + "(" + resourceId + ")", null, outputStream);
        }
        else {
          var insufficientPrivilegesNodes = availableActions.getElementsByTagName("insufficient-privileges");
          if (insufficientPrivilegesNodes == null || insufficientPrivilegesNodes.getLength() == 0) {
            // invalid workflowId, create new workflow
            if (startWorkflowForExistingDocument(uriPrefix, resourceId)) {
              renewAvailableActionsCache(resourceId);
              cocoon.processPipelineTo("defaultAndWorkflowActions(" + resourceId + ")", null, outputStream);
            }
            else {
              cocoon.processPipelineTo("defaultActions(" + resourceId + ")", null, outputStream);
            }
          }
          else {
            cocoon.processPipelineTo("defaultAndWorkflowActions(" + resourceId + ")", null, outputStream);
          }
        }
      }
      else {
        cocoon.processPipelineTo("defaultActions(" + resourceId + ")", null, outputStream);
      }
  }
    });
    // the request attribute is used in the actions sitemap
    cocoon.request.setAttribute("actionSourcesCache", actionSourcesCacheFile.getCanonicalPath());
  }

  cocoon.sendPage("cachedActions(" + cocoon.request.getAttribute("actionSourcesCache") + ")");
}

// - Helper functions -------------------------------------------------


function renewAvailableActionsCache(resourceId)
{
  var location = cocoon.request.getAttribute("availableActionsCache");
  if (location == null)
  {
    var cacheFile = Packages.java.io.File.createTempFile("availableActionsCache", ".xml");
    location = cacheFile.getCanonicalPath();
     cacheFile.deleteOnExit();
     // the request attribute is used in the actions sitemap
     cocoon.request.setAttribute("availableActionsCache", location);
   }
   if (location != null && location != "")
   {
     var availableActionsCacheFile = new File(location);
     if (availableActionsCacheFile != null)
     {
       usingBufferedFileOutputStreamDo(availableActionsCacheFile, function(outputStream) {
       cocoon.processPipelineTo("/workflow/availableActions/" + resourceId, null, outputStream);
     });
   }
 }
}


function usingBufferedFileOutputStreamDo(file, processor) {
  var outputStream = null;
  try {
    outputStream = new BufferedOutputStream(new FileOutputStream(file));
    processor(outputStream);
  }
  finally {
    if (outputStream != null) {
      outputStream.close();
    }
  }
}

function uriPrefix2liveUriPrefix(uriPrefix) {

  var liveUriPrefix = uriPrefix.replace("preview","www");  //TODO: do this in a clean way
  return liveUriPrefix;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -