forked from pablof7z/chardin.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chardinjs.coffee
141 lines (111 loc) · 5.47 KB
/
chardinjs.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
(($, window) ->
# Define the plugin class
class chardinJs
constructor: (el) -> @$el = $(el)
start: ->
return false if @._overlay_visible()
@._add_overlay_layer()
@._show_element(el) for el in @$el.find('*[data-intro]')
@$el.trigger 'chardinJs:start'
toggle: () ->
if not @._overlay_visible()
@.start()
else
@.stop()
stop: () ->
@$el.find(".chardinjs-overlay").fadeOut -> @.remove()
@$el.find('.chardinjs-helper-layer').remove()
@$el.find('.chardinjs-show-element').removeClass('chardinjs-show-element')
if window.removeEventListener
window.removeEventListener "keydown", @_onKeyDown, true
#IE
else document.detachEvent "onkeydown", @_onKeyDown if document.detachEvent
@$el.trigger 'chardinJs:stop'
_overlay_visible: ->
@$el.find('.chardinjs-overlay').length != 0
_add_overlay_layer: () ->
return false if @._overlay_visible()
overlay_layer = document.createElement("div")
styleText = ""
overlay_layer.className = "chardinjs-overlay"
#check if the target element is body, we should calculate the size of overlay layer in a better way
if @$el.prop('tagName') is "BODY"
styleText += "top: 0;bottom: 0; left: 0;right: 0;position: fixed;"
overlay_layer.setAttribute "style", styleText
else
element_position = @._get_offset()
if element_position
styleText += "width: " + element_position.width + "px; height:" + element_position.height + "px; top:" + element_position.top + "px;left: " + element_position.left + "px;"
overlay_layer.setAttribute "style", styleText
@$el.get()[0].appendChild overlay_layer
overlay_layer.onclick = => @.stop()
setTimeout ->
styleText += "opacity: .8;"
overlay_layer.setAttribute "style", styleText
, 10
_get_position: (element) -> element.getAttribute('data-position') or 'bottom'
_place_tooltip: (element, tooltip_layer) ->
tooltip_layer_position = @._get_offset(tooltip_layer)
#reset the old style
tooltip_layer.style.top = null
tooltip_layer.style.right = null
tooltip_layer.style.bottom = null
tooltip_layer.style.left = null
switch @._get_position(element)
when "top", "bottom"
target_element_position = @._get_offset(element)
target_width = target_element_position.width
my_width = $(tooltip_layer).width()
tooltip_layer.style.left = "#{(target_width/2)-(tooltip_layer_position.width/2)}px"
when "left", "right"
target_element_position = @._get_offset(element)
target_height = target_element_position.height
my_height = $(tooltip_layer).height()
tooltip_layer.style.top = "#{(target_height/2)-(tooltip_layer_position.height/2)}px"
switch @._get_position(element)
when "left" then tooltip_layer.style.left = "-" + (tooltip_layer_position.width - 34) + "px"
when "right" then tooltip_layer.style.right = "-" + (tooltip_layer_position.width - 34) + "px"
when "bottom" then tooltip_layer.style.bottom = "-" + (tooltip_layer_position.height) + "px"
when "top" then tooltip_layer.style.top = "-" + (tooltip_layer_position.height) + "px"
_show_element: (element) ->
element_position = @._get_offset(element)
helper_layer = document.createElement("div")
tooltip_layer = document.createElement("div")
helper_layer.setAttribute "data-id", element.id if element.id
helper_layer.className = "chardinjs-helper-layer chardinjs-#{@._get_position(element)}"
helper_layer.setAttribute "style", "width: #{element_position.width}px; height:#{element_position.height}px; top:#{element_position.top}px; left: #{element_position.left}px;"
@$el.get()[0].appendChild helper_layer
tooltip_layer.className = "chardinjs-tooltip chardinjs-#{@._get_position(element)}"
tooltip_layer.innerHTML = "<div class='chardinjs-tooltiptext'>#{element.getAttribute('data-intro')}</div>"
helper_layer.appendChild tooltip_layer
@._place_tooltip element, tooltip_layer
element.className += " chardinjs-show-element"
current_element_position = ""
if element.currentStyle #IE
current_element_position = element.currentStyle["position"]
#Firefox
else current_element_position = document.defaultView.getComputedStyle(element, null).getPropertyValue("position") if document.defaultView and document.defaultView.getComputedStyle
current_element_position = current_element_position.toLowerCase()
element.className += " chardinjs-relative-position" if current_element_position isnt "absolute" and current_element_position isnt "relative"
_get_offset: (element) ->
element_position =
width: element.offsetWidth
height: element.offsetHeight
_x = 0
_y = 0
while element and not isNaN(element.offsetLeft) and not isNaN(element.offsetTop)
_x += element.offsetLeft
_y += element.offsetTop
element = element.offsetParent
element_position.top = _y
element_position.left = _x
element_position
$.fn.extend chardinJs: (option, args...) ->
$this = $(this[0])
data = $this.data('chardinJs')
if !data
$this.data 'chardinJs', (data = new chardinJs(this, option))
if typeof option == 'string'
data[option].apply(data, args)
data
) window.jQuery, window