Module: Heatmap::Helper

Defined in:
lib/heatmap/helper.rb

Instance Method Summary collapse

Instance Method Details

#save_heatmap(options = {}) ⇒ Object



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

#show_heatmap(path) ⇒ Object



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
# File 'lib/heatmap/helper.rb', line 111

def show_heatmap(path)
  heatmap = HeatMap.where(path: path.to_s)
  @data_points = []
  @data_xpaths = []
  heatmap.each do |coordinate|
    @data_xpaths.push({xpath: coordinate.xpath, x: 0, y:0, value: 100})
  end
  html = ""
  js = "<script type=\"text/javascript\">\n  var heatmapInstance = h337.create({\ncontainer: document.querySelector('body'),\nradius: 40\n  });\n  var xpath = JSON.parse('\#{raw(@data_xpaths.to_json.html_safe)}');\n  var data_xpath = xpath.map(function(path){\nvar x_coord = getOffset(path.xpath).x+25;\nvar y_coord = getOffset(path.xpath).y+25;\ndelete path[\"xpath\"];\npath.x = parseInt(x_coord);\npath.y = parseInt(y_coord);\nreturn path;\n  });\n  heatmapInstance.addData(data_xpath);\n</script>\n"

  html += js
  html.respond_to?(:html_safe) ? html.html_safe : html
end