This repository was archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwikitree.js
More file actions
250 lines (203 loc) · 7.38 KB
/
wikitree.js
File metadata and controls
250 lines (203 loc) · 7.38 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* ======================================================================
*
*
*
*
* ======================================================================
*/
window.wikitree = window.wikitree || {};
wikitree.API_URL = 'https://api.wikitree.com/api.php';
/* ======================================================================
*
* Session
*
/* ======================================================================
*/
// Create functional object "class"
wikitree.Session = function(opts) {
this.user_id = (opts && opts.user_id) ? opts.user_id : $.cookie('wikitree_wtb_UserID');
this.user_name = (opts && opts.user_name) ? opts.user_name : $.cookie('wikitree_wtb_UserName');
this.loggedIn = false;
};
// Define new method for Session objects to check the current login.
// Return a promise object (from our .ajax() call) so we can do things when this resolves.
wikitree.Session.prototype.checkLogin = function (opts){
var self = this;
if (opts && opts.user_id) { self.user_id = opts.user_id; }
if (opts && opts.user_name) { self.user_name = opts.user_name; }
var request = $.ajax({
url: wikitree.API_URL,
crossDomain: true,
xhrFields: { withCredentials: true },
type: 'POST',
dataType: 'json',
data: { 'action': 'login', 'user_id': self.user_id },
// Local success handling to set our cookies.
success: function(data) {
if (data.login.result == self.user_id) {
$.cookie('wikitree_wtb_UserID', self.user_id);
$.cookie('wikitree_wtb_UserName', self.user_name);
self.loggedIn = true;
} else {
//$.cookie('wikitree_wtb_UserID', '');
//$.cookie('wikitree_wtb_UserName', '');
self.loggedIn = false;
}
},
error: function(xhr, status) {
$.cookie('wikitree_wtb_UserID', '');
$.cookie('wikitree_wtb_UserName', '');
self.loggedIn = false;
}
});
return request.promise();
}
// Do an actual login through the server API with an Ajax call.
// This is for bots where the script has a built-in/known username/password.
wikitree.Session.prototype.login = function(opts) {
var self = this;
var email = (opts && opts.email) ? opts.email : '';
var password = (opts && opts.password) ? opts.password : '';
var request = $.ajax({
url: wikitree.API_URL,
crossDomain: true,
xhrFields: { withCredentials: true },
type: 'POST',
dataType: 'json',
data: { 'action': 'login', 'email': email, 'password': password },
// On successful POST return, check our data. Note from that data whether the login itself was
// successful (setting session cookies if so). Call the user callback function when done.
success: function(data) {
if (data.login.result == 'Success') {
self.user_id = data.login.userid;
self.user_name = data.login.username;
self.loggedIn = true;
$.cookie('wikitree_wtb_UserID', self.user_id);
$.cookie('wikitree_wtb_UserName', self.user_name);
} else {
this.loggedIn = false;
$.cookie('wikitree_wtb_UserID', self.user_id);
$.cookie('wikitree_wtb_UserName', self.user_name);
}
},
// On failed POST/server error, act like a failed login.
error: function(xhr, status) {
self.user_id = 0;
self.user_name = '';
self.loggedin = false;
$.cookie('wikitree_wtb_UserID', self.user_id);
$.cookie('wikitree_wtb_UserName', self.user_name);
}
});
return request.promise();
}
// Do an actual login through the server API with an Ajax call.
// This is for interactive apps where we sent the user to wikitree.com to login and got
// back an auth code to use for login here.
wikitree.Session.prototype.clientLogin = function(opts) {
var self = this;
var authcode = (opts && opts.authcode) ? opts.authcode : '';
var request = $.ajax({
url: wikitree.API_URL,
crossDomain: true,
xhrFields: { withCredentials: true },
type: 'POST',
dataType: 'json',
data: { 'action': 'clientLogin', 'authcode': authcode },
// On successful POST return, check our data. Note from that data whether the login itself was
// successful (setting session cookies if so). Call the user callback function when done.
success: function(data) {
if (data.clientLogin.result == 'Success') {
self.user_id = data.clientLogin.userid;
self.user_name = data.clientLogin.username;
self.loggedIn = true;
$.cookie('wikitree_wtb_UserID', self.user_id, {'path': '/'});
$.cookie('wikitree_wtb_UserName', self.user_name, {'path': '/'});
} else {
self.loggedIn = false;
self.error = data.error;
$.cookie('wikitree_wtb_UserID', '', {'path': '/'});
$.cookie('wikitree_wtb_UserName', '', {'path': '/'});
}
},
// On failed POST/server error, act like a failed login.
error: function(xhr, status) {
self.user_id = 0;
self.user_name = '';
self.loggedin = false;
$.cookie('wikitree_wtb_UserID', '', {'path': '/'});
$.cookie('wikitree_wtb_UserName', '', {'path': '/'});
}
});
return request.promise();
}
wikitree.Session.prototype.logout = function(opts) {
this.loggedIn = false;
this.user_id = 0;
this.user_name = '';
$.removeCookie('wikitree_wtb_UserID');
$.removeCookie('wikitree_wtb_UserName');
}
wikitree.init = function(opts) {
wikitree.session = new wikitree.Session();
}
/*
* Person
*
*
*/
// wikitree.Person( opts )
wikitree.Person = function(opts){
this.user_id = (opts && opts.user_id) ? opts.user_id : 0;
this.loaded = false;
this.loading = false;
};
// wikitree.Person.load(callback, [fields])
// The API on the server is called with $.ajax/$.post functions from jQuery. Those calls are *asynchronous*.
wikitree.Person.prototype.load = function(opts){
var self = this;
// If we have a fields passed in, use those. If not, use a default set.
var fields = 'Id,Name,FirstName,MiddleName,LastNameAtBirth,LastNameCurrent,BirthDate,DeathDate,Father,Mother';
if (opts && opts.fields) { fields = opts.fields; }
// If this Person is already loaded, we're all done. If not, we (may) have work to do.
if (!self.loaded) {
// Javascript will run right through the .ajax() call below and it's possible this Person.load() function will get called
// again before loaded = true and before the first .ajax() call has returned. We don't want to post to the server more than once.
// If we're loading already, we don't have anything new to do.
if (!self.loading) {
// Start loading our content from the server API.
self.loading = true;
// Post our content to the server API, passing along the requested fields.
// Use crossDomain=true in case we end up hosting this on something like apps.wikitree.com but configured
// to query the live database/API at www.wikitree.com.
var request = $.ajax({
url: wikitree.API_URL,
crossDomain: true,
xhrFields: { withCredentials: true },
type: "POST",
dataType: 'json',
data: { 'action': 'getPerson', 'key': self.user_id, 'fields': fields, 'format': 'json' },
// On success, we note that we're done loading. If we got data back, we store it in self and set loaded=true.
success: function(data) {
self.loading = false;
self.status = data[0].status;
if (!self.status) {
for (var x in data[0].person) {
self[x] = data[0].person[x];
}
} else {
self.user_id = 0;
}
self.loaded = true;
},
// On error, report the "status" we got back.
error: function(xhr, status) {
self.loading = false;
self.loaded = false;
self.status = 'Error in API query';
}
});
return request.promise();
}
}
};