class Glimmer::SWT::ListProxy

Constants

ITEM_EMPTY
STYLE

Attributes

items[R]
selection[R]

Public Class Methods

new(parent, args, block) click to toggle source
Calls superclass method
# File lib/glimmer/swt/list_proxy.rb, line 24
def initialize(parent, args, block)
  super(parent, args, block)
  @selection = []
end

Public Instance Methods

dom() click to toggle source
# File lib/glimmer/swt/list_proxy.rb, line 92
def dom
  list_id = id
  list_style = css
  list_selection = selection
  @dom ||= html {
    ul(id: list_id, class: name, style: list_style) {
    }
  }.to_s
end
element() click to toggle source
# File lib/glimmer/swt/list_proxy.rb, line 88
def element
  'ul'
end
index_of(item) click to toggle source
# File lib/glimmer/swt/list_proxy.rb, line 45
def index_of(item)
  @items.index(item)
end
items=(items) click to toggle source
# File lib/glimmer/swt/list_proxy.rb, line 29
def items=(items)
  @items = items.map {|item| item.strip == '' ? ITEM_EMPTY : item}
  list_selection = selection
  items_dom = @items.to_a.each_with_index.map do |item, index|
    li_class = ''
    li_class += ' selected' if list_selection.include?(item)
    li_class += ' empty-list-item' if item == ITEM_EMPTY
    html {
      li(class: li_class) {
        item
      }
    }.to_s
  end
  dom_element.html(items_dom)
end
observation_request_to_event_mapping() click to toggle source
# File lib/glimmer/swt/list_proxy.rb, line 71
def observation_request_to_event_mapping
  {
    'on_widget_selected' => {
      event: 'click',
      event_handler: -> (event_listener) {
        -> (event) {
          if event.target.prop('nodeName') == 'LI'
            selected_item = event.target.text
            select(index_of(selected_item), event.meta_key)
            event_listener.call(event)
          end
        }
      }
    }
  }
end
select(index, meta = false) click to toggle source

used for single selection taking an index

# File lib/glimmer/swt/list_proxy.rb, line 60
def select(index, meta = false)
  selected_item = @items[index]
  if @selection.include?(selected_item)
    @selection.delete(selected_item) if meta
  else
    @selection = [] if !meta || (!has_style?(:multi) && @selection.to_a.size >= 1)
    @selection << selected_item
  end
  self.selection = @selection
end
selection=(selection) click to toggle source

used for multi-selection taking an array

# File lib/glimmer/swt/list_proxy.rb, line 50
def selection=(selection)
  @selection = selection
  dom_element.find('li').remove_class('selected')
  @selection.each do |item|
    index = @items.index(item)
    dom_element.find("li:nth-child(#{index + 1})").add_class('selected')
  end
end