Permalink
Browse files

Refactor #inform, #confirm and #choose_from.

File conversation.rb is gone. Long live dialog.rb.
  • Loading branch information...
1 parent 089dd00 commit 2cb116eb55b26b553dba61d5d4da48832df9402c @mneumann committed Jan 6, 2010
Showing with 127 additions and 48 deletions.
  1. +1 −0 lib/wee.rb
  2. +0 −48 lib/wee/conversation.rb
  3. +126 −0 lib/wee/dialog.rb
View
@@ -12,6 +12,7 @@ module Wee
require 'wee/component'
require 'wee/root_component'
require 'wee/task'
+require 'wee/dialog'
require 'wee/application'
require 'wee/request'
@@ -1,48 +0,0 @@
-require 'wee/component'
-
-module Wee
-
- class BlockComponent < Component
- def initialize(&block)
- @block = block
- end
-
- def render(r)
- instance_exec(r, &@block)
- end
- end
-
- class Component
- def display(&block)
- callcc BlockComponent.new(&block)
- end
-
- def confirm(question)
- display do |r|
- r.h3 question
- r.submit_button.callback { answer true }.value("Yes"); r.space
- r.submit_button.callback { answer false }.value("No")
- end
- end
-
- def inform(message)
- display do |r|
- r.h3 message
- r.submit_button.callback { answer }.value("Ok")
- end
- end
-
- def choose_from(items, caption=nil)
- display do |r|
- r.h3 caption if caption
- selection = nil
- r.select_list(items).callback {|s| selection = s }
- r.break
- r.submit_button.callback { answer selection }.value("Ok"); r.space
- r.submit_button.callback { answer nil }.value("Cancel")
- end
- end
-
- end
-
-end
View
@@ -0,0 +1,126 @@
+require 'wee/component'
+
+module Wee
+ class Dialog < Component; end
+
+ #
+ # Abstract class
+ #
+ class FormDialog < Dialog
+ def initialize(caption)
+ @caption = caption
+ end
+
+ def render(r)
+ r.div.css_class('wee').with {
+ render_caption(r)
+ render_form(r)
+ }
+ end
+
+ def render_caption(r)
+ r.h3 @caption if @caption
+ end
+
+ def render_form(r)
+ r.form.with {
+ render_body(r)
+ render_buttons(r)
+ }
+ end
+
+ def render_body(r)
+ end
+
+ def render_buttons(r)
+ return if buttons.empty?
+ r.div.css_class('dialog-buttons').with {
+ buttons.each do |title, return_value, sym, method|
+ sym ||= title.downcase
+ r.span.css_class("dialog-button-#{sym}").with {
+ if method
+ r.submit_button.callback_method(method).value(title)
+ else
+ r.submit_button.callback_method(:answer, return_value).value(title)
+ end
+ }
+ end
+ }
+ end
+
+ def buttons
+ []
+ end
+ end # class FormDialog
+
+ class MessageDialog < FormDialog
+ def initialize(caption, *buttons)
+ super(caption)
+ @buttons = buttons
+ end
+
+ def buttons
+ @buttons
+ end
+ end
+
+ class InformDialog < FormDialog
+ def buttons
+ [['Ok', nil, :ok]]
+ end
+ end # class InformDialog
+
+ class ConfirmDialog < FormDialog
+ def buttons
+ [['Yes', true, :yes], ['No', false, :no]]
+ end
+ end # class ConfirmDialog
+
+ class SingleSelectionDialog < FormDialog
+ attr_accessor :selected_item
+
+ def initialize(items, caption=nil, selected_item=nil)
+ super(caption)
+ @items = items
+ @selected_item = selected_item
+ end
+
+ def state(s) super
+ s.add_ivar(self, :@selected_item)
+ end
+
+ def render_body(r)
+ r.select_list(@items).selected(@selected_item).callback_method(:selected_item=)
+ end
+
+ def buttons
+ [['Ok', nil, :ok, :ok], ['Cancel', nil, :cancel, :cancel]]
+ end
+
+ def ok
+ answer @selected_item
+ end
+
+ def cancel
+ answer nil
+ end
+ end # class SingleSelectionDialog
+
+ #
+ # Extend class Component with shortcuts for the dialogs above
+ #
+ class Component
+ def confirm(question, &block)
+ call! ConfirmDialog.new(question), &block
+ end
+
+ def inform(message, &block)
+ call! InformDialog.new(message), &block
+ end
+
+ def choose_from(items, caption=nil, selected_item=nil, &block)
+ call! SingleSelectionDialog.new(items, caption, selected_item), &block
+ end
+ end # class Component
+
+end # module Wee

0 comments on commit 2cb116e

Please sign in to comment.