wdEditor.base.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. console.log("base.js")
  2. manager.registerUI('bold', function (wdeditor, name) {
  3. var btn = new EditorManager.CheckBox({
  4. name: name,
  5. className: "icon-bold",
  6. text: "",
  7. title: "加粗"
  8. });
  9. return btn;
  10. });
  11. manager.registerUI('italic', function (wdeditor, name) {
  12. var btn = new EditorManager.CheckBox({
  13. name: name,
  14. className: "icon-italic",
  15. text: "",
  16. title: "斜体"
  17. });
  18. return btn;
  19. });
  20. manager.registerUI('underline', function (wdeditor, name) {
  21. var btn = new EditorManager.CheckBox({
  22. name: name,
  23. className: "icon-underline",
  24. text: "",
  25. title: "下划线"
  26. });
  27. return btn;
  28. });
  29. manager.registerUI('undo', function (wdeditor, name) {
  30. var btn = new EditorManager.Button({
  31. name: name,
  32. className: "icon-undo",
  33. text: "",
  34. title: "撤销",
  35. // cssRules: "float:right"
  36. });
  37. return btn;
  38. });
  39. manager.registerUI('redo', function (wdeditor, name) {
  40. var btn = new EditorManager.Button({
  41. name: name,
  42. className: "icon-redo",
  43. text: "",
  44. title: "重做",
  45. // cssRules: "float:right"
  46. });
  47. return btn;
  48. });
  49. manager.registerUI('strikethrough', function (wdeditor, name) {
  50. var btn = new EditorManager.CheckBox({
  51. name: name,
  52. className: "icon-strikethrough",
  53. text: "",
  54. title: "删除线"
  55. });
  56. return btn;
  57. });
  58. manager.registerUI('horizontal', function (wdeditor, name) {
  59. var btn = new EditorManager.Button({
  60. name: name,
  61. className: "icon-hLine",
  62. text: "",
  63. title: "分隔线"
  64. });
  65. return btn;
  66. });
  67. manager.registerUI('removeformat', function (wdeditor, name) {
  68. var btn = new EditorManager.Button({
  69. name: name,
  70. className: "insideButton-removeFormat",
  71. text: "清除格式",
  72. title: "清除格式"
  73. });
  74. return btn;
  75. });
  76. manager.registerUI('fullscreen', function (wdeditor, name) {
  77. var btn = new EditorManager.CheckBox({
  78. name: name,
  79. // text: "全屏",
  80. label: "全屏",
  81. title: "全屏",
  82. labelClassName: 'label-fullScreen',
  83. className: "rightBtn invertIcon-fullscreen",
  84. cssRules: "display:inline-block;float:right;margin-top: 6px;",
  85. onBtnClick: function (e) {
  86. var ueditor = wdeditor.getUeditor();
  87. var bool = ueditor.ui.isFullScreen();
  88. var ifr = ueditor.iframe;
  89. var height = parseInt($("body").height());
  90. var theight = parseInt(wdeditor.getToolsPanel().style.height);
  91. ueditor.ui.setFullScreen(!bool);
  92. if (!bool) {
  93. // var ifr = ueditor.iframe;
  94. // var height = parseInt($("body").height());
  95. // var theight = parseInt(wdeditor.getToolsPanel().style.height);
  96. ueditor.setHeight(height - theight);
  97. $(ueditor.body).height(height - theight - 26);
  98. } else {
  99. $(ueditor.body).css("height","");
  100. //ueditor.setHeight(wdeditor.getToolsPanel().style.height);
  101. }
  102. // this.getElement().value = !bool ? "还原" : "全屏";
  103. // this.setChecked(!bool);
  104. ueditor._selectionChange();
  105. },
  106. setSelectionChange: function () {
  107. var this_ = this;
  108. wdeditor.addListener("selectionchange", function () {
  109. var bool = wdeditor.getUeditor().ui.isFullScreen();
  110. this_.setChecked(bool);
  111. // this_.getElement().value = bool ? "还原" : "全屏";
  112. });
  113. }
  114. });
  115. return btn;
  116. });
  117. manager.registerUI('fontfamily', function (wdeditor, name) {
  118. var btn = new EditorManager.Combobox({
  119. name: name,
  120. command: name,
  121. labelCss: "",
  122. text: "字体",
  123. title: "字体",
  124. className: "ziti",
  125. cssRules: "height: 25px;line-height: 25px;position: relative;bottom: 3px;min-width:3em;",
  126. items: [{
  127. "text": "黑体", //菜单项的文字
  128. "value": "SimHei", //应用的样式
  129. "cssRules": "font-family:SimHei" //菜单项的样式
  130. }, {
  131. "text": "方正小标宋简体",
  132. "value": "方正小标宋简体",
  133. "cssRules": "font-family:方正小标宋简体"
  134. }, {
  135. "text": "Sans-serif",
  136. "value": "Sans-serif",
  137. "cssRules": "font-family:,Sans-serif"
  138. }, {
  139. "text": "新宋体",
  140. "value": "NSimSun",
  141. "cssRules": "font-family:NSimSun"
  142. }, {
  143. "text": "宋体",
  144. "value": "SimSun",
  145. "cssRules": "font-family:SimSun"
  146. }, {
  147. "text": "仿宋",
  148. "value": "FangSong",
  149. "cssRules": "font-family:FangSong"
  150. }, {
  151. "text": "楷体",
  152. "value": "KaiTi",
  153. "cssRules": "font-family:KaiTi"
  154. }, {
  155. "text": "仿宋_GB2312",
  156. "value": "FangSong_GB2312",
  157. "cssRules": "font-family:FangSong_GB2312"
  158. }, {
  159. "text": "楷体_GB2312",
  160. "value": "KaiTi_GB2312",
  161. "cssRules": "font-family:KaiTi_GB2312"
  162. }, {
  163. "text": "微软雅黑",
  164. "value": "Microsoft YaHei",
  165. "cssRules": "font-family:Microsoft YaHei"
  166. }, {
  167. "text": "隶书",
  168. "value": "LiSu",
  169. "cssRules": "font-family:LiSu"
  170. }, {
  171. "text": "幼圆",
  172. "value": "YouYuan",
  173. "cssRules": "font-family:YouYuan"
  174. }, {
  175. "text": "华文细黑",
  176. "value": "STXihei",
  177. "cssRules": "font-family:STXihei"
  178. }, {
  179. "text": "华文楷体",
  180. "value": "STKaiti",
  181. "cssRules": "font-family:STKaiti"
  182. }, {
  183. "text": "华文宋体",
  184. "value": "STSong",
  185. "cssRules": "font-family:STSong"
  186. }, {
  187. "text": "华文中宋",
  188. "value": "STZhongsong",
  189. "cssRules": "font-family:STZhongsong"
  190. }, {
  191. "text": "华文仿宋",
  192. "value": "STFangsong",
  193. "cssRules": "font-family:STFangsong"
  194. }, {
  195. "text": "方正舒体",
  196. "value": "FZShuTi",
  197. "cssRules": "font-family:FZShuTi"
  198. }, {
  199. "text": "方正姚体",
  200. "value": "FZYaoti",
  201. "cssRules": "font-family:FZYaoti"
  202. }, {
  203. "text": "华文彩云",
  204. "value": "STCaiyun",
  205. "cssRules": "font-family:STCaiyun"
  206. }, {
  207. "text": "华文琥珀",
  208. "value": "STHupo",
  209. "cssRules": "font-family:STHupo"
  210. }, {
  211. "text": "华文隶书",
  212. "value": "STLiti",
  213. "cssRules": "font-family:STLiti"
  214. }, {
  215. "text": "华文行楷",
  216. "value": "STXingkai",
  217. "cssRules": "font-family:STXingkai"
  218. }, {
  219. "text": "华文新魏",
  220. "value": "STXinwei",
  221. "cssRules": "font-family:STXinwei"
  222. }
  223. ],
  224. oninit: function () {
  225. this.defaultSelectValue = this.selectValue;
  226. this.selectValue = function (value) {
  227. this.element.style.fontFamily = "";
  228. this.element.style.cssText = "font-family:" + value + " !important;" + this.element.style.cssText;
  229. this.defaultSelectValue(value);
  230. }
  231. }
  232. });
  233. return btn;
  234. });
  235. manager.registerUI('helpstyle', function (wdeditor, name) {
  236. wdeditor.getUeditor().registerCommand(name, {
  237. queryCommandValue: function () {
  238. var sn = wdeditor.getUeditor().selection,
  239. range = sn.getRange(),
  240. startNode;
  241. startNode = range.getClosedNode() || sn.getStart();
  242. return startNode.className;
  243. },
  244. execCommand: function (cmdName, className) {
  245. var sn = wdeditor.getUeditor().selection,
  246. range = sn.getRange(),
  247. node;
  248. node = range.getClosedNode() || sn.getStart();
  249. node.className = className;
  250. }
  251. });
  252. var btn = new EditorManager.Combobox({
  253. name: name,
  254. command: name,
  255. label: "",
  256. labelCss: "",
  257. text: "帮助样式",
  258. title: "样式",
  259. // cssRules: "width:7em;",
  260. items: [{
  261. "text": "正文",
  262. "value": "help"
  263. }, {
  264. "text": "标题",
  265. "value": "help-title"
  266. }, {
  267. "text": "一级标题",
  268. "value": "help-title1"
  269. }, {
  270. "text": "二级标题",
  271. "value": "help-title2"
  272. }, {
  273. "text": "项目",
  274. "value": "help-item"
  275. }
  276. ],
  277. oninit: function () {
  278. this.defaultSelectValue = this.selectValue;
  279. this.selectValue = function (value) {
  280. this.element.style.fontFamily = "";
  281. this.element.style.cssText = "font-family:" + value + " !important;" + this.element.style.cssText;
  282. this.defaultSelectValue(value);
  283. }
  284. // $(wdeditor.getUeditor().document.head).append('<link rel="stylesheet" type="text/css" href="/zzfb_/theme/Green/general.css">')
  285. },
  286. onSelect: function (value) {
  287. wdeditor.execCommand("helpstyle", value);
  288. }
  289. });
  290. return btn;
  291. });
  292. manager.registerUI('mobilehelpstyle', function (wdeditor, name) {
  293. wdeditor.getUeditor().registerCommand(name, {
  294. queryCommandValue: function () {
  295. var sn = wdeditor.getUeditor().selection,
  296. range = sn.getRange(),
  297. startNode;
  298. startNode = range.getClosedNode() || sn.getStart();
  299. return startNode.className;
  300. },
  301. execCommand: function (cmdName, className) {
  302. var sn = wdeditor.getUeditor().selection,
  303. range = sn.getRange(),
  304. node;
  305. node = range.getClosedNode() || sn.getStart();
  306. node.className = className;
  307. }
  308. });
  309. var btn = new EditorManager.Combobox({
  310. name: name,
  311. command: name,
  312. label: "",
  313. labelCss: "",
  314. text: "帮助样式",
  315. title: "样式",
  316. // cssRules: "width:7em;",
  317. items: [{
  318. "text": "正文",
  319. "value": "mobileHelp-text"
  320. }, {
  321. "text": "一级标题",
  322. "value": "mobileHelp-title1Text"
  323. }
  324. ],
  325. oninit: function () {
  326. this.defaultSelectValue = this.selectValue;
  327. this.selectValue = function (value) {
  328. this.element.style.fontFamily = "";
  329. this.element.style.cssText = "font-family:" + value + " !important;" + this.element.style.cssText;
  330. this.defaultSelectValue(value);
  331. }
  332. // $(wdeditor.getUeditor().document.head).append('<link rel="stylesheet" type="text/css" href="/zzfb_/theme/Green/general.css">')
  333. },
  334. onSelect: function (value) {
  335. wdeditor.execCommand("mobilehelpstyle", value);
  336. }
  337. });
  338. return btn;
  339. });
  340. manager.registerUI('fontsize', function (wdeditor, name) {
  341. var its = [{
  342. "text": "初号",
  343. "value": "56px",
  344. "cssRules": "font-size:56px"
  345. }, {
  346. "text": "小初",
  347. "value": "48px",
  348. "cssRules": "font-size:48px"
  349. }, {
  350. "text": "一号",
  351. "value": "34.7px",
  352. "cssRules": "font-size:34.7px"
  353. }, {
  354. "text": "小一",
  355. "value": "32px",
  356. "cssRules": "font-size:32px"
  357. }, {
  358. "text": "二号",
  359. "value": "29.3px",
  360. "cssRules": "font-size:29.3px"
  361. }, {
  362. "text": "小二",
  363. "value": "24px",
  364. "cssRules": "font-size:24px"
  365. }, {
  366. "text": "三号",
  367. "value": "21px",
  368. "cssRules": "font-size:21px"
  369. }, {
  370. "text": "小三",
  371. "value": "20px",
  372. "cssRules": "font-size:20px"
  373. }, {
  374. "text": "四号",
  375. "value": "18.7px",
  376. "cssRules": "font-size:18.7px"
  377. }, {
  378. "text": "小四",
  379. "value": "16px",
  380. "cssRules": "font-size:16px"
  381. }, {
  382. "text": "五号",
  383. "value": "14px",
  384. "cssRules": "font-size:14px"
  385. }, {
  386. "text": "小五",
  387. "value": "12px",
  388. "cssRules": "font-size:12px"
  389. }
  390. ];
  391. var docele = wdeditor.getUeditor().iframe.contentDocument.documentElement;
  392. var size = parseInt(window.getComputedStyle(docele).fontSize);
  393. for (var iii = 0; iii < its.length; iii++) {
  394. var vvv = parseInt(its[iii].value);
  395. var rem = vvv / size;
  396. its[iii].value = rem + "rem";
  397. its[iii].cssRules = "font-size:" + rem + "rem";
  398. }
  399. console.log(size);
  400. console.log(its);
  401. var btn = new EditorManager.Combobox({
  402. name: name,
  403. command: name,
  404. text: "字号",
  405. title: "字号",
  406. cssRules: "height: 25px;line-height: 25px;position: relative;bottom: 3px;min-width:3em;",
  407. className: "zihao",
  408. items: its,
  409. baseSize: size,
  410. onBtnMouseDown: function () {
  411. var opts = this.getElement().options;
  412. var font = this.wdeditor.queryCommandValue("fontfamily");
  413. for (var i = 0; i < opts.length; i++) {
  414. opts[i].style.fontFamily = font;
  415. }
  416. },
  417. setSelectionChange: function () {
  418. var me=this;
  419. wdeditor.addListener("selectionchange", function () {
  420. var value = wdeditor.queryCommandValue(btn.getCommand());
  421. if (value.endsWith("px")) {
  422. value = parseInt(value) / me.baseSize + "rem";
  423. }
  424. me.selectValue(value);
  425. });
  426. },
  427. });
  428. return btn;
  429. });
  430. manager.registerUI('fontcolor', function (wdeditor, name) {
  431. var btn = new EditorManager.Button({
  432. name: name,
  433. className: "icon-color",
  434. command: 'forecolor',
  435. wdeditor: wdeditor,
  436. text: "",
  437. title: "颜色",
  438. onBtnClick: function () {
  439. console.log(this.getElement().offsetTop);
  440. //this.prop.css({"top":this.getElement().offsetTop+this.getElement().offsetHeight,"left":""});
  441. var h = this.getElement().offsetHeight;
  442. var ofs = $(this.getElement()).offset();
  443. ofs.top += h + 3;
  444. this.popup.showAt(ofs);
  445. },
  446. _onPickColor: function (color) {
  447. wdeditor.execCommand(this.getCommand(), color);
  448. this.setColor && this.setColor(color);
  449. this.popup.hide();
  450. },
  451. _onpicknocolor: function (color) {
  452. this.popup.hide();
  453. },
  454. setSelectionChange: function () {
  455. wdeditor.addListener("selectionchange", function () {
  456. var value = wdeditor.queryCommandValue(btn.getCommand());
  457. if (btn.setColor) {
  458. btn.setColor(value);
  459. }
  460. });
  461. },
  462. setColor: function (color) {
  463. this.getElement().style.backgroundColor = color || 'black';
  464. },
  465. oninit: function () {
  466. var this_ = this;
  467. var picker = new baidu.editor.ui.ColorPicker({
  468. noColorText: wdeditor.getUeditor().getLang("clearColor"),
  469. editor: wdeditor.getUeditor(),
  470. onpickcolor: function (t, color) {
  471. this_._onPickColor(color);
  472. },
  473. onpicknocolor: function (t, color) {
  474. this_._onPickNoColor(color);
  475. }
  476. })
  477. var popup = new baidu.editor.ui.Popup({
  478. content: picker,
  479. editor: wdeditor.getUeditor()
  480. });
  481. this.popup = popup;
  482. }
  483. });
  484. return btn;
  485. });
  486. manager.registerUI('insertimage', function (wdeditor, name) {
  487. var btn = new EditorManager.Button({
  488. name: name,
  489. className: "icon-image",
  490. text: "",
  491. title: "图片",
  492. command: "-",
  493. onBtnClick: function () {
  494. console.log(this.input)
  495. this.input.click();
  496. },
  497. oninit: function () {
  498. var ele = this.getElement();
  499. var uploaddiv = document.createElement("div");
  500. uploaddiv.id = btn.getBtnID() + new Date().toString(36);
  501. uploaddiv.style.display = "none";
  502. ele.parentNode.insertBefore(uploaddiv, ele);
  503. this.uploaddiv = uploaddiv;
  504. this.appendUploadFrame();
  505. },
  506. appendUploadFrame: function () {
  507. var this_ = this;
  508. var ele = this_.getElement();
  509. var input = document.createElement("input");
  510. input.type = "file";
  511. input.setAttribute("accept", "image/*");
  512. input.name = "upfile";
  513. var form = document.createElement("form");
  514. form.method = "post";
  515. form.enctype = "multipart/form-data";
  516. form.action = this_.wdeditor.getUeditor().getOpt("serverUrl");
  517. // var uploadHtml = '<form style="display:none;" method="POST" enctype="multipart/form-data" action="' + this.getUeditor().getOpt("serverUrl") + '"><input type="file" name="upfile" access="image/*"/>';
  518. form.appendChild(input);
  519. var frame = document.createElement("iframe");
  520. frame.style.display = "none";
  521. this_.uploaddiv.innerHTML = "";
  522. this_.uploaddiv.appendChild(frame);
  523. (frame.contentWindow || frame.contentDocument).document.body.appendChild(form);
  524. input.onchange = function () {
  525. this_.filechange(this)
  526. };
  527. this_.frame = frame;
  528. this_.input = input;
  529. this_.form = form;
  530. },
  531. filechange: function (input) {
  532. var this_ = this;
  533. var utils = UE.utils;
  534. var me = wdeditor.getUeditor();
  535. var domUtils = UE.dom.domUtils;
  536. if (!input.value)
  537. return;
  538. var loadingId = 'loading_' + (new Date()).getTime().toString(36);
  539. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  540. var imageActionUrl = me.getActionUrl(me
  541. .getOpt('imageActionName'));
  542. var allowFiles = me.getOpt('imageAllowFiles');
  543. me.focus();
  544. me.execCommand('inserthtml', '<img class="loadingclass" id="' + loadingId + '" src="' + me.options.themePath + me.options.theme + '/images/spacer.gif" title="' + (me.getLang('simpleupload.loading') || '') + '" >');
  545. function callback() {
  546. try {
  547. var link,
  548. json,
  549. loader,
  550. body = (this_.frame.contentDocument || this_.frame.contentWindow.document).body,
  551. result = body.innerText ||
  552. body.textContent || '';
  553. json = (new Function("return " + result))();
  554. link = me.options.imageUrlPrefix + json.url;
  555. if (json.state == 'SUCCESS' && json.url) {
  556. loader = me.document.getElementById(loadingId);
  557. loader.setAttribute("srcAttrName", "src");
  558. loader.setAttribute("tokentag", "true");
  559. // loader.setAttribute("source", json.url);
  560. loader.setAttribute('src', json.tokenURL);
  561. loader.setAttribute('_src', json.tokenURL);
  562. loader.setAttribute('title', json.title || '');
  563. loader.setAttribute('alt', json.original || '');
  564. loader.removeAttribute('id');
  565. domUtils.removeClasses(loader, 'loadingclass');
  566. } else {
  567. var size = this_.getSize();
  568. console.log(size)
  569. size.top += size.height;
  570. this_.popup.showMsg(json.state, size);
  571. showErrorLoader && showErrorLoader(json.state);
  572. }
  573. } catch (er) {
  574. this_.popup.showMsg(json.state, this.offset());
  575. showErrorLoader
  576. &&
  577. showErrorLoader(me
  578. .getLang('simpleupload.loadError'));
  579. }
  580. this_.form.reset();
  581. domUtils.un(this_.frame, 'load', callback);
  582. this_.appendUploadFrame();
  583. }
  584. function showErrorLoader(title) {
  585. if (loadingId) {
  586. var loader = me.document.getElementById(loadingId);
  587. loader && domUtils.remove(loader);
  588. me.fireEvent('showmessage', {
  589. 'id': loadingId,
  590. 'content': title,
  591. 'type': 'error',
  592. 'timeout': 4000
  593. });
  594. }
  595. }
  596. /* 判断后端配置是否没有加载成功 */
  597. if (!me.getOpt('imageActionName')) {
  598. showErrorLoader(me.getLang('autoupload.errorLoadConfig'));
  599. return;
  600. }
  601. // 判断文件格式是否错误
  602. var filename = input.value,
  603. fileext = filename ? filename
  604. .substr(filename.lastIndexOf('.')) : '';
  605. if (!fileext ||
  606. (allowFiles && (allowFiles.join('') + '.')
  607. .indexOf(fileext.toLowerCase() + '.') == -1)) {
  608. showErrorLoader(me
  609. .getLang('simpleupload.exceedTypeError'));
  610. return;
  611. }
  612. domUtils.on(btn.frame, 'load', callback);
  613. btn.form.action = utils.formatUrl(imageActionUrl +
  614. (imageActionUrl.indexOf('?') == -1 ? '?' : '&') + params); // + params) + getWdApp()。去掉 ?wdApplication=,不支持多个应用Lin
  615. btn.form.submit();
  616. }
  617. });
  618. var pop = new manager.msgPopup();
  619. pop.init();
  620. btn.popup = pop;
  621. return btn;
  622. });
  623. manager.registerUI('insertDoc', function (wdeditor, name) {
  624. var btn = new EditorManager.Button({
  625. name: name,
  626. className: "icon-word",
  627. text: "",
  628. title: "word",
  629. command: "-",
  630. onBtnClick: function () {
  631. console.log(this.input)
  632. this.input.click();
  633. },
  634. oninit: function () {
  635. var ele = this.getElement();
  636. var uploaddiv = document.createElement("div");
  637. uploaddiv.id = btn.getBtnID() + new Date().toString(36);
  638. uploaddiv.style.display = "none";
  639. ele.parentNode.insertBefore(uploaddiv, ele);
  640. this.uploaddiv = uploaddiv;
  641. this.appendUploadFrame();
  642. },
  643. appendUploadFrame: function () {
  644. var this_ = this;
  645. var ele = this_.getElement();
  646. var input = document.createElement("input");
  647. input.type = "file";
  648. input.setAttribute("accept", ".doc,.docx");
  649. input.name = "upfile";
  650. input.onchange = function () {
  651. this_.filechange(this)
  652. };
  653. this_.input = input;
  654. },
  655. filechange: function (input) {
  656. // 没有选择文件
  657. if (!input.files[0]) {
  658. return;
  659. }
  660. wd.display.showLoad();
  661. var formData = new FormData();
  662. formData.append("file", input.files[0]);
  663. formData.append("name", input.name);
  664. $.ajax({
  665. type: "post",
  666. url: "/service?ssServ=u_uploadDoc",
  667. async: true,
  668. data: formData,
  669. processData: false,
  670. contentType: false,
  671. dataType: 'json',
  672. success: function (data) {
  673. if (data.msg != "success") {
  674. alert("插入失败!")
  675. wd.display.closeLoad();
  676. return;
  677. }
  678. $.ajax({
  679. type: "post",
  680. url: "/service?ssServ=u_docToHtml",
  681. async: true,
  682. data: {
  683. "filePath": data.data.src
  684. },
  685. success: function (data) {
  686. wdeditor.getUeditor().execCommand("insertHTML", data);
  687. wd.display.closeLoad();
  688. },
  689. error: function () {
  690. wdeditor.getUeditor().execCommand("insertHTML", "error");
  691. wd.display.closeLoad();
  692. }
  693. });
  694. },
  695. error: function () {
  696. alert("ERROR");
  697. wd.display.closeLoad();
  698. }
  699. });
  700. }
  701. });
  702. var pop = new manager.msgPopup();
  703. pop.init();
  704. btn.popup = pop;
  705. return btn;
  706. });
  707. manager.registerUI('insertExcel', function (wdeditor, name) {
  708. var btn = new EditorManager.Button({
  709. name: name,
  710. className: "icon-excel",
  711. text: "",
  712. title: "excel",
  713. command: "-",
  714. onBtnClick: function () {
  715. console.log(this.input)
  716. this.input.click();
  717. },
  718. oninit: function () {
  719. var ele = this.getElement();
  720. var uploaddiv = document.createElement("div");
  721. uploaddiv.id = btn.getBtnID() + new Date().toString(36);
  722. uploaddiv.style.display = "none";
  723. ele.parentNode.insertBefore(uploaddiv, ele);
  724. this.uploaddiv = uploaddiv;
  725. this.appendUploadFrame();
  726. },
  727. appendUploadFrame: function () {
  728. var this_ = this;
  729. var ele = this_.getElement();
  730. var input = document.createElement("input");
  731. input.type = "file";
  732. input.setAttribute("accept", ".xls,.xlsx");
  733. input.name = "upfile";
  734. input.onchange = function () {
  735. this_.filechange(this)
  736. };
  737. this_.input = input;
  738. },
  739. filechange: function (input) {
  740. // 没有选择文件
  741. if (!input.files[0]) {
  742. return;
  743. }
  744. var formData = new FormData();
  745. formData.append("file", input.files[0]);
  746. formData.append("name", input.name);
  747. $.ajax({
  748. type: "post",
  749. url: "/service?ssServ=u_uploadDoc",
  750. async: true,
  751. data: formData,
  752. processData: false,
  753. contentType: false,
  754. dataType: 'json',
  755. success: function (data) {
  756. if (data.msg != "success") {
  757. alert("插入失败!")
  758. return;
  759. }
  760. $.ajax({
  761. type: "post",
  762. url: "/service?ssServ=u_docToHtml",
  763. async: true,
  764. data: {
  765. "filePath": data.data.src
  766. },
  767. success: function (data) {
  768. wdeditor.getUeditor().execCommand("insertHTML", data);
  769. },
  770. error: function () {
  771. wdeditor.getUeditor().execCommand("insertHTML", "error!");
  772. }
  773. });
  774. },
  775. error: function () {
  776. alert("ERROR");
  777. }
  778. });
  779. }
  780. });
  781. var pop = new manager.msgPopup();
  782. pop.init();
  783. btn.popup = pop;
  784. return btn;
  785. });
  786. manager.registerUI('customstyle', function (wdeditor, name) {
  787. console.log(wdeditor.options.template);
  788. //获取items
  789. var btn = new EditorManager.Combobox({
  790. name: name,
  791. command: name,
  792. className: "wenbenStyle",
  793. text: "文本样式",
  794. title: "文本样式",
  795. cssRules: "min-width:3em;line-height: 25px;position: relative;height:25px;margin-left:7px;",
  796. onBtnMouseDown: function () {
  797. var opts = this.getElement().options;
  798. var font = this.wdeditor.queryCommandValue(this.getCommand());
  799. for (var i = 0; i < opts.length; i++) {
  800. opts[i].style.fontFamily = font;
  801. }
  802. },
  803. oninit: function () {
  804. loadStyles();
  805. }
  806. });
  807. var loadStyles = window.loadStyles = function (json, callback) {
  808. var tmpl = wdeditor.options.template
  809. if (!tmpl) {
  810. btn.hide();
  811. } else {
  812. var items = [];
  813. if (json != null) {
  814. console.log(json)
  815. if (typeof(json) == "string") {
  816. json = JSON.parse(json);
  817. }
  818. for (var key in json) {
  819. var item = json[key];
  820. var _item = {
  821. "text": item.name,
  822. "cssRules": item.style,
  823. "tag": item.tag,
  824. value: {
  825. "tag": item.tag,
  826. "label": item.name,
  827. "style": item.style
  828. }
  829. };
  830. items.push(_item);
  831. }
  832. } else {
  833. console.log(wdeditor.options.template + "没有样式");
  834. }
  835. wdeditor.getUeditor().setOpt({
  836. "customstyle": items
  837. });
  838. btn.menu.setItems(items, true);
  839. if (callback) {
  840. callback();
  841. }
  842. }
  843. }
  844. return btn;
  845. });
  846. manager.registerUI('helpimage', function (wdeditor, name) {
  847. var btn = new EditorManager.Button({
  848. name: name,
  849. className: "icon-image",
  850. text: "",
  851. title: "帮助图片",
  852. command: "-",
  853. onBtnClick: function () {
  854. console.log(this.input)
  855. this.input.click();
  856. },
  857. oninit: function () {
  858. var ele = this.getElement();
  859. var uploaddiv = document.createElement("div");
  860. uploaddiv.id = btn.getBtnID() + new Date().toString(36);
  861. uploaddiv.style.display = "none";
  862. ele.parentNode.insertBefore(uploaddiv, ele);
  863. this.uploaddiv = uploaddiv;
  864. this.appendUploadFrame();
  865. },
  866. appendUploadFrame: function () {
  867. var this_ = this;
  868. var ele = this_.getElement();
  869. var input = document.createElement("input");
  870. input.type = "file";
  871. input.setAttribute("accept", "image/*");
  872. input.name = "upfile";
  873. var form = document.createElement("form");
  874. form.method = "post";
  875. form.enctype = "multipart/form-data";
  876. form.action = this_.wdeditor.getUeditor().getOpt("serverUrl");
  877. // var uploadHtml = '<form style="display:none;" method="POST" enctype="multipart/form-data" action="' + this.getUeditor().getOpt("serverUrl") + '"><input type="file" name="upfile" access="image/*"/>';
  878. form.appendChild(input);
  879. var frame = document.createElement("iframe");
  880. frame.style.display = "none";
  881. this_.uploaddiv.innerHTML = "";
  882. this_.uploaddiv.appendChild(frame);
  883. (frame.contentWindow || frame.contentDocument).document.body.appendChild(form);
  884. input.onchange = function () {
  885. this_.filechange(this)
  886. };
  887. this_.frame = frame;
  888. this_.input = input;
  889. this_.form = form;
  890. },
  891. filechange: function (input) {
  892. var this_ = this;
  893. var utils = UE.utils;
  894. var me = wdeditor.getUeditor();
  895. var domUtils = UE.dom.domUtils;
  896. if (!input.value)
  897. return;
  898. var loadingId = 'loading_' + (new Date()).getTime().toString(36);
  899. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  900. if ($("input[name='helpAppName']").size() > 0 && $("input[name='helpAppName']")[0].value != "") {
  901. params += "&helpAppName=" + $("input[name='helpAppName']")[0].value;
  902. }
  903. if ($("input[name='helpObjectName']").size() > 0 && $("input[name='helpObjectName']")[0].value != "") {
  904. params += "&helpObjectName=" + $("input[name='helpObjectName']")[0].value;
  905. }
  906. if ($("input[name='helpBusiness']").size() > 0 && $("input[name='helpBusiness']")[0].value != "") {
  907. params += "&helpBusiness=" + $("input[name='helpBusiness']")[0].value;
  908. }
  909. var imageActionUrl = me.getActionUrl(me
  910. .getOpt('imageActionName'));
  911. var allowFiles = me.getOpt('imageAllowFiles');
  912. me.focus();
  913. me.execCommand('inserthtml', '<img class="loadingclass" id="' + loadingId + '" src="' + me.options.themePath + me.options.theme + '/images/spacer.gif" title="' + (me.getLang('simpleupload.loading') || '') + '" >');
  914. function callback() {
  915. try {
  916. var link,
  917. json,
  918. loader,
  919. body = (this_.frame.contentDocument || this_.frame.contentWindow.document).body,
  920. result = body.innerText ||
  921. body.textContent || '';
  922. json = (new Function("return " + result))();
  923. link = me.options.imageUrlPrefix + json.url;
  924. if (json.state == 'SUCCESS' && json.url) {
  925. loader = me.document.getElementById(loadingId);
  926. loader.setAttribute("srcAttrName", "src");
  927. loader.setAttribute("tokentag", "0" == json.tokentag ? "false" : "true");
  928. // loader.setAttribute("source", json.url);
  929. loader.setAttribute('src', json.tokenURL);
  930. loader.setAttribute('_src', json.tokenURL);
  931. loader.setAttribute('title', json.title || '');
  932. loader.setAttribute('alt', json.original || '');
  933. loader.removeAttribute('id');
  934. domUtils.removeClasses(loader, 'loadingclass');
  935. } else {
  936. var size = this_.getSize();
  937. console.log(size)
  938. size.top += size.height;
  939. this_.popup.showMsg(json.state, size);
  940. showErrorLoader && showErrorLoader(json.state);
  941. }
  942. } catch (er) {
  943. this_.popup.showMsg(json.state, this.offset());
  944. showErrorLoader
  945. &&
  946. showErrorLoader(me
  947. .getLang('simpleupload.loadError'));
  948. }
  949. this_.form.reset();
  950. domUtils.un(this_.frame, 'load', callback);
  951. this_.appendUploadFrame();
  952. }
  953. function showErrorLoader(title) {
  954. if (loadingId) {
  955. var loader = me.document.getElementById(loadingId);
  956. loader && domUtils.remove(loader);
  957. me.fireEvent('showmessage', {
  958. 'id': loadingId,
  959. 'content': title,
  960. 'type': 'error',
  961. 'timeout': 4000
  962. });
  963. }
  964. }
  965. /* 判断后端配置是否没有加载成功 */
  966. if (!me.getOpt('imageActionName')) {
  967. showErrorLoader(me.getLang('autoupload.errorLoadConfig'));
  968. return;
  969. }
  970. // 判断文件格式是否错误
  971. var filename = input.value,
  972. fileext = filename ? filename
  973. .substr(filename.lastIndexOf('.')) : '';
  974. if (!fileext ||
  975. (allowFiles && (allowFiles.join('') + '.')
  976. .indexOf(fileext.toLowerCase() + '.') == -1)) {
  977. showErrorLoader(me
  978. .getLang('simpleupload.exceedTypeError'));
  979. return;
  980. }
  981. domUtils.on(btn.frame, 'load', callback);
  982. btn.form.action = utils.formatUrl(imageActionUrl +
  983. (imageActionUrl.indexOf('?') == -1 ? '?' : '&') + params) +"&ishelp=1"; // + params) + getWdApp() +。去掉 ?wdApplication=,不支持多个应用Lin
  984. btn.form.submit();
  985. }
  986. });
  987. var pop = new manager.msgPopup();
  988. pop.init();
  989. btn.popup = pop;
  990. return btn;
  991. });