summaryrefslogtreecommitdiff
blob: af325ec63390d35eec0b8320ef0fdf21ea704589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
Index: test/unit/lib/redmine/menu_manager/menu_item_test.rb
===================================================================
--- test/unit/lib/redmine/menu_manager/menu_item_test.rb	(リビジョン 8213)
+++ test/unit/lib/redmine/menu_manager/menu_item_test.rb	(リビジョン 8214)
@@ -114,7 +114,7 @@
 
   def test_has_children
     parent_item = get_menu_item(:test_menu, :parent)
-    assert parent_item.hasChildren?
+    assert parent_item.children.present?
     assert_equal 2, parent_item.children.size
     assert_equal get_menu_item(:test_menu, :child_menu), parent_item.children[0]
     assert_equal get_menu_item(:test_menu, :child2_menu), parent_item.children[1]
Index: config/environment.rb
===================================================================
--- config/environment.rb	(リビジョン 8213)
+++ config/environment.rb	(リビジョン 8214)
@@ -54,7 +54,6 @@
   # It will automatically turn deliveries on
   config.action_mailer.perform_deliveries = false
 
-  config.gem 'rubytree', :lib => 'tree'
   config.gem 'coderay', :version => '~>1.0.0'
 
   # Load any local configuration that is kept out of source control
Index: lib/redmine/menu_manager.rb
===================================================================
--- lib/redmine/menu_manager.rb	(リビジョン 8213)
+++ lib/redmine/menu_manager.rb	(リビジョン 8214)
@@ -15,93 +15,6 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
-require 'tree' # gem install rubytree
-
-# Monkey patch the TreeNode to add on a few more methods :nodoc:
-module TreeNodePatch
-  def self.included(base)
-    base.class_eval do
-      attr_reader :last_items_count
-
-      alias :old_initilize :initialize
-      def initialize(name, content = nil)
-        old_initilize(name, content)
-      	@childrenHash ||= {}
-        @last_items_count = 0
-        extend(InstanceMethods)
-      end
-    end
-  end
-
-  module InstanceMethods
-    # Adds the specified child node to the receiver node.  The child node's
-    # parent is set to be the receiver.  The child is added as the first child in
-    # the current list of children for the receiver node.
-    def prepend(child)
-      raise "Child already added" if @childrenHash.has_key?(child.name)
-
-      @childrenHash[child.name]  = child
-      @children = [child] + @children
-      child.parent = self
-      return child
-
-    end
-
-    # Adds the specified child node to the receiver node.  The child node's
-    # parent is set to be the receiver.  The child is added at the position
-    # into the current list of children for the receiver node.
-    def add_at(child, position)
-      raise "Child already added" if @childrenHash.has_key?(child.name)
-
-      @childrenHash[child.name]  = child
-      @children = @children.insert(position, child)
-      child.parent = self
-      return child
-
-    end
-
-    def add_last(child)
-      raise "Child already added" if @childrenHash.has_key?(child.name)
-
-      @childrenHash[child.name]  = child
-      @children <<  child
-      @last_items_count += 1
-      child.parent = self
-      return child
-
-    end
-
-    # Adds the specified child node to the receiver node.  The child node's
-    # parent is set to be the receiver.  The child is added as the last child in
-    # the current list of children for the receiver node.
-    def add(child)
-      raise "Child already added" if @childrenHash.has_key?(child.name)
-
-      @childrenHash[child.name]  = child
-      position = @children.size - @last_items_count
-      @children.insert(position, child)
-      child.parent = self
-      return child
-
-    end
-
-    # Wrapp remove! making sure to decrement the last_items counter if
-    # the removed child was a last item
-    def remove!(child)
-      @last_items_count -= +1 if child && child.last
-      super
-    end
-
-
-    # Will return the position (zero-based) of the current child in
-    # it's parent
-    def position
-      self.parent.children.index(self)
-    end
-  end
-end
-Tree::TreeNode.send(:include, TreeNodePatch)
-
 module Redmine
   module MenuManager
     class MenuError < StandardError #:nodoc:
@@ -169,7 +82,7 @@
 
       def display_main_menu?(project)
         menu_name = project && !project.new_record? ? :project_menu : :application_menu
-        Redmine::MenuManager.items(menu_name).size > 1 # 1 element is the root
+        Redmine::MenuManager.items(menu_name).children.present?
       end
 
       def render_menu(menu, project=nil)
@@ -181,7 +94,7 @@
       end
 
       def render_menu_node(node, project=nil)
-        if node.hasChildren? || !node.child_menus.nil?
+        if node.children.present? || !node.child_menus.nil?
           return render_menu_node_with_children(node, project)
         else
           caption, url, selected = extract_node_details(node, project)
@@ -306,13 +219,13 @@
       end
 
       def items(menu_name)
-        @items[menu_name.to_sym] || Tree::TreeNode.new(:root, {})
+        @items[menu_name.to_sym] || MenuNode.new(:root, {})
       end
     end
 
     class Mapper
       def initialize(menu, items)
-        items[menu] ||= Tree::TreeNode.new(:root, {})
+        items[menu] ||= MenuNode.new(:root, {})
         @menu = menu
         @menu_items = items[menu]
       end
@@ -398,7 +311,102 @@
       end
     end
 
-    class MenuItem < Tree::TreeNode
+    class MenuNode
+      include Enumerable
+      attr_accessor :parent
+      attr_reader :last_items_count, :name
+
+      def initialize(name, content = nil)
+        @name = name
+        @childrenHash ||= {}
+        @children = []
+        @last_items_count = 0
+      end
+
+      def children
+        if block_given?
+          @children.each {|child| yield child}
+        else
+          @children
+        end
+      end
+
+      # Returns the number of descendants + 1
+      def size
+        @children.inject(1) {|sum, node| sum + node.size}
+      end
+
+      def each &block
+        yield self
+        children { |child| child.each(&block) }
+      end
+
+      # Adds a child at first position
+      def prepend(child)
+        raise "Child already added" if @childrenHash.has_key?(child.name)
+  
+        @childrenHash[child.name]  = child
+        @children = [child] + @children
+        child.parent = self
+        return child
+      end
+
+      # Adds a child at given position
+      def add_at(child, position)
+        raise "Child already added" if @childrenHash.has_key?(child.name)
+  
+        @childrenHash[child.name]  = child
+        @children = @children.insert(position, child)
+        child.parent = self
+        return child
+      end
+
+      # Adds a child as last child
+      def add_last(child)
+        raise "Child already added" if @childrenHash.has_key?(child.name)
+  
+        @childrenHash[child.name]  = child
+        @children <<  child
+        @last_items_count += 1
+        child.parent = self
+        return child
+      end
+
+      # Adds a child
+      def add(child)
+        raise "Child already added" if @childrenHash.has_key?(child.name)
+  
+        @childrenHash[child.name]  = child
+        position = @children.size - @last_items_count
+        @children.insert(position, child)
+        child.parent = self
+        return child
+      end
+      alias :<< :add
+
+      # Removes a child
+      def remove!(child)
+        @childrenHash.delete(child.name)
+        @children.delete(child)
+        @last_items_count -= +1 if child && child.last
+        child.parent = nil
+        child
+      end
+
+      # Returns the position for this node in it's parent
+      def position
+        self.parent.children.index(self)
+      end
+
+      # Returns the root for this node
+      def root
+        root = self
+        root = root.parent while root.parent
+        root
+      end
+    end
+
+    class MenuItem < MenuNode
       include Redmine::I18n
       attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last