goto-line-dialog.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*!
  2. * Goto line dialog plugin for Editor.md
  3. *
  4. * @file goto-line-dialog.js
  5. * @author pandao
  6. * @version 1.2.1
  7. * @updateTime 2015-06-09
  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 = "goto-line-dialog";
  15. var langs = {
  16. "zh-cn" : {
  17. toolbar : {
  18. "goto-line" : "跳转到行"
  19. },
  20. dialog : {
  21. "goto-line" : {
  22. title : "跳转到行",
  23. label : "请输入行号",
  24. error : "错误:"
  25. }
  26. }
  27. },
  28. "zh-tw" : {
  29. toolbar : {
  30. "goto-line" : "跳轉到行"
  31. },
  32. dialog : {
  33. "goto-line" : {
  34. title : "跳轉到行",
  35. label : "請輸入行號",
  36. error : "錯誤:"
  37. }
  38. }
  39. },
  40. "en" : {
  41. toolbar : {
  42. "goto-line" : "Goto line"
  43. },
  44. dialog : {
  45. "goto-line" : {
  46. title : "Goto line",
  47. label : "Enter a line number, range ",
  48. error : "Error: "
  49. }
  50. }
  51. }
  52. };
  53. exports.fn.gotoLineDialog = function() {
  54. var _this = this;
  55. var cm = this.cm;
  56. var editor = this.editor;
  57. var settings = this.settings;
  58. var path = settings.pluginPath + pluginName +"/";
  59. var classPrefix = this.classPrefix;
  60. var dialogName = classPrefix + pluginName, dialog;
  61. $.extend(true, this.lang, langs[this.lang.name]);
  62. this.setToolbar();
  63. var lang = this.lang;
  64. var dialogLang = lang.dialog["goto-line"];
  65. var lineCount = cm.lineCount();
  66. dialogLang.error += dialogLang.label + " 1-" + lineCount;
  67. if (editor.find("." + dialogName).length < 1)
  68. {
  69. var dialogContent = [
  70. "<div class=\"editormd-form\" style=\"padding: 10px 0;\">",
  71. "<p style=\"margin: 0;\">" + dialogLang.label + " 1-" + lineCount +"&nbsp;&nbsp;&nbsp;<input type=\"number\" class=\"number-input\" style=\"width: 60px;height: 30px;\" value=\"1\" max=\"" + lineCount + "\" min=\"1\" data-line-number /></p>",
  72. "</div>"
  73. ].join("\n");
  74. dialog = this.createDialog({
  75. name : dialogName,
  76. title : dialogLang.title,
  77. width : 400,
  78. height : 180,
  79. mask : settings.dialogShowMask,
  80. drag : settings.dialogDraggable,
  81. content : dialogContent,
  82. lockScreen : settings.dialogLockScreen,
  83. maskStyle : {
  84. opacity : settings.dialogMaskOpacity,
  85. backgroundColor : settings.dialogMaskBgColor
  86. },
  87. buttons : {
  88. enter : [lang.buttons.enter, function() {
  89. var line = parseInt(this.find("[data-line-number]").val());
  90. if (line < 1 || line > lineCount) {
  91. alert(dialogLang.error);
  92. return false;
  93. }
  94. _this.gotoLine(line);
  95. this.hide().lockScreen(false).hideMask();
  96. return false;
  97. }],
  98. cancel : [lang.buttons.cancel, function() {
  99. this.hide().lockScreen(false).hideMask();
  100. return false;
  101. }]
  102. }
  103. });
  104. }
  105. dialog = editor.find("." + dialogName);
  106. this.dialogShowMask(dialog);
  107. this.dialogLockScreen();
  108. dialog.show();
  109. };
  110. };
  111. // CommonJS/Node.js
  112. if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
  113. {
  114. module.exports = factory;
  115. }
  116. else if (typeof define === "function") // AMD/CMD/Sea.js
  117. {
  118. if (define.amd) { // for Require.js
  119. define(["editormd"], function(editormd) {
  120. factory(editormd);
  121. });
  122. } else { // for Sea.js
  123. define(function(require) {
  124. var editormd = require("./../../editormd");
  125. factory(editormd);
  126. });
  127. }
  128. }
  129. else
  130. {
  131. factory(window.editormd);
  132. }
  133. })();