forms-lib.js

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

JS
361
字号
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* 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.
*/
/**
 * Runtime JavaScript library for Cocoon forms.
 *
 * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
 * @version CVS $Id: forms-lib.js 9282 2007-12-13 13:54:13Z nvankampenhout $
 */

// Handlers that are to be called in the document's "onload" event
var forms_onloadHandlers = new Array();

function forms_onload() {
	  for (var i = 0; i < forms_onloadHandlers.length; i++) {
        forms_onloadHandlers[i].forms_onload();
    }
    // Clear it (we no more need them)
    forms_onloadHandlers = null;
}

// Handlers that are to be called in form's "onsubmit" event
//FIXME: this single var implies only one form per page, and needs to be
//       visited if we decide to support several forms per page.
var forms_onsubmitHandlers = new Array();

function forms_onsubmit() {
    if (forms_onsubmitHandlers == null) {
        alert("onsubmit called twice!");
    }

    for (var i = 0; i < forms_onsubmitHandlers.length; i++) {
        forms_onsubmitHandlers[i].forms_onsubmit();
    }
    // clear it
    //forms_onsubmitHandlers = null;
}

//
function forms_reset(){

}

/**
 * Submit the form containing an element, also storing in the hidden
 * 'forms_submit_id' field the name of the element which triggered the submit.
 */
function forms_submitForm(element, name) {
    if (name == undefined) {
        name = element.name;
    }
    
    var form = forms_getForm(element);
    if (form == null) {
        alert("Cannot find form for " + element);
    } else {
        form["forms_submit_id"].value = name;
        // FIXME: programmatically submitting the form doesn't trigger onsubmit ? (both in IE and Moz)
        forms_onsubmit();
	      window.onunload();
        form.submit();
    }
}

/**
 * Crawl the parents of an element up to finding a form.
 */
function forms_getForm(element) {
    while(element != null && element.tagName != "FORM") {
        element = element.parentNode;
    }
    return element;
}

/**
 * Move a named element as an immediate child of the <body> element.
 * This is required for help popups inside <wi:group> tabs. The reason is that CSS positioning
 * properties ("left" and "top") on a block with a "position: absolute" are actually relative to
 * the nearest ancestor that has a position of "absolute", "relative" or "fixed".
 * See http://www.w3.org/TR/CSS21/visudet.html#containing-block-details $4
 */

function forms_moveInBody(element) {
    element.parentNode.removeChild(element);
    document.body.appendChild(element);
}

/**
 * Create a popup window for a named element.
 *
 * @param id the ID of the element to make a popup with.
 */
function forms_createPopupWindow(id) {
    var result = new PopupWindow(id);
    result.autoHide();
    // add to onload handlers
    result.forms_id = id;
    result.forms_onload = function() {
        forms_moveInBody(document.getElementById(this.forms_id));
    }
    forms_onloadHandlers.push(result);
    return result;
}


function forms_createOptionTransfer(id, submitOnChange) {
    var result = new OptionTransfer(document.getElementById(id + ".unselected"), document.getElementById(id));
    result.setAutoSort(true);
    // add to onload handlers
    result.forms_id = id;
    result.forms_onload = function() {
        var form = forms_getForm(document.getElementById(this.forms_id));
        this.init(form);
        sortSelect(this.left);
        sortSelect(this.right);
    }
    result.submitOnChange = submitOnChange;
    result.forms_transferLeft = function() {
        this.transferLeft();
        if (this.submitOnChange) {
            forms_submitForm(document.getElementById(this.forms_id));
        }
    }
    result.forms_transferRight = function() {
        this.transferRight();
        if (this.submitOnChange) {
            forms_submitForm(document.getElementById(this.forms_id));
        }
    }
    result.forms_transferAllLeft = function() {
        this.transferAllLeft();
        if (this.submitOnChange) {
            forms_submitForm(document.getElementById(this.forms_id));
        }
    };
    result.forms_transferAllRight = function() {
        this.transferAllRight();
        if (this.submitOnChange) {
            forms_submitForm(document.getElementById(this.forms_id));
        }
    };
    forms_onloadHandlers.push(result);
    
    // add to onsubmit handlers
    result.forms_onsubmit = function() {
        // Select all options in the "selected" list to that
        // its values are sent.
        selectAllOptions(this.right);
    }
    forms_onsubmitHandlers.push(result);
    return result;
}


/**
 * Show a tab in a <wi:group>
 *
 * @param tabgroup (string) name of the <wi:group>
 * @param idx (integer) index of the selected tab
 * @param length (integer) total number of tabs
 * @param state (string, optional) name of the input storing the tabgroup state
 */
function forms_showTab(tabgroup, idx, length, state) {
    for (var i = 0; i < length; i++) {
        // Change tab status (selected/unselected)
        var tab = document.getElementById(tabgroup + "_tab_" + i);
        if (tab != null) {
            tab.className = (i == idx) ? 'forms-tab forms-activeTab': 'forms-tab';
        }
        // Change tab content visibilty
        var tabitems = document.getElementById(tabgroup + "_items_" + i);
        if (tabitems != null) {
            tabitems.style.display = (i == idx) ? '' : 'none';
        }
    }
    // Change state value
    if (state.length > 0) {
        document.forms[0][state].value = idx;
    }
}

