jqplot_rails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == v0.0.3
2
+ * DRY up implementation
3
+ * Provide #jqplot_data_onclick helper for onclick events on plot data
4
+
1
5
  == v0.0.2
2
6
  * require version into tasks
3
7
 
@@ -13,13 +13,23 @@ module JqPlotRails
13
13
  def jqplot(dom_id, data, opts={})
14
14
  output = jqplot_setup
15
15
  output << "window._jqplot_rails['#{_j_key(dom_id)}'] = jQuery.jqplot('#{_j_key(dom_id)}', #{format_type_to_js(data)}, #{format_type_to_js(opts)});".html_safe
16
- if(opts.delete(:raw))
17
- output
18
- else
19
- javascript_tag do
20
- output
21
- end
16
+ _j_wrap(opts[:raw], output)
17
+ end
18
+
19
+ # dom_id:: DOM ID used for the plot
20
+ # opts:: Ooptions hash for building plot binding
21
+ # - :function -> RawJS instance with full function defined
22
+ # - :link_to -> {:url, :remote}
23
+ # Bind to click event on data and make request
24
+ def jqplot_data_onclick(dom_id, opts={})
25
+ output = jqplot_setup
26
+ raise 'Only :function or :link_to may be defined, not both.' if opts[:function].present? && opts[:link_to].present?
27
+ raise 'Must provide :function or :link_to for event.' if opts[:function].blank? && opts[:link_to].blank?
28
+ function = opts[:link_to].present? ? _j_build_click_url_event(dom_id, opts[:link_to]) : opts[:function]
29
+ output << jqplot_exists(dom_id) do
30
+ "jQuery(#{format_type_to_js(format_id(dom_id))}).bind('jqplotDataClick', #{format_type_to_js(function)});".html_safe
22
31
  end
32
+ _j_wrap(opts[:raw], output)
23
33
  end
24
34
 
25
35
  # dom_id:: DOM ID used for the plot
@@ -31,13 +41,7 @@ module JqPlotRails
31
41
  output << jqplot_exists(dom_id) do
32
42
  "#{jqplot_instance(dom_id)}.replot({clear:true,resetAxes:true});"
33
43
  end
34
- if(args.include?(:raw))
35
- output.html_safe
36
- else
37
- javascript_tag do
38
- output.html_safe
39
- end
40
- end
44
+ _j_wrap(args.include?(:raw), output)
41
45
  end
42
46
 
43
47
  # text:: Button text
@@ -68,21 +72,18 @@ module JqPlotRails
68
72
  "jQuery(#{format_type_to_js(format_id(dom_id))}).resizable(#{format_type_to_js(resize_args)});" +
69
73
  "jQuery(#{format_type_to_js(format_id(dom_id))}).bind('resize', function(event,ui){ #{jqplot_instance(dom_id)}.replot(); });"
70
74
  end
71
- if(args.include?(:raw))
72
- output.html_safe
73
- else
74
- javascript_tag{ output.html_safe }
75
- end
75
+ _j_wrap(args.include?(:raw), output)
76
76
  end
77
77
 
78
78
  private
79
-
79
+
80
+ # Setups up environment for plots (creates storage area)
80
81
  def jqplot_setup
81
82
  output = ''
82
83
  unless(@_jqplot_setup)
83
84
  output << 'if(window._jqplot_rails == undefined){ window._jqplot_rails = {}; }'
84
85
  end
85
- output
86
+ output.html_safe
86
87
  end
87
88
 
88
89
  # dom_id:: DOM ID used for the plot
@@ -94,11 +95,44 @@ module JqPlotRails
94
95
  # dom_id:: DOM ID used for the plot
95
96
  # Returns the plot instance
96
97
  def jqplot_instance(dom_id)
97
- "window._jqplot_rails['#{_j_key(dom_id)}']"
98
+ "window._jqplot_rails['#{_j_key(dom_id)}']".html_safe
98
99
  end
99
100
 
101
+ # key:: DOM ID for plot
102
+ # Helper to remove hash prefix if found
100
103
  def _j_key(key)
101
- key.sub('#', '')
104
+ key.sub('#', '').html_safe
105
+ end
106
+
107
+ # raw:: Boolean. Raw or wrapped string
108
+ # string:: Javascript string
109
+ # Helper to wrap javascript within tag if required
110
+ def _j_wrap(raw, string)
111
+ raw ? string.html_safe : javascript_tag{ string.html_safe }
112
+ end
113
+
114
+ # opts:: Hash
115
+ # - :url -> Path or symbole
116
+ # - :use_ticks -> Map index to tick name and pass tick name instead (true defaults to x-axis or :x/:y)
117
+ # - :remote -> Boolean for ajax call
118
+ # - :args -> extra arguments for url building
119
+ def _j_build_click_url_event(dom_id, opts={})
120
+ output = 'function(ev, seriesIndex, pointIndex, data){'
121
+ index = opts[:use_ticks] ? "#{jqplot_instance(dom_id)}.axes.#{opts[:use_ticks] == :y ? 'y' : 'x'}axis.ticks[data[0] - 1]" : "data[0]"
122
+ if(opts[:url].is_a?(Symbol))
123
+ args = ['000']
124
+ args += opts[:args] if opts[:args].present?
125
+ url = RawJS.new("'#{Rails.application.routes.url_helpers.send(opts[:url].to_s.sub('_url', '_path').to_sym, *args)}'.replace('000', #{index})")
126
+ else
127
+ url = RawJS.new("'#{opts[:url]}#{opts[:url].include?('?') ? '&' : '?'}jqplot_id='+#{index}")
128
+ end
129
+ if(opts[:remote])
130
+ output << "jQuery.get(#{format_type_to_js(url)}, null, 'script');"
131
+ else
132
+ output << "window.location = #{format_type_to_js(url)};"
133
+ end
134
+ output << '}'
135
+ output.html_safe
102
136
  end
103
137
  end
104
138
  end
@@ -13,5 +13,5 @@ module JqPlotRails
13
13
  end
14
14
  end
15
15
 
16
- VERSION = Version.new('0.0.2')
16
+ VERSION = Version.new('0.0.3')
17
17
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jqplot_rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Roberts
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-08 00:00:00 -08:00
18
+ date: 2011-12-13 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency