editor2toolbar.js

来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 497 行 · 第 1/2 页

JS
497
字号
/*
	Copyright (c) 2004-2006, The Dojo Foundation
	All Rights Reserved.

	Licensed under the Academic Free License version 2.1 or above OR the
	modified BSD license. For more information on Dojo licensing, see:

		http://dojotoolkit.org/community/licensing.shtml
*/



dojo.provide("dojo.widget.Editor2Toolbar");
dojo.require("dojo.lang.*");
dojo.require("dojo.widget.*");
dojo.require("dojo.event.*");
dojo.require("dojo.html.layout");
dojo.require("dojo.html.display");
dojo.require("dojo.widget.RichText");
dojo.require("dojo.widget.PopupContainer");
dojo.require("dojo.widget.ColorPalette");
dojo.lang.declare("dojo.widget.HandlerManager", null, function () {
	this._registeredHandlers = [];
}, {registerHandler:function (obj, func) {
	if (arguments.length == 2) {
		this._registeredHandlers.push(function () {
			return obj[func].apply(obj, arguments);
		});
	} else {
		this._registeredHandlers.push(obj);
	}
}, removeHandler:function (func) {
	for (var i = 0; i < this._registeredHandlers.length; i++) {
		if (func === this._registeredHandlers[i]) {
			delete this._registeredHandlers[i];
			return;
		}
	}
	dojo.debug("HandlerManager handler " + func + " is not registered, can not remove.");
}, destroy:function () {
	for (var i = 0; i < this._registeredHandlers.length; i++) {
		delete this._registeredHandlers[i];
	}
}});
dojo.widget.Editor2ToolbarItemManager = new dojo.widget.HandlerManager;
dojo.lang.mixin(dojo.widget.Editor2ToolbarItemManager, {getToolbarItem:function (name) {
	var item;
	name = name.toLowerCase();
	for (var i = 0; i < this._registeredHandlers.length; i++) {
		item = this._registeredHandlers[i](name);
		if (item) {
			return item;
		}
	}
	switch (name) {
	  case "bold":
	  case "copy":
	  case "cut":
	  case "delete":
	  case "indent":
	  case "inserthorizontalrule":
	  case "insertorderedlist":
	  case "insertunorderedlist":
	  case "italic":
	  case "justifycenter":
	  case "justifyfull":
	  case "justifyleft":
	  case "justifyright":
	  case "outdent":
	  case "paste":
	  case "redo":
	  case "removeformat":
	  case "selectall":
	  case "strikethrough":
	  case "subscript":
	  case "superscript":
	  case "underline":
	  case "undo":
	  case "unlink":
	  case "createlink":
	  case "insertimage":
	  case "htmltoggle":
		item = new dojo.widget.Editor2ToolbarButton(name);
		break;
	  case "forecolor":
	  case "hilitecolor":
		item = new dojo.widget.Editor2ToolbarColorPaletteButton(name);
		break;
	  case "plainformatblock":
		item = new dojo.widget.Editor2ToolbarFormatBlockPlainSelect("formatblock");
		break;
	  case "formatblock":
		item = new dojo.widget.Editor2ToolbarFormatBlockSelect("formatblock");
		break;
	  case "fontsize":
		item = new dojo.widget.Editor2ToolbarFontSizeSelect("fontsize");
		break;
	  case "fontname":
		item = new dojo.widget.Editor2ToolbarFontNameSelect("fontname");
		break;
	  case "inserttable":
	  case "insertcell":
	  case "insertcol":
	  case "insertrow":
	  case "deletecells":
	  case "deletecols":
	  case "deleterows":
	  case "mergecells":
	  case "splitcell":
		dojo.debug(name + " is implemented in dojo.widget.Editor2Plugin.TableOperation, please require it first.");
		break;
	  case "inserthtml":
	  case "blockdirltr":
	  case "blockdirrtl":
	  case "dirltr":
	  case "dirrtl":
	  case "inlinedirltr":
	  case "inlinedirrtl":
		dojo.debug("Not yet implemented toolbar item: " + name);
		break;
	  default:
		dojo.debug("dojo.widget.Editor2ToolbarItemManager.getToolbarItem: Unknown toolbar item: " + name);
	}
	return item;
}});
dojo.addOnUnload(dojo.widget.Editor2ToolbarItemManager, "destroy");
dojo.declare("dojo.widget.Editor2ToolbarButton", null, function (name) {
	this._name = name;
}, {create:function (node, toolbar, nohover) {
	this._domNode = node;
	var cmd = toolbar.parent.getCommand(this._name);
	if (cmd) {
		this._domNode.title = cmd.getText();
	}
	this.disableSelection(this._domNode);
	this._parentToolbar = toolbar;
	dojo.event.connect(this._domNode, "onclick", this, "onClick");
	if (!nohover) {
		dojo.event.connect(this._domNode, "onmouseover", this, "onMouseOver");
		dojo.event.connect(this._domNode, "onmouseout", this, "onMouseOut");
	}
}, disableSelection:function (rootnode) {
	dojo.html.disableSelection(rootnode);
	var nodes = rootnode.all || rootnode.getElementsByTagName("*");
	for (var x = 0; x < nodes.length; x++) {
		dojo.html.disableSelection(nodes[x]);
	}
}, onMouseOver:function () {
	var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
	if (curInst) {
		var _command = curInst.getCommand(this._name);
		if (_command && _command.getState() != dojo.widget.Editor2Manager.commandState.Disabled) {
			this.highlightToolbarItem();
		}
	}
}, onMouseOut:function () {
	this.unhighlightToolbarItem();
}, destroy:function () {
	this._domNode = null;
	this._parentToolbar = null;
}, onClick:function (e) {
	if (this._domNode && !this._domNode.disabled && this._parentToolbar.checkAvailability()) {
		e.preventDefault();
		e.stopPropagation();
		var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
		if (curInst) {
			var _command = curInst.getCommand(this._name);
			if (_command) {
				_command.execute();
			}
		}
	}
}, refreshState:function () {
	var curInst = dojo.widget.Editor2Manager.getCurrentInstance();
	var em = dojo.widget.Editor2Manager;
	if (curInst) {
		var _command = curInst.getCommand(this._name);
		if (_command) {
			var state = _command.getState();
			if (state != this._lastState) {
				switch (state) {
				  case em.commandState.Latched:
					this.latchToolbarItem();
					break;
				  case em.commandState.Enabled:
					this.enableToolbarItem();
					break;
				  case em.commandState.Disabled:
				  default:
					this.disableToolbarItem();
				}
				this._lastState = state;
			}
		}
	}
	return em.commandState.Enabled;
}, latchToolbarItem:function () {
	this._domNode.disabled = false;
	this.removeToolbarItemStyle(this._domNode);
	dojo.html.addClass(this._domNode, this._parentToolbar.ToolbarLatchedItemStyle);
}, enableToolbarItem:function () {
	this._domNode.disabled = false;
	this.removeToolbarItemStyle(this._domNode);
	dojo.html.addClass(this._domNode, this._parentToolbar.ToolbarEnabledItemStyle);
}, disableToolbarItem:function () {
	this._domNode.disabled = true;
	this.removeToolbarItemStyle(this._domNode);
	dojo.html.addClass(this._domNode, this._parentToolbar.ToolbarDisabledItemStyle);
}, highlightToolbarItem:function () {
	dojo.html.addClass(this._domNode, this._parentToolbar.ToolbarHighlightedItemStyle);
}, unhighlightToolbarItem:function () {
	dojo.html.removeClass(this._domNode, this._parentToolbar.ToolbarHighlightedItemStyle);
}, removeToolbarItemStyle:function () {
	dojo.html.removeClass(this._domNode, this._parentToolbar.ToolbarEnabledItemStyle);
	dojo.html.removeClass(this._domNode, this._parentToolbar.ToolbarLatchedItemStyle);
	dojo.html.removeClass(this._domNode, this._parentToolbar.ToolbarDisabledItemStyle);
	this.unhighlightToolbarItem();
}});
dojo.declare("dojo.widget.Editor2ToolbarDropDownButton", dojo.widget.Editor2ToolbarButton, {onClick:function () {
	if (this._domNode && !this._domNode.disabled && this._parentToolbar.checkAvailability()) {
		if (!this._dropdown) {
			this._dropdown = dojo.widget.createWidget("PopupContainer", {});
			this._domNode.appendChild(this._dropdown.domNode);
		}
		if (this._dropdown.isShowingNow) {
			this._dropdown.close();
		} else {
			this.onDropDownShown();
			this._dropdown.open(this._domNode, null, this._domNode);
		}
	}
}, destroy:function () {
	this.onDropDownDestroy();
	if (this._dropdown) {
		this._dropdown.destroy();
	}
	dojo.widget.Editor2ToolbarDropDownButton.superclass.destroy.call(this);
}, onDropDownShown:function () {
}, onDropDownDestroy:function () {
}});
dojo.declare("dojo.widget.Editor2ToolbarColorPaletteButton", dojo.widget.Editor2ToolbarDropDownButton, {onDropDownShown:function () {
	if (!this._colorpalette) {
		this._colorpalette = dojo.widget.createWidget("ColorPalette", {});
		this._dropdown.addChild(this._colorpalette);
		this.disableSelection(this._dropdown.domNode);
		this.disableSelection(this._colorpalette.domNode);
		dojo.event.connect(this._colorpalette, "onColorSelect", this, "setColor");
		dojo.event.connect(this._dropdown, "open", this, "latchToolbarItem");
		dojo.event.connect(this._dropdown, "close", this, "enableToolbarItem");

⌨️ 快捷键说明

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