bullet_train-sortable 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e3e9f130a8eaf38775f12b57b19eec6e24f09173fe73fe86f21d482c72485432
4
+ data.tar.gz: a03eb64c8764ab04ff8009e3551d85393edbac81798e4126ce371040704aa872
5
+ SHA512:
6
+ metadata.gz: a705a94240af2aa513253152dd5f9e5698a6b24e465f2c96c34f41f6b6050f005b9e4b6c64d8fc3bc0975196b15550bbd1ee76baa34103d30a76b27bffaaefde
7
+ data.tar.gz: 6c5b267403b3cc410e31bd31e6b280339b67f416f6fbc305680f49325bbb5fe8ba2d3a9b0f512ee1dd03da910a98360d57782d80f78c2072ad43ad30ee429344
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Andrew Culver
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # BulletTrain::Sortable
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "bullet_train-sortable"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install bullet_train-sortable
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ module SortableActions
2
+ extend ActiveSupport::Concern
3
+
4
+ def reorder
5
+ params[:ids_in_order].each_with_index do |id, sort_order|
6
+ if (child_object = @parent_object.send(@child_collection).find_by_id(id))
7
+ child_object.sort_order = sort_order
8
+ child_object.save
9
+ end
10
+ end
11
+ render json: true, status: :ok
12
+ end
13
+ end
@@ -0,0 +1,63 @@
1
+ require("dragula/dist/dragula.min.css")
2
+
3
+ import dragula from 'dragula';
4
+
5
+ function saveAssignments($container) {
6
+ var parents = $container.find('[data-reassignment-parent]').map(function(index,element) { return parseInt($(element).attr('data-id')); }).toArray();
7
+ var assignmentsById = {}
8
+ $.each(parents, function(_, parentId) {
9
+ assignmentsById[parentId] = $container.find("[data-reassignment-parent]").filter("[data-id='" + parentId + "']").find("[data-reassignable]").map(function(index,element) { return parseInt($(element).attr('data-id')); }).toArray();
10
+ });
11
+ $.post($container.attr('data-reassign'), {assignments_by_id: assignmentsById, dispatched_at: new Date().getTime()});
12
+ }
13
+
14
+ function enableReassignable($scope) {
15
+ setTimeout(function() {
16
+ var selector = '[data-reassign]';
17
+ var $reassignable = $scope.find(selector).addBack(selector);
18
+
19
+ $reassignable.each(function (_, container) {
20
+
21
+ console.log("🍩 Reassignable 💬 Found the following container:")
22
+ console.log(container);
23
+
24
+ var $container = $(container);
25
+ var dragulaObj = dragula($container.find('[data-reassignment-parent]').toArray(), {
26
+ moves: function(el, container, handle) {
27
+ if ($(handle).hasClass('undraggable') || ($(handle).closest('.undraggable').length > 0)) {
28
+ return false;
29
+ }
30
+ return !!$(handle).closest('[data-reassignable]').length;
31
+ },
32
+ accepts: function (el, target, source, sibling) {
33
+ if ($(sibling).hasClass('undraggable') && $(sibling).prev().hasClass('undraggable')) {
34
+ return false;
35
+ } else {
36
+ return true;
37
+ }
38
+ },
39
+ }).on('drag', function (el) {
40
+ $reassignable.addClass('show-reassignable-targets')
41
+ }).on('drop', function (el) {
42
+ $reassignable.removeClass('show-reassignable-targets')
43
+ saveAssignments($container);
44
+ }).on('cancel', function (el) {
45
+ $reassignable.removeClass('show-reassignable-targets')
46
+ saveAssignments($container);
47
+ }).on('over', function (el, container) {
48
+ $(document.activeElement).blur();
49
+ });
50
+ });
51
+ }, 500);
52
+ }
53
+
54
+ $(document).on('turbo:load', function() {
55
+ console.log("🍩 Reassignable: Enabling on <body> after a Turbo load.")
56
+ enableReassignable($('body'));
57
+ })
58
+
59
+ $(document).on('sprinkles:update', function(event) {
60
+ console.log("🍩 Reassignable: Enabling on the following element after a Sprinkles content update:")
61
+ console.log(event.target);
62
+ enableReassignable($(event.target));
63
+ })
@@ -0,0 +1,70 @@
1
+ require("dragula/dist/dragula.min.css")
2
+
3
+ import dragula from 'dragula';
4
+
5
+ function saveSortOrder($container) {
6
+ var idsInOrder = $container.find('> *').map(function(index,element) { return parseInt($(element).attr('data-id')); }).toArray();
7
+ $.post($container.attr('data-reorder'), {ids_in_order: idsInOrder}, function() {
8
+ if ($container.closest('.opened-modally').length) {
9
+ refreshModalBase();
10
+ }
11
+ });
12
+ }
13
+
14
+ function enableSortable($scope) {
15
+ setTimeout(function() {
16
+ var selector = '[data-reorder]';
17
+ var $reorderable = $scope.find(selector).addBack(selector);
18
+ console.log("enabling sort on array of " + $reorderable.length);
19
+
20
+ $reorderable.each(function (index, container) {
21
+
22
+ var $container = $(container);
23
+
24
+ // enable drag-and-drop reordering.
25
+ var dragulaObj = dragula([container], {
26
+ moves: function(el, container, handle) {
27
+ var $handles = $(el).find('.reorder-handle')
28
+ if ($handles.length) {
29
+ return !!$(handle).closest('.reorder-handle').length
30
+ } else {
31
+ if (!$(handle).closest('.undraggable').length) {
32
+ return $(handle).closest('[data-reorder]')[0] == container;
33
+ } else {
34
+ return false;
35
+ }
36
+ }
37
+ },
38
+ accepts: function (el, target, source, sibling) {
39
+ if ($(sibling).hasClass('undraggable') && $(sibling).prev().hasClass('undraggable')) {
40
+ return false;
41
+ } else {
42
+ return true;
43
+ }
44
+ },
45
+ }).on('drop', function (el) {
46
+
47
+ // save order here.
48
+ saveSortOrder($container);
49
+
50
+ }).on('over', function (el, container) {
51
+
52
+ // deselect any text fields, or else things go slow!
53
+ $(document.activeElement).blur();
54
+
55
+ });
56
+
57
+ });
58
+ }, 500);
59
+ }
60
+
61
+ $(document).on('turbo:load', function() {
62
+ console.log("🍩 Sortable: Enabling on <body> after a Turbo load.")
63
+ enableSortable($('body'));
64
+ })
65
+
66
+ $(document).on('sprinkles:update', function(event) {
67
+ console.log("🍩 Sortable: Enabling on the following element after a Sprinkles content update:")
68
+ console.log(event.target);
69
+ enableSortable($(event.target));
70
+ })
@@ -0,0 +1,14 @@
1
+ module Sortable
2
+ extend ActiveSupport::Concern
3
+
4
+ # define relationships.
5
+ included do
6
+ # Yes, everyone hates default scopes, but they for sure make sense here.
7
+ # See the thread at https://twitter.com/andrewculver/status/1405900896664313867?s=20 for more context.
8
+ default_scope -> { order(:sort_order) }
9
+
10
+ before_create do
11
+ self.sort_order ||= (collection.maximum(:sort_order) || -1) + 1
12
+ end
13
+ end
14
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,6 @@
1
+ module BulletTrain
2
+ module Sortable
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module BulletTrain
2
+ module Sortable
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "bullet_train/sortable/version"
2
+ require "bullet_train/sortable/engine"
3
+
4
+ module BulletTrain
5
+ module Sortable
6
+ # Your code goes here...
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :bullet_train_sortable do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bullet_train-sortable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Culver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ description: Bullet Train Sortable
28
+ email:
29
+ - andrew.culver@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/config/bullet_train_sortable_manifest.js
38
+ - app/controllers/concerns/sortable_actions.rb
39
+ - app/javascript/concerns/reassignable.js
40
+ - app/javascript/concerns/sortable.js
41
+ - app/models/concerns/sortable.rb
42
+ - config/routes.rb
43
+ - lib/bullet_train/sortable.rb
44
+ - lib/bullet_train/sortable/engine.rb
45
+ - lib/bullet_train/sortable/version.rb
46
+ - lib/tasks/bullet_train/sortable_tasks.rake
47
+ homepage: https://github.com/bullet-train-co/bullet_train-sortable
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/bullet-train-co/bullet_train-sortable
52
+ source_code_uri: https://github.com/bullet-train-co/bullet_train-sortable
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.2.22
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Bullet Train Sortable
72
+ test_files: []