glib-web 0.0.8 → 0.0.9

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: 58e7303d43cb79ad1bb67de2ee06a41928010ba7
4
- data.tar.gz: f9152dfd8538f68686e6bfbf3a26bfa8514715d0
3
+ metadata.gz: 3f1f2c691c60bbb4eaa96f531d11385e007d59a6
4
+ data.tar.gz: c86e3560a6c66421d717dd3b4527a041ad6e8109
5
5
  SHA512:
6
- metadata.gz: 7fefed6116c08f30ef5f042d7dc58f2d63d48d1050f61209664d371b438d6c6058e8f79afa9013b9bcf9c479d9ef3c5d140393e7cf07a4e994c0d034921e2828
7
- data.tar.gz: a75b13fead8a276bb14aee83e329b95ceee3ad4eef35aed8bd4e614891f27ece990d0f59367c9819f774fc5b1fd025b2898893f43863927127ba6d95349c8d61
6
+ metadata.gz: d1e4c57ee97b85249b859605919eb50ada249aea3cd59618c59e2d6e9799e5f08cb5557c80513ac031b591177f6a57a481754e2f660af74bc4797c99e8b66e00
7
+ data.tar.gz: 1245782f81ef3925b007b27384d6e8928a3ef68dffeb6f174e92371486078badb4d477b91057f588cdddcfc8d4db38ea091f9aaf4e2146a0d12b108190207631
@@ -0,0 +1,23 @@
1
+ module Glib
2
+ class HomeController < ApplicationController
3
+
4
+ def json_ui_garage
5
+ @path_prefix = 'json_ui/garage'
6
+ @sample_image_url = 'https://cdn-images-1.medium.com/max/1200/1*Qc0XxYm-qAZL-7tjjlNfrg.png'
7
+
8
+ # We can't use prepend_view_path because it affects the app, not the gem
9
+ path = "#{@path_prefix}/#{params[:path] || 'home/index'}"
10
+ render path
11
+ end
12
+
13
+ def json_ui_garage_url(options = {})
14
+ Glib::Web::Engine.routes.url_helpers.json_ui_garage_url(options.merge(
15
+ host: request.host,
16
+ port: request.port,
17
+ _render: params[:_render], format: params[:format])
18
+ )
19
+ end
20
+
21
+ helper_method :json_ui_garage_url
22
+ end
23
+ end
@@ -0,0 +1,108 @@
1
+ module Glib
2
+ module JsonUi
3
+ class AbstractBuilder
4
+ attr_reader :json, :page
5
+
6
+ def initialize json, page
7
+ @json = json
8
+ @page = page
9
+ end
10
+
11
+ private
12
+
13
+ def capitalize_first_letter_and_leave_others_alone(str)
14
+ str.slice(0,1).capitalize + str.slice(1..-1)
15
+ end
16
+
17
+ def add_singleton_element type, name, *args
18
+ name_components = name.to_s.split('_')
19
+ json.set! type, "#{name_components.join('/')}-v1" if type
20
+ args = [yield] if block_given?
21
+ "#{self.class.name}::#{name_components.map{ |str| capitalize_first_letter_and_leave_others_alone(str) }.join('::')}".constantize.new(json, @page).parse(*args)
22
+ end
23
+
24
+ def add_element_to_array type, name, *args
25
+ json.child! do
26
+ add_singleton_element type, name, *args
27
+ end
28
+ end
29
+ end
30
+
31
+ class JsonUiElement
32
+ attr_reader :json, :page
33
+
34
+ def initialize json, page
35
+ @json = json
36
+ @page = page
37
+ end
38
+
39
+ def parse args = {}
40
+ @args = args
41
+
42
+ if args.is_a? Hash
43
+ args.each do |k, v|
44
+ send(k, v)
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def self.string propName
52
+ define_method(propName) do |value|
53
+ json.set! propName, value&.to_s
54
+ end
55
+ end
56
+
57
+ def self.int propName
58
+ define_method(propName) do |value|
59
+ json.set! propName, value&.to_i
60
+ end
61
+ end
62
+
63
+ def self.float propName
64
+ define_method(propName) do |value|
65
+ json.set! propName, value&.to_f
66
+ end
67
+ end
68
+
69
+ def self.bool propName
70
+ define_method(propName) do |value|
71
+ json.set! propName, value == true
72
+ end
73
+ end
74
+
75
+ def self.icon propName
76
+ define_method(propName) do |value|
77
+ json.set!(propName) do
78
+ json.materialName value
79
+ end
80
+ end
81
+ end
82
+
83
+ def self.action propName
84
+ define_method(propName) do |value|
85
+ json.set!(propName) { value.call(page.action_builder) }
86
+ end
87
+ end
88
+
89
+ def self.views propName
90
+ define_method(propName) do |value|
91
+ json.set!(propName) { value.call(page.view_builder) }
92
+ end
93
+ end
94
+
95
+ def self.array propName
96
+ define_method(propName) do |value|
97
+ json.set! propName, value&.to_a
98
+ end
99
+ end
100
+
101
+ def self.hash propName
102
+ define_method(propName) do |value|
103
+ json.set! propName, value
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,76 @@
1
+ module Glib
2
+ module JsonUi
3
+ class ActionBuilder < AbstractBuilder
4
+ def method_missing m, *args
5
+ add_singleton_element 'action', m, *args
6
+ end
7
+
8
+ class Action < JsonUiElement
9
+ end
10
+
11
+ ### Action definitions
12
+
13
+ module Dialogs
14
+ class Alert < Action
15
+ string :message
16
+ action :onClose
17
+ end
18
+
19
+ class Options < Action
20
+ string :message
21
+
22
+ def buttons(block)
23
+ json.buttons do
24
+ block.call page.menu_builder
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ module Forms
31
+ class Submit < Action
32
+ end
33
+
34
+ end
35
+
36
+ module Windows
37
+ class CloseAll < Action
38
+ action :onClose
39
+ end
40
+
41
+ class Open < Action
42
+ string :url
43
+ end
44
+
45
+ end
46
+
47
+ module Auth
48
+ class SaveCsrfToken < Action
49
+ string :token
50
+ action :onSave
51
+ end
52
+
53
+ end
54
+
55
+ module Http
56
+ class Post < Action
57
+ string :url
58
+ hash :formData
59
+ end
60
+
61
+ class Patch < Action
62
+ string :url
63
+ hash :formData
64
+ end
65
+
66
+ class Delete < Action
67
+ string :url
68
+ hash :formData
69
+ end
70
+
71
+ end
72
+
73
+ ###
74
+ end
75
+ end
76
+ end
@@ -1,5 +1,6 @@
1
1
  module Glib
