charts.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. UE.plugin.register('charts', function (){
  2. var me = this;
  3. return {
  4. bindEvents: {
  5. 'chartserror': function () {
  6. }
  7. },
  8. commands:{
  9. 'charts': {
  10. execCommand: function ( cmd, data ) {
  11. var tableNode = domUtils.findParentByTagName(this.selection.getRange().startContainer, 'table', true),
  12. flagText = [],
  13. config = {};
  14. if ( !tableNode ) {
  15. return false;
  16. }
  17. if ( !validData( tableNode ) ) {
  18. me.fireEvent( "chartserror" );
  19. return false;
  20. }
  21. config.title = data.title || '';
  22. config.subTitle = data.subTitle || '';
  23. config.xTitle = data.xTitle || '';
  24. config.yTitle = data.yTitle || '';
  25. config.suffix = data.suffix || '';
  26. config.tip = data.tip || '';
  27. //数据对齐方式
  28. config.dataFormat = data.tableDataFormat || '';
  29. //图表类型
  30. config.chartType = data.chartType || 0;
  31. for ( var key in config ) {
  32. if ( !config.hasOwnProperty( key ) ) {
  33. continue;
  34. }
  35. flagText.push( key+":"+config[ key ] );
  36. }
  37. tableNode.setAttribute( "data-chart", flagText.join( ";" ) );
  38. domUtils.addClass( tableNode, "edui-charts-table" );
  39. },
  40. queryCommandState: function ( cmd, name ) {
  41. var tableNode = domUtils.findParentByTagName(this.selection.getRange().startContainer, 'table', true);
  42. return tableNode && validData( tableNode ) ? 0 : -1;
  43. }
  44. }
  45. },
  46. inputRule:function(root){
  47. utils.each(root.getNodesByTagName('table'),function( tableNode ){
  48. if ( tableNode.getAttr("data-chart") !== undefined ) {
  49. tableNode.setAttr("style");
  50. }
  51. })
  52. },
  53. outputRule:function(root){
  54. utils.each(root.getNodesByTagName('table'),function( tableNode ){
  55. if ( tableNode.getAttr("data-chart") !== undefined ) {
  56. tableNode.setAttr("style", "display: none;");
  57. }
  58. })
  59. }
  60. }
  61. function validData ( table ) {
  62. var firstRows = null,
  63. cellCount = 0;
  64. //行数不够
  65. if ( table.rows.length < 2 ) {
  66. return false;
  67. }
  68. //列数不够
  69. if ( table.rows[0].cells.length < 2 ) {
  70. return false;
  71. }
  72. //第一行所有cell必须是th
  73. firstRows = table.rows[ 0 ].cells;
  74. cellCount = firstRows.length;
  75. for ( var i = 0, cell; cell = firstRows[ i ]; i++ ) {
  76. if ( cell.tagName.toLowerCase() !== 'th' ) {
  77. return false;
  78. }
  79. }
  80. for ( var i = 1, row; row = table.rows[ i ]; i++ ) {
  81. //每行单元格数不匹配, 返回false
  82. if ( row.cells.length != cellCount ) {
  83. return false;
  84. }
  85. //第一列不是th也返回false
  86. if ( row.cells[0].tagName.toLowerCase() !== 'th' ) {
  87. return false;
  88. }
  89. for ( var j = 1, cell; cell = row.cells[ j ]; j++ ) {
  90. var value = utils.trim( ( cell.innerText || cell.textContent || '' ) );
  91. value = value.replace( new RegExp( UE.dom.domUtils.fillChar, 'g' ), '' ).replace( /^\s+|\s+$/g, '' );
  92. //必须是数字
  93. if ( !/^\d*\.?\d+$/.test( value ) ) {
  94. return false;
  95. }
  96. }
  97. }
  98. return true;
  99. }
  100. });