tagedit.js

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

JS
467
字号
		else { // tag is unlocked
			if(!bodyContent)
				theDOM.documentElement.outerHTML = beforeNode + afterNode;
			else {
				tag.outerHTML = tag.innerHTML
			}
			//tag = null; 
			//theDOM.setSelectedNode(offsets[0],offsets[0]);
			dw.setLiveDataMode(true);
		}
	}
}

/* function: getCurTag()
 * ---------------------
 * Sets the global tag variable to the DOM node of the currently
 * selected tag (which may be any tag, custom or otherwise).
 * UltraDev will raise JavaScript errors if we attempt to convert
 * a pair offsets to a node in many cases, including the following:
 * 1: Current selection begins at the end of the page's <BODY> tag
 *    or ends at the beginning of the </BODY> tag.
 * 2: Current selection is inside the <BODY> tag, but there are no
 *    children of the <BODY> tag.
 * We work around these errors by setting the current tag to
 * null under these cases, so the floater will not be populated.
 * Additionally, if the cursor is immediately before an opening or
 * closing tag, UltraDev reports the current tag as the tag following 
 * the cursor. If we find these cases we also set tag to null, so the
 * floater is not populated with an unselected tag.
 */

function getCurTag() {
	var theDOM = dw.getDocumentDOM();
	var selection = theDOM.getSelection();
	var validOpen;  // to test that selection is not the last offset of the <BODY> tag
	var validClose; // to test that selection is not the first offset of the </BODY> tag
	var selection = theDOM.getSelection(true); // offsets of the current selection
	var selectedNode; // currently selected node
	var tableNode; // dummy node to test if current node is a <TR> or <TD> tag
	var selNodeOffsets; // currently selected node's offsets
	var bodyNode = theDOM.body; // the <BODY> tag
	var bodyChilds = bodyNode.childNodes; // the children of the <BODY> tag

	if (selection[selection.length - 1] + 1 < (theDOM.nodeToOffsets(bodyNode)[0])) {
		selectedNode = theDOM.offsetsToNode(selection[0],selection[0]);
		tag = selectedNode;
	}

	else if (bodyNode.hasChildNodes() && 
		!(selection[0] == selection[selection.length - 1] && 
		bodyChilds[bodyChilds.length - 1].nodeType == Node.TEXT_NODE && 		   // if body's last child is a text node, and selection is
		selection[0] > (theDOM.nodeToOffsets(bodyChilds[bodyChilds.length - 1]))[0]))     // insertion right after the text node, dw raises a js error
	{										       	  		   
		validOpen = theDOM.nodeToOffsets(bodyChilds[0])[0];		       
		validClose = theDOM.nodeToOffsets(bodyChilds[bodyChilds.length - 1])[1];

		if (selection[0] >= validOpen && selection[selection.length - 1] <= validClose && selection[0] + 1 < validClose) {
			selectedNode = theDOM.getSelectedNode();
			selNodeOffsets = theDOM.nodeToOffsets(selectedNode);		
	
			if ((selection[0] == selection[selection.length - 1]) && ((selection[0] == selNodeOffsets[0]) ||  			   // make sure we're not right before an opening tag
				(selectedNode.hasChildNodes() && selection[0] == (selNodeOffsets[1] - (selectedNode.tagName.length + 3))))) {     // make sure we're not right before a closing tag
				tag = null; //current selection is cursor right before an opening or closing tag. Don't select.   
			}
			else {
				tableNode = theDOM.offsetsToNode(selection[0],selection[selection.length - 1]);										
				if (tableNode.tagName == "TR" || tableNode.tagName == "TD" && theDOM.nodeToOffsets(tableNode)[0] < selection[0]) { // dw will give us a <TD> or <TR> tag if a custom tag surrounding		       
					tag = tableNode.parentNode;		       	          						  // one of these is selected, so use the parent tag.
				}
				else {
					tag = theDOM.offsetsToNode(selection[0], selection[0]+1); // set tag to node surrounding current selection
				}
			}
		}
	}
	else tag = null; //populateGrid() will call noCTSelected()
}

/* function: populateGrid()
 * ------------------------
 * Populates the Edit Tag floater with the attribute/value pairs
 * of the currently selected custom tag. First sets "orig" to the 
 * opening tag of the currently selected tag. If current tag is
 * locked, uses the untranslated source by calling unescape().
 * Then calls tagIsCustom to determine if the current tag is a
 * custom tag. If so, populates the floater with the tag's
 * attribute/value pairs.
*/

