omf_web 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,8 @@
4
4
  require 'maruku'
5
5
  require 'maruku/ext/math'
6
6
  # Monkey patches to add line numbers to html output
7
+
8
+ require 'omf_web'
7
9
  require 'omf-web/widget/text/maruku/input/parse_block'
8
10
  require 'omf-web/widget/text/maruku/output/to_html'
9
11
  require 'omf-web/widget/text/maruku/helpers'
@@ -29,31 +31,66 @@ module OMF::Web::Widget::Text
29
31
 
30
32
  # Fetch text and parse it
31
33
  #
32
- def self.format_content(content_proxy)
34
+ def self.format_content_proxy(content_proxy)
33
35
  unless content_proxy.is_a? OMF::Web::ContentProxy
34
36
  raise "Expected content proxy, but got '#{content_proxy.class}'"
35
37
  end
36
- content = content_proxy.content
37
- #puts ">>>> CREATING NEW MARUKU"
38
+ format_content(content_proxy.content)
39
+ end
40
+
41
+ def self.format_content(content)
38
42
  ::Maruku.new(content)
39
43
  end
40
44
 
41
45
 
42
- class WidgetElement
46
+ class WidgetElement < OMF::Base::LObject
47
+
48
+ @@pre_create_handlers = []
49
+
50
+ # Register a block which is presented with the
51
+ # widget description (Hash) we are about to create. The
52
+ # block is assumed to return a widget description.
53
+ #
54
+ def self.on_pre_create(&block)
55
+ @@pre_create_handlers << block
56
+ end
57
+
58
+ def self.create(wdescr)
59
+ wdescr = @@pre_create_handlers.reduce(wdescr) do |wd, block|
60
+ wd2 = block.call(wd)
61
+ unless wd2.is_a? Hash
62
+ raise "Pre_create handler '#{block}' does not return hash, but '#{wd2}'"
63
+ end
64
+ wd2
65
+ end
66
+ self.new(wdescr)
67
+ end
68
+
43
69
  attr_reader :widget
44
70
 
45
71
  def initialize(wdescr)
72
+ debug "Embedding widget - #{wdescr} "
46
73
  @wdescr = wdescr
47
74
  @widget = OMF::Web::Widget.create_widget(wdescr)
75
+ debug "Created widget - #{@widget.class}"
48
76
  end
49
77
 
50
78
  def to_html
51
79
  content = @widget.content
52
80
  h = content.to_html
53
- if title = @widget.title
54
- h += "<div class='caption'>#{title}</div>"
81
+ klass = ['embedded']
82
+ if caption = @wdescr[:caption] || @widget.title
83
+ if mt = @wdescr[:mime-type]
84
+ klass << "embedded-#{mt.gsub('/', '-')}"
85
+ end
86
+ if ty = @wdescr[:type]
87
+ klass << "embedded-#{ty.gsub('/', '-')}"
88
+ end
89
+ h += "<div class='caption'><span class='figure'>Figure: </span><span class='text'>#{caption}</span></div>"
55
90
  end
56
- ::REXML::Document.new("<div class='embedded'>#{h}</div>").root
91
+ root = ::REXML::Document.new("<div class='#{klass.join(' ')}'>#{h}</div>").root
92
+ #puts "EMBEDDED >>> #{root}"
93
+ root
57
94
  end
58
95
 
59
96
  def node_type
@@ -80,7 +117,7 @@ module OMF::Web::Widget::Text
80
117
  descr = YAML::load(lines.join("\n"))
81
118
  descr = OMF::Web::deep_symbolize_keys(descr)
82
119
  if (wdescr = descr[:widget])
83
- wel = WidgetElement.new(wdescr)
120
+ wel = WidgetElement.create(wdescr)
84
121
  context << wel
85
122
  (doc.attributes[:widgets] ||= []) << wel.widget
86
123
  else
@@ -94,13 +131,12 @@ module OMF::Web::Widget::Text
94
131
 
95
132
  end # OMF::Web::Widget::Text
96
133
 
