Class: Foliokit::Overlay::OverlayBase

Inherits:
Object
  • Object
show all
Includes:
Modules::Element
Defined in:
lib/foliokit/overlay/overlay_base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, package) ⇒ OverlayBase

Returns a new instance of OverlayBase.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/foliokit/overlay/overlay_base.rb', line 24

def initialize(element, package)
  super(element, package)

  # calculate bounds
  bounds_target = element.css("#{package.manifest.orientation}Bounds > rectangle")
  bounds_target = element if bounds_target.empty?
  bounds_target = bounds_target.first if bounds_target.kind_of?(Nokogiri::XML::NodeSet)
  @bounds = Types::Bounds.convert(bounds_target, "bounds")

  # set initial state
  @state = states.first
end

Instance Attribute Details

#section_indexObject (readonly)

Returns the value of attribute section_index.



5
6
7
# File 'lib/foliokit/overlay/overlay_base.rb', line 5

def section_index
  @section_index
end

#stateObject (readonly)

Returns the value of attribute state.



5
6
7
# File 'lib/foliokit/overlay/overlay_base.rb', line 5

def state
  @state
end

Class Method Details

.element_baseObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/foliokit/overlay/overlay_base.rb', line 7

def self.element_base
  element_reader :id, hash: true
  element_reader :type
  element_reader :lastUpdated, type: :datetime
  element_reader :bounds, type: Types::Bounds, selector: ->(options) { "#{package.manifest.orientation}Bounds > rectangle" }
  element_reader :stateTransitionDuration, type: :f, selector: "data > stateTransitionDuration", text: true, default: 0.5
  element_reader :swipe_event, attribute: "type", selector: "data > eventHandling > event"
  element_reader :swipe_handler, attribute: "handler", selector: "data > eventHandling > event"
  element_collection :states, "data > states > state", State
  element_collection :events, "data > bindings > onevent", Event
  element_collection :overlays, "data > children > overlay", Overlay
end

Instance Method Details

#export(document, parent: nil, section_index: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/foliokit/overlay/overlay_base.rb', line 41

def export(document, parent: nil, section_index: nil)
  @section_index = section_index
  node = Nokogiri::XML::Node.new(tagname.to_s, document)
  # node["id"] = id if package.manifest.valid_referenced_id?(id)
  node["id"] = id
  node["class"] = "overlay-#{type}" unless tagname.include? "overlay"
  style = {
    width: "#{bounds.width}px",
    height: "#{bounds.height}px",
    left: "#{bounds.x}px",
    top: "#{bounds.y}px"
  }
  if parent&.stateful?
    node["name"] = id
    if state_transition_duration != 0
      style[:transition] = "opacity #{state_transition_duration}s"
    elsif parent.state_transition_duration != 0
      style[:transition] = "opacity #{parent.state_transition_duration}s"
    else
      style[:transition] = "opacity 0s"
    end
  end
  node["style"] = style.map { |k, v| "#{k}:#{v}" }.join(";")

  # stateful behaviour
  node["active-state"] = state.id if state

  # event / action behaviour
  bindings = {}
  unless swipe_event.nil?
    if swipe_event == "verticalSwipe"
      node["class"] += " prevent-swipe-vertical"
    elsif swipe_event == "horizontalSwipe"
      node["class"] += " prevent-swipe-horizontal"
    else
      node["class"] += " prevent-swipe-horizontal prevent-swipe-vertical"
    end
    node["touchable"] = true
    if swipe_handler == "changeState"
      bindings[swipe_event] = { selector: swipe_handler.downcase }
    elsif swipe_handler == "scroll"
      node["scroll"] = swipe_event.gsub("Swipe", "")
    end
  end
  if events.any?
    events.each do |event|
      actions = event.all_actions
      if actions.any?
        bindings[event.name] = event.all_actions.map(&:to_hash)
      end
    end
  end

  if bindings.any?
    node["bindings"] = JSON.dump(bindings)
    if bindings["click"] or bindings["horizontalSwipe"] or bindings["verticalSwipe"]
      node["touchable"] = true
    end
  end
  overlays.each do |overlay|
    child_node = overlay.export(document, parent: self, section_index: section_index)
    node << child_node unless child_node.nil?
  end
  node
end

#stateful?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/foliokit/overlay/overlay_base.rb', line 37

def stateful?
  !!state
end

#tagnameObject



20
21
22
# File 'lib/foliokit/overlay/overlay_base.rb', line 20

def tagname
  "div"
end