tao_on_rails 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
4
+ * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
+ * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
+ * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
+ * Code distributed by Google as part of the polymer project is also
8
+ * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9
+ */
10
+ (function() {
11
+ 'use strict';
12
+
13
+ // patch all built-in subclasses of HTMLElement to inherit from the new HTMLElement
14
+ // See https://html.spec.whatwg.org/multipage/indices.html#element-interfaces
15
+
16
+ /** @const */
17
+ var htmlElementSubclasses = [
18
+ 'Button',
19
+ 'Canvas',
20
+ 'Data',
21
+ 'Head',
22
+ 'Mod',
23
+ 'TableCell',
24
+ 'TableCol',
25
+ 'Anchor',
26
+ 'Area',
27
+ 'Base',
28
+ 'Body',
29
+ 'BR',
30
+ 'DataList',
31
+ 'Details',
32
+ 'Dialog',
33
+ 'Div',
34
+ 'DList',
35
+ 'Embed',
36
+ 'FieldSet',
37
+ 'Form',
38
+ 'Heading',
39
+ 'HR',
40
+ 'Html',
41
+ 'IFrame',
42
+ 'Image',
43
+ 'Input',
44
+ 'Keygen',
45
+ 'Label',
46
+ 'Legend',
47
+ 'LI',
48
+ 'Link',
49
+ 'Map',
50
+ 'Media',
51
+ 'Menu',
52
+ 'MenuItem',
53
+ 'Meta',
54
+ 'Meter',
55
+ 'Object',
56
+ 'OList',
57
+ 'OptGroup',
58
+ 'Option',
59
+ 'Output',
60
+ 'Paragraph',
61
+ 'Param',
62
+ 'Picture',
63
+ 'Pre',
64
+ 'Progress',
65
+ 'Quote',
66
+ 'Script',
67
+ 'Select',
68
+ 'Slot',
69
+ 'Source',
70
+ 'Span',
71
+ 'Style',
72
+ 'TableCaption',
73
+ 'Table',
74
+ 'TableRow',
75
+ 'TableSection',
76
+ 'Template',
77
+ 'TextArea',
78
+ 'Time',
79
+ 'Title',
80
+ 'Track',
81
+ 'UList',
82
+ 'Unknown',
83
+ ];
84
+
85
+ for (var i = 0; i < htmlElementSubclasses.length; i++) {
86
+ var ctor = window['HTML' + htmlElementSubclasses[i] + 'Element'];
87
+ if (ctor) {
88
+ ctor.prototype.__proto__ = window.HTMLElement.prototype;
89
+ }
90
+ }
91
+ })();
@@ -0,0 +1,93 @@
1
+
2
+ if window.customElements && !customElements.polyfilled
3
+ eval '''
4
+ (() => {
5
+ 'use strict';
6
+
7
+ const NativeHTMLElement = window.HTMLElement;
8
+ const nativeDefine = window.customElements.define;
9
+ const nativeGet = window.customElements.get;
10
+
11
+ /**
12
+ * Map of user-provided constructors to tag names.
13
+ *
14
+ * @type {Map<Function, string>}
15
+ */
16
+ const tagnameByConstructor = new Map();
17
+
18
+ /**
19
+ * Map of tag anmes to user-provided constructors.
20
+ *
21
+ * @type {Map<string, Function>}
22
+ */
23
+ const constructorByTagname = new Map();
24
+
25
+
26
+ /**
27
+ * Whether the constructors are being called by a browser process, ie parsing
28
+ * or createElement.
29
+ */
30
+ let browserConstruction = false;
31
+
32
+ /**
33
+ * Whether the constructors are being called by a user-space process, ie
34
+ * calling an element constructor.
35
+ */
36
+ let userConstruction = false;
37
+
38
+ window.HTMLElement = function() {
39
+ if (!browserConstruction) {
40
+ const tagname = tagnameByConstructor.get(this.constructor);
41
+ const fakeClass = nativeGet.call(window.customElements, tagname);
42
+
43
+ // Make sure that the fake constructor doesn't call back to this constructor
44
+ userConstruction = true;
45
+ const instance = new (fakeClass)();
46
+ return instance;
47
+ }
48
+ // Else do nothing. This will be reached by ES5-style classes doing
49
+ // HTMLElement.call() during initialization
50
+ browserConstruction = false;
51
+ };
52
+
53
+ window.HTMLElement.prototype = Object.create(NativeHTMLElement.prototype);
54
+ window.HTMLElement.prototype.constructor = window.HTMLElement;
55
+
56
+ window.customElements.define = (tagname, elementClass) => {
57
+ const elementProto = elementClass.prototype;
58
+ const StandInElement = class extends NativeHTMLElement {
59
+ constructor() {
60
+ // Call the native HTMLElement constructor, this gives us the
61
+ // under-construction instance as `this`:
62
+ super();
63
+
64
+ // The prototype will be wrong up because the browser used our fake
65
+ // class, so fix it:
66
+ Object.setPrototypeOf(this, elementProto);
67
+
68
+ if (!userConstruction) {
69
+ // Make sure that user-defined constructor bottom's out to a do-nothing
70
+ // HTMLElement() call
71
+ browserConstruction = true;
72
+ // Call the user-defined constructor on our instance:
73
+ elementClass.call(this);
74
+ }
75
+ userConstruction = false;
76
+ }
77
+ };
78
+ const standInProto = StandInElement.prototype;
79
+ StandInElement.observedAttributes = elementClass.observedAttributes;
80
+ standInProto.connectedCallback = elementProto.connectedCallback;
81
+ standInProto.disconnectedCallback = elementProto.disconnectedCallback;
82
+ standInProto.attributeChangedCallback = elementProto.attributeChangedCallback;
83
+ standInProto.adoptedCallback = elementProto.adoptedCallback;
84
+
85
+ tagnameByConstructor.set(elementClass, tagname);
86
+ constructorByTagname.set(tagname, elementClass);
87
+ nativeDefine.call(window.customElements, tagname, StandInElement);
88
+ };
89
+
90
+ window.customElements.get = (tagname) => constructorByTagname.get(tagname);
91
+
92
+ })();
93
+ '''
@@ -0,0 +1,4 @@
1
+ #= require ./es6
2
+ #= require ./custom-elements
3
+ #= require ./native-shim
4
+ #= require ./htmlelement-instanceof
@@ -6,7 +6,7 @@ class TaoApplication extends TaoModule
6
6
  @_initGon()
