mini_tree 0.0.4 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfcc12efe875d58a64a005b8bcf01c4287521f929dafe941e55abfe0774bd64e
4
- data.tar.gz: 6314c4eecfbe0c7cf0df0b2db8e952515bb3bba14158c94aeb250eb89f5763e4
3
+ metadata.gz: 12d6779a95c606ab979615288138df0cc0b95a7258762c3958e951c66e27b31e
4
+ data.tar.gz: 9f7e59d5f541ec24dcbfd3fac9a5215c0f25df775b59eaec0ede937b6b05cbd4
5
5
  SHA512:
6
- metadata.gz: 264d652696e5c73cfda4b8b6e7094e9b8596d9ba30bf71be3361712845a41929fab44017fda6d6b9834b7c8eecf73ab3c5ca74469def6cb35119dcd1698a21d5
7
- data.tar.gz: f3139e7a2cd685933b07341c2503aa3ba553c0279cfd6e52d41fed823f1d3eb7e8a0351f83bd7a29c6aa40280e2494133e6f74a9fab2c9aa0c77b03c01b71b0f
6
+ metadata.gz: 44cd57701e82393b97a40dba307761f5cab89f85300f238e04674063b292c379d54ae57f751311c56280b83fa171a0061dff3c553769ca843270249d6091f27f
7
+ data.tar.gz: 1b67d0c973790ff6a20fefff8ad9a7bd797b405db72d75758d4dff00a73d6ccf6a99cf943675309642efc8a09e529fbe6a45309806ed2c602c8bbcec51173bcd
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2025 Dittmar Krall (www.matiq.com)
1
+ Copyright (c) 2025-2026 Dittmar Krall (www.matiq.com)
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,5 +1,3 @@
1
- # UNDER CONSTRUCTION
2
-
3
1
  # MiniTree
4
2
 
