html-entities-dialog.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*!
  2. * HTML entities dialog plugin for Editor.md
  3. *
  4. * @file html-entities-dialog.js
  5. * @author pandao
  6. * @version 1.2.0
  7. * @updateTime 2015-03-08
  8. * {@link https://github.com/pandao/editor.md}
  9. * @license MIT
  10. */
  11. (function() {
  12. var factory = function (exports) {
  13. var $ = jQuery;
  14. var pluginName = "html-entities-dialog";
  15. var selecteds = [];
  16. var entitiesData = [];
  17. exports.fn.htmlEntitiesDialog = function() {
  18. var _this = this;
  19. var cm = this.cm;
  20. var lang = _this.lang;
  21. var settings = _this.settings;
  22. var path = settings.pluginPath + pluginName + "/";
  23. var editor = this.editor;
  24. var cursor = cm.getCursor();
  25. var selection = cm.getSelection();
  26. var classPrefix = _this.classPrefix;
  27. var dialogName = classPrefix + "dialog-" + pluginName, dialog;
  28. var dialogLang = lang.dialog.htmlEntities;
  29. var dialogContent = [
  30. '<div class="' + classPrefix + 'html-entities-box" style=\"width: 760px;height: 334px;margin-bottom: 8px;overflow: hidden;overflow-y: auto;\">',
  31. '<div class="' + classPrefix + 'grid-table">',
  32. '</div>',
  33. '</div>',
  34. ].join("\r\n");
  35. cm.focus();
  36. if (editor.find("." + dialogName).length > 0)
  37. {
  38. dialog = editor.find("." + dialogName);
  39. selecteds = [];
  40. dialog.find("a").removeClass("selected");
  41. this.dialogShowMask(dialog);
  42. this.dialogLockScreen();
  43. dialog.show();
  44. }
  45. else
  46. {
  47. dialog = this.createDialog({
  48. name : dialogName,
  49. title : dialogLang.title,
  50. width : 800,
  51. height : 475,
  52. mask : settings.dialogShowMask,
  53. drag : settings.dialogDraggable,
  54. content : dialogContent,
  55. lockScreen : settings.dialogLockScreen,
  56. maskStyle : {
  57. opacity : settings.dialogMaskOpacity,
  58. backgroundColor : settings.dialogMaskBgColor
  59. },
  60. buttons : {
  61. enter : [lang.buttons.enter, function() {
  62. cm.replaceSelection(selecteds.join(" "));
  63. this.hide().lockScreen(false).hideMask();
  64. return false;
  65. }],
  66. cancel : [lang.buttons.cancel, function() {
  67. this.hide().lockScreen(false).hideMask();
  68. return false;
  69. }]
  70. }
  71. });
  72. }
  73. var table = dialog.find("." + classPrefix + "grid-table");
  74. var drawTable = function() {
  75. if (entitiesData.length < 1) return ;
  76. var rowNumber = 20;
  77. var pageTotal = Math.ceil(entitiesData.length / rowNumber);
  78. table.html("");
  79. for (var i = 0; i < pageTotal; i++)
  80. {
  81. var row = "<div class=\"" + classPrefix + "grid-table-row\">";
  82. for (var x = 0; x < rowNumber; x++)
  83. {
  84. var entity = entitiesData[(i * rowNumber) + x];
  85. if (typeof entity !== "undefined")
  86. {
  87. var name = entity.name.replace("&amp;", "&");
  88. row += "<a href=\"javascript:;\" value=\"" + entity.name + "\" title=\"" + name + "\" class=\"" + classPrefix + "html-entity-btn\">" + name + "</a>";
  89. }
  90. }
  91. row += "</div>";
  92. table.append(row);
  93. }
  94. dialog.find("." + classPrefix + "html-entity-btn").bind(exports.mouseOrTouch("click", "touchend"), function() {
  95. $(this).toggleClass("selected");
  96. if ($(this).hasClass("selected"))
  97. {
  98. selecteds.push($(this).attr("value"));
  99. }
  100. });
  101. };
  102. if (entitiesData.length < 1)
  103. {
  104. if (typeof (dialog.loading) == "function") dialog.loading(true);
  105. $.getJSON(path + pluginName.replace("-dialog", "") + ".json", function(json) {
  106. if (typeof (dialog.loading) == "function") dialog.loading(false);
  107. entitiesData = json;
  108. drawTable();
  109. });
  110. }
  111. else
  112. {
  113. drawTable();
  114. }
  115. };
  116. };
  117. // CommonJS/Node.js
  118. if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
  119. {
  120. module.exports = factory;
  121. }
  122. else if (typeof define === "function") // AMD/CMD/Sea.js
  123. {
  124. if (define.amd) { // for Require.js
  125. define(["editormd"], function(editormd) {
  126. factory(editormd);
  127. });
  128. } else { // for Sea.js
  129. define(function(require) {
  130. var editormd = require("./../../editormd");
  131. factory(editormd);
  132. });
  133. }
  134. }
  135. else
  136. {
  137. factory(window.editormd);
  138. }
  139. })();