cellalignpicker.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ///import core
  2. ///import uicore
  3. (function() {
  4. var utils = baidu.editor.utils,
  5. Popup = baidu.editor.ui.Popup,
  6. Stateful = baidu.editor.ui.Stateful,
  7. UIBase = baidu.editor.ui.UIBase;
  8. /**
  9. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  10. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  11. * @update 2013/4/2 hancong03@baidu.com
  12. */
  13. var CellAlignPicker = (baidu.editor.ui.CellAlignPicker = function(options) {
  14. this.initOptions(options);
  15. this.initSelected();
  16. this.initCellAlignPicker();
  17. });
  18. CellAlignPicker.prototype = {
  19. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  20. initSelected: function() {
  21. var status = {
  22. valign: {
  23. top: 0,
  24. middle: 1,
  25. bottom: 2
  26. },
  27. align: {
  28. left: 0,
  29. center: 1,
  30. right: 2
  31. },
  32. count: 3
  33. },
  34. result = -1;
  35. if (this.selected) {
  36. this.selectedIndex =
  37. status.valign[this.selected.valign] * status.count +
  38. status.align[this.selected.align];
  39. }
  40. },
  41. initCellAlignPicker: function() {
  42. this.initUIBase();
  43. this.Stateful_init();
  44. },
  45. getHtmlTpl: function() {
  46. var alignType = ["left", "center", "right"],
  47. COUNT = 9,
  48. tempClassName = null,
  49. tempIndex = -1,
  50. tmpl = [];
  51. for (var i = 0; i < COUNT; i++) {
  52. tempClassName = this.selectedIndex === i
  53. ? ' class="edui-cellalign-selected" '
  54. : "";
  55. tempIndex = i % 3;
  56. tempIndex === 0 && tmpl.push("<tr>");
  57. tmpl.push(
  58. '<td index="' +
  59. i +
  60. '" ' +
  61. tempClassName +
  62. ' stateful><div class="edui-icon edui-' +
  63. alignType[tempIndex] +
  64. '"></div></td>'
  65. );
  66. tempIndex === 2 && tmpl.push("</tr>");
  67. }
  68. return (
  69. '<div id="##" class="edui-cellalignpicker %%">' +
  70. '<div class="edui-cellalignpicker-body">' +
  71. '<table onclick="$$._onClick(event);">' +
  72. tmpl.join("") +
  73. "</table>" +
  74. "</div>" +
  75. "</div>"
  76. );
  77. },
  78. getStateDom: function() {
  79. return this.target;
  80. },
  81. _onClick: function(evt) {
  82. var target = evt.target || evt.srcElement;
  83. if (/icon/.test(target.className)) {
  84. this.items[target.parentNode.getAttribute("index")].onclick();
  85. Popup.postHide(evt);
  86. }
  87. },
  88. _UIBase_render: UIBase.prototype.render
  89. };
  90. utils.inherits(CellAlignPicker, UIBase);
  91. utils.extend(CellAlignPicker.prototype, Stateful, true);
  92. })();