/* * Copyright (c) 2012-2014 Kaarle Ritvanen * See LICENSE file for license details */ define( [ "aconf/dom", "aconf/error", "aconf/navigation", "aconf/path", "aconf/statusbar", "aconf/transaction", "aconf/type", "aconf/widget/inline", "jquery", "underscore", "aconf/layout/stacked", "aconf/layout/tabular", "domReady", "jquery-blockui" ], function( dom, formatError, navi, pth, statusBar, txnMgr, type, Inline, $, _ ) { return function() { $("#login").submit(function() { $.ajax("/login", { type: "POST", data: JSON.stringify({ username: $("#username").val(), password: $("#password").val() }) }).done(function(data, status, xhr) { txnMgr = txnMgr( xhr.getResponseHeader("X-AConf-Auth-Token"), xhr.getResponseHeader("X-AConf-Save-Required") == "1" ); var objPath; function renderObject(path, data) { if (path) objPath = path; return ( data ? $.Deferred().resolve(data) : txnMgr.query(objPath) ).done(function(data) { var layout = data.meta.widget; var name = pth.split(objPath).pop(); (layout ? require("aconf/layout/" + layout) : Inline).extend({ createEl: function() { return $("#content").empty(); }, wrap: function() { return this.el; }, requestData: function(value, meta) { return $.Deferred() .resolve(data, data.meta); } }).new( { get: function(name) { return objPath; }, status: function(name) { return null; }, match: function(filter) { return true; } }, name, {}, 0, true, false ); }).fail(function() { var comps = pth.split(objPath); comps.pop(); comps.unshift("/"); navi.setPath(pth.join.apply(undefined, comps)); }); }; $("#content").on("reload", function(event, txnValid) { statusBar.validationReady(txnValid); renderObject(); event.stopPropagation(); }); function render() { var path = navi.getPath(); function renderMenu( target, path, current, selectFirst ) { var def = $.Deferred(); txnMgr.query(path).done(function(data) { if (data.meta.type != "model") { def.reject(data); return; } var tnFields = []; var extraFields = false; _.each(data.meta.fields, function(field) { if (!field.visible) return; if (!type.isTreeNode(field)) extraFields = true; else if (data.data[field.name]) tnFields.push(field); }); if (!tnFields.length) { def.reject(data); return; } function addItem( path, ui_name, status, current ) { var el = $("
").text("Logged out")); }); }); statusBar.reset(); $("#content").empty(); $(window).bind("hashchange", render); navi.setPath("/"); }).fail(function() { statusBar.setError("Login failed", "login"); }); return false; }); $("#username").focus(); } } );