usetaglibs.js

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

JS
248
字号
/* ====================================================================
 * 
 * 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: useTagLibs.js
 * author: Dan Mandell [dmandell@stanford.edu]
 * -------------------------------------------
 * JavaScript implementation for the useTagLibs.html floater of the
 * UltraDev Custom Tag Library extension. Pings the TLDParser servlet
 * via an HTTP request for a list of TLD files to populate the 
 * Tag Library drop down list. When the user selects a taglib for use, 
 * checks the tagLibData.js file (the file of JavaScript associative
 * array declarations that contains all locally cached taglibs) for
 * the taglib JavaScript declaration. If it is not found locally,
 * pings the TLDParser servlet for a description of the taglib as a 
 * series of JavaScript associative array declarations, appends them
 * to the tagLibData.js file, and loads the names of the taglib's
 * tags into the Insert Taglib dropdown menu. If the namespace
 * instantiation for the chosen taglib does not exist at the top of
 * the JSP, asks the user for the required TLD and PREFIX
 * values and inserts it at the top of the page.
 *
 * Also Implements the purgeCache() function, which removes all 
 * JavaScript taglib declarations from the tagLibData.js file, 
 * to reduce memory footprint (since most taglibs take up 1 to 5 kb 
 * of memory, roughly 200-1000 taglibs present in the tagLibData.js file
 * will increase UltraDev's memory footprint by 1 MB).
 */

var TL_DATA = "tagLibData.js";
var TL_DATA_LOC = dw.getConfigurationPath() + "/Commands/customTags/";
var DATA_INIT = "taglibs = new Array();\n"
var NO_CTL_IN_USE = "No Tag Library In Use";
var ctServletURL = "http://localhost:8080/TLDParser/servlet/TLDParser";
var resp = (MMHttp.getText(ctServletURL + "?mode=tldlist")).data.toLowerCase();
var taglibList = resp.split("\t");
var ddMenu = document.selected_taglib.options;
var numPrevOpts = 0;
var libName; // name of the currently selected tag library

/* Load the Use Taglib drop down menu with the list of taglibs obtained
 * from pinging server for available TLDs.
 */

for (i = 0; i < taglibList.length; i++) {
	ddMenu[i] = new Option(taglibList[i]);
}


/* 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: useTaglib()
 * ---------------------
 * Checks if the chosen taglib is stored in memory (which is
 * always identical to the local cache). If not, calls
 * retrieveTaglib() to find it on the server. If it can be found,
 * calls insertTaglib().
 */

function useTaglib() {
	var taglibAvailable = true;
	var selectedLib = document.selected_taglib.options[document.selected_taglib.selectedIndex];

	if (selectedLib == null) {
		return; // disallow selection of empty option
	}

	libName = selectedLib.text;

	if (taglibs[libName] == null) {
		taglibAvailable = retrieveTaglib();
	}
	if (taglibAvailable) insertTaglib();
}

/* function: retrieveTaglib()
 * --------------------------
 * Makes an HTTP request to the server for a JavaScript declaration of
 * the chosen taglib. If the server sucessfully returns the declaration, 
 * calls addTaglib to add the declaration to local cache, and returns
 * true. Raises an alert and returns false otherwise.
 */

function retrieveTaglib() {
	var resp = (MMHttp.getText(ctServletURL + "?mode=ultradev&prefix=" + libName)).data			

	if (!resp == "") {
		return addTaglib(resp);
	}
	else {
		alert("Tag library \"" + libName + "\" not found on server.");
		return false;
	}
}

/* function: insertTaglib()
 * ------------------------
 * If the namespace instantiation for the chosen taglib does not exist in the
 * JSP, ask user for required TLD and PREFIX values and insert namespace
 * instantiation at top of page. Call supplyTagOptions to populate the
 * Insert Tag drop-down menu with the chosen taglib's tags.
 */

function insertTaglib() {
	var prefix; //prefix chosen by user to serve as taglib's namespace
	theDOM = dw.getDocumentDOM();
	docObj = theDOM.documentElement;
	
	if (docObj.outerHTML.indexOf(libName + ".tld") < 0 || docObj.outerHTML.indexOf(libName + ".tld\"") < 0) {
		var uri = prompt("Enter the URI of the tld file for this tag library","/WEB-INF/" + libName + ".tld");
		if (uri == null) return; 	//the user hit cancel, do not instantiate namespace or load
		prefix = prompt("Enter tag library prefix (must be alphanumeric)",libName.replace(/\W*/g, "")); // require that prefix is alphanumeric
		if (prefix == null) return; 	//the user hit cancel, do not instantiate namespace or load
		var taglibDec = "<%@ taglib uri=\"" + uri + "\" prefix=\"" + prefix + "\" %>";	
		docObj.outerHTML = taglibDec + "\n" + docObj.outerHTML;
	}
	supplyTagOptions();
}

/* function: supplyTagOptions() 
 * ----------------------------
 * Populates the Insert Tag drop-down menu with the list of available
 * tags for the current taglib in use. If no taglib in use, only
 * populates menu with the NO_CTL_IN_USE message.
 */

function supplyTagOptions() { 
	var list = document.selected_tag.options;
	var numTags;
	var filled;
	
	if (libName != null) {
		numTags = taglibs[libName].length;
		for (filled = 0; filled < numTags; filled++) {
			if (list[filled] == null) {
				list[filled] = new Option((taglibs[libName][filled])[0]);
			}
			else list[filled].text = (taglibs[libName][filled])[0];
		}
	}
	else {
		list[0].text = NO_CTL_IN_USE;
		filled = 1;	 // only 1 option has been filled
	}	

	for (i = filled; i < list.length; ) {
		list[i] = null;  // set remaining options to null so they don't
			        // appear.
	}
}

/* function: purgeCache()
 * ----------------------
 * Removes all taglib JavaScript declarations from local cache. Re-
 * evaluates local cache so memory from all previously used taglibs
 * can be freed.
 */

function purgeCache() {
	var msg = "Delete all local cache?\n(files on server will be unaffected)"

	if (confirm(msg)) {
		var result = DWfile.write(TL_DATA_LOC + TL_DATA, DATA_INIT);
		if (!result) { 
			alert("File IO error: " + "TL_DATA" + " could not be written."); 
		}
		eval(DWfile.read(TL_DATA_LOC + TL_DATA)); // refresh memory to reflect purged cache
		libName = null;
		supplyTagOptions();
	}
}

/* function: addTaglib()
 * ---------------------
 * Appends the server's HTTP response of JavaScript declarations for the 
 * chosen taglib to the local cache. Re-evaluates the local cache so the
 * new taglib is stored into memory for use.
 */

function addTaglib(newLib) {
	var result = DWfile.write(TL_DATA_LOC + TL_DATA, newLib, "append");
	if (!result) { 
		alert("File IO error: " + "TL_DATA" + " could not be written."); 
		return false;
	}
	else {
		eval(DWfile.read(TL_DATA_LOC + TL_DATA)); // refresh memory to reflect new taglib in cache
		return true; // file was succesfully appended with new Taglib data
	}
}

⌨️ 快捷键说明

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