pakyow-presenter/lib/presenter/string_doc.rb in pakyow-presenter-0.9.1 vs pakyow-presenter/lib/presenter/string_doc.rb in pakyow-presenter-0.10.0

- old
+ new

@@ -22,17 +22,16 @@ end def initialize_copy(original_doc) super - original_structure = original_doc.instance_variable_get(:@structure) - @structure = Utils::Dup.deep(original_structure) if original_structure + if original_structure = original_doc.instance_variable_get(:@structure) + @structure = Utils::Dup.deep(original_structure) + end - original_node = original_doc.instance_variable_get(:@node) - if original_node - node_index = original_structure.index(original_node) - @node = @structure[node_index] + if original_doc.node? + @node = @structure[original_doc.node_index] end end # Creates a StringDoc instance with the same structure, but a duped node. # @@ -66,10 +65,12 @@ attributes.delete(name.to_sym) end def remove @structure.delete_if { |n| n.equal?(node) } + @node = ['', {}, [['', {}, []]]] + @removed = true end def clear children.clear end @@ -114,11 +115,11 @@ def after(doc) doc = StringDoc.ensure(doc) if doc.node? - @structure.push(doc.node) + @structure.insert(node_index + 1, doc.node) else @structure.concat(doc.structure) end end @@ -132,11 +133,11 @@ end end def replace(doc) doc = StringDoc.ensure(doc) - index = @structure.index(node) || 0 + index = node_index || 0 if doc.node? @structure.insert(index + 1, node) else @structure.insert(index + 1, *doc.structure) @@ -156,10 +157,18 @@ def container(name) containers.fetch(name, {})[:doc] end + def component(name) + components.select { |c| c[:component] == name } + end + + def channel(name) + find_channel(scopes, name) + end + def containers find_containers(@node ? [@node] : @structure) end def partials @@ -168,10 +177,14 @@ def scopes find_scopes(@node ? [@node] : @structure) end + def components + find_components(@node ? [@node] : @structure) + end + def to_html StringDocRenderer.render(@node ? [@node] : @structure) end alias :to_s :to_html @@ -184,12 +197,19 @@ def node return @structure if @structure.empty? return @node || @structure[0] end + def node_index + return nil unless node? + @structure.index { |n| n.equal?(@node) } + end + def node? - !@node.nil? + return false if @node.nil? + return false if @removed + return true end def tagname node[0].gsub(/[^a-zA-Z]/, '') end @@ -198,10 +218,14 @@ StringDoc.from_structure(node[2][0][2].select { |option| option[1][:value] == value.to_s }) end + def exists? + @structure.include?(node) + end + private def title_search @structure.flatten.each do |n| next unless n.is_a?(String) @@ -215,12 +239,20 @@ # def attributes node[1] end + def has_attribute?(name) + attributes.key?(name) + end + def children - node[2][0][2] + if @structure.empty? + @structure + else + node[2][0][2] + end end def find_containers(structure, primary_structure = @structure, containers = {}) return {} if structure.empty? structure.inject(containers) { |s, e| @@ -243,16 +275,22 @@ end def find_scopes(structure, primary_structure = @structure, scopes = []) ret_scopes = structure.inject(scopes) { |s, e| if e[1].has_key?(:'data-scope') - s << { + scope = { doc: StringDoc.from_structure(primary_structure, node: e), scope: e[1][:'data-scope'].to_sym, props: find_node_props(e).concat(find_props(e[2])), nested: find_scopes(e[2]), } + + if version = e[1][:'data-version'] + scope[:version] = version.to_sym + end + + s << scope end # only find scopes if `e` is the root node or we're not decending into a nested scope find_scopes(e[2], e[2], s) if e == node || !e[1].has_key?(:'data-scope') s } || [] @@ -279,9 +317,38 @@ unless node[1].has_key?(:'data-scope') find_props(node[2], node[2], props) end props + end + + def find_channel(scopes, name) + scopes.each do |scope| + if scope[:doc].get_attribute(:'data-channel') == name + return scope[:doc] + end + + if doc = find_channel(scope[:nested], name) + return doc + end + end + + nil + end + + def find_components(structure, primary_structure = @structure, components = []) + ret_components = structure.inject(components) { |s, e| + if e[1].has_key?(:'data-ui') + s << { + doc: StringDoc.from_structure(primary_structure, node: e), + component: e[1][:'data-ui'].to_sym, + } + end + find_components(e[2], e[2], s) + s + } || [] + + ret_components end end end end