Skip to content

Commit

Permalink
merge Auth and App controllers, make forms work
Browse files Browse the repository at this point in the history
  • Loading branch information
tilgovi committed Dec 7, 2012
1 parent 10e426d commit 32de01f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
1 change: 1 addition & 0 deletions h/js/app.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ imports = [




configure = ($routeProvider, $locationProvider) -> configure = ($routeProvider, $locationProvider) ->
$locationProvider.html5mode = true
$routeProvider.when '/app/viewer', $routeProvider.when '/app/viewer',
controller: 'Viewer' controller: 'Viewer'
reloadOnSearch: false reloadOnSearch: false
Expand Down
95 changes: 49 additions & 46 deletions h/js/controllers.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,22 +1,61 @@
class App class App
this.$inject = ['$compile', '$http', '$location', '$scope', 'annotator'] this.$inject = [
constructor: ($compile, $http, $location, $scope, annotator) -> '$compile', '$http', '$location', '$scope',
'annotator', 'deform'
]
constructor: ($compile, $http, $location, $scope, annotator, deform) ->
{plugins} = annotator {plugins} = annotator


$location.path '/app/viewer' angular.extend $scope,
auth: null
forms: []


$scope.$on 'reset', => $scope.reset = =>
angular.extend $scope, angular.extend $scope,
auth: null
username: null
password: null
email: null
code: null
personas: [] personas: []
persona: null persona: null
token: null token: null


$scope.addForm = ($form, name) =>
$scope.forms[name] = $form

$scope.submit = ->
fields = switch $scope.auth
when 'login' then ['username', 'password']
when 'register' then ['username', 'password', 'email']
when 'forgot' then ['email']
when 'activate' then ['password', 'code']
params = ([key, $scope[key]] for key in fields when $scope[key]?)
params.push ['__formid__', $scope.auth]
data = (((p.map encodeURIComponent).join '=') for p in params).join '&'

$http.post '', data,
headers:
'Content-Type': 'application/x-www-form-urlencoded'
withCredentials: true
.success (data) =>
# Extend the scope with updated model data
angular.extend($scope, data.model) if data.model?

# Compile and link any forms which were re-rendered in this response
for oid of data.form
$form = angular.element data.form[oid]
if oid of $scope.forms
$scope.forms[oid].replaceWith $form
($compile $form) $scope
deform.focusFirstInput $form

$scope.$watch 'personas', (newValue, oldValue) => $scope.$watch 'personas', (newValue, oldValue) =>
if newValue?.length if newValue?.length
annotator.element.find('#persona') annotator.element.find('#persona')
.off('change').on('change', -> $(this).submit()) .off('change').on('change', -> $(this).submit())
.off('click') .off('click')
$scope.showAuth = false $scope.auth = null
else else
$scope.persona = null $scope.persona = null
$scope.token = null $scope.token = null
Expand Down Expand Up @@ -44,51 +83,16 @@ class App
plugins.Permissions.setUser(null) plugins.Permissions.setUser(null)
delete plugins.Auth delete plugins.Auth


$scope.$on 'showAuth', (event, show=true) =>
$scope.auth = if show then 'login' else null

# Fetch the initial model from the server # Fetch the initial model from the server
$scope.reset()
$http.get 'model', $http.get 'model',
withCredentials: true withCredentials: true
.success (data) => .success (data) =>
angular.extend $scope, data angular.extend $scope, data

$location.path '/app/viewer'
# Set the initial state
# Asynchronous so that other controllers get time to initialize
$scope.$evalAsync "$broadcast('reset')"


class Auth
this.$inject = ['$compile', '$element', '$http', '$scope', 'deform']
constructor: ($compile, $element, $http, $scope, deform) ->
$scope.submit = ->
controls = $element.find('.sheet .active form').formSerialize()
$http.post '', controls,
headers:
'Content-Type': 'application/x-www-form-urlencoded'
withCredentials: true
.success (data) =>
# Extend the scope with updated model data
angular.extend($scope, data.model) if data.model?

# Replace any forms which were re-rendered in this response
for oid of data.form
target = '#' + oid

$form = $(data.form[oid])
$form.replaceAll(target)

link = $compile $form
link $scope

deform.focusFirstInput target

$scope.$on 'reset', =>
angular.extend $scope,
auth: null
username: null
password: null
email: null
code: null

$scope.$on 'showAuth', => $scope.auth = 'login'




class Viewer class Viewer
Expand Down Expand Up @@ -125,5 +129,4 @@ class Viewer


angular.module('h.controllers', []) angular.module('h.controllers', [])
.controller('App', App) .controller('App', App)
.controller('Auth', Auth)
.controller('Viewer', Viewer) .controller('Viewer', Viewer)
3 changes: 3 additions & 0 deletions h/js/deform.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ deformDirective = ->


# Link function # Link function
(scope, iElement, iAttrs, controller) -> (scope, iElement, iAttrs, controller) ->
if scope.addForm?
name = iAttrs.name or iAttrs.ngModel or iAttrs.id
scope.addForm iElement, name
deform.processCallbacks() deform.processCallbacks()
restrict: 'C' restrict: 'C'
require: 'form' require: 'form'
Expand Down
1 change: 0 additions & 1 deletion h/templates/app.pt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
</div> </div>
<div id="gutter"> <div id="gutter">
<header data-ng-class="auth || 'collapsed'" <header data-ng-class="auth || 'collapsed'"
data-ng-controller="Auth"
data-ng-model="auth" data-ng-model="auth"
data-ng-submit="submit()" data-ng-submit="submit()"
data-tab-reveal="['forgot','activate']" data-tab-reveal="['forgot','activate']"
Expand Down

0 comments on commit 32de01f

Please sign in to comment.