Thomas Wade a205dc9c7e Add FAQ functionality and some style tweaks
Switched to HTML5-style routing
Reworked Express backend to accommodate HTML5-style routing
Links are now highlighted with a black background and white foreground
Reworked spacing on H1s
Added classes to nav buttons
2018-10-30 17:33:24 +10:30

157 lines
3.9 KiB
JavaScript

var app = angular.module("app", ["ngRoute", "ngSanitize", "ngTagsInput"]);
app.config(
function ($routeProvider, $locationProvider)
{
$routeProvider
.when("/", {templateUrl: "views/blog.html", controller: "blog"})
.when("/create", {templateUrl: "views/create.html", controller: "create"})
.when("/about", {templateUrl: "views/about.html", controller: "about"})
.when("/faq", {templateUrl: "views/faq.html", controller: "faq"})
.otherwise({templateUrl: "404.html"});
$locationProvider.html5Mode({enabled: true, requireBase: false});
}
);
app.run(($anchorScroll) =>
{
$anchorScroll.yOffset = 100;
})
app.controller("blog", ["$scope", "$http",
function($scope, $http)
{
$scope.posts = [];
$http.get("/api/posts")
.then((r) => { $scope.posts = angular.fromJson(r.data); });
}
]);
app.controller("create", ["$scope", "$http",
function($scope, $http)
{
$scope.title = "";
$scope.author = "";
$scope.content = "";
$scope.tags = [];
$scope.previewing = false;
$scope.onSubmit = function()
{
// Create the post object
post = {
"title": $scope.title,
"author": $scope.author,
"content": $scope.content,
"tags": $scope.tags,
"date": new Date().getTime()
};
// POST it to the server
$http.post("/api/posts", post);
// Clear the fields
$scope.title = "";
$scope.author = "";
$scope.content = "";
$scope.tags = [];
alert("Post has been submitted.");
}
$scope.onTagAdding = ($tag) =>
{
if (!$tag.text.startsWith("#"))
$tag.text = "#" + $tag.text;
return $scope.tags.indexOf($tag.text) === -1;
}
$scope.onPreview = () =>
{
$scope.previewing = !$scope.previewing;
// Update HTML on preview to avoid parsing for each character change
$scope.previewHtml = () =>
{
return $scope.content && $scope.previewing
? marked($scope.content)
: "";
}
}
}
]);
app.controller("about", ["$scope", "$http",
function($scope, $http)
{
$scope.aboutContent = "";
$http.get("/api/about")
.then((r) => { $scope.aboutContent = angular.fromJson(r.data).content; });
}
]);
app.controller("faq", ["$scope", "$http", "$location", "$anchorScroll",
function($scope, $http, $location, $anchorScroll)
{
$scope.entries = []
$http.get("/api/faq")
.then((r) => { $scope.entries = angular.fromJson(r.data) });
$scope.scrollTo = (hash) =>
{
$anchorScroll(hash);
};
}
]);
app.controller("navButtons", ["$scope", "$location",
function($scope, $location)
{
// Set the current relative path
// Helpful to set the navButtons appropriately when refreshing
$scope.current = $location.path();
// Changes location to the given target and sets the current path
$scope.navClick = (target) =>
{
$location.path(target);
$scope.current = target;
}
// Applies the 'current' class if the target matches the current path
$scope.isCurrent = (target) =>
{
return $scope.current == target ? "current" : "";
}
}
]);
// <head>-specific functionality
app.controller("head", ["$scope",
function($scope)
{
$scope.title = "Lemonblog";
}
]);
app.controller("footer", ["$scope",
function($scope)
{
$scope.year = new Date().getFullYear();
}
]);
app.filter("markdown", () =>
{
return function(input)
{
return marked(input);
}
});