var currentItem = "";
var showImagePreview = false;
var showImageSetPreview = false;
var showTitle = false;
var childWindow;

function forms_getImage(object)
{
	 //var field = document.getElementById(currentItem);
 	currentItem.value = object.f_url;
 	childWindow.close();
}

function forms_getLink(object)
{ 
	 //var field = document.getElementById(currentItem);
 	currentItem.value = object;
 	childWindow.close();
 	if(showTitle)
 	{
 		showTitle = false;
 		forms_previewTitle(currentItem);
 	}
 	if(showImagePreview) {
 	  showImagePreview = false;
 	  forms_previewImage(currentItem);
 	  forms_setContentType(currentItem);
 	}
 	if(showImageSetPreview) {
 	  showImageSetPreview = false;
 	  forms_previewImageSet(currentItem);
 	} 	
}

function forms_insertImage(element)
{ 
	 currentItem = element;
	 childWindow = window.open('/explorer/resource-picker/binaries?mode=html', 'nw', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=400');
	 showImagePreview = true;
  //childWindow = window.open('/editing/cf/insert_image.html','picker','width=400,height=338,scrollbars=yes,resizable=yes,status=yes');
}
function forms_createImagePreview(id){
    var result = new imagePreview();
    // add to onload handlers
    result.forms_id = id;
    result.forms_onload = function() {
        forms_previewImage(document.getElementById(this.forms_id));
    }
    forms_onloadHandlers.push(result);
}

function forms_createImageSetPreview(id){
    var result = new imagePreview();
    // add to onload handlers
    result.forms_id = id;
    result.forms_onload = function() {
        forms_previewImageSet(document.getElementById(this.forms_id));
    }
    forms_onloadHandlers.push(result);
}

function imagePreview() {}


function forms_previewTitle(element)
{ 
	var title = document.getElementById(element.id + '-title');
	title.src = '/editing/cf/showtitle/content/' + element.value + '.xml'; 
}

function forms_setContentType(element)
{
	 var eleId = element.id;
	 var eleVal = element.value.toLowerCase();
	 
	 var tempId = eleId.substring(0,eleId.lastIndexOf('src'));
	 tempId = tempId + "type";
	 var myField = document.getElementById(tempId);
	 if(eleVal.lastIndexOf(".jpg") > 0 || eleVal.lastIndexOf(".jpeg") > 0)
	 {
	 	 myField.value = "image/jpeg";
	 }
	 else if(eleVal.lastIndexOf(".gif") > 0)
	 {
	 	 myField.value = "image/gif";
	 }	 
	 else if(eleVal.lastIndexOf(".tif") > 0)
	 {
	 	 myField.value = "image/tiff";
	 }	 
	 else if(eleVal.lastIndexOf(".bmp") > 0)
	 {
	 	 myField.value = "image/bmp";
	 }	 
	 else if(eleVal.lastIndexOf(".png") > 0)
	 {
	 	 myField.value = "image/png";
	 }	 	 
	 
}

function forms_previewImageSet(element)
{ 
	currentItem = element;
	var imgFrame = document.getElementById(element.id + '-preview');
	if(element.value!='')
	{
	  imgFrame.src = '/imageset/' + element.value + '.xml'; 	
	}
	else
	{
		
	}
	
}

function forms_previewImage(element)
{ 
	var img = document.getElementById(element.id + '-image');
	img.src = element.value;
}

function forms_parentResize(width,height)
{
	var imgFrame = document.getElementById(currentItem.id + '-preview');
	imgFrame.width = width;
	imgFrame.height = height;
}

function forms_insertImgSet(element)
{
	 childWindow = window.open('/explorer/resource-picker/imageset/?mode=html', 'nw', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=400');
	 currentItem = element;
	 showImageSetPreview = true;
}

function forms_insertLink(element)
{ 
  	 
	 childWindow = window.open('/explorer/resource-picker/content/?mode=html', 'nw', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=400');
	 currentItem = element;
	 showTitle = true;
	 
  //childWindow = window.open('/editing/cf/insert_link.html','picker','width=410,height=120,scrollbars=yes,resizable=yes,status=yes');  
  //childWindow = window.open("/explorer/htmlassetpicker/content/","picker",'width=400,height=400,scrollbars=yes,resizable=yes,status=yes');  

}

function forms_setCheckboxesState(containerElementId, state)
{
  var containerElement = document.getElementById(containerElementId);
  var inputs = containerElement.getElementsByTagName("input");
  for (var i = 0; i < inputs.length; i++)
  {
    inputs[i].checked = state;
  }
}

function forms_selectAllCheckboxes(containerElementId)
{
  forms_setCheckboxesState(containerElementId, true);
}

function forms_unselectAllCheckboxes(containerElementId)
{
  forms_setCheckboxesState(containerElementId, false);
}

⌨️ 快捷键说明

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