Skip to content

Commit

Permalink
Remove sortable drag handle on mobile device
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Dec 27, 2023
1 parent 86cb871 commit bf341f4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base
include Pagy::Backend
include SessionsHelper

helper_method :native_app?, :need_transcode?, :render_flash
helper_method :native_app?, :need_transcode?, :render_flash, :mobile?

before_action :find_current_session
before_action :find_current_request_details
Expand Down Expand Up @@ -78,6 +78,10 @@ def render_flash(type: :success, message: "")
turbo_stream.update "turbo-flash", partial: "shared/flash"
end

def mobile?
browser.device.mobile?
end

private

def find_current_session
Expand Down
7 changes: 5 additions & 2 deletions app/javascript/controllers/playlist_sortable_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { fetchRequest } from '../helper'
import { installEventHandler } from './mixins/event_handler'

export default class extends Controller {
DRAG_HANDLE_SELECTOR = '.js-playlist-sortable-item-handle'

initialize () {
installEventHandler(this)
}
Expand Down Expand Up @@ -47,7 +49,7 @@ export default class extends Controller {
}

#handleMouseDown = (event) => {
const handle = event.target.closest('.js-playlist-sortable-item-handle')
const handle = event.target.closest(this.DRAG_HANDLE_SELECTOR)

if (handle) {
this.dragHandle = handle
Expand All @@ -58,8 +60,9 @@ export default class extends Controller {

#handleDragStart = (event) => {
const target = event.target
const handleExists = target.querySelector(this.DRAG_HANDLE_SELECTOR)

if (!target.draggable || !target.contains(this.dragHandle)) {
if (!target.draggable || (handleExists && !target.contains(this.dragHandle))) {
event.preventDefault()
return
}
Expand Down
4 changes: 3 additions & 1 deletion app/views/current_playlist/songs/_song.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
data-test-id='current_playlist_song'>

<div class='o-flex o-flex--align-center u-py-narrow'>
<button class='c-button c-button--icon js-playlist-sortable-item-handle u-mr-narrow' data-test-id='playlist_song_sortable_handle'><%= icon_tag "drag-indicator", size: "small" %></button>
<% unless mobile? %>
<button class='c-button c-button--icon js-playlist-sortable-item-handle u-mr-narrow' data-test-id='playlist_song_sortable_handle'><%= icon_tag "drag-indicator", size: "small" %></button>
<% end %>

<button class='c-button c-button--link o-flex__item--grow-1' data-delegated-action='current-playlist-songs#play'>
<div class='o-flex o-flex--justify-between o-flex--align-center'>
Expand Down
5 changes: 4 additions & 1 deletion app/views/playlists/songs/_song.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<li id='<%= "#{dom_id(playlist)}_#{dom_id(song)}" %>' class='c-list__item u-p-0' data-playlist-songs-target='item' data-song-id='<%= song.id %>' data-test-id='playlist_song' draggable='true'>
<div class='o-flex o-flex--align-center u-py-narrow'>
<button class='c-button c-button--icon js-playlist-sortable-item-handle u-mr-narrow' data-test-id='playlist_song_sortable_handle'><%= icon_tag "drag-indicator", size: "small" %></button>
<% unless mobile? %>
<button class='c-button c-button--icon js-playlist-sortable-item-handle u-mr-narrow' data-test-id='playlist_song_sortable_handle'><%= icon_tag "drag-indicator", size: "small" %></button>
<% end %>
<%= button_to(
current_playlist_songs_path(song_id: song.id, should_play: true),
class: "c-button c-button--link u-w-100",
Expand Down

0 comments on commit bf341f4

Please sign in to comment.