Skip to content

Commit

Permalink
clear saved status within checkboxHooks when checkbox directory is de…
Browse files Browse the repository at this point in the history
…stroyed
  • Loading branch information
Luxiyalu committed Dec 18, 2014
1 parent 17a3fbc commit 0e91d6f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
55 changes: 34 additions & 21 deletions angular-checkbox-set.coffee
@@ -1,44 +1,51 @@
module = angular.module 'luxiyalu.checkboxSet', []

# make sure each checkbox hook has a unique name, since
# they'll depend on this to identify their anchors
# make sure each checkbox has a unique name, since
# they'll depend on this to identify their parent/children
module.value 'checkboxHooks', {}

# if you don't want the check/uncheck status to remain
# after ng-view switch, clear this value to {}
# upon page entrances.
# after ng-view switch, clear the value checkboxes to {}
# upon entering pages.
module.directive 'checkbox', (checkboxHooks) ->
restrict: 'E'
scope:
hook: '='
hookedTo: '='
statusStoredIn: '='
template:
'<div class="checkbox-outer" ng-click="toggleCheck()">' +
'<div class="checkbox-inner" ng-show="statusStoredIn.checked"></div>' +
'</div>'
templateUrl: 'templates/checkbox.html'
link: ($scope, element, attr) ->
self = {}
hook = $scope.hook
hookedTo = $scope.hookedTo
statusObj = $scope.statusStoredIn
statusObj.checked = false

if hookedTo? # parentCheckboxHooks
pcb = checkboxHooks[hookedTo] ?= {}
# if my parent already exists, check its status, follow its status
# and broadcast my status to my potential children at the end
# (my scope toggleCheck hasn't been defined yet)
if checkboxHooks[hookedTo]?
pcb = checkboxHooks[hookedTo]
statusObj.checked = pcb.scope.statusStoredIn.checked
else
pcb = checkboxHooks[hookedTo] = {}

self.scope = $scope
self.parent = hookedTo
self.statusObj = statusObj

pcb.children ?= []
pcb.children.push
scope: $scope
parent: hookedTo
statusObj: statusObj
pcb.children.push(self)

if hook? # childrenCheckboxHooks
ccb = checkboxHooks[hook] ?= {}
ccb.scope ?= $scope
ccb.parent = hookedTo
if hook? # childrenCheckboxHooks, this is the object
self = checkboxHooks[hook] ?= {}
self.scope ?= $scope
self.parent = hookedTo

# as a parent, have an api to check if all its children is
# of a certain state. If they're all checked, check itself.
$scope.checkIfAll = (state) ->
result = ccb.children.every (e, i, a) ->
result = self.children.every (e, i, a) ->
e.statusObj.checked is state

$scope.toggleCheck = (specify, direction) ->
Expand All @@ -50,8 +57,8 @@ module.directive 'checkbox', (checkboxHooks) ->

# everything below me should update their status to my status
broadcastUpdate = ->
return if !ccb?.children?
for child in ccb.children
return if !self?.children?
for child in self.children
child.scope.toggleCheck(statusObj.checked, 'broadcast')

# everything above me:
Expand All @@ -65,3 +72,9 @@ module.directive 'checkbox', (checkboxHooks) ->
else
return if !pcb.scope.checkIfAll(true)
pcb.scope.toggleCheck(true, 'emit')

if hook?
# if I have children before I even initialised, broadcast my status down
$scope.toggleCheck(statusObj.checked, 'broadcast')
# recycle my position in checkboxHooks if I'm destroyed
$scope.$on '$destroy', -> delete checkboxHooks[hook]
44 changes: 28 additions & 16 deletions angular-checkbox-set.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e91d6f

Please sign in to comment.