function populateGrid() {
	var orig; 	 // the tag outerHTML inside a locked tag
	var pairs;	// the array of attribute value pairs to populate the control grid

	if (tag != null && tag.outerHTML != null) { 		
		if (tag.tagName != "MM:BEGINLOCK" && tagIsCustom(tag.outerHTML)) { // see if unlocked tag is custom
			pairs = new Array(attlist.length);
			for (var i = 0; i < attlist.length; i++) {
				var userEntry = tag.getAttribute(attlist[i]);
				if (userEntry == null) userEntry = "";  // can't send null to the control grid
				pairs[i] = new Array(attlist[i],userEntry);
				userEntry = "";
			}
			gc.setContents(pairs);
		}
		else if (tag.tagName == "MM:BEGINLOCK")	{	// see if locked tag is custom
			orig = unescape(tag.getAttribute("ORIG"));
			if (tagIsCustom(orig)) {
				pairs = new Array(attlist.length);
				for (var i = 0; i < attlist.length; i++) {
					var userEntry = getParam(orig,(attlist[i]));
					pairs[i] = new Array(attlist[i],userEntry);
					userEntry = "";
				}
				gc.setContents(pairs);
			}
			else { // no custom tag selected
				noCTSelected();
			}
		}

		else { // no custom tag selected
			noCTSelected();
		}
	}
}


/* function: tagIsCustom(orig)
 * ---------------------------
 * Checks to see if the supplied tag is one of the currently loaded 
 * custom tags. First, calls getLibNameFromPrefix() to see if the
 * prefix of the current tag corresponds to a tag custom tag library.
 * If so, it takes the tag library name and checks if it is in
 * the "taglibs" global associative array. If so, it fills the 
 * attlist (attribute list) and bodyContent variables with the tag's 
 * info and returns true. If not found but name contains a colon (:),
 * reload the taglibs array from the tagLibData file, since the user
 * may have added a new tag library or purged the cache. In all other
 * cases, do nothing and return false.
 */

function tagIsCustom(orig) {
	var token;
	var taginfo = new Array(2); // an array containing exactly the prefix and name of the tag
	var curTagName;
	
	if (taglibs == null) { // refresh memory in case cache was purged
		eval(DWfile.read(TL_DATA_LOC + TL_DATA));
	}

	token = orig.split(" ")[0];
	taginfo = token.substring(1).split(":");
	prefix = taginfo[0];
	tagName = taginfo[1];
	selTaglib = getLibnameFromPrefix(prefix);
	if (selTaglib == null) return false; // there is no namespace instantiation for this prefix

	if(token.indexOf(":") > 0) {	
		if (taglibs[selTaglib] == null) { 
			  // tag not found in memory, but is probably a custom tag
			 // reload taglib file in case user added
			// a taglib or purged cache	

			eval(DWfile.read(TL_DATA_LOC + TL_DATA));
			if (taglibs[selTaglib] == null) { // taglib still not found, so its not custom
				return false;
			}
		}
	}
	else return false; // Has no colon (:) or has no namespace
			  // instantiation. Cannot be a custom tag

	 // We know a namespace instantiation exists for this tag.
        // See if its in the taglibs associative array.
	for (var i = 0; i < taglibs[selTaglib].length; i++) {
		var curTagName = ((taglibs[selTaglib])[i])[0];
		if (curTagName == tagName) {
			attlist = ((taglibs[selTaglib])[i])[3];
			bodyContent = ((taglibs[selTaglib])[i])[1];
			return true;
		}
	}
	return false; // tagLibrary does not exist locally
}

/* function: noCTSelected()
 * ------------------------
 * Called when no custom tag is selected. Checks if the GridControl
 * contents have already been set with the NO_CT message. If not,
 * sets the content to the NO_CT message to so the user knows that
 * no custom tag is selected.
 */

function noCTSelected() {
	var content;
	tagName = selTaglib = prefix = attlist = null;
	if (!((content = (gc.getContents())[0]) != null && (content[0] == NO_CT))) {
		gc.setContents(NO_CT_SELECTED);  //only send message to floater if a custom tag
						//was previously selected
	}
}

/* function: getLibnameFromPrefix
 * ------------------------------
 * Takes the prefix of the currently selected tag, finds the 
 * namespace instantiation at the top of the page with that prefix, 
 * and returns the tag library name based on the name of the TLD file, 
 * or null if no such tag library is found.
 */

function getLibnameFromPrefix(prefix) {
	var theDOM = dw.getDocumentDOM();
	var prefixIndex;
	var childText;
	var tldPath; // the full tldPath as specified in namespace instantiation
	var libname = null; // the name of the actual tag library stored in TLD file

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

⌨️ 快捷键说明

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