From ecc1b30b5a024abef66367ead110a67abdd22d4c Mon Sep 17 00:00:00 2001
From: Rene Saarsoo <nene@triin.net>
Date: Fri, 10 Dec 2010 19:03:41 +0200
Subject: [PATCH] Creation of Docs.icons = {...} for tree.js.

Added TreeIcons class that extracts icons from tree.
---
 lib/jsduck.rb            |  7 ++--
 lib/jsduck/tree_icons.rb | 19 ++++++++++
 spec/tree_icons_spec.rb  | 75 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 2 deletions(-)
 create mode 100644 lib/jsduck/tree_icons.rb
 create mode 100644 spec/tree_icons_spec.rb

diff --git a/lib/jsduck.rb b/lib/jsduck.rb
index 4be6643b..0558b75f 100755
--- a/lib/jsduck.rb
+++ b/lib/jsduck.rb
@@ -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
 
diff --git a/lib/jsduck/tree_icons.rb b/lib/jsduck/tree_icons.rb
new file mode 100644
index 00000000..062304d6
--- /dev/null
+++ b/lib/jsduck/tree_icons.rb
@@ -0,0 +1,19 @@
+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
diff --git a/spec/tree_icons_spec.rb b/spec/tree_icons_spec.rb
new file mode 100644
index 00000000..0526438e
--- /dev/null
+++ b/spec/tree_icons_spec.rb
@@ -0,0 +1,75 @@
+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
+
-- 
GitLab