zpeditor-core.js

来自「zapatec suite 最新版 20070204,非常棒的ajax widg」· JavaScript 代码 · 共 2,362 行 · 第 1/5 页

JS
2,362
字号
		// Move cursor after <br>		range.collapse(false);		range.select();		return true;	}	return false;}/** * Maximizes and Restores the editor frame.  The icon is updated as * needed. * * @param {boolean} isMaximize if true the editor iframe is to be maximized, * otherwise it is to be restored to its original state. If omitted the function * will act as a toggle inverting the maximize state in each call. * @private */Zapatec.MinimalEditor.prototype.resizeEditor = function() {	var isMaximize;	if (!this.maximizer) {		this.maximizer = new Zapatec.Utils.Maximizable({			container: this.container,			maximizedBorder: this.config.maximizedBorder		});		var self = this;		// Attach to onBeforeMaximize events		this.maximizer.addEventListener('onBeforeMaximize', function(restoreState) {			// Store editor panel size			restoreState.editorPanelRestorer = new Zapatec.SRProp(self.editorPanel);			restoreState.editorPanelRestorer.saveProps("style.width", "style.height");			// Store pane size			restoreState.paneRestorer = new Zapatec.SRProp(self.pane.getContainer());			restoreState.paneRestorer.saveProps("style.width", "style.height");			// Store field size			restoreState.fieldRestorer = new Zapatec.SRProp(self.config.field);			restoreState.fieldRestorer.saveProps("style.width", "style.height");			// Store toolbar width			restoreState.toolbarRestorer = new Zapatec.SRProp(self.toolbar);			restoreState.toolbarRestorer.saveProps("style.width");		});		// Attach to onAfterRestore events		this.maximizer.addEventListener('onAfterRestore', function(restoreState) {			// Restore editor panel size			restoreState.editorPanelRestorer.restoreProps("style.width", "style.height");			// Restore iframe size			restoreState.paneRestorer.restoreProps("style.width", "style.height");			// Restore text area size			restoreState.fieldRestorer.restoreProps("style.width", "style.height");			// Restore toolbar width			restoreState.toolbarRestorer.restoreProps("style.width");		});		// Attach to onAfterSize events		this.maximizer.addEventListener('onAfterSize', function(width, height) {			self.setSize(width, height);		});		isMaximize = true;	}	else {		isMaximize = !this.maximizer.isMaximized;	}	this.maximizer.setMaximized(isMaximize);	// Enumerate all toolbar buttons	for (var ii = 0; ii < this.buttons.length; ii++) {		var button = this.buttons[ii];		var buttonClass = button.config.className;		if (-1 != buttonClass.indexOf('maximize')) {			if (isMaximize) {				Zapatec.Utils.removeClass(button.internalContainer, 'maximize');				Zapatec.Utils.addClass(button.internalContainer, 'restore');			}			else {				Zapatec.Utils.removeClass(button.internalContainer, 'restore');				Zapatec.Utils.addClass(button.internalContainer, 'maximize');			}			break;		}	}}/** * Gets the key under which the html content is to be saved/fetched server-side * * @public */Zapatec.MinimalEditor.prototype.getPersistKey = function() {	// If persist key is assigned by user	if (null != this.config.persistKey) {		// Return it		return this.config.persistKey;	}	var persistKeyInput = document.getElementById("persistKey");	if (null != persistKeyInput) {		return persistKeyInput.value;	}	return null;}/** * Sets the key under which the html content is to be saved/fetched server-side * * @public * @param {string} persistKey A key under which the html content is to be * saved/fetched server-side */Zapatec.MinimalEditor.prototype.setPersistKey = function(persistKey) {	this.config.persistKey = persistKey;}/** * Saves the editor content to a remote server. * * @public */Zapatec.MinimalEditor.prototype.save = function() {	var key = this.getPersistKey();	if (null == key || 0 == key.length) {		return false;	}	var url = this.config.persistPath + "?key=" + escape(key) + '&r=' + Math.random();	var self = this;	Zapatec.Transport.fetch({		url: url,		method: "POST",		content: "content=" + escape(self.getHTML()),		onLoad: function(result) {		},		onError: function(error) {			switch (error.errorCode) {				case 404:					alert(self.getMessage('noSuchFileError'));					break;				case 403:					alert(self.getMessage('writeAccessForbiddenError'));					break;				default:					alert(self.getMessage('fetchError', error.errorCode, 									error.errorDescription));			}		}	});	return true;};/** * Loads editor content from a remove server * * @public */Zapatec.MinimalEditor.prototype.fetch = function() {	var key = this.getPersistKey();	if (null == key || 0 == key.length) {		return false;	}	var url = this.config.persistPath + "?key=" + escape(key) + '&r=' + Math.random();	var self = this;	Zapatec.Transport.fetch({		url: url,		method: "GET",		onLoad: function(result) {			// Unescape unicode characters			var html = self.unicodeToText(result.responseText);			self.setHTML(html);		},		onError: function(error) {			switch (error.errorCode) {				case 404:					alert(self.getMessage('noSuchFileError'));					break;				case 403:					alert(self.getMessage('readAccessForbiddenError'));					break;				default:					alert(self.getMessage('fetchError', error.errorCode,									error.errorDescription));			}		}	});	return true;};/** * Converts text in for of %u0075%u006e%u0069%u0063%u006f%u0064%u0065 * to its text un-escaped equivalent * * @param {string} escapedUnicodeText text to convert * @private */Zapatec.MinimalEditor.prototype.unicodeToText = function(escapedUnicodeText) {	if (-1 == escapedUnicodeText.indexOf("%u")) {		// Nothing to un-escape		return escapedUnicodeText;	}	escapedUnicodeText = escapedUnicodeText.replace(/%u/g, "\\u");	// If there are quotes in string	if (-1 != escapedUnicodeText.indexOf("\"")) {		// Escape quotes before passing to eval to avoid JS error		escapedUnicodeText = escapedUnicodeText.replace(/\"/g, "&quot;");	}	var text = eval("\"" + escapedUnicodeText + "\"");	return text;}/** * Shows a special character input window * * @public */Zapatec.MinimalEditor.prototype.showCharMapWindow = function() {	// If character map windows is not yet closed	if (this.characterMapWindow) {		return;	}	if (Zapatec.is_khtml) {		this.storeSelection();	}	var characters =					[									'&Yuml;', '&scaron;', '&#064;', '&quot;', '&iexcl;', '&cent;', '&pound;', '&curren;', '&yen;', '&brvbar;',									'&sect;', '&uml;', '&copy;', '&ordf;', '&laquo;', '&not;', '&macr;', '&deg;', '&plusmn;', '&sup2;',									'&sup3;', '&acute;', '&micro;', '&para;', '&middot;', '&cedil;', '&sup1;', '&ordm;', '&raquo;', '&frac14;',									'&frac12;', '&frac34;', '&iquest;', '&times;', '&Oslash;', '&divide;', '&oslash;', '&fnof;', '&circ;',									'&tilde;', '&ndash;', '&mdash;', '&lsquo;', '&rsquo;', '&sbquo;', '&ldquo;', '&rdquo;', '&bdquo;',									'&dagger;', '&Dagger;', '&bull;', '&hellip;', '&permil;', '&lsaquo;', '&rsaquo;', '&euro;', '&trade;',									'&Agrave;', '&Aacute;', '&Acirc;', '&Atilde;', '&Auml;', '&Aring;', '&AElig;', '&Ccedil;', '&Egrave;',									'&Eacute;', '&Ecirc;', '&Euml;', '&Igrave;', '&Iacute;', '&Icirc;', '&Iuml;', '&ETH;', '&Ntilde;',									'&Ograve;', '&Oacute;', '&Ocirc;', '&Otilde;', '&Ouml;', '&reg;', '&times;', '&Ugrave;', '&Uacute;',									'&Ucirc;', '&Uuml;', '&Yacute;', '&THORN;', '&szlig;', '&agrave;', '&aacute;', '&acirc;', '&atilde;',									'&auml;', '&aring;', '&aelig;', '&ccedil;', '&egrave;', '&eacute;', '&ecirc;', '&euml;', '&igrave;',									'&iacute;', '&icirc;', '&iuml;', '&eth;', '&ntilde;', '&ograve;', '&oacute;', '&ocirc;', '&otilde;',									'&ouml;', '&divide;', '&oslash;', '&ugrave;', '&uacute;', '&ucirc;', '&uuml;', '&yacute;', '&thorn;',									'&yuml;', '&OElig;', '&oelig;', '&Scaron;'									];	var html = '<table class="charmap" border="0" cellspacing="1" cellpadding="0" width="100%">';	for (var charIndex = 0; charIndex < characters.length; ++charIndex) {		var charStr = characters[charIndex];		if (charIndex % 16 == 0) {			if (0 != charIndex) {				html += '</tr>';			}			html += '<tr>';		}		html += '<td class="character" onmouseover="';		html += 'Zapatec.MinimalEditor.OnMouseOverChar(' + this.id + ', this)" ';		html += 'onclick="return Zapatec.MinimalEditor.InsertChar(' + this.id + ', \'' +		        charStr + '\')">' + charStr + '</td>';	}	html += '<td class="character" colspan="4">&nbsp;</td>';	html += '</tr>';	html += '</table>';	var self = this;	var onClose = function(win) {		self.characterMapWindow = null;		// Hide modal		self.setIsModal(false);	}	var title = this.getMessage('characterMapTitle');	this.characterMapWindow = this.createWindow(420, 245, title, html, onClose);}/** * Called internally when the mouse goes over a character cell in character map * * @private * @param {string} id editor id * @param {object} cell reference to character cell */Zapatec.MinimalEditor.OnMouseOverChar = function(id, cell) {	// Get editor by id	var objEditor = Zapatec.Widget.getWidgetById(id);	if (objEditor.oldHiliteCell) {		objEditor.oldHiliteCell.className = "character";	}	var cellClass = cell.className;	if (-1 == cellClass.indexOf("character-hilite")) {		cell.className = "character character-hilite";		objEditor.oldHiliteCell = cell;	}}/** * Called internally when a character cell is clicked * * @private * @param {string} id editor id * @param {string} character selected character */Zapatec.MinimalEditor.prototype.insertText = function(text) {	if (Zapatec.is_ie) {		var range = this.oldSelectionRange;		if (!range) {			var sel = this.pane.getContainer().contentWindow.document.selection;			range = sel.createRange();		}		var self = this;		setTimeout(function() {			range.pasteHTML(text);			range.select();			// Store an undo step			self.undo.saveUndo();			// Update toolbar icon states			self.updateToolbar();		}, 50);	}	else {		if (Zapatec.is_khtml) {			this.restoreSelection();		}		var charElement = this.pane.getIframeDocument().createTextNode(text);		this.insertNodeAtSelection(charElement);		this.focus();		// Store an undo step		this.undo.saveUndo();		// Update toolbar icon states		this.updateToolbar();	}}/** * Called internally when a character cell is clicked * * @private * @param {string} id editor id * @param {string} character selected character */Zapatec.MinimalEditor.InsertChar = function(id, character) {	// Get editor by id	var objEditor = Zapatec.Widget.getWidgetById(id);	// Close character map widow	objEditor.characterMapWindow.close();	// Change selection text into our text field value	objEditor.insertText(character);}/** * Stores current selection state and its range. Safari specific. * * @private */Zapatec.MinimalEditor.prototype.storeSelection = function() {	var sel = this.pane.getContainer().contentWindow.getSelection();	var range = this.pane.getContainer().contentWindow.document.createRange();	var isRangeSet = false;	if (sel.baseNode == sel.extentNode && sel.baseOffset == sel.extentOffset) {		if (sel.type == "Range") {			range.setStartBefore(sel.baseNode);			range.setEndAfter(sel.extentNode);			isRangeSet = true;		}	}	if (!isRangeSet && sel.baseNode) {		range.setStart(sel.baseNode, sel.baseOffset);		range.setEnd(sel.extentNode, sel.extentOffset);	}	this.oldSelection = sel;	this.oldRange = range;}/** * Restores a previously stored selection state and its range. Safari specific. * * @private */Zapatec.MinimalEditor.prototype.restoreSelection = function() {	var sel = this.oldSelection;	var newSelection = this.pane.getContainer().contentWindow.getSelection();	newSelection.setBaseAndExtent(this.oldRange.startContainer,					this.oldRange.startOffset, this.oldRange.endContainer,					this.oldRange.endOffset);}/** * Checks if some selection exists in the editor * * @private */Zapatec.MinimalEditor.prototype.isSelectionExists = function() {	var range = null;	var sel = null;	if (Zapatec.is_ie) {		sel = this.pane.getContainer().contentWindow.document.selection;		range = sel.createRange();	}	else {		sel = this.pane.getContainer().contentWindow.getSelection();		if (!Zapatec.is_khtml) {			range = sel.getRangeAt(0);		}	}	var compare = 0;	if (Zapatec.is_ie)	{		if (sel.type == "Control")		{			compare = range.length;		}		else		{			compare = range.compareEndPoints("StartToEnd", range);		}	}	else {		if (Zapatec.is_khtml) {			var selectionType = '' + sel.type;			if (selectionType == "Range") {				return true;			}			if (selectionType == "Caret") {			

⌨️ 快捷键说明

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