mrkdown
Version:
Markdown files to HTML static site generator - angular.js app
249 lines (220 loc) • 9.26 kB
JavaScript
app.controller('authController', ['$scope', "$location", "NgConfig", "$anchorScroll", "$rootScope", function ($scope, $location, NgConfig, $anchorScroll, $rootScope) {
$scope.removeSideBar = NgConfig.removeRightSideBar;
$scope.title = NgConfig.title
$scope.ngConfig = NgConfig;
$scope.user = {};
let favIcon = document.getElementById("favIcon")
favIcon.href = NgConfig.favIconUrl
$scope.useHashValue = NgConfig.useHash ? "#!" : ""
$scope.isMobileView = false
var onResize = function () {
console.log(window.screen.width)
if (window.screen.width < 500) {
$scope.isMobileView = true
setTimeout(() => {
var sidebar = document.getElementById('sidebar')
sidebar.classList.toggle("show")
$scope.isMobileView = true
}, 0);
} else {
$scope.isMobileView = false
}
}
onResize();
window.addEventListener('resize', onResize, true);
var showActiveMenu = function () {
var pathUrl = "";
if (NgConfig.useHash) {
pathUrl = window.location.href.split("#!")[1]
if (!pathUrl) return;
pathUrl = "#!" + pathUrl.replace("/", "") // remove first path value
} else {
pathUrl = window.location.pathname
if (!pathUrl) return;
pathUrl = pathUrl.replace("/", "") // remove first path value
}
var retryCount = 0;
var selector = `#sidebar [href="${pathUrl}"]`
var subscription = setInterval(() => {
if (document.querySelector(selector) && subscription) {
var element = document.querySelector(selector)
if (element.parentElement && element.parentElement.classList) {
element.parentElement.classList.add("active")
}
while (element) {
if (element.tagName.toUpperCase() === "NAV") {
element = null;
continue;
} else if (element.tagName.toUpperCase() === "UL") {
element = element.previousElementSibling
if (element && element.click) element.click()
}
element = element && element.parentElement ? element.parentElement : null
}
setTimeout(() => {
var element = document.querySelector(selector)
if (element.parentElement && element.parentElement.classList)
element.parentElement.classList.add("active")
}, 0);
clearInterval(subscription)
} else {
retryCount++;
}
if (retryCount > 20)
clearInterval(subscription)
}, 100);
}
showActiveMenu();
$scope.isAuthenticated = true;
$scope.checkLoginOption = function () {
setTimeout(() => {
$scope.changeSideHeader(null)
}, 100);
$scope.screenWithEvent()
setTimeout(() => {
document.body.classList.remove("hide")
}, 300);
$scope.hasUser();
}
$scope.hasUser = function () {
if (NgConfig.users && NgConfig.users.length > 0) {
var loginUser = localStorage.getItem("loginUser")
if (loginUser && loginUser.length > 0) {
var user = JSON.parse($scope.decode(loginUser))
if (NgConfig.users.some(x => x.userName == user.userName && x.password == user.password)) {
$scope.user = NgConfig.users.find(x => x.userName == user.userName && x.password == user.password)
$scope.isAuthenticated = true
} else $scope.isAuthenticated = false
} else {
$scope.isAuthenticated = false
}
} else {
$scope.isAuthenticated = true
}
}
$scope.loginModel = {
userName: null,
password: null
}
$scope.login = function () {
debugger
localStorage.setItem("loginUser", $scope.encode(JSON.stringify($scope.loginModel)))
$scope.hasUser();
if (!$scope.isAuthenticated) {
alert("check your username and password and try again")
} else {
$scope.loginModel = {
userName: null,
password: null
}
}
}
$scope.logout = function () {
localStorage.clear(CONST.USER_ID)
window.location.reload()
}
$scope.scrollingTo = function (id) {
$location.hash(id)
$anchorScroll();
}
$scope.sideLinks = [];
$scope.changeSideHeader = function (id) {
console.log(id)
$scope.sideLinks = []
var list = document.getElementsByClassName('active')
for (const iterator of list) {
iterator.classList.remove('active')
}
let activeElementId = id ? id : localStorage.getItem('activeId')
if (activeElementId != 'home') {
if (document.getElementById(activeElementId))
document.getElementById(activeElementId).parentElement.classList.add('active') // li < a
}
$scope.onSideMenuChanges();
setTimeout(() => {
let allHeader = document.querySelectorAll("h1, h2, h3")
let allOb = []
for (const iterator of allHeader) {
var ob = {
id: iterator.id,
name: iterator.id.split('-').join(' ').trim()
}
allOb.push(ob)
}
$scope.sideLinks = allOb;
$scope.$apply(function () {
$scope.sideLinks = allOb;
});
}, 500);
};
$scope.onSideMenuChanges = function () {
var sidebar = document.getElementById('sidebar')
sidebar.classList.toggle("show")
}
$scope.screenWithEvent = function (event) {
if (window.innerWidth <= 500) {
if (document.body && !document.body.classList.contains('mobile'))
document.body.classList.add('mobile')
} else {
if (document.body && document.body.classList.contains('mobile'))
document.body.classList.remove('mobile')
}
}
window.addEventListener('resize', $scope.screenWithEvent)
$scope.activateSideMenu = function (item) {
if($scope.isMobileView) return;
console.log(item)
console.log(item.templateUrl.replace(/\W/g, "-"))
$scope.changeSideHeader(item.id)
}
$scope.openSearchDropDown = function (event) {
event.stopPropagation()
let menuSearch = document.getElementById("menuSearchDp")
if (!menuSearch.classList.contains("open"))
menuSearch.classList.add('open')
}
$scope.maxSpace = Math.max(...NgConfig.menus.map(x => x.menuName.length));
NgConfig.menus = NgConfig.menus.map(x => {
let curLen = ($scope.maxSpace + 5) - x.menuName.length;
for (let index = 0; index < curLen; index++)
x.menuName += " ";
return x;
})
$scope.encodeDecodeChar = ["!", "@", "#", "$", "%", "_", "+", "&", "+", "-"];
$scope.encodeDecodeNumber = 97;
$scope.encode = function (r) {
if (!(r && 0 < r.length)) return "";
for (var e, t = "", a = Math.floor(26 * Math.random()), n = a, o = 0; o < r.length; o++) {
e = r.charCodeAt(o), e += n;
var h = Math.floor(e / 26),
f = 1 < h ? h : "";
f && Math.ceil(10 * Math.random()) % 2 == 0 && f < 9 && (f = $scope.encodeDecodeChar[f]), n = e % 26, t += String.fromCharCode(n + $scope.encodeDecodeNumber) + f
}
return t.substr(0, Math.floor(t.length / 2)) + String.fromCharCode(a + $scope.encodeDecodeNumber) + t.substr(Math.floor(t.length / 2))
}
$scope.decode = function (r) {
var e, t = "",
a = 0;
if (!(r && 0 < r.length)) return "";
var n = r.charCodeAt(Math.floor((r.length - 1) / 2)) - $scope.encodeDecodeNumber;
r = r.substr(0, Math.floor((r.length - 1) / 2)) + r.substr(Math.floor((r.length - 1) / 2) + 1) + r[Math.floor((r.length - 1) / 2)];
for (var o = 0; o < r.length - 1; o++)
if (e = r.charCodeAt(o), $scope.encodeDecodeNumber - 1 < e && e < $scope.encodeDecodeNumber + 27) {
for (var h = a = 0; h < 100; h++) {
var f = r.charCodeAt(o + 1);
if ($scope.encodeDecodeNumber - 1 < f && f < $scope.encodeDecodeNumber + 27) {
0 === a && (a = 1);
break
}
if (o++, 47 < f && f < 58) a = parseInt(10 * a) + parseInt(String.fromCharCode(f));
else {
var l = $scope.encodeDecodeChar.indexOf(String.fromCharCode(f));
a = parseInt(10 * a) + parseInt(l)
}
}
var u = 26 * a + e - $scope.encodeDecodeNumber;
t += String.fromCharCode(u - n), n = u % 26
}
return t
}
}]);