-
-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1758 from mamhoff/sortable-menus
Sortable menus
- Loading branch information
Showing
17 changed files
with
321 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Alchemy.NodeTree = { | ||
onFinishDragging: function (evt) { | ||
var url = Alchemy.routes.move_api_node_path(evt.item.dataset.id) | ||
var data = { | ||
target_parent_id: evt.to.dataset.nodeId, | ||
new_position: evt.newIndex | ||
}; | ||
var ajax = Alchemy.ajax('PATCH', url, data) | ||
|
||
ajax.then(function(response) { | ||
Alchemy.growl('Successfully moved menu item.') | ||
Alchemy.NodeTree.displayNodeFolders() | ||
}).catch(function() { | ||
Alchemy.growl(error.message || error); | ||
}) | ||
}, | ||
|
||
displayNodeFolders: function () { | ||
document.querySelectorAll('li.menu-item').forEach(function (el) { | ||
var leftIconArea = el.querySelector('.nodes_tree-left_images') | ||
var list = el.querySelector('ul') | ||
var node = { folded: el.dataset.folded === 'true', id: el.dataset.id } | ||
|
||
if (list.children.length > 0 || node.folded ) { | ||
leftIconArea.innerHTML = HandlebarsTemplates.node_folder({ node: node }) | ||
} else { | ||
leftIconArea.innerHTML = ' ' | ||
} | ||
}); | ||
}, | ||
|
||
handleNodeFolders: function() { | ||
Alchemy.on('click', '.nodes_tree', '.node_folder', function(evt) { | ||
var nodeId = this.dataset.nodeId | ||
var menu_item = this.closest('li.menu-item') | ||
var url = Alchemy.routes.toggle_folded_api_node_path(nodeId) | ||
var list = menu_item.querySelector('.children') | ||
var ajax = Alchemy.ajax('PATCH', url) | ||
|
||
ajax.then(function() { | ||
list.classList.toggle('folded') | ||
menu_item.dataset.folded = menu_item.dataset.folded == 'true' ? 'false' : 'true' | ||
Alchemy.NodeTree.displayNodeFolders(); | ||
}).catch(function(error){ | ||
Alchemy.growl(error.message || error); | ||
}); | ||
}); | ||
}, | ||
|
||
init: function() { | ||
this.handleNodeFolders() | ||
this.displayNodeFolders() | ||
|
||
document.querySelectorAll('.nodes_tree ul.children').forEach(function (el) { | ||
new Sortable(el, { | ||
group: 'nodes', | ||
animation: 150, | ||
fallbackOnBody: true, | ||
swapThreshold: 0.65, | ||
handle: '.node_name', | ||
invertSwap: true, | ||
onEnd: Alchemy.NodeTree.onFinishDragging | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Alchemy.on = function (eventName, baseSelector, targetSelector, callback) { | ||
var baseNode = document.querySelector(baseSelector) | ||
baseNode.addEventListener(eventName, function (evt) { | ||
var targets = Array.from(baseNode.querySelectorAll(targetSelector)) | ||
var currentNode = evt.target | ||
while (currentNode !== baseNode) { | ||
if (targets.includes(currentNode)) { | ||
callback.call(currentNode, evt) | ||
return | ||
} | ||
currentNode = currentNode.parentElement | ||
} | ||
}); | ||
} | ||
|
||
Alchemy.ajax = function(method, url, data) { | ||
var xhr = new XMLHttpRequest() | ||
var token = document.querySelector('meta[name="csrf-token"]').attributes.content.textContent | ||
var promise = new Promise(function (resolve, reject) { | ||
xhr.onload = function() { | ||
try { | ||
resolve({ | ||
data: JSON.parse(xhr.responseText), | ||
status: xhr.status | ||
}) | ||
} catch (error) { | ||
reject(new Error(JSON.parse(xhr.responseText).error)) | ||
} | ||
}; | ||
xhr.onerror = function() { | ||
reject(new Error(xhr.statusText)) | ||
} | ||
}); | ||
xhr.open(method, url); | ||
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8'); | ||
xhr.setRequestHeader('Accept', 'application/json'); | ||
xhr.setRequestHeader('X-CSRF-Token', token) | ||
if (data) { | ||
xhr.send(JSON.stringify(data)) | ||
} else { | ||
xhr.send() | ||
} | ||
|
||
return promise | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
//= require alchemy/templates/spinner | ||
//= require alchemy/templates/page | ||
//= require alchemy/templates/node_folder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<a class="node_folder" data-node-id="{{ node.id }}"> | ||
<i class="far fa-{{#if node.folded }}plus{{else}}minus{{/if}}-square fa-fw"></i> | ||
</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class Api::NodesController < Api::BaseController | ||
before_action :load_node | ||
before_action :authorize_access, only: [:move, :toggle_folded] | ||
|
||
def move | ||
target_parent_node = Node.find(params[:target_parent_id]) | ||
@node.move_to_child_with_index(target_parent_node, params[:new_position]) | ||
render json: @node, serializer: NodeSerializer | ||
end | ||
|
||
def toggle_folded | ||
@node.update(folded: !@node.folded) | ||
render json: @node, serializer: NodeSerializer | ||
end | ||
|
||
private | ||
|
||
def load_node | ||
@node = Node.find(params[:id]) | ||
end | ||
|
||
def authorize_access | ||
authorize! :update, @node | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class NodeSerializer < ActiveModel::Serializer | ||
attributes :id, | ||
:name, | ||
:lft, | ||
:rgt, | ||
:url, | ||
:parent_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.