Class: Glimmer::SWT::DateTimeProxy

Inherits:
WidgetProxy show all
Defined in:
lib/glimmer/swt/date_time_proxy.rb

Constant Summary

Constants inherited from WidgetProxy

WidgetProxy::DEFAULT_INITIALIZERS

Instance Attribute Summary

Attributes inherited from WidgetProxy

#args, #background, #children, #disposed?, #enabled, #focus, #font, #foreground, #menu, #menu_requested, #parent, #path, #rendered

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WidgetProxy

#add_content_on_render, #add_css_class, #add_css_classes, #add_observer, #apply_property_type_converters, #build_dom, #can_handle_observation_request?, #clear_css_classes, #content, #content_on_render_blocks, #css_classes, #default_observation_request_to_event_mapping, #dispose, #dom_element, #effective_observation_request_to_event_mapping, #event_listener_proxies, for, #get_data, #handle_observation_request, #has_style?, #id, #id=, #layout, #listener_dom_element, #listener_path, max_id_number_for, max_id_numbers, #method_missing, #name, next_id_number_for, #observation_requests, #pack, #parent_dom_element, #parent_path, #post_dispose_child, #post_initialize_child, #property_type_converters, #remove_all_listeners, #remove_css_class, #remove_css_classes, #remove_event_listener_proxies, #render, reset_max_id_numbers!, #selector, #set_attribute, #set_data, #set_focus, #skip_content_on_render_blocks?, #style_element, #swt_widget, underscored_widget_name, widget_class, widget_exists?, #widget_property_listener_installers

Methods included from PropertyOwner

#attribute_getter, #attribute_setter, #get_attribute, #set_attribute

Constructor Details

#initialize(parent, args, block) ⇒ DateTimeProxy

Returns a new instance of DateTimeProxy.



22
23
24
25
# File 'lib/glimmer/swt/date_time_proxy.rb', line 22

def initialize(parent, args, block)
  super(parent, args, block)
  post_add_content if block.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::SWT::WidgetProxy

Class Method Details

