1
0

DataGridRenderer.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. //
  2. // DataGridRenderer.js
  3. // Part of Mr-Data-Converter
  4. //
  5. // Created by Shan Carter on 2010-10-18.
  6. //
  7. var DataGridRenderer = {
  8. //---------------------------------------
  9. // Actionscript
  10. //---------------------------------------
  11. as: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  12. //inits...
  13. var commentLine = "//";
  14. var commentLineEnd = "";
  15. var outputText = "[";
  16. var numRows = dataGrid.length;
  17. var numColumns = headerNames.length;
  18. //begin render loops
  19. for (var i=0; i < numRows; i++) {
  20. var row = dataGrid[i];
  21. outputText += "{";
  22. for (var j=0; j < numColumns; j++) {
  23. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  24. var rowOutput = row[j] || "null";
  25. } else {
  26. var rowOutput = '"'+( row[j] || "" )+'"';
  27. };
  28. outputText += (headerNames[j] + ":" + rowOutput)
  29. if (j < (numColumns-1)) {outputText+=","};
  30. };
  31. outputText += "}";
  32. if (i < (numRows-1)) {outputText += ","+newLine};
  33. };
  34. outputText += "];";
  35. return outputText;
  36. },
  37. //---------------------------------------
  38. // ASP / VBScript
  39. //---------------------------------------
  40. asp: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  41. //inits...
  42. var commentLine = "'";
  43. var commentLineEnd = "";
  44. var outputText = "";
  45. var numRows = dataGrid.length;
  46. var numColumns = headerNames.length;
  47. //begin render loop
  48. for (var i=0; i < numRows; i++) {
  49. var row = dataGrid[i];
  50. for (var j=0; j < numColumns; j++) {
  51. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  52. var rowOutput = row[j] || "null";
  53. } else {
  54. var rowOutput = '"'+( row[j] || "" )+'"';
  55. };
  56. outputText += 'myArray('+j+','+i+') = '+rowOutput+newLine;
  57. };
  58. };
  59. outputText = 'Dim myArray('+(j-1)+','+(i-1)+')'+newLine+outputText;
  60. return outputText;
  61. },
  62. //---------------------------------------
  63. // HTML Table
  64. //---------------------------------------
  65. html: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  66. //inits...
  67. var commentLine = "<!--";
  68. var commentLineEnd = "-->";
  69. var outputText = "";
  70. var numRows = dataGrid.length;
  71. var numColumns = headerNames.length;
  72. //begin render loop
  73. outputText += "<table>"+newLine;
  74. outputText += indent+"<thead>"+newLine;
  75. outputText += indent+indent+"<tr>"+newLine;
  76. for (var j=0; j < numColumns; j++) {
  77. outputText += indent+indent+indent+'<th class="'+headerNames[j]+'-cell">';
  78. outputText += headerNames[j];
  79. outputText += '</th>'+newLine;
  80. };
  81. outputText += indent+indent+"</tr>"+newLine;
  82. outputText += indent+"</thead>"+newLine;
  83. outputText += indent+"<tbody>"+newLine;
  84. for (var i=0; i < numRows; i++) {
  85. var row = dataGrid[i];
  86. var rowClassName = ""
  87. if (i === numRows-1) {
  88. rowClassName = ' class="lastRow"';
  89. } else if (i === 0){
  90. rowClassName = ' class="firstRow"';
  91. }
  92. outputText += indent+indent+"<tr"+rowClassName+">"+newLine;
  93. for (var j=0; j < numColumns; j++) {
  94. outputText += indent+indent+indent+'<td class="'+headerNames[j]+'-cell">';
  95. outputText += row[j]
  96. outputText += '</td>'+newLine
  97. };
  98. outputText += indent+indent+"</tr>"+newLine;
  99. };
  100. outputText += indent+"</tbody>"+newLine;
  101. outputText += "</table>";
  102. return outputText;
  103. },
  104. //---------------------------------------
  105. // JSON properties
  106. //---------------------------------------
  107. json: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  108. //inits...
  109. var commentLine = "//";
  110. var commentLineEnd = "";
  111. var outputText = "[";
  112. var numRows = dataGrid.length;
  113. var numColumns = headerNames.length;
  114. //begin render loop
  115. for (var i=0; i < numRows; i++) {
  116. var row = dataGrid[i];
  117. outputText += "{";
  118. for (var j=0; j < numColumns; j++) {
  119. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  120. var rowOutput = row[j] || "null";
  121. } else {
  122. var rowOutput = '"' + ( row[j] || "" ) + '"';
  123. };
  124. outputText += ('"'+headerNames[j] +'"' + ":" + rowOutput );
  125. if (j < (numColumns-1)) {outputText+=","};
  126. };
  127. outputText += "}";
  128. if (i < (numRows-1)) {outputText += ","+newLine};
  129. };
  130. outputText += "]";
  131. return outputText;
  132. },
  133. //---------------------------------------
  134. // JSON Array of Columns
  135. //---------------------------------------
  136. jsonArrayCols: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  137. //inits...
  138. var commentLine = "//";
  139. var commentLineEnd = "";
  140. var outputText = "";
  141. var numRows = dataGrid.length;
  142. var numColumns = headerNames.length;
  143. //begin render loop
  144. outputText += "{"+newLine;
  145. for (var i=0; i < numColumns; i++) {
  146. outputText += indent+'"'+headerNames[i]+'":[';
  147. for (var j=0; j < numRows; j++) {
  148. if ((headerTypes[i] == "int")||(headerTypes[i] == "float")) {
  149. outputText += dataGrid[j][i] || 0;
  150. } else {
  151. outputText += '"'+(dataGrid[j][i] || "")+'"' ;
  152. }
  153. if (j < (numRows-1)) {outputText+=","};
  154. };
  155. outputText += "]";
  156. if (i < (numColumns-1)) {outputText += ","+newLine};
  157. };
  158. outputText += newLine+"}";
  159. return outputText;
  160. },
  161. //---------------------------------------
  162. // JSON Array of Rows
  163. //---------------------------------------
  164. jsonArrayRows: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  165. //inits...
  166. var commentLine = "//";
  167. var commentLineEnd = "";
  168. var outputText = "";
  169. var numRows = dataGrid.length;
  170. var numColumns = headerNames.length;
  171. //begin render loop
  172. outputText += "["+newLine;
  173. for (var i=0; i < numRows; i++) {
  174. outputText += indent+"[";
  175. for (var j=0; j < numColumns; j++) {
  176. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  177. outputText += dataGrid[i][j] || 0;
  178. } else {
  179. outputText += '"'+(dataGrid[i][j] || "")+'"' ;
  180. }
  181. if (j < (numColumns-1)) {outputText+=","};
  182. };
  183. outputText += "]";
  184. if (i < (numRows-1)) {outputText += ","+newLine};
  185. };
  186. outputText += newLine+"]";
  187. return outputText;
  188. },
  189. //---------------------------------------
  190. // JSON Dictionary
  191. //---------------------------------------
  192. jsonDict: function(dataGrid, headerNames, headerTypes, indent, newLine) {
  193. //inits...
  194. var commentLine = "//";
  195. var commentLineEnd = "";
  196. var outputText = "";
  197. var numRows = dataGrid.length;
  198. var numColumns = headerNames.length;
  199. //begin render loop
  200. outputText += "{" + newLine;
  201. for (var i = 0; i < numRows; i++) {
  202. outputText += indent + '"' + dataGrid[i][0] + '": ';
  203. if (numColumns == 2) {
  204. outputText += _fmtVal(i, 1, dataGrid);
  205. } else {
  206. outputText += '{ ';
  207. for (var j = 1; j < numColumns; j++) {
  208. if (j > 1) outputText += ', ';
  209. outputText += '"' + headerNames[j] + '"' + ":" + _fmtVal(i, j, dataGrid);
  210. }
  211. outputText += '}';
  212. }
  213. if (i < (numRows - 1)) {
  214. outputText += "," + newLine;
  215. }
  216. }
  217. outputText += newLine + "}";
  218. function _fmtVal(i, j) {
  219. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  220. return dataGrid[i][j] || 0;
  221. } else {
  222. return '"'+(dataGrid[i][j] || "")+'"' ;
  223. }
  224. }
  225. return outputText;
  226. },
  227. //---------------------------------------
  228. // MYSQL
  229. //---------------------------------------
  230. mysql: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  231. //inits...
  232. var commentLine = "/*";
  233. var commentLineEnd = "*/";
  234. var outputText = "";
  235. var numRows = dataGrid.length;
  236. var numColumns = headerNames.length;
  237. var tableName = "MrDataConverter"
  238. //begin render loop
  239. outputText += 'CREATE TABLE '+tableName+' (' + newLine;
  240. outputText += indent+"id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"+newLine;
  241. for (var j=0; j < numColumns; j++) {
  242. var dataType = "VARCHAR(255)";
  243. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  244. dataType = headerTypes[j].toUpperCase();
  245. };
  246. outputText += indent+""+headerNames[j]+" "+dataType;
  247. if (j < numColumns - 1) {outputText += ","};
  248. outputText += newLine;
  249. };
  250. outputText += ');' + newLine;
  251. outputText += "INSERT INTO "+tableName+" "+newLine+indent+"(";
  252. for (var j=0; j < numColumns; j++) {
  253. outputText += headerNames[j];
  254. if (j < numColumns - 1) {outputText += ","};
  255. };
  256. outputText += ") "+newLine+"VALUES "+newLine;
  257. for (var i=0; i < numRows; i++) {
  258. outputText += indent+"(";
  259. for (var j=0; j < numColumns; j++) {
  260. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  261. outputText += dataGrid[i][j] || "null";
  262. } else {
  263. outputText += "'"+( dataGrid[i][j] || "" )+"'";
  264. };
  265. if (j < numColumns - 1) {outputText += ","};
  266. };
  267. outputText += ")";
  268. if (i < numRows - 1) {outputText += ","+newLine;};
  269. };
  270. outputText += ";";
  271. return outputText;
  272. },
  273. //---------------------------------------
  274. // PHP
  275. //---------------------------------------
  276. php: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  277. //inits...
  278. var commentLine = "//";
  279. var commentLineEnd = "";
  280. var outputText = "";
  281. var numRows = dataGrid.length;
  282. var numColumns = headerNames.length;
  283. var tableName = "MrDataConverter"
  284. //begin render loop
  285. outputText += "array(" + newLine;
  286. for (var i=0; i < numRows; i++) {
  287. var row = dataGrid[i];
  288. outputText += indent + "array(";
  289. for (var j=0; j < numColumns; j++) {
  290. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  291. var rowOutput = row[j] || "null";
  292. } else {
  293. var rowOutput = '"'+(row[j] || "")+'"';
  294. };
  295. outputText += ('"'+headerNames[j]+'"' + "=>" + rowOutput)
  296. if (j < (numColumns-1)) {outputText+=","};
  297. };
  298. outputText += ")";
  299. if (i < (numRows-1)) {outputText += ","+newLine};
  300. };
  301. outputText += newLine + ");";
  302. return outputText;
  303. },
  304. //---------------------------------------
  305. // Python dict
  306. //---------------------------------------
  307. python: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  308. //inits...
  309. var commentLine = "//";
  310. var commentLineEnd = "";
  311. var outputText = "[";
  312. var numRows = dataGrid.length;
  313. var numColumns = headerNames.length;
  314. //begin render loop
  315. for (var i=0; i < numRows; i++) {
  316. var row = dataGrid[i];
  317. outputText += "{";
  318. for (var j=0; j < numColumns; j++) {
  319. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  320. var rowOutput = row[j] || "None";
  321. } else {
  322. var rowOutput = '"'+(row[j] || "")+'"';
  323. };
  324. outputText += ('"'+headerNames[j] +'"' + ":" + rowOutput );
  325. if (j < (numColumns-1)) {outputText+=","};
  326. };
  327. outputText += "}";
  328. if (i < (numRows-1)) {outputText += ","+newLine};
  329. };
  330. outputText += "];";
  331. return outputText;
  332. },
  333. //---------------------------------------
  334. // Ruby
  335. //---------------------------------------
  336. ruby: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  337. //inits...
  338. var commentLine = "#";
  339. var commentLineEnd = "";
  340. var outputText = "";
  341. var numRows = dataGrid.length;
  342. var numColumns = headerNames.length;
  343. var tableName = "MrDataConverter"
  344. //begin render loop
  345. outputText += "[";
  346. for (var i=0; i < numRows; i++) {
  347. var row = dataGrid[i];
  348. outputText += "{";
  349. for (var j=0; j < numColumns; j++) {
  350. if ((headerTypes[j] == "int")||(headerTypes[j] == "float")) {
  351. var rowOutput = row[j] || "nil"
  352. } else {
  353. var rowOutput = '"'+(row[j] || "")+'"';
  354. };
  355. outputText += ('"'+headerNames[j]+'"' + "=>" + rowOutput)
  356. if (j < (numColumns-1)) {outputText+=","};
  357. };
  358. outputText += "}";
  359. if (i < (numRows-1)) {outputText += ","+newLine};
  360. };
  361. outputText += "];";
  362. return outputText;
  363. },
  364. //---------------------------------------
  365. // XML Nodes
  366. //---------------------------------------
  367. xml: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  368. //inits...
  369. var commentLine = "<!--";
  370. var commentLineEnd = "-->";
  371. var outputText = "";
  372. var numRows = dataGrid.length;
  373. var numColumns = headerNames.length;
  374. //begin render loop
  375. outputText = '<?xml version="1.0" encoding="UTF-8"?>' + newLine;
  376. outputText += "<rows>"+newLine;
  377. for (var i=0; i < numRows; i++) {
  378. var row = dataGrid[i];
  379. outputText += indent+"<row>"+newLine;
  380. for (var j=0; j < numColumns; j++) {
  381. outputText += indent+indent+'<'+headerNames[j]+'>';
  382. outputText += row[j] || ""
  383. outputText += '</'+headerNames[j]+'>'+newLine
  384. };
  385. outputText += indent+"</row>"+newLine;
  386. };
  387. outputText += "</rows>";
  388. return outputText;
  389. },
  390. //---------------------------------------
  391. // XML properties
  392. //---------------------------------------
  393. xmlProperties: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  394. //inits...
  395. var commentLine = "<!--";
  396. var commentLineEnd = "-->";
  397. var outputText = "";
  398. var numRows = dataGrid.length;
  399. var numColumns = headerNames.length;
  400. //begin render loop
  401. outputText = '<?xml version="1.0" encoding="UTF-8"?>' + newLine;
  402. outputText += "<rows>"+newLine;
  403. for (var i=0; i < numRows; i++) {
  404. var row = dataGrid[i];
  405. outputText += indent+"<row ";
  406. for (var j=0; j < numColumns; j++) {
  407. outputText += headerNames[j]+'=';
  408. outputText += '"' + row[j] + '" ';
  409. };
  410. outputText += "></row>"+newLine;
  411. };
  412. outputText += "</rows>";
  413. return outputText;
  414. },
  415. //---------------------------------------
  416. // XML Illustrator
  417. //---------------------------------------
  418. xmlIllustrator: function (dataGrid, headerNames, headerTypes, indent, newLine) {
  419. //inits...
  420. var commentLine = "<!--";
  421. var commentLineEnd = "-->";
  422. var outputText = "";
  423. var numRows = dataGrid.length;
  424. var numColumns = headerNames.length;
  425. //begin render loop
  426. outputText = '<?xml version="1.0" encoding="utf-8"?>' + newLine;
  427. outputText += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20001102//EN" "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd" [' + newLine;
  428. outputText += indent+'<!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/">' + newLine;
  429. outputText += indent+'<!ENTITY ns_vars "http://ns.adobe.com/Variables/1.0/">' + newLine;
  430. outputText += indent+'<!ENTITY ns_imrep "http://ns.adobe.com/ImageReplacement/1.0/">' + newLine;
  431. outputText += indent+'<!ENTITY ns_custom "http://ns.adobe.com/GenericCustomNamespace/1.0/">' + newLine;
  432. outputText += indent+'<!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">' + newLine;
  433. outputText += indent+'<!ENTITY ns_extend "http://ns.adobe.com/Extensibility/1.0/">' + newLine;
  434. outputText += ']>' + newLine;
  435. outputText += '<svg>' + newLine;
  436. outputText += '<variableSets xmlns="&ns_vars;">' + newLine;
  437. outputText += indent+'<variableSet varSetName="binding1" locked="none">' + newLine;
  438. outputText += indent+indent+'<variables>' + newLine;
  439. for (var i=0; i < numColumns; i++) {
  440. outputText += indent+indent+indent+'<variable varName="'+headerNames[i]+'" trait="textcontent" category="&ns_flows;"></variable>' + newLine;
  441. };
  442. outputText += indent+indent+'</variables>' + newLine;
  443. outputText += indent+indent+'<v:sampleDataSets xmlns:v="http://ns.adobe.com/Variables/1.0/" xmlns="http://ns.adobe.com/GenericCustomNamespace/1.0/">' + newLine;
  444. for (var i=0; i < numRows; i++) {
  445. var row = dataGrid[i];
  446. outputText += indent+indent+indent+'<v:sampleDataSet dataSetName="' + row[0] + '">'+newLine;
  447. for (var j=0; j < numColumns; j++) {
  448. outputText += indent+indent+indent+indent+'<'+headerNames[j]+'>'+newLine;
  449. outputText += indent+indent+indent+indent+indent+'<p>' + row[j] + '</p>' +newLine;
  450. outputText += indent+indent+indent+indent+'</'+headerNames[j]+'>'+newLine
  451. };
  452. outputText += indent+indent+indent+'</v:sampleDataSet>'+newLine;
  453. };
  454. outputText += indent+indent+'</v:sampleDataSets>' + newLine;
  455. outputText += indent+'</variableSet>' + newLine;
  456. outputText += '</variableSets>' + newLine;
  457. outputText += '</svg>' + newLine;
  458. return outputText;
  459. },
  460. }