formula.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. (function () {
  2. var editor = null;
  3. UM.registerWidget('formula', {
  4. tpl: "<link type=\"text/css\" rel=\"stylesheet\" href=\"<%=formula_url%>formula.css\">" +
  5. "<div class=\"edui-formula-wrapper\">" +
  6. "<ul class=\"edui-tab-nav\"></ul>" +
  7. "<div class=\"edui-tab-content\"></div>" +
  8. "</div>",
  9. sourceData: {
  10. formula: {
  11. 'common': [
  12. "{/}frac{ }{ }", "^{ }/_{ }", "x^{ }", "x_{ }", "x^{ }_{ }", "{/}bar{ }", "{/}sqrt{ }", "{/}nthroot{ }{ }",
  13. "{/}sum^{ }_{n=}", "{/}sum", "{/}log_{ }", "{/}ln", "{/}int_{ }^{ }", "{/}oint_{ }^{ }"
  14. ],
  15. 'symbol': [
  16. "+", "-", "{/}pm", "{/}times", "{/}ast", "{/}div", "/", "{/}bigtriangleup",
  17. "=", "{/}ne", "{/}approx", ">", "<", "{/}ge", "{/}le", "{/}infty",
  18. "{/}cap", "{/}cup", "{/}because", "{/}therefore", "{/}subset", "{/}supset", "{/}subseteq", "{/}supseteq",
  19. "{/}nsubseteq", "{/}nsupseteq", "{/}in", "{/}ni", "{/}notin", "{/}mapsto", "{/}leftarrow", "{/}rightarrow",
  20. "{/}Leftarrow", "{/}Rightarrow", "{/}leftrightarrow", "{/}Leftrightarrow"
  21. ],
  22. 'letter': [
  23. "{/}alpha", "{/}beta", "{/}gamma", "{/}delta", "{/}varepsilon", "{/}varphi", "{/}lambda", "{/}mu",
  24. "{/}rho", "{/}sigma", "{/}omega", "{/}Gamma", "{/}Delta", "{/}Theta", "{/}Lambda", "{/}Xi",
  25. "{/}Pi", "{/}Sigma", "{/}Upsilon", "{/}Phi", "{/}Psi", "{/}Omega"
  26. ]
  27. }
  28. },
  29. initContent: function (_editor, $widget) {
  30. var me = this,
  31. formula = me.sourceData.formula,
  32. lang = _editor.getLang('formula').static,
  33. formulaUrl = UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/formula/',
  34. options = $.extend({}, lang, { 'formula_url': formulaUrl }),
  35. $root = me.root();
  36. if (me.inited) {
  37. me.preventDefault();
  38. return;
  39. }
  40. me.inited = true;
  41. editor = _editor;
  42. me.$widget = $widget;
  43. $root.html($.parseTmpl(me.tpl, options));
  44. me.tabs = $.eduitab({selector: "#edui-formula-tab-Jpanel"});
  45. /* 初始化popup的内容 */
  46. var headHtml = [], xMax = 0, yMax = 0,
  47. $tabContent = me.root().find('.edui-tab-content');
  48. $.each(formula, function (k, v) {
  49. var contentHtml = [];
  50. $.each(v, function (i, f) {
  51. contentHtml.push('<li class="edui-formula-latex-item" data-latex="' + f + '" style="background-position:-' + (xMax * 30) + 'px -' + (yMax * 30) + 'px"></li>');
  52. if (++xMax >=8) {
  53. ++yMax; xMax = 0;
  54. }
  55. });
  56. yMax++; xMax = 0;
  57. $tabContent.append('<div class="edui-tab-pane"><ul>' + contentHtml.join('') + '</ul>');
  58. headHtml.push('<li class="edui-tab-item"><a href="javascript:void(0);" class="edui-tab-text">' + lang['lang_tab_' + k] + '</a></li>');
  59. });
  60. headHtml.push('<li class="edui-formula-clearboth"></li>');
  61. $root.find('.edui-tab-nav').html(headHtml.join(''));
  62. $root.find('.edui-tab-content').append('<div class="edui-formula-clearboth"></div>');
  63. /* 选中第一个tab */
  64. me.switchTab(0);
  65. },
  66. initEvent: function () {
  67. var me = this;
  68. //防止点击过后关闭popup
  69. me.root().on('click', function (e) {
  70. return false;
  71. });
  72. //点击tab切换菜单
  73. me.root().find('.edui-tab-nav').delegate('.edui-tab-item', 'click', function (evt) {
  74. me.switchTab(this);
  75. return false;
  76. });
  77. //点击选中公式
  78. me.root().find('.edui-tab-pane').delegate('.edui-formula-latex-item', 'click', function (evt) {
  79. var $item = $(this),
  80. latex = $item.attr('data-latex') || '';
  81. if (latex) {
  82. me.insertLatex(latex.replace("{/}", "\\"));
  83. }
  84. me.$widget.edui().hide();
  85. return false;
  86. });
  87. },
  88. switchTab:function(index){
  89. var me = this,
  90. $root = me.root(),
  91. index = $.isNumeric(index) ? index:$.inArray(index, $root.find('.edui-tab-nav .edui-tab-item'));
  92. $root.find('.edui-tab-nav .edui-tab-item').removeClass('edui-active').eq(index).addClass('edui-active');
  93. $root.find('.edui-tab-content .edui-tab-pane').removeClass('edui-active').eq(index).addClass('edui-active');
  94. /* 自动长高 */
  95. me.autoHeight(0);
  96. },
  97. autoHeight: function () {
  98. this.$widget.height(this.root() + 2);
  99. },
  100. insertLatex: function (latex) {
  101. editor.execCommand('formula', latex );
  102. },
  103. width: 350,
  104. height: 400
  105. });
  106. })();