97
- # module MaRuKu::Out::HTML
98
- #
99
- # def to_html_viz
100
- # span = Element.new 'javascript'
101
- # span.attributes['class'] = 'maruku_section_number'
102
- # span << Text.new('Foooo')
103
- # add_ws span
104
- # end
105
- #
106
- # end
134
+ if __FILE__ == $0
135
+ unless fname = ARGV[0]
136
+ puts "ERROR: Missing file name"
137
+ exit -1
138
+ end
139
+ content = File.open(fname).read
140
+ x = OMF::Web::Widget::Text::Maruku.format_content(content)
141
+ puts x.to_html
142
+ end
@@ -4,49 +4,49 @@ require 'omf-web/content/repository'
4
4
 
5
5
  module OMF::Web::Widget
6
6
 
7
- # Supports widgets which displays text with
7
+ # Supports widgets which displays text with
8
8
  # potentially other widgets embedded.
9
9
  #
10
10
  class TextWidget < AbstractWidget
11
-
11
+
12
12
  def self.create_text_widget(type, wdescr)
13
13
  return self.new(wdescr)
14
14
  end
15
-
15
+
16
16
  def initialize(opts)
17
17
  opts = opts.dup # not sure why we may need to this. Is this hash used anywhere else?
18
- super opts
18
+ super opts
19
19
 
20
20
  unless (content_descr = opts[:content])
21
21
  raise "Missing 'content' option in '#{opts.inspect}'"
22
- end
22
+ end
23
23
  # if content_descr.is_a? OMF::Web::ContentProxy
24
24
  # self.content_proxy = content_descr
25
- # else
25
+ # else
26
26
  # #self.content_proxy = OMF::Web::ContentRepository[opts].load(content_descr)
27
27
  # self.content_proxy = OMF::Web::ContentRepository.create_content_proxy_for(content_descr, opts)
28
28
  # end
29
29
  self.content_proxy = OMF::Web::ContentRepository.create_content_proxy_for(content_descr, opts)
30
30
  end
31
-
31
+
32
32
  def content_proxy=(content_proxy)
33
33
  @content_proxy = content_proxy
34
34
  end
35
-
35
+
36
36
  def content()
37
- update()
37
+ update()
38
38
  OMF::Web::Theme.require 'text_renderer'
39
39
  @opts[:title] = @content.attributes[:title] || opts[:title]
40
40
  OMF::Web::Theme::TextRenderer.new(self, @content, @opts)
41
41
  end
42
-
42
+
43
43
  def update()
44
44
  # Could avoid doing the next three steps every time if we would know if the
45
45
  # content in content_proxy has changed.
46
- @content = OMF::Web::Widget::Text::Maruku.format_content(@content_proxy)
46
+ @content = OMF::Web::Widget::Text::Maruku.format_content_proxy(@content_proxy)
47
47
  @widgets = @content.attributes[:widgets] || []
48
48
  end
49
-
49
+
50
50
 
51
51
  def content_url
52
52
  @content_proxy.content_url
@@ -55,7 +55,7 @@ module OMF::Web::Widget
55
55
  def content_id
56
56
  @content_proxy.content_id
57
57
  end
58
-
58
+
59
59
  def mime_type
60
60
  'text/html'
61
61
  end
