tablepicker.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ///import core
  2. ///import uicore
  3. (function() {
  4. var utils = baidu.editor.utils,
  5. uiUtils = baidu.editor.ui.uiUtils,
  6. UIBase = baidu.editor.ui.UIBase;
  7. var TablePicker = (baidu.editor.ui.TablePicker = function(options) {
  8. this.initOptions(options);
  9. this.initTablePicker();
  10. });
  11. TablePicker.prototype = {
  12. defaultNumRows: 10,
  13. defaultNumCols: 10,
  14. maxNumRows: 20,
  15. maxNumCols: 20,
  16. numRows: 10,
  17. numCols: 10,
  18. lengthOfCellSide: 22,
  19. initTablePicker: function() {
  20. this.initUIBase();
  21. },
  22. getHtmlTpl: function() {
  23. var me = this;
  24. return (
  25. '<div id="##" class="edui-tablepicker %%">' +
  26. '<div class="edui-tablepicker-body">' +
  27. '<div class="edui-infoarea">' +
  28. '<span id="##_label" class="edui-label"></span>' +
  29. "</div>" +
  30. '<div class="edui-pickarea"' +
  31. ' onmousemove="$$._onMouseMove(event, this);"' +
  32. ' onmouseover="$$._onMouseOver(event, this);"' +
  33. ' onmouseout="$$._onMouseOut(event, this);"' +
  34. ' onclick="$$._onClick(event, this);"' +
  35. ">" +
  36. '<div id="##_overlay" class="edui-overlay"></div>' +
  37. "</div>" +
  38. "</div>" +
  39. "</div>"
  40. );
  41. },
  42. _UIBase_render: UIBase.prototype.render,
  43. render: function(holder) {
  44. this._UIBase_render(holder);
  45. this.getDom("label").innerHTML =
  46. "0" +
  47. this.editor.getLang("t_row") +
  48. " x 0" +
  49. this.editor.getLang("t_col");
  50. },
  51. _track: function(numCols, numRows) {
  52. var style = this.getDom("overlay").style;
  53. var sideLen = this.lengthOfCellSide;
  54. style.width = numCols * sideLen + "px";
  55. style.height = numRows * sideLen + "px";
  56. var label = this.getDom("label");
  57. label.innerHTML =
  58. numCols +
  59. this.editor.getLang("t_col") +
  60. " x " +
  61. numRows +
  62. this.editor.getLang("t_row");
  63. this.numCols = numCols;
  64. this.numRows = numRows;
  65. },
  66. _onMouseOver: function(evt, el) {
  67. var rel = evt.relatedTarget || evt.fromElement;
  68. if (!uiUtils.contains(el, rel) && el !== rel) {
  69. this.getDom("label").innerHTML =
  70. "0" +
  71. this.editor.getLang("t_col") +
  72. " x 0" +
  73. this.editor.getLang("t_row");
  74. this.getDom("overlay").style.visibility = "";
  75. }
  76. },
  77. _onMouseOut: function(evt, el) {
  78. var rel = evt.relatedTarget || evt.toElement;
  79. if (!uiUtils.contains(el, rel) && el !== rel) {
  80. this.getDom("label").innerHTML =
  81. "0" +
  82. this.editor.getLang("t_col") +
  83. " x 0" +
  84. this.editor.getLang("t_row");
  85. this.getDom("overlay").style.visibility = "hidden";
  86. }
  87. },
  88. _onMouseMove: function(evt, el) {
  89. var style = this.getDom("overlay").style;
  90. var offset = uiUtils.getEventOffset(evt);
  91. var sideLen = this.lengthOfCellSide;
  92. var numCols = Math.ceil(offset.left / sideLen);
  93. var numRows = Math.ceil(offset.top / sideLen);
  94. this._track(numCols, numRows);
  95. },
  96. _onClick: function() {
  97. this.fireEvent("picktable", this.numCols, this.numRows);
  98. }
  99. };
  100. utils.inherits(TablePicker, UIBase);
  101. })();