Skip to content

Commit

Permalink
Now allows the user to specify which controller to use for popups and…
Browse files Browse the repository at this point in the history
… tooltips in options-object
  • Loading branch information
rmilesson committed May 29, 2014
1 parent 8ee3ba2 commit af76fce
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 66 deletions.
29 changes: 3 additions & 26 deletions example-app/js/app.js

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

4 changes: 2 additions & 2 deletions example-app/partials/annotation-tooltip.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<label for="comment">
Comment
</label>
<p ng-if="$annotation.data.comment.length">{{$annotation.data.comment}}</p>
<p>{{$annotation.data.comment}}</p>
<label for="score">
Score
</label>
<p ng-if="$annotation.data.score">{{$annotation.data.score}}</p>
<p>{{$annotation.data.score}}</p>
</div>
3 changes: 1 addition & 2 deletions example-app/partials/annotation.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div ng-controller="AnnotationController">
<div>
<label for="comment">
Comment
</label>
Expand All @@ -13,7 +13,6 @@
<select id="color" class="form-control" ng-model="$annotation.type" ng-options="c.value as c.name for c in annotationColours">
<option value="">-- Choose color --</option>
</select>

<div style="margin-top: 10px;">
<button class="btn btn-success pull-right" ng-click="close()">Ok</button>
<button class="btn" ng-click="reject()" ng-show="$isNew">Cancel</button>
Expand Down
87 changes: 51 additions & 36 deletions js/src/ng-annotate.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ ngAnnotate.factory "NGAnnotation", ->

return Annotation

ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotation, NGAnnotatePopup, NGAnnotateTooltip)->
ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, $controller, NGAnnotation, NGAnnotatePopup, NGAnnotateTooltip)->
return {
restrict: "A"
scope:
Expand All @@ -143,7 +143,6 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio
activePopups = []
activeTooltips = []


# Cache the template when we fetch it
popupTemplateData = ""
tooltipTemplateData = ""
Expand All @@ -160,7 +159,9 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio

# Setting options
options =
popupController: ""
popupTemplateUrl: ""
tooltipController: ""
tooltipTemplateUrl: ""
options = angular.extend options, $scope.options

Expand All @@ -178,25 +179,11 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio
clearPopups()
clearTooltips()

getPopupTemplate = (url)->
if popupTemplateData.length
deferred = $q.defer()
deferred.resolve popupTemplateData
return deferred.promise

return $http.get(url).then (response)->
popupTemplateData = response.data
return response.data

getTooltipTemplate = (url)->
if tooltipTemplateData.length
deferred = $q.defer()
deferred.resolve tooltipTemplateData
return deferred.promise
$http.get(options.popupTemplateUrl).then (response)->
popupTemplateData = response.data

return $http.get(url).then (response)->
tooltipTemplateData = response.data
return response.data
$http.get(options.tooltipTemplateUrl).then (response)->
tooltipTemplateData = response.data

removeChildren = (annotation)->
for i in [annotation.children.length - 1..0] by -1
Expand Down Expand Up @@ -257,6 +244,9 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio
return annotation

onSelect = (event)->
if popupTemplateData.length is 0
return

try
annotation = createAnnotation()
$scope.$apply()
Expand All @@ -273,6 +263,9 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio
loadAnnotationPopup annotation, $span, true

onClick = (event)->
if popupTemplateData.length is 0
return

$target = angular.element event.target
targetId = if (attrId = $target.attr("data-annotation-id"))? then parseInt(attrId, 10)

Expand All @@ -292,6 +285,9 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio
loadAnnotationPopup annotation, $target, false

onMouseEnter = (event)->
if tooltipTemplateData.length is 0
return

event.stopPropagation()
$target = angular.element event.target
targetId = if (attrId = $target.attr("data-annotation-id"))? then parseInt(attrId, 10)
Expand All @@ -318,17 +314,27 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio

activeTooltips.push tooltip

getTemplatePromise = getTooltipTemplate options.tooltipTemplateUrl
getTemplatePromise.then (template)->
$compile(angular.element(template)) tooltip.scope, ($content)->
tooltip.$el.html $content
tooltip.$el.appendTo "body"
tooltip.positionTop()
tooltip.positionLeft element.offset().left - tooltip.$el.innerWidth()
tooltip.show()
locals =
$scope: tooltip.scope
$template: tooltipTemplateData

tooltip.$el.html locals.$template
tooltip.$el.appendTo "body"

if options.tooltipController
controller = $controller options.tooltipController, locals
tooltip.$el.data "$ngControllerController", controller
tooltip.$el.children().data "$ngControllerController", controller

$compile(tooltip.$el) tooltip.scope
tooltip.positionTop()
tooltip.positionLeft element.offset().left - tooltip.$el.innerWidth()
tooltip.scope.$apply()
tooltip.show()

onMouseLeave = (event)->
event.stopPropagation()

$target = angular.element event.target
targetId = if (attrId = $target.attr("data-annotation-id"))? then parseInt(attrId, 10)

Expand Down Expand Up @@ -368,14 +374,23 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio

activePopups.push popup

getTemplatePromise = getPopupTemplate options.popupTemplateUrl
getTemplatePromise.then (template)->
$compile(angular.element(template)) popup.scope, ($content)->
popup.$el.html $content
popup.$el.appendTo "body"
popup.positionTop()
popup.positionLeft element.offset().left - popup.$el.innerWidth()
popup.show()
locals =
$scope: popup.scope
$template: popupTemplateData

popup.$el.html locals.$template
popup.$el.appendTo "body"

if options.popupController
controller = $controller options.popupController, locals
popup.$el.data "$ngControllerController", controller
popup.$el.children().data "$ngControllerController", controller

$compile(popup.$el) popup.scope
popup.positionTop()
popup.positionLeft element.offset().left - popup.$el.innerWidth()
popup.scope.$apply()
popup.show()

element.on "mouseover", "span", onMouseEnter
element.on "mouseleave", "span", onMouseLeave
Expand Down

0 comments on commit af76fce

Please sign in to comment.