addons.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. define([], function () {
  2. require([], function () {
  3. //绑定data-toggle=addresspicker属性点击事件
  4. $(document).on('click', "[data-toggle='addresspicker']", function () {
  5. var that = this;
  6. var callback = $(that).data('callback');
  7. var input_id = $(that).data("input-id") ? $(that).data("input-id") : "";
  8. var lat_id = $(that).data("lat-id") ? $(that).data("lat-id") : "";
  9. var lng_id = $(that).data("lng-id") ? $(that).data("lng-id") : "";
  10. var zoom_id = $(that).data("zoom-id") ? $(that).data("zoom-id") : "";
  11. var lat = lat_id ? $("#" + lat_id).val() : '';
  12. var lng = lng_id ? $("#" + lng_id).val() : '';
  13. var zoom = zoom_id ? $("#" + zoom_id).val() : '';
  14. var url = "/addons/address/index/select";
  15. url += (lat && lng) ? '?lat=' + lat + '&lng=' + lng + (input_id ? "&address=" + $("#" + input_id).val() : "") + (zoom ? "&zoom=" + zoom : "") : '';
  16. Fast.api.open(url, '位置选择', {
  17. callback: function (res) {
  18. input_id && $("#" + input_id).val(res.address).trigger("change");
  19. lat_id && $("#" + lat_id).val(res.lat).trigger("change");
  20. lng_id && $("#" + lng_id).val(res.lng).trigger("change");
  21. zoom_id && $("#" + zoom_id).val(res.zoom).trigger("change");
  22. try {
  23. //执行回调函数
  24. if (typeof callback === 'function') {
  25. callback.call(that, res);
  26. }
  27. } catch (e) {
  28. }
  29. }
  30. });
  31. });
  32. });
  33. require.config({
  34. paths: {
  35. 'vue': '../addons/service/js/vue.min'
  36. },
  37. shim: {
  38. }
  39. });
  40. require(['vue'], function(Vue, Element){
  41. //插件逻辑代码
  42. });
  43. require.config({
  44. paths: {
  45. 'simditor': '../addons/simditor/js/simditor',
  46. 'simple-module': '../addons/simditor/js/module',
  47. 'simple-hotkeys': '../addons/simditor/js/hotkeys',
  48. 'simple-uploader': '../addons/simditor/js/uploader',
  49. 'dompurify': '../addons/simditor/js/dompurify',
  50. },
  51. shim: {
  52. 'simditor': [
  53. 'css!../addons/simditor/css/simditor.min.css',
  54. ]
  55. }
  56. });
  57. require(['form'], function (Form) {
  58. var _bindevent = Form.events.bindevent;
  59. Form.events.bindevent = function (form) {
  60. _bindevent.apply(this, [form]);
  61. if ($(Config.simditor.classname || '.editor', form).length > 0) {
  62. //修改上传的接口调用
  63. require(['upload', 'simditor', 'dompurify'], function (Upload, Simditor, DOMPurify) {
  64. var editor, mobileToolbar, toolbar;
  65. Simditor.locale = 'zh-CN';
  66. Simditor.list = {};
  67. toolbar = ['title', 'bold', 'italic', 'underline', 'strikethrough', 'fontScale', 'color', '|', 'ol', 'ul', 'blockquote', 'code', 'table', '|', 'link', 'image', 'hr', '|', 'indent', 'outdent', 'alignment'];
  68. mobileToolbar = ["bold", "underline", "strikethrough", "color", "ul", "ol"];
  69. // 添加 hook 过滤 iframe 来源
  70. DOMPurify.addHook('uponSanitizeElement', function (node, data, config) {
  71. if (data.tagName === 'iframe') {
  72. var allowedIframePrefixes = Config.nkeditor.allowiframeprefixs || [];
  73. var src = node.getAttribute('src');
  74. // 判断是否匹配允许的前缀
  75. var isAllowed = false;
  76. for (var i = 0; i < allowedIframePrefixes.length; i++) {
  77. if (src && src.indexOf(allowedIframePrefixes[i]) === 0) {
  78. isAllowed = true;
  79. break;
  80. }
  81. }
  82. if (!isAllowed) {
  83. // 不符合要求则移除该节点
  84. return node.parentNode.removeChild(node);
  85. }
  86. // 添加安全属性
  87. node.setAttribute('allowfullscreen', '');
  88. node.setAttribute('allow', 'fullscreen');
  89. }
  90. });
  91. var purifyOptions = {
  92. ADD_TAGS: ['iframe'],
  93. FORCE_REJECT_IFRAME: false
  94. };
  95. $(Config.simditor.classname || '.editor', form).each(function () {
  96. var id = $(this).attr("id");
  97. editor = new Simditor({
  98. textarea: this,
  99. height: isNaN(Config.simditor.height) ? null : parseInt(Config.simditor.height),
  100. minHeight: parseInt(Config.simditor.minHeight || 250),
  101. toolbar: Config.simditor.toolbar || [],
  102. mobileToolbar: Config.simditor.mobileToolbar || [],
  103. toolbarFloat: parseInt(Config.simditor.toolbarFloat),
  104. placeholder: Config.simditor.placeholder || '',
  105. dompurify: {
  106. enabled: Config.simditor.isdompurify,
  107. options: purifyOptions
  108. },
  109. pasteImage: true,
  110. defaultImage: Config.__CDN__ + '/assets/addons/simditor/images/image.png',
  111. upload: {url: '/'},
  112. allowedTags: ['div', 'br', 'span', 'a', 'img', 'b', 'strong', 'i', 'strike', 'u', 'font', 'p', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'h1', 'h2', 'h3', 'h4', 'hr'],
  113. allowedAttributes: {
  114. div: ['data-tpl', 'data-source', 'data-id'],
  115. span: ['data-id']
  116. },
  117. allowedStyles: {
  118. div: ['width', 'height', 'padding', 'background', 'color', 'display', 'justify-content', 'border', 'box-sizing', 'max-width', 'min-width', 'position', 'margin-left', 'bottom', 'left', 'margin', 'float'],
  119. p: ['margin', 'color', 'height', 'line-height', 'position', 'width', 'border', 'bottom', 'float'],
  120. span: ['text-decoration', 'color', 'margin-left', 'float', 'background', 'padding', 'margin-right', 'border-radius', 'font-size', 'border', 'float'],
  121. img: ['vertical-align', 'width', 'height', 'object-fit', 'float', 'margin', 'float'],
  122. a: ['text-decoration']
  123. }
  124. });
  125. editor.toolbar.wrapper.find(".menu-item-select-image").on('click', function(){
  126. var selectUrl = typeof Config !== 'undefined' && Config.modulename === 'index' ? 'user/attachment' : 'general/attachment/select';
  127. parent.Fast.api.open(selectUrl + "?element_id=&multiple=true&mimetype=image/", __('Choose'), {
  128. callback: function (data) {
  129. var urlArr = data.url.split(/\,/);
  130. $.each(urlArr, function () {
  131. var url = Fast.api.cdnurl(this, true);
  132. var imgHtml = '<img src="' + url + '" />';
  133. editor.insertHTML(imgHtml);
  134. });
  135. }
  136. });
  137. return false;
  138. });
  139. editor.uploader.on('beforeupload', function (e, file) {
  140. Upload.api.send(file.obj, function (data) {
  141. var url = Fast.api.cdnurl(data.url, true);
  142. editor.uploader.trigger("uploadsuccess", [file, {success: true, file_path: url}]);
  143. });
  144. return false;
  145. });
  146. editor.on("blur", function () {
  147. this.textarea.trigger("blur");
  148. });
  149. if (editor.opts.height) {
  150. editor.body.css({height: editor.opts.height, 'overflow-y': 'auto'});
  151. }
  152. if (editor.opts.minHeight) {
  153. editor.body.css({'min-height': editor.opts.minHeight});
  154. }
  155. Simditor.list[id] = editor;
  156. });
  157. });
  158. }
  159. }
  160. });
  161. });