wdEditor.js 47 KB

1
  1. //<style>.UEDITORFIELD .popupList{height:unset;line-height:unset;}</style> var srca='<style id="ueditorExtraStyle">body>.popup-div.hLine-highlightBlod.objectPickerOption .popupList{height:unset;line-height:unset;}/*这是在wdeditor.js里面写入的*/</style>' if($("#ueditorExtraStyle").length<1){ $('body').prepend($(srca)); } NodeList.prototype.contains = function (val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) { return true; } } return false; }; var EditorManager = EditorManager || { toolbox: [], instances: {}, getWDEditor: function (id, opt) { var editor = this.instances[id]; console.log("wdEditor.js-getWDEditor(") if (!editor) { editor = this.instances[id] = new wdEditor(id, opt); } return editor; }, registerUI: function (name, func) { try { manager.toolbox[name] = func; } catch (e) { console.error(e.message); // } }, utils: { loadFile: function () { var tmpList = []; function getItem(doc, obj) { try { for (var i = 0, ci; ci = tmpList[i++]; ) { if (ci.doc === doc && ci.url == (obj.src || obj.href)) { return ci; } } } catch (e) { return null; } } return function (doc, obj, fn) { var item = getItem(doc, obj); if (item) { if (item.ready) { fn && fn(); } else { item.funs.push(fn) } return; } tmpList.push({ doc: doc, url: obj.src || obj.href, funs: [fn] }); if (!doc.body) { var html = []; for (var p in obj) { if (p == 'tag') continue; html.push(p + '="' + obj[p] + '"') } doc.write('<' + obj.tag + ' ' + html.join(' ') + ' ></' + obj.tag + '>'); return; } if (obj.id && doc.getElementById(obj.id)) { return; } var element = doc.createElement(obj.tag); delete obj.tag; for (var p in obj) { element.setAttribute(p, obj[p]); } element.onload = element.onreadystatechange = function () { if (!this.readyState || /loaded|complete/.test(this.readyState)) { item = getItem(doc, obj); if (item.funs.length > 0) { item.ready = 1; for (var fi; fi = item.funs.pop(); ) { fi(); } } element.onload = element.onreadystatechange = null; } }; element.onerror = function () { throw Error('加载失败:' + (obj.href || obj.src) + ' ') }; doc.getElementsByTagName("head")[0].appendChild(element); } } (), parseDom: function (arg) { var objE = document.createElement("div"); objE.innerHTML = arg; return objE.childNodes; } }, getOuterEditorField: function (id) { return document.getElementById("" + id + "Edit"); }, getEditorField: function (id) { return document.querySelector("#" + id + "Edit .edui-editor"); }, getEditorFrame: function (id) { return document.querySelector("#" + id + "Edit .edui-editor-iframeholder"); } }; //-------------------------------------------- var manager = EditorManager; manager.instances = manager.instances || []; var wdEditor = function (id, options) { this.id = id; this.options = options; //初始化ueditor var this_ = this, opt = this.options, id = this.id; this.options.onToolbarInit = []; opt.toolbars = opt.toolbars || manager.config.toolbars; //没指定就是默认 for (var i = 0; i < opt.toolbars.length; i++) { var bari = opt.toolbars[i]; for (var j = 0; j < bari.length; j++) { var tool = bari[j]; if (typeof(tool) == "object") { var obj = tool; var name = manager.config.getToolName(obj["name"]); // "附件"。Lin this.options["btn_opt_" + name] = obj; bari[j] = name; } else if (typeof(tool) == "string") { var name = tool; bari[j] = manager.config.getToolName(name); } } } //左边按钮和右边按钮 //默认不显示右边按钮,通过manager.config.toolbars指定 //没有指定左边按钮则默认都显示 //没指定右边按钮不显示 var btncount = 0, rightStr = manager.config.rightBtn.join(","), hasOtherBtn = false; for (var i = 0; i < opt.toolbars.length; i++) { btncount += opt.toolbars[i].length; } if (btncount > 0 && btncount <= manager.config.rightBtn.length) { //指定的按钮数量少于右边按钮,则检查是不是只有右边按钮 var rightCount = 0; for (var i = 0; i < opt.toolbars.length; i++) { for (var ii = 0; ii < opt.toolbars[i].length; ii++) { console.log(opt.toolbars[i][ii]) if (rightStr.indexOf(opt.toolbars[i][ii]) < 0) { //当遇到不是右面按钮的(全屏一定加入) hasOtherBtn = true; break; } } if (hasOtherBtn) { break; } } if (!hasOtherBtn) { //全部都是右边按钮,补上默认按钮 var btns = [ [] ]; btns[0] = manager.config.toolbars[0]; for (var i = 0; i < btns.length; i++) { for (var ii = 0; ii < btns[i].length; ii++) { opt.toolbars[i] = (opt.toolbars[i] || []); opt.toolbars[i].push(btns[i][ii]); } i == 0 && opt.toolbars[i].unshift("fullscreen"); } } } window[id + "_initWDEditor"] = (function (editor) { return function (ueditor) { editor.initWDEditor(ueditor); } })(this); addEditor(id, opt); return this; }; wdEditor.prototype = { id: null, options: {}, ueditor: null, commands: [], addListener: function (name, func) { this.ueditor.addListener(name, func); }, getContent: function () { if (this.ueditor) { return this.ueditor.getContent(); } else { console.error("没找到ueditor" + this.id); } }, swichToolbar: function () { if (this.wdtoolbar == true) { var ueditortoolbars = manager.getOuterEditorField(this.id).getElementsByClassName("edui-editor-toolbarbox") if (ueditortoolbars) { for (var i = 0; i < ueditortoolbars.length; i++) { ueditortoolbars[i].style.display = ""; } } this.getToolbar().style.display = "none"; this.wdtoolbar = false; } else { var ueditortoolbars = manager.getOuterEditorField(this.id).getElementsByClassName("edui-editor-toolbarbox") if (ueditortoolbars) { for (var i = 0; i < ueditortoolbars.length; i++) { ueditortoolbars[i].style.display = "none"; } } this.getToolbar().style.display = ""; this.wdtoolbar = true; } }, execCommand: function (command) { var func = (this.commands[command] || {}).execCommand != null ? this.commands[command].execCommand : this.ueditor.execCommand; if (func) { return func.apply(this.ueditor, arguments); } else { console.log("找不到“" + command + "”对应的execCommand") } }, queryCommandState: function (command) { var func = (this.commands[command] || {}).queryCommandState != null ? this.commands[command].queryCommandState : this.ueditor.queryCommandState; if (func) { return func.apply(this.ueditor, arguments); } else { console.log("找不到“" + command + "”对应的commandstate") } }, getUeditor: function () { return this.ueditor; }, setHeight: function (height) { this.getUeditor().setHeight(height); }, registerCommand: function (command, obj) { this.commands[command] = obj this.ueditor.registerCommand[command] = obj; }, getToolbar: function () { var editorid = this.id; if (this.btnpanel) { return this.btnpanel; } btnpanel = manager.utils.parseDom("<div onselectstart='return false' id='baseToolsPanel_" + editorid + " 'class='toolbar-div'></div>")[0]; btnpanel.style.position = "relative"; var ueditordiv = manager.getOuterEditorField(editorid); editorfield = ueditordiv.parentNode; editorfield.id = 'WDEditor_' + editorid; // var ueditorfield = document.getElementById('WDEditor_' + editorid); editordiv = manager.getEditorField(editorid); editordiv.insertBefore(btnpanel, document.querySelector("#" + editorfield.id + " .edui-editor-iframeholder")); this.btnpanel = btnpanel; return btnpanel; }, getToolsPanel: function (index) { index = index || 0; var panel = document.getElementById("toolsPanel_" + this.id + "_" + index); if (panel) {} else { panel = EditorManager.utils.parseDom("<div onselectstart='return false' id='toolsPanel_" + this.id + "_" + index + "' class='toolsPanel' style='height: 38px;padding-left: 8px;padding-right:8px;box-sizing:border-box;'></div>")[0]; } // if (index > 0) { // panel.style.display = "none"; // panel.setAttribute("ismorepanel", "true"); // //left: 0px; display: block; position: absolute; right: 0px; z-index: 9999; background: white; // panel.style.left = 0; // panel.style.position = "absolute"; // panel.style.right = 0; // panel.style.zIndex = 9999; // } return panel; }, hideEditorFrame: function () { EditorManager.getEditorFrame(this.id).style.display = "none"; }, showEditorFrame: function () { EditorManager.getEditorFrame(this.id).style.display = "block"; }, initWDEditor: function (ueditor) { var this_ = this, id = this.id, opt = this.options; this.ueditor = ueditor; var editor = document.getElementById(id + "Edit"); if (editor) { EditorManager.getWDEditor(id).swichToolbar(); this_.initedButtons = []; var manager = EditorManager; if (!wd.edit) { console.log("加载edit.js"); manager.utils.loadFile(document, { tag: "script", src: "/wd/js/edit.js" }, function () {}); //为了onoffbutton console.log("return"); // return; // 等待个1秒 var start = new Date().getTime(); } //套一层div var _toolbars = this_.getToolbar(id); //插入按钮区域 //初始化按钮列表 var toolbox = manager.toolbox; var bars = opt.toolbars || manager.config.toolbars; var index = 0; for (var i = 0; i < bars.length; i++) { var bar = this_.getToolsPanel(i); _toolbars.appendChild(bar); } for (var i = 0; i < bars.length; i++) { var bar = this_.getToolsPanel(i); _toolbars.appendChild(bar); var cmds = bars[i]; for (var j = 0; j < cmds.length; j++) { var cmd = cmds[j]; try { if (toolbox[cmd]) { var btn = toolbox[cmd].call(this_, this_, cmd); btn.wdeditor = btn.wdeditor || this_; var dom = btn.getElement(index, true); bar.appendChild(dom); if (btn.afterInit) { btn.afterInit(); } if (btn.getType() == "checkbox" && wd.edit && wd.edit.onoffInit) { wd.edit.onoffInit(btn.getType(), btn.getElementName(), '1', false, 'toolbar-button-selected', 'toolbar-button', null); } // if (btn.onToolbarInit) { // this_.options.onToolbarInit.push(btn.onToolbarInit); // } this_.initedButtons[index] = btn; index++; } } catch (e) { console.log(cmd); console.error(e); } } } } // if (optcallback) { // if (typeof (optcallback) == "string") { // var callbackname = optcallback // optcallback = window[callbackname]; // if (typeof optcallback == "undefined") { // console.info("没找到函数:" + callbackname + ";可能函数写在初始化ueditor方法的下面了,函数放到onload执行"); // (function () { // window.addEventListener("load", function () { // console.log("ueditor绑定的onload"); // console.log(window[callbackname]); // window[callbackname](); // console.log("ueditor的回调执行完毕"); // }); // })(callbackname) // } else { // optcallback(); // } // } else { // optcallback(); // } // } if (this_.initedButtons && this_.initedButtons.length > 0) { for (var i = 0; i < this_.initedButtons.length; i++) { try { this_.initedButtons[i].onToolbarInit && this_.initedButtons[i].onToolbarInit(); } catch (e) { console.error(e); } } } if (this_.initedButtons && this_.initedButtons.length > 0) { for (var i = this_.initedButtons.length - 1; i > 0; i--) { try { this_.initedButtons[i].setSelectionChange && this_.initedButtons[i].setSelectionChange(); } catch (e) { console.error(e); } } } }, queryCommandValue: function (cmd) { if (cmd instanceof Array) { return this.getUeditor().queryCommandValue.apply(this.getUeditor(), cmd); } else { return this.getUeditor().queryCommandValue(cmd); } } } manager.msgPopup = function (options) { manager.BaseBtn.call(this); this.settimeout = function (time) { var this_ = this; setTimeout(function () { this_.hide(); }, time || 1300); } this.showMsg = function (msg, offset) { if (offset == null) { offset = this.getSize(document.body); var tos = this.getSize(); offset.left = (offset.width - tos.width) / 2; offset.top = (offset.height - tos.height) / 2; } this.setMsg(msg || ""); this.showAt(offset); this.settimeout() } this.hide = function () { this.dom.style.display = "none"; this.isShow = false; }; this.setMsg = function (msg) { this.txt.innerText = msg; }; this.showAt = function (opt) { this.dom.style.top = opt.top + "px"; this.dom.style.left = opt.left + "px"; this.dom.style.display = "block"; this.isShow = true; }; this.init = function () { var spancss = 'width:0; height:0; font-size:0; overflow:hidden; position:absolute;'; var spantopcss = 'border-width:5px;border-style:solid dashed dashed;border-color:transparent transparent white transparent;left:8px;top:-9px;'; var spanbotcss = 'border-width:5px;border-style:solid dashed dashed;border-color:transparent transparent black transparent;left:8px;top:-10px;'; var span_top = manager.utils.parseDom("<span>")[0]; span_top.style.cssText = spancss + spantopcss; var span_bot = manager.utils.parseDom("<span>")[0]; span_bot.style.cssText = spancss + spanbotcss; var dom = this.dom = manager.utils.parseDom("<div class='pop'>")[0]; var txt = this.txt = manager.utils.parseDom("<span>")[0]; dom.id = new Date().getTime(); dom.style.minHeight = "1em"; dom.style.minWidth = "1em"; dom.style.width = "auto"; dom.style.padding = ".1em"; dom.style.position = "absolute"; dom.style.background = "white"; dom.style.borderRadius = '.3em'; dom.style.border = 'black 1px solid'; dom.style.zIndex = 9999; dom.appendChild(span_bot); dom.appendChild(span_top); dom.appendChild(txt); document.body.appendChild(dom); this.hide(); } } manager.ComboboxMenu = function (options) { manager.BaseBtn.call(this); options = options || { tag: "div" }; this.isShow = false; this.initOptions(options); this.cssRules = "width:" + this.text.length + "em;" + (options.cssRules || ""); this.height = options.height || 150; this.width = options.width || 200; this.items = []; this.tag = options.tag || "div"; this.showAt = function (opt) { if (this.items.length < 1) return; this.menudiv.style.top = opt.top + 4 + "px"; this.menudiv.style.left = opt.left + "px"; this.menudiv.style.display = "block"; this.isShow = true; }; this.hide = function () { this.menudiv.style.display = "none"; this.isShow = false; }; this.checkValue = function (value) { for (var i = 0; i < this.items.length; i++) { var item = this.items[i]; // console.log(item+":::"+value) if (item.text == value || item.value == value) { if (this.selectedItem) { this.selectedItem.className = "popupList"; } this.selectedItem = item.dom; this.selectedItem.className = "wdeditor_combobox_menu_nextDiv_vov"; var top = this.offset(this.selectedItem).top; this.menudiv.scrollTop = top; break; } } }; this.setItems = function (optitems, needclear) { var this_ = this; if (needclear) this.contentDiv.innerHTML = ''; for (var i = 0; i < optitems.length; i++) { var _item = optitems[i]; var _value = _item.value; var dom = document.createElement(_item.tag || this.tag); _item.dom = dom; this.items.push(_item); dom.text = _item.text; dom.className = "popupList"; dom.value = _value; dom.item = _item; dom.onmouseover = function () {}; dom.onmouseout = function () { if (this_.selectedItem != this) { this.style.background = ''; } }; // dom.style.display = "inline-block"; var itemcontent = document.createElement(_item.tag || this.tag); itemcontent.style.cssText = _item.cssRules; itemcontent.innerText = _item.text; dom.appendChild(itemcontent); dom.onclick = function () { this_.hide(); this_.onSelect(this.value); } this.contentDiv.appendChild(dom); } } this.initMenu = function () { var this_ = this; var contentDiv = this.contentDiv = document.createElement("div"); var outerDiv = this.menudiv = document.createElement("div"); outerDiv.appendChild(contentDiv); if (options.items) { var optitems = options.items; this.setItems(optitems) } contentDiv.className = "scrollbar"; contentDiv.style.overflow = "auto"; contentDiv.style.maxHeight = "200px"; contentDiv.style.height = this.height; contentDiv.style.maxWidth = this.width; contentDiv.style.maxHeight = "200px"; outerDiv.className = "popup-div hLine-highlightBlod objectPickerOption"; outerDiv.style.display="none"; // outerDiv.style.height = this.height; // outerDiv.style.maxWidth = this.width; outerDiv.style.zIndex = "9999"; outerDiv.setAttribute("tabIndex", 1); contentDiv.onblur = function () { this_.hide(); } this.element = this.menudiv; document.body.appendChild(outerDiv); }; }; manager.Combobox = function (options) { manager.Button.call(this); options = options || {}; options.text = options.text || "&nbsp;&nbsp;"; var this_ = this; this.baseClassName = typeof options.baseClassName == "undefined" ? "input-underlineDiv" : options.baseClassName; // // this.cssRules = 'width:5em;'; options.onBtnClick = options.onBtnClick || function () { this_.menu.checkValue(this_.wdeditor.queryCommandValue(this_.getCommand())); if (this_.menu.isShow) { this_.menu.hide(); } else { var offset = this_.offset(); offset.top += this_.element.offsetHeight - 1; this_.menu.showAt(offset); } event.stopPropagation(offset); return false; }; this.getType = function () { return "combobox"; }; this.getHtmlTpl = function () { return '<div id="' + this.getBtnID() + '"name="' + this.getBtnName() + '" class="' + [(this.baseClassName || ""), (this.className || "")].join(" ").trim() + '" title="' + this.title + '" value="' + this.text + '" ' + (this.cssRules == '' ? '' : 'style="' + this.cssRules + '"') + /* 改 wdValue,不确定,先改 -- 规范 wdValue=、wdName= 命名。Lin ' wdValue="' + this.name + '" wdName="' + this.getBtnName() + '"/>'; */ ' ssVal="'+ this.name +'" ssName="'+ this.getBtnName() +'"/>'; // return '<div class="input-div" id="' + this.getBtnID() + '" name="' + this.getBtnName() + '" title="' + this.title + '" value="' + this.text + '" wdValue="' + this.name + '" wdName="' + this.getBtnName() + '" style="min-height: 28px;">'+ // '<textarea class="form-text" disabled="disabled" style="width: calc(100% - 26px); padding-left: 5px; padding-top: 5px; resize: none; overflow-y: hidden; height: 26px; min-height: 26px;"></textarea>'+ // '<span class="icon-down"></span>'+ // '</div>' } this.getElement = function (index, bool) { if (this.element) { if (bool && this.label) return this.realElement; return this.element; } else { this.id = index; var html = this.getHtmlTpl(); var dom = this.element = manager.utils.parseDom(html)[0]; dom.value = dom.value; dom.onclick = options.onBtnClick; } this.label && this.createLabel(); if (bool && this.label) return this.realElement; return this.element; }; this.afterInit = function () { var this_ = this; options.wdeditor = this.wdeditor; this.menu = new manager.ComboboxMenu(options); this_.menu.checkValue = options.checkValue || this_.menu.checkValue; this.menu.initMenu(); this.menu.onSelect = options.onSelect || function (value) { this.wdeditor.execCommand(this.getCommand(), value); } this.setSelectionChange(); document.addEventListener("click", function (event) { var bool = this_.menu.menudiv.querySelectorAll("*").contains(event.srcElement); if (!bool) { this_.menu.hide(); this_.menu.element.style.minWidth = window.getComputedStyle(this_.element).width; } }); if (options.oninit) { options.oninit.call(this); } this.initWdHelp && this.initWdHelp(); }; this.selectValue = function (value) { this.menu.checkValue(value); var txt = value; if (value != null && this.menu.items) { for (var i = 0; i < this.menu.items.length; i++) { if (this.menu.items[i].value == value || this.menu.items[i].text == value) { txt = this.menu.items[i].text || value; break; } } } // txt = txt || (value != null && value != "" ? value.toString() : this.text); // function textSize(fontSize, text) { // var span = document.createElement("span"); // var result = {}; // result.width = span.offsetWidth; // result.height = span.offsetWidth; // span.style.visibility = "hidden"; // span.fontSize = fontSize; // document.body.appendChild(span); // if (typeof span.textContent != "undefined") // span.textContent = text; // else // span.innerText = text; // result.width = span.offsetWidth - result.width; // result.height = span.offsetHeight - result.height; // span.parentNode.removeChild(span); // return result; // } (txt && txt != '') && this.setText(txt); this.setValueIntoTitle(txt); // var i = 100, // len = txt.length; // do { // txt = txt.substr(0, len--); // var styles = window.getComputedStyle(this.element), // paddingleft = parseInt(styles.paddingLeft.replace("px", "")), // paddingright = parseInt(styles.paddingRight.replace("px", "")), // size = parseInt(styles.fontSize.replace("px", "")), // width = parseInt(styles.width.replace("px", "")), // txtWidth = textSize(size, txt + /*(i < 100 ? "..." : "") + */"▼").width; // this.element.value = txt + /*(i < 100 ? "..." : "") +*/ "▼"; // } while (txtWidth > (width - paddingleft) && i-- > 0); } this.setText = function (txt) { this.element.innerText = txt; } this.setTitle = function (txt) { this.element.title = txt; } this.setValueIntoTitle = function (val) { this.oriTitle = this.oriTitle || this.element.title; this.element.title = this.oriTitle + (val == '' ? "" : (":" + val)); } this.setSelectionChange = function () { var this_ = this; this.wdeditor.addListener("selectionchange", function () { var value = this_.wdeditor.queryCommandValue(this_.getCommand()); this_.selectValue(value); this_.menu.hide(); }); } this.initOptions(options); } manager.Spliter = function (options) { manager.BaseBtn.call(this); this.type = "spliter" this.getType = function () { return this.type; } this.getElement = function (index) { this.id = this.id || index || 0; return manager.utils.parseDom("<span id='" + this.getBtnID() + "' class='insideButton-Spliter vline-dim' style='" + (this.cssRules || '') + "'>" + (this.text || ' ') + "</span>")[0]; } this.initOptions(options); } manager.Button = function (options) { manager.BaseBtn.call(this); this.baseClassName = "toolbar-button"; this.initOptions(options); }; manager.RadioGroup = function (options) { manager.ButtonGroup.call(this, options); this.getRadioGroupName = function () { return this.getElementName(); }; this.initBtn = function (button_) { var this_ = this; var btn = new manager.CheckBox(button_); btn.wdeditor = this_.getEditor(); var btncmd = btn.getCommand(); var thiscmd = this_.getCommand(); btn.getType = function () { return "radio"; } btn.getBtnName = function () { return this_.getRadioGroupName(); } btn.setSelectionChange = function () {} btn.onBtnClick = function () { try { if (this.wdeditor) { // console.log(this.getCommand()); return this.wdeditor.execCommand(thiscmd, btncmd); } } catch (e) { console.error(e) } } var element = btn.getElement(this_.getBtnID()); btn.afterInit(); button_.btn = btn return element; } this.setSelectionChange = function () { var btn = this; btn.wdeditor.addListener('selectionchange', function () { var value = btn.wdeditor.queryCommandValue(btn.getCommand()); if (btn.checkValue) { btn.checkValue(value); } btn.setDisable(btn.wdeditor.queryCommandState(btn.getCommand()) == -1); }); } this.afterInit = function () { wd.edit && wd.edit.onoffInit && wd.edit.onoffInit(this.getType(), this.getRadioGroupName(), '', false, 'toolbar-button-selected', 'toolbar-button', null) options.oninit && options.oninit.call(this); this.initWdHelp && this.initWdHelp() } this.initOptions(options); } manager.ButtonGroup = function (options) { manager.BaseBtn.call(this); this.baseClassName = ""; this.options = options; this.cssRules = "display:inline-block"; this.getType = function () { return "buttongroup"; } this.checkValue = function (value) { for (var key in this.buttons) { try { this.buttons[key].btn.checkValue(value); } catch (error) { console.error(error); } } } this.setEnable = function (bool) { for (var key in this.buttons) { this.buttons[key].btn.setEnable(bool); } }; this.setDisable = function (bool) { for (var key in this.buttons) { this.buttons[key].btn.setDisable(bool); } }; this.getElement = function (index, bool) { var this_ = this; if (this_.element) { if (bool && this.label) return this.realElement; return this_.element; } this_.id = this_.id || index || 0; /* 改 wdValue,不确定,先改 -- 规范 wdValue= 命名。Lin * 另,wdvalue= 为小写 var base = this_.element = manager.utils.parseDom("<div class='" + (this.className || "") + "' name='" + this_.getBtnName() + "' id='" + this_.getBtnID() + "'" + (this.cssRules == '' ? '' : 'style="' + this.cssRules + '"') + " wdvalue='" + this_.getCommand() + "' ></div>")[0]; */ var base = this_.element = manager.utils.parseDom( "<div class='"+ (this.className || "") +"' name='"+ this_.getBtnName() +"' id='"+ this_.getBtnID() +"'"+ (this.cssRules == '' ? '' : 'style="' + this.cssRules +'"') + " ssVal='" + this_.getCommand() + "' ></div>")[0]; if (this_.buttons) { for (var key in this_.buttons) { var dom = this.initBtn(this_.buttons[key]); base.appendChild(dom); } } this.label && this.createLabel(); if (bool && this.label) return this.realElement; return base; }; this.initBtn = function (button_) { var this_ = this; var btn; var type = this.type = button_.type; button_.getBtnName = function () { return "wdEditor_" + button_.type + "_" + this_.name + "_" + button_.name; } if (type == "checkbox") { btn = new manager.CheckBox(button_); } else if (type == "combobox") { btn = new manager.Combobox(button_) } else { btn = new manager.Button(button_); } btn.wdeditor = this_.getEditor(); var btncmd = btn.getCommand(); var thiscmd = this_.getCommand(); var element = btn.getElement(this_.getBtnID(), true); btn.afterInit(); button_.btn = btn return element; } this.setSelectionChange = function () { var btn = this; btn.wdeditor.addListener('selectionchange', function () { // var value = btn.wdeditor.queryCommandValue(btn.getCommand()); // if (btn.checkValue) { // btn.checkValue(value); // } // btn.setDisable(btn.wdeditor.queryCommandState("inserttable") == -1); }); } this.afterInit = function () { // if (wd.edit && wd.edit.onoffInit) // wd.edit.onoffInit(this.getType(), this.getRadioGroupName(), '', false, 'insideButton-selected', 'toolbar-button', null) if (options.oninit) { options.oninit.call(this); ; } } this.initOptions(options); }; manager.CheckBox = function (options) { manager.BaseBtn.call(this); this.baseClassName = 'toolbar-button '; this.getType = function () { return "checkbox"; } this.checkValue = function (value) { wd.edit.onoffEdit(this.getElementName(), value); } this.setChecked = function (bool) { try { if (bool) { wd.edit.onoffEdit(this.getElementName(), this.name); } else { wd.edit.onoffEdit(this.getElementName(), ''); } } catch (error) { console.error("setChecked报错,可能没有初始化onoffbutton"); console.error(error); } } this.setSelectionChange = function () { var btn = this; var editor = btn.getEditor(); editor.addListener('selectionchange', function () { var cmd = btn.getCommand(); var state = editor.queryCommandState(btn.getCommand()); var bool; if (typeof(state) == "boolean") { bool = state; } else if (typeof(state) == "number") { bool = state == 1; } if (btn.setDisable) btn.setDisable(state == -1); if (btn.setChecked) btn.setChecked(bool); }); } this.initOptions(options); }; manager.Dialog = function (options) { manager.BaseBtn.call(this); this.initOptions(options); return UE.ui.Dialog.call(this, options); } manager.BaseBtn = function () { this.id = ''; this.name = ''; this.text = ''; this.cssRules = ''; this.title = this.title || this.text || ''; this.wdeditor = null; this.element = null; this.command = null; this.baseClassName = 'toolbar-button'; this.getCommand = function () { return this.command || this.name; } this.getEditor = function () { return this.wdeditor; } this.getElementAttr = function (attrname) { if (this.element) { return this.element.getAttribute(attrname); } } this.createLabel = function () { var span = document.createElement("span"); span.style.cssText=this.element.style.cssText; this.element.style.cssText=""; var label = document.createElement("label"); label.className = ["wdEditor_label" ,"wdEditor_label_" + this.name ,this.labelClassName].join(" "); label.innerHTML = this.label; label.style.cssText = this.labelCss; label.title = this.title; span.appendChild(label); span.appendChild(this.element); this.realElement = span; } this.getElementID = function () { return this.getElementAttr("id"); } this.getElementName = function () { return this.getElementAttr("name"); } this.getType = function () { return "button" } this.getBtnID = function () { return "wdEditor_" + this.getType() + "_" + this.name + "_" + this.id; } this.getBtnName = function () { return "wdEditor_" + this.getType() + "_" + this.name; } this.getHtmlTpl = function () { /* 改 wdValue -- 规范 wdValue= 命名。Lin baseHtml = "<span><input id='@ID@' name='@NAME@' class='@CLASS@' style='@STYLE@' value='@VALUE@' title='@TITLE@' wdValue='@WDVALUE@'/></span>"; */ baseHtml = "<span><input id='@ID@' name='@NAME@' class='@CLASS@' style='@STYLE@' value='@VALUE@' title='@TITLE@' ssVal='@WDVALUE@'/></span>"; return '<input type="button" id="' + this.getBtnID() + '"name="' + this.getBtnName() + '" class="' + [(this.baseClassName || ""), (this.className || "")].join(" ").trim() + '" title="' + this.title + '" value="' + this.text + '" ' + (this.cssRules == '' ? '' : 'style="' + this.cssRules + '"') + /* 改 wdValue,不确定,先改 -- 规范 wdValue=、wdName= 命名。Lin ' wdValue="' + this.name + '" wdName="' + this.getBtnName() + '"/>'; */ ' ssVal="'+ this.name +'" ssName="'+ this.getBtnName() +'"/>'; } this.getElement = function (index, bool) { if (this.element) { if (bool && this.label) return this.realElement; return this.element; } else { this.id = this.id || index || 0; this.element = manager.utils.parseDom(this.getHtmlTpl())[0]; var this_ = this; this.element.onclick = function (e) { this_.onBtnClick(e); return false; }; this.label && this.createLabel(); if (bool && this.label) return this.realElement; return this.element; } } this.onBtnClick = function () { try { if (this.wdeditor) { // console.log(this.getCommand()); return this.wdeditor.execCommand(this.getCommand()); } } catch (e) { console.error(e) } }; this.initOptions = function (options) { // (this.className = (this.baseClassName || "") + " " + (this.className || "")); for (var key in options) { if (key == "className") { this[key] = (this.baseClassName || "") + " " + options[key]; } else { this[key] = options[key]; } } }; this.setDisable = function (bool) { this.getElement().disabled = bool; // this.getElement().style.display = bool ? "none" : (((this.cssRules || "").match(/.*display\s*?:\s*?([^;]*).*/) || ["", ""])[1]); var txt = this.getElement().style.cssText || ""; txt = txt.replace(/(;)?(-webkit-)?filter\s*?:\s*?[^;]*;?(.*)/, "$1$3"); if (bool) { txt += '-webkit-filter: grayscale(100%);'; } this.getElement().style.cssText = txt; this.getElement().style.filter = bool ? "gray" : ""; txt = txt.replace(/.*-webkit-filter\s*?:\s*?([^;]*).*/, bool ? '-webkit-filter: grayscale(100%);' : ''); this.getElement().style.cssText = txt; }; this.setEnable = function (bool) { this.setDisable(!bool); }; this.setSelectionChange = function () { var this_ = this; this.wdeditor.addListener("selectionchange", function () { try { var state = this_.wdeditor.queryCommandState(this_.getCommand()); this_.setDisable(state < 0); var value = this_.wdeditor.queryCommandValue(this_.getCommand()); if (this_.checkValue) { this_.checkValue(value); } if (this_.selectValue) { this_.selectValue(value); } } catch (e) { console.error(e) } console.groupEnd(); }); }; this.initWdHelp = function () { try { function getToken() { var vars = [], hash; var hashes = window.location.href.slice( window.location.href.indexOf('?') + 1).split('&'); for (var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars["ssToken"]; // ["token"]。Lin } var ajax = wd.c.wdAjax || $.ajax, dt = { token: getToken(), name: this.getElementName() }; // ajax({ // type: "post", // url: "/wd/js/ueditor/jsp/controller.jsp", // data: dt, // async: true, // success: function(data) { // eval(data); // } // }); } catch (e) { //TODO handle the exception } }; this.onBtnMouseDown = function () {}; this.afterInit = function () { var this_ = this; this.setSelectionChange(); this.getElement().onchange = function () { if (this_.onChange) this_.onChange.call(this_); } this.getElement().onclick = function (event) { if (this_.onBtnClick) this_.onBtnClick.call(this_, event); } this.getElement().onmousedown = function () { if (this_.onBtnMouseDown) this_.onBtnMouseDown.call(this_); } this.getElement().onmouseup = function () { if (this_.onBtnMouseUp) this_.onBtnMouseUp.call(this_); } if (this.oninit) this.oninit(); this.initWdHelp && this.initWdHelp() }; this.getScrollInfo = function (element) { var scrollinfo = { left: 0, top: 0 } getScroll(element || this.getElement()); return scrollinfo; function getScroll(node) { // 非Element 终止递归 if (!node) { return } if (node.nodeType !== 1) { return; } if (node.parentNode && node.parentNode.tagName == "body") { getScroll(node.parentNode); return; } scrollinfo.top += node.scrollTop; scrollinfo.left += node.scrollLeft; // position = fixed: 获取值后退出递归 if (node.parentNode) getScroll(node.parentNode); } }; this.offset = function (element) { /* 摘自jquery */ var docElem, win, box = { top: 0, left: 0 }, elem = (element || this.getElement()), doc = elem && elem.ownerDocument; if (!doc) { return; } docElem = doc.documentElement; if (!document.querySelectorAll("*").contains(this.element)) { return box; } if (typeof elem.getBoundingClientRect !== typeof undefined) { box = elem.getBoundingClientRect(); } function getWindow(elem) { return (elem && elem == elem.window) ? elem : elem.nodeType === 9 ? elem.defaultView || elem.parentWindow : false; } win = getWindow(doc); return { top: box.top + (win.pageYOffset || docElem.scrollTop) - (docElem.clientTop || 0), left: box.left + (win.pageXOffset || docElem.scrollLeft) - (docElem.clientLeft || 0) }; }; this.getStyles = function (element) { var _style; element = element || this.getElement(); if (!document.querySelectorAll("*").contains(element)) { console.error("注意,该节点不在文档里面,无法获取元素相关信息"); offset = { top: element.offsetTop, left: element.offsetLeft } _style = element.style; } else { _style = window.getComputedStyle(element) offset = this.offset(); } return _style; }; this.getSize = function (element) { element = element || this.getElement(); var offset, _style; if (!document.querySelectorAll("*").contains(element)) { console.error("注意,该节点不在文档里面,无法获取元素相关信息"); offset = { top: element.offsetTop, left: element.offsetLeft } } else { offset = this.offset(); } _style = this.getStyles(); var ele = element, sizeInfo = {}, marginTop = parseInt(_style.marginTop.replace("px", "")) || 0, marginRight = parseInt(_style.marginRight.replace("px", "")) || 0, marginBottom = parseInt(_style.marginBottom.replace("px", "")) || 0, marginLeft = parseInt(_style.marginLeft.replace("px", "")) || 0, width = parseInt(_style.width.replace("px", "")) || 0, height = parseInt(_style.height.replace("px", "")) || 0, scrollleft = document.documentElement.scrollLeft || document.body.scrollLeft || 0, scrolltop = document.documentElement.scrollTop || document.body.scrollTop || 0; var scrollinfo = this.getScrollInfo(); if (offset) { sizeInfo.top = offset.top - scrollinfo.top; // 距离body的top sizeInfo.left = offset.left - scrollinfo.left; // 距离body的left sizeInfo.width = width + marginLeft + marginRight; // 宽 sizeInfo.height = height + marginTop + marginBottom; // 高 } return sizeInfo; }; this.hide = function () { this.getElement().style.display = "none"; } this.show = function () { this.getElement().style.display = ""; } };