-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata.js
159 lines (147 loc) · 4.86 KB
/
data.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
// goog.provide('Chatbot');
if(typeof Chatbot !== 'undefined'){}else{
Chatbot = {};
}
Chatbot.database = null;
// Same as 'window.onload', but uses jQuery so it doesn't overwrite the other one in index.js
Chatbot.saveLocalWorkspace = function() {
var xml = Blockly.Xml.domToText(Blockly.Xml.workspaceToDom(Blockly.mainWorkspace));
localStorage.setItem('xml', xml);
};
Chatbot.getBlocks = function() {
return Blockly.Xml.domToText(Blockly.Xml.workspaceToDom(Blockly.mainWorkspace));
};
Chatbot.getScript = function() {
return Blockly.JavaScript.workspaceToCode(Blockly.mainWorkspace);
};
Chatbot.loadLocalWorkspace = function() {
if (localStorage.xml != null) {
Blockly.mainWorkspace.clear();
Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(localStorage.xml), Blockly.mainWorkspace);
} else {
console.log('No local save found');
}
};
Chatbot.getBlocksFromServer = function(userId) {
// This is for end users since it doesn't require logging in.
// Returns a promise, so use .then on the return variable
// ex: Chatbot.getBlocksFromServer(x,y).then(function(data){console.log(data);});
return $.get(`https://blocklychatbot.firebaseio.com/${userId}/blocks.json`).done(function(data) {
console.log("GET succeeded");
return data;
}).fail(function(error) {
console.log("GET failed");
console.log(error);
});
};
Chatbot.getScriptFromServer = function(userId) {
// This is for end users since it doesn't require logging in.
// Returns a promise, so use .then on the return variable
// ex: Chatbot.getBlocksFromServer(x,y).then(function(data){console.log(data);});
return $.get(`https://blocklychatbot.firebaseio.com/${userId}/script.json`).done(function(data) {
console.log("GET succeeded");
return data;
}).fail(function(error) {
console.log("GET failed");
console.log(error);
});
};
Chatbot.readFromServer = function() {
//This is for use with the block editor and requires a user to be logged in
// Returns a promise, so use .then on the return variable
// ex: Chatbot.readFromServer(x).then(function(data){console.log(data);});
return Chatbot.database.once('value').then(function(snapshot) {
return snapshot.val();
});
};
Chatbot.writeToServer = function(blocks, script) {
var updates = {};
updates.blocks = blocks;
updates.script = script;
console.log('here');
Chatbot.readFromServer().then(function(data) {
if(data == null){
data = {blocks: ""};
}
if (data.blocks == blocks) {
return;
} else {
if (confirm("Are you sure you want to overwrite your cloud files? This action cannot be undone.")) {
Chatbot.database.update(updates).then(function() {
console.log("Successfully wrote to server");
}).catch(function(error) {
console.log("Failed to write to server");
console.log(error);
});
} else {
return;
}
}
});
};
Chatbot.getUserId = function(){
var user = firebase.auth().currentUser;
if(user){
return user.uid;
}else{
console.log("Can't retrieve id without being logged in.");
return;
}
};
$(document).ready(function() {
var loginButton = $("#loginButton");
var config = {
apiKey: "AIzaSyBaistpX-tR7EgwF_ZPBfVzAKobRN_e_B0",
authDomain: "blocklychatbot.firebaseapp.com",
databaseURL: "https://blocklychatbot.firebaseio.com",
projectId: "blocklychatbot",
storageBucket: "",
messagingSenderId: "857756444543"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
console.log("signed in");
// console.log(user);
Chatbot.database = firebase.database().ref(user.uid);
loginButton.text("Log Out");
} else {
// User is signed out.
console.log("signed out");
loginButton.text("Log In");
}
}, function(error) {
console.log(error);
});
loginButton.click(function() {
var user = firebase.auth().currentUser;
if (user) {
firebase.auth().signOut().then(function() {
}).catch(function(error) {
console.log("error signing in");
console.log(error);
});
} else {
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
console.log(result.user.displayName + " has logged in");
}).catch(function(error) {
//Handle errors
console.log('error logging in');
console.log(error);
});
}
});
$("#publish").click(function() {
Chatbot.writeToServer(Chatbot.getBlocks(), Chatbot.getScript());
});
$("#loadCloud").click(function() {
console.log("here");
Chatbot.readFromServer().then(function(data){
console.log("blocks:",data.blocks);
Blockly.mainWorkspace.clear();
Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(data.blocks), Blockly.mainWorkspace);
});
});
});