Created
May 3, 2012 16:21
-
-
Save mandric/2586943 to your computer and use it in GitHub Desktop.
kanso session handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lib/events.js | |
// topnav ala bootstrap | |
// stores a reference to the modal dialog | |
var m; | |
session.on('change', function (userCtx) { | |
if (!$('#session_menu').length) { | |
$('#topnav .pull-right').append( | |
'<li id="session_menu" class="dropdown"><li>' | |
); | |
} | |
var el; | |
if (userCtx.name) { | |
el = $( | |
'<li id="session_menu" class="dropdown">' + | |
'<a class="dropdown-toggle">' + | |
'<i class="icon-user"></i> ' + userCtx.name + ' ' + | |
'<b class="icon-chevron-down"></b>' + | |
'</a>' + | |
'<ul class="dropdown-menu">' + | |
'<li><a class="logout" href="#">Logout</a></li>' + | |
'</ul>' + | |
'</li>' | |
); | |
$('.dropdown-toggle', el).click(handleDropdown); | |
$('.logout', el).click(function (ev) { | |
ev.preventDefault(); | |
session.logout(function(err, resp) { | |
location.href = dutils.getBaseURL(); | |
}); | |
return false; | |
}); | |
} | |
else { | |
el = $( | |
'<li id="session_menu">' + | |
'<a class="login" href="#">Login</a>' + | |
'</li>' | |
); | |
if (m) { | |
// clear previous modal dialog | |
m.modal('hide'); | |
m.data('modal', null); | |
m.remove(); | |
} | |
m = $(templates.render("login_modal.html", {userCtx: userCtx}, {})); | |
var submitHandler = function(ev) { | |
ev.preventDefault(); | |
var username = $('#id_username', m).val(); | |
var password = $('#id_password', m).val(); | |
$('.alert', m).remove(); | |
$('.help-inline', m).remove(); | |
$('.control-group', m).removeClass('error'); | |
$('.controls', m).show(); // sometimes these get hidden | |
var username_cg = $('#id_username').parents('.control-group'); | |
var password_cg = $('#id_password').parents('.control-group'); | |
var errors = false; | |
if (!username) { | |
username_cg.addClass('error'); | |
$('#id_username').after( | |
'<span class="help-inline">Required</span>' | |
); | |
errors = true; | |
} | |
if (!password) { | |
password_cg.addClass('error'); | |
$('#id_password').after( | |
'<span class="help-inline">Required</span>' | |
); | |
errors = true; | |
} | |
if (errors) { | |
return false; | |
} | |
session.login(username, password, function (err) { | |
if (err) { | |
var msg = err.toString(); | |
$('form', m).before( | |
'<div class="alert alert-error">' + msg + '</div>' | |
); | |
} | |
else { | |
m.modal('hide'); | |
location.href = dutils.getBaseURL(); | |
} | |
}); | |
return false; | |
}; | |
// this is broken for some reason, I suspect the bootstrap-modal | |
// plugin is doing preventDefault on events inside the modal | |
//$('form', m).submit(submitHandler); | |
// fake form submit event | |
$('input', m).keyup(function (ev) { | |
if (ev.keyCode === 13) { | |
return submitHandler.apply(this, arguments); | |
} | |
}); | |
$('.btn-primary', m).click(submitHandler); | |
$('.btn-close', m).click(function () { | |
m.modal('hide'); | |
}); | |
$('.login', el).click(function (ev) { | |
ev.preventDefault(); | |
m.modal('show'); | |
$('#id_username').focus(); | |
return false; | |
}); | |
} | |
$('#session_menu').replaceWith(el); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- templates/login_modal.html --> | |
<div class="modal" id="myModal"> | |
<div class="modal-header"> | |
<a class="close" data-dismiss="modal">×</a> | |
<h3>Login</h3> | |
</div> | |
<div class="modal-body"> | |
<form class="form-horizontal" id="login_form" action="" method="POST"> | |
<div class="control-group"> | |
<label class="control-label" for="id_username">Username</label> | |
<div class="controls"> | |
<input id="id_username" name="username" type="text" /> | |
</div> | |
</div> | |
<div class="control-group"> | |
<label class="control-label" for="id_password">Password</label> | |
<div class="controls"> | |
<input id="id_password" name="password" type="password" /> | |
</div> | |
</div> | |
</form> | |
</div> | |
<div class="modal-footer"> | |
<a href="#" class="btn btn-primary">Login</a> | |
<a href="#" class="btn btn-close">Close</a> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment