zfben_libjs 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -77,11 +77,13 @@ lib = ->
77
77
 
78
78
  if pending_css.length > 0
79
79
  loading_css = []
80
+
80
81
  if lib.defaults.version
81
82
  for url in pending_css
82
83
  loading_css.push url + lib.defaults.version
83
84
  else
84
85
  loading_css = pending_css
86
+
85
87
  LazyLoad.css(loading_css, ->
86
88
  for url in pending_css
87
89
  delete pending_urls[url]
@@ -97,13 +99,15 @@ lib = ->
97
99
  pending_urls[url] = true
98
100
  pending_js.push url
99
101
 
100
- if js.length > 0
102
+ if pending_js.length > 0
101
103
  loading_js = []
104
+
102
105
  if lib.defaults.version
103
106
  for url in pending_js
104
107
  loading_js.push url + lib.defaults.version
105
108
  else
106
109
  loading_js = pending_js
110
+
107
111
  LazyLoad.js(loading_js, ->
108
112
  for url in pending_js
109
113
  delete pending_urls[url]
@@ -114,7 +118,8 @@ lib = ->
114
118
  # put funcs to pending_funcs
115
119
  if funcs.length > 0
116
120
  pending_funcs[urls.join(' ')] = funcs
117
- run_funcs()
121
+
122
+ run_funcs()
118
123
 
119
124
  return {
120
125
  css: css
@@ -161,4 +166,39 @@ lib.libs = (new_libs)->
161
166
 
162
167
  lib.defaults = {}
163
168
 
169
+ # Route
170
+ routes = {}
171
+ last_location = ''
172
+
173
+ path2regex = (path)->
174
+ if path[0] is '/' && path[path.length - 1] is '/'
175
+ path = path[1..(path.length - 2)]
176
+ return path
177
+
178
+ lib.routes = (method, args)->
179
+ switch method
180
+ when 'add'
181
+ for path, lib_name of args
182
+ routes[path2regex(path)] = lib_name
183
+ last_location = ''
184
+ when 'del'
185
+ for path, lib_name of args
186
+ delete(routes[path2regex(path)])
187
+ return routes
188
+
189
+ Location_watch = ->
190
+ location_path = location.hostname + location.pathname + location.hash
191
+ if location_path isnt last_location
192
+ libs_name = []
193
+ for path, lib_name of routes
194
+ if RegExp(path).test location_path
195
+ libs_name.push lib_name
196
+ if libs_name.length > 0
197
+ lib(libs_name.join(' '))
198
+ last_location = location_path
199
+ setTimeout(->
200
+ Location_watch()
201
+ , 200)
202
+
164
203
  window.lib = lib
204
+ Location_watch()
data/lib/zfben_libjs.rb CHANGED
@@ -71,6 +71,8 @@ module Zfben_libjs
71
71
 
72
72
  @bundle = @data['bundle']
73
73
 
74
+ @routes = @data['routes']
75
+
74
76
  @preload = @data['preload']
75
77
 
76
78
  if @config.has_key?('before')
@@ -202,10 +204,14 @@ module Zfben_libjs
202
204
  if type == 'stylesheets' && @config['changeImageUrl'] && reg =~ File.read(path)
203
205
  css = File.read(path).partition_all(reg).map{ |f|
204
206
  if reg =~ f
205
- if @config['url'] == ''
206
- f = 'url("../images/' << File.basename(f.match(reg)[1]) << '")'
207
- else
208
- f = 'url("' + @config['url/images'] + File.basename(f.match(reg)[1]) << '")'
207
+ filename = File.basename(f.match(reg)[1])
208
+ if File.exists?(File.join(@config['src/images'], filename))
209
+ if @config['url'] == ''
210
+ url = '../images/' << File.basename(f.match(reg)[1])
211
+ else
212
+ url = @config['url/images'] + File.basename(f.match(reg)[1])
213
+ end
214
+ f = 'url("' << url << '")'
209
215
  end
210
216
  end
211
217
  f
@@ -314,6 +320,15 @@ module Zfben_libjs
314
320
  libjs << "\n/* bundle */\nlib.libs(#{@bundle.to_json});"
315
321
  end
316
322
 
323
+ if defined?(@routes)
324
+ routes = {}
325
+ @routes.each do |path, lib_name|
326
+ lib_name = lib_name.join ' ' if lib_name.class == Array
327
+ routes[path] = lib_name
328
+ end
329
+ libjs << "\n/* routes */\nlib.routes('add', #{routes.to_json});"
330
+ end
331
+
317
332
  if @preload.class == Array && @preload.length > 0
318
333
  libjs << "\n/* preload */\nlib('#{@preload.join(' ')}');"
319
334
  end
@@ -334,10 +349,11 @@ module Zfben_libjs
334
349
  if defined?(Rails)
335
350
  module Rails
336
351
  module ActionView::Helpers::AssetTagHelper
337
- def lib *opt
352
+ def lib *opts
353
+ p opts
338
354
  html = '<script src="/javascripts/lib.js' + Lib_version + '"></script>'
339
- unless opt[0].blank?
340
- html += '<script>lib("' + opt[0].to_s + '");</script>'
355
+ unless opts.blank?
356
+ html += "<script>lib('#{opts.join(' ')}')</script>"
341
357
  end
342
358
  return html
343
359
  end
@@ -51,7 +51,9 @@ test('lib.libs', 6, function(){
51
51
  '/javascripts/support_filetype.js'
52
52
  ],
53
53
  unload: [ '/javascripts/unload.js' ],
54
- load_times: [ '/javascripts/load_times.js' ]
54
+ load_times: [ '/javascripts/load_times.js' ],
55
+ route_string: [ '/javascripts/route_string.js' ],
56
+ route_regexp: [ '/javascripts/route_regexp.js' ]
55
57
  }, 'lib.libs is ok');
56
58
 
57
59
  equal(typeof lib.lazyload, 'function', 'lib.lazyload is a function');
@@ -64,6 +66,8 @@ test('lib.libs', 6, function(){
64
66
  ],
65
67
  unload: [ '/javascripts/unload.js' ],
66
68
  load_times: [ '/javascripts/load_times.js' ],
69
+ route_string: [ '/javascripts/route_string.js' ],
70
+ route_regexp: [ '/javascripts/route_regexp.js' ],
67
71
  test: 'test'
68
72
  }, "lib.libs({test: 'test'}) has been added");
69
73
 
@@ -76,7 +80,9 @@ test('lib.libs', 6, function(){
76
80
  '/javascripts/support_filetype.js'
77
81
  ],
78
82
  unload: [ '/javascripts/unload.js' ],
79
- load_times: [ '/javascripts/load_times.js' ]
83
+ load_times: [ '/javascripts/load_times.js' ],
84
+ route_string: [ '/javascripts/route_string.js' ],
85
+ route_regexp: [ '/javascripts/route_regexp.js' ]
80
86
  }, "lib.libs({test: null}) has been deleted");
81
87
 
82
88
  equal(typeof lib.test, 'undefined', 'lib.test is undefined');
@@ -108,10 +114,10 @@ module('support_filetype');
108
114
 
109
115
  asyncTest('stylesheet', 4, function() {
110
116
  lib.support_filetype(function(){
111
- equal($('.css').css('font-size'), '1px', 'css file loaded');
112
- equal($('.sass').css('font-size'), '2px', 'sass file loaded');
113
- equal($('.scss').css('font-size'), '3px', 'scss file loaded');
114
- equal($('.rb_css').css('font-size'), '4px', 'rb_css file loaded');
117
+ equal($('.css').css('color'), 'rgb(0, 0, 1)', 'css file loaded');
118
+ equal($('.sass').css('color'), 'rgb(0, 0, 2)', 'sass file loaded');
119
+ equal($('.scss').css('color'), 'rgb(0, 0, 3)', 'scss file loaded');
120
+ equal($('.rb_css').css('color'), 'rgb(0, 0, 4)', 'rb_css file loaded');
115
121
  start();
116
122
  });
117
123
  });
@@ -124,3 +130,24 @@ asyncTest('javascript', 3, function() {
124
130
  start();
125
131
  });
126
132
  });
