You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

518 lines
11 KiB

3 years ago
  1. /*
  2. * 图片转换对话框脚本
  3. **/
  4. var tableData = [],
  5. //编辑器页面table
  6. editorTable = null,
  7. chartsConfig = window.typeConfig,
  8. resizeTimer = null,
  9. //初始默认图表类型
  10. currentChartType = 0;
  11. window.onload = function () {
  12. editorTable = domUtils.findParentByTagName( editor.selection.getRange().startContainer, 'table', true);
  13. //未找到表格, 显示错误页面
  14. if ( !editorTable ) {
  15. document.body.innerHTML = "<div class='edui-charts-not-data'>未找到数据</div>";
  16. return;
  17. }
  18. //初始化图表类型选择
  19. initChartsTypeView();
  20. renderTable( editorTable );
  21. initEvent();
  22. initUserConfig( editorTable.getAttribute( "data-chart" ) );
  23. $( "#scrollBed .view-box:eq("+ currentChartType +")" ).trigger( "click" );
  24. updateViewType( currentChartType );
  25. dialog.addListener( "resize", function () {
  26. if ( resizeTimer != null ) {
  27. window.clearTimeout( resizeTimer );
  28. }
  29. resizeTimer = window.setTimeout( function () {
  30. resizeTimer = null;
  31. renderCharts();
  32. }, 500 );
  33. } );
  34. };
  35. function initChartsTypeView () {
  36. var contents = [];
  37. for ( var i = 0, len = chartsConfig.length; i<len; i++ ) {
  38. contents.push( '<div class="view-box" data-chart-type="'+ i +'"><img width="300" src="images/charts'+ i +'.png"></div>' );
  39. }
  40. $( "#scrollBed" ).html( contents.join( "" ) );
  41. }
  42. //渲染table, 以便用户修改数据
  43. function renderTable ( table ) {
  44. var tableHtml = [];
  45. //构造数据
  46. for ( var i = 0, row; row = table.rows[ i ]; i++ ) {
  47. tableData[ i ] = [];
  48. tableHtml[ i ] = [];
  49. for ( var j = 0, cell; cell = row.cells[ j ]; j++ ) {
  50. var value = getCellValue( cell );
  51. if ( i > 0 && j > 0 ) {
  52. value = +value;
  53. }
  54. if ( i === 0 || j === 0 ) {
  55. tableHtml[ i ].push( '<th>'+ value +'</th>' );
  56. } else {
  57. tableHtml[ i ].push( '<td><input type="text" class="data-item" value="'+ value +'"></td>' );
  58. }
  59. tableData[ i ][ j ] = value;
  60. }
  61. tableHtml[ i ] = tableHtml[ i ].join( "" );
  62. }
  63. //draw 表格
  64. $( "#tableContainer" ).html( '<table id="showTable" border="1"><tbody><tr>'+ tableHtml.join( "</tr><tr>" ) +'</tr></tbody></table>' );
  65. }
  66. /*
  67. * 根据表格已有的图表属性初始化当前图表属性
  68. */
  69. function initUserConfig ( config ) {
  70. var parsedConfig = {};
  71. if ( !config ) {
  72. return;
  73. }
  74. config = config.split( ";" );
  75. $.each( config, function ( index, item ) {
  76. item = item.split( ":" );
  77. parsedConfig[ item[ 0 ] ] = item[ 1 ];
  78. } );
  79. setUserConfig( parsedConfig );
  80. }
  81. function initEvent () {
  82. var cacheValue = null,
  83. //图表类型数
  84. typeViewCount = chartsConfig.length- 1,
  85. $chartsTypeViewBox = $( '#scrollBed .view-box' );
  86. $( ".charts-format" ).delegate( ".format-ctrl", "change", function () {
  87. renderCharts();
  88. } )
  89. $( ".table-view" ).delegate( ".data-item", "focus", function () {
  90. cacheValue = this.value;
  91. } ).delegate( ".data-item", "blur", function () {
  92. if ( this.value !== cacheValue ) {
  93. renderCharts();
  94. }
  95. cacheValue = null;
  96. } );
  97. $( "#buttonContainer" ).delegate( "a", "click", function (e) {
  98. e.preventDefault();
  99. if ( this.getAttribute( "data-title" ) === 'prev' ) {
  100. if ( currentChartType > 0 ) {
  101. currentChartType--;
  102. updateViewType( currentChartType );
  103. }
  104. } else {
  105. if ( currentChartType < typeViewCount ) {
  106. currentChartType++;
  107. updateViewType( currentChartType );
  108. }
  109. }
  110. } );
  111. //图表类型变化
  112. $( '#scrollBed' ).delegate( ".view-box", "click", function (e) {
  113. var index = $( this ).attr( "data-chart-type" );
  114. $chartsTypeViewBox.removeClass( "selected" );
  115. $( $chartsTypeViewBox[ index ] ).addClass( "selected" );
  116. currentChartType = index | 0;
  117. //饼图, 禁用部分配置
  118. if ( currentChartType === chartsConfig.length - 1 ) {
  119. disableNotPieConfig();
  120. //启用完整配置
  121. } else {
  122. enableNotPieConfig();
  123. }
  124. renderCharts();
  125. } );
  126. }
  127. function renderCharts () {
  128. var data = collectData();
  129. $('#chartsContainer').highcharts( $.extend( {}, chartsConfig[ currentChartType ], {
  130. credits: {
  131. enabled: false
  132. },
  133. exporting: {
  134. enabled: false
  135. },
  136. title: {
  137. text: data.title,
  138. x: -20 //center
  139. },
  140. subtitle: {
  141. text: data.subTitle,
  142. x: -20
  143. },
  144. xAxis: {
  145. title: {
  146. text: data.xTitle
  147. },
  148. categories: data.categories
  149. },
  150. yAxis: {
  151. title: {
  152. text: data.yTitle
  153. },
  154. plotLines: [{
  155. value: 0,
  156. width: 1,
  157. color: '#808080'
  158. }]
  159. },
  160. tooltip: {
  161. enabled: true,
  162. valueSuffix: data.suffix
  163. },
  164. legend: {
  165. layout: 'vertical',
  166. align: 'right',
  167. verticalAlign: 'middle',
  168. borderWidth: 1
  169. },
  170. series: data.series
  171. } ));
  172. }
  173. function updateViewType ( index ) {
  174. $( "#scrollBed" ).css( 'marginLeft', -index*324+'px' );
  175. }
  176. function collectData () {
  177. var form = document.forms[ 'data-form' ],
  178. data = null;
  179. if ( currentChartType !== chartsConfig.length - 1 ) {
  180. data = getSeriesAndCategories();
  181. $.extend( data, getUserConfig() );
  182. //饼图数据格式
  183. } else {
  184. data = getSeriesForPieChart();
  185. data.title = form[ 'title' ].value;
  186. data.suffix = form[ 'unit' ].value;
  187. }
  188. return data;
  189. }
  190. /**
  191. * 获取用户配置信息
  192. */
  193. function getUserConfig () {
  194. var form = document.forms[ 'data-form' ],
  195. info = {
  196. title: form[ 'title' ].value,
  197. subTitle: form[ 'sub-title' ].value,
  198. xTitle: form[ 'x-title' ].value,
  199. yTitle: form[ 'y-title' ].value,
  200. suffix: form[ 'unit' ].value,
  201. //数据对齐方式
  202. tableDataFormat: getTableDataFormat (),
  203. //饼图提示文字
  204. tip: $( "#tipInput" ).val()
  205. };
  206. return info;
  207. }
  208. function setUserConfig ( config ) {
  209. var form = document.forms[ 'data-form' ];
  210. config.title && ( form[ 'title' ].value = config.title );
  211. config.subTitle && ( form[ 'sub-title' ].value = config.subTitle );
  212. config.xTitle && ( form[ 'x-title' ].value = config.xTitle );
  213. config.yTitle && ( form[ 'y-title' ].value = config.yTitle );
  214. config.suffix && ( form[ 'unit' ].value = config.suffix );
  215. config.dataFormat == "-1" && ( form[ 'charts-format' ][ 1 ].checked = true );
  216. config.tip && ( form[ 'tip' ].value = config.tip );
  217. currentChartType = config.chartType || 0;
  218. }
  219. function getSeriesAndCategories () {
  220. var form = document.forms[ 'data-form' ],
  221. series = [],
  222. categories = [],
  223. tmp = [],
  224. tableData = getTableData();
  225. //反转数据
  226. if ( getTableDataFormat() === "-1" ) {
  227. for ( var i = 0, len = tableData.length; i < len; i++ ) {
  228. for ( var j = 0, jlen = tableData[ i ].length; j < jlen; j++ ) {
  229. if ( !tmp[ j ] ) {
  230. tmp[ j ] = [];
  231. }
  232. tmp[ j ][ i ] = tableData[ i ][ j ];
  233. }
  234. }
  235. tableData = tmp;
  236. }
  237. categories = tableData[0].slice( 1 );
  238. for ( var i = 1, data; data = tableData[ i ]; i++ ) {
  239. series.push( {
  240. name: data[ 0 ],
  241. data: data.slice( 1 )
  242. } );
  243. }
  244. return {
  245. series: series,
  246. categories: categories
  247. };
  248. }
  249. /*
  250. * 获取数据源数据对齐方式
  251. */
  252. function getTableDataFormat () {
  253. var form = document.forms[ 'data-form' ],
  254. items = form['charts-format'];
  255. return items[ 0 ].checked ? items[ 0 ].value : items[ 1 ].value;
  256. }
  257. /*
  258. * 禁用非饼图类型的配置项
  259. */
  260. function disableNotPieConfig() {
  261. updateConfigItem( 'disable' );
  262. }
  263. /*
  264. * 启用非饼图类型的配置项
  265. */
  266. function enableNotPieConfig() {
  267. updateConfigItem( 'enable' );
  268. }
  269. function updateConfigItem ( value ) {
  270. var table = $( "#showTable" )[ 0 ],
  271. isDisable = value === 'disable' ? true : false;
  272. //table中的input处理
  273. for ( var i = 2 , row; row = table.rows[ i ]; i++ ) {
  274. for ( var j = 1, cell; cell = row.cells[ j ]; j++ ) {
  275. $( "input", cell ).attr( "disabled", isDisable );
  276. }
  277. }
  278. //其他项处理
  279. $( "input.not-pie-item" ).attr( "disabled", isDisable );
  280. $( "#tipInput" ).attr( "disabled", !isDisable )
  281. }
  282. /*
  283. * 获取饼图数据
  284. * 饼图的数据只取第一行的
  285. **/
  286. function getSeriesForPieChart () {
  287. var series = {
  288. type: 'pie',
  289. name: $("#tipInput").val(),
  290. data: []
  291. },
  292. tableData = getTableData();
  293. for ( var j = 1, jlen = tableData[ 0 ].length; j < jlen; j++ ) {
  294. var title = tableData[ 0 ][ j ],
  295. val = tableData[ 1 ][ j ];
  296. series.data.push( [ title, val ] );
  297. }
  298. return {
  299. series: [ series ]
  300. };
  301. }
  302. function getTableData () {
  303. var table = document.getElementById( "showTable" ),
  304. xCount = table.rows[0].cells.length - 1,
  305. values = getTableInputValue();
  306. for ( var i = 0, value; value = values[ i ]; i++ ) {
  307. tableData[ Math.floor( i / xCount ) + 1 ][ i % xCount + 1 ] = values[ i ];
  308. }
  309. return tableData;
  310. }
  311. function getTableInputValue () {
  312. var table = document.getElementById( "showTable" ),
  313. inputs = table.getElementsByTagName( "input" ),
  314. values = [];
  315. for ( var i = 0, input; input = inputs[ i ]; i++ ) {
  316. values.push( input.value | 0 );
  317. }
  318. return values;
  319. }
  320. function getCellValue ( cell ) {
  321. var value = utils.trim( ( cell.innerText || cell.textContent || '' ) );
  322. return value.replace( new RegExp( UE.dom.domUtils.fillChar, 'g' ), '' ).replace( /^\s+|\s+$/g, '' );
  323. }
  324. //dialog确认事件
  325. dialog.onok = function () {
  326. //收集信息
  327. var form = document.forms[ 'data-form' ],
  328. info = getUserConfig();
  329. //添加图表类型
  330. info.chartType = currentChartType;
  331. //同步表格数据到编辑器
  332. syncTableData();
  333. //执行图表命令
  334. editor.execCommand( 'charts', info );
  335. };
  336. /*
  337. * 同步图表编辑视图的表格数据到编辑器里的原始表格
  338. */
  339. function syncTableData () {
  340. var tableData = getTableData();
  341. for ( var i = 1, row; row = editorTable.rows[ i ]; i++ ) {
  342. for ( var j = 1, cell; cell = row.cells[ j ]; j++ ) {
  343. cell.innerHTML = tableData[ i ] [ j ];
  344. }
  345. }
  346. }