7
7
  @_initI18n()
8
8
  @_initUjs()
9
- @_bind()
9
+ @_initTurbolinks()
10
10
 
11
11
  _initGon: ->
12
12
  $.extend @, window.gon
@@ -21,7 +21,7 @@ class TaoApplication extends TaoModule
21
21
  $(@).removeClass 'disabled'
22
22
 
23
23
  _initI18n: ->
24
- I18n.locale = @locale
24
+ I18n?.locale = @locale
25
25
 
26
26
  _initIcons: ($page) ->
27
27
  $icons = $page.siblings('#tao-icons')
@@ -31,7 +31,7 @@ class TaoApplication extends TaoModule
31
31
  _initPage: ($page) ->
32
32
  window.currentPage = @currentPage = $page[0]
33
33
 
34
- _bind: ->
34
+ _initTurbolinks: ->
35
35
  $(document).on 'turbolinks:before-visit', (e) =>
36
36
  if @currentPage.trigger('before-leave') == false
37
37
  e.preventDefault()
@@ -46,8 +46,8 @@ class TaoApplication extends TaoModule
46
46
  @trigger 'page-visit', [e.originalEvent?.data.url]
47
47
 
48
48
  .on 'turbolinks:before-cache', (e) =>
49
- @currentPage.trigger 'before-cache'
50
49
  @trigger 'before-page-cache', [@currentPage]
50
+ @currentPage.prepareCache?()
51
51
  window.currentPage = @currentPage = null
52
52
 
53
53
  .on 'turbolinks:request-end', (e) =>
@@ -1,6 +1,5 @@
1
1
  #= require lodash
