cellalignpicker.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 = status.valign[ this.selected.valign ] * status.count + status.align[ this.selected.align ];
  37. }
  38. },
  39. initCellAlignPicker:function () {
  40. this.initUIBase();
  41. this.Stateful_init();
  42. },
  43. getHtmlTpl:function () {
  44. var alignType = [ 'left', 'center', 'right' ],
  45. COUNT = 9,
  46. tempClassName = null,
  47. tempIndex = -1,
  48. tmpl = [];
  49. for( var i= 0; i<COUNT; i++ ) {
  50. tempClassName = this.selectedIndex === i ? ' class="edui-cellalign-selected" ' : '';
  51. tempIndex = i % 3;
  52. tempIndex === 0 && tmpl.push('<tr>');
  53. tmpl.push( '<td index="'+ i +'" ' + tempClassName + ' stateful><div class="edui-icon edui-'+ alignType[ tempIndex ] +'"></div></td>' );
  54. tempIndex === 2 && tmpl.push('</tr>');
  55. }
  56. return '<div id="##" class="edui-cellalignpicker %%">' +
  57. '<div class="edui-cellalignpicker-body">' +
  58. '<table onclick="$$._onClick(event);">' +
  59. tmpl.join('') +
  60. '</table>' +
  61. '</div>' +
  62. '</div>';
  63. },
  64. getStateDom: function (){
  65. return this.target;
  66. },
  67. _onClick: function (evt){
  68. var target= evt.target || evt.srcElement;
  69. if(/icon/.test(target.className)){
  70. this.items[target.parentNode.getAttribute("index")].onclick();
  71. Popup.postHide(evt);
  72. }
  73. },
  74. _UIBase_render:UIBase.prototype.render
  75. };
  76. utils.inherits(CellAlignPicker, UIBase);
  77. utils.extend(CellAlignPicker.prototype, Stateful,true);
  78. })();