object-view 0.5.1 → 0.5.2

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: 9a3e852c6cf134735ab472d51e36be97544b96d4
4
- data.tar.gz: e618efed7c6ef952d5dcbec9555e694ff24031f5
3
+ metadata.gz: 789698e398c060a73331c478334ebfea705f5ed1
4
+ data.tar.gz: 9d4a12327c47ad84c221e0e8781f071cec7f2474
5
5
  SHA512:
6
- metadata.gz: 9d14e2554569b961f9b67b32d57106ad3fcecaed35b5e653b992dd717d8717ae81f606fdac7f8eb381599f09b1a7c28153e5c853a4745f7895491e4d33a4df14
7
- data.tar.gz: d69ced5afebbeba30aad4a68922fe4189ccdd7628a50b6476482b34a8af58ec97369687f94ca9f3d985dd0f7a28509da9f821499096e6fed3010618b8e00735f
6
+ metadata.gz: a9d01a138e154c216b5f18771ccc48d5495be149cc5332926c3b252bf4ac0183f8564ab016cb26dda256bb1b83cc535f8cf7fd938d133c0de8fe08c31f447dd0
7
+ data.tar.gz: 87dcef46cbaf4de1ea57478f9b2639ba5ddc9e426146c9175436bd2134cb0430887d8d4b42dbcd64e36ac93936c26a55320758446d43d90aec8233687e38ed3d
data/lib/object-view.rb CHANGED
@@ -18,6 +18,9 @@ require_relative "object_view/paragraph"
18
18
  require_relative 'object_view/span'
19
19
  require_relative 'object_view/input'
20
20
  require_relative 'object_view/iframe'
21
+ require_relative 'object_view/pie_chart'
22
+ require_relative 'object_view/line_chart'
23
+ require_relative 'object_view/iframe'
21
24
 
22
25
 
23
26
  module ObjectView
@@ -1,15 +1,15 @@
1
- @@chart_id = 0
2
-
3
-
4
1
  module ObjectView
5
2
  # ajax_method_for_params specifies a javascript method that will be called by ajax update script to retrieve non-static params
6
3
  # This method needs to be added to page and returns a string in the form : "a=b&c=d&e=f"
7
4
  class ChartData
5
+ @@chart_id = 0
8
6
  attr_accessor :id, :series, :label, :chart_type, :x_axis_label, :y_axis_labels, :ajax_url, :ajax_params, :ajax_refresh_seconds, :ajax_method_for_params
9
7
 
10
8
  def initialize(type)
11
9
  @@chart_id += 1
12
10
  @ajax_method_for_params = nil
11
+ @ajax_refresh_seconds = 20
12
+
13
13
  @ajax_params = Hash.new
14
14
  @id = "chart_#{@@chart_id}"
15
15
  @chart_type = type
