wtpcore.js

来自「C#编写的在线用户统计、在线编辑器、验证码图片」· JavaScript 代码 · 共 247 行

JS
247
字号
/*
 * WtpCore.js @Microsoft Visual Studio 2008 <.NET Framework 2.0 (or Higher)>
 * AfritXia
 * 2008/3/17
 *
 * Copyright(c) http://www.AfritXia.NET/
 *
 */

/*@cc_on @*/
/*@set @DEBUG = true; @*/

// WebTextPane 控件集合
var g_wtpCollection = new Array();
// 当前的 WebTextPane 控件对象
var g_currWtp = null;

///////////////////////////////////////////////////////////////////
//
// 类 CWebTextPane 构造器
//

function CWebTextPane() {
	// 将当前编辑器对象加入到集合
	g_wtpCollection[g_wtpCollection.length] = this;
	// 将变量指向当前编辑器对象
	g_currWtp = this;

	// 文本模型控件
	this.m_textModelPane = null;
	// 设计器控件
	this.m_designerPane = null;
}

///////////////////////////////////////////////////////////////////
//
// 类 CWtpTextModelPane 构造器
// 
// parameters:
//     textAreaID, <textarea /> 文本框标记 ID
//

function CWtpTextModelPane(textAreaID) {
    // 获取标记元素
	this.m_textAreaElement = document.getElementById(textAreaID);

	if (this.m_textAreaElement == null)
		throw new Error("Can't Find Element With ID = '" + textAreaID + "'");

	// 设置 CWebTextPane 属性值
	g_currWtp.m_textModelPane = this;
}

///////////////////////////////////////////////////////////////////
// 
// 类 CWtpDesignerPane 构造器
//
// parameters:
//     spanID, <span /> 设计器标记 ID
//

function CWtpDesignerPane(spanID) {
    // 获取标记元素
	this.m_spanElement = document.getElementById(spanID);

	if (this.m_spanElement == null)
		throw new Error("Can't Find Element With ID = '" + spanID + "'");

    with(this) {
        m_spanElement.onpaste = function() {
            // 过滤 HTML 代码
            m_spanElement.document.selection.createRange().pasteHTML(global_htmlEncode(clipboardData.getData("Text")));
            return false;
        }
    }

	// 设置设计器引用
	g_currWtp.m_designerPane = this;
}

///////////////////////////////////////////////////////////////////
//
// 类 CWtpDropDownList 构造器
//
// parameters:
//     selectID, 下拉列表框标记 ID
//     objCommand, 命令对象
//

function CWtpDropDownList(selectID, objCommand) {
    // 获取标记元素
	this.m_selectElement = document.getElementById(selectID);

	if (this.m_selectElement == null)
		throw new Error("Can't Find Element With ID = '" + selectID + "'");

    // 设置命令对象
    this.m_objCmd = objCommand;

    with(this) {
        // 选项修改事件
	    m_selectElement.onchange = function() {
	        m_objCmd.execute();
	    }
	}
}

///////////////////////////////////////////////////////////////////
//
// 类 CWtpCmdButton 构造器
//
// parameters:
//     strImgID, <img /> 图片按钮标记 ID
//     objCommand, 命令对象
//

function CWtpCmdButton(strImgID, objCommand) {
    // 获取标记元素
	this.m_imgElement = document.getElementById(strImgID);

	if (this.m_imgElement == null)
		throw new Error("Can't Find Element With ID = '" + strImgID + "'");

	// 设置命令对象
	this.m_objCmd = objCommand;

	with(this) {
		// 添加点击事件
		m_imgElement.onclick = function() {
			m_objCmd.execute();
		}

		// 添加鼠标滑入事件
		m_imgElement.onmouseover = function() {
			m_imgElement.className = "WtpCmdButton_OnMouseEntry";
		}

		// 添加鼠标滑出事件
		m_imgElement.onmouseout = function() {
			m_imgElement.className = "WtpCmdButton_OnMouseExit";
		}

		// 添加鼠标按键事件
		m_imgElement.onmousedown = function() {
			m_imgElement.className = "WtpCmdButton_OnMouseDown";
		}

		// 添加鼠标按键释放事件
		m_imgElement.onmouseup = function() {
			m_imgElement.className = "WtpCmdButton_OnMouseUp";
		}
	}
}

///////////////////////////////////////////////////////////////////
// 
// 更新文本模型,在页面提交之前
//
// WebTextPane 控件的核心函数
//

function global_wtpUpdateTextModelPaneOnSubmit()
{
	if (g_wtpCollection.length <= 0)
		return;

	for(var i=0; i < g_wtpCollection.length; i++)
	{
		// 获取 CWebTextPane 对象
		var wtp = g_wtpCollection[i];

		// 更新文本模型中的文本值
		wtp.m_textModelPane.m_textAreaElement.value = wtp.m_designerPane.m_spanElement.innerHTML;
	}
}

///////////////////////////////////////////////////////////////////
// 
// global_htmlEncode 超链接文本字符串过滤函数
//
// parameters:
//     src, 源字符串
//
// returns:
//     过滤后的字符串,字符串不会含有 & " ' < > 空格等字符
// 

function global_htmlEncode(src) {
    // 字符是否为空?
    if (src == null || src.length <= 0)
        return null;

    var resultStr = "";
    
    for (var i = 0; i < src.length; i++) {
        // 获取字符
        var c = src.charAt(i);
        
        switch (c) {
            case '&':
                resultStr += "&#38;";
                break;

            case '\"':
                resultStr += "&#34;";
                break;

            case '\'':
                resultStr += "&#39;";
                break;

            case '<':
                resultStr += "&#60;";
                break;

            case '>':
                resultStr += "&#62;";
                break;
                
            case ' ':
                if (i > 0 && src.charAt(i - 1) == ' ') {
                    resultStr += "&nbsp;";
                } else {
                    resultStr += " ";
                }
   
                break;

            case '\n':
                resultStr += "<br />";
                break;

            case '\t':
                resultStr += " &nbsp;&nbsp;&nbsp;";
                break;
                
            default:
                resultStr += c;
                break;
        }
    }
    
    return resultStr;
}

/*@if (@DEBUG) @*/
/*@end @*/

⌨️ 快捷键说明

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