fron 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ module DOM
2
+ module ClassList
3
+ def addClass(*classes)
4
+ classes.each do |cls|
5
+ `#{@el}.classList.add(#{cls})`
6
+ end
7
+ end
8
+
9
+ def removeClass(*classes)
10
+ classes.each do |cls|
11
+ `#{@el}.classList.remove(#{cls})`
12
+ end
13
+ end
14
+
15
+ def hasClass(cls)
16
+ `#{@el}.classList.contains(#{cls})`
17
+ end
18
+
19
+ def toggleClass(cls,value)
20
+ if value
21
+ addClass cls
22
+ else
23
+ removeClass cls
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ require 'native'
2
+
3
+ module DOM
4
+ module Dimensions
5
+
6
+ def top
7
+ clientRect[:top] + Window.scrollY
8
+ end
9
+
10
+ def left
11
+ clientRect[:left] + Window.scrollX
12
+ end
13
+
14
+ def right
15
+ clientRect[:right] + Window.scrollX
16
+ end
17
+
18
+ def bottom
19
+ clientRect[:bottom] + Window.scrollY
20
+ end
21
+
22
+ def width
23
+ clientRect[:width]
24
+ end
25
+
26
+ def height
27
+
28
+ clientRect[:height]
29
+ end
30
+
31
+ private
32
+
33
+ def clientRect
34
+ Hash.new `JSON.parse(JSON.stringify(#{@el}.getBoundingClientRect()))`
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ module DOM
2
+ module Events
3
+ def on(event, &listener)
4
+ `#{@el}.addEventListener(#{event},function(e){#{ listener.call Event.new(`e`)}})`
5
+ self
6
+ end
7
+
8
+ def delegate(event,selector, &listener)
9
+ %x{
10
+ #{@el}.addEventListener(#{event},function(e){
11
+ if(e.target.webkitMatchesSelector(#{selector})){
12
+ #{ listener.call Event.new(`e`)}
13
+ }
14
+ },true)
15
+ }
16
+ self
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,79 @@
1
+ module DOM
2
+ module Node
3
+ include Events
4
+
5
+ # Cloning
6
+ # ---------------------------------------
7
+ def dup
8
+ self.class.new `#{@el}.cloneNode()`
9
+ end
10
+
11
+ def dup!
12
+ self.class.new `#{@el}.cloneNode(true)`
13
+ end
14
+
15
+ # Hierarchy
16
+ # ---------------------------------------
17
+ def parentNode
18
+ DOM::Node.new `#{@el}.parentNode`
19
+ end
20
+
21
+ def parent
22
+ DOM::Node.new `#{@el}.parentElement`
23
+ end
24
+
25
+ def empty?
26
+ `#{@el}.childNodes.length === 0`
27
+ end
28
+
29
+ def children
30
+ `Array.prototype.slice.call(#{@el}.childNodes)`
31
+ end
32
+
33
+ # Remove
34
+ # ---------------------------------------
35
+ def remove(el)
36
+ `#{@el}.removeChild(#{getEl el})`
37
+ end
38
+
39
+ def remove!
40
+ return unless parent
41
+ parent.remove self
42
+ end
43
+
44
+ # Hierarchy Manipulation
45
+ # ---------------------------------------
46
+ def << el
47
+ `#{@el}.appendChild(#{getEl el})`
48
+ self
49
+ end
50
+
51
+ def >> el
52
+ `#{getEl el}.appendChild(#{@el})`
53
+ end
54
+
55
+ def insertBefore(what,where)
56
+ `#{@el}.insertBefore(#{what},#{where})`
57
+ end
58
+
59
+ # Text manipulation
60
+ # ---------------------------------------
61
+ def text
62
+ `#{@el}.textContent`
63
+ end
64
+
65
+ def text=(text)
66
+ `#{@el}.textContent = #{text}`
67
+ end
68
+
69
+ def normalize
70
+ `#{@el}.normalize()`
71
+ end
72
+
73
+ private
74
+
75
+ def getEl(obj)
76
+ obj.is_a?(Node) ? obj.instance_variable_get('@el') : obj
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,21 @@
1
+ class Style
2
+ def initialize(el)
3
+ @el = el
4
+ end
5
+
6
+ def method_missing(name,value)
7
+ if name =~ /\=$/
8
+ self[name[0..-2]] = value
9
+ else
10
+ self[name]
11
+ end
12
+ end
13
+
14
+ def [](prop)
15
+ `#{@el}.style[#{prop}]`
16
+ end
17
+
18
+ def []=(prop,value)
19
+ `#{@el}.style[#{prop}] = #{value}`
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module DOM
2
+ class Text
3
+ include Node
4
+
5
+ def initialize(data)
6
+ @el = `typeof #{data} === 'string'` ? `document.createTextNode(#{data})` : data
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module DOM
2
+ module Window
3
+ def self.on(event, &listener)
4
+ `window.addEventListener(#{event},#{listener})`
5
+ end
6
+
7
+ def self.hash
8
+ `window.location.hash.slice(1)`
9
+ end
10
+
11
+ def self.hash=(value)
12
+ `window.location.hash = #{value}`
13
+ end
14
+
15
+ def self.scrollY
16
+ `window.scrollY || document.documentElement.scrollTop`
17
+ end
18
+
19
+ def self.scrollX
20
+ `window.scrollX || document.documentElement.scrollTop`
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ require './core-ext/hash'
2
+ require './request/response'
3
+ require './request/request'
@@ -0,0 +1,51 @@
1
+ require 'json'
2
+
3
+ module Fron
4
+ class Request
5
+ attr_accessor :url, :headers
6
+
7
+ def initialize(url, headers = {})
8
+ @url = url
9
+ @request = `new XMLHttpRequest()`
10
+ `#{@request}.addEventListener('readystatechange' , function(){#{handle_state_change}})`
11
+ self
12
+ end
13
+
14
+ def handle_state_change
15
+ if ready_state == 4
16
+ response = Response.new `#{@request}.status`, `#{@request}.response`, `#{@request}.getAllResponseHeaders()`
17
+ @callback.call response
18
+ end
19
+ end
20
+
21
+ def ready_state
22
+ `#{@request}.readyState`
23
+ end
24
+
25
+ def request( method = 'GET',data = nil, &callback)
26
+ if ready_state == 0 or ready_state == 4
27
+ if method.upcase == "GET" && data
28
+ `#{@request}.open(#{method},#{@url+"?"+data.to_query_string})`
29
+ else
30
+ `#{@request}.open(#{method},#{@url})`
31
+ end
32
+ @callback = callback
33
+ `#{@request}.send(#{data.to_form_data if data})`
34
+ else
35
+ raise "The request is already running!"
36
+ end
37
+ end
38
+
39
+ def get(data, &callback)
40
+ request 'GET', data, &callback
41
+ end
42
+
43
+ def post(data, &callback)
44
+ request 'POST', data, &callback
45
+ end
46
+
47
+ def put(data, &callback)
48
+ request 'PUT', data, &callback
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ module Fron
2
+ class Response
3
+ attr_reader :body, :headers, :status
4
+
5
+ def initialize(status, body, headers)
6
+ @body = body
7
+ @status = status
8
+ @headers = {}
9
+
10
+ headers.strip.split(/\n/).each do |item|
11
+ match = item.split(/:/)
12
+ @headers[match[0]] = match[1].strip
13
+ end
14
+ end
15
+
16
+ def content_type
17
+ @headers['Content-Type']
18
+ end
19
+
20
+ def ok?
21
+ @status == 200
22
+ end
23
+
24
+ def json
25
+ JSON.parse @body
26
+ end
27
+
28
+ def dom
29
+ d = DOM::Element.new 'div'
30
+ d.html = @body
31
+ f = DOM::Fragment.new()
32
+ f << d
33
+ f
34
+ end
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ require './storage/local-storage'
@@ -0,0 +1,18 @@
1
+ module LocalStorage
2
+ def self.get(key)
3
+ value = `window.localStorage.getItem(#{key}) || false`
4
+ value ? JSON.parse(value) : nil
5
+ end
6
+
7
+ def self.set(key, data)
8
+ `window.localStorage.setItem(#{key},#{data.to_json})`
9
+ end
10
+
11
+ def self.keys
12
+ `ret = []; for (var key in localStorage){ ret.push(key) }; return ret;`
13
+ end
14
+
15
+ def self.all
16
+ self.keys.map{ |key| self.get key }
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gusztav Szikszai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: opal-rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.0.beta3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.0.beta3
41
+ description: Frontend Application Framework that uses Opal
42
+ email: gusztav.szikszai@digitalnatives.hu
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - docs/application.md
48
+ - docs/configuration.md
49
+ - docs/controllers.md
50
+ - docs/routing.md
51
+ - fron.gemspec
52
+ - lib/fron.rb
53
+ - lib/fron/version.rb
54
+ - opal/fron.rb
55
+ - opal/fron/core-ext/hash.rb
56
+ - opal/fron/core.rb
57
+ - opal/fron/core/adapters/local-storage.rb
58
+ - opal/fron/core/adapters/rails.rb
59
+ - opal/fron/core/application.rb
60
+ - opal/fron/core/component.rb
61
+ - opal/fron/core/configuration.rb
62
+ - opal/fron/core/controller.rb
63
+ - opal/fron/core/eventable.rb
64
+ - opal/fron/core/logger.rb
65
+ - opal/fron/core/model.rb
66
+ - opal/fron/core/router.rb
67
+ - opal/fron/dom.rb
68
+ - opal/fron/dom/document.rb
69
+ - opal/fron/dom/element.rb
70
+ - opal/fron/dom/event.rb
71
+ - opal/fron/dom/fragment.rb
72
+ - opal/fron/dom/modules/classlist.rb
73
+ - opal/fron/dom/modules/dimensions.rb
74
+ - opal/fron/dom/modules/events.rb
75
+ - opal/fron/dom/node.rb
76
+ - opal/fron/dom/style.rb
77
+ - opal/fron/dom/text.rb
78
+ - opal/fron/dom/window.rb
79
+ - opal/fron/request.rb
80
+ - opal/fron/request/request.rb
81
+ - opal/fron/request/response.rb
82
+ - opal/fron/storage.rb
83
+ - opal/fron/storage/local-storage.rb
84
+ homepage: ''
85
+ licenses: []
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.1.11
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Frontend Application Framework
107
+ test_files: []