2
- module Web
2
+ module JsonUi
3
+ # TODO: Remove (deprecated)
3
4
  module JsonUiHelper
4
5
  class Builder
5
6
  def initialize(json)
@@ -0,0 +1,51 @@
1
+ module Glib
2
+ module JsonUi
3
+ module ListBuilders
4
+ class Template < AbstractBuilder
5
+ def method_missing m, *args
6
+ add_element_to_array 'template', m, *args
7
+ end
8
+
9
+ class AbstractTemplate < JsonUiElement
10
+ def editButtons(block)
11
+ json.editButtons do
12
+ block.call page.menu_builder
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ class Thumbnail < AbstractTemplate
19
+ string :title
20
+ string :subtitle
21
+ action :onClick
22
+ end
23
+ end
24
+
25
+ class Section < AbstractBuilder
26
+ def header(options = {})
27
+ json.header do
28
+ json.padding options[:padding]
29
+ json.subviews do
30
+ yield page.view_builder
31
+ end
32
+ end
33
+ end
34
+
35
+ def rows(array = nil)
36
+ template = page.list_template_builder
37
+ json.rows do
38
+ if array
39
+ array.each do |row|
40
+ yield template, row
41
+ end
42
+ else
43
+ yield template
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,18 @@
1
+ module Glib
2
+ module JsonUi
3
+ class MenuBuilder < AbstractBuilder
4
+ def method_missing m, *args
5
+ add_element_to_array nil, m, *args
6
+ end
7
+
8
+ class MenuItem < JsonUiElement
9
+ end
10
+
11
+ class Button < MenuItem
12
+ string :title
13
+ icon :icon
14
+ action :onClick
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,74 @@
1
+ module Glib
2
+ module JsonUi
3
+ module PageHelper
4
+ def json_ui_page json
5
+ yield Page.new(json, self)
6
+ end
7
+
8
+ class Page
9
+ attr_reader :json, :context, :view_builder, :action_builder, :menu_builder
10
+ attr_reader :list_section_builder, :list_template_builder
11
+ attr_reader :table_section_builder, :table_template_builder
12
+
13
+ def initialize json, context
14
+ @json = json
15
+ @context = context
16
+
17
+ @view_builder = ViewBuilder.new(json, self)
18
+ @action_builder = ActionBuilder.new(json, self)
19
+ @menu_builder = MenuBuilder.new(json, self)
20
+ @list_section_builder = ListBuilders::Section.new(json, self)
21
+ @list_template_builder = ListBuilders::Template.new(json, self)
22
+ @table_section_builder = TableBuilders::Section.new(json, self)
23
+ @table_template_builder = TableBuilders::Template.new(json, self)
24
+ end
25
+
26
+ def rightNavButtons
27
+ json.rightNavButtons do
28
+ yield @menu_builder
29
+ end
30
+ end
31
+
32
+ def header(options = {})
33
+ json.header do
34
+ json.padding options[:padding]
35
+ json.backgroundColor options[:backgroundColor]
36
+ json.subviews do
37
+ options[:childViews]&.call @view_builder
38
+ end
39
+ end
40
+ end
41
+
42
+ def list(options = {})
43
+ json.content do
44
+ json.subviews do
45
+ @view_builder.add_view('panels_list', options.reverse_merge(width: 'matchParent'))
46
+ end
47
+ end
48
+ end
49
+
50
+ def table(options = {})
51
+ json.content do
52
+ json.subviews do
53
+ @view_builder.add_view('panels_table', options.reverse_merge(width: 'matchParent'))
54
+ end
55
+ end
56
+ end
57
+
58
+ def scroll(options = {})
59
+ json.content do
60
+ json.subviews do
61
+ @view_builder.add_view('panels_scroll', options.reverse_merge(width: 'matchParent'))
62
+ end
63
+ end
64
+ end
65
+
66
+ def form(options = {})
67
+ scroll subviews: ->(scroll) do
68
+ @view_builder.add_view('panels_form', options.reverse_merge(width: 'matchParent'))
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,23 @@
1
+ module Glib
2
+ module JsonUi
3
+ module ResponseHelper
4
+ def json_ui_response json
5
+ json.onResponse do
6
+ response = Response.new(json, self)
7
+ yield response.action_builder
8
+ end
9
+ end
10
+
11
+ class Response
12
+ attr_reader :action_builder
13
+
14
+ def initialize json, context
15
+ @json = json
16
+ @context = context
17
+
18
+ @action_builder = ActionBuilder.new(json, self)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,51 @@
1
+ module Glib
2
+ module JsonUi
3
+ module TableBuilders
4
+ class Template < AbstractBuilder
5
+ def method_missing m, *args
6
+ add_element_to_array 'template', m, *args
7
+ end
8
+
9
+ class AbstractTemplate < JsonUiElement
10
+
11
+ def editButtons(block)
12
+ json.editButtons do
13
+ block.call page.menu_builder
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ class Default < AbstractTemplate
20
+ views :cellViews
21
+ action :onClick
22
+ end
23
+ end
24
+
25
+ class Section < AbstractBuilder
26
+ def header(options = {})
27
+ json.header do
28
+ json.backgroundColor options[:backgroundColor]
29
+ json.cellViews do
30
+ options[:cellViews]&.call page.view_builder
31
+ end
32
+ end
33
+ end
34
+
35
+ def rows(array = nil)
36
+ template = page.table_template_builder
37
+ json.rows do
38
+ if array
39
+ array.each do |row|
40
+ yield template, row
41
+ end
42
+ else
43
+ yield template
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,167 @@
1
+ module Glib
2
+ module JsonUi
3
+ class ViewBuilder < AbstractBuilder
4
+ def add_view name, *args
5
+ add_element_to_array 'view', name, *args
6
+ end
7
+
8
+ def method_missing m, *args
9
+ add_view m, *args
10
+ end
11
+
12
+ class View < JsonUiElement
13
+ string :width
14
+ string :height
15
+ string :backgroundColor
16
+ hash :padding
17
+ end
18
+
19
+ ### View definitions
20
+
21
+ class H1 < View
22
+ string :text
23
+ end
24
+
25
+ class H2 < View
26
+ string :text
27
+ end
28
+
29
+ class H3 < View
30
+ string :text
31
+ end
32
+
33
+ class Br < View
34
+ end
35
+
36
+ class Hr < View
37
+ end
38
+
39
+ class Button < View
40
+ icon :icon
41
+ string :text
42
+ action :onClick
43
+ end
44
+
45
+ class Fab < View
46
+ icon :icon
47
+ action :onClick
48
+ end
49
+
50
+ class Image < View
51
+ string :url
52
+ end
53
+
54
+ class Label < View
55
+ string :text
56
+ end
57
+
58
+ module Fields
59
+ class AbstractField < View
60
+ string :name
61
+ string :label
62
+ string :value
63
+ end
64
+
65
+ class Text < AbstractField
66
+ end
67
+
68
+ class Email < Text
69
+ end
70
+
71
+ class Password < Text
72
+ end
73
+
74
+ class Hidden < Text
75
+ end
76
+
77
+ class Country < AbstractField
78
+ hash :region
79
+ end
80
+
81
+ class Select < AbstractField
82
+ array :options
83
+ end
84
+
85
+ class RadioGroup < View
86
+ string :name
87
+ views :childViews
88
+ end
89
+
90
+ class Radio < View
91
+ string :label
92
+ string :value
93
+ end
94
+ end
95
+
96
+ module Panels
97
+ class Form < View
98
+ string :url
99
+ string :method
100
+ # boolean :local
101
+
102
+ def subviews(block)
103
+ # insert_method_field = false
104
+ # method = @args[:method].to_sym
105
+ # case method
106
+ # when :patch, :put, :delete
107
+ # json.method :post
108
+ # insert_method_field = true
109
+ # else
110
+ # json.method method
111
+ # end
112
+
113
+ json.subviews do
114
+ json.child! do
115
+ json.view 'fields/hidden-v1'
116
+ json.name 'authenticity_token'
117
+ json.value page.context.form_authenticity_token
118
+ end
119
+
120
+ # if insert_method_field
121
+ # json.child! do
122
+ # json.view 'fields/hidden-v1'
123
+ # json.name '_method'
124
+ # json.value method
125
+ # end
126
+ # end
127
+
128
+ block.call(page.view_builder)
129
+ end
130
+ end
131
+ end
132
+
133
+ class List < View
134
+ hash :nextPage
135
+
136
+ def firstSection(block)
137
+ json.sections [1] do
138
+ block.call page.list_section_builder
139
+ end
140
+ end
141
+ end
142
+
143
+ class Table < View
144
+ def firstSection(block)
145
+ json.sections [1] do
146
+ block.call page.table_section_builder
147
+ end
148
+ end
149
+ end
150
+
151
+ class Scroll < View
152
+ views :subviews
153
+ end
154
+
155
+ class Split < View
156
+ views :leftViews
157
+ views :rightViews
158
+ end
159
+
160
+ class Vertical < View
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+ end
167
+ end
@@ -1,25 +1,37 @@
1
1
  json.title 'Actions'
