tree_html 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f77234678d84b3d33b037d9ee4e751b24bdfc6d7
4
- data.tar.gz: 71bc45162df3b72a4afc35a71a50b0bc60c2af73
2
+ SHA256:
3
+ metadata.gz: 9cbc5537a5398b73c215f6f9c7dfb581681339b3545de6238f5b020fbb94ebe3
4
+ data.tar.gz: '04715792d8c13be85ad51724a2f0ac7c996cd3aba8fb64d18fbf4d73aeff5d7f'
5
5
  SHA512:
6
- metadata.gz: 5870241f2cf857d0d056c7ec7d99db4a7d4f60a0ef1076ce839dd3655f380451d742592d6a3d948f22d20ca38f5b856acef4f43cfc883e95d90e25c043e336f7
7
- data.tar.gz: d017c395743a8251d1422d71cf82304a5036152ce426aabefdc6effe84985787980714a4a9f952de62b64f93a71a52591c0431952fd42bf221ac6a309fce88d8
6
+ metadata.gz: f8f109200650e9a75aef6b0fd3d83cab75ac24c9b4de806508d76706a33a97e2cc6127ab12257af3f6eb484d05dd9c9273a7f1a77635cf7a4a4d4f3f21fc05f1
7
+ data.tar.gz: b502548e6a53e8bef302843747d3da49d5e209870ace083da123713ffd12c324fa2c2cd29448bc7b8d0efa79856741a6089eed113c50ef4c1335286223c8540d
data/README.md CHANGED
@@ -28,3 +28,27 @@ In generated html, hover a branch and press `f`/`u` to fold/unfold it's children
28
28
 
29
29
  Or checkout [test/tree_html_test.rb](https://github.com/turnon/tree_html/blob/master/test/tree_html_test.rb) to see how to use.
30
30
 
31
+ ## Extending
32
+
33
+ You may register more handlers for responsing key press. For example, to do something when hovering on any `li` and pressing `r`
34
+
35
+ ```javascript
36
+ TreeHtml.hover_press('r', function(li){
37
+ // actions
38
+ })
39
+ ```
40
+
41
+ You may also group `li`s with same key under additional `ul`:
42
+
43
+ ```javascript
44
+ var switch = TreeHtmlGroup({
45
+ name: 'by_whatever_key',
46
+ key: function get_path(li) {
47
+ var p = li.querySelector('a').innerText.replace(/* key calculation here */)
48
+ return '<b>' + p + '</b>'
49
+ }
50
+ })
51
+
52
+ switch('by_whatever_key')
53
+ switch('') // switch back
54
+ ```
data/lib/tree_html.rb CHANGED
@@ -7,7 +7,7 @@ module TreeHtml
7
7
  BLANK = ''.freeze
8
8
  NO_CUSTOM_JS = [].freeze
9
9
  Css = File.read File.expand_path('../tree_html/tree_html.css', __FILE__)
10
- Js = File.read File.expand_path('../tree_html/tree_html.js', __FILE__)
10
+ Js = Dir.glob("#{File.expand_path(__dir__)}/tree_html/*.js").map{|f| File.read f }.join(';')
11
11
 
12
12
  def tree_html
13
13
  "<ul class='tree-html'>#{li_for_tree_html}</ul>"
@@ -0,0 +1,88 @@
1
+ var TreeHtmlGroup = (function () {
2
+ var tree = document.querySelector('ul')
3
+ var group_id = 0
4
+ var tree_id_prefix = 0
5
+
6
+ function group_by_key(ul, key_func) {
7
+ var last_key = null
8
+ var lis_groups = []
9
+
10
+ function last_group() {
11
+ return lis_groups[lis_groups.length - 1]
12
+ }
13
+
14
+ var children_count = ul.children.length
15
+ for (var i = 0; i < children_count; i++) {
16
+ var li = ul.children[i]
17
+ callee_path = key_func(li)
18
+ if (last_key !== callee_path) {
19
+ last_key = callee_path
20
+ lis_groups.push([])
21
+ }
22
+ last_group().push(li)
23
+ }
24
+
25
+ if (lis_groups.length == 1 && key_func(lis_groups[0][0]) == key_func(ul.parentElement)) {
26
+ return
27
+ }
28
+
29
+ lis_groups.forEach(function (group) {
30
+ var key = key_func(group[0])
31
+ wrap_into_group(group, key)
32
+ })
33
+ }
34
+
35
+ function wrap_into_group(lis, key) {
36
+ var id = 'g' + group_id++
37
+ var html = '<li>' +
38
+ '<input type="checkbox" id="' + id + '">' +
39
+ '<label for="' + id + '"></label>' +
40
+ '<a>' + key + '</a>' +
41
+ '<ul></ul>' +
42
+ '</li>'
43
+
44
+ lis[0].insertAdjacentHTML('beforebegin', html)
45
+ var ul = document.querySelector('#' + id + ' ~ ul')
46
+ lis.forEach(function (li) {
47
+ ul.insertAdjacentElement('beforeend', li)
48
+ })
49
+ }
50
+
51
+ function switch_tree(name) {
52
+ var id = name === '' ? name : name_to_id(name)
53
+ document.querySelectorAll('.tree-html').forEach(function (tree) {
54
+ if (tree.id !== id) {
55
+ tree.style = 'display:none'
56
+ } else {
57
+ tree.style = ''
58
+ }
59
+ })
60
+ }
61
+
62
+ function name_to_id(name) {
63
+ return 'tree-html-group-' + name
64
+ }
65
+
66
+ return function (opt) {
67
+ var id = name_to_id(opt['name'])
68
+ if (document.getElementById(id)) {
69
+ return switch_tree
70
+ }
71
+
72
+ var id_pattern = '$1p-' + tree_id_prefix++ + '$2$3'
73
+ var li_ids_changed = tree.innerHTML
74
+ .replace(/(<input type="checkbox" id=")(.+?)(">)/, id_pattern)
75
+ .replace(/(<label for=")(.+?)(">)/, id_pattern)
76
+
77
+ tree.insertAdjacentHTML('beforebegin', '<ul id="' + id + '" class="tree-html" style="display:none">' + li_ids_changed + '</ul>')
78
+
79
+ var uls = document.querySelectorAll('#' + id + ',#' + id + ' ul')
80
+ var key_func = opt['key']
81
+ for (var i = uls.length - 1; i > 0; i--) {
82
+ var ul = uls[i]
83
+ group_by_key(ul, key_func)
84
+ }
85
+
86
+ return switch_tree
87
+ }
88
+ })();
File without changes
@@ -1,3 +1,3 @@
1
1
  module TreeHtml
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tree_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - ken
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-07 00:00:00.000000000 Z
11
+ date: 2019-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,8 +68,9 @@ files:
68
68
  - bin/console
69
69
  - bin/setup
70
70
  - lib/tree_html.rb
71
+ - lib/tree_html/group.js
72
+ - lib/tree_html/shortkey.js
71
73
  - lib/tree_html/tree_html.css
72
- - lib/tree_html/tree_html.js
73
74
  - lib/tree_html/version.rb
74
75
  - tree_html.gemspec
75
76
  homepage: https://github.com/turnon/tree_html
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
93
  version: '0'
93
94
  requirements: []
94
95
  rubyforge_project:
95
- rubygems_version: 2.6.8
96
+ rubygems_version: 2.7.6
96
97
  signing_key:
97
98
  specification_version: 4
98
99
  summary: generate plain css tree structure