2
- #= require custom-elements
3
- #= require native-shim
2
+ #= require polyfills/polyfills
4
3
 
5
4
  components = {}
6
5
 
@@ -39,28 +38,41 @@ TaoComponentBasedOn = (superClass = 'HTMLElement') ->
39
38
  set: setMethod
40
39
  configurable: true
41
40
 
42
- @property: (name, observed) ->
41
+ @property: (name, options = {}) ->
43
42
  attrName = _.kebabCase(name)
44
- @get name, -> @getAttribute attrName
43
+ @get name, ->
44
+ if @hasAttribute attrName
45
+ @getAttribute(attrName) || true
46
+ else
47
+ false
45
48
  @set name, (val) ->
46
49
  if val == true
47
50
  @setAttribute attrName, ''
48
- else if val == false
51
+ else if val != false
49
52
  @setAttribute attrName, val
50
53
  else
51
54
  @removeAttribute attrName
52
- @observedAttributes.push(attrName) if observed
55
+ @observedAttributes.push(attrName) if options.observe
56
+
57
+ @tag: '' # to be set by child class
58
+
59
+ @register: (componentClass) ->
60
+ return unless componentClass.tag
61
+ customElements.define componentClass.tag, componentClass
53
62
 
54
63
  @observedAttributes: []
55
64
 
56
65
  connectedCallback: ->
66
+ @connected = true
67
+ @classList.add 'tao-component'
57
68
  @_init()
58
69
 
59
70
  disconnectedCallback: ->
71
+ @connected = false
60
72
  @_destroy()
61
73
 
62
74
  attributeChangedCallback: (attrName, oldValue, newValue) ->
63
- @["#{_.camelCase attrName}Changed"]?(newValue, oldValue)
75
+ @["_#{_.camelCase attrName}Changed"]?(newValue, oldValue)
64
76
 
65
77
  on: (args...) ->
66
78
  $(@).on args...
@@ -77,8 +89,11 @@ TaoComponentBasedOn = (superClass = 'HTMLElement') ->
77
89
  _init: ->
78
90
  # to be implemented
79
91
 
80
- _destory: ->
92
+ _destroy: ->
81
93
  # to be implemented
82
94
 
95
+ prepareCache: ->
96
+ # called before turbolinks cache pages
97
+
83
98
  window.TaoComponentBasedOn = TaoComponentBasedOn
84
99
  window.TaoComponent = TaoComponentBasedOn 'HTMLElement'
@@ -0,0 +1,2 @@
1
+ # generate this file by exec command in project:
2
+ # rake tao:generate_icons
@@ -40,3 +40,5 @@ class TaoModule
40
40
 
41
41
  one: (args...) ->
42
42
  $(@).one args...
43
+
44
+ window.TaoModule = TaoModule
@@ -4,10 +4,9 @@ class TaoPage extends TaoComponent
4
4
 
5
5
  @property 'layout'
6
6
 
7
- _init: ->
8
- # to be implemented
9
-
10
- _destroy: ->
11
- # to be implemented
7
+ prepareCache: ->
8
+ $(@).find('.tao-component').each (i, el) =>
9
+ el.prepareCache?()
10
+ null
12
11
 
13
12
  window.TaoPage = TaoPage
@@ -5,9 +5,9 @@
5
5
  #= require i18n
6
6
 
7
7
  #= require_self
8
- #= require_tree tao/application
9
- #= require_tree tao/page
10
- #= require_tree tao/helpers
11
- #= require_tree tao/icons
8
+ #= require tao/application
9
+ #= require tao/page
10
+ #= require tao/helpers
11
+ #= require tao/icons
12
12
 
13
13
  window.tao = {}
@@ -1,25 +1,15 @@
1
- @import 'base';
2
1
 
3
2
  @keyframes spin {
4
3
  from { transform:rotate(0deg); }
5
4
  to { transform:rotate(360deg); }
6
5
  }
7
6
 