2
2
 
3
- render './nav_menu', json: json
3
+ render "#{@path_prefix}/nav_menu", json: json
4
4
 
5
- json_body_with_list json do
6
- json.rows do
7
- json.child! do
8
- json.template 'default-v1'
9
- json.title 'Alert'
10
- json.onClick do
11
- json.action 'dialogs/alert-v1'
12
- json.message 'This is an alert'
13
- end
14
- end
15
-
16
- # json.child! do
17
- # json.template 'default-v1'
18
- # json.title 'Remote Content'
19
- # json.onClick do
20
- # json.action 'dialogs/open-v1'
21
- # json.url ''
5
+ json_ui_page json do |page|
6
+
7
+ page.list firstSection: ->(section) do
8
+ # section.header(padding: { top: 12, left: 16, right: 16, bottom: 12 }) do |header|
9
+ # header.panels_split width: 'matchParent', leftViews: ->(split) do
10
+ # split.label text: 'Left'
11
+ # end, rightViews: ->(split) do
12
+ # split.label text: 'Right'
22
13
  # end
14
+
15
+ # header.fab icon: 'add', onClick: ->(action) { action.alert(message: 'TODO: edit name') }
23
16
  # end
17
+
18
+ section.rows do |row|
19
+ row.thumbnail title: 'dialogs/alert', onClick: ->(action) do
20
+ action.dialogs_alert message: 'This is an alert'
21
+ end
22
+
23
+ row.thumbnail title: 'dialogs/option', onClick: ->(action) do
24
+ action.dialogs_options message: 'Select one', buttons: ->(menu) do
25
+ menu.button title: 'Option1', onClick: ->(action) do
26
+ action.dialogs_alert message: 'Option 1'
27
+ end
28
+ menu.button title: 'Option2', onClick: ->(action) do
29
+ action.dialogs_alert message: 'Option 2'
30
+ end
31
+ menu.button title: 'Cancel'
32
+ end
33
+ end
34
+ end
35
+
24
36
  end
