glimmer-dsl-web 0.2.8 → 0.3.1

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
  SHA256:
3
- metadata.gz: a9f4b7e33522df500ca481bf4b9ba138c6cb3dad3847bee24f3f506fa985ac11
4
- data.tar.gz: fd7fd300d3519276ffb27de97049daf2b59cec4a5f1c2882b7386257dddc25d7
3
+ metadata.gz: 162d98e6fa4d8eefcec1f4edb2f5af6c3d5d3a88aaa01704ded825b982aabbd9
4
+ data.tar.gz: 8c133105ac1cf160658c476771a9b58c0ac351116a1b919cdef37db19bd085e7
5
5
  SHA512:
6
- metadata.gz: dbfcc16bf944ef8726912ee31e2ab8f045bba776a63d4377fea70c0fa28d990907b17f94b59ea0cc8e6e4855a4bba2bd965a66bea44fb53fd20ce3539cb09518
7
- data.tar.gz: 1220943c86ee1455654901700c1bf90834b81af96e355b86ebb7a91275a6130030917ce13eaa755cd0a8388d3fb9497b57a75372f1409989b9ffe0646774df32
6
+ metadata.gz: 5bc70f98db8b2fec83310e789967447663f9d1d7017ad6dd8b1984ce5e520ef835430d5f5b8c885fa92282d63617a58ba42cb30dda6b0212cafd63e3ce378b93
7
+ data.tar.gz: 75561cba8a7a7d61e71e286e197dbbb80879261715500249ddd565f0833273777039632f2553a1cb5e0e3c4c1258704fc83f73ea7e163c1d4ab64a0af0e2f307
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.3.1
4
+
5
+ - Optimize Todo MVC performance for filtering between all, active, and completed (it happens instantly now)
6
+ - Append Todo MVC todos at the bottom instead of prepending them at the top (I copied the ES6 version initially which did things the opposite way from how Todo MVC behaves normally in other versions)
7
+ - Make Todo MVC "items left" text show "item left" if there is only 1 todo (I missed this detail before)
8
+ - Make Todo MVC footer links open a new tab/window (with `target: '_blank'` option)
9
+ - Refactor/Simplify Todo MVC sample code
10
+ - Upgrade to glimmer 2.7.8
11
+
12
+ ## 0.3.0
13
+
14
+ - Optimize performance (~170%-226% faster) of rendering by building GUI in bulk, assembling html as a string from all nested elements and mounting all HTML at once (instead of making many small DOM mount calls). The trade-off is not being able to interact with elements until rendering of the complete hierarchy is complete, which is acceptable because interactions do not happen till after everything is rendered anyways. Can be disabled by passing the `bulk_render: false` option to the top-level component/element of a frontend app.
15
+ - Fix issue with not being able to add content to a custom control by opening a block that should add content inside its markup root element
16
+
3
17
  ## 0.2.8
4
18
 
5
19
  - Support Content Data-Binding to multiple model attributes via `computed_by` option (e.g. `content(@game, :scale, computed_by: [:width, :height])` or `content(@game, computed_by: [:scale, :width, :height])` will re-render content on changes to `:scale`, `:width`, or `:height`)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for Web 0.2.8 (Beta)
1
+ # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for Web 0.3.1 (Beta)
2
2
  ## Ruby in the Browser Web Frontend Framework
3
3
  ### Finally, Ruby Developer Productivity, Happiness, and Fun in the Frontend!!!
