forked from chenasraf/ng-qtip2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-qtip2.coffee
140 lines (117 loc) · 3.85 KB
/
ng-qtip2.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
NgQtip2 = ($timeout, $compile, $http, $templateCache, qtipDefaults) ->
restrict: 'A'
scope:
qtipVisible: '=?'
qtipDisable: '=?'
qtipFixed: '=?'
qtipDelay: '=?'
qtipAdjustX: '@'
qtipAdjustY: '@'
qtipModalStyle: '=?'
qtipTipStyle: '=?'
qtipShowEffect: '=?'
qtipHideEffect: '=?'
qtipPersistent: '=?'
qtip: '@'
qtipTitle: '@'
qtipTarget: '@'
qtipContent: '@'
qtipSelector: '@'
qtipTemplate: '@'
qtipEvent: '@'
qtipEventOut: '@'
qtipHide: '=?'
qtipShow: '=?'
qtipClass: '@'
qtipMy: '@'
qtipAt: '@'
qtipDefaults: '=?'
qtipOptions: '=?'
object: '=qtipTemplateObject'
link: (scope, el, attrs) ->
scope.qtipOptions ?= {}
str2bool = (str) -> String(str).toLowerCase() not in ['false', '0', 'null', '']
scope.getQtipId = ->
el.data('hasqtip')
scope.getQtipElement = (id = scope.getQtipId()) ->
($ "#qtip-#{id}")
scope.closeQtip = (e, id = scope.getQtipId(), {rendered = yes} = {}) ->
e?.preventDefault?()
qtEl = ($ "#qtip-#{id}")
qtEl.qtip 'hide'
qtEl.qtip().rendered = scope.qtipPersistent ? rendered
return
removeEmpties = (obj, deep = true) ->
for k, v of obj
if v? and typeof v is 'object' and deep
removeEmpties obj[k], deep
else if not v?
delete obj[k]
generateQtip = (content) ->
attrOptions =
position:
my: scope.qtipMy
at: scope.qtipAt
target: if scope.qtipTarget? then ($ scope.qtipTarget)
adjust:
x: if scope.qtipAdjustX? then parseInt(scope.qtipAdjustX)
y: if scope.qtipAdjustY? then parseInt(scope.qtipAdjustY)
show:
effect: scope.qtipShowEffect
event: scope.qtipEvent
hide:
effect: scope.qtipHideEffect
fixed: str2bool scope.qtipFixed
event: scope.qtipEventOut
style:
classes: scope.qtipClass
modal: scope.qtipModalStyle
tip: scope.qtipTipStyle
content: if content? then content else text: scope.qtipContent ? scope.qtip
angular.merge attrOptions.hide, scope.qtipHide if scope.qtipHide?
angular.merge attrOptions.show, scope.qtipShow if scope.qtipShow?
removeEmpties options
removeEmpties attrOptions
removeEmpties scope.qtipOptions
options = angular.merge {}, qtipDefaults, attrOptions, scope.qtipOptions
($ el).qtip options
if attrs.qtipVisible?
scope.$watch 'qtipVisible', (newVal) ->
($ el).qtip 'toggle', newVal
if attrs.qtipDisable?
scope.$watch 'qtipDisable', (newVal) ->
($ el).qtip 'disable', newVal
if scope.qtipTitle?
scope.$watch 'qtipTitle', (newVal) ->
($ el).qtip 'option', 'content.title', newVal
scope.$watch 'qtip', (newVal, oldVal) ->
($ el).qtip 'option', 'content.text', newVal if newVal isnt oldVal
if attrs.qtipSelector
$timeout ->
generateQtip ($ scope.qtipSelector).html()
else if scope.qtipTemplate?
$http.get scope.qtipTemplate, cache: $templateCache
.then (html) ->
generateQtip text: ->
$timeout ->
scope.$apply ->
text = $compile(html.data)(scope)
return text
else if scope.qtipTitle?
generateQtip title: scope.qtipTitle, text: scope.qtip
else
content = scope.qtip ? scope.qtipContent
generateQtip text: ->
$timeout ->
scope.$apply ->
$compile("<div>#{content}</div>")(scope)
return
NgQtip2.$inject = ['$timeout', '$compile', '$http', '$templateCache', 'qtipDefaults']
angular.module('ngQtip2', [])
.directive 'qtip', NgQtip2
.provider 'qtipDefaults', ->
@defaults = {}
@setDefaults = (defaults = {}) =>
angular.merge @defaults, defaults
@$get = => @defaults
return