25
- end
37
+ end
@@ -33,8 +33,8 @@ json_body_with_form json, nil, nil, options do
33
33
  json.view 'panels/split-v1'
34
34
  json.width 'matchParent'
35
35
 
36
- json.content do
37
- json.right do
36
+ json.rightViews do
37
+ json.child! do
38
38
  json.view 'button-v1'
39
39
  json.text 'Submit'
40
40
  json.onClick do
data/config/routes.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  Glib::Web::Engine.routes.draw do
2
- get 'json_ui_garage', to: 'home#json_ui_garage'
3
- post 'json_ui_garage', to: 'home#json_ui_garage'
2
+ scope module: :glib do
3
+ get 'json_ui_garage', to: 'home#json_ui_garage'
4
+ post 'json_ui_garage', to: 'home#json_ui_garage'
5
+ end
4
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glib-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
@@ -34,8 +34,16 @@ files:
34
34
  - app/controllers/concerns/application/json/transformation.rb
35
35
  - app/controllers/concerns/application/json/ui.rb
36
36
  - app/controllers/concerns/application/json/validation.rb
37
- - app/controllers/home_controller.rb
38
- - app/helpers/glib/web/json_ui_helper.rb
37
+ - app/controllers/glib/home_controller.rb
38
+ - app/helpers/glib/json_ui/abstract_builder.rb
39
+ - app/helpers/glib/json_ui/action_builder.rb
40
+ - app/helpers/glib/json_ui/json_ui_helper.rb
41
+ - app/helpers/glib/json_ui/list_builders.rb
42
+ - app/helpers/glib/json_ui/menu_builder.rb
43
+ - app/helpers/glib/json_ui/page_helper.rb
44
+ - app/helpers/glib/json_ui/response_helper.rb
45
+ - app/helpers/glib/json_ui/table_builders.rb
46
+ - app/helpers/glib/json_ui/view_builder.rb
39
47
  - app/views/app/views/json_ui/vue/renderer.html.erb
40
48
  - app/views/json_ui/garage/_nav_menu.json.jbuilder
41
49
  - app/views/json_ui/garage/actions/index.json.jbuilder
@@ -1,21 +0,0 @@
1
- class HomeController < ApplicationController
2
-
3
- def json_ui_garage
4
- @path_prefix = 'json_ui/garage'
5
- @sample_image_url = 'https://cdn-images-1.medium.com/max/1200/1*Qc0XxYm-qAZL-7tjjlNfrg.png'
6
-
7
- # We can't use prepend_view_path because it affects the app, not the gem
8
- path = "#{@path_prefix}/#{params[:path] || 'home/index'}"
9
- render path
10
- end
11
-
12
- def json_ui_garage_url(options = {})
13
- Glib::Web::Engine.routes.url_helpers.json_ui_garage_url(options.merge(
14
- host: request.host,
15
- port: request.port,
16
- _render: params[:_render], format: params[:format])
17
- )
18
- end
19
-
20
- helper_method :json_ui_garage_url
21
- end