mirror of
https://github.com/rcon-web-admin/rcon-web-admin.git
synced 2026-12-05 09:18:11 +00:00
191 lines
5.9 KiB
JavaScript
191 lines
5.9 KiB
JavaScript
"use strict";
|
|
|
|
/**
|
|
* Simple view management
|
|
*/
|
|
var View = {};
|
|
|
|
/**
|
|
* The most recent loaded view
|
|
* @type {string}
|
|
*/
|
|
View.current = "";
|
|
|
|
/**
|
|
* All registeres views
|
|
* @type {object}
|
|
*/
|
|
View.views = {};
|
|
|
|
/**
|
|
* Register a view handler
|
|
* @param {string} name
|
|
* @param {function} handler
|
|
*/
|
|
View.register = function (name, handler) {
|
|
View.views[name] = handler;
|
|
};
|
|
|
|
/**
|
|
* Get view data by current hash
|
|
* @param {string=} hash
|
|
* @returns {{view: string, messageData: *}}
|
|
*/
|
|
View.getViewDataByHash = function (hash) {
|
|
var messageData = null;
|
|
var view = null;
|
|
hash = hash || window.location.hash;
|
|
if (hash) {
|
|
view = hash.substr(1);
|
|
var viewSplit = view.split("-");
|
|
view = viewSplit[0];
|
|
if (viewSplit[1]) {
|
|
messageData = JSON.parse(atob(viewSplit[1]));
|
|
}
|
|
}
|
|
return {"view": view, "messageData": messageData};
|
|
};
|
|
|
|
/**
|
|
* Change current hash in url, using pushState to be later able to detect back button
|
|
* @param {string} newHash
|
|
*/
|
|
View.changeHash = function (newHash) {
|
|
if (window.location.hash != "#" + newHash) {
|
|
history.pushState({hash: newHash}, null, window.location.href.replace(/\#.*/ig, "") + "#" + newHash);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Get json message string for use in html links
|
|
* @param {object} data
|
|
* @returns {string}
|
|
*/
|
|
View.getJsonMessage = function (data) {
|
|
return btoa(JSON.stringify(data));
|
|
};
|
|
|
|
/**
|
|
* Load given view
|
|
* @param {string} view
|
|
* @param {object=} messageData
|
|
* @param {function=} callback
|
|
*/
|
|
View.load = function (view, messageData, callback) {
|
|
var initialMessageData = messageData;
|
|
if (!messageData) messageData = {};
|
|
messageData.view = view;
|
|
var c = $("#content");
|
|
c.html('');
|
|
spinner(c);
|
|
Socket.send("view", messageData, function (viewData) {
|
|
if (viewData.note) {
|
|
note(viewData.note[0], viewData.note[1]);
|
|
}
|
|
if (viewData.error) {
|
|
var message = "Server Error: " + viewData.error.message;
|
|
if (viewData.error.stack) {
|
|
message = "<strong>Server Error</strong>\n" + viewData.error.stack;
|
|
}
|
|
$("#content").html($('<div class="alert alert-danger" style="white-space: pre-wrap"></div>').html(message));
|
|
if (callback) callback(null);
|
|
return;
|
|
}
|
|
var loadCallback = function (firstLoad) {
|
|
var hash = viewData.view;
|
|
// only change the hash if no form data has been sent back or if redirect is given
|
|
if (viewData.redirect) {
|
|
View.changeHash(viewData.redirect);
|
|
} else if (!viewData.form) {
|
|
if (!viewData.form && initialMessageData) {
|
|
hash = hash + "-" + View.getJsonMessage(initialMessageData);
|
|
}
|
|
View.changeHash(hash);
|
|
}
|
|
$.get("views/" + viewData.view + ".html", function (htmlData) {
|
|
View.current = viewData.view;
|
|
c.html(htmlData);
|
|
// set body classes for login and admin checks
|
|
var b = $("body");
|
|
b.removeClass("is-not-logged-in is-logged-in is-not-admin is-admin");
|
|
if (viewData.sessionUserData) {
|
|
b.addClass("is-logged-in");
|
|
if (viewData.sessionUserData.admin) {
|
|
b.addClass("is-admin");
|
|
} else {
|
|
b.addClass("is-not-admin");
|
|
}
|
|
} else {
|
|
b.addClass("is-not-logged-in is-not-admin");
|
|
}
|
|
// load view
|
|
View.views[viewData.view](viewData, firstLoad);
|
|
if (callback) callback(viewData);
|
|
// replace language keys
|
|
lang.replaceInHtml();
|
|
// init selectpicker
|
|
$('.selectpicker').selectpicker();
|
|
});
|
|
};
|
|
if (typeof View.views[viewData.view] == "undefined") {
|
|
$.getScript("views/" + viewData.view + ".js", function () {
|
|
loadCallback(true);
|
|
});
|
|
} else {
|
|
loadCallback(false);
|
|
}
|
|
});
|
|
};
|
|
|
|
// delegate events
|
|
$(document).on("click", ".page-link", function (ev) {
|
|
// onclick pagelink
|
|
ev.stopPropagation();
|
|
ev.preventDefault();
|
|
$(".hamburger.is-open").trigger("click");
|
|
var messageData = null;
|
|
var hash = $(this).attr("href").substr(1);
|
|
if ($(this).attr("data-message")) {
|
|
messageData = JSON.parse(atob($(this).attr("data-message")));
|
|
View.changeHash(hash + "-" + $(this).attr("data-message"));
|
|
} else {
|
|
View.changeHash(hash);
|
|
}
|
|
View.load(hash, messageData);
|
|
}).on("click", ".submit-form", function () {
|
|
// onclick form submit btn
|
|
var f = $(this).closest("form");
|
|
var name = f.attr("name");
|
|
if (f[0].checkValidity()) {
|
|
var data = {};
|
|
var formData = f.serializeArray();
|
|
for (var i in formData) {
|
|
data[formData[i].name] = formData[i].value;
|
|
}
|
|
var view = f.attr("data-view");
|
|
var messageData = {
|
|
"form": name,
|
|
"btn": $(this).attr("data-name"),
|
|
"formData": data
|
|
};
|
|
// if view not given, just use the current view
|
|
if (!view) {
|
|
var hashData = View.getViewDataByHash();
|
|
view = hashData.view;
|
|
if (hashData.messageData) {
|
|
$.extend(messageData, hashData.messageData);
|
|
}
|
|
}
|
|
// send data to view
|
|
View.load(view, messageData, function (viewData) {
|
|
// just filling data back into form if no redirect is going on
|
|
if (!viewData.redirect) {
|
|
populateForm($("#content").find("form").filter("[name='" + name + "']"), data);
|
|
}
|
|
});
|
|
} else {
|
|
// on validation error trigger a fake submit button to enable validation UI popup
|
|
$(this).after('<input type="submit">');
|
|
$(this).next().trigger("click").remove();
|
|
}
|
|
}); |