Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
refactor(macModal): Add mac-modal-close to handle closing modal to re…
Browse files Browse the repository at this point in the history
…move a function used by

 modal to close. Changed mac-modal directive to use the same parent scope.
  • Loading branch information
adrianlee44 committed Feb 4, 2014
1 parent 409fd44 commit 2992e35
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
38 changes: 19 additions & 19 deletions src/directives/modal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,26 @@ angular.module("Mac").directive("macModal", [
replace: true
transclude: true

# NOTE: Isolated scope to prevent adding `close` function to parent
# scope. Transcluded content is currently using parent scope.
scope:
open: "&macModalOpen"

# NOTE: As of AngularJS 1.2.2, transclude function is on the link
# function instead of compile
link: ($scope, element, attrs, controller, transclude) ->
# NOTE: Similiar to ngTransclude directive but the scope is
# parent scope
transclude $scope.$parent, (clone) ->
wrapper = angular.element(
transclude $scope, (clone) ->
angular.element(
element[0].getElementsByClassName "modal-content-wrapper"
)
wrapper.html ""
wrapper.append clone
).replaceWith clone

opts = util.extendAttributes "macModal", modalViews.defaults, attrs

if opts.overlayClose
element.on "click", ($event) ->
if angular.element($event.target).hasClass("modal-overlay")
$scope.$apply -> modal.hide()

registerModal = (id) ->
if id? and id
opts.callback = $scope.open
opts.callback = $parse(attrs.macModalOpen)($scope)
modal.register id, element, opts

$scope.close = ($event, force = false) ->
if force or (opts.overlayClose and
angular.element($event.target).hasClass("modal-overlay"))
modal.hide()

if attrs.id
registerModal attrs.id
else
Expand All @@ -65,7 +56,7 @@ angular.module("Mac").directive("macModal", [
# @param {String} mac-modal Modal ID to trigger
# @param {Expr} mac-modal-data Extra data to pass along
#
directive "macModal", [
directive("macModal", [
"$parse"
"modal"
($parse, modal) ->
Expand All @@ -84,4 +75,13 @@ directive "macModal", [
scope: modalScope
modalScope.$apply()
return
]).

directive "macModalClose", [
"modal"
(modal) ->
restrict: "A"
link: ($scope, element, attrs) ->
element.bind "click", ->
$scope.$apply -> modal.hide()
]
9 changes: 7 additions & 2 deletions src/services/modal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ angular.module("Mac").service("modal", [
opened: null

modalTemplate: """
<div ng-click="close($event)" class="modal-overlay hide">
<div class="modal-overlay hide">
<div class="modal">
<a ng-click="close($event, true)" class="close-modal"></a>
<a mac-modal-close class="close-modal"></a>
<div class="modal-content-wrapper"></div>
</div>
</div>
Expand Down Expand Up @@ -114,6 +114,11 @@ angular.module("Mac").service("modal", [
)
wrapper.html template

if options.overlayClose
element.bind "click", ($event) ->
if angular.element($event.target).hasClass("modal-overlay")
viewScope.$apply => @hide()

$animate.enter element, angular.element(document.body)
$compile(element) viewScope

Expand Down

0 comments on commit 2992e35

Please sign in to comment.