133
+
134
+
135
+ module('lib.routes');
136
+
137
+ asyncTest('String route', 1, function(){
138
+ location.hash = '#Test_StringRoute';
139
+
140
+ setTimeout(function(){
141
+ ok(route_string, 'String route is applied');
142
+ start();
143
+ }, 1000);
144
+ });
145
+
146
+ asyncTest('RegExp route', 1, function(){
147
+ location.hash = '#Test_RegExpRoute';
148
+
149
+ setTimeout(function(){
150
+ ok(route_regexp, 'RegExp route is applied');
151
+ start();
152
+ }, 1000);
153
+ });
@@ -0,0 +1 @@
1
+ route_regexp = true;
@@ -0,0 +1 @@
1
+ route_string = true;
@@ -1 +1 @@
1
- .css{ font-size: 1px }
1
+ .css{ color: rgb(0,0,1) !important }
@@ -1 +1 @@
1
- { :css => '.rb_css{ font-size: 4px }' }
1
+ { :css => '.rb_css{ color: rgb(0,0,4) !important }' }
@@ -1,2 +1,2 @@
1
1
  .sass
2
- font-size: 2px
2
+ color: rgb(0,0,2) !important
@@ -1,3 +1,3 @@
1
1
  .scss{
2
- font-size: 3px
2
+ color: rgb(0,0,3) !important
3
3
  }
