This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
bootstrap.coffee
341 lines (269 loc) · 8.31 KB
/
bootstrap.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
###
Bootstrap based tip implementation
###
class Tourist.Tip.Bootstrap extends Tourist.Tip.Base
initialize: (options) ->
defs =
showEffect: null
hideEffect: null
@options = _.extend(defs, options)
@tip = new Tourist.Tip.BootstrapTip(@options)
destroy: ->
@tip.destroy()
super()
# Show the tip
show: ->
if @options.showEffect
fn = Tourist.Tip.Bootstrap.effects[@options.showEffect]
fn.call(this, @tip, @tip.el)
else
@tip.show()
# Hide the tip
hide: ->
if @options.hideEffect
fn = Tourist.Tip.Bootstrap.effects[@options.hideEffect]
fn.call(this, @tip, @tip.el)
else
@tip.hide()
###
Private
###
# Overridden to get the bootstrap element
_getTipElement: ->
@tip.el
# Set the current target. Overridden to set the target on the tip.
#
# target - a jquery element that this flyout should point to.
# step - step object
#
# Return nothing
_setTarget: (target, step) ->
super(target, step)
@tip.setTarget(target)
# Jam the content into the tip's body. Also place the tip along side the
# target element.
_renderContent: (step, contentElement) ->
my = step.my or 'left center'
at = step.at or 'right center'
@tip.setContainer(step.container or $('body'))
@tip.setContent(contentElement)
@tip.setPosition(step.target or false, my, at)
# One can add more effects by hanging a function from this object, then using
# it in the tipOptions.hideEffect or showEffect. i.e.
#
# @s = new Tourist.Tip.Bootstrap
# model: m
# showEffect: 'slidein'
#
Tourist.Tip.Bootstrap.effects =
# Move tip away from target 80px, then slide it in.
slidein: (tip, element) ->
OFFSETS = top: 80, left: 80, right: -80, bottom: -80
# this is a 'Corner' object. Will give us a top, bottom, etc
side = tip.my.split(' ')[0]
side = side or 'top'
# figure out where to start the animation from
offset = OFFSETS[side]
# side must be top or left.
side = 'top' if side == 'bottom'
side = 'left' if side == 'right'
value = parseInt(element.css(side))
# stop the previous animation
element.stop()
# set initial position
css = {}
css[side] = value + offset
element.css(css)
element.show()
css[side] = value
# if they have jquery ui, then use a fancy easing. Otherwise, use a builtin.
easings = ['easeOutCubic', 'swing', 'linear']
for easing in easings
break if $.easing[easing]
element.animate(css, 300, easing)
null
###
Simple implementation of tooltip with bootstrap markup.
Almost entirely deals with positioning. Uses the similar method for
positioning as qtip2:
my: 'top center'
at: 'bottom center'
###
class Tourist.Tip.BootstrapTip
template: '''
<div class="popover tourist-popover">
<div class="arrow"></div>
<div class="popover-content"></div>
</div>
'''
FLIP_POSITION:
bottom: 'top'
top: 'bottom'
left: 'right'
right: 'left'
constructor: (options) ->
defs =
offset: 10
tipOffset: 10
@options = _.extend(defs, options)
@el = $($.parseHTML(@template))
@hide()
destroy: ->
@el.remove()
show: ->
@el.show().addClass('visible')
hide: ->
@el.hide().removeClass('visible')
setTarget: (@target) ->
@_setPosition(@target, @my, @at)
setPosition: (@target, @my, @at) ->
@_setPosition(@target, @my, @at)
setContainer: (container) ->
container.append(@el)
setContent: (content) ->
@_getContentElement().html(content)
###
Private
###
_getContentElement: ->
@el.find('.popover-content')
_getTipElement: ->
@el.find('.arrow')
# Sets the target and the relationship of the tip to the project.
#
# target - target node as a jquery element
# my - position of the tip e.g. 'top center'
# at - where to point to the target e.g. 'bottom center'
_setPosition: (target, my='left center', at='right center') ->
return unless target
[clas, shift] = my.split(' ')
originalDisplay = @el.css('display')
@el
.css({ top: 0, left: 0, margin: 0, display: 'block' })
.removeClass('top').removeClass('bottom')
.removeClass('left').removeClass('right')
.addClass(@FLIP_POSITION[clas])
return unless target
# unset any old tip positioning
tip = @_getTipElement().css
left: ''
right: ''
top: ''
bottom: ''
if shift != 'center'
tipOffset =
left: tip[0].offsetWidth/2
right: 0
top: tip[0].offsetHeight/2
bottom: 0
css = {}
css[shift] = tipOffset[shift] + @options.tipOffset
css[@FLIP_POSITION[shift]] = 'auto'
tip.css(css)
targetPosition = @_caculateTargetPosition(at, target)
tipPosition = @_caculateTipPosition(my, targetPosition)
position = @_adjustForArrow(my, tipPosition)
@el.css(position)
# reset the display so we dont inadvertantly show the tip
@el.css(display: originalDisplay)
# Figure out where we need to point to on the target element.
#
# myPosition - position string on the target. e.g. 'top left'
# target - target as a jquery element or an array of coords. i.e. [10,30]
#
# returns an object with top and left attrs
_caculateTargetPosition: (atPosition, target) ->
if Object.prototype.toString.call(target) == '[object Array]'
return {left: target[0], top: target[1]}
bounds = @_getTargetBounds(target)
pos = @_lookupPosition(atPosition, bounds.width, bounds.height)
return {
left: bounds.left + pos[0]
top: bounds.top + pos[1]
}
# Position the tip itself to be at the right place in relation to the
# targetPosition.
#
# myPosition - position string for the tip. e.g. 'top left'
# targetPosition - where to point to on the target element. e.g. {top: 20, left: 10}
#
# returns an object with top and left attrs
_caculateTipPosition: (myPosition, targetPosition) ->
width = @el[0].offsetWidth
height = @el[0].offsetHeight
pos = @_lookupPosition(myPosition, width, height)
return {
left: targetPosition.left - pos[0]
top: targetPosition.top - pos[1]
}
# Just adjust the tip position to make way for the arrow.
#
# myPosition - position string for the tip. e.g. 'top left'
# tipPosition - proper position for the whole tip. e.g. {top: 20, left: 10}
#
# returns an object with top and left attrs
_adjustForArrow: (myPosition, tipPosition) ->
[clas, shift] = myPosition.split(' ') # will be top, left, right, or bottom
tip = @_getTipElement()
width = tip[0].offsetWidth
height = tip[0].offsetHeight
position =
top: tipPosition.top
left: tipPosition.left
# adjust the main direction
switch clas
when 'top'
position.top += height+@options.offset
when 'bottom'
position.top -= height+@options.offset
when 'left'
position.left += width+@options.offset
when 'right'
position.left -= width+@options.offset
# shift the tip
switch shift
when 'left'
position.left -= width/2+@options.tipOffset
when 'right'
position.left += width/2+@options.tipOffset
when 'top'
position.top -= height/2+@options.tipOffset
when 'bottom'
position.top += height/2+@options.tipOffset
position
# Figure out how much to shift based on the position string
#
# position - position string like 'top left'
# width - width of the thing
# height - height of the thing
#
# returns a list: [left, top]
_lookupPosition: (position, width, height) ->
width2 = width/2
height2 = height/2
posLookup =
'top left': [0,0]
'left top': [0,0]
'top right': [width,0]
'right top': [width,0]
'bottom left': [0,height]
'left bottom': [0,height]
'bottom right': [width,height]
'right bottom': [width,height]
'top center': [width2,0]
'left center': [0,height2]
'right center': [width,height2]
'bottom center': [width2,height]
posLookup[position]
# Returns the boundaries of the target element
#
# target - a jquery element
_getTargetBounds: (target) ->
el = target[0]
if typeof el.getBoundingClientRect == 'function'
size = el.getBoundingClientRect()
else
size =
width: el.offsetWidth
height: el.offsetHeight
$.extend({}, size, target.offset())