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
|
# File 'lib/heatmap/helper.rb', line 6
def save_heatmap(options = {})
click = options[:click] || Heatmap::Rails.options[:click]
move = options[:move] || Heatmap::Rails.options[:move]
html = ""
js = "<script type=\"text/javascript\">\n$( document ).ready(function() {\n var move_array = [];\n document.querySelector('body').onmousemove = function(ev) {\nvar xpath_element = xpathstring(ev);\nvar pageCoords = { path: window.location.pathname, type: 'move', xpath: xpath_element };\nconsole.log(xpath_element);\nvar obj = move_array.find(function (obj) { return obj.xpath === xpath_element; });\nif (obj == null){\n move_array.push(pageCoords);\n}\nif (move_array.length >= parseInt(\#{move}))\n{\n var coordinates = move_array;\n sendRequest({'move_data': coordinates});\n move_array = [];\n}\n };\n var click_array = [];\n document.querySelector('body').onclick = function(ev) {\n\nvar xpath_element= xpathstring(ev);\nvar pageCoords = { path: window.location.pathname, type: 'click', xpath: xpath_element };\nclick_array.push(pageCoords);\nif (click_array.length >= parseInt(\#{click}))\n{\n var coordinates = click_array;\n sendRequest({'data': coordinates});\n click_array = [];\n}\n };\n function sendRequest(coordinates_data){\n$.ajax({\n method: \"POST\",\n url: '/points',\n data: coordinates_data,\n dataType: 'application/json'\n});\n }\n});\n\nfunction xpathstring(event) {\n var\n e = event.srcElement || event.originalTarget,\n path = xpath(e, '');;\n return path\n}\nfunction xpath(element, suffix) {\n var parent, child_index, node_name;\n parent = element.parentElement;\n if (parent) {\n node_name = element.nodeName.toLowerCase();\n child_index = nodeindex(element, parent.children) + 1;\n return xpath(parent, '/' + node_name + '[' + child_index + ']' + suffix);\n } else {\n return '//html[1]' + suffix;\n }\n}\nfunction nodeindex(element, array) {\n var i,\n found = -1,\n element_name = element.nodeName.toLowerCase(),\n matched\n ;\n\n for (i = 0; i != array.length; ++i) {\n matched = array[i];\n if (matched.nodeName.toLowerCase() === element_name) {\n ++found;\n\n\n if (matched === element) {\n return found;\n }\n }\n }\n\n return -1;\n}\nfunction getOffset( path ) {\nel = document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\nvar _x = 0;\nvar _y = 0;\nwhile( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {\n _x += el.offsetLeft - el.scrollLeft;\n _y += el.offsetTop - el.scrollTop;\n el = el.offsetParent;\n}\nreturn { y: _y, x: _x };\n}\n </script>\n"
html += js
html.respond_to?(:html_safe) ? html.html_safe : html
end
|