taglibinsert.js

来自「jakarta-taglibs」· JavaScript 代码 · 共 201 行

JS
201
字号
/* ====================================================================
 * 
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights 
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:  
 *       "This product includes software developed by the 
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written 
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * file: tagLibInsert.js
 * author: Dan Mandell [dmandell@stanford.edu]
 * -------------------------------------------
 * Implements the insertTag() function for the useTagLibs.html
 * floater of the UltraDev Custom Tag Library extension. Inserts
 * the selected tag at or around the current selection based on the
 * following conditions:
 * 	1: If the tag has body content, tag is wrapped around current
 *	   selection. If the selection is invalid (not completely
 * 	   surrounding any node) the tag is placed around the nearest
 *	   node which entirely encompasses the selection.
 *	2: If the tag has no body content, there are two subcases:
 *		a: If the selection is an insertion point, the tag is
 *		   inserted at the current insertion point.
 * 		b: If the selection exactly encompasses a node with
 *		   content, the inner body content of the node is
 *		   is replaced with the tag.
 *	3. If the tag is a <TR> or a custom tag surrounding a <TR>,
 *	   the tag is wrapped around the <TR> tag. Custom tags cannot
 *	   be wrapped around other custom tags surrounding a <TR> tag
 * 	   because UltraDev returns the same selection offsets for any
 *	   tag between the previous </TR> tag and the current <TR> tag.
 *	4: In all other cases, the tag is place at the start of the
 *	   the selection. If the selection is invalid, the tag is
 *	   inserted before the nearest valid node.
 * The prefix for the inserted tag is obtained from the prefix
 * corresponding to the current tag library in use specified in the
 * namespace instantiation at the top of the JSP.
 */

var MIN_VAL_SIZE = 1; 		// Minimum allowable length of attribute values
				// Default is 1 to prevent empty entries

var PREFIX_MATCH = "prefix=\"";	// text use to find the prefix attribute within 
				// a tag library namespace instantiation


function insertTag() {
	var tagIndex;
	var tagName;
	var allAtts = "";
	var newValue = "";
	var attributes;
	var tagOpen;  // Custom tag opening
	var tagClose; // Custom tag closing
	var noBodyTag; // Custom tag with no body
	var hasBodyContent; // Boolean value, true if tag has body content of any kind
	var origOffsets; // Offsets of the user's original selection
	var node; // currently selected node
	var offsets; // If the user selected a <TR> tag, new offsets containing the <TR> tag
	var theDOM = dw.getDocumentDOM("document");
	var entireDoc = theDOM.documentElement.outerHTML;
	var tldLoc; // location of TLD incantation for current taglib
	var libPrefix; // the prefix of the tag library currently in use

	if (libName == null) return; // disallow selection of empty option
	if (taglibs[libName] == null) { // refresh memory in case cache was purged
		eval(DWfile.read(TL_DATA_LOC + TL_DATA));
	}

	tagIndex = document.selected_tag.selectedIndex;
	tagName = ((taglibs[libName])[tagIndex])[0];
	attributes = ((taglibs[libName])[tagIndex])[2];
	for (var i = 0; i < attributes.length; i++) { 
		// add a leading space for each attribute
		allAtts += " "; 
		// gather values for all required atts
		newValue = prompt("Enter a value for the " + attributes[i] + " attribute") + "\"";
		if (newValue == "null\"") {
			return; // user cancelled
		}
		while(newValue.length <= MIN_VAL_SIZE) {

			newValue = prompt("Required. Enter a value for the " + attributes[i] + " attribute") + "\"";
			if (newValue == "null\"") return; // user cancelled
		}
		allAtts += attributes[i] + "=" + "\"" + newValue;
	}

	hasBodyContent = ((taglibs[libName])[tagIndex])[1];
	libPrefix = getTaglibToPrefix();

	// Find the offsets. If the currently selected tag is <TR>, we must convert the selection
	// to a node, then back to offsets, to compensate for the fact the UltraDev returns
	// the inner HTML of the selection when a <TR> is selected, and the outer HTML in all
	// other cases.

	origOffsets = theDOM.getSelection(true);
	node = theDOM.offsetsToNode(origOffsets[0],origOffsets[origOffsets.length-1]);
	if (node.tagName != "TR") offsets = origOffsets;
	else offsets = theDOM.nodeToOffsets(node);

	if(hasBodyContent == false) { 
		// No body content. Insert after original selection.
		noBodyTag = "<" + libPrefix + ":" + tagName + allAtts + " />";
		if(offsets[0] == theDOM.nodeToOffsets(node)[0] &&
			offsets[1] == theDOM.nodeToOffsets(node)[1] &&
			node.childNodes.length > 0) {  // if we've selected an entire tag with
			//alert("Whole tag selected"); // body content, replace the body content
			node.innerHTML = noBodyTag;    // with the new tag
		}
		else theDOM.insertHTML(noBodyTag, false);
	}

	else {  // Tag has body content. Must insert around current selection.
		// Replace outer HTML of whole document, with the tagOpen and
		// tagClose statements around the currently selected node.

		tagOpen = "<" + libPrefix + ":" + tagName + allAtts + ">";
		tagClose = "</" + libPrefix + ":" + tagName + ">";
		theDOM.documentElement.outerHTML = entireDoc.substring(0,offsets[0]) + tagOpen + 
				entireDoc.substring(offsets[0],offsets[offsets.length-1]) + tagClose +
				entireDoc.substring(offsets[offsets.length-1]);

	}
	dw.setLiveDataMode(true); // re-translate page so new tag shows up
}

/* function: setTaglibToPrefix()
 * -----------------------------
 * Checks the namespace of all tag libraries introduced in the current page.
 * Sets the current tag library selected to the name of the prefix, so when
 * we insert a tag, we use the prefix for the tag library. This practice allows
 * a tag library to be used under any prefix on any page.
 */

function getTaglibToPrefix() {
	var theDOM = dw.getDocumentDOM("document");
	var tldIndex;
	var prefixIndex;
	var childText;
	var prefix;

	for (i = 0; i < theDOM.childNodes.length; i++) {
		childText = theDOM.childNodes[i].outerHTML;
		tldIndex= childText.indexOf(libName + ".tld");
		if (tldIndex != -1) {
			if (childText.indexOf(PREFIX_MATCH) != -1) {
				prefix = childText.substring(childText.indexOf(PREFIX_MATCH) + 
					PREFIX_MATCH.length, childText.lastIndexOf("\""));
				// now the name of the current tag library is set to the taglib's prefix
				break;
			}
		}
	}
	return prefix;
}

⌨️ 快捷键说明

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