nested_list 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +46 -0
- data/lib/nested_list.rb +53 -39
- data/lib/nested_list/nested_select.rb +14 -13
- data/lib/nested_list/tasks.rb +6 -2
- data/lib/nested_list/version.rb +1 -1
- data/lib/public/nested_list.html +13 -0
- data/nested_list.gemspec +1 -1
- metadata +5 -3
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
Description
|
2
|
+
===========
|
3
|
+
|
4
|
+
nested_list gem is a tool for creating nested cross browser drop downs. It doesn't use any fancy UI, all margins were done with spaces ( ). It is simple and it works.
|
5
|
+
|
6
|
+
Install
|
7
|
+
=======
|
8
|
+
|
9
|
+
`gem install nested_list`
|
10
|
+
|
11
|
+
OR
|
12
|
+
|
13
|
+
Put this line in your Gemfile:
|
14
|
+
`gem 'nested_list'`
|
15
|
+
|
16
|
+
Then run the bundle command:
|
17
|
+
`$ bundle`
|
18
|
+
|
19
|
+
Example of usage
|
20
|
+
=======
|
21
|
+
|
22
|
+
Array with nested options which using ">" as a delimiter
|
23
|
+
|
24
|
+
@nested_names_arr = []
|
25
|
+
@nested_names_arr << {name: "All Categories", id: 'all'}
|
26
|
+
@nested_names_arr << {name: "Audio", id: '1'}
|
27
|
+
@nested_names_arr << {name: "Audio>Accessorize>Smile", id: '566767'}
|
28
|
+
@nested_names_arr << {name: "Audio>Accessorize>Pillow", id: '45255'}
|
29
|
+
@nested_names_arr << {name: "Audio>DVD", id: '234245'}
|
30
|
+
@nested_names_arr << {name: "Baby", id: '44245tr5'}
|
31
|
+
|
32
|
+
Pass it as a parameter and get options for drop down as an output
|
33
|
+
|
34
|
+
nested_list = NestedList.new(@nested_names_arr)
|
35
|
+
nested_list.html_options
|
36
|
+
|
37
|
+
As a result you will see a drop down like this:
|
38
|
+
|
39
|
+
![generated drop down][dropdown_screenshot]
|
40
|
+
|
41
|
+
Contributing
|
42
|
+
============
|
43
|
+
|
44
|
+
if you find a bug or have something to add, please do
|
45
|
+
|
46
|
+
[dropdown_screenshot]: http://i.imgur.com/OylKc.png "Drop Down Example"
|
data/lib/nested_list.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require "nested_list/nested_select"
|
2
3
|
require "active_support/inflector"
|
3
4
|
|
4
5
|
module Generator
|
5
6
|
|
6
7
|
class NestedList
|
8
|
+
include NestedSelect
|
7
9
|
# Get Array of nested names:
|
8
10
|
#[{name: "All Categories", id: 'all'},
|
9
11
|
# {name: "Audio", id: '1'},
|
@@ -17,58 +19,56 @@ module Generator
|
|
17
19
|
|
18
20
|
#Return a list of nested html_options from nested names array
|
19
21
|
def html_options
|
20
|
-
|
21
|
-
|
22
|
+
@wraped_list = {}
|
23
|
+
list = NestedSelect::List.new("root", nil)
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
@nested_names_arr.each do |retailer_category|
|
26
|
+
@names = retailer_category[:name].split(">")
|
27
|
+
point = list
|
28
|
+
length = @names.length - 1
|
29
|
+
@names.each_with_index do |name, level|
|
30
|
+
id = retailer_category[:id]
|
31
|
+
full_name = parameterized_name(@names[0..level])
|
32
|
+
if exist_node=list.find_by_name(full_name)
|
33
|
+
# Use existed name
|
34
|
+
if exist_node.full_name!=full_name && length != level
|
30
35
|
|
31
|
-
|
32
|
-
|
33
|
-
if exist_node.full_name!=full_name && length != level
|
34
|
-
|
35
|
-
found_item = exist_node.find_item(name)
|
36
|
-
moved_item = found_item
|
37
|
-
|
38
|
-
exist_node.remove_item(found_item)
|
39
|
-
item = NestedSelect::List.new(name, nil)
|
40
|
-
item.add_item(moved_item)
|
41
|
-
exist_node.add_item(item)
|
42
|
-
point = item
|
43
|
-
else
|
44
|
-
point = exist_node
|
45
|
-
end
|
36
|
+
found_item = exist_node.find_item(name)
|
37
|
+
moved_item = found_item
|
46
38
|
|
39
|
+
exist_node.remove_item(found_item)
|
40
|
+
item = NestedSelect::List.new(name, nil)
|
41
|
+
item.add_item(moved_item)
|
42
|
+
exist_node.add_item(item)
|
43
|
+
point = item
|
47
44
|
else
|
48
|
-
|
49
|
-
unless (level == length)
|
50
|
-
item = NestedSelect::List.new(name, nil)
|
51
|
-
item.add_item(NestedSelect::Item.new(name, nil))
|
52
|
-
else
|
53
|
-
item = NestedSelect::Item.new(name, id)
|
54
|
-
end
|
55
|
-
|
56
|
-
point.add_item(item)
|
57
|
-
point = item
|
45
|
+
point = exist_node
|
58
46
|
end
|
59
|
-
end
|
60
47
|
|
61
|
-
|
48
|
+
else
|
49
|
+
# Create new node
|
50
|
+
unless (level == length)
|
51
|
+
item = NestedSelect::List.new(name, nil)
|
52
|
+
item.add_item(NestedSelect::Item.new(name, nil))
|
53
|
+
else
|
54
|
+
item = NestedSelect::Item.new(name, id)
|
55
|
+
end
|
56
|
+
|
57
|
+
point.add_item(item)
|
58
|
+
point = item
|
59
|
+
end
|
60
|
+
end
|
62
61
|
|
63
|
-
|
64
|
-
|
62
|
+
end
|
63
|
+
|
64
|
+
@wraped_list = list.wrap
|
65
|
+
end
|
65
66
|
end
|
66
67
|
|
67
68
|
# Example
|
68
69
|
def nested_options_example
|
69
70
|
@nested_names_arr = []
|
70
71
|
@nested_names_arr << {name: "All Categories", id: 'all'}
|
71
|
-
@nested_names_arr << {name: "Audio", id: '1'}
|
72
72
|
@nested_names_arr << {name: "Audio>Accessorize>Smile", id: '566767'}
|
73
73
|
@nested_names_arr << {name: "Audio>Accessorize>Pillow", id: '45255'}
|
74
74
|
@nested_names_arr << {name: "Audio>DVD", id: '234245'}
|
@@ -78,4 +78,18 @@ module Generator
|
|
78
78
|
nested_list.html_options
|
79
79
|
end
|
80
80
|
|
81
|
+
#Support other language
|
82
|
+
# Example
|
83
|
+
def nested_options_example_ru
|
84
|
+
@nested_names_arr = []
|
85
|
+
@nested_names_arr << {name: "Все категории", id: 'all'}
|
86
|
+
@nested_names_arr << {name: "Аудио>Аксессуары>Smile", id: '566767'}
|
87
|
+
@nested_names_arr << {name: "Аудио>Аксессуары>Подушка", id: '45255'}
|
88
|
+
@nested_names_arr << {name: "Аудио>DVD", id: '234245'}
|
89
|
+
@nested_names_arr << {name: "Товары для детей", id: '44245tr5'}
|
90
|
+
|
91
|
+
nested_list = NestedList.new(@nested_names_arr)
|
92
|
+
nested_list.html_options
|
93
|
+
end
|
94
|
+
|
81
95
|
end
|
@@ -1,6 +1,11 @@
|
|
1
1
|
module NestedSelect
|
2
|
+
def parameterized_name(names)
|
3
|
+
names.map! { |name| name.to_s.strip.downcase }
|
4
|
+
names.join("-")
|
5
|
+
end
|
2
6
|
|
3
7
|
class Item
|
8
|
+
include NestedSelect
|
4
9
|
attr_accessor :name, :parent, :id
|
5
10
|
@@use_spaces = true
|
6
11
|
def initialize(name, id)
|
@@ -19,7 +24,7 @@ module NestedSelect
|
|
19
24
|
|
20
25
|
def find_by_name(name_tmp)
|
21
26
|
if !self.name.empty? && self.full_name == name_tmp
|
22
|
-
self.parent
|
27
|
+
self.parent
|
23
28
|
else
|
24
29
|
false
|
25
30
|
end
|
@@ -48,9 +53,9 @@ module NestedSelect
|
|
48
53
|
end
|
49
54
|
# Remove last general for all element
|
50
55
|
full_name.pop
|
51
|
-
full_name_str = full_name.reverse
|
56
|
+
full_name_str = parameterized_name(full_name.reverse!)
|
52
57
|
end
|
53
|
-
@full_name_str ||= full_name_str
|
58
|
+
@full_name_str ||= full_name_str
|
54
59
|
end
|
55
60
|
|
56
61
|
def wrap
|
@@ -72,7 +77,6 @@ module NestedSelect
|
|
72
77
|
|
73
78
|
def branches
|
74
79
|
@branches = @items[1..@items.size]
|
75
|
-
@branches
|
76
80
|
end
|
77
81
|
|
78
82
|
def add_item(item)
|
@@ -86,27 +90,24 @@ module NestedSelect
|
|
86
90
|
end
|
87
91
|
|
88
92
|
def find_item(tmp_name)
|
89
|
-
|
90
|
-
|
93
|
+
tmp_name = tmp_name.to_s.strip
|
94
|
+
@items.find do |item|
|
95
|
+
item.name == tmp_name
|
91
96
|
end
|
92
97
|
end
|
93
98
|
|
94
99
|
def items_count
|
95
|
-
|
96
|
-
@items.each do |item|
|
97
|
-
count += item.items_count
|
98
|
-
end
|
99
|
-
count
|
100
|
+
@items.inject(0){|acc,i| acc+i.items_count}
|
100
101
|
end
|
101
102
|
|
102
103
|
def find_by_name(name_tmp)
|
103
104
|
existed_item = nil
|
104
105
|
existed_items = []
|
105
|
-
|
106
|
+
|
106
107
|
if self.full_name == name_tmp
|
107
108
|
return self
|
108
109
|
end
|
109
|
-
|
110
|
+
|
110
111
|
@items.each do |item|
|
111
112
|
existed_item = item.find_by_name(name_tmp)
|
112
113
|
existed_items << existed_item if existed_item
|
data/lib/nested_list/tasks.rb
CHANGED
@@ -8,11 +8,15 @@ module NestedList
|
|
8
8
|
template = ERB.new <<-EOF
|
9
9
|
<!DOCTYPE html>
|
10
10
|
<html>
|
11
|
-
|
11
|
+
<head>
|
12
12
|
<title>Nested List</title>
|
13
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
13
14
|
</head>
|
14
15
|
<body>
|
15
|
-
|
16
|
+
Nested categories list:<br/>
|
17
|
+
<select><%= nested_options_example %></select><br/><br/>
|
18
|
+
Can be used for other language:<br/>
|
19
|
+
<select><%= nested_options_example_ru %></select>
|
16
20
|
</body>
|
17
21
|
</html>
|
18
22
|
EOF
|
data/lib/nested_list/version.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Nested List</title>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
6
|
+
</head>
|
7
|
+
<body>
|
8
|
+
Nested categories list:<br/>
|
9
|
+
<select><option value='all' class='level_1 item'>All categories</option><option value='audio:group' class='level_1 group'>Audio</option><option value='audio-accessorize:group' class='level_2 group'> Accessorize</option><option value='566767' class='level_3 item'> Smile</option><option value='45255' class='level_3 item'> Pillow</option><option value='234245' class='level_2 item'> Dvd</option><option value='44245tr5' class='level_1 item'>Baby</option></select><br/><br/>
|
10
|
+
Can be used for other language:<br/>
|
11
|
+
<select><option value='all' class='level_1 item'>Все категории</option><option value='Аудио:group' class='level_1 group'>Аудио</option><option value='Аудио-Аксессуары:group' class='level_2 group'> Аксессуары</option><option value='566767' class='level_3 item'> Smile</option><option value='45255' class='level_3 item'> Подушка</option><option value='234245' class='level_2 item'> Dvd</option><option value='44245tr5' class='level_1 item'>Товары для детей</option></select>
|
12
|
+
</body>
|
13
|
+
</html>
|
data/nested_list.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Dina Zhyliaieva"]
|
10
10
|
s.email = ["din.chick@gmail.com"]
|
11
|
-
s.homepage = ""
|
11
|
+
s.homepage = "https://github.com/dinaionenko/nested_list"
|
12
12
|
s.summary = %q{A Nested list is library that allow to generate a set of options to use it in simple select element.}
|
13
13
|
s.description = %q{A Nested list is library that allow to generate a set of options to use it in simple select element. The options are generated in such way that you can get nested drop-down list saved system style just using simple <select/> element. Run rake nested_list_html_page to generate html page as example.}
|
14
14
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: nested_list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Dina Zhyliaieva
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-03-01 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -26,14 +26,16 @@ extra_rdoc_files: []
|
|
26
26
|
files:
|
27
27
|
- Gemfile
|
28
28
|
- Gemfile.lock
|
29
|
+
- README.md
|
29
30
|
- Rakefile
|
30
31
|
- lib/nested_list.rb
|
31
32
|
- lib/nested_list/nested_select.rb
|
32
33
|
- lib/nested_list/tasks.rb
|
33
34
|
- lib/nested_list/version.rb
|
35
|
+
- lib/public/nested_list.html
|
34
36
|
- nested_list.gemspec
|
35
37
|
has_rdoc: true
|
36
|
-
homepage:
|
38
|
+
homepage: https://github.com/dinaionenko/nested_list
|
37
39
|
licenses: []
|
38
40
|
|
39
41
|
post_install_message:
|