resource-selector.js

来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 213 行

JS
213
字号
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
 * Javascript code for the resource selector frame.
 *
 * TODO: make it more configurable (accepted resource type(s), perspective to select from)
 */

var resourceSelector = {}

resourceSelector.destination = null;
resourceSelector.otherdestination = null;
resourceSelector.otherdestinations = null;
resourceSelector.visible = false;
resourceSelector.workbench = top.frames["topframe"];  //refactor name
resourceSelector.destinationPerspective = null;
resourceSelector.selectionPerspective = null;


/**
 * Selects a resource.
 *
 * @param   path      path of selected resource relative to filespath
 * @param   caption   caption of selected resource
 */
resourceSelector.update = function(path, caption)
{
  if (this.visible)
  {
    document.getElementById('selectedResourcePath').value = path;
    document.getElementById('selectedResourceCaption').value = caption;
  }
}

resourceSelector.getContentType = function(source)
{

	 var eleVal = source.toLowerCase();
	 var newVal = "";
	 
	 if(eleVal.lastIndexOf(".jpg") > 0 || eleVal.lastIndexOf(".jpeg") > 0)
	 {
	 	 newVal = "image/jpeg";
	 }
	 else if(eleVal.lastIndexOf(".gif") > 0)
	 {
	 	 newVal = "image/gif";
	 }	 
	 else if(eleVal.lastIndexOf(".tif") > 0)
	 {
	 	 newVal = "image/tiff";
	 }	 
	 else if(eleVal.lastIndexOf(".bmp") > 0)
	 {
	 	 newVal = "image/bmp";
	 }	 
	 else if(eleVal.lastIndexOf(".png") > 0)
	 {
	 	 newVal = "image/png";
	 }	 	 
  return newVal;
}

/**
 * Sets the selected resource location as value of the destination form field.
 */
resourceSelector.useSelectedResource = function()
{
  if (this.destination != null)
  { 
  	 var currentValue = document.getElementById('selectedResourcePath').value;
  	 if(currentValue.indexOf('/content/') >= 0)
  	 {
  	 	 currentValue = currentValue.replace('/content/','');
		  	 if(currentValue.indexOf('.xml') >= 0)
		  	 {
		   	 	currentValue = currentValue.replace('.xml','');
		  	 }
						this.otherdestination.src = '/editing/cf/showtitle/content/' + currentValue + '.xml';
  	 }
  	 if(currentValue.indexOf('/binaries/') >= 0)
  	 {
  	 	 currentValue = currentValue.replace('/binaries/','');
  	 }
  	 if(currentValue.indexOf('/imageset/') >= 0)
  	 {
  	 	 this.otherdestination.src = currentValue;
  	 	 currentValue = currentValue.replace('/imageset/','');
  	 	 currentValue = currentValue.replace('.xml','');
  	 }
  	 if(currentValue.indexOf('/imageresources/') >= 0)
  	 {
      this.otherdestination.src = currentValue;
  	 	 currentValue = currentValue.replace('/imageresources/','');
      this.typedestination.value = this.getContentType(currentValue);
  	 }
    this.destination.value = currentValue;
  }
  
  this.deactivate();
}


/**
 * Opens the selected location in the document view.
 */
resourceSelector.lookupSelectedResource = function()
{
  var selection = document.getElementById('selectedResourcePath').value;
  var event = "selectSource";
  if (selection.indexOf("/imageresources/") == 0)
  {
    event = "images" + event;
  }
  if (selection.indexOf("/imagesets/") == 0)
  {
    event = "imageset" + event;
  }
  if (selection.indexOf("/binaries/") == 0)
  {
    event = "asset" + event;
  }
  if (selection != null && selection != "")
  {
    fireEvent(event, [{key:'url', val:selection}]);
  }
  else
  {
    fireEvent("selectSource", [{key:'url', val:"/content/"}]);
  }
}


/**
 * Deactivates the selector frame.
 */
resourceSelector.cancel = function()
{
  this.deactivate();
}


/**
 * Opens the resource selector frame and set the destination form field.
 * TODO: make destinationPerspective and selectionPerspective configurable
 *
 * @param   destination   form field object to set the value of
 */
resourceSelector.activate = function(list)
{

  this.destination = list[0];
  this.otherdestination = list[1];
  this.destinationPerspective = "editing";
  this.selectionPerspective = list[2];
  this.typedestination = list[3];
  
  if (!this.visible)
  {
    this.workbench.showResourceSelector();
    this.visible = true;
  }

/*  if (this.destination != null && this.destination.value != null && this.destination.value != "")
  {
    // use existing selection
    this.update(this.destination.value, "");
    this.lookupSelectedResource();
  }
  else
  {*/
    this.workbench.Application.showMenu(this.selectionPerspective);

    if(this.selectionPerspective=='search' && this.destination.id.indexOf('images') > 0)
    { var params;
      this.workbench.Application.sm.lookup('framework.eventmanager').fireEvent("clearView", params);
    }
    
/*  }*/
}


/**
 * Closes the resource selector frame and switches back to the perspective
 * from which it was opened.
 */
resourceSelector.deactivate = function()
{
  this.workbench.hideResourceSelector();
  this.visible = false;
  this.destination = null;
  document.getElementById('selectedResourcePath').value = "";
  document.getElementById('selectedResourceCaption').value = "";

  // switch back to original perspective
  this.workbench.workbench.showMenu(this.destinationPerspective);
  this.destinationPerspective = null;
  this.selectionPerspective = null;
}

⌨️ 快捷键说明

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