wedge 0.0.24 → 0.0.25

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: df960de9ec0aff5b5755f648b90f36bebfd54611
4
- data.tar.gz: 44fe18577d66a8048f32c45cb6ca22ff263e59d6
3
+ metadata.gz: 1325b8416e2afc87ff70321a0a91c11722e56e09
4
+ data.tar.gz: a47ea5fdb2f6d59e2b8d4122a99aeec775ff85aa
5
5
  SHA512:
6
- metadata.gz: e6f682ce625ca5e7c0b256ea447fd140d2a10f9bac29b8ccf291df603392d219cd52c606467095c8619b5e1af1ac67b71218e846b94aa0572b55605ea4245c47
7
- data.tar.gz: e88022f54d6c51bf5061d6a935001575a21ef20f5845919c92c748f27e9f1adbda4176a034e61a3d9fdc9fdaea3745dea7b8642913096b478cb5a211e21d2b49
6
+ metadata.gz: 2daafe5c24e6e0d7aeb5a69a80d41a88d790d38da448619f5bbdc428ae05083e537c7eedd140791f2507c4ae5254db3663ab06c17d015afdffb415b4491f7294
7
+ data.tar.gz: a28ed8752d98e989756705e63666fd13be0ea943ea253ec8cc714ea7d87c9d752a03ee66d58dca874fbedf91c900d49f4ca38a29d8e26b4ee5f1e4023412f1cb
@@ -75,7 +75,7 @@ class Roda
75
75
  res = scope.wedge(name, data).send(method_called, *method_args) || ''
76
76
  end
77
77
 
78
- scope.response.headers["BIO-CSRF-TOKEN"] = scope.csrf_token if scope.methods.include? :csrf_token
78
+ scope.response.headers["WEDGE-CSRF-TOKEN"] = scope.csrf_token if scope.methods.include? :csrf_token
79
79
 
80
80
  if res.is_a? Hash
81
81
  scope.response.headers["Content-Type"] = 'application/json; charset=UTF-8'
@@ -22,7 +22,7 @@ module Wedge
22
22
 
23
23
  class << self
24
24
  attr_accessor :requires, :loaded_requires, :loaded_requires_events, :javascript_cache,
25
- :wedge_javascript_loaded, :object_events, :browser_events
25
+ :wedge_javascript_loaded, :object_events, :browser_events, :events_triggered
26
26
 
27
27
  def compile_opal
28
28
  javascript
@@ -78,9 +78,13 @@ module Wedge
78
78
  # @return [Array] List of opal paths.
79
79
  def append_paths
80
80
  @append_paths ||= begin
81
- file = method(:components).source_location.first.sub('/wedge.rb', '')
81
+ file = method(:components).source_location.first.sub('/wedge.rb', '')
82
+ gems_dir = ::Opal.gem_dir.gsub(/(?<=gems)\/opal-.*/, '')
82
83
  Wedge::Opal.append_path file
83
84
  Wedge::Opal.append_path Dir.pwd
85
+ Dir["#{gems_dir}/**/"].sort.each do |folder|
86
+ Wedge::Opal.append_path "#{folder}/lib"
87
+ end
84
88
  end
85
89
  end
86
90
  end
@@ -132,6 +136,7 @@ module Wedge
132
136
  end
133
137
  end
134
138
  else
139
+ Wedge.events_triggered ||= []
135
140
  Wedge.loaded_requires ||= []
136
141
  Wedge.loaded_requires_events ||= []
137
142
  reqs = Wedge.requires[options[:name].to_sym]
@@ -152,9 +157,17 @@ module Wedge
152
157
  comp = Wedge[name, options]
153
158
 
154
159
  Document.ready? do
160
+ if method_called && !comp.wedge_opts.on_server_methods.include?(method_called)
161
+ comp.send(method_called, *method_args)
162
+ end
163
+
164
+ unless Wedge.events_triggered.include?(name)
165
+ comp.wedge_trigger :browser_events
166
+ Wedge.loaded_requires << name
167
+ Wedge.events_triggered << name
168
+ end
169
+
155
170
  trigger_requires_events requires.dup