@@ -134,7 +134,7 @@ define(['omf/data_source_repo', 'vendor/d3/d3'], function(ds_repo) {
134
134
  // Find the appropriate data source and bind to it
135
135
  //
136
136
  init_single_data_source: function(ds_descr) {
137
- var ds = ds_repo.lookup(ds_descr.stream);
137
+ var ds = ds_repo.lookup(ds_descr);
138
138
  var self = this;
139
139
  OHUB.bind(ds.event_name, function() {
140
140
  self.update();;
@@ -172,8 +172,8 @@ function omf_web_data_source(opts) {
172
172
  // Reset state
173
173
  rows = [];
174
174
  update_indexes();
175
- var sm = {col_name: active_slice_col_name, col_value: col_value}
176
- send_server_msg('request_slice', {slice: sm})
175
+ var sm = {col_name: active_slice_col_name, col_value: col_value};
176
+ send_server_msg('request_slice', {slice: sm});
177
177
  }
178
178
  var active_slice_col_name = null;
179
179
  var active_slice_value = null;
@@ -211,6 +211,28 @@ function omf_web_data_source(opts) {
211
211
  return true;
212
212
  }
213
213
 
214
+ function fetch_data(data_url) {
215
+ require(['vendor/jquery/jquery'], function() {
216
+ $.ajax({
217
+ url: data_url,
218
+ dataType: 'json',
219
+ //data: opts,
220
+ type: 'GET'
221
+ }).done(function(reply) {
222
+ var data = reply.data;
223
+ rows.splice(0, rows.length); // this is an update, not an add
224
+ _.each(data, function(r) { rows.push(r); });
225
+
226
+ update_indexes();
227
+ var evt = {data_source: data_source};
228
+ OHUB.trigger(event_name, evt);
229
+ OHUB.trigger("data_source.changed", evt);
230
+ }).error(function(ajax, textStatus, errorThrown) {
231
+ console.log("ERROR: '" + textStatus + "' - " + errorThrown);
232
+ });
233
+ });
234
+ }
235
+
214
236
  // Let's check if this data_source maintains uniqueness along one axis (column)
215
237
  if (opts.unique_column) {
216
238
  var col_name = opts.unique_column;
@@ -233,7 +255,7 @@ function omf_web_data_source(opts) {
233
255
  }
234
256
  key2row[key] = row;
235
257
  return false;
236
- }
258
+ };
237
259
  // pre-seed
238
260
  rows = _.uniq(rows, false, function(r) { return r[index]; });
239
261
  _.each(rows, function(r) { key2row[r[index]] = r; });
@@ -262,7 +284,7 @@ function omf_web_data_source(opts) {
262
284
  var schema = msg.schema || msg.data_source.schema;
263
285
 
264
286
  var key = so.event.key;
265
- var col = _.find(schema, function(cd) { return cd.name == key });
287
+ var col = _.find(schema, function(cd) { return cd.name == key; });
266
288
  if (col) {
267
289
  var event = msg.datum;
268
290
  var col_id = event[col.index];
@@ -270,12 +292,18 @@ function omf_web_data_source(opts) {
270
292
  set_slice_column(col_id);
271
293
  }
272
294
  }
273
- })
295
+ });
274
296
  }
275
297
  }
276
298
 
277
- if (window.WebSocket) {
299
+ if (window.WebSocket && opts.ws_url) {
278
300
  start_web_socket();
301
+ } else if (opts.update_url) {
302
+ throw "Missing implementation for NON web socket browsers";
303
+ } else if (opts.data_url) {
304
+ fetch_data(opts.data_url);
305
+ } else {
306
+ throw "Don't know how to fetch data source '" + name + "'.";
279
307
  }
280
308
 
281
309
  // Bind to event directly
@@ -289,7 +317,7 @@ function omf_web_data_source(opts) {
289
317
  define(function() {
290
318
  return function(opts) {
291
319
  return omf_web_data_source(opts);
292
- }
293
- })
320
+ };
321
+ });
294
322
 
295
323
 
@@ -18,15 +18,23 @@ define(['omf/data_source3'], function(data_source) {
18
18
  var name;
19
19
  var dynamic = false;
20
20
 
21
- if (typeof(ds_descr) == 'object') {
22
- name = ds_descr.id || ds_descr.stream || ds_descr.name;
23
- dynamic = ds_descr.dynamic;
24
- } else {
25
- name = ds_descr;
21
+ if (typeof(ds_descr) != 'object') {
22
+ ds_descr = {name: ds_descr};
26
23
  }
24
+ name = ds_descr.id || ds_descr.stream || ds_descr.name;
25
+ dynamic = ds_descr.dynamic;
27
26
  var source = sources[name];
28
27
  if (! source) {
29
- throw("Unknown data source '" + name + "'.");
28
+ // Let's see if we can create one out of
29
+ if (ds_descr.data_url) {
30
+ // We can try to fetch it directly
31
+ if (! name) {
32
+ name = ds_descr.id = ds_descr.data_url;
33
+ }
34
+ source = sources[name] = data_source(ds_descr);
35
+ } else {
36
+ throw("Unknown data source '" + name + "'.");
37
+ }
30
38
  }
31
39
  if (dynamic) {
32
40
  source.is_dynamic(dynamic);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omf_web
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-24 00:00:00.000000000 Z
12
+ date: 2014-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: omf_oml