Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246module UI.DnD exposing (Environment, Model, Msg, hasDropped, initialModel, isBeingDraggedOver, isDragging, isDraggingOver, listenToEnterLeave, listenToStart, modelSubject, modelTarget, startDragging, stoppedDragging, update)
import Html exposing (Attribute)import Html.Events.Extra.Mouse as Mouseimport Html.Events.Extra.Pointer as Pointer
-- 🌳
type Model context = NotDragging | Dragging { subject : context } | DraggingOver { subject : context, target : context } | Dropped { subject : context, target : context }
type Msg context = Start context | Enter context | Leave context | Stop
type alias Environment context msg = { model : Model context , toMsg : Msg context -> msg }
type alias Response = { initiated : Bool }
initialModel : Model contextinitialModel = NotDragging
-- 📣
update : Msg context -> Model context -> ( Model context, Response )update msg model = ( ------------------------------------ -- Model ------------------------------------ case msg of Start context -> Dragging { subject = context }
Enter context -> case model of NotDragging -> NotDragging
Dragging { subject } -> DraggingOver { subject = subject, target = context }
DraggingOver { subject } -> DraggingOver { subject = subject, target = context }
Dropped _ -> NotDragging
Leave context -> case model of NotDragging -> NotDragging
Dragging env -> Dragging env
DraggingOver { subject, target } -> if context == target then Dragging { subject = subject }
else model
Dropped _ -> NotDragging
Stop -> case model of DraggingOver { subject, target } -> if subject /= target then Dropped { subject = subject, target = target }
else NotDragging
_ -> NotDragging ------------------------------------ -- Response ------------------------------------ , case msg of Start _ -> { initiated = True }
_ -> { initiated = False } )
-- 🔱 ░░ EVENTS & MESSAGES
listenToStart : Environment context msg -> context -> Attribute msglistenToStart { toMsg } context = Pointer.onWithOptions "pointerdown" { stopPropagation = True , preventDefault = False } (\event -> case ( event.pointer.button, event.isPrimary ) of ( Mouse.MainButton, True ) -> toMsg (Start context)
_ -> toMsg Stop )
listenToEnterLeave : Environment context msg -> context -> List (Attribute msg)listenToEnterLeave { model, toMsg } context = case model of NotDragging -> []
_ -> [ context |> Enter |> toMsg |> always |> Pointer.onEnter , context |> Leave |> toMsg |> always |> Pointer.onLeave ]
startDragging : context -> Msg contextstartDragging = Start
stoppedDragging : Msg contextstoppedDragging = Stop
-- 🔱 ░░ MODEL
isBeingDraggedOver : context -> Model context -> BoolisBeingDraggedOver context model = case model of DraggingOver { target } -> context == target
_ -> False
isDragging : Model context -> BoolisDragging model = case model of NotDragging -> False
Dragging _ -> True
DraggingOver _ -> True
Dropped _ -> False
isDraggingOver : context -> Model context -> BoolisDraggingOver context model = case model of NotDragging -> False
Dragging _ -> False
DraggingOver { target } -> target == context
Dropped _ -> False
hasDropped : Model context -> BoolhasDropped model = case model of Dropped _ -> True
_ -> False
modelSubject : Model context -> Maybe contextmodelSubject model = case model of NotDragging -> Nothing
Dragging { subject } -> Just subject
DraggingOver { subject } -> Just subject
Dropped { subject } -> Just subject
modelTarget : Model context -> Maybe contextmodelTarget model = case model of NotDragging -> Nothing
Dragging _ -> Nothing
DraggingOver { target } -> Just target
Dropped { target } -> Just target