156
- comp.send(method_called, *method_args) if method_called
157
- comp.wedge_trigger :browser_events
158
171
  end
159
172
  end
160
173
  end
@@ -166,9 +179,11 @@ module Wedge
166
179
 
167
180
  reqs.each do |r|
168
181
  next if Wedge.loaded_requires_events.include? r[:name]
182
+
169
183
  Wedge.loaded_requires_events << r[:name]
170
184
  comp = Wedge[r[:name], r]
171
185
  comp.wedge_trigger :browser_events
186
+ Wedge.events_triggered << r[:name]
172
187
  end
173
188
 
174
189
  trigger_requires_events requires if requires.any?
@@ -184,7 +199,7 @@ module Wedge
184
199
  Wedge.loaded_requires << r[:name]
185
200
 
186
201
  promises << -> { load_comp r }
187
- end
202
+ end if reqs
188
203
 
189
204
  Promise.when(*promises.map!(&:call)).then do
190
205
  requires.any?? load_requires(requires, promise) : promise.resolve(true)
@@ -211,13 +226,17 @@ module Wedge
211
226
  path_name = options[:path_name]
212
227
  assets_url = Wedge.assets_url_with_host
213
228
 
214
- # fix: this could give people unwanted behaviour, change getScript to just
215
- # use ajax.
216
- `jQuery.ajaxSetup({ cache: true })` if Wedge.opts.cache_assets
217
- `$.getScript(assets_url + "/" + path_name + ".js").done(function(){`
229
+ if !Wedge.components[options[:name]]
230
+ # fix: this could give people unwanted behaviour, change getScript to just
231
+ # use ajax.
232
+ `jQuery.ajaxSetup({ cache: true })` if Wedge.opts.cache_assets
233
+ `$.getScript(assets_url + "/" + path_name + ".js").done(function(){`
234
+ promise.resolve true
235
+ `}).fail(function(jqxhr, settings, exception){ window.console.log(exception); });`
236
+ #########################################################################
237
+ else
218
238
  promise.resolve true
219
- `}).fail(function(jqxhr, settings, exception){ window.console.log(exception); });`
220
- #########################################################################
239
+ end
221
240
 
222
241
  promise
223
242
  end
@@ -2,16 +2,19 @@ module Wedge
2
2
  class Component
3
3
  include Methods
4
4
 
5
- ALLOWED_CLIENT_OPTS = %i(name path_name method_args method_called cache tmpl key cache_assets assets_key assets_url assets_url_with_host requires)
5
+ ALLOWED_CLIENT_OPTS = %i(name path_name method_args method_called cache tmpl key cache_assets assets_key assets_url assets_url_with_host requires skip_method_wrap on_server_methods)
6
6
 
7
7
  class << self
8
8
  # Override the default new behaviour
9
9
  def new(*args, &block)
10
- obj = allocate
11
-
10
+ obj = allocate
12
11
  obj.wedge_opts.js = args.delete(:js)
13
12
  obj.wedge_opts.init = args.delete(:init)
14
13
 
14
+ if args.any? && obj.wedge_opts.skip_method_wrap
15
+ obj.wedge_opts.init = args
16
+ end
17
+
15
18
  # Merge other args into opts
16
19
  args.each { |a| a.each {|k, v| obj.wedge_opts[k] = v } } if args.any?
17
20
 
@@ -34,7 +37,7 @@ module Wedge
34
37
  end
35
38
 
36
39
  # don't need to wrap the method if it's opal
37
- unless RUBY_ENGINE == 'opal' || wedge_opts.methods_wrapped
40
+ unless RUBY_ENGINE == 'opal' || wedge_opts.methods_wrapped || wedge_opts.skip_method_wrap
38
41
  obj.wedge_opts.methods_wrapped = wedge_opts.methods_wrapped = true
39
42
 
40
43
  public_instance_methods(false).each do |meth|
@@ -185,7 +188,26 @@ module Wedge
185
188
 
186
189
  def wedge_on_server(&block)
187
190
  if server?
191
+ m = Module.new(&block)
192
+
188
193
  yield
