-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzentext.js
132 lines (123 loc) · 4.23 KB
/
zentext.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
// This script is by the Zentext contributors.
var zentext = (function () {
var results = new Array();
var zentext = new Object();
function result(text, type) {
if (typeof text !== "string") {
text = "";
}
if (type !== 'error' && type !== 'success') {
type = 'result';
}
newResult = {
type: type,
message: text
};
if ( results[0] && results[0].message === "no results yet" ) {
results.splice(0,1);
}
results.push(newResult);
return newResult;
};
zentext.checkType = function (address) {
// Some code to detect the type of message we want to send. Will return 'mobile' if telephone number, 'email' if email.
return result('Please teach me how to check an address\'s type.', 'error');
};
zentext.sendEmail = function (content, subject, address, sender, callback) {
if (!( address && typeof address === "string" )) {
return result('Address not specified or not in an acceptable format.', 'error');
}
if (!( content && typeof content === "string" )) {
return result('Nothing to send or data is not in an acceptable format.', 'error');
}
if (!( sender && typeof sender === "string" )) {
return result('Sender not specified or not in an acceptable format','error');
}
if ( subject === null || subject === undefined ) {
subject = "";
}
if (!( typeof subject === "string" )) {
return result('Subject not in an acceptable format','error')
}
// Some code to send email.
var xhr = new XMLHttpRequest();
xhr.open("POST","/email",true);
xhr.onload = function(e) {
if(callback) {
if( this.status === 200 ) {
var JSONResponse = JSON.parse(this.response);
if(JSONResponse.message === "Queued. Thank you.") {
callback(result("message has been sent successfully", "success"));
}
else {
callback(result(JSONResponse.message, "error"));
}
}
else {
callback(result("server responded an error {code}"+this.status+"{/code}{message}"+this.response+"{/message}","error"));
}
}
}
var sendData = new FormData();
sendData.append("to",address);
sendData.append("from",sender);
sendData.append("subject",subject);
sendData.append("contents",content);
xhr.send(sendData);
return result("sending the email","success");
};
zentext.sendText = function (content, address) {
if (!( address && typeof address === "string" )) {
return result('Address not specified or not in an acceptable format.', 'error');
}
if (!( content && typeof content === "string" )) {
return result('Nothing to send or data is not in an acceptable format.', 'error');
}
// Some code to send a text.
return result('Please teach me how to send texts.', 'error');
};
zentext.makeCall = function (address) {
if (!( address && typeof address === "string" )) {
return result('Address not specified or not in an acceptable format.', 'error');
}
if (!( content && typeof content === "string" )) {
return result('Nothing to send or data is not in an acceptable format.', 'error');
}
// Some code to make a call.
return result('Please teach me how to call people.', 'error');
};
zentext.results = function(format) {
var returnResult = "";
switch (format) {
case 'html':
for ( var i = 0; i < results.length; i++ ) {
switch (results[i].type) {
case "success":
returnResult += '<div class="zentext-success">';
break;
case "error":
returnResult += '<div class="zentext-error">';
break;
case "result":
returnResult += '<div class="zentext-result">';
break;
}
returnResult += results[i].message;
returnResult += '</div>';
}
break;
case 'json':
returnResult = results;
break;
default:
returnResult = result("invalid format","error")
}
return returnResult;
};
zentext.resetResults = function() {
results = new Array();
result("no results yet","error");
}
zentext.resetResults();
return zentext;
})();