converter.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // converter.js
  3. // Mr-Data-Converter
  4. //
  5. // Created by Shan Carter on 2010-09-01.
  6. //
  7. function DataConverter(nodeId) {
  8. //---------------------------------------
  9. // PUBLIC PROPERTIES
  10. //---------------------------------------
  11. this.nodeId = nodeId;
  12. this.node = $("#"+nodeId);
  13. this.outputDataTypes = [
  14. {"text":"Actionscript", "id":"as", "notes":""},
  15. {"text":"ASP/VBScript", "id":"asp", "notes":""},
  16. {"text":"HTML", "id":"html", "notes":""},
  17. {"text":"JSON - Properties", "id":"json", "notes":""},
  18. {"text":"JSON - Column Arrays", "id":"jsonArrayCols", "notes":""},
  19. {"text":"JSON - Row Arrays", "id":"jsonArrayRows", "notes":""},
  20. {"text":"JSON - Dictionary", "id":"jsonDict", "notes":""},
  21. {"text":"MySQL", "id":"mysql", "notes":""},
  22. {"text":"PHP", "id":"php", "notes":""},
  23. {"text":"Python - Dict", "id":"python", "notes":""},
  24. {"text":"Ruby", "id":"ruby", "notes":""},
  25. {"text":"XML - Properties", "id":"xmlProperties", "notes":""},
  26. {"text":"XML - Nodes", "id":"xml", "notes":""},
  27. {"text":"XML - Illustrator", "id":"xmlIllustrator", "notes":""}];
  28. this.outputDataType = "json";
  29. this.columnDelimiter = "\t";
  30. this.rowDelimiter = "\n";
  31. this.inputTextArea = {};
  32. this.outputTextArea = {};
  33. this.inputHeader = {};
  34. this.outputHeader = {};
  35. this.dataSelect = {};
  36. this.inputText = "";
  37. this.outputText = "";
  38. this.newLine = "\n";
  39. this.indent = " ";
  40. this.root = "rows";
  41. this.child = "row";
  42. this.commentLine = "//";
  43. this.commentLineEnd = "";
  44. this.tableName = "MrDataConverter"
  45. this.useUnderscores = true;
  46. this.headersProvided = true;
  47. this.downcaseHeaders = true;
  48. this.upcaseHeaders = false;
  49. this.includeWhiteSpace = true;
  50. this.useTabsForIndent = false;
  51. }
  52. //---------------------------------------
  53. // PUBLIC METHODS
  54. //---------------------------------------
  55. DataConverter.prototype.create = function(w,h) {
  56. var self = this;
  57. //build HTML for converter
  58. this.inputHeader = $('<div class="groupHeader" id="inputHeader"><p class="groupHeadline">数据源输入<span class="subhead">(可以直接从Excel/CVS中拷贝内容到这里! <a href="#" id="insertSample">简单示例!</a>)</span></p></div>');
  59. this.inputTextArea = $('<textarea class="textInputs" id="dataInput" placeholder="输入CVS或者以tab为间隔的数据"></textarea>');
  60. var outputHeaderText = '<div class="groupHeader" id="outputHeader"><p class="groupHeadline">结果转换为<select name="Data Types" id="dataSelector" class="form-control">';
  61. for (var i=0; i < this.outputDataTypes.length; i++) {
  62. outputHeaderText += '<option value="'+this.outputDataTypes[i]["id"]+'" '
  63. + (this.outputDataTypes[i]["id"] == this.outputDataType ? 'selected="selected"' : '')
  64. + '>'
  65. + this.outputDataTypes[i]["text"]+'</option>';
  66. };
  67. outputHeaderText += '</select><span class="subhead" id="outputNotes"></span></p></div>';
  68. this.outputHeader = $(outputHeaderText);
  69. this.outputTextArea = $('<textarea class="textInputs" id="dataOutput" placeholder="转换后的结果会显示在这里"></textarea>');
  70. this.node.append(this.inputHeader);
  71. this.node.append(this.inputTextArea);
  72. this.node.append(this.outputHeader);
  73. this.node.append(this.outputTextArea);
  74. this.dataSelect = this.outputHeader.find("#dataSelector");
  75. //add event listeners
  76. // $("#convertButton").bind('click',function(evt){
  77. // evt.preventDefault();
  78. // self.convert();
  79. // });
  80. this.outputTextArea.click(function(evt){this.select();});
  81. $("#insertSample").bind('click',function(evt){
  82. evt.preventDefault();
  83. self.insertSampleData();
  84. self.convert();
  85. _gaq.push(['_trackEvent', 'SampleData','InsertGeneric']);
  86. });
  87. $("#dataInput").keyup(function() {self.convert()});
  88. $("#dataInput").change(function() {
  89. self.convert();
  90. _gaq.push(['_trackEvent', 'DataType',self.outputDataType]);
  91. });
  92. $("#dataSelector").bind('change',function(evt){
  93. self.outputDataType = $(this).val();
  94. self.convert();
  95. });
  96. this.resize(w,h);
  97. }
  98. DataConverter.prototype.resize = function(w,h) {
  99. var paneWidth = w;
  100. var paneHeight = (h-90)/2-20;
  101. this.node.css({width:paneWidth});
  102. this.inputTextArea.css({width:paneWidth-20,height:paneHeight});
  103. this.outputTextArea.css({width: paneWidth-20, height:paneHeight});
  104. }
  105. DataConverter.prototype.convert = function() {
  106. this.inputText = this.inputTextArea.val();
  107. this.outputText = "";
  108. //make sure there is input data before converting...
  109. if (this.inputText.length > 0) {
  110. if (this.includeWhiteSpace) {
  111. this.newLine = "\n";
  112. } else {
  113. this.indent = "";
  114. this.newLine = "";
  115. }
  116. CSVParser.resetLog();
  117. var parseOutput = CSVParser.parse(this.inputText, this.headersProvided, this.delimiter, this.downcaseHeaders, this.upcaseHeaders);
  118. var dataGrid = parseOutput.dataGrid;
  119. var headerNames = parseOutput.headerNames;
  120. var headerTypes = parseOutput.headerTypes;
  121. var errors = parseOutput.errors;
  122. this.outputText = DataGridRenderer[this.outputDataType](dataGrid, headerNames, headerTypes, this.indent, this.newLine);
  123. //验证成功,将会对其中的节点进行替换
  124. //否者,直接对数据进行输出
  125. if(this.root
  126. && this.child
  127. && (this.outputDataType === "xmlProperties"
  128. || this.outputDataType === "xml")){
  129. //替换其中的根节点与字节点
  130. this.outputText = this.outputText.replace(/rows/g,this.root)
  131. this.outputText = this.outputText.replace(/row/g,this.child);
  132. }
  133. this.outputTextArea.val(errors + this.outputText);
  134. }; //end test for existence of input text
  135. }
  136. DataConverter.prototype.insertSampleData = function() {
  137. this.inputTextArea.val("NAME\tVALUE\tCOLOR\tDATE\nAlan\t12\tblue\tSep. 25, 2009\nShan\t13\t\"green\tblue\"\tSep. 27, 2009\nJohn\t45\torange\tSep. 29, 2009\nMinna\t27\tteal\tSep. 30, 2009");
  138. }