@@ -0,0 +1,360 @@
1
+ require_relative "./javascript"
2
+ module ObjectView
3
+
4
+ class HighChartLoadJavascriptAjax < ObjectView::Javascript
5
+
6
+ def initialize(chart_data_list, prepend_script)
7
+ super()
8
+
9
+ script = ""
10
+ if (prepend_script != nil)
11
+ script += prepend_script
12
+ end
13
+
14
+ script += chart_class_script()
15
+
16
+
17
+ script += "
18
+ _chartHash = new Object();
19
+ _chartList = new Array();
20
+ "
21
+ chart_data_list.each do |chart_data|
22
+ params_line = params_for_chart(chart_data)
23
+ refresh_seconds = chart_data.ajax_refresh_seconds
24
+ if (refresh_seconds == nil)
25
+ refresh_seconds = "null"
26
+ end
27
+
28
+ # 'apple=green&extent=' + _extent + ''
29
+ # 'extent=' + _extent + '&' + 'apple=green' + '&' + 'banana=yellow'
30
+ script += "
31
+ #{params_line}
32
+ var chartDraw = new HighChartDraw('#{chart_data.id}', '#{chart_data.ajax_url}', params, #{refresh_seconds});
33
+ _chartHash['#{chart_data.id}'] = chartDraw;
34
+ _chartList.push(chartDraw);
35
+ console.log('_chartList.length: ' + _chartList.length);
36
+ "
37
+ end
38
+ script += "
39
+ function periodicCheckForChartDraw() {
40
+ var chartCount = _chartList.length;
41
+ //console.log('periodicCheckForChartDraw() count: ' + chartCount);
42
+ for (i = 0; i < chartCount; i++) {
43
+ chart = _chartList[i];
44
+ chart.periodicCheck();
45
+ }
46
+ }
47
+ setInterval(periodicCheckForChartDraw, 500);
48
+ "
49
+
50
+ self.add script
51
+ end
52
+
53
+ def chart_class_script
54
+ script =
55
+ "
56
+
57
+ function prepareChartData(chartData) {
58
+ console.log('Data type: ' + typeof(chartData));
59
+ prepareObjectAndChildren(chartData);
60
+ }
61
+
62
+
63
+
64
+ function prepareObjectAndChildren(object) {
65
+ if (typeof(object) == 'object') {
66
+ for (var key in object) {
67
+ if (object.hasOwnProperty(key)) {
68
+ //console.log('key is: ' + key + ', value is: ' + object[key]);
69
+ child = object[key];
70
+ if (typeof(child) == \"object\" ) {
71
+ prepareObjectAndChildren(child);
72
+ } else if (typeof(child) == 'string') {
73
+ //console.log('STRING key is: ' + key + ', value is: ' + object[key]);
74
+ if (child.indexOf('/@') == 0) {
75
+ var functionCode = child.replace('/@', '');
76
+ functionCode = functionCode.replace('@/', '');
77
+ object[key] = function() {
78
+ return eval(functionCode);
79
+ }
80
+ //console.log('STRING (functionCode): ' + key + ', value is: ' + functionCode);
81
+ //console.log('STRING was function ' + key + ', value is: ' + object[key]);
82
+ }
83
+ }
84
+ }
85
+ }
86
+ }
87
+ }
88
+ //response = response.replace('\"@@', '');
89
+ //response = response.replace('@@\"', '');
90
+
91
+
92
+ function HighChartDraw(chartId, ajaxUrl, ajaxParams, refreshSeconds) {
93
+ console.log('Created HighChartDraw (' + chartId + ')');
94
+ this.nextTimeToDraw = 0;
95
+ this.currentlyDrawing = false;
96
+ this.ajaxUrl = ajaxUrl;
97
+ this.ajaxParams = ajaxParams;
98
+ this.refreshSeconds = refreshSeconds;
99
+ this.chartId = chartId;
100
+ console.log('Initializing chart: ' + this.chartId);
101
+ this.chartRequestMade = false;
102
+ this.clearChart();
103
+ }
104
+
105
+ HighChartDraw.prototype.buildParamString = function() {
106
+ var paramString = '';
107
+ for (var paramName in this.ajaxParams) {
108
+ if (this.ajaxParams.hasOwnProperty(paramName)) {
109
+ paramValue = this.ajaxParams[paramName];
110
+ if (paramString.length > 0) {
111
+ paramString = paramString + '&';
112
+ }
113
+ paramString += paramName + '=';
114
+ var indexOfVal = paramValue.indexOf('VAR|');
115
+ if (indexOfVal == 0) {
116
+ var variable = paramValue.substring(4, paramValue.length);
117
+ console.log('Variable for eval: ' + variable);
118
+ paramString += encodeURIComponent(eval(variable));
119
+ //paramString += eval(variable);
120
+ } else {
121
+ paramString += encodeURIComponent(paramValue);
122
+ //paramString += paramValue;
123
+ }
124
+ }
125
+ }
126
+ console.log('Post params: ' + paramString);
127
+ return paramString;
128
+ }
129
+
130
+
131
+ HighChartDraw.prototype.draw = function() {
132
+ try {
133
+ this.currentlyDrawing = true;
134
+ console.log('Drawing chart (' + this.chartId + ')');
135
+ var chartElementId = 'chart_1';
136
+ var chartId = this.chartId;
137
+ $.ajax({
138
+ type: 'GET',
139
+ url: this.ajaxUrl,
140
+ data: this.buildParamString(),
141
+ dataType: 'text',
142
+ success: function(response) {
143
+ var chartData = jQuery.parseJSON(response);
144
+ prepareChartData(chartData);
145
+ $('#' + chartId).highcharts(chartData);
146
+ },
147
+ error: function(XMLHttpRequest, textStatus, errorThrown) {
148
+ $('#' + chartId).html('<h3>Error getting chart data - QA server may be down.</h3>');
149
+ }
150
+ });
151
+ console.log('finished drawing chart. Queuing another drawHighChart request.');
152
+ if (this.refreshSeconds != null) {
153
+ this.makeChartDrawRequest(1000 * this.refreshSeconds);
154
+ }
155
+ this.currentlyDrawing = false;
156
+ } catch(error) {
157
+ console.log('Exception drawing chart: ' + error);
158
+ this.currentlyDrawing = false;
159
+ this.makeChartDrawRequest(1000);
160
+ throw error;
161
+ }
162
+ this.currentlyDrawing = false;
163
+ }
164
+
165
+ HighChartDraw.prototype.clearChart = function() {
166
+ $('#' + this.chartId).html('<h2 style=\"color: #aaaaaa\">Building Chart...</h2>');
167
+ }
168
+
169
+ HighChartDraw.prototype.makeChartDrawRequest = function(millisFromNow) {
170
+ var nextTime = jQuery.now() + millisFromNow;
171
+ if ((this.nextTimeToDraw == null) || (nextTime < this.nextTimeToDraw)) {
172
+ this.nextTimeToDraw = nextTime;
173
+ }
174
+ console.log('Request to drawHighChart (' + this.chartId + ') queued for: ' + this.nextTimeToDraw);
175
+ }
176
+
177
+ HighChartDraw.prototype.periodicCheck = function() {
178
+ //console.log('periodicCheck for ' + this.chartId);
179
+ var now = jQuery.now();
180
+ if (this.nextTimeToDraw != null) {
181
+ if (now > this.nextTimeToDraw) {
182
+ this.doingPeriodicCheck = false;
183
+ if (this.currentlyDrawing == false) {
184
+ this.nextTimeToDraw = null;
185
+ this.draw();
186
+ } else {
187
+ //console.log('periodicCheck for ' + this.chartId + ' currently drawing, so ignoring request');
188
+ }
189
+ } else {
190
+ //console.log('periodicCheck for ' + this.chartId + ' next draw time not reached');
191
+ }
192
+ } else {
193
+ //console.log('periodicCheck for ' + this.chartId + ' nextTimeToDraw is null');
194
+ }
195
+ this.doingPeriodicCheck = false;
196
+ }
197
+ "
198
+ return script
199
+ end
200
+
201
+
202
+
203
+ def draw_chart_function(ajax_refresh_seconds, chart_data_list)
204
+ script = "
205
+ function drawHighChart() {
206
+ console.log('drawHighChart called');
207
+ "
208
+ script += "
209
+ try {
210
+ _currentlyDrawing = true;
211
+ console.log('Drawing charts.');
212
+ //alert('drawHighChart()');
213
+ "
214
+ script += " #{chart_data_load_script(chart_data_list)}\n"
215
+
216
+ if (ajax_refresh_seconds != nil)
217
+ script += "
218
+ console.log('finished drawing chart. Queuing another drawHighChart request.');
219
+ makeChartDrawRequest(#{ajax_refresh_seconds * 1000});\n
220
+ _currentlyDrawing = false;
221
+ "
222
+ end
223
+
224
+ script += "
225
+ } catch(error) {
226
+ console.log('Exception drawing chart: ' + error);
227
+ _currentlyDrawing = false;
228
+ makeChartDrawRequest(1000);
229
+ }
230
+ "
231
+
232
+ script += "
233
+ _currentlyDrawing = false;
234
+ }
235
+ "
236
+ return script
237
+ end
238
+
239
+
240
+ def clear_script_for_each_chart(chart_data_list)
241
+ script = ""
242
+ chart_data_list.each do |chart_data|
243
+ chart_data.id
244
+ script += " $('##{chart_data.id}').html('<h3 style=\"color: #aaaaaa\">Building Chart</h3>');\n"
245
+ end
246
+ return script
247
+ end
248
+
249
+ def periodic_check_function(chart_data_list)
250
+ script = "
251
+ function periodicCheck(chartId) {
252
+ var now = jQuery.now();
253
+ if (_nextTimeToDraw[chartId] != null) {
254
+ if (now > _nextTimeToDraw[chartId]) {
255
+ _doingPeriodicCheck = false;
256
+ if (_currentlyDrawing == false) {
257
+ _nextTimeToDraw[chartId] = null;
258
+ drawHighChart(chartId);
259
+ } else {
260
+ }
261
+ }
262
+ }
263
+ _doingPeriodicCheck = false;
264
+ }
265
+ }
266
+ "
267
+
268
+ script +=
269
+ "
270
+ function periodicCheckAll() {
271
+ if (!_doingPeriodicCheck) {
272
+ _doingPeriodicCheck = true;
273
+ "
274
+
275
+ chart_data_list.each do |chart_data|
276
+ script += " periodicCheck('#{chart_data.id}'); "
277
+ end
278
+
279
+ script +=
280
+ "
281
+ }
282
+ }
283
+ "
284
+ end
285
+
286
+ def params_for_chart(chart_data)
287
+ params_str = "var params = new Object();\n"
288
+
289
+ chart_data.ajax_params.each do |name, value|
290
+ params_str += " params['#{name}'] = '#{value}';\n"
291
+ end
292
+
293
+ return params_str
294
+ end
295
+
296
+ # def params_for_chart(chart_data)
297
+ # params_str = "var params = '' + "
298
+ #
299
+ # chart_data.ajax_params.each do |name, value|
300
+ # # params_string += "#{name}=#{value}&"
301
+ # if (value.to_s.start_with? "VAR|")
302
+ # params_str += "'#{name}=' + #{value[4..(value.length - 1)]}"
303
+ # else
304
+ # encoded_value = URI.encode_www_form([[name, value]])
305
+ # params_str += "'#{encoded_value}'"
306
+ # #params_str += "'#{name}=#{encoded_value}'"
307
+ # end
308
+ # params_str += "+'&'+"
309
+ # end
310
+ #
311
+ # params_str = params_str[0..params_str.length - 6]
312
+ # params_str += ';'
313
+ # # if (params_string.length > 0)
314
+ # # params_string = params_string[0..params_string.length - 2]
315
+ # # end
316
+ # # params_string = URI.encode_www_form(params)
317
+ # return params_str
318
+ # end
319
+
320
+ def chart_data_load_script(chart_data_list)
321
+ script = ""
322
+ chart_data_list.each do |chart_data|
323
+
324
+
325
+ script +=
326
+ "
327
+ var chartElementId = '#{chart_data.id}';
328
+ $.ajax({
329
+ type: 'GET',
330
+ url: '#{chart_data.ajax_url}',
331
+ data: params,
332
+ dataType: 'text',
333
+ success: function(response) {
334
+ //alert('Response: ' + response);
335
+ var chartData = jQuery.parseJSON(response);
336
+ //alert('Response: ' + response);
337
+ $('#' + chartElementId).highcharts(chartData);"
338
+
339
+
340
+ if (chart_data.ajax_refresh_seconds != nil)
341
+ script += "
342
+ makeChartDrawRequest(#{chart_data.ajax_refresh_seconds * 1000});
343
+ "
344
+ end
345
+
346
+ script += "
347
+ },
348
+ error: function(XMLHttpRequest, textStatus, errorThrown) {
349
+ $('#' + chartElementId).html('<h3>Error getting chart data - QA server may be down.</h3>');
350
+ }
351
+ });
352
+ "
353
+ end
354
+ return script
355
+ end
356
+
357
+ end
358
+
359
+
360
+ end # Module
@@ -0,0 +1,17 @@
1
+ class LineChart < ObjectView::Div
2
+ include ObjectView
3
+
4
+ attr_reader :data
5
+
6
+ def initialize(label)
7
+ super()
8
+ @data = ChartData.new(:line)
9
+ @data.label = label
10
+ @data.chart_type = :line
11
+ self.id = @data.id
12
+ add "Loading Chart..."
13
+ self.style = "border: 1px solid black; width: 300px; height: 120px"
14
+ end
15
+
16
+
17
+ end
@@ -0,0 +1,12 @@
1
+ class PieChart < LineChart
2
+ def initialize(label)
3
+ super(label)
4
+
5
+ self.data.chart_type = :pie
6
+ self.data.x_axis_label = "Functional Area"
7
+ self.data.y_axis_labels = ["Defects"]
8
+ add "Loading Chart..."
9
+ self.style = "border: 1px solid black; width: 200px; height: 200px"
10
+
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object-view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -83,15 +83,18 @@ files:
83
83
  - lib/object_view/errors/object_view_error.rb
84
84
  - lib/object_view/head.rb
85
85
  - lib/object_view/header.rb
86
+ - lib/object_view/high_chart_ajax_javascript.rb
86
87
  - lib/object_view/iframe.rb
87
88
  - lib/object_view/image.rb
88
89
  - lib/object_view/input.rb
89
90
  - lib/object_view/javascript.rb
90
91
  - lib/object_view/javascript_file.rb
92
+ - lib/object_view/line_chart.rb
91
93
  - lib/object_view/link.rb
92
94
  - lib/object_view/nav.rb
93
95
  - lib/object_view/page.rb
94
96
  - lib/object_view/paragraph.rb
97
+ - lib/object_view/pie_chart.rb
95
98
  - lib/object_view/span.rb
96
99
  - lib/object_view/stylesheet.rb
97
100
  - lib/object_view/table.rb