-
Notifications
You must be signed in to change notification settings - Fork 769
Expand file tree
/
Copy pathconverter.js
More file actions
executable file
·178 lines (129 loc) · 6.24 KB
/
Copy pathconverter.js
File metadata and controls
executable file
·178 lines (129 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// converter.js
// Mr-Data-Converter
//
// Created by Shan Carter on 2010-09-01.
//
function DataConverter(nodeId) {
//---------------------------------------
// PUBLIC PROPERTIES
//---------------------------------------
this.nodeId = nodeId;
this.node = $("#"+nodeId);
this.outputDataTypes = [
{"text":"Actionscript", "id":"as", "notes":""},
{"text":"ASP/VBScript", "id":"asp", "notes":""},
{"text":"HTML", "id":"html", "notes":""},
{"text":"JSON - Properties", "id":"json", "notes":""},
{"text":"JSON - Column Arrays", "id":"jsonArrayCols", "notes":""},
{"text":"JSON - Row Arrays", "id":"jsonArrayRows", "notes":""},
{"text":"JSON - Dictionary", "id":"jsonDict", "notes":""},
{"text":"MySQL", "id":"mysql", "notes":""},
{"text":"PHP", "id":"php", "notes":""},
{"text":"Python - Dict", "id":"python", "notes":""},
{"text":"Ruby", "id":"ruby", "notes":""},
{"text":"XML - Properties", "id":"xmlProperties", "notes":""},
{"text":"XML - Nodes", "id":"xml", "notes":""},
{"text":"XML - Illustrator", "id":"xmlIllustrator", "notes":""}];
this.outputDataType = "json";
this.columnDelimiter = "\t";
this.rowDelimiter = "\n";
this.inputTextArea = {};
this.outputTextArea = {};
this.inputHeader = {};
this.outputHeader = {};
this.dataSelect = {};
this.inputText = "";
this.outputText = "";
this.newLine = "\n";
this.indent = " ";
this.commentLine = "//";
this.commentLineEnd = "";
this.tableName = "MrDataConverter"
this.useUnderscores = true;
this.headersProvided = true;
this.downcaseHeaders = true;
this.upcaseHeaders = false;
this.includeWhiteSpace = true;
this.useTabsForIndent = false;
this.includeKeyInDictionary = false;
}
//---------------------------------------
// PUBLIC METHODS
//---------------------------------------
DataConverter.prototype.create = function(w,h) {
var self = this;
//build HTML for converter
this.inputHeader = $('<div class="groupHeader" id="inputHeader"><p class="groupHeadline">Input CSV or tab-delimited data. <span class="subhead"> Using Excel? Simply copy and paste. No data on hand? <a href="#" id="insertSample">Use sample</a></span></p></div>');
this.inputTextArea = $('<textarea class="textInputs" id="dataInput"></textarea>');
var outputHeaderText = '<div class="groupHeader" id="inputHeader"><p class="groupHeadline">Output as <select name="Data Types" id="dataSelector" >';
for (var i=0; i < this.outputDataTypes.length; i++) {
outputHeaderText += '<option value="'+this.outputDataTypes[i]["id"]+'" '
+ (this.outputDataTypes[i]["id"] == this.outputDataType ? 'selected="selected"' : '')
+ '>'
+ this.outputDataTypes[i]["text"]+'</option>';
};
outputHeaderText += '</select><span class="subhead" id="outputNotes"></span></p></div>';
this.outputHeader = $(outputHeaderText);
this.outputTextArea = $('<textarea class="textInputs" id="dataOutput"></textarea>');
this.node.append(this.inputHeader);
this.node.append(this.inputTextArea);
this.node.append(this.outputHeader);
this.node.append(this.outputTextArea);
this.dataSelect = this.outputHeader.find("#dataSelector");
//add event listeners
// $("#convertButton").bind('click',function(evt){
// evt.preventDefault();
// self.convert();
// });
this.outputTextArea.click(function(evt){this.select();});
$("#insertSample").bind('click',function(evt){
evt.preventDefault();
self.insertSampleData();
self.convert();
_gaq.push(['_trackEvent', 'SampleData','InsertGeneric']);
});
$("#dataInput").keyup(function() {self.convert()});
$("#dataInput").change(function() {
self.convert();
_gaq.push(['_trackEvent', 'DataType',self.outputDataType]);
});
$("#dataSelector").bind('change',function(evt){
self.outputDataType = $(this).val();
self.convert();
});
this.resize(w,h);
}
DataConverter.prototype.resize = function(w,h) {
var paneWidth = w;
var paneHeight = (h-90)/2-20;
this.node.css({width:paneWidth});
this.inputTextArea.css({width:paneWidth-20,height:paneHeight});
this.outputTextArea.css({width: paneWidth-20, height:paneHeight});
}
DataConverter.prototype.convert = function() {
this.inputText = this.inputTextArea.val();
this.outputText = "";
//make sure there is input data before converting...
if (this.inputText.length > 0) {
if (this.includeWhiteSpace) {
this.newLine = "\n";
// console.log("yes")
} else {
this.indent = "";
this.newLine = "";
// console.log("no")
}
CSVParser.resetLog();
var parseOutput = CSVParser.parse(this.inputText, this.headersProvided, this.delimiter, this.downcaseHeaders, this.upcaseHeaders);
var dataGrid = parseOutput.dataGrid;
var headerNames = parseOutput.headerNames;
var headerTypes = parseOutput.headerTypes;
var errors = parseOutput.errors;
this.outputText = DataGridRenderer[this.outputDataType](dataGrid, headerNames, headerTypes, this.indent, this.newLine, this.includeKeyInDictionary);
this.outputTextArea.val(errors + this.outputText);
}; //end test for existence of input text
}
DataConverter.prototype.insertSampleData = function() {
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");
}