Skip to content

Commit af76fce

Browse files
committed
Now allows the user to specify which controller to use for popups and tooltips in options-object
1 parent 8ee3ba2 commit af76fce

File tree

4 files changed

+57
-66
lines changed

4 files changed

+57
-66
lines changed

example-app/js/app.js

Lines changed: 3 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example-app/partials/annotation-tooltip.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<label for="comment">
33
Comment
44
</label>
5-
<p ng-if="$annotation.data.comment.length">{{$annotation.data.comment}}</p>
5+
<p>{{$annotation.data.comment}}</p>
66
<label for="score">
77
Score
88
</label>
9-
<p ng-if="$annotation.data.score">{{$annotation.data.score}}</p>
9+
<p>{{$annotation.data.score}}</p>
1010
</div>

example-app/partials/annotation.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div ng-controller="AnnotationController">
1+
<div>
22
<label for="comment">
33
Comment
44
</label>
@@ -13,7 +13,6 @@
1313
<select id="color" class="form-control" ng-model="$annotation.type" ng-options="c.value as c.name for c in annotationColours">
1414
<option value="">-- Choose color --</option>
1515
</select>
16-
1716
<div style="margin-top: 10px;">
1817
<button class="btn btn-success pull-right" ng-click="close()">Ok</button>
1918
<button class="btn" ng-click="reject()" ng-show="$isNew">Cancel</button>

js/src/ng-annotate.coffee

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ ngAnnotate.factory "NGAnnotation", ->
127127

128128
return Annotation
129129

130-
ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotation, NGAnnotatePopup, NGAnnotateTooltip)->
130+
ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, $controller, NGAnnotation, NGAnnotatePopup, NGAnnotateTooltip)->
131131
return {
132132
restrict: "A"
133133
scope:
@@ -143,7 +143,6 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio
143143
activePopups = []
144144
activeTooltips = []
145145

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

161160
# Setting options
162161
options =
162+
popupController: ""
163163
popupTemplateUrl: ""
164+
tooltipController: ""
164165
tooltipTemplateUrl: ""
165166
options = angular.extend options, $scope.options
166167

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

181-
getPopupTemplate = (url)->
182-
if popupTemplateData.length
183-
deferred = $q.defer()
184-
deferred.resolve popupTemplateData
185-
return deferred.promise
186-
187-
return $http.get(url).then (response)->
188-
popupTemplateData = response.data
189-
return response.data
190-
191-
getTooltipTemplate = (url)->
192-
if tooltipTemplateData.length
193-
deferred = $q.defer()
194-
deferred.resolve tooltipTemplateData
195-
return deferred.promise
182+
$http.get(options.popupTemplateUrl).then (response)->
183+
popupTemplateData = response.data
196184

197-
return $http.get(url).then (response)->
198-
tooltipTemplateData = response.data
199-
return response.data
185+
$http.get(options.tooltipTemplateUrl).then (response)->
186+
tooltipTemplateData = response.data
200187

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

259246
onSelect = (event)->
247+
if popupTemplateData.length is 0
248+
return
249+
260250
try
261251
annotation = createAnnotation()
262252
$scope.$apply()
@@ -273,6 +263,9 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio
273263
loadAnnotationPopup annotation, $span, true
274264

275265
onClick = (event)->
266+
if popupTemplateData.length is 0
267+
return
268+
276269
$target = angular.element event.target
277270
targetId = if (attrId = $target.attr("data-annotation-id"))? then parseInt(attrId, 10)
278271

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

294287
onMouseEnter = (event)->
288+
if tooltipTemplateData.length is 0
289+
return
290+
295291
event.stopPropagation()
296292
$target = angular.element event.target
297293
targetId = if (attrId = $target.attr("data-annotation-id"))? then parseInt(attrId, 10)
@@ -318,17 +314,27 @@ ngAnnotate.directive "ngAnnotate", ($rootScope, $compile, $http, $q, NGAnnotatio
318314

319315
activeTooltips.push tooltip
320316

321-
getTemplatePromise = getTooltipTemplate options.tooltipTemplateUrl
322-
getTemplatePromise.then (template)->
323-
$compile(angular.element(template)) tooltip.scope, ($content)->
324-
tooltip.$el.html $content
325-
tooltip.$el.appendTo "body"
326-
tooltip.positionTop()
327-
tooltip.positionLeft element.offset().left - tooltip.$el.innerWidth()
328-
tooltip.show()
317+
locals =
318+
$scope: tooltip.scope
319+
$template: tooltipTemplateData
320+
321+
tooltip.$el.html locals.$template
322+
tooltip.$el.appendTo "body"
323+
324+
if options.tooltipController
325+
controller = $controller options.tooltipController, locals
326+
tooltip.$el.data "$ngControllerController", controller
327+
tooltip.$el.children().data "$ngControllerController", controller
328+
329+
$compile(tooltip.$el) tooltip.scope
330+
tooltip.positionTop()
331+
tooltip.positionLeft element.offset().left - tooltip.$el.innerWidth()
332+
tooltip.scope.$apply()
333+
tooltip.show()
329334

330335
onMouseLeave = (event)->
331336
event.stopPropagation()
337+
332338
$target = angular.element event.target
333339
targetId = if (attrId = $target.attr("data-annotation-id"))? then parseInt(attrId, 10)
334340

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

369375
activePopups.push popup
370376

371-
getTemplatePromise = getPopupTemplate options.popupTemplateUrl
372-
getTemplatePromise.then (template)->
373-
$compile(angular.element(template)) popup.scope, ($content)->
374-
popup.$el.html $content
375-
popup.$el.appendTo "body"
376-
popup.positionTop()
377-
popup.positionLeft element.offset().left - popup.$el.innerWidth()
378-
popup.show()
377+
locals =
378+
$scope: popup.scope
379+
$template: popupTemplateData
380+
381+
popup.$el.html locals.$template
382+
popup.$el.appendTo "body"
383+
384+
if options.popupController
385+
controller = $controller options.popupController, locals
386+
popup.$el.data "$ngControllerController", controller
387+
popup.$el.children().data "$ngControllerController", controller
388+
389+
$compile(popup.$el) popup.scope
390+
popup.positionTop()
391+
popup.positionLeft element.offset().left - popup.$el.innerWidth()
392+
popup.scope.$apply()
393+
popup.show()
379394

380395
element.on "mouseover", "span", onMouseEnter
381396
element.on "mouseleave", "span", onMouseLeave

0 commit comments

Comments
 (0)