5
3
  [![Gem Version](https://img.shields.io/gem/v/mini_tree?color=168AFE&logo=rubygems&logoColor=FE1616)](https://rubygems.org/gems/mini_tree)
@@ -65,9 +63,9 @@ end
65
63
 
66
64
  You may specify your view of an item in the treeview:
67
65
  ~~~Ruby
68
- # ./app/views/mini_trees/_mini_tree_title.html.erb
66
+ # ./app/views/application/_mini_tree_title.html.erb
69
67
  # id and legend are defined
70
- <%= link_to "action", edit_<model>(id:), class: 'button' %>
68
+ <%= link_to "action", edit_<model>(id:) %>
71
69
  <%= legend %>
72
70
  ~~~
73
71
 
@@ -75,9 +73,9 @@ You may specify your view of an item in the treeview:
75
73
 
76
74
  ~~~Ruby
77
75
  <Model>Tree.refresh
78
- <Model>Tree.refresh_item(<id>, <legend>)
79
76
  <Model>Tree.create_item(<id>, <legend>)
80
- <Model>Tree.del_item(<id>)
77
+ <Model>Tree.destroy_item(<id>)
78
+ <Model>Tree.update_item(<id>, <legend>)
81
79
  ~~~
82
80
 
83
81
  ## Usage
@@ -103,7 +101,7 @@ gem "mini_tree"
103
101
  and run "bundle install".
104
102
 
105
103
  Furthermore, copy manually *app/javascript/controllers/tree_controller.js*
106
- from the _gem mini-tree_
104
+ from the _gem mini_tree_
107
105
  into your own _app/javascript/controllers/_ directory.
108
106
 
109
107
 
@@ -112,7 +110,7 @@ into your own _app/javascript/controllers/_ directory.
112
110
  This software has been developed and tested with:
113
111
  - Ubuntu 24.04
114
112
  - Ruby 3.4.7
115
- - Rails 8.1.0
113
+ - Rails 8.1.1
116
114
 
117
115
  See also:
118
116
  - ./.github/workflows/rake.yml
@@ -131,6 +129,6 @@ If you are curious you may search in particular for:
131
129
 
132
130
  ## License
133
131
 
134
- Copyright (c) 2025 Dittmar Krall (www.matiq.com),
132
+ Copyright (c) 2025-2026 Dittmar Krall (www.matiq.com),
135
133
  released as open source under the terms of the
136
134
  [MIT license](https://opensource.org/licenses/MIT).
@@ -5,8 +5,8 @@ export default class extends Controller {
5
5
  static values = {owner: String}
6
6
 
7
7
  connect() {
8
- this.expanded = "\u25BE" // down triangle
9
- this.collapsed = "\u25b8" // right triangle
8
+ this.expanded = "\u25BC" // down triangle
9
+ this.collapsed = "\u25bA" // right triangle
10
10
  this.itemTargets.forEach(item => this.attach(item))
11
11
  }
12
12
 
@@ -20,7 +20,6 @@ export default class extends Controller {
20
20
  dragStart(e, el) {
21
21
  e.stopPropagation()
22
22
 
23
- // ev.dataTransfer.setData("text/plain", "")
24
23
  this.dragged = el
25
24
  e.dataTransfer.effectAllowed = "move"
26
25
  el.classList.add("dragging")
@@ -30,10 +29,35 @@ export default class extends Controller {
30
29
  e.stopPropagation()
31
30
  this.dragged.classList.remove("dragging")
32
31
  this.dragged = null
32
+ this.removeTraversed()
33
33
  }
34
34
 
35
35
  dragOver(e, el) {
36
36
  e.preventDefault()
37
+ e.stopPropagation()
38
+ this.removeTraversed()
39
+
40
+ const title = el.querySelector("span.title")
41
+ if (this.topPart(e, el)) {
42
+ title.classList.add("topline")
43
+ } else {
44
+ title.classList.add("bottomline")
45
+ }
46
+ this.traversed = title
47
+ }
48
+
49
+ removeTraversed() {
50
+ if (!this.traversed) { return }
51
+
52
+ this.traversed.classList.remove("topline")
53
+ this.traversed.classList.remove("bottomline")
54
+ this.traversed = null
55
+ }
56
+
57
+ topPart(event, elem) {
58
+ const rect = elem.getBoundingClientRect()
59
+ const offset = event.clientY - rect.top
60
+ return offset < (rect.height / 3) * 2
37
61
  }
38
62
 
39
63
  drop(e) {
@@ -60,11 +84,7 @@ export default class extends Controller {
60
84
  }
61
85
  }
62
86
 
63
- const rect = target.getBoundingClientRect()
64
- const offset = e.clientY - rect.top
65
- const threshold = (rect.height / 3) * 2
66
-
67
- const target2 = (offset > threshold) ? target.nextSibling : target
87
+ const target2 = this.topPart(e, target) ? target : target.nextSibling
68
88
  target.parentNode.insertBefore(dragged, target2)
69
89
  }
70
90
 
@@ -94,8 +114,10 @@ export default class extends Controller {
94
114
 
95
115
  toNode(elem) {
96
116
  var row = elem.firstElementChild
97
- const newRow = this.create("button", {className: "toggle-btn",
98
- type: "button", textContent: this.expanded})
117
+ const newRow = this.create("a", {className: "toggle-btn",
118
+ textContent: this.expanded})
119
+ // type: "button", textContent: this.expanded})
120
+ newRow.dataset.action = "click->tree#toggle"
99
121
 
100
122
  this.removeUl(elem)
101
123
  const ulRow2 = this.create("ul", {className: "nested"})
@@ -212,41 +234,11 @@ export default class extends Controller {
212
234
  }
213
235
  }
214
236
 
215
-
216
-
217
- // over(e, el) {
218
- //console.log("over")
219
- // e.preventDefault()
220
- // if (this.dragged === el) return
221
- // const rect = el.getBoundingClientRect()
222
- // const offset = e.clientY - rect.top
223
- // const third = rect.height / 3
224
- //
225
- // if (offset < third) {
226
- // el.parentNode.insertBefore(this.placeholder, el)
227
- // } else if (offset > 2 * third) {
228
- // el.parentNode.insertBefore(this.placeholder, el.nextSibling)
229
- // } else {
230
- // let ul = el.querySelector(".nested")
231
- // if (!ul) {
232
- // ul = document.createElement("ul")
233
- // ul.className = "nested"
234
- // el.appendChild(ul)
235
- // }
236
- // ul.classList.remove("hidden")
237
- // ul.appendChild(this.placeholder)
238
- // }
239
- // }
240
- //
241
- // removePlaceholder() {
242
- // this.placeholder?.remove()
243
- // }
244
- //
245
- //
246
- // // rails javascript no jquery no sortablejs use stimulus sortable treeview
247
- // // expand collapse support
248
- // // nested reordering
237
+ // rails javascript no jquery no sortablejs use stimulus sortable treeview
238
+ // expand collapse support
239
+ // nested reordering
249
240
  //
250
241
  //
251
- // // https://wiki.selfhtml.org/wiki/JavaScript/Drag_%26_Drop
252
- // // https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
242
+ // https://wiki.selfhtml.org/wiki/JavaScript/Drag_%26_Drop
243
+ // https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
244
+ // simple rails 8 program with two javascript classes
@@ -1,8 +1,17 @@
1
1
  <style>
2
2
  .mini-tree {
3
- xbackground: yellow;
4
3
  .mini-tree-view, .nested {
5
4
  list-style: none;
5
+ }
6
+ span.title.topline {
7
+ border-top: solid 2px red;
8
+ border-top: solid 1px red;
9
+ }
10
+ span.title.bottomline {
11
+ border-bottom: solid 2px red;
12
+ border-bottom: solid 1px red;
13
+ }
14
+ .nested {
6
15
  padding-left: 1rem;
7
16
  }
8
17
  .tree-row {
@@ -11,7 +20,7 @@
11
20
  }
12
21
  .toggle-btn {
13
22
  background: transparent;
14
- border: none;
23
+ cursor: pointer;
15
24
  }
16
25
  .dragging { opacity: 0.5; }
17
26
  .hidden { display: none; }
@@ -25,6 +34,7 @@
25
34
  owner = list.first.class.name[0..-5]
26
35
  %>
27
36
  <div class="mini-tree" data-item-id="0"
37
+ data-turbo-prefetch="false"
28
38
  data-controller="tree" data-tree-owner-value="<%= owner %>">
29
39
  <ul class="mini-tree-view" data-tree-target="top">
30
40
  <%= render partial: "mini_trees/item",
@@ -1,13 +1,14 @@
1
1
  <%
2
- icon = (item.collapsed ? "\u25b8" : "\u25be").html_safe
2
+ icon = (item.collapsed ? "\u25ba" : "\u25bc").html_safe
3
3
  klass = item.collapsed ? "nested hidden" : "nested"
4
4
  %>
5
5
 
6
6
  <li class="tree-item" data-tree-target="item"
7
7
  data-item-id="<%= item.id %>" draggable="true">
8
- <div class="tree-row" data-action="click-&gt;tree#toggle">
8
+ <div class="tree-row">
9
9
  <% if item.kind == "node" %>
10
- <button class="toggle-btn" type="button"><%= icon %></button>
10
+ <a class="toggle-btn" type="button"
11
+ data-action="click-&gt;tree#toggle"> <%= icon %> </a>
11
12
  <% else %>
12
13
  <span class="toggle-space"></span>
13
14
  <% end %>
@@ -23,5 +24,4 @@
23
24
  <%= render partial: "mini_trees/item", collection: item.children %>
24
25
  </ul>
25
26
  <% end %>
26
-
27
27
  </li>
@@ -24,28 +24,35 @@ module MiniTree::Utils
24
24
  missing_cnt += 1
25
25
  }
26
26
  (ids - owner_ids).each { |id|
27
- del_item(id)
27
+ destroy_item(id)
28
28
  delete_cnt += 1
29
29
  }
30
30
  (owner_ids & ids).each { |id|
31
- refresh_item(id, owner_class.find_by(id:).legend)
31
+ update_item(id, owner_class.find_by(id:).legend)
32
32
  refresh_cnt += 1
33
33
  }
34
34
  [missing_cnt, delete_cnt, refresh_cnt]
35
35
  end
36
36
 
37
- def refresh_item(id, legend)
38
- find_by(id:).update!(legend:)
39
- end
40
-
41
37
  def create_item(id, legend)
42
38
  create! id:, legend:, parent_id: 0, position: id, kind: "leaf"
43
39
  end
44
40
 
45
- def del_item(id)
41
+ def destroy_item(id)
46
42
  where(id:).delete_all
47
43
  end
48
44
 
45
+ def update_item(id, legend)
46
+ find_by(id:).update!(legend:)
47
+ end
48
+
49
+ def ancestors(id)
50
+ parent_id = find(id).parent_id
51
+ return [id] if parent_id == 0
52
+
53
+ ancestors(parent_id) << id
54
+ end
55
+
49
56
  # used by tests
50
57
  def flat
51
58
  res = []
@@ -1,5 +1,8 @@
1
1
  module MiniTree
2
- VERSION = "0.0.4" # 2025-10-29
2
+ VERSION = "0.1.2" # 2026-01-03
3
+ # VERSION = "0.1.1" # 2025-12-06
4
+ # VERSION = "0.1.0" # 2025-12-06
5
+ # VERSION = "0.0.4" # 2025-10-29
3
6
  # VERSION = "0.0.3" # 2025-10-28
4
7
  # VERSION = "0.0.2" # 2025-10-27
5
8
  # VERSION = "0.0.1" # 2025-10-26
@@ -1,5 +1,8 @@
1
1
  module MiniTree
2
- VERSION = "0.0.3" # 2025-10-28
2
+ VERSION = "0.1.1" # 2025-12-06
3
+ # VERSION = "0.1.0" # 2025-12-06
4
+ # VERSION = "0.0.4" # 2025-10-29
5
+ # VERSION = "0.0.3" # 2025-10-28
3
6
  # VERSION = "0.0.2" # 2025-10-27
4
7
  # VERSION = "0.0.1" # 2025-10-26
5
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dittmar Krall
@@ -115,7 +115,7 @@ require_paths:
115
115
  - lib
116
116
  required_ruby_version: !ruby/object:Gem::Requirement
117
117
  requirements:
118
- - - "~>"
118
+ - - ">="
119
119
  - !ruby/object:Gem::Version
120
120
  version: '3'
121
121
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
126
  requirements: []
127
- rubygems_version: 3.6.9
127
+ rubygems_version: 4.0.3
128
128
  specification_version: 4
129
129
  summary: MiniTree a simple TreeView Rails 8+ gem.
130
130
  test_files: []