/
OhJavascript.js
159 lines (145 loc) · 4.6 KB
/
OhJavascript.js
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
var serverUrl = 'https://ec2-35-166-174-199.us-west-2.compute.amazonaws.com/'
var lastBox = "box";
$(document).ready(function () {
$("#raw").focusin(function () {
document.getElementById("raw").style.opacity = 100;
});
$("#raw").focusout(function () {
$("#converted").html(prettifyPLZ($("#raw").text()));
document.getElementById("raw").style.opacity = 0;
});
$("#input_file").bind('change', handleFileUpload);
var obj = $("."+lastBox);
var counter = 0;
obj.on('dragenter', function (e) {
e.stopPropagation();
e.preventDefault();
counter++;
$(this).removeClass().addClass('box_is_dragover');
});
obj.on('dragover', function (e) {
e.stopPropagation();
e.preventDefault();
});
obj.on('dragleave', function (e) {
e.preventDefault();
counter--;
if (counter == 0) {
$(this).removeClass().addClass(lastBox);
}
});
obj.on('drop', function (e) {
e.stopPropagation();
e.preventDefault();
$(this).removeClass().addClass('box');
storeCSV(e.originalEvent.dataTransfer.files);
});
$(document).on('dragenter', function (e) {
e.stopPropagation();
e.preventDefault();
});
$(document).on('dragover', function (e) {
e.stopPropagation();
e.preventDefault();
});
$(document).on('drop', function (e) {
e.stopPropagation();
e.preventDefault();
});
});
function handleFileUpload(e) {
storeCSV(e.target.files);
}
function storeCSV(files)
{
var file = files[0];
var reader = new FileReader();
sessionStorage.clear();
reader.readAsText(file);
reader.onload = function (event) {
var csv = event.target.result;
sessionStorage.setItem("csv", csv);
validateCSV();
}
}
function prettifyPLZ(someDopeText) {
var daUltimateRegex = /{{[a-zA-Z0-9.-]{1,30}}}/g;
var arrayOfSweetMatches;
while (arrayOfSweetMatches = daUltimateRegex.exec(someDopeText)) {
someDopeText = someDopeText.replace(arrayOfSweetMatches[0], "<span class=\"bubble\"><span class=\"invisible\">{{</span>" + arrayOfSweetMatches[0].slice(2, -2) + "<span class=\"invisible\">}}</span></span>");
}
return someDopeText;
}
function validateCSV() {
try{
var input_Csv = window.btoa(sessionStorage.getItem("csv"));
var input_Text = window.btoa(getText());
postInputCsv(input_Csv, input_Text);
} catch (e) {
$('.' + lastBox).removeClass().addClass('box_invalid');
$("#sendButton").prop("disabled", true)
lastBox = "box_invalid";
}
}
function getText() {
var input = $(".textarea#converted").text();
return input;
}
function postInputCsv(data, text) {
var url = serverUrl + 'upload_csv';
$.ajax({
'method': 'POST',
'url': url,
'header': 'application/json',
'data': { csv: data, userInput: text }
}).then(function successCallback(res) {
if (res == true) {
$('.' + lastBox).removeClass().addClass('box_valid');
lastBox = "box_valid";
$("#sendButton").prop("disabled", false);
}
else {
$('.' + lastBox).removeClass().addClass('box_invalid');
$("#sendButton").prop("disabled", true)
lastBox = "box_invalid";
//$('.box').css('background-color', 'red');
}
}, function errorCallback(res) {
console.log('Error: ' + res.data)
});
}
function sendSMS() {
var sid = $('#SID').val();
var authToken = $('#authToken').val();
$.ajax({
'method': 'POST',
'url': serverUrl + 'sendText',
'header': 'application/json',
'data': { SID: sid, authToken: authToken }
});
}
function examplePostRequest() {
var testObject = {
'name': 'This is some arbitrary JSON object',
'info': 'Can contain anything'
};
console.log('Sending: ' + testObject + ' to the server')
$.ajax({
'method': 'POST',
'url': serverUrl + 'upload_csv',
'header': 'application/json',
'data': testObject
}).then(function successCallback(res) {
console.log('Successfully received' + res.name + 'from the server')
}, function errorCallback(res) {
console.log('Error: ' + res.data)
});
}
var isAdvancedUpload = function () {
var div = document.createElement('div');
return (('draggable' in div) || ('ondragstart' in div && 'ondrop' in div)) && 'FormData' in window && 'FileReader' in window;
}();
var $form = $('.box');
if (isAdvancedUpload) {
$form.addClass('has-advanced-upload');
}