4
4
  [![Gem Version](https://badge.fury.io/rb/glimmer-dsl-web.svg)](http://badge.fury.io/rb/glimmer-dsl-web)
@@ -1154,6 +1154,10 @@ Screenshot:
1154
1154
 
1155
1155
  [Todo MVC Ruby Edition Is the One Todo MVC To Rule Them All!!!](https://andymaleh.blogspot.com/2024/06/todo-mvc-in-ruby-is-one-todo-mvc-to.html)
1156
1156
 
1157
+ [lib/glimmer-dsl-web/samples/regular/todo_mvc.rb](/lib/glimmer-dsl-web/samples/regular/todo_mvc.rb)
1158
+
1159
+ [lib/glimmer-dsl-web/samples/regular/todo_mvc](/lib/glimmer-dsl-web/samples/regular/todo_mvc)
1160
+
1157
1161
  ```ruby
1158
1162
  require 'glimmer-dsl-web'
1159
1163
 
@@ -1329,7 +1333,7 @@ rails new glimmer_app_server
1329
1333
  Add the following to `Gemfile`:
1330
1334
 
1331
1335
  ```
1332
- gem 'glimmer-dsl-web', '~> 0.2.8'
1336
+ gem 'glimmer-dsl-web', '~> 0.3.1'
1333
1337
  ```
1334
1338
 
1335
1339
  Run:
@@ -1521,6 +1525,8 @@ module ApplicationHelper
1521
1525
  end
1522
1526
  ```
1523
1527
 
1528
+ By default, elements are rendered in bulk for faster performance, meaning you cannot interact with element objects until rendering is done. This is a sensible default because most of the time, there is no need to interact with elements until the full frontend application is fully rendered. That said, if it is preferred every once in a while to render elements piecemeal instead of in bulk, this behavior can be adjusted by passing the option `bulk_render: false` to the top-level component or top-level element (if there is no component).
1529
+
1524
1530
  Note that Turbo is disabled on Glimmer elements/components. You can still use Turbo/Hotwire side by side with Glimmer DSL for Web by using one of the two technologies in every page. But, mixing them in the same pages is not recommended at the moment, so any pages loaded with Glimmer DSL for Web must be loaded without Turbo (e.g. by putting "data-turbo"="false" on anchor "a" tag links to Glimmer pages).
1525
1531
 
1526
1532
  If you run into any issues in setup, refer to the [Sample Glimmer DSL for Web Rails 7 App](https://github.com/AndyObtiva/sample-glimmer-dsl-web-rails7-app) project (in case I forgot to include some setup steps by mistake).
@@ -1556,7 +1562,7 @@ Disable the `webpacker` gem line in `Gemfile`:
1556
1562
  Add the following to `Gemfile`:
1557
1563
 
1558
1564
  ```ruby
1559
- gem 'glimmer-dsl-web', '~> 0.2.8'
1565
+ gem 'glimmer-dsl-web', '~> 0.3.1'
1560
1566
  ```
1561
1567
 
1562
1568
  Run:
@@ -3262,6 +3268,8 @@ Screenshot:
3262
3268
 
3263
3269
  [lib/glimmer-dsl-web/samples/regular/todo_mvc.rb](/lib/glimmer-dsl-web/samples/regular/todo_mvc.rb)
3264
3270
 
3271
+ [lib/glimmer-dsl-web/samples/regular/todo_mvc](/lib/glimmer-dsl-web/samples/regular/todo_mvc)
3272
+
3265
3273
  ```ruby
3266
3274
  require 'glimmer-dsl-web'
3267
3275
 
@@ -3354,10 +3362,6 @@ end
3354
3362
 
3355
3363
  ![Todo MVC](/images/glimmer-dsl-web-samples-regular-todo-mvc.gif)
3356
3364
 
3357
- The rest of the files are found at:
3358
-
3359
- [lib/glimmer-dsl-web/samples/regular/todo_mvc](/lib/glimmer-dsl-web/samples/regular/todo_mvc)
3360
-
3361
3365
  ## Design Principles
3362
3366
 
3363
3367
  - The Ruby Way (including TIMTOWTDI: There Is More Than One Way To Do It)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.8
1
+ 0.3.1
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: glimmer-dsl-web 0.2.8 ruby lib
5
+ # stub: glimmer-dsl-web 0.3.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "glimmer-dsl-web".freeze
9
- s.version = "0.2.8"
9
+ s.version = "0.3.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Andy Maleh".freeze]
14
- s.date = "2024-06-21"
14
+ s.date = "2024-07-01"
15
15
  s.description = "Glimmer DSL for Web (Ruby in the Browser Web Frontend Framework) enables building Web Frontends using Ruby in the Browser, as per Matz's recommendation in his RubyConf 2022 keynote speech to replace JavaScript with Ruby. It aims at providing the simplest, most intuitive, most straight-forward, and most productive frontend framework in existence. The framework follows the Ruby way (with DSLs and TIMTOWTDI) and the Rails way (Convention over Configuration) in building Isomorphic Ruby on Rails Applications. It provides a Ruby HTML DSL, which uniquely enables writing both structure code and logic code in one language. It supports both Unidirectional (One-Way) Data-Binding (using <=) and Bidirectional (Two-Way) Data-Binding (using <=>). Dynamic rendering (and re-rendering) of HTML content is also supported via Content Data-Binding. Modular design is supported with Glimmer Web Components. And, a Ruby CSS DSL is supported with the included Glimmer DSL for CSS. Many samples are demonstrated in the Rails sample app (there is a very minimal Standalone [No Rails] sample app too). You can finally live in pure Rubyland on the Web in both the frontend and backend with Glimmer DSL for Web! This gem relies on Opal Ruby.".freeze
16
16
  s.email = "andy.am@gmail.com".freeze
17
17
  s.extra_rdoc_files = [
@@ -93,7 +93,7 @@ Gem::Specification.new do |s|
93
93
 
94
94
  s.specification_version = 4
95
95
 
96
- s.add_runtime_dependency(%q<glimmer>.freeze, ["~> 2.7.6"])
96
+ s.add_runtime_dependency(%q<glimmer>.freeze, ["~> 2.7.8"])
97
97
  s.add_runtime_dependency(%q<glimmer-dsl-xml>.freeze, ["~> 1.4.0"])
98
98
  s.add_runtime_dependency(%q<glimmer-dsl-css>.freeze, ["~> 1.4.0"])
99
99
  s.add_runtime_dependency(%q<opal>.freeze, ["= 1.8.2"])
@@ -101,7 +101,7 @@ Gem::Specification.new do |s|
101
101
  s.add_runtime_dependency(%q<opal-async>.freeze, ["~> 1.4.1"])
102
102
  s.add_runtime_dependency(%q<opal-jquery>.freeze, ["~> 0.5.1"])
103
103
  s.add_runtime_dependency(%q<to_collection>.freeze, [">= 2.0.1", "< 3.0.0"])
104
- s.add_development_dependency(%q<puts_debuggerer>.freeze, [">= 1.0.0"])
104
+ s.add_development_dependency(%q<puts_debuggerer>.freeze, [">= 1.0.1"])
105
105
  s.add_development_dependency(%q<rake>.freeze, [">= 10.1.0", "< 14.0.0"])
106
106
  s.add_development_dependency(%q<rake-tui>.freeze, [">= 0"])
107
107
  s.add_development_dependency(%q<jeweler>.freeze, [">= 2.3.9", "< 3.0.0"])
@@ -12,11 +12,9 @@ module Glimmer
12
12
  end
13
13
 
14
14
  def add_content(parent, keyword, *args, &block)
15
- if parent.rendered? || parent.skip_content_on_render_blocks?
15
+ if parent.bulk_render? || parent.rendered? || parent.skip_content_on_render_blocks?
16
16
  return_value = super(parent, keyword, *args, &block)
17
- if return_value.is_a?(String) && parent.dom_element.text.to_s.empty?
18
- parent.add_text_content(return_value)
19
- end
17
+ parent.add_text_content(return_value, on_empty: true) if return_value.is_a?(String)
20
18
  parent.post_add_content
21
19
  return_value
22
20
  else
@@ -14,12 +14,10 @@ module Glimmer
14
14
  end
15
15
 
16
16
  def add_content(parent, keyword, *args, &block)
17
- if parent.rendered? || parent.skip_content_on_render_blocks?
17
+ if parent.bulk_render? || parent.rendered? || parent.skip_content_on_render_blocks?
18
18
  return_value = css(&block).to_s
19
19
  return_value = super(parent, keyword, *args, &block) if return_value.to_s.empty?
20
- if return_value.is_a?(String) && parent.dom_element.text.to_s.empty?
21
- parent.add_text_content(return_value)
22
- end
20
+ parent.add_text_content(return_value, on_empty: true) if return_value.is_a?(String)
23
21
  parent.post_add_content
24
22
  return_value
25
23
  else
@@ -316,6 +316,10 @@ module Glimmer
316
316
  @markup_root&.render(parent: parent, custom_parent_dom_element: custom_parent_dom_element, brand_new: brand_new)
317
317
  end
318
318
 
319
+ def remove
320
+ @markup_root&.remove
321
+ end
322
+
319
323
  # Returns content block if used as an attribute reader (no args)
320
324
  # Otherwise, if a block is passed, it adds it as content to this Glimmer web component
321
325
  def content(*args, &block)
@@ -83,6 +83,11 @@ module Glimmer
83
83
  "<#{element}#{attributes}>#{content}</#{element}>"
84
84
  end
85
85
  end
86
+
87
+ def unrendered_dom_element(keyword)
88
+ @unrendered_dom_elements ||= {}
89
+ @unrendered_dom_elements[keyword] ||= Element["<#{keyword} />"]
90
+ end
86
91
  end
87
92
 
88
93
  include Glimmer
@@ -118,22 +123,28 @@ module Glimmer
118
123
  REGEX_FORMAT_DATE = /^\d{4}-\d{2}-\d{2}$/
119
124
  REGEX_FORMAT_TIME = /^\d{2}:\d{2}$/
120
125
 
121
- attr_reader :keyword, :parent, :args, :options, :children, :enabled, :foreground, :background, :removed?, :rendered
126
+ attr_reader :keyword, :parent, :parent_component, :args, :options, :children, :enabled, :foreground, :background, :removed?, :rendered
122
127
  alias rendered? rendered
123
128
 
124
129
  def initialize(keyword, parent, args, block)
125
130
  @keyword = keyword
126
- @parent = parent
131
+ @parent = parent.is_a?(Glimmer::Web::Component) ? parent.markup_root : parent
132
+ @parent_component = parent if parent.is_a?(Glimmer::Web::Component)
127
133
  @options = args.last.is_a?(Hash) ? args.last.symbolize_keys : {}
128
134
  if parent.nil?
129
135
  options[:parent] ||= Component.interpretation_stack.last&.options&.[](:parent)
130
136
  options[:render] ||= Component.interpretation_stack.last&.options&.[](:render)
137
+ options[:bulk_render] ||= Component.interpretation_stack.last&.options&.[](:bulk_render)
131
138
  end
132
139
  @args = args
133
140
  @block = block
134
141
  @children = []
135
142
  @parent&.post_initialize_child(self)
136
- render if !@rendered && render_after_create?
143
+ render if !bulk_render? && !@rendered && render_after_create?
144
+ end
145
+
146
+ def bulk_render?
147
+ options[:bulk_render] != false && (@parent.nil? || @parent.bulk_render?)
137
148
  end
138
149
 
139
150
  def render_after_create?
@@ -143,7 +154,7 @@ module Glimmer
143
154
  # Executes for the parent of a child that just got added
144
155
  def post_initialize_child(child)
145
156
  @children << child
146
- child.render if !render_after_create?
157
+ child.render if !bulk_render? && !render_after_create?
147
158
  end
148
159
 
149
160
  # Executes for the parent of a child that just got removed
@@ -153,21 +164,22 @@ module Glimmer
153
164
 
154
165
  # Executes at the closing of a parent widget curly braces after all children/properties have been added/set
155
166
  def post_add_content
156
- # TODO double check every place we should call this method
157
- # No Op
167
+ render if bulk_render? && @parent.nil?
158
168
  end
159
169
 
160
170
  def css_classes
161
- dom_element.attr('class').to_s.split
171
+ dom_element.attr('class').to_s.split if rendered?
162
172
  end
163
173
 
164
174
  def remove
165
- @children.dup.each do |child|
166
- child.remove
167
- end
168
175
  on_remove_listeners = listeners_for('on_remove').dup
169
- remove_all_listeners
170
- dom_element.remove
176
+ if rendered?
177
+ @children.dup.each do |child|
178
+ child.remove
179
+ end
180
+ remove_all_listeners
181
+ dom_element.remove
182
+ end
171
183
  parent&.post_remove_child(self)
172
184
  @removed = true
173
185
  on_remove_listeners.each do |listener|
@@ -222,20 +234,32 @@ module Glimmer
222
234
  end
223
235
 
224
236
  def enabled=(value)
225
- @enabled = value
226
- dom_element.prop('disabled', !@enabled)
237
+ if rendered?
238
+ @enabled = value
239
+ dom_element.prop('disabled', !@enabled)
240
+ else
241
+ enqueue_post_render_method_call('enabled=', value)
242
+ end
227
243
  end
228
244
 
229
245
  def foreground=(value)
230
- value = ColorProxy.new(value) if value.is_a?(String)
231
- @foreground = value
232
- dom_element.css('color', foreground.to_css) unless foreground.nil?
246
+ if rendered?
247
+ value = ColorProxy.new(value) if value.is_a?(String)
248
+ @foreground = value
249
+ dom_element.css('color', foreground.to_css) unless foreground.nil?
250
+ else
251
+ enqueue_post_render_method_call('foreground=', value)
252
+ end
233
253
  end
234
254
 
235
255
  def background=(value)
236
- value = ColorProxy.new(value) if value.is_a?(String)
237
- @background = value
238
- dom_element.css('background-color', background.to_css) unless background.nil?
256
+ if rendered?
257
+ value = ColorProxy.new(value) if value.is_a?(String)
258
+ @background = value
259
+ dom_element.css('background-color', background.to_css) unless background.nil?
260
+ else
261
+ enqueue_post_render_method_call('background=', value)
262
+ end
239
263
  end
240
264
 
241
265
  def parent_selector
@@ -243,8 +267,8 @@ module Glimmer
243
267
  end
244
268
 
245
269
  def parent_dom_element
246
- if parent_selector
247
- Document.find(parent_selector)
270
+ if parent
271
+ parent.dom_element
248
272
  else
249
273
  options[:parent] ||= 'body'
250
274
  the_element = Document.find(options[:parent])
@@ -272,26 +296,12 @@ module Glimmer
272
296
  else
273
297
  reattach(old_element)
274
298
  end
275
- observation_requests&.each do |keyword, event_listener_set|
276
- event_listener_set.each do |event_listener|
277
- handle_observation_request(keyword, event_listener)
278
- end
279
- end
280
- unless render_after_create?
281
- children.each do |child|
282
- child.render
283
- end
284
- end
285
- @rendered = true
286
- unless skip_content_on_render_blocks?
287
- content_on_render_blocks.each do |content_block|
288
- content(&content_block)
289
- end
290
- end
291
- # TODO replace following line with a method call like (`notify_listeners('on_render')`)
292
- listeners_for('on_render').each do |listener|
293
- listener.original_event_listener.call(EventProxy.new(listener: listener))
294
- end
299
+ mark_rendered
300
+ invoke_post_render_method_calls if bulk_render?
301
+ handle_observation_requests
302
+ children.each(&:render) if !bulk_render? && !render_after_create?
303
+ add_contents_for_render_blocks
304
+ notify_on_render_listeners
295
305
  end
296
306
  alias rerender render
297
307
 
@@ -303,8 +313,17 @@ module Glimmer
303
313
  old_element.replace_with(@dom)
304
314
  end
305
315
 
306
- def add_text_content(text)
307
- dom_element.append(text.to_s)
316
+ def mark_rendered
317
+ @rendered = true
318
+ children.each(&:mark_rendered) if bulk_render?
319
+ end
320
+
321
+ def add_text_content(text, on_empty: false)
322
+ if rendered?
323
+ dom_element.append(text.to_s) if !on_empty || dom_element.text.to_s.empty?
324
+ else
325
+ enqueue_post_render_method_call('add_text_content', text, on_empty:)
326
+ end
308
327
  end
309
328
 
310
329
  def content_on_render_blocks
@@ -332,11 +351,16 @@ module Glimmer
332
351
  # TODO auto-convert known glimmer attributes like parent to data attributes like data-parent
333
352
  # TODO check if we need to avoid rendering content block if no content is available
334
353
  @dom ||= begin
335
- content = args.first if args.first.is_a?(String)
354
+ content = args.first.is_a?(String) ? args.first : ''
355
+ content += children_dom_content if bulk_render?
336
356
  ElementProxy.render_html(keyword, html_options, content)
337
357
  end
338
358
  end
339
359
 
360
+ def children_dom_content
361
+ children.map(&:dom).join
362
+ end
363
+
340
364
  def html_options
341
365
  body_class = ([name, element_id] + css_classes.to_a).join(' ')
342
366
  html_options = options.dup
@@ -351,8 +375,12 @@ module Glimmer
351
375
  html_options
352
376
  end
353
377
 
354
- def content(&block)
355
- Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Web::ElementExpression.new, keyword, &block)
378
+ def content(bulk_render: false, &block)
379
+ original_bulk_render = options[:bulk_render]
380
+ options[:bulk_render] = bulk_render if rendered?
381
+ return_value = Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Web::ElementExpression.new, keyword, &block)
382
+ options[:bulk_render] = original_bulk_render if rendered?
383
+ return_value
356
384
  end
357
385
 
358
386
  # Subclasses must override with their own mappings
@@ -372,13 +400,21 @@ module Glimmer
372
400
  end
373
401
 
374
402
  def class_name=(value)
375
- value = value.is_a?(Array) ? value.join(' ') : value.to_s
376
- new_class_name = "#{name} #{element_id} #{value}"
377
- dom_element.prop('className', new_class_name)
403
+ if rendered?
404
+ value = value.is_a?(Array) ? value.join(' ') : value.to_s
405
+ new_class_name = "#{name} #{element_id} #{value}"
406
+ dom_element.prop('className', new_class_name)
407
+ else
408
+ enqueue_post_render_method_call('class_name=', value)
409
+ end
378
410
  end
379
411
 
380
412
  def add_css_class(css_class)
381
- dom_element.add_class(css_class)
413
+ if rendered?
414
+ dom_element.add_class(css_class)
415
+ else
416
+ enqueue_post_render_method_call('class_name=', value)
417
+ end
382
418
  end
383
419
 
384
420
  def add_css_classes(css_classes_to_add)
@@ -386,7 +422,11 @@ module Glimmer
386
422
  end
387
423
 
388
424
  def remove_css_class(css_class)
389
- dom_element.remove_class(css_class)
425
+ if rendered?
426
+ dom_element.remove_class(css_class)
427
+ else
428
+ enqueue_post_render_method_call('class_name=', value)
429
+ end
390
430
  end
391
431
 
392
432
  def remove_css_classes(css_classes_to_remove)
@@ -397,38 +437,17 @@ module Glimmer
397
437
  css_classes.each {|css_class| remove_css_class(css_class)}
398
438
  end
399
439
 
400
- def has_style?(symbol)
401
- @args.include?(symbol) # not a very solid implementation. Bring SWT constants eventually
402
- end
403
-
404
440
  def dom_element
405
- # TODO consider making this pick an element in relation to its parent, allowing unhooked dom elements to be built if needed (unhooked to the visible page dom)
406
- Document.find(selector)
407
- end
408
-
409
- # TODO consider adding a default #dom method implementation for the common case, automatically relying on #element and other methods to build the dom html
410
-
411
- def style_element
412
- style_element_id = "#{id}-style"
413
- style_element_selector = "style##{style_element_id}"
414
- element = dom_element.find(style_element_selector)
415
- if element.empty?
416
- new_element = Element.new(:style)
417
- new_element.attr('id', style_element_id)
418
- new_element.attr('class', "#{name.gsub('_', '-')}-instance-style widget-instance-style")
419
- dom_element.prepend(new_element)
420
- element = dom_element.find(style_element_selector)
441
+ if rendered?
442
+ # TODO consider making this pick an element in relation to its parent, allowing unhooked dom elements to be built if needed (unhooked to the visible page dom)
443
+ Document.find(selector)
444
+ else
445
+ # Using a fill-in dom element until self is rendered
446
+ ElementProxy.unrendered_dom_element(keyword)
421
447
  end
422
- element
423
448
  end
424
449
 
425
- def listener_selector
426
- selector
427
- end
428
-
429
- def listener_dom_element
430
- Document.find(listener_selector)
431
- end
450
+ # TODO consider adding a default #dom method implementation for the common case, automatically relying on #element and other methods to build the dom html
432
451
 
433
452
  def observation_requests
434
453
  @observation_requests ||= {}
@@ -473,16 +492,20 @@ module Glimmer
473
492
  end
474
493
 
475
494
  def handle_observation_request(keyword, original_event_listener)
476
- listener = ListenerProxy.new(
477
- element: self,
478
- selector: selector,
479
- dom_element: dom_element,
480
- event_attribute: keyword,
481
- original_event_listener: original_event_listener,
482
- )
483
- listener.register
484
- listeners_for(keyword) << listener
485
- listener
495
+ if rendered?
496
+ listener = ListenerProxy.new(
497
+ element: self,
498
+ selector: selector,
499
+ dom_element: dom_element,
500
+ event_attribute: keyword,
501
+ original_event_listener: original_event_listener,
502
+ )
503
+ listener.register
504
+ listeners_for(keyword) << listener
505
+ listener
506
+ else
507
+ enqueue_post_render_method_call('handle_observation_request', keyword, original_event_listener)
508
+ end
486
509
  end
487
510
 
488
511
  def remove_event_listener_proxies
@@ -492,17 +515,36 @@ module Glimmer
492
515
  event_listener_proxies.clear
493
516
  end
494
517
 
518
+ def notify_listeners(event)
519
+ listeners_for(event).each do |listener|
520
+ listener.original_event_listener.call(EventProxy.new(listener: listener))
521
+ end
522
+ end
523
+
524
+ def notify_on_render_listeners
525
+ notify_listeners('on_render')
526
+ children.each(&:notify_on_render_listeners) if bulk_render?
527
+ end
528
+
495
529
  def data_bindings
496
530
  @data_bindings ||= {}
497
531
  end
498
532
 
533
+ def type
534
+ if rendered?
535
+ super
536
+ else
537
+ options[:type] || 'text'
538
+ end
539
+ end
540
+
499
541
  def data_bind(property, model_binding)
500
542
  element_binding_translator = value_converters_for_input_type(type)[:model_to_view]
501
543
  element_binding_parameters = [self, property, element_binding_translator]
502
544
  element_binding = DataBinding::ElementBinding.new(*element_binding_parameters)
503
- element_binding.call(model_binding.evaluate_property)
504
545
  #TODO make this options observer dependent and all similar observers in element specific data binding handlers
505
546
  element_binding.observe(model_binding)
547
+ element_binding.call(model_binding.evaluate_property)
506
548
  data_bindings[element_binding] = model_binding
507
549
  unless model_binding.binding_options[:read_only]
508
550
  # TODO add guards against nil cases for hash below
@@ -521,10 +563,20 @@ module Glimmer
521
563
  # Data-binds the generation of nested content to a model/property (in binding args)
522
564
  # consider providing an option to avoid initial rendering without any changes happening
523
565
  def bind_content(*binding_args, &content_block)
524
- # TODO in the future, consider optimizing code by diffing content if that makes sense
525
566
  content_binding_work = proc do |*values|
567
+ # TODO in the future, consider optimizing code by diffing content if that makes sense (e.g. using opal-virtual-dom)
568
+ # To do so, we must avoid generating new content with new unique IDs/Classes and only append the new IDs classes after mounting
569
+ # TODO consider optimizing remove performance by doing clear instead and removing listeners separately
526
570
  children.dup.each { |child| child.remove }
527
- content(&content_block)
571
+ content(bulk_render: true, &content_block)
572
+ if bulk_render? && rendered?
573
+ self.inner_html = children_dom_content
574
+ children.each(&:mark_rendered)
575
+ children.each(&:invoke_post_render_method_calls)
576
+ children.each(&:handle_observation_requests)
577
+ children.each(&:add_contents_for_render_blocks)
578
+ children.each(&:notify_on_render_listeners)
579
+ end
528
580
  end
529
581
  model_binding_observer = Glimmer::DataBinding::ModelBinding.new(*binding_args)
530
582
  content_binding_observer = Glimmer::DataBinding::Observer.proc(&content_binding_work)
@@ -552,35 +604,86 @@ module Glimmer
552
604
  if method_name.to_s.start_with?('on_')
553
605
  handle_observation_request(method_name, block)
554
606
  elsif dom_element.respond_to?(method_name)
555
- dom_element.send(method_name, *args, &block)
607
+ if rendered?
608
+ dom_element.send(method_name, *args, &block)
609
+ else
610
+ enqueue_post_render_method_call(method_name, *args, &block)
611
+ end
556
612
  elsif !dom_element.prop(property_name).nil? && !dom_element.prop(property_name).is_a?(Proc)
557
- if method_name.end_with?('=')
558
- dom_element.prop(property_name, *args)
613
+ if rendered?
614
+ if method_name.end_with?('=')
615
+ dom_element.prop(property_name, *args)
616
+ else
617
+ dom_element.prop(property_name)
618
+ end
559
619
  else
560
- dom_element.prop(property_name)
620
+ enqueue_post_render_method_call(method_name, *args, &block)
561
621
  end
562
622
  elsif !dom_element.prop(unnormalized_property_name).nil? && !dom_element.prop(unnormalized_property_name).is_a?(Proc)
563
- if method_name.end_with?('=')
564
- dom_element.prop(unnormalized_property_name, *args)
623
+ if rendered?
624
+ if method_name.end_with?('=')
625
+ dom_element.prop(unnormalized_property_name, *args)
626
+ else
627
+ dom_element.prop(unnormalized_property_name)
628
+ end
565
629
  else
566
- dom_element.prop(unnormalized_property_name)
630
+ enqueue_post_render_method_call(method_name, *args, &block)
567
631
  end
568
632
  elsif dom_element && dom_element.length > 0
569
- js_args = block.nil? ? args : (args + [block])
570
- begin
571
- Native.call(dom_element, '0').method_missing(method_name.to_s.camelcase, *js_args)
572
- rescue Exception => e
633
+ if rendered?
634
+ js_args = block.nil? ? args : (args + [block])
573
635
  begin
574
- Native.call(dom_element, '0').method_missing(method_name.to_s, *js_args)
636
+ Native.call(dom_element, '0').method_missing(method_name.to_s.camelcase, *js_args)
575
637
  rescue Exception => e
576
- super(method_name, *args, &block)
638
+ begin
639
+ Native.call(dom_element, '0').method_missing(method_name.to_s, *js_args)
640
+ rescue Exception => e
641
+ super(method_name, *args, &block)
642
+ end
577
643
  end
644
+ else
645
+ enqueue_post_render_method_call(method_name, *args, &block)
578
646
  end
579
647
  else
580
648
  super(method_name, *args, &block)
581
649
  end
582
650
  end
583
651
 
652
+ def post_render_method_calls
653
+ @post_render_method_calls ||= []
654
+ end
655
+
656
+ def enqueue_post_render_method_call(method_name, *args, &block)
657
+ post_render_method_calls << [method_name, args, block]
658
+ nil
659
+ end
660
+
661
+ def invoke_post_render_method_calls
662
+ return unless rendered?
663
+ post_render_method_calls.each do |method_name, args, block|
664
+ send(method_name, *args, &block)
665
+ end
666
+ children.each(&:invoke_post_render_method_calls) if bulk_render?
667
+ end
668
+
669
+ def handle_observation_requests
670
+ observation_requests&.each do |keyword, event_listener_set|
671
+ event_listener_set.each do |event_listener|
672
+ handle_observation_request(keyword, event_listener)
673
+ end
674
+ end
675
+ children.each(&:handle_observation_requests) if bulk_render?
676
+ end
677
+
678
+ def add_contents_for_render_blocks
679
+ unless skip_content_on_render_blocks?
680
+ content_on_render_blocks.each do |content_block|
681
+ content(&content_block)
682
+ end
683
+ end
684
+ children.each(&:add_contents_for_render_blocks) if bulk_render?
685
+ end
686
+
584
687
  def property_name_for(method_name)
585
688
  attribute_name = method_name.end_with?('=') ? method_name.to_s[0...-1] : method_name.to_s
586
689
  PROPERTY_ALIASES[attribute_name] || attribute_name.camelcase
@@ -1,28 +1,9 @@
1
- Todo = Struct.new(:task, :completed, :editing, keyword_init: true) do
2
- class << self
3
- attr_writer :all
4
-
5
- def all
6
- @all ||= []
7
- end
8
-
9
- def active
10
- all.select(&:active?)
11
- end
12
-
13
- def completed
14
- all.select(&:completed?)
15
- end
16
- end
17
-
18
- FILTERS = [:all, :active, :completed]
19
-
1
+ Todo = Struct.new(:task, :completed, :editing, :deleted, keyword_init: true) do
20
2
  alias completed? completed
21
3
  alias editing? editing
4
+ alias deleted? deleted
22
5
 
23
- def active
24
- !completed
25
- end
6
+ def active = !completed
26
7
  alias active? active
27
8
 
28
9
  def start_editing
@@ -3,53 +3,54 @@ require 'glimmer/data_binding/observer'
3
3
  require_relative '../models/todo'
4
4
 
5
5
  class TodoPresenter
6
+ FILTERS = [:all, :active, :completed]
6
7
  FILTER_ROUTE_REGEXP = /\#\/([^\/]*)$/
7
8
 
8
- attr_accessor :todos, :can_clear_completed, :active_todo_count
9
- attr_reader :new_todo, :filter
9
+ attr_accessor :can_clear_completed, :active_todo_count, :created_todo
10
+ attr_reader :todos, :new_todo, :filter
10
11
 
11
12
  def initialize
12
- @todos = Todo.all.clone
13
+ @todos = []
13
14
  @new_todo = Todo.new(task: '')
14
15
  @filter = :all
15
- refresh_todo_stats
16
+ @can_clear_completed = false
17
+ @active_todo_count = 0
18
+ todo_stat_refresh_observer.observe(todos) # refresh stats if todos array adds/removes todo objects
16
19
  end
17
20
 
18
- def create_todo(todo = nil)
19
- todo ||= new_todo.clone
20
- Todo.all.prepend(todo)
21
- observers_for_todo_stats[todo.object_id] = todo_stat_observer.observe(todo, :completed) unless observers_for_todo_stats.has_key?(todo.object_id)
22
- refresh_todos_with_filter
23
- refresh_todo_stats
24
- new_todo.task = ''
25
- end
21
+ def active_todos = todos.select(&:active?)
22
+
23
+ def completed_todos = todos.select(&:completed?)
26
24
 
27
- def refresh_todos_with_filter
28
- self.todos = Todo.send(filter).clone
25
+ def create_todo
26
+ todo = new_todo.clone
27
+ todos.append(todo)
28
+ observe_todo_completion_to_update_todo_stats(todo)
29
+ new_todo.task = ''
30
+ self.created_todo = todo # notifies View observer indirectly to add created todo to todo list
29
31
  end
30
32
 
31
33
  def filter=(filter)
32
34
  return if filter == @filter
33
35
  @filter = filter
34
- refresh_todos_with_filter
35
36
  end
36
37
 
37
38
  def destroy(todo)
38
39
  delete(todo)
39
- refresh_todos_with_filter
40
- refresh_todo_stats
41
40
  end
42
41
 
43
42
  def clear_completed
44
- Todo.completed.each { |todo| delete(todo) }
45
- refresh_todos_with_filter
46
- refresh_todo_stats
43
+ refresh_todo_stats do
44
+ completed_todos.each { |todo| delete(todo) }
45
+ end
47
46
  end
48
47
 
49
48
  def toggle_all_completed
50
- target_completed_value = Todo.active.any?
51
- todos_to_update = target_completed_value ? Todo.active : Todo.completed
52
- todos_to_update.each { |todo| todo.completed = target_completed_value }
49
+ target_completed_value = active_todos.any?
50
+ todos_to_update = target_completed_value ? active_todos : completed_todos
51
+ refresh_todo_stats do
52
+ todos_to_update.each { |todo| todo.completed = target_completed_value }
53
+ end
53
54
  end
54
55
 
55
56
  def setup_filter_routes
@@ -73,30 +74,42 @@ class TodoPresenter
73
74
 
74
75
  private
75
76
 
77
+ def observe_todo_completion_to_update_todo_stats(todo)
78
+ # saving observer registration object to deregister when deleting todo
79
+ observers_for_todo_stats[todo.object_id] = todo_stat_refresh_observer.observe(todo, :completed)
80
+ end
81
+
82
+ def todo_stat_refresh_observer
83
+ @todo_stat_refresh_observer ||= Glimmer::DataBinding::Observer.proc { refresh_todo_stats }
84
+ end
85
+
76
86
  def delete(todo)
77
- Todo.all.delete(todo)
87
+ todos.delete(todo)
78
88
  observer_registration = observers_for_todo_stats.delete(todo.object_id)
79
89
  observer_registration&.deregister
90
+ todo.deleted = true # notifies View observer indirectly to delete todo
80
91
  end
81
92
 
82
93
  def observers_for_todo_stats
83
94
  @observers_for_todo_stats = {}
84
95
  end
85
96
 
86
- def todo_stat_observer
87
- @todo_stat_observer ||= Glimmer::DataBinding::Observer.proc { refresh_todo_stats }
88
- end
89
-
90
- def refresh_todo_stats
97
+ def refresh_todo_stats(&work_before_refresh)
98
+ if work_before_refresh
99
+ @do_not_refresh_todo_stats = true
100
+ work_before_refresh.call
101
+ @do_not_refresh_todo_stats = nil
102
+ end
103
+ return if @do_not_refresh_todo_stats
91
104
  refresh_can_clear_completed
92
105
  refresh_active_todo_count
93
106
  end
94
107
 
95
108
  def refresh_can_clear_completed
96
- self.can_clear_completed = Todo.completed.any?
109
+ self.can_clear_completed = todos.any?(&:completed?)
97
110
  end
98
111
 
99
112
  def refresh_active_todo_count
100
- self.active_todo_count = Todo.active.count
113
+ self.active_todo_count = active_todos.count
101
114
  end
102
115
  end
@@ -5,7 +5,7 @@ class TodoFilters
5
5
 
6
6
  markup {
7
7
  footer(class: 'todo-filters') {
8
- style <= [ Todo, :all,
8
+ style <= [ presenter, :todos,
9
9
  on_read: ->(todos) { todos.empty? ? 'display: none;' : '' }
10
10
  ]
11
11
 
@@ -14,12 +14,14 @@ class TodoFilters
14
14
  inner_text <= [presenter, :active_todo_count]
15
15
  }
16
16
  span {
17
- " items left"
17
+ inner_text <= [presenter, :active_todo_count,
18
+ on_read: -> (active_todo_count) { " item#{'s' if active_todo_count != 1} left" }
19
+ ]
18
20
  }
19
21
  }
20
22
 
21
23
  ul(class: 'filters') {
22
- Todo::FILTERS.each do |filter|
24
+ TodoPresenter::FILTERS.each do |filter|
23
25
  li {
24
26
  a(filter.to_s.capitalize, href: "#/#{filter unless filter == :all}") {
25
27
  class_name <= [ presenter, :filter,
@@ -5,9 +5,17 @@ class TodoList
5
5
 
6
6
  option :presenter
7
7
 
8
+ after_render do
9
+ observe(presenter, :created_todo) do |todo|
10
+ @todo_ul.content { # re-open todo ul content to add created todo
11
+ todo_list_item(presenter:, todo:)
12
+ }
13
+ end
14
+ end
15
+
8
16
  markup {
9
17
  main(class: 'main') {
10
- style <= [ Todo, :all,
18
+ style <= [ presenter, :todos,
11
19
  on_read: ->(todos) { todos.empty? ? 'display: none;' : '' }
12
20
  ]
13
21
 
@@ -21,12 +29,14 @@ class TodoList
21
29
  }
22
30
  }
23
31
 
24
- ul(class: 'todo-list') {
25
- content(presenter, :todos) {
26
- presenter.todos.each do |todo|
27
- todo_list_item(presenter:, todo:)
28
- end
29
- }
32
+ @todo_ul = ul {
33
+ class_name <= [presenter, :filter,
34
+ on_read: ->(filter) { "todo-list #{filter}" }
35
+ ]
36
+
37
+ presenter.todos.each do |todo|
38
+ todo_list_item(presenter:, todo:)
39
+ end
30
40
  }
31
41
 
32
42
  style {
@@ -84,5 +94,15 @@ class TodoList
84
94
  margin '0'
85
95
  padding '0'
86
96
  }
97
+
98
+ rule('.todo-list.active li.completed') {
99
+ display 'none'
100
+ }
101
+
102
+ rule('.todo-list.completed li.active') {
103
+ display 'none'
104
+ }
105
+
106
+ TodoListItem.todo_list_item_styles
87
107
  end
88
108
  end
@@ -6,20 +6,25 @@ class TodoListItem
6
6
  option :presenter
7
7
  option :todo
8
8
 
9
+ after_render do
10
+ # after rendering markup, observe todo deleted attribute and remove component when deleted
11
+ observe(todo, :deleted) do |deleted|
12
+ self.remove if deleted
13
+ end
14
+ end
15
+
9
16
  markup {
10
17
  li {
11
18
  class_name <= [ todo, :completed,
12
- on_read: -> (completed) { li_class_name(todo) }
19
+ on_read: -> { li_class_name(todo) }
13
20
  ]
14
21
  class_name <= [ todo, :editing,
15
- on_read: -> (editing) { li_class_name(todo) }
22
+ on_read: -> { li_class_name(todo) }
16
23
  ]
17
24
 
18
25
  div(class: 'view') {
19
26
  input(class: 'toggle', type: 'checkbox') {
20
- checked <=> [ todo, :completed,
21
- after_write: -> (_) { presenter.refresh_todos_with_filter if presenter.filter != :all }
22
- ]
27
+ checked <=> [todo, :completed]
23
28
  }
24
29
 
25
30
  label {
@@ -38,23 +43,18 @@ class TodoListItem
38
43
  }
39
44
 
40
45
  edit_todo_input(presenter:, todo:)
41
-
42
- if todo == presenter.todos.first
43
- style {
44
- todo_list_item_styles
45
- }
46
- end
47
46
  }
48
47
  }
49
48
 
50
49
  def li_class_name(todo)
51
50
  classes = []
52
51
  classes << 'completed' if todo.completed?
52
+ classes << 'active' if !todo.completed?
53
53
  classes << 'editing' if todo.editing?
54
54
  classes.join(' ')
55
55
  end
56
56
 
57
- def todo_list_item_styles
57
+ def self.todo_list_item_styles
58
58
  rule('.todo-list li.completed label') {
59
59
  color '#949494'
60
60
  text_decoration 'line-through'
@@ -7,10 +7,10 @@ class TodoMvcFooter
7
7
  "Double-click to edit a todo"
8
8
  }
9
9
  p {
10
- "Created by #{a('Andy Maleh', href: 'https://github.com/AndyObtiva')}"
10
+ "Created by #{a('Andy Maleh', href: 'https://github.com/AndyObtiva', target: '_blank')}"
11
11
  }
12
12
  p {
13
- "Part of #{a('TodoMVC', href: 'http://todomvc.com')}"
13
+ "Part of #{a('TodoMVC', href: 'http://todomvc.com', target: '_blank')}"
14
14
  }
15
15
 
16
16
  style {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-dsl-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-21 00:00:00.000000000 Z
11
+ date: 2024-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.7.6
19
+ version: 2.7.8
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.7.6
26
+ version: 2.7.8
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: glimmer-dsl-xml
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -134,14 +134,14 @@ dependencies:
134
134
  requirements:
135
135
  - - ">="
136
136
  - !ruby/object:Gem::Version
137
- version: 1.0.0
137
+ version: 1.0.1
138
138
  type: :development
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
141
141
  requirements:
142
142
  - - ">="
143
143
  - !ruby/object:Gem::Version
144
- version: 1.0.0
144
+ version: 1.0.1
145
145
  - !ruby/object:Gem::Dependency
146
146
  name: rake
147
147
  requirement: !ruby/object:Gem::Requirement