194
+
195
+ m.public_instance_methods(false).each do |meth|
196
+ wedge_opts.on_server_methods << meth.to_s
197
+
198
+ alias_method :"wedge_on_server_#{meth}", :"#{meth}"
199
+ define_method "#{meth}" do |*args, &blk|
200
+ o_name = "wedge_on_server_#{meth}"
201
+
202
+ if method(o_name).parameters.length > 0
203
+ result = send(o_name, *args, &block)
204
+ else
205
+ result = send(o_name, &block)
206
+ end
207
+
208
+ blk ? blk.call(result) : result
209
+ end
210
+ end
189
211
  else
190
212
  m = Module.new(&block)
191
213
 
@@ -214,7 +236,7 @@ module Wedge
214
236
 
215
237
  # We set the new csrf token
216
238
  xhr = Native(response.xhr)
217
- csrf = xhr.getResponseHeader('BIO-CSRF-TOKEN')
239
+ csrf = xhr.getResponseHeader('WEDGE-CSRF-TOKEN')
218
240
  Element.find('meta[name=_csrf]').attr 'content', csrf
219
241
  ###########################
220
242
 
@@ -22,6 +22,7 @@ module Wedge
22
22
  scope: false,
23
23
  loaded: false,
24
24
  requires: [],
25
+ skip_method_wrap: false,
25
26
  on: [],
26
27
  on_server_methods: [],
27
28
  object_events: {},
@@ -84,6 +85,10 @@ module Wedge
84
85
  opts.to_h.inject({}) {|copy, (key, value)| copy[key] = value.dup rescue value; copy}
85
86
  end
86
87
 
88
+ def skip_method_wrap
89
+ opts.skip_method_wrap = true
90
+ end
91
+
87
92
  %w(scope assets_url assets_url_with_host cache_assets assets_key debug).each do |m|
88
93
  define_method m do |v|
89
94
  opts[m] = v
@@ -14,6 +14,8 @@ module Wedge
14
14
  end
15
15
 
16
16
  def initialize html
17
+ html = '' if html.nil?
18
+
17
19
  @raw_html = html
18
20
 
19
21
  if server?
@@ -26,7 +26,7 @@ module Wedge
26
26
  end
27
27
  end
28
28
 
29
- if event[:name].to_s == 'ready' || event[:name] =~ /[:\s]/ || event[:selector]
29
+ if %w(ready history_change).include?(event[:name].to_s) || event[:name] =~ /[:\s]/ || event[:selector]
30
30
  browser_events << event
31
31
  else
32
32
  event[:component] = scope.wedge_opts.name
@@ -73,7 +73,7 @@ module Wedge
73
73
  el = Element.find(event[:selector] != '' ? event[:selector] : 'body')
74
74
 
75
75
  comp.instance_exec el, &event[:block]
76
- when event[:name].to_s == 'history_change'
76
+ when event[:name].to_s == 'history_change'
77
77
  $window.history.change do |he|
78
78
  comp.instance_exec he, &event[:block]
79
79
  end
@@ -339,6 +339,14 @@ module Wedge
339
339
  _attributes.empty?
340
340
  end
341
341
 
342
+ def wedge_config
343
+ @wedge_config ||= begin
344
+ c = super
345
+ c.skip_method_wrap
346
+ c
347
+ end
348
+ end
349
+
342
350
  module InstanceMethods
343
351
  def render_fields data, options = {}
344
352
  data = data.is_a?(Hash) ? data.to_obj : data
@@ -1,30 +1,24 @@
1
+ if RUBY_ENGINE == 'opal'
2
+ class Element
3
+ alias_native :mask
4
+ alias_native :remove_data, :removeData
5
+ alias_native :replace_with, :replaceWith
6
+ alias_native :selectize
7
+
8
+ def date_picker options = {}
9
+ `self.datepicker(JSON.parse(#{options.to_json}))`
10
+ end
11
+ end
12
+ end
13
+
1
14
  module Wedge
2
15
  module Plugins
3
16
  class Pjax < Component
4
17
  config.name :pjax, :pjax_plugin
5
18
  config.requires :history_plugin
6
19
 
