-$.fn.populateSubmenu = function(header, items, translator) {
- var submenuHeader = $("<li/>").append($("<a/>", {text : header, href : "#"}));
- var submenu = $("<ul/>", {"class" : "submenu"});
-
- for (var item in items) {
- var info = translator(item, items);
- submenu.append($("<li/>").append(
- $("<a/>", {text : info.text,
- href : info.href, target : "_blank"})
- ));
- }
-
- this.append(submenuHeader.append(submenu));
- return this;
+$.fn.populateNavMenu = function() {
+ var root = $("<ul/>")
+ for (var i = 0; i < mctx.menu.length; i++) {
+ var item = mctx.menu[i];
+ var entry = $("<li/>").append(
+ $("<a/>", {text : item.text, href: item.href})
+ );
+ root.append(entry);
+ }
+ $(this).append(root);
+ return this;
+}
+
+/**
+* Performs a login attempt.
+* @returns The AJAX object of the login request */
+$.fn.login = function () {
+ var username = this.find("input[name='username']").val();
+ var password = this.find("input[name='pass']").val();
+ var out = this.find("#result");
+ var redirect = function () {
+ window.location.href = mctx.location;
+ };
+
+ out.removeAttr("class");
+ out.text("Logging in...");
+
+ return $.ajax({
+ url : mctx.api + "bind",
+ type : "POST",
+ data : {user: username, pass : password}
+ }).done(function (data) {
+ if (data.status < 0) {
+ mctx.has_control = false;
+ out.attr("class", "fail");
+ out.text("Login failed: " + data.description);
+ } else {
+ //todo: error check
+ mctx.has_control = true;
+ out.attr("class", "pass");
+ out.text("Login ok!");
+ setTimeout(redirect, 800);
+ }
+ }).fail(function (jqXHR) {
+ mctx.has_control = false;
+ out.attr("class", "fail");
+ out.text("Login request failed - connection issues.")
+ });