-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathng-qtip2.coffee
180 lines (150 loc) · 4.87 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
str2bool = (str) -> String(str).toLowerCase() not in ['false', '0', 'null', '']
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]
NgQtip2 = ($timeout, $compile, $http, $templateCache, qtipDefaults, $q) ->
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: '@'
qtipOptions: '=?'
qtipApi: '=?'
object: '=qtipTemplateObject'
link: (scope, el, attrs) ->
# these link attrs to qtip props (el.qtip(prop, val))
watchProps =
qtipVisible: 'toggle'
qtipDisable: 'disable'
# these link attrs to qtip options (el.qtip('option', name, val))
watchOptions =
qtipTitle: 'content.title'
qtipClass: 'style.classes'
qtip: 'content.text'
scope.qtipOptions ?= {}
scope.apiPromise = $q.defer()
scope.closeQtip = (e, id = scope.getQtipId(), {rendered = yes} = {}) ->
e?.preventDefault?()
qtEl = ($ "#qtip-#{id}")
qtEl.qtip 'hide'
qtEl.qtip().rendered = scope.qtipPersistent ? rendered
return
# Generic methods
scope.getQtipId = ->
el.data('hasqtip')
scope.getQtipElement = (id = scope.getQtipId()) ->
($ "#qtip-#{id}")
# qTip API
scope.api = (e, id = scope.getQtipId()) ->
qtEl = ($ "#qtip-#{id}")
return qtEl.qtip "api"
scope.isApiReady = -> !!scope.getQtipElement().qtip().rendered
scope.qtipApi =
isReady: scope.isApiReady
api: scope.api
apiPromise: scope.apiPromise.promise
scope.resolveApiPromise = (event, api) ->
scope.apiPromise.resolve(api)
scope._before = (before, fn) -> ->
before?(arguments...)
fn?(arguments...)
# Main init method
generateQtip = (content) ->
# Default options
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
# Clear empty values to use defaults
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
# Merge final opts
options = angular.merge {}, qtipDefaults, attrOptions, scope.qtipOptions
# qTip API
if options.events?.render?
options.events.render = scope._before(scope.resolveApiPromise, options.events.render)
else
options.events ?= {}
options.events.render = scope.resolveApiPromise
# Create qtip
($ el).qtip options
# Assign watch props/options pairs
for k, v of watchProps
scope.$watch k, (newVal) ->
el.qtip v, newVal
for k, v of watchOptions
scope.$watch k, (newVal) ->
el.qtip 'option', v, newVal
# Switch for triggering init by different types of main content/position
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', '$q']
angular.module('ngQtip2', [])
.directive 'qtip', NgQtip2
.provider 'qtipDefaults', ->
@defaults = {}
@setDefaults = (defaults = {}) =>
angular.merge @defaults, defaults
@$get = => @defaults
return