Skip to content

Commit

Permalink
defer loading Auth, pop out sidebar to log in
Browse files Browse the repository at this point in the history
fixes #23
  • Loading branch information
tilgovi committed Jul 6, 2012
1 parent de440e9 commit 5efc796
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
11 changes: 5 additions & 6 deletions h/js/src/hypothesis.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Hypothesis extends Annotator

# Plugin configuration
options:
Auth: {}
Heatmap: {}
Permissions:
showEditPermissionsCheckbox: false,
Expand Down Expand Up @@ -59,19 +58,18 @@ class Hypothesis extends Annotator

super

_initialize: =>
# Load plugins
for own name, opts of @options
if not @plugins[name]
this.addPlugin(name, opts)

this

_initialize: =>
# Set up interface elements
this._setupHeatmap()
@wrapper.append(@viewer.element, @editor.element, @heatmap.element)

@plugins.Auth.withToken (token) =>
@plugins.Permissions.setUser token.userId

this.subscribe 'beforeAnnotationCreated', (annotation) =>
annotation.created = annotation.updated = (new Date()).toString()
annotation.user = @plugins.Permissions.options.userId(
Expand Down Expand Up @@ -390,7 +388,8 @@ class Hypothesis extends Annotator

showEditor: (annotation) =>
unless @plugins.Permissions?.user
alert("Not logged in!")
@editor.hide();
this.show()
return

if not annotation.user?
Expand Down
26 changes: 22 additions & 4 deletions h/templates/app.pt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,29 @@
tal:repeat="src environment['underscore'].urls()"></script>
<script>
window.annotator = new Hypothesis(document.body, {
Auth: {
tokenUrl: '${request.route_url('token')}'
},
Heatmap: {}
Heatmap: {}
})

var setupAuth = function () {
window.annotator.addPlugin('Auth', {
tokenUrl: '${request.route_url('token')}'
})
window.annotator.plugins.Auth.withToken(function (token) {
window.annotator.plugins.Permissions.setUser(token.userId);
})
}

<tal:b tal:condition="request.user">setupAuth()</tal:b>

var loginSuccess = function (data) {
deform.processCallbacks();
var newForm = $('#' + this.id);
if (newForm.find('.alert-error').length == 0) {
$(newForm).detach().appendTo('#toolbar');
$('#header').remove()
setupAuth();
}
}
</script>
</div>
</metal:main>
4 changes: 4 additions & 0 deletions h/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def partial(self):
class login(FormView):
schema = LoginSchema(validator=login_validator)
buttons = ('log in',)
ajax_options = """{
success: loginSuccess,
type: 'POST'
}"""

def __call__(self):
if self.request.user:
Expand Down

0 comments on commit 5efc796

Please sign in to comment.