7
- class Nanobar
8
- include Native
9
-
10
- alias_native :go
11
- alias_native :start
12
- alias_native :finish
13
-
14
- def initialize options = {}
15
- `var Nanobar=function(){"use strict";var t,i,e,s,h,n,o={width:"100%",height:"4px",zIndex:9999,top:"0"},a={width:0,height:"100%",clear:"both",transition:"height .3s"};return t=function(t,i){var e;for(e in i)t.style[e]=i[e];t.style["float"]="left"},s=function(){var t=this,i=this.width-this.here;.1>i&&i>-.1?(h.call(this,this.here),this.moving=!1,100==this.width&&(this.el.style.height=0,setTimeout(function(){t.cont.el.removeChild(t.el)},300))):(h.call(this,this.width-i/4),setTimeout(function(){t.go()},16))},h=function(t){this.width=t,this.el.style.width=this.width+"%"},n=function(){var t=new i(this);this.bars.unshift(t)},i=function(i){this.el=document.createElement("div"),this.el.style.backgroundColor=i.opts.bg,this.width=0,this.here=0,this.moving=!1,this.cont=i,t(this.el,a),i.el.appendChild(this.el)},i.prototype.go=function(t){t?(this.here=t,this.moving||(this.moving=!0,s.call(this))):this.moving&&s.call(this)},e=function(i){var e,s,h=this.opts=i||{};h.bg=h.bg||"#000",this.bars=[],e=this.el=document.createElement("div"),t(this.el,o),h.id&&(e.id=h.id),h.className&&(e.className=h.className),e.style.position=h.target?"relative":"fixed",h.target?(s=h.target,s.insertBefore(e,h.target.firstChild)):(s=document.getElementsByTagName("body")[0],s.appendChild(e)),s.className="nanobar-custom-parent",n.call(this)},e.prototype.go=function(t){this.bars[0].go(t),100==t&&n.call(this)},e.prototype.start=function(){(function(){var t=this.bars[0],i=function(){setTimeout(function(){var e=t.here+Math.round(10*Math.random());t.here>=99||(e>99&&(e=99),t.go(e),i())},500)};t.go(10),i()}).call(this)},e.prototype.finish=function(){this.go(100)},e}();`
16
- super `new Nanobar(options)`
17
- end
18
- end if client?
19
-
20
- def progress_bar
21
- $pjax_progress_bar
22
- end
23
-
24
20
  def get href = false
25
- $pjax_progress_bar = Nanobar.new({bg: '#f99f22'}.to_n)
26
- progress_bar.start
27
- `$(document).trigger('page:click')`
21
+ `$(document).trigger('page:get')`
28
22
  $window.history.push href, pjax: true
29
23
  end
30
24
 
@@ -37,16 +31,101 @@ module Wedge
37
31
  end
38
32
  end
39
33
 
