Commit ecc1b30b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Creation of Docs.icons = {...} for tree.js.

Added TreeIcons class that extracts icons from tree.
parent 0993a52a
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ $:.unshift File.dirname(__FILE__) # For running the actual JsDuck app
require 'jsduck/aggregator'
require 'jsduck/class'
require 'jsduck/tree'
require 'jsduck/tree_icons'
require 'jsduck/page'
require 'json'

@@ -58,8 +59,10 @@ module JsDuck
  # Given array of doc-objects, generates namespace tree and writes in
  # in JSON form into a file.
  def self.write_tree(filename, docs)
    js = "Docs.classData = " + JSON.generate( Tree.new.create(docs) ) + ";"
    js += "Docs.icons = {};"
    tree = Tree.new.create(docs)
    icons = TreeIcons.new.extract_icons(tree)
    js = "Docs.classData = " + JSON.generate( tree ) + ";"
    js += "Docs.icons = " + JSON.generate( icons ) + ";"
    File.open(filename, 'w') {|f| f.write(js) }
  end

+19 −0
Original line number Diff line number Diff line
module JsDuck

  # Takes the structure produced by JsDuck::Tree.create and creates
  # hashmap of classname-icon pairs.
  class TreeIcons
    def extract_icons(node)
      icons = {}
      if node[:children]
        node[:children].each do |child|
          icons.merge!(extract_icons(child))
        end
      else
        icons[node[:id]] = node[:iconCls]
      end
      icons
    end
  end

end
+75 −0
Original line number Diff line number Diff line
require "jsduck/tree_icons"

describe JsDuck::TreeIcons do

  before do
    @icons = JsDuck::TreeIcons.new.extract_icons({
        :id => "apidocs",
        :iconCls => "icon-docs",
        :text => "API Documentation",
        :singleClickExpand => true,
        :children => [
          {
            :id => "pkg-SamplePackage",
            :text => "SamplePackage",
            :iconCls => "icon-pkg",
            :cls => "package",
            :singleClickExpand => true,
            :children => [
              {
                :href => "output/SamplePackage.Component.html",
                :text => "Component",
                :id => "SamplePackage.Component",
                :isClass => true,
                :iconCls => "icon-cmp",
                :cls => "cls",
                :leaf => true
              },
              {
                :href => "output/SamplePackage.Singleton.html",
                :text => "Singleton",
                :id => "SamplePackage.Singleton",
                :isClass => true,
                :iconCls => "icon-static",
                :cls => "cls",
                :leaf => true
              },
              {
                :id => "pkg-SamplePackage",
                :text => "sub",
                :iconCls => "icon-pkg",
                :cls => "package",
                :singleClickExpand => true,
                :children => [
                  {
                    :href => "output/SamplePackage.sub.Foo.html",
                    :text => "Foo",
                    :id => "SamplePackage.sub.Foo",
                    :isClass => true,
                    :iconCls => "icon-cls",
                    :cls => "cls",
                    :leaf => true
                  },
                ]
              }
            ]
          }
        ]
      })
  end

  it "extracts as many icons as there are classes in tree" do
    @icons.length.should == 3
  end

  it "extracts icons inside a package" do
    @icons["SamplePackage.Component"].should == "icon-cmp"
    @icons["SamplePackage.Singleton"].should == "icon-static"
  end

  it "extracts icons inside all subpackages too" do
    @icons["SamplePackage.sub.Foo"].should == "icon-cls"
  end

end