.create(keyword, parent, args, block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/glimmer/swt/date_time_proxy.rb', line 7

def create(keyword, parent, args, block)
  case keyword
  when 'date'
    args += [:date]
  when 'date_drop_down'
    args += [:date, :drop_down]
  when 'time'
    args += [:time]
  when 'calendar'
    args += [:calendar]
  end
  new(parent, args, block)
end

Instance Method Details

#calendar?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/glimmer/swt/date_time_proxy.rb', line 71

def calendar?
  args.to_a.include?(:calendar)
end

#date?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/glimmer/swt/date_time_proxy.rb', line 59

def date?
  args.to_a.include?(:date)
end

#date_timeObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/glimmer/swt/date_time_proxy.rb', line 75

def date_time
  if @added_content
    default_date = DateTime.new if @date_time.nil?
    default_year = @date_time&.year || default_date.year
    default_month = @date_time&.month || default_date.month
    default_day = @date_time&.day || default_date.day
    default_hour = @date_time&.hour || default_date.hour
    default_min = @date_time&.min || default_date.min
    default_sec = @date_time&.sec || default_date.sec
    if time?
      @date_time = DateTime.new(default_year, default_month, default_day, dom_element.timepicker('getHour').to_i, dom_element.timepicker('getMinute').to_i, default_sec)
    else
      @date_time = DateTime.new(dom_element.datepicker('getDate')&.year.to_i, dom_element.datepicker('getDate')&.month.to_i, dom_element.datepicker('getDate')&.day.to_i, default_hour, default_min, default_sec)
    end
    @date_time = @date_time&.to_datetime
  else
    @initial_date_time
  end
end

#date_time=(value) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/glimmer/swt/date_time_proxy.rb', line 95

def date_time=(value)
  if @added_content
    @date_time = value&.to_datetime || DateTime.new
    if time?
      dom_element.timepicker('setTime', "#{@date_time.hour}:#{@date_time.min}")
    else
      dom_element.datepicker('setDate', @date_time.to_time)
    end
  else
    @initial_date_time = value
  end
end

#domObject



191
192
193
194
195
196
197
198
# File 'lib/glimmer/swt/date_time_proxy.rb', line 191

def dom
  @dom ||= html {
    span {
      send(element, type: 'text', id: id, class: name)
      button(id: time_button_id, class: time_button_class, style: "border: none; background: url(assets/glimmer/images/ui-icons_222222_256x240.png) -80px, -96px; width: 16px; height: 16px;") if time?
    }
  }.to_s
end

Returns:

  • (Boolean)


67
68
69
# File 'lib/glimmer/swt/date_time_proxy.rb', line 67

def drop_down?
  args.to_a.include?(:drop_down)
end

#elementObject



187
188
189
# File 'lib/glimmer/swt/date_time_proxy.rb', line 187

def element
  calendar? ? 'div' : 'input'
end

#observation_request_to_event_mappingObject

TODO add date, time, year, month, day, hours, minutes, seconds attribute methods



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/glimmer/swt/date_time_proxy.rb', line 110

def observation_request_to_event_mapping
  {
    'on_widget_selected' => [
      {
        event: 'change',
        event_handler: -> (event_listener) {
          -> (event) {
            if calendar? || date? || (time? && @timepicker_done)
              @timepicker_done = false if time?
              event_listener.call(event)
            end
          }
        }
      },
    ],
    'on_focus_lost' => [
      {
        event: 'blur',
        event_handler: -> (event_listener) {
          -> (event) {
            # TODO support blur event for date?
            if time? && @timepicker_done
              @timepicker_done = false if time?
              event_listener.call(event)
            end
          }
        }
      },
    ],
    'on_key_pressed' => {
      event: 'keydown',
      event_handler: -> (event_listener) {
        -> (event) {
          # TODO generalize this solution to all widgets that support key presses
          # TODO support event.location once DOM3 is supported by opal-jquery
          event.define_singleton_method(:keyCode) {event.which}
          event.define_singleton_method(:key_code, &event.method(:keyCode))
          event.define_singleton_method(:character) {event.which.chr}
          event.define_singleton_method(:stateMask) do
            state_mask = 0
            state_mask |= SWTProxy[:alt] if event.alt_key
            state_mask |= SWTProxy[:ctrl] if event.ctrl_key
            state_mask |= SWTProxy[:shift] if event.shift_key
            state_mask |= SWTProxy[:command] if event.meta_key
            state_mask
          end
          event.define_singleton_method(:state_mask, &event.method(:stateMask))
          doit = true
          event.define_singleton_method(:doit=) do |value|
            doit = value
          end
          event.define_singleton_method(:doit) { doit }
          event_listener.call(event)
          
            # TODO Fix doit false, it's not stopping input
          unless doit
            event.prevent
            event.prevent_default
            event.stop_propagation
            event.stop_immediate_propagation
          end
          
          doit
        }
      }
    },
  }
end

#post_add_contentObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/glimmer/swt/date_time_proxy.rb', line 27

def post_add_content
  # TODO handle date_drop_down version
  if time?
    dom_element.timepicker({
      showPeriod: true,
      showLeadingZero: true,
      showOn: 'both',
      showNowButton: true,
      showCloseButton: true,
      button: "##{time_button_id}",
      onClose: ->(v) {
        @timepicker_done = true
        dom_element.trigger('change')
      },
    })
  else
    options = {}
    if drop_down?
      options = {
        showOn: 'both',
        buttonImage: 'assets/glimmer/images/calendar.gif',
        buttonImageOnly: true,
        buttonText: 'Select date'
      }
    end
    dom_element.datepicker(options)
  end
  date_time_value = self.date_time
  @added_content = true
  self.date_time = date_time_value
end

#time?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/glimmer/swt/date_time_proxy.rb', line 63

def time?
  args.to_a.include?(:time)
end

#time_button_classObject



183
184
185
# File 'lib/glimmer/swt/date_time_proxy.rb', line 183

def time_button_class
  "#{name}-time-button"
end

#time_button_idObject



179
180
181
# File 'lib/glimmer/swt/date_time_proxy.rb', line 179

def time_button_id
  "#{id}-time-button"
end