34
+ on :ready do
35
+ %x{
36
+ (function() {
37
+ (function($) {
38
+ $("<style type='text/css'>").text("#wedgePjaxLoader{-webkit-box-shadow:0 0 5px #333;-moz-box-shadow:0 0 5px #333;box-shadow:0 0 5px #333;background:#999;height:2px;position:fixed;top:0;width:50px;z-index:9999999}").appendTo("head");
39
+ return $.fn.wedgeLoadingBar = function(options) {
40
+ var settings;
41
+ settings = $.extend({
42
+ turbolinks: true,
43
+ ajax: true
44
+ }, options);
45
+ if (settings.turbolinks) {
46
+ $(document).on('page:fetch', function() {
47
+ return window.wedgePjaxLoader.startLoader();
48
+ });
49
+ $(document).on('page:receive', function() {
50
+ return window.wedgePjaxLoader.sliderWidth = $('#wedgePjaxLoader').width();
51
+ });
52
+ $(document).on('page:load', function() {
53
+ return window.wedgePjaxLoader.restoreLoader();
54
+ });
55
+ $(document).on('page:restore', function() {
56
+ $('#wedgePjaxLoader').remove();
57
+ return window.wedgePjaxLoader.restoreLoader();
58
+ });
59
+ }
60
+ if (settings.ajax) {
61
+ $(document).ajaxComplete(function(e) {
62
+ $('#wedgePjaxLoader').remove();
63
+ return window.wedgePjaxLoader.restoreLoader();
64
+ });
65
+ $(document).ajaxStart(function() {
66
+ return window.wedgePjaxLoader.startLoader();
67
+ });
68
+ }
69
+ return window.wedgePjaxLoader = {
70
+ sliderWidth: 0,
71
+ startLoader: function() {
72
+ $('#wedgePjaxLoader').remove();
73
+ return $('<div/>', {
74
+ id: 'wedgePjaxLoader'
75
+ }).appendTo('body').animate({
76
+ width: $(document).width() * .4
77
+ }, 2000).animate({
78
+ width: $(document).width() * .6
79
+ }, 6000).animate({
80
+ width: $(document).width() * .90
81
+ }, 10000).animate({
82
+ width: $(document).width() * .99
83
+ }, 20000);
84
+ },
85
+ restoreLoader: function() {
86
+ return $('<div/>', {
87
+ id: 'wedgePjaxLoader'
88
+ }).css({
89
+ width: window.wedgePjaxLoader.sliderWidth
90
+ }).appendTo('body').animate({
91
+ width: $(document).width()
92
+ }, 500).fadeOut(function() {
93
+ return $(this).remove();
94
+ });
95
+ }
96
+ };
97
+ };
98
+ })(jQuery);
99
+
100
+ }).call(this);
101
+ }
102
+ `$(window).wedgeLoadingBar({turbolinks: true, ajax: false})`
103
+ end
104
+
40
105
  on :history_change do |e|
41
106
  if e.data.pjax
42
- progress_bar.start
43
- `$(document).trigger('page:request')`
107
+ `$(document).trigger('page:fetch')`
44
108
  HTTP.get(e.url) do |response|
109
+ `$(document).trigger('page:receive')`
45
110
  res = Native(response.xhr)
46
111
  html = res.responseText
112
+ # grab title
113
+ if title = dom.find('head title')
114
+ matches = html.match(/(<title[^>]*>)((.|[\n\r])*)<\/title>/im)
115
+ title.text matches[2]
116
+ end
47
117
  # grab and add the body
48
- matches = html.match(/<body[^>]*>((.|[\n\r])*)<\/body>/im)
49
- dom.find('body').html matches[1]
118
+ matches = html.match(/(<body[^>]*>)((.|[\n\r])*)<\/body>/im)
119
+ # grab the body attributes and set them
120
+ attr_str = matches[1].gsub(/(^<body|>$)/, '').strip
121
+ body = Element['<body/>']
122
+ attr_matches = attr_str.scan(/([a-z\-]*)(?:=)((?:')[^']*(?:')|(?:")[^"]*(?:"))/im)
123
+ attr_matches.each do |match|
124
+ k, v = match
125
+ body.attr(k, v.gsub(/(^("|')|("|')$)/, ''))
126
+ end
127
+ body.html matches[2]
128
+ dom.find('body').replace_with body
50
129
  # grab and eval the scripts
51
130
  matches = html.match(/<script>((.|[\n\r])*)<\/script>/im)
52
131
  # `eval(#{matches[0]})`
@@ -55,7 +134,6 @@ module Wedge
55
134
  script = script.strip.sub('</html>', '').sub('<script>', '')
56
135
  `jQuery.globalEval(script);`
57
136
  end
58
- progress_bar.finish
59
137
  `$('html, body').animate({ scrollTop: 0 }, 0); $(document).trigger('page:load');`
60
138
  end
61
139
  end
@@ -1,3 +1,3 @@
1
1
  module Wedge
2
- VERSION = '0.0.24'
2
+ VERSION = '0.0.25'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wedge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.24
4
+ version: 0.0.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-02 00:00:00.000000000 Z
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -218,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  requirements: []
220
220
  rubyforge_project:
221
- rubygems_version: 2.4.3
221
+ rubygems_version: 2.2.2
222
222
  signing_key:
223
223
  specification_version: 4
224
224
  summary: Components for the Browser and Server
@@ -235,3 +235,4 @@ test_files:
235
235
  - test/test_basic_component.rb
236
236
  - test/test_browserio.rb
237
237
  - test/test_helper.rb
238
+ has_rdoc: