-
Notifications
You must be signed in to change notification settings - Fork 68
/
gh.js
191 lines (157 loc) · 4.47 KB
/
gh.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
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
/**
* Created by haiku on 8/19/15.
*/
var GH = {};
// github instance
GH._g = false;
// gist data
GH.fGist = false;
GH.fGistId = false;
// timer stuff
GH.tTime = false;
GH.tId = false;
GH.setup = function(username, password) {
GH.username = username;
GH.password = password;
GH._g = new Github({
username: GH.username,
password: GH.password,
auth: 'basic'
});
GH.findSyncGist();
};
GH.teardown = function() {
GH._g = false;
GH.fGist = false;
GH.fGistId = false;
clearInterval(GH.tId);
GH.username = null;
GH.password = null;
};
GH.getExportData = function() {
Molpy.RefreshExport();
return document.getElementById('exporttext').value;
};
GH.importDataFromString = function(data) {
Molpy.FromNeedlePulledThing(Molpy.BeanishToCuegish(data));
Molpy.Save();
};
GH.withAllUserGists = function(cb) {
GH._g.getUser().userGists(GH.username, function(err, gists) {
if (!err) {
cb(gists)
} else {
alert('Something went wrong while getting user gists')
}
})
};
GH.deleteAllGists = function() {
GH.withAllUserGists(function(gists) {
for (var i = 0; i < gists.length; i++) {
GH._g.getGist(gists[i].id).delete(function(err, res) {
if (err) {
alert('Something went wrong while deleting gist')
}
})
}3
})
};
GH.creteSyncGist = function() {
var filename = new Date().toString();
delta = {
'description': 'sb-sync',
'files': {
'initial-save': {
'content': GH.getExportData()
}
}
};
GH._g.getGist().create(delta, function(err, res) {
if (!err) {
GH.fGistId = res.id;
GH.fGist = GH._g.getGist(GH.fGistId);
} else {
alert('Something wrong')
}
})
};
GH.findSyncGist = function() {
GH.fGist = false;
GH.fGistId = false;
// first, search gists for previous file
GH.withAllUserGists(function(gists) {
console.log(gists);
for (var i = 0; i < gists.length; i++) {
console.log(gists[i]);
if (gists[i].description == 'sb-sync') {
console.log('Found!');
GH.fGistId = gists[i].id;
GH.fGist = GH._g.getGist(GH.fGistId);
break;
}
}
if (!GH.fGist) { // if not found, create and assign one
GH.creteSyncGist();
console.log('Created!')
}
})
};
GH.prepareToSync = function() {
var filename = new Date().toString();
GH.syncDelta = {
'description': 'sb-sync',
'files': {}
};
GH.syncDelta['files'][filename] = {
'content': GH.getExportData()
}
};
// Export/Import
// exports data to a new file in a gist each time
GH.exportToCloud = function() {
if (GH.fGist) {
GH.prepareToSync();
GH.fGist.update(GH.syncDelta, function(err, res) {
if (!err) {
console.log('Successfully exported!')
} else {
console.log('Couldn\'t export save to cloud!')
}
})
} else {
console.log('No gist currently selected!')
}
};
// this goes through list of files within a gist and imports data from the latest one
GH.importFromCloud = function() {
var savefile = false;
var cmp = Infinity;
var filenames = [];
if (GH.fGist) {
GH.fGist.read(function(err, res) {
if (!err) {
for (k in res.files) {
filenames.push(k);
}
for (var i = 0; i < filenames.length; i++) {
if ((new Date(filenames[i])).valueOf()) {
console.log(filenames[i]);
if ((new Date() - new Date(filenames[i])) < cmp) {
cmp = (new Date() - new Date(filenames[i]));
savefile = filenames[i];
}
}
}
// if there's no recent backup, get data from first one done
if (!savefile) {
savefile = 'initial-save'
}
GH.importDataFromString(res.files[savefile].content);
console.log('Imported!')
}
})
} else {
console.log('No gist currently selected!')
}
};
//GH.createGist = function()