8
- #tao-icons {
9
- symbol {
10
- .zhiren_icon,
11
- .Main {
12
- fill: inherit;
13
- }
14
- }
15
- }
16
-
17
7
  .icon {
18
8
  display: inline-block;
19
9
  width: 24px;
20
10
  height: 24px;
21
11
  vertical-align: middle;
22
- fill: $color-light-text;
12
+ fill: #9b9b9b;
23
13
 
24
14
  &.spin {
25
15
  animation: spin 1s infinite linear;
@@ -1,2 +1,2 @@
1
1
  //= require normalize-rails
2
- //= require_tree tao/icons
2
+ //= require tao/icons
@@ -1,4 +1,7 @@
1
+ require 'turbolinks'
1
2
  require 'jquery-rails'
3
+ require 'lodash-rails'
4
+ require 'normalize-rails'
2
5
 
3
6
  module TaoOnRails
4
7
  module Rails
@@ -1,5 +1,5 @@
1
1
  module TaoOnRails
2
2
  module Rails
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -33,6 +33,7 @@ namespace :tao do
33
33
  .gsub(/id=/,'class=')
34
34
  .gsub(/<svg.+?>/, %Q{<svg id="icon-#{name}" #{dimensions(content)}>})
35
35
  .gsub(/svg/,'symbol')
36
+ .gsub(/\sfill=".+?"/,'')
36
37
  .gsub(/\n/, '') # Remove endlines
37
38
  .gsub(/\s{2,}/, ' ') # Remove whitespace
38
39
  .gsub(/>\s+</, '><') # Remove whitespace between tags
@@ -40,15 +41,7 @@ namespace :tao do
40
41
 
41
42
  def dimensions(content)
42
43
  dimension = content.scan(/<svg.+(viewBox=["'](.+?)["'])/).flatten
43
- viewbox = dimension.first
44
- #coords = dimension.last.split(' ')
45
-
46
- #width = coords[2].to_i - coords[0].to_i
47
- #height = coords[3].to_i - coords[1].to_i
48
- #hack android svg
49
- width = '100%'
50
- height = '100%'
51
- %Q{#{viewbox} width="#{width}" height="#{height}"}
44
+ %Q{#{dimension.first} width="100%" height="100%"}
52
45
  end
53
46
 
54
47
  end
data/tao_on_rails.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Siyuan Liu", "Terry Tai"]
10
10
  spec.email = ["farthinker@mycolorway.com", "t@mycolorway.com"]
11
11
 
12
- spec.summary = %q{The missing frontend solucation for Rails project}
13
- spec.description = %q{The missing frontend solucation for Rails project}
12
+ spec.summary = %q{The missing frontend solution for Rails project}
13
+ spec.description = %q{Ruby on Rails lacks a recommended way to structure your frontend code for many years. Tao on Rails is the framework to fill the gap which will modularize your page with the new Custom Elements v1 API.}
14
14
  spec.homepage = "https://github.com/mycolorway/tao_on_rails"
15
15
  spec.license = "MIT"
16
16
 
@@ -21,15 +21,14 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_dependency "rails", "~> 5.0"
25
- spec.add_dependency "jquery-rails", "~> 4.2"
26
- spec.add_dependency "lodash-rails", "~> 4.16"
27
- spec.add_dependency "normalize-rails", "~> 4.1"
28
- spec.add_dependency "turbolinks", "~> 5.0"
29
- spec.add_dependency "i18n-js", "~> 3.0.0.rc14"
30
- spec.add_dependency "gon", "~> 6.0"
24
+ spec.add_runtime_dependency "turbolinks", "~> 5.0"
25
+ spec.add_runtime_dependency "jquery-rails", "~> 4.2"
26
+ spec.add_runtime_dependency "lodash-rails", "~> 4.16"
27
+ spec.add_runtime_dependency "normalize-rails", "~> 4.1"
28
+ spec.add_runtime_dependency "rails", '~> 5.0'
31
29
 
32
30
  spec.add_development_dependency "bundler", "~> 1.13"
33
31
  spec.add_development_dependency "rake", "~> 10.0"
34
- spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "minitest", "~> 5.0"
33
+ spec.add_development_dependency "blade", "~> 0.7.0"
35
34
  end