Properly implement ajax fail callback

This commit is contained in:
James Wang 2016-01-17 02:39:25 +00:00
parent 353b639cbe
commit 3c9121ea05

View file

@ -1,251 +1,251 @@
var app = angular.module("easyctf", [ "ngRoute" ]); var app = angular.module("easyctf", [ "ngRoute" ]);
app.config(["$compileProvider", function($compileProvider) { app.config(["$compileProvider", function($compileProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|javascript):/); $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|javascript):/);
}]); }]);
app.config(function($routeProvider, $locationProvider) { app.config(function($routeProvider, $locationProvider) {
$routeProvider.when("/", { $routeProvider.when("/", {
templateUrl: "pages/home.html", templateUrl: "pages/home.html",
controller: "mainController" controller: "mainController"
}) })
.when("/about", { .when("/about", {
templateUrl: "pages/about.html", templateUrl: "pages/about.html",
controller: "mainController" controller: "mainController"
}) })
.when("/chat", { .when("/chat", {
templateUrl: "pages/chat.html", templateUrl: "pages/chat.html",
controller: "mainController" controller: "mainController"
}) })
.when("/learn", { .when("/learn", {
templateUrl: "pages/learn.html", templateUrl: "pages/learn.html",
controller: "mainController" controller: "mainController"
}) })
.when("/login", { .when("/login", {
templateUrl: "pages/login.html", templateUrl: "pages/login.html",
controller: "mainController" controller: "mainController"
}) })
.when("/logout", { .when("/logout", {
templateUrl: "pages/blank.html", templateUrl: "pages/blank.html",
controller: "logoutController" controller: "logoutController"
}) })
.when("/profile", { .when("/profile", {
templateUrl: "pages/profile.html", templateUrl: "pages/profile.html",
controller: "profileController" controller: "profileController"
}) })
.when("/profile/:username", { .when("/profile/:username", {
templateUrl: "pages/profile.html", templateUrl: "pages/profile.html",
controller: "profileController" controller: "profileController"
}) })
.when("/register", { .when("/register", {
templateUrl: "pages/register.html", templateUrl: "pages/register.html",
controller: "mainController" controller: "mainController"
}) })
.when("/scoreboard", { .when("/scoreboard", {
templateUrl: "pages/scoreboard.html", templateUrl: "pages/scoreboard.html",
controller: "scoreboardController" controller: "scoreboardController"
}) })
.when("/settings", { .when("/settings", {
templateUrl: "pages/settings.html", templateUrl: "pages/settings.html",
controller: "mainController" controller: "mainController"
}) })
.when("/forgot", { .when("/forgot", {
templateUrl: "pages/forgot.html", templateUrl: "pages/forgot.html",
controller: "resetController" controller: "resetController"
}) })
.when("/forgot/:token", { .when("/forgot/:token", {
templateUrl: "pages/forgot.html", templateUrl: "pages/forgot.html",
controller: "resetController" controller: "resetController"
}) })
.when("/team", { .when("/team", {
templateUrl: "pages/team.html", templateUrl: "pages/team.html",
controller: "teamController" controller: "teamController"
}) })
.when("/team/:teamname", { .when("/team/:teamname", {
templateUrl: "pages/team.html", templateUrl: "pages/team.html",
controller: "teamController" controller: "teamController"
}) })
.when("/admin/problems", { .when("/admin/problems", {
templateUrl: "pages/admin/problems.html", templateUrl: "pages/admin/problems.html",
controller: "adminProblemsController" controller: "adminProblemsController"
}) })
.otherwise({ .otherwise({
templateUrl: "pages/404.html", templateUrl: "pages/404.html",
controller: "mainController" controller: "mainController"
}); });
$locationProvider.html5Mode(true); $locationProvider.html5Mode(true);
}); });
app.controller("mainController", ["$scope", "$http", function($scope, $http) { app.controller("mainController", ["$scope", "$http", function($scope, $http) {
$scope.config = { navbar: { } }; $scope.config = { navbar: { } };
$.get("/api/user/status", function(result) { $.get("/api/user/status", function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
delete result["success"]; delete result["success"];
$scope.config.navbar = result; $scope.config.navbar = result;
$scope.$emit("loginStatus"); $scope.$emit("loginStatus");
} else { } else {
$scope.config.navbar.logged_in = false; $scope.config.navbar.logged_in = false;
} }
$scope.$apply(); $scope.$apply();
}).fail(function() { }).fail(function() {
$scope.config.navbar.logged_in = false; $scope.config.navbar.logged_in = false;
$scope.$apply(); $scope.$apply();
}); });
}]); }]);
app.controller("logoutController", function() { app.controller("logoutController", function() {
$.get("/api/user/logout", function(result) { $.get("/api/user/logout", function(result) {
location.href = "/"; location.href = "/";
}); });
}); });
app.controller("profileController", ["$controller", "$scope", "$http", "$routeParams", function($controller, $scope, $http, $routeParams) { app.controller("profileController", ["$controller", "$scope", "$http", "$routeParams", function($controller, $scope, $http, $routeParams) {
var data = { }; var data = { };
if ("username" in $routeParams) data["username"] = $routeParams["username"]; if ("username" in $routeParams) data["username"] = $routeParams["username"];
$controller("mainController", { $scope: $scope }); $controller("mainController", { $scope: $scope });
$.get("/api/user/info", data, function(result) { $.get("/api/user/info", data, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
$scope.user = result["user"]; $scope.user = result["user"];
} }
$scope.$apply(); $scope.$apply();
$(".timeago").timeago(); $(".timeago").timeago();
}); });
}]); }]);
app.controller("loginController", ["$controller", "$scope", "$http", function($controller, $scope, $http) { app.controller("loginController", ["$controller", "$scope", "$http", function($controller, $scope, $http) {
$controller("mainController", { $scope: $scope }); $controller("mainController", { $scope: $scope });
$scope.$on("loginStatus", function() { $scope.$on("loginStatus", function() {
if ($scope.config["navbar"].logged_in != true) { if ($scope.config["navbar"].logged_in != true) {
location.href = "/login"; location.href = "/login";
return; return;
} }
}); });
}]); }]);
app.controller("teamController", ["$controller", "$scope", "$http", "$routeParams", function($controller, $scope, $http, $routeParams) { app.controller("teamController", ["$controller", "$scope", "$http", "$routeParams", function($controller, $scope, $http, $routeParams) {
var data = { }; var data = { };
if ("teamname" in $routeParams) { if ("teamname" in $routeParams) {
data["teamname"] = $routeParams["teamname"]; data["teamname"] = $routeParams["teamname"];
} else { } else {
$controller("loginController", { $scope: $scope }); $controller("loginController", { $scope: $scope });
} }
$.get("/api/team/info", data, function(result) { $.get("/api/team/info", data, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
$scope.team = result["team"]; $scope.team = result["team"];
} }
$scope.$apply(); $scope.$apply();
$(".timeago").timeago(); $(".timeago").timeago();
}); });
}]); }]);
app.controller("scoreboardController", ["$controller", "$scope", "$http", function($controller, $scope, $http) { app.controller("scoreboardController", ["$controller", "$scope", "$http", function($controller, $scope, $http) {
$controller("mainController", { $scope: $scope }); $controller("mainController", { $scope: $scope });
api_call("GET", "/api/stats/scoreboard", { }, function(result) { api_call("GET", "/api/stats/scoreboard", { }, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
$scope.scoreboard = result["scoreboard"]; $scope.scoreboard = result["scoreboard"];
$scope.$apply(); $scope.$apply();
} }
}); });
}]); }]);
app.controller("resetController", ["$controller", "$scope", "$http", "$routeParams", function($controller, $scope, $http, $routeParams) { app.controller("resetController", ["$controller", "$scope", "$http", "$routeParams", function($controller, $scope, $http, $routeParams) {
var data = { }; var data = { };
$scope.token = false; $scope.token = false;
data["csrf_token"] = $.cookie("csrf_token"); data["csrf_token"] = $.cookie("csrf_token");
if ("token" in $routeParams) { if ("token" in $routeParams) {
$scope.token = true; $scope.token = true;
token = $routeParams["token"]; token = $routeParams["token"];
$.get("/api/user/forgot/" + token, data, function(data) { $.get("/api/user/forgot/" + token, data, function(data) {
$scope.body = data["message"]; $scope.body = data["message"];
$scope.success = data["success"] $scope.success = data["success"]
$scope.$apply(); $scope.$apply();
}); });
} else { } else {
$controller("mainController", { $scope: $scope }); $controller("mainController", { $scope: $scope });
} }
}]); }]);
app.controller("adminController", ["$controller", "$scope", "$http", function($controller, $scope, $http) { app.controller("adminController", ["$controller", "$scope", "$http", function($controller, $scope, $http) {
$controller("mainController", { $scope: $scope }); $controller("mainController", { $scope: $scope });
$scope.$on("loginStatus", function() { $scope.$on("loginStatus", function() {
if ($scope.config["navbar"].logged_in != true) { if ($scope.config["navbar"].logged_in != true) {
location.href = "/login"; location.href = "/login";
return; return;
} }
if ($scope.config["navbar"].admin != true) { if ($scope.config["navbar"].admin != true) {
location.href = "/profile"; location.href = "/profile";
return; return;
} }
}); });
}]); }]);
app.controller("adminProblemsController", ["$controller", "$scope", "$http", function($controller, $scope, $http) { app.controller("adminProblemsController", ["$controller", "$scope", "$http", function($controller, $scope, $http) {
$controller("adminController", { $scope: $scope }); $controller("adminController", { $scope: $scope });
$.get("/api/admin/problems/list", function(result) { $.get("/api/admin/problems/list", function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
$scope.problems = result["problems"]; $scope.problems = result["problems"];
} }
$scope.$apply(); $scope.$apply();
}); });
}]); }]);
function display_message(containerId, alertType, message, callback) { function display_message(containerId, alertType, message, callback) {
$("#" + containerId).html("<div class=\"alert alert-" + alertType + "\">" + message + "</div>"); $("#" + containerId).html("<div class=\"alert alert-" + alertType + "\">" + message + "</div>");
$("#" + containerId).hide().slideDown("fast", "swing", function() { $("#" + containerId).hide().slideDown("fast", "swing", function() {
window.setTimeout(function () { window.setTimeout(function () {
$("#" + containerId).slideUp("fast", "swing", callback); $("#" + containerId).slideUp("fast", "swing", callback);
}, message.length * 75); }, message.length * 75);
}); });
}; };
function api_call(method, url, data, callback_success, callback_fail) { function api_call(method, url, data, callback_success, callback_fail) {
if (method.toLowerCase() == "post") { if (method.toLowerCase() == "post") {
data["csrf_token"] = $.cookie("csrf_token"); data["csrf_token"] = $.cookie("csrf_token");
} }
$.ajax({ $.ajax({
"type": method, "type": method,
"datatype": "json", "datatype": "json",
"data": data, "data": data,
"url": url "url": url
}).done(callback_success).fail(callback_fail); }).done(callback_success).fail(callback_fail);
} }
$.fn.serializeObject = function() { $.fn.serializeObject = function() {
var a, o; var a, o;
o = {}; o = {};
a = this.serializeArray(); a = this.serializeArray();
$.each(a, function() { $.each(a, function() {
if (o[this.name]) { if (o[this.name]) {
if (!o[this.name].push) { if (!o[this.name].push) {
o[this.name] = [o[this.name]]; o[this.name] = [o[this.name]];
} }
return o[this.name].push(this.value || ""); return o[this.name].push(this.value || "");
} else { } else {
return o[this.name] = this.value || ""; return o[this.name] = this.value || "";
} }
}); });
return o; return o;
}; };
// register page // register page
var register_form = function() { var register_form = function() {
var input = "#register_form input"; var input = "#register_form input";
var data = $("#register_form").serializeObject(); var data = $("#register_form").serializeObject();
$(input).attr("disabled", "disabled"); $(input).attr("disabled", "disabled");
api_call("POST", "/api/user/register", data, function(result) { api_call("POST", "/api/user/register", data, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
location.href = "/profile"; location.href = "/profile";
} else { } else {
display_message("register_msg", "danger", result["message"], function() { display_message("register_msg", "danger", result["message"], function() {
$(input).removeAttr("disabled"); $(input).removeAttr("disabled");
}); });
} }
}).fail(function(jqXHR, status, error) { }, function(jqXHR, status, error) {
var result = JSON.parse(jqXHR["responseText"]); var result = jqXHR["responseText"];
display_message("register_msg", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() { display_message("register_msg", "danger", "Failed to connect to the API.", function() {
$(input).removeAttr("disabled"); button.removeAttr("disabled");
}); });
}); });
}; };
// password reset // password reset
@ -260,11 +260,11 @@ var request_reset_form = function() {
$(input).removeAttr("disabled"); $(input).removeAttr("disabled");
}); });
} }
}).fail(function(jqXHR, status, error) { }, function(jqXHR, status, error) {
var result = JSON.parse(jqXHR["responseText"]); var result = jqXHR["responseText"];
display_message("reset_msg", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() { display_message("reset_msg", "danger", "Failed to connect to the API.", function() {
$(input).removeAttr("disabled"); button.removeAttr("disabled");
}); });
}); });
} }
@ -284,92 +284,92 @@ var reset_form = function() {
$(input).removeAttr("disabled"); $(input).removeAttr("disabled");
}); });
} }
}).fail(function(jqXHR, status, error) { }, function(jqXHR, status, error) {
var result = JSON.parse(jqXHR["responseText"]); var result = jqXHR["responseText"];
display_message("reset_msg", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() { display_message("reset_msg", "danger", "Failed to connect to the API.", function() {
$(input).removeAttr("disabled"); button.removeAttr("disabled");
}); });
}); });
} }
// login page // login page
var login_form = function() { var login_form = function() {
var input = "#login_form input"; var input = "#login_form input";
var data = $("#login_form").serializeObject(); var data = $("#login_form").serializeObject();
$(input).attr("disabled", "disabled"); $(input).attr("disabled", "disabled");
api_call("POST", "/api/user/login", data, function(result) { api_call("POST", "/api/user/login", data, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
location.href = "/profile"; location.href = "/profile";
} else { } else {
display_message("login_msg", "danger", result["message"], function() { display_message("login_msg", "danger", result["message"], function() {
$(input).removeAttr("disabled"); $(input).removeAttr("disabled");
}); });
} }
}).fail(function(jqXHR, status, error) { }, function(jqXHR, status, error) {
var result = JSON.parse(jqXHR["responseText"]); var result = jqXHR["responseText"];
display_message("login_msg", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() { display_message("login_msg", "danger", "Failed to connect to the API.", function() {
$(input).removeAttr("disabled"); button.removeAttr("disabled");
}); });
}); });
}; };
// team page // team page
var create_team = function() { var create_team = function() {
var input = "#create_team input"; var input = "#create_team input";
var data = $("#create_team").serializeObject(); var data = $("#create_team").serializeObject();
$(input).attr("disabled", "disabled"); $(input).attr("disabled", "disabled");
api_call("POST", "/api/team/create", data, function(result) { api_call("POST", "/api/team/create", data, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
location.reload(true); location.reload(true);
} else { } else {
display_message("create_team_msg", "danger", result["message"], function() { display_message("create_team_msg", "danger", result["message"], function() {
$(input).removeAttr("disabled"); $(input).removeAttr("disabled");
}); });
} }
}).fail(function(jqXHR, status, error) { }, function(jqXHR, status, error) {
var result = JSON.parse(jqXHR["responseText"]); var result = jqXHR["responseText"];
display_message("create_team_msg", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() { display_message("create_team_msg", "danger", "Failed to connect to the API.", function() {
$(input).removeAttr("disabled"); button.removeAttr("disabled");
}); });
}); });
}; };
var add_member = function() { var add_member = function() {
var input = "#add_member input"; var input = "#add_member input";
var data = $("#add_member").serializeObject(); var data = $("#add_member").serializeObject();
$(input).attr("disabled", "disabled"); $(input).attr("disabled", "disabled");
api_call("POST", "/api/team/invite", data, function(result) { api_call("POST", "/api/team/invite", data, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
location.reload(true); location.reload(true);
} else { } else {
$(input).removeAttr("disabled"); $(input).removeAttr("disabled");
} }
}).fail(function(jqXHR, status, error) { }, function(jqXHR, status, error) {
var result = JSON.parse(jqXHR["responseText"]); var result = JSON.parse(jqXHR["responseText"]);
display_message("create_team_msg", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() { display_message("create_team_msg", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() {
$(input).removeAttr("disabled"); $(input).removeAttr("disabled");
}); });
}); });
}; };
var rescind_invitation = function(uid) { var rescind_invitation = function(uid) {
var input = "#add_member input"; var input = "#add_member input";
var data = { "uid": uid }; var data = { "uid": uid };
api_call("POST", "/api/team/invite/rescind", data, function(result) { api_call("POST", "/api/team/invite/rescind", data, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
location.reload(true); location.reload(true);
} }
}); });
}; };
var request_invitation = function(tid) { var request_invitation = function(tid) {
var input = "#add_member input"; var input = "#add_member input";
var data = { "tid": tid }; var data = { "tid": tid };
api_call("POST", "/api/team/invite/request", data, function(result) { api_call("POST", "/api/team/invite/request", data, function(result) {
if (result["success"] == 1) { if (result["success"] == 1) {
location.reload(true); location.reload(true);
} }
}); });
}; };