space.ueditor.toolbar.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. $(document).ready(function() {
  2. space.ueditor.toolbar.init.myFace();// 初始化,自定义百度编辑器的《表情》工具按钮
  3. space.ueditor.toolbar.init.commonLanguage();// 初始化,自定义百度编辑器的《常用语》工具按钮
  4. space.ueditor.toolbar.init.cmsFileSystem();// 初始化,自定义百度编辑器的《附件》工具按钮
  5. });
  6. space.ueditor = {
  7. toolbar: {}
  8. }
  9. space.ueditor.toolbar = {
  10. /**
  11. * 模板
  12. */
  13. template: {
  14. // 气泡模板
  15. airbubbles: '<div id="{airbubbles}" class="bubble-layer" style="left: 300px; top: 100px;"><div class="bubble-layer-content"></div><div class="bubble-layer-arrows"></div></div>',
  16. // 微标
  17. badge: '<span id="{badge}" class="fileDot"></span>',
  18. },
  19. /**
  20. * 关闭所有弹层
  21. */
  22. closeAllBubbleLayer: function() {
  23. $(".bubble-layer").fadeOut(100);
  24. },
  25. /**
  26. * 注册按钮事件
  27. * @param {Object} btnId
  28. * @param {Object} callback
  29. */
  30. regClickDocument: function(btnId, callback) {
  31. // 全局注册点击事件
  32. document.onclick = function(e) {
  33. var len = $(e.target).parents("div[id='" + btnId + "']").length;
  34. if(e.target.id != btnId && len == 0) {
  35. space.ueditor.toolbar.closeAllBubbleLayer();
  36. }
  37. if(callback) {
  38. callback(e)
  39. }
  40. }
  41. },
  42. /**
  43. * 追加内容
  44. * @param {Object} elId 元素 id
  45. * @param {Object} html 内容
  46. */
  47. appendHtml: function(elId, html) {
  48. $("#" + elId).find('.bubble-layer-content').append(html);
  49. },
  50. editorAppendHtml: function(editor, html) {
  51. editor.execCommand('inserthtml', html);
  52. },
  53. /**
  54. * 重置气泡大小
  55. */
  56. resetAirbubblesSize: function(editor, btn, id) {
  57. // 编辑器的坐标
  58. var ediOffSet = $(editor.iframe).parent().offset();
  59. // 初始化坐标
  60. var btnOffSet = $(btn.target).offset();
  61. var airbubblesLeft = 0;// 气泡窗口,左边距离
  62. var arrowsLeft = 30;// 三角箭头
  63. var btnLeft = (btnOffSet.left - 26);
  64. // 计算左边的距离
  65. if (ediOffSet.left <= 0 && btnLeft < ediOffSet.left) {
  66. airbubblesLeft = btnOffSet.left;
  67. arrowsLeft = btnOffSet.left - 4;
  68. }
  69. else {
  70. airbubblesLeft = btnLeft;
  71. }
  72. var airbubblesTop = (btnOffSet.top - $(id).height() - 16);
  73. var airbubblesHeight = 112;
  74. // 计算高度
  75. if (airbubblesHeight > airbubblesTop) {
  76. airbubblesHeight = airbubblesHeight - (airbubblesHeight - btnOffSet.top + 16)
  77. }
  78. if (airbubblesTop < 0) {
  79. airbubblesTop = 0;
  80. }
  81. // 气泡窗口
  82. $(id).css({
  83. "top": airbubblesTop + "px",
  84. "left": airbubblesLeft + "px",
  85. "height": airbubblesHeight + "px",
  86. });
  87. // 三角箭头
  88. $(id).find('.bubble-layer-arrows').css({
  89. "left": arrowsLeft + "px"
  90. })
  91. },
  92. init: {
  93. /**
  94. * 表情
  95. */
  96. myFace: function() {
  97. var id = space.utils.randomString(8);
  98. $('body').append(space.ueditor.toolbar.template.airbubbles.replace("{airbubbles}", id));
  99. $("#" + id).css({
  100. 'width': '300px',
  101. 'height': '112px'
  102. });
  103. for(var i = 1; i <= 32; i++) {
  104. var html = '<img class="face-img" src="/apps/space/images/ueditor/faces/FACE_' + i + '.png">';
  105. space.ueditor.toolbar.appendHtml(id, html);
  106. }
  107. space.plugins.ueditor.registerUeditorToolBar.button({
  108. title: '表情',
  109. uiName: 'myFace',
  110. icon: '/apps/space/images/ueditor/MyFace.png',
  111. click: function(editor, btn, param) {
  112. space.ueditor.toolbar.closeAllBubbleLayer();
  113. var id = "#" + param.id;
  114. // 如果已经显示了,再次点击就隐藏掉
  115. if(!$(id).is(':hidden')) {
  116. return;
  117. }
  118. space.ueditor.toolbar.resetAirbubblesSize(editor, btn, id);
  119. $(id).fadeIn(200);
  120. space.ueditor.toolbar.regClickDocument(btn.id, function(e) {
  121. if(e.target.nodeName == "IMG" && $(e.target).parents("div[id='" + param.id + "']").length == 1) {
  122. space.ueditor.toolbar.editorAppendHtml(editor, "<img width='26px' src='" + $(e.target).attr("src") + "' />");
  123. }
  124. });
  125. },
  126. param: {
  127. id: id
  128. }
  129. })
  130. },
  131. /**
  132. * 常用语
  133. */
  134. commonLanguage: function() {
  135. var id = space.utils.randomString(8);
  136. $('body').append(space.ueditor.toolbar.template.airbubbles.replace("{airbubbles}", id));
  137. $("#" + id).css({
  138. 'width': '300px',
  139. 'height': '112px'
  140. });
  141. $.ajax({
  142. type: "get",
  143. url: "/apps?ark=personCustomPhrase_list&app=personnel",
  144. async: false,
  145. dataType: 'json',
  146. success: function(data) {
  147. if(data.code == 'success') {
  148. var list = data.personCustomPhraseList;
  149. if(!list || list.length == 0) {
  150. var noDataHtml = '<div style="width:100%;height:100%;color:#999;text-align:center;line-height:110px;cursor: default;"><i class="el-icon-warning-outline"></i>&nbsp;未设置常用语</div>';
  151. space.ueditor.toolbar.appendHtml(id, noDataHtml);
  152. return;
  153. }
  154. var html = '<ul class="ul-list">';
  155. for (var i = 0;i < list.length;i++) {
  156. html += '<li>' + list[i].content + '</li>';
  157. }
  158. html += '</ul>';
  159. space.ueditor.toolbar.appendHtml(id, html);
  160. }
  161. }
  162. });
  163. space.plugins.ueditor.registerUeditorToolBar.button({
  164. title: '常用语',
  165. uiName: 'commonLanguage',
  166. icon: '/apps/space/images/ueditor/CommonLanuage.png',
  167. click: function(editor, btn, param) {
  168. space.ueditor.toolbar.closeAllBubbleLayer();
  169. var id = "#" + param.id;
  170. // 如果已经显示了,再次点击就隐藏掉
  171. if(!$("#" + param.id).is(':hidden')) {
  172. return;
  173. }
  174. space.ueditor.toolbar.resetAirbubblesSize(editor, btn, id);
  175. $(id).fadeIn(200);
  176. space.ueditor.toolbar.regClickDocument(btn.id, function(e) {
  177. if(e.target.nodeName == "LI" && $(e.target).parents("div[id='" + param.id + "']").length == 1 &&
  178. $(e.target).parent("ul.ul-list").length == 1) {
  179. // 插入内容
  180. space.ueditor.toolbar.editorAppendHtml(editor, e.target.innerText);
  181. }
  182. });
  183. },
  184. param: {
  185. id: id
  186. }
  187. });
  188. },
  189. /**
  190. * 附件
  191. */
  192. cmsFileSystem: function() {
  193. var id = space.utils.randomString(8);
  194. $('body').append(space.ueditor.toolbar.template.badge.replace("{badge}", id));
  195. space.plugins.ueditor.registerUeditorToolBar.button({
  196. title: '附件',
  197. uiName: 'cmsFileSystem',
  198. icon: '/apps/space/images/ueditor/CmsFileSystem.png',
  199. ready: function(editor, btn) {
  200. $("#" + id).on('click', function() {
  201. $(btn).click();
  202. });
  203. },
  204. click: function(editor, btn, param) {
  205. // 初始化《附件》的 CMS 参数
  206. editor.CMS.CMS_badge = param.id;
  207. var winSetting = {"title": "附件", "width": "90%", "height": "90%", "url": "/apps?app=sys&destination=sys.cms.uploading"};
  208. // 打开 CMS 上传文件的窗口
  209. space.plugins.layer.openPredefinePage(winSetting, {
  210. success: function(win) {
  211. win.CMS = {
  212. id: editor.CMS.childID
  213. }
  214. },
  215. end: function() {
  216. if (!window.CMS) {
  217. return
  218. }
  219. var badgeId = "#" + editor.CMS.CMS_badge;// 当前插件在工具类的 id
  220. var fileCount = window.CMS.fileCount;// 已上传的附件数量
  221. editor.CMS.childID = window.CMS.id;
  222. var btnOffSet = $(btn.target).offset();
  223. $(badgeId).css({
  224. "top": (btnOffSet.top - $(badgeId).height() + 12) + "px",
  225. "left": (btnOffSet.left + 16) + "px",
  226. });
  227. $(badgeId).text(fileCount);
  228. $(badgeId).show();
  229. }
  230. });
  231. },
  232. param: {
  233. id: id,
  234. }
  235. });
  236. }
  237. }
  238. }