added fetching of source

This commit is contained in:
sm
2015-08-30 01:48:35 -07:00
parent 33f94af784
commit 6bad869501
2 changed files with 39 additions and 123 deletions
+25 -102
View File
@@ -1,73 +1,12 @@
var savebot = function(name, callback){
data = {
"id": $("#robot_picker").val(),
"code": editor.getValue()
};
if (name){
data.name = name;
}
$.ajax({
method:"POST",
data: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
url:"/api/v1/bot/" + $("#robot_picker").val() + "/",
success: function(data){
var d = new Date();
$("#lastsave").html("Saved at " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
if (callback){
callback();
}
}
});
};
var newbot = function(){
data = {
"code": editor.getValue(),
};
$.ajax({
method:"POST",
data: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
url:"/api/v1/bot/" + $("#robot_picker").val() + "/",
success: function(data){
console.log(data);
if (data.id){
$("#robot_picker").val(data.id);
}
// I'm lazy
set_current_bot(data.id);
location.reload();
}
});
};
var set_current_bot = function(name){
var queryParameters = {}, queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
while (m = re.exec(queryString)) {
queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
queryParameters['bot'] = name;
history.pushState(null, null, "?"+ $.param(queryParameters));
};
$(document).ready(function(){
$('#starter,#watch').prop('disabled', 'disabled');
$("#error").hide();
editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
function resizeAce() {
return $('#editor').height($(window).height() - $("#editor").offset().top - 20);
return $('#editor').height($(window).height() - $("#editor").offset().top - 100);
};
$(window).resize(resizeAce);
@@ -85,44 +24,7 @@ $(document).ready(function(){
});
});
$("#save").click(function(e){
savebot();
});
$("#new").click(function(e){
newbot();
});
var renaming = false;
$("#rename").click(function(e){
if (renaming){
// POST new name to server
savebot($("#newname").val(), function(){
// BE LAZY
location.reload();
});
$("#rename-block").hide();
$("#rename").html("Rename Bot");
renaming = false;
} else {
$("#rename-block").show();
$("#rename").html("Ok");
renaming = true;
}
});
$.ajax({
url:"/api/v1/bot/1/",
success: function(data){
editor.setValue(data.code);
$("#robot_picker").val(1);
}
});
$('#starter').click(function() {
savebot();
$('#tabs a[href="#arena"]').tab('show');
var url = "ws://{{ server.hostname }}:8666/ws/";
gorobots.set_spectator(false);
@@ -132,8 +34,6 @@ $(document).ready(function(){
);
});
$('#watch').click(function() {
savebot();
$('#tabs a[href="#arena"]').tab('show');
var url = "ws://{{ server.hostname }}:8666/ws/";
gorobots.set_spectator(true);
@@ -145,4 +45,27 @@ $(document).ready(function(){
$('#tabs a[href="#code"]').tab('show');
$("#fetch-url").keydown(function(e){
if(e.keyCode == 13 || e.keyCode == 32){
loadCode();
}
});
$("#fetch-button").click(loadCode);
});
function loadCode() {
var url = $("#fetch-url").val();
$.get(url).success(function(e) {
$("#error").val(e).hide();
editor.setValue(e);
$("#editor").show();
$("#fetch-area").hide();
$('#starter,#watch').prop('disabled', function(i, v) { return !v; });
}).fail(function(e){
var msg = "got a useless error message; is the remote server down?";
if (e.statusText != "error") {
msg = e.responseText;
}
$("#error").text(msg).show();
});
}