space.ueditor.toolbar.js 5.7 KB

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