ajax_canvas_field 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9255a0fcb8af907db44abb00ed37621d5d2b18b
4
- data.tar.gz: '0081b9fb765eeba33e525a7b3334f19323f2288a'
3
+ metadata.gz: cf10d08ef3a76a3276c6a2cba8a371d451a7d7bc
4
+ data.tar.gz: c28991ea6197847f70f6b13bc60585027944402b
5
5
  SHA512:
6
- metadata.gz: c0dffa02118ff8665d65f5be6594e77abf22c1f375e37dc6f382614cbdb194a7cf58eeada06a53dc385de44e5328ff5a39bce92b92e2354c060c32a8d63d5813
7
- data.tar.gz: 15273d04a079fa0196988b04accc3e11c4ca98eb805e74e5108bbdc84146ff35285bf711041e210563e0646eb523aa6815942ae4597c581ac3820ab71594ca07
6
+ metadata.gz: 0dbeab8a431220af99fa794f8b8e6c4d043ea9620fb88484342382dbd4d8efd93f0eb525a3fe34149c9f909c1995b4f0511eac0ef72d771a9c6218fc416d0e05
7
+ data.tar.gz: a5cc0f4448b335e735d1769bb0580aae732d8e7a3bf5c7cd23f99afc9e67b7f91833d888489dd5959f6660f3b8046a4534edb00899ff985425dc7495ad7fe488
@@ -1,198 +1,259 @@
1
- document.addEventListener('DOMContentLoaded', function(){
2
- var canvas = document.getElementById('canvas_field');
1
+ function refreshAll(canvas, context, points, e) {
2
+ points = [];
3
+ initCircles(canvas, context, points);
4
+ }
5
+
6
+ function initCircles(canvas, context, points) {
7
+ var initial_data = JSON.parse(find_data_field(canvas).dataset.initialData);
8
+ for (i = 0; i < initial_data.length; i++) {
9
+ var point = buildCircle(initial_data[i][1], initial_data[i][2], initial_data[i][3], canvas);
10
+ point.database_id = initial_data[i][0];
11
+ points.push(point);
12
+ }
13
+ redrawAllCircles(canvas, context, points);
14
+ return points;
15
+ }
16
+
17
+ function isPixelCollision(canvas, e) {
18
+ e.preventDefault();
19
+ if (e.type == "contextmenu")
20
+ return;
21
+ var r = canvas.getBoundingClientRect(),
22
+ context = canvas.getContext('2d'),
23
+ points = [],
24
+ x = e.clientX - r.left,
25
+ y = e.clientY - r.top,
26
+ removed = false,
27
+ i;
28
+
29
+ points = initCircles(canvas, context, points);
30
+ for (i = points.length - 1; i >= 0; --i) {
31
+ if (context.isPointInPath(points[i], x, y, 'nonzero')) {
32
+ deleteAjax(i, canvas, context, points);
33
+ removed = true;
34
+ }
35
+ }
36
+
37
+ if (removed == false) {
38
+ createAjax(x, y, e, canvas, context, points);
39
+ }
40
+ }
41
+
42
+ function buildCircle(x, y, e, canvas) {
43
+ var c = new Path2D(),
44
+ offset = 3.5;
45
+
46
+ c.arc(x - offset, y - offset, 7, 0, Math.PI * 2);
47
+ c.color = (typeof e === 'string')
48
+ ? e
49
+ : chooseColor(e, canvas);
50
+ c.x = x;
51
+ c.y = y;
52
+ return c;
53
+ }
54
+
55
+ function chooseColor(e, canvas) {
56
+ if (e === null)
57
+ return '#ff0000';
58
+ switch (e.which) {
59
+ case 1:
60
+ return canvas.dataset.leftColor;
61
+ case 2:
62
+ return canvas.dataset.middleColor;
63
+ case 3:
64
+ return canvas.dataset.rightColor;
65
+ }
66
+ }
67
+
68
+ function chooseButton(e) {
69
+ if (e === null)
70
+ return null;
71
+ switch (e.which) {
72
+ case 1:
73
+ return 'left';
74
+ case 2:
75
+ return 'middle';
76
+ case 3:
77
+ return 'right';
78
+ }
79
+ }
80
+
81
+ function deleteAjax(i, canvas, context, points) {
82
+ var id = points[i].database_id;
83
+ $.ajax({
84
+ type: 'DELETE',
85
+ url: canvas.dataset.url + '/' + id,
86
+ headers: {
87
+ 'Authorization': 'Token token=' + canvas.dataset.token
88
+ },
89
+ dataType: "json",
90
+ success: function(data) {
91
+ removeCircle(i, data, canvas, context, points);
92
+ },
93
+ error: function(data) {
94
+ alert('No connection to Server');
95
+ }
96
+ });
97
+ }
98
+
99
+ function removeCircle(i, data, canvas, context, points) {
100
+ points.splice(i, 1);
101
+ redrawAllCircles(canvas, context, points);
102
+ }
103
+
104
+ function createAjax(x, y, e, canvas, context, points) {
105
+ post_data = collectPostData(x, y, e, canvas, context, points);
106
+ $.ajax({
107
+ type: 'POST',
108
+ url: canvas.dataset.url,
109
+ headers: {
110
+ 'Authorization': 'Token token=' + canvas.dataset.token
111
+ },
112
+ data: post_data,
113
+ dataType: "json",
114
+ success: function(data) {
115
+ addCircle(x, y, data, e, canvas, context, points);
116
+ },
117
+ error: function(data) {
118
+ alert('No connection to Server');
119
+ }
120
+ });
121
+ }
122
+
123
+ function collectPostData(x, y, e, canvas, context, points) {
124
+ var params = {};
125
+ var param = canvas.dataset.strongParam;
126
+
127
+ var additional_data = JSON.parse(find_data_field(canvas).dataset.additionalData);
128
+ params[param] = {
129
+ x_value: x,
130
+ y_value: y,
131
+ button: chooseButton(e)
132
+ };
133
+ for (var attrname in additional_data) {
134
+ params[param][attrname] = additional_data[attrname];
135
+ }
136
+ return params;
137
+ }
138
+
139
+ function find_data_field(canvas) {
140
+ var active_datafields = document.getElementsByClassName('active canvas_data_field');
141
+ var fields = document.querySelectorAll('.canvas_field, .ro_canvas_field');
142
+ if (active_datafields.length == 1)
143
+ return active_datafields[0];
144
+ if (fields.length == 1)
145
+ return active_datafields[0];
146
+ var active_id_fields = document.querySelectorAll('.canvas_data_field.active[data-for=' + canvas.id + ']');
147
+ if (active_id_fields.length == 1)
148
+ return active_id_fields[0];
149
+ return active_datafields[0];
150
+ }
151
+
152
+ function addCircle(x, y, data, e, canvas, context, points) {
153
+ new_point = buildCircle(x, y, e, canvas);
154
+ new_point['database_id'] = data.id;
155
+ points.push(new_point);
156
+ redrawAllCircles(canvas, context, points);
157
+ }
158
+
159
+ function redrawAllCircles(canvas, context, points) {
160
+ var tmpData = [];
161
+ context.clearRect(0, 0, canvas.width, canvas.height);
162
+ for (i = 0; i < points.length; i++) {
163
+ context.fillStyle = points[i].color;
164
+ context.fill(points[i], 'nonzero');
165
+ context.stroke(points[i], 'nonzero');
166
+
167
+ tmpData.push([
168
+ points[i].database_id,
169
+ points[i].x,
170
+ points[i].y,
171
+ points[i].color
172
+ ]);
173
+ }
174
+ find_data_field(canvas).dataset.initialData = JSON.stringify(tmpData);
175
+ }
176
+
177
+ function handle_data_table(data_fields, canvas, context, points) {
178
+ var tableRows = document.getElementsByClassName('request_row');
179
+ var markRequestRow = function() {
180
+ for (var i = 0; i < tableRows.length; i++) {
181
+ tableRows[i].classList.remove('active');
182
+ }
183
+ for (var i = 0; i < data_fields.length; i++) {
184
+ data_fields[i].classList.remove('active');
185
+ }
186
+ this.classList.add('active');
187
+ this.getElementsByClassName('canvas_data_field')[0].classList.add('active');
188
+ refreshAll(canvas, context, points, event);
189
+ };
190
+
191
+ for (var i = 0; i < tableRows.length; i++) {
192
+ tableRows[i].addEventListener('click', markRequestRow, false);
193
+ }
194
+ }
195
+
196
+ var start_with_canvas_fields = function() {
197
+ var canvas_fields = document.getElementsByClassName('canvas_field');
198
+ var ro_canvas_fields = document.getElementsByClassName('ro_canvas_field');
3
199
  var data_fields = document.getElementsByClassName('canvas_data_field');
4
- if (canvas) {
5
- if (data_fields.length == 0) {
6
- console.log('No Data Field found. Please add canvas_data_field in your code!');
7
- document.getElementsByClassName('canvas_table')[0].style.display = 'none';
8
- canvas.style.display = 'none';
9
- } else {
10
- canvas.width = canvas.dataset.width;
11
- canvas.height = canvas.dataset.height;
12
- var context = canvas.getContext('2d'),
13
- url = canvas.dataset.url,
14
- param = canvas.dataset.strongParam,
15
- token = canvas.dataset.token,
16
- points = [];
17
-
18
- function refreshAll(e) {
19
- points = [];
20
- initCircles();
21
- }
22
-
23
- function initCircles() {
24
- var data_field = document.getElementsByClassName('active canvas_data_field')[0].dataset;
25
- var initialData = JSON.parse(data_field.initialData);
26
- for (i = 0; i < initialData.length; i++) {
27
- var point = buildCircle(initialData[i][1], initialData[i][2], initialData[i][3]);
28
- point.database_id = initialData[i][0];
29
- points.push(point);
30
- }
31
- redrawAllCircles();
32
- }
33
-
34
- function isPixelCollision(e) {
35
- e.preventDefault();
36
-
37
- if (e.type == "contextmenu") return;
38
-
39
- var r = canvas.getBoundingClientRect(),
40
- x = e.clientX - r.left,
41
- y = e.clientY - r.top,
42
- removed = false,
43
- i;
44
-
45
- for (i = points.length - 1; i >= 0; --i) {
46
- if (context.isPointInPath(points[i], x, y, 'nonzero')) {
47
- deleteAjax(i);
48
- removed = true;
49
- }
50
- }
51
-
52
- if (removed == false) {
53
- createAjax(x, y, e);
54
- }
55
- }
56
-
57
- function buildCircle(x, y, e) {
58
- var c = new Path2D(),
59
- offset = 3.5;
60
-
61
- c.arc(x - offset, y - offset, 7, 0, Math.PI * 2);
62
- c.color = (typeof e === 'string') ? e : chooseColor(e)
63
- c.x = x;
64
- c.y = y;
65
- return c;
66
- }
67
-
68
- function chooseColor(e) {
69
- if (e === null) return '#ff0000';
70
- switch (e.which) {
71
- case 1:
72
- return canvas.dataset.leftColor;
73
- case 2:
74
- return canvas.dataset.middleColor;
75
- case 3:
76
- return canvas.dataset.rightColor;
77
- }
78
- }
79
-
80
- function chooseButton(e) {
81
- if (e === null) return null;
82
- switch (e.which) {
83
- case 1:
84
- return 'left';
85
- case 2:
86
- return 'middle';
87
- case 3:
88
- return 'right';
89
- }
90
- }
91
-
92
- function deleteAjax(i) {
93
- var id = points[i].database_id;
94
- $.ajax({
95
- type: 'DELETE',
96
- url: url + '/' + id,
97
- headers: {
98
- 'Authorization': 'Token token=' + token
99
- },
100
- dataType: "json",
101
- success: function(data) {
102
- removeCircle(i, data);
103
- },
104
- error: function(data) {
105
- alert('No connection to Server');
106
- }
107
- });
108
- }
109
-
110
- function removeCircle(i, data) {
111
- points.splice(i, 1);
112
- redrawAllCircles();
113
- }
114
-
115
- function createAjax(x, y, e) {
116
- post_data = collectPostData(x, y, e);
117
- $.ajax({
118
- type: 'POST',
119
- url: url,
120
- headers: {
121
- 'Authorization': 'Token token=' + token
122
- },
123
- data: post_data,
124
- dataType: "json",
125
- success: function(data) {
126
- addCircle(x, y, data, e);
127
- },
128
- error: function(data) {
129
- alert('No connection to Server');
130
- }
131
- });
132
- }
133
-
134
- function collectPostData(x, y, e) {
135
- var params = {};
136
- var data_field = document.getElementsByClassName('active canvas_data_field')[0].dataset;
137
- var additionalData = JSON.parse(data_field.additionalData);
138
- params[param] = {
139
- x_value: x,
140
- y_value: y,
141
- button: chooseButton(e)
142
- };
143
- for (var attrname in additionalData) {
144
- params[param][attrname] = additionalData[attrname];
145
- }
146
- return params;
147
- }
148
-
149
- function addCircle(x, y, data, e) {
150
- new_point = buildCircle(x, y, e);
151
- new_point['database_id'] = data.id;
152
- points.push(new_point);
153
- redrawAllCircles();
154
- }
155
-
156
- function redrawAllCircles() {
157
- var tmpData = [];
158
- context.clearRect(0, 0, canvas.width, canvas.height);
159
- for (i = 0; i < points.length; i++) {
160
- context.fillStyle = points[i].color;
161
- context.fill(points[i], 'nonzero');
162
- context.stroke(points[i], 'nonzero');
163
-
164
- tmpData.push([points[i].database_id, points[i].x, points[i].y, points[i].color]);
165
- }
166
- document.getElementsByClassName('active canvas_data_field')[0].dataset.initialData = JSON.stringify(tmpData);
167
- }
168
-
169
- if (canvas.dataset.leftActive == 'true') {
170
- canvas.addEventListener('click', isPixelCollision, false);
171
- }
172
- if (canvas.dataset.middleActive == 'true') {
173
- canvas.addEventListener('auxclick', isPixelCollision, false);
174
- }
175
- if (canvas.dataset.rightActive == 'true') {
176
- canvas.addEventListener('contextmenu', isPixelCollision, false);
177
- }
178
- initCircles();
179
-
180
- var tableRows = document.getElementsByClassName('request_row');
181
- var markRequestRow = function() {
182
- for (var i = 0; i < tableRows.length; i++) {
183
- tableRows[i].classList.remove('active');
184
- }
185
- for (var i = 0; i < data_fields.length; i++) {
186
- data_fields[i].classList.remove('active');
187
- }
188
- this.classList.add('active');
189
- this.getElementsByClassName('canvas_data_field')[0].classList.add('active');
190
- refreshAll();
191
- };
192
-
193
- for (var i = 0; i < tableRows.length; i++) {
194
- tableRows[i].addEventListener('click', markRequestRow, false);
195
- }
200
+ if (canvas_fields.length == 0 && ro_canvas_fields.length == 0) {
201
+ return;
202
+ }
203
+ if (data_fields.length == 0) {
204
+ console.log('No Data Field found. Please add canvas_data_field in your code!');
205
+ for (o = 0; o < canvas_table.length; o++) {
206
+ document.getElementsByClassName('canvas_table')[o].style.display = 'none';
207
+ }
208
+ for (o = 0; o < canvas_fields.length; o++) {
209
+ document.getElementsByClassName('canvas_fields')[o].style.display = 'none';
196
210
  }
211
+ for (o = 0; o < ro_canvas_fields.length; o++) {
212
+ document.getElementsByClassName('ro_canvas_fields')[o].style.display = 'none';
213
+ }
214
+ return;
197
215
  }
198
- }, false);
216
+
217
+ for (o = 0; o < canvas_fields.length; o++) {
218
+ var canvas = canvas_fields[o];
219
+ canvas.width = canvas.dataset.width;
220
+ canvas.height = canvas.dataset.height;
221
+ var context = canvas.getContext('2d');
222
+ var points = [];
223
+ if (canvas.dataset.leftActive == 'true') {
224
+ canvas.addEventListener('click', function() {
225
+ isPixelCollision(this, event)
226
+ }, false);
227
+ }
228
+ if (canvas.dataset.middleActive == 'true') {
229
+ canvas.addEventListener('auxclick', function() {
230
+ isPixelCollision(this, event)
231
+ }, false);
232
+ }
233
+ if (canvas.dataset.rightActive == 'true') {
234
+ canvas.addEventListener('contextmenu', function() {
235
+ isPixelCollision(this, event)
236
+ }, false);
237
+ }
238
+ initCircles(canvas, context, points);
239
+
240
+ handle_data_table(data_fields, canvas, context, points);
241
+ }
242
+
243
+ for (o = 0; o < ro_canvas_fields.length; o++) {
244
+ var canvas = ro_canvas_fields[o];
245
+ canvas.width = canvas.dataset.width;
246
+ canvas.height = canvas.dataset.height;
247
+ var context = canvas.getContext('2d');
248
+ var points = [];
249
+ initCircles(canvas, context, points);
250
+
251
+ handle_data_table(data_fields, canvas, context, points);
252
+ }
253
+ };
254
+
255
+ if (document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) {
256
+ start_with_canvas_fields();
257
+ } else {
258
+ document.addEventListener("DOMContentLoaded", start_with_canvas_fields);
259
+ }
@@ -1,8 +1,4 @@
1
- #canvas_field {
2
- border: 1px groove lightgrey;
3
- }
4
-
5
- .canvas_field {
1
+ .canvas_field, .ro_canvas_field {
6
2
  border: 1px groove lightgrey;
7
3
  }
8
4
 
@@ -10,15 +10,15 @@ module CanvasFieldHelper
10
10
  background_url = options[:background_url].blank? ? '' : "url(#{options[:background_url]})"
11
11
  background = "background: #fff #{background_url} no-repeat center top"
12
12
  failure_message = 'Your browser does not support the canvas element.'
13
-
14
- content_tag :canvas, failure_message, id: :canvas_field, data: collect_data(url, options), style: background
13
+ additional_classes = options[:class] || ''
14
+ content_tag :canvas, failure_message, class: "#{additional_classes} canvas_field", id: options[:id],
15
+ data: collect_data(url, options), style: background
15
16
  end
16
17
 
17
18
  def canvas_data_field(active = false, options = {})
18
19
  klass = active ? 'active' : ''
19
- data = { additional_data: options[:additional_data].to_json,
20
- initial_data: options[:initial_data].to_json,
21
- content: options[:content] || '' }
20
+ data = { additional_data: options[:additional_data].to_json, initial_data: options[:initial_data].to_json,
21
+ content: options[:content] || '', for: options[:for] }
22
22
  content_tag(:span, '', class: "#{klass} canvas_data_field", data: data)
23
23
  end
24
24
 
@@ -43,8 +43,10 @@ module CanvasFieldHelper
43
43
  background += "; background-size: #{data[:width]}px #{data[:height]}px"
44
44
  end
45
45
  failure_message = 'Your browser does not support the canvas element.'
46
+ additional_classes = options[:class] || ''
46
47
 
47
- content_tag :canvas, failure_message, data: data, style: background, class: 'canvas_field'
48
+ content_tag :canvas, failure_message, class: "#{additional_classes} ro_canvas_field",
49
+ id: options[:id], data: data, style: background
48
50
  end
49
51
 
50
52
  private
@@ -22,7 +22,7 @@ module AjaxCanvasField
22
22
  # Configure through yaml file
23
23
  def self.configure_with(path_to_yaml_file)
24
24
  begin
25
- config = YAML.safe_load(IO.read(path_to_yaml_file))
25
+ config = YAML.load(IO.read(path_to_yaml_file))
26
26
  rescue Errno::ENOENT
27
27
  log(:warning, "YAML configuration file couldn't be found. Using defaults."); return
28
28
  rescue Psych::SyntaxError
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AjaxCanvasField #:nodoc:
2
4
  module Rails #:nodoc:
3
5
  class Engine < ::Rails::Engine
4
6
  initializer 'ajax_canvas_field.assets.precompile' do |app|
5
- %w(stylesheets javascripts images).each do |sub|
7
+ %w[stylesheets javascripts images].each do |sub|
6
8
  app.config.assets.paths << root.join('app/assets', sub).to_s
7
9
  end
8
- app.config.assets.precompile += %w( mouse_left.png mouse_right.png mouse_middle.png )
10
+ app.config.assets.precompile += %w[mouse_left.png mouse_right.png mouse_middle.png]
9
11
  end
10
12
  end
11
13
  end
@@ -1,5 +1,5 @@
1
1
  module AjaxCanvasField #:nodoc:
2
2
  module Rails #:nodoc:
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ajax_canvas_field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - datyv
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-23 00:00:00.000000000 Z
11
+ date: 2018-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,6 @@ files:
53
53
  - app/assets/images/mouse_middle.png
54
54
  - app/assets/images/mouse_right.png
55
55
  - app/assets/javascripts/ajax_canvas_field.js
56
- - app/assets/javascripts/ro_canvas_field.js
57
56
  - app/assets/stylesheets/ajax_canvas_field.css
58
57
  - app/helpers/canvas_field_helper.rb
59
58
  - app/views/ajax_canvas_field/_canvas_legend.html.haml
@@ -86,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
85
  version: '0'
87
86
  requirements: []
88
87
  rubyforge_project:
89
- rubygems_version: 2.5.2
88
+ rubygems_version: 2.6.13
90
89
  signing_key:
91
90
  specification_version: 4
92
91
  summary: HTML5 CanvasField Support
@@ -1,42 +0,0 @@
1
- var canvases = document.getElementsByClassName('canvas_field');
2
- if (canvases) {
3
- for (o = 0; o < canvases.length; o++) {
4
- var canvas = canvases[o];
5
- canvas.width = canvas.dataset.width;
6
- canvas.height = canvas.dataset.height;
7
- var context = canvas.getContext('2d');
8
- var points = [];
9
-
10
- function initCircles() {
11
- var initialData = JSON.parse(canvas.dataset.initialData);
12
- for (i = 0; i < initialData.length; i++) {
13
- var point = buildCircle(initialData[i][1], initialData[i][2], initialData[i][3]);
14
- point.database_id = initialData[i][0];
15
- points.push(point);
16
- }
17
- redrawAllCircles();
18
- }
19
-
20
- function buildCircle(x, y, e) {
21
- var c = new Path2D(),
22
- offset = 3.5;
23
-
24
- c.arc(x - offset, y - offset, 7, 0, Math.PI * 2);
25
- c.color = (typeof e === 'string') ? e : '#ff0000'
26
- c.x = x;
27
- c.y = y;
28
- return c;
29
- }
30
-
31
- function redrawAllCircles() {
32
- context.clearRect(0, 0, canvas.width, canvas.height);
33
- for (i = 0; i < points.length; i++) {
34
- context.fillStyle = points[i].color;
35
- context.fill(points[i], 'nonzero');
36
- context.stroke(points[i], 'nonzero');
37
- }
38
- }
39
-
40
- initCircles();
41
- }
42
- }