data/test.yml CHANGED
@@ -8,9 +8,16 @@ config:
8
8
 
9
9
  # Files to be download and writen in lib.js
10
10
  libs:
11
- support_filetype:
12
- - test/support_filetype/*
13
- unload:
14
- - test/unload.js
15
- load_times:
16
- - test/load_times.js
11
+ support_filetype: test/support_filetype/*
12
+
13
+ unload: test/unload.js
14
+
15
+ load_times: test/load_times.js
16
+
17
+ route_string: test/route_string.js
18
+
19
+ route_regexp: test/route_regexp.js
20
+
21
+ routes:
22
+ "#Test_StringRoute": route_string
23
+ "/#Test_RegExpRoute/": route_regexp
data/zfben_libjs.gemspec CHANGED
@@ -2,8 +2,8 @@
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.name = "zfben_libjs"
6
- s.version = '0.0.9'
5
+ s.name = 'zfben_libjs'
6
+ s.version = '0.0.10'
7
7
  s.authors = ["Ben"]
8
8
  s.email = ["ben@zfben.com"]
9
9
  s.homepage = "https://github.com/benz303/zfben_libjs"
@@ -18,8 +18,6 @@ Gem::Specification.new do |s|
18
18
  ********************************************************************************
19
19
  }
20
20
 
21
- s.rubyforge_project = "zfben_libjs"
22
-
23
21
  s.files = `git ls-files`.split("\n")
24
22
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
23
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zfben_libjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-18 00:00:00.000000000Z
12
+ date: 2011-08-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rainbow
16
- requirement: &9024340 !ruby/object:Gem::Requirement
16
+ requirement: &17698840 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *9024340
24
+ version_requirements: *17698840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &9023880 !ruby/object:Gem::Requirement
27
+ requirement: &17698380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *9023880
35
+ version_requirements: *17698380
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: compass
38
- requirement: &9023460 !ruby/object:Gem::Requirement
38
+ requirement: &17697960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *9023460
46
+ version_requirements: *17697960
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: coffee-script
49
- requirement: &9023040 !ruby/object:Gem::Requirement
49
+ requirement: &17697540 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *9023040
57
+ version_requirements: *17697540
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: uglifier
60
- requirement: &9022620 !ruby/object:Gem::Requirement
60
+ requirement: &17697120 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *9022620
68
+ version_requirements: *17697120
69
69
  description: ''
70
70
  email:
71
71
  - ben@zfben.com
@@ -88,6 +88,8 @@ files:
88
88
  - test/javascript/lib_test.js
89
89
  - test/javascript/templates/test_case.erb
90
90
  - test/load_times.js
91
+ - test/route_regexp.js
92
+ - test/route_string.js
91
93
  - test/support_filetype/coffee.coffee
92
94
  - test/support_filetype/css.css
93
95
  - test/support_filetype/js.js
@@ -118,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
120
  - !ruby/object:Gem::Version
119
121
  version: '0'
120
122
  requirements: []
121
- rubyforge_project: zfben_libjs
123
+ rubyforge_project:
122
124
  rubygems_version: 1.8.6
123
125
  signing_key:
124
126
  specification_version: 3