tagedit.js

来自「jakarta-taglibs」· JavaScript 代码 · 共 467 行 · 第 1/2 页

JS
467
字号
/* ====================================================================
 * 
 * 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: tagEdit.js
 * author: Dan Mandell [dmandell@stanford.edu]
 * -------------------------------------------
 * The JavaScript implementation of the tagEdit.html floater.
 * Uses the Dreamweaver GridControl class to display the attribute/
 * value pairs of the currently selected custom tag in the Edit Tag
 * floater. Provides functionality for adding/removing/altering
 * the attribute/value pairs, and removing the currently selected
 * custom tag.
 */

var selTaglib; // the name of the currently selected tag library
var prefix; // the name of the currently selected prefix
var tag;
var gc; // the GridControl (visual interface for floater)
var GC_NAME = "att_grid"; // name of the GridControl
var floater = "tagEdit";
var NO_CT = "No custom tag selected";
var NO_CT_SELECTED = new Array(new Array(NO_CT));
var TL_DATA = "tagLibData.js";
var TL_DATA_LOC = dw.getConfigurationPath() + "/Commands/customTags/";
var PREFIX_MATCH = "prefix=";	 // text used to find the prefix attribute within 
			        // a tag library namespace instantiation

var URI_MATCH = "uri=";		// used to find uri attribute within namespace instantiation
var tagName; // the name of the currently selected custom tag
var attlist; // the attributes of the current custom tag
var bodyContent; //boolean, true if the current tag allows any body content

var content = new Array();
gc = new GridControl(GC_NAME);
populateGrid(); // initialize the GridControl

/* function: isAvailableInCodeView()
 * ---------------------------------
 * Called by UltraDev. Tells UltraDev that the floater should be
 * available when the user is in "Code View" mode.
 */

function isAvailableInCodeView() {
	return true;
}

/* function: selectionChanged()
 * ----------------------------
 * Called by UltraDev whenever the user changes the selection.
 * NOTE: UltraDev does not call selectionChanged() when the selection
 * is outside of the <BODY> tag, unless:
 * 1: The user selects one of the Head Content icons, or
 * 2: Previous selection was one of the Head Content icons.
 */

function selectionChanged() {
	var theDOM = dw.getDocumentDOM();
	
	if (theDOM != null) {
		tag = null; 
		getCurTag();
		populateGrid(content);
	}
}

/* function: applyEdits()
 * ----------------------
 * Replaces the opening tag of the currently selected custom tag
 * with a new string that contains all the attribute/value pairs
 * used in the Edit Tag floater.
 */

function applyEdits() {
	if (attlist != null) { // there is a custom tag selected
		var newTag = "<" + prefix + ":" + tagName;
		var content = gc.getContents();
		var theDOM = dw.getDocumentDOM();
		var oldOuterHTML = theDOM.documentElement.outerHTML;
		var offsets = theDOM.nodeToOffsets(tag);
		var beforeNode = oldOuterHTML.substring(0,offsets[0]);
		var afterNode = oldOuterHTML.substring(offsets[0] + tag.outerHTML.indexOf(">") + 1);
		var newOuterHTML;

		for (i = 0; i < attlist.length; i++) {
			if(content[i][1] != "") {
				newTag += " " + attlist[i] + "=\"" + content[i][1] + "\"";
			}
		}
		newTag += ((bodyContent == true) ? ">" : " />");
		newOuterHTML = beforeNode + newTag + afterNode;
		theDOM.documentElement.outerHTML = newOuterHTML;
		if (offsets[0] > theDOM.nodeToOffsets(theDOM.body)[0]) {
			if (tagName == "MM:BEGINLOCK") {
				theDOM.setSelection(offsets[0],offsets[0]+1);
				theDOM.setSelectedNode(theDOM.getSelectedNode());
			}

		}  // update selection if after <BODY> and tag is locked,
       		  // but otherwise do not, since dw may error
	}
}

/* function: reset()
 * -----------------
 * Sets the values of all attributes in the Edit Tag floater to
 * the values from the HTML source after the most recent call to
 * applyEdits().
 */

function reset() {
	populateGrid();
}

/* function: removeTag()
 * ---------------------
 * Removes the currently selected custom tag from the HTML source.
 * If the tag is locked and it has body content, UltraDev only
 * locks the opening tag, dissociating it from the closing tag.
 * To find the closing tag, we find the closing tag by extracting the
 * name of the current tag from the "orig" attribute of the locked
 * tag (which contains the HTML of the original opening tag before it
 * was locked) and searching for the closing tag in all siblings of the
 * current tag located in the DOM after the current tag.
 * Once we have the location of the opening tag and closing tag (if
 * applicable), we generate a new DOM source by concatenating the
 * substrings of all source around the locations we do not want.
 * Generating such a new source is the only way to guarantee we
 * are affecting the untranslated source (locked tags are translated
 * into new source which is displayed in the design window).
*/

function removeTag() {
	if (attlist != null) {
		var theDOM = dw.getDocumentDOM();
		var oldOuterHTML = theDOM.documentElement.outerHTML;
		var offsets = theDOM.nodeToOffsets(tag);
		var beforeNode = oldOuterHTML.substring(0,offsets[0]);
		var afterNode = oldOuterHTML.substring(offsets[1]);
		var sibNodes = tag.parentNode.childNodes; //the sibling tags of the current tag
		var escapedText; //the text of the currently selected tag
		var closingName; //the text of the closing tag for the currently selected tag
		var beforeClose; //the offset of the closing tag
		var curOuter; //outer HTML of currently tested sibling node
		var curSibOffsets; //offsets of currently tested sibling node
		var curSelection = theDOM.getSelection(); //offsets of the current selection
		var orig = unescape(tag.getAttribute("ORIG"));



		if (tag.tagName == "MM:BEGINLOCK") { // tag is locked
			escapedText = unescape(tag.getAttribute("ORIG")); 
			closingName = "</" + escapedText.split(/<\s*|\s+/)[1] + ">";
			if (bodyContent) {
				for (i = 0; i < sibNodes.length; i++) {
					curOuter = sibNodes[i].outerHTML;
					curSibOffsets = theDOM.nodeToOffsets(sibNodes[i]);					
					if (curOuter.substring(curOuter.length - (closingName.length)) == closingName &&
						curSibOffsets[0] >= offsets[0]) {
						beforeClose = theDOM.nodeToOffsets(sibNodes[i])[1] - closingName.length;
						break;	
					}
				}
				if (i == sibNodes.length) {
					alert("Could not find closing tag for current tag. Remove tag manually.");
					return;
				}
				else {
					theDOM.documentElement.outerHTML = oldOuterHTML.substring(0,offsets[0]) +
					oldOuterHTML.substring(offsets[1],beforeClose) +
					oldOuterHTML.substring(beforeClose + closingName.length);
					//tag = null;
					theDOM.setSelection(offsets[0] + (offsets[1]-offsets[0]), beforeClose);
				}
			}
			else {
				theDOM.documentElement.outerHTML = oldOuterHTML.substring(0,offsets[0]) + 
					oldOuterHTML.substring(offsets[1]);
				//tag = null;
				theDOM.setSelection(offsets[0], offsets[0]);
			}	  
		}

⌨️ 快捷键说明

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