autotypesetbutton.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ///import core
  2. ///import uicore
  3. ///import ui/popup.js
  4. ///import ui/autotypesetpicker.js
  5. ///import ui/splitbutton.js
  6. (function() {
  7. var utils = baidu.editor.utils,
  8. Popup = baidu.editor.ui.Popup,
  9. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  10. SplitButton = baidu.editor.ui.SplitButton,
  11. AutoTypeSetButton = (baidu.editor.ui.AutoTypeSetButton = function(options) {
  12. this.initOptions(options);
  13. this.initAutoTypeSetButton();
  14. });
  15. function getPara(me) {
  16. var opt = {},
  17. cont = me.getDom(),
  18. editorId = me.editor.uid,
  19. inputType = null,
  20. attrName = null,
  21. ipts = domUtils.getElementsByTagName(cont, "input");
  22. for (var i = ipts.length - 1, ipt; (ipt = ipts[i--]); ) {
  23. inputType = ipt.getAttribute("type");
  24. if (inputType == "checkbox") {
  25. attrName = ipt.getAttribute("name");
  26. opt[attrName] && delete opt[attrName];
  27. if (ipt.checked) {
  28. var attrValue = document.getElementById(
  29. attrName + "Value" + editorId
  30. );
  31. if (attrValue) {
  32. if (/input/gi.test(attrValue.tagName)) {
  33. opt[attrName] = attrValue.value;
  34. } else {
  35. var iptChilds = attrValue.getElementsByTagName("input");
  36. for (
  37. var j = iptChilds.length - 1, iptchild;
  38. (iptchild = iptChilds[j--]);
  39. ) {
  40. if (iptchild.checked) {
  41. opt[attrName] = iptchild.value;
  42. break;
  43. }
  44. }
  45. }
  46. } else {
  47. opt[attrName] = true;
  48. }
  49. } else {
  50. opt[attrName] = false;
  51. }
  52. } else {
  53. opt[ipt.getAttribute("value")] = ipt.checked;
  54. }
  55. }
  56. var selects = domUtils.getElementsByTagName(cont, "select");
  57. for (var i = 0, si; (si = selects[i++]); ) {
  58. var attr = si.getAttribute("name");
  59. opt[attr] = opt[attr] ? si.value : "";
  60. }
  61. utils.extend(me.editor.options.autotypeset, opt);
  62. me.editor.setPreferences("autotypeset", opt);
  63. }
  64. AutoTypeSetButton.prototype = {
  65. initAutoTypeSetButton: function() {
  66. var me = this;
  67. this.popup = new Popup({
  68. //传入配置参数
  69. content: new AutoTypeSetPicker({ editor: me.editor }),
  70. editor: me.editor,
  71. hide: function() {
  72. if (!this._hidden && this.getDom()) {
  73. getPara(this);
  74. this.getDom().style.display = "none";
  75. this._hidden = true;
  76. this.fireEvent("hide");
  77. }
  78. }
  79. });
  80. var flag = 0;
  81. this.popup.addListener("postRenderAfter", function() {
  82. var popupUI = this;
  83. if (flag) return;
  84. var cont = this.getDom(),
  85. btn = cont.getElementsByTagName("button")[0];
  86. btn.onclick = function() {
  87. getPara(popupUI);
  88. me.editor.execCommand("autotypeset");
  89. popupUI.hide();
  90. };
  91. domUtils.on(cont, "click", function(e) {
  92. var target = e.target || e.srcElement,
  93. editorId = me.editor.uid;
  94. if (target && target.tagName == "INPUT") {
  95. // 点击图片浮动的checkbox,去除对应的radio
  96. if (
  97. target.name == "imageBlockLine" ||
  98. target.name == "textAlign" ||
  99. target.name == "symbolConver"
  100. ) {
  101. var checked = target.checked,
  102. radioTd = document.getElementById(
  103. target.name + "Value" + editorId
  104. ),
  105. radios = radioTd.getElementsByTagName("input"),
  106. defalutSelect = {
  107. imageBlockLine: "none",
  108. textAlign: "left",
  109. symbolConver: "tobdc"
  110. };
  111. for (var i = 0; i < radios.length; i++) {
  112. if (checked) {
  113. if (radios[i].value == defalutSelect[target.name]) {
  114. radios[i].checked = "checked";
  115. }
  116. } else {
  117. radios[i].checked = false;
  118. }
  119. }
  120. }
  121. // 点击radio,选中对应的checkbox
  122. if (
  123. target.name == "imageBlockLineValue" + editorId ||
  124. target.name == "textAlignValue" + editorId ||
  125. target.name == "bdc"
  126. ) {
  127. var checkboxs = target.parentNode.previousSibling.getElementsByTagName(
  128. "input"
  129. );
  130. checkboxs && (checkboxs[0].checked = true);
  131. }
  132. getPara(popupUI);
  133. }
  134. });
  135. flag = 1;
  136. });
  137. this.initSplitButton();
  138. }
  139. };
  140. utils.inherits(AutoTypeSetButton, SplitButton);
  141. })();