touchpage 0.2.2

control panel server with shared-state web controls
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
module SvgXY exposing
    ( Model
    , Msg(..)
    , Spec
    , UpdateMessage
    , UpdateType(..)
    , buildEvtHandlerList
    , encodeUpdateMessage
    , encodeUpdateType
    , getLocation
    , getX
    , getY
    , init
    , jsSpec
    , jsUpdateMessage
    , jsUpdateType
    , onMouseDown
    , onMouseLeave
    , onMouseMove
    , onMouseUp
    , onTouchCancel
    , onTouchEnd
    , onTouchLeave
    , onTouchMove
    , onTouchStart
    , pressedColor
    , resize
    , sliderEvt
    , update
    , updsend
    , view
    )

import Json.Decode as JD
import Json.Encode as JE
import List
import Svg exposing (Attribute, Svg, g, rect, svg, text)
import Svg.Attributes exposing (..)
import Svg.Events exposing (onClick, onMouseDown, onMouseOut, onMouseUp)
import SvgCommand exposing (Command(..))
import SvgTextSize exposing (calcTextSvg, calcTextSvgM, resizeCommand)
import SvgThings exposing (Orientation(..), UiColor(..), UiTheme)
import SvgTouch as ST
import Toop
import VirtualDom as VD


type alias Spec =
    { name : String
    , label : Maybe String
    }


jsSpec : JD.Decoder Spec
jsSpec =
    JD.map2 Spec
        (JD.field "name" JD.string)
        (JD.maybe (JD.field "label" JD.string))


type alias Model =
    { name : String
    , label : String
    , stringWidth : Maybe Float
    , textSvg : List (Svg ())
    , cid : SvgThings.ControlId
    , rect : SvgThings.Rect
    , srect : SvgThings.SRect
    , pressed : Bool
    , location : ( Float, Float )
    , touchonly : Bool
    }


type Msg
    = SvgPress JE.Value
    | SvgUnpress JE.Value
    | NoOp
    | Reply String
    | SvgMoved JE.Value
    | SvgTouch ST.Msg
    | SvgUpdate UpdateMessage


type UpdateType
    = Press
    | Unpress


type alias UpdateMessage =
    { controlId : SvgThings.ControlId
    , updateType : Maybe UpdateType
    , location : Maybe ( Float, Float )
    , label : Maybe String
    }


init :
    SvgThings.Rect
    -> SvgThings.ControlId
    -> Spec
    -> ( Model, Command )
init rect cid spec =
    let
        model =
            Model spec.name
                (Maybe.withDefault "" spec.label)
                Nothing
                []
                cid
                rect
                (SvgThings.SRect (String.fromInt rect.x)
                    (String.fromInt rect.y)
                    (String.fromInt rect.w)
                    (String.fromInt rect.h)
                )
                False
                ( 0.5, 0.5 )
                False
    in
    ( model, resizeCommand model )


pressedColor : Bool -> String
pressedColor pressed =
    case pressed of
        True ->
            "#f000f0"

        False ->
            "#60B5CC"


getX : JD.Decoder Int
getX =
    JD.field "pageX" JD.int


getY : JD.Decoder Int
getY =
    JD.field "pageY" JD.int


encodeUpdateMessage : UpdateMessage -> JD.Value
encodeUpdateMessage um =
    let
        outlist1 =
            [ ( "controlType", JE.string "xy" )
            , ( "controlId", SvgThings.encodeControlId um.controlId )
            ]

        outlist2 =
            case um.updateType of
                Just ut ->
                    List.append outlist1 [ ( "state", encodeUpdateType ut ) ]

                Nothing ->
                    outlist1

        outlist3 =
            case um.location of
                Just ( locx, locy ) ->
                    List.append outlist2 [ ( "location", JE.list JE.float [ locx, locy ] ) ]

                Nothing ->
                    outlist2

        outlist4 =
            case um.label of
                Just txt ->
                    List.append outlist3 [ ( "label", JE.string txt ) ]

                Nothing ->
                    outlist3
    in
    JE.object outlist4


encodeUpdateType : UpdateType -> JD.Value
encodeUpdateType ut =
    case ut of
        Press ->
            JE.string "Press"

        Unpress ->
            JE.string "Unpress"


jsUpdateMessage : JD.Decoder UpdateMessage
jsUpdateMessage =
    JD.map4 UpdateMessage
        (JD.field "controlId" SvgThings.decodeControlId)
        (JD.maybe (JD.field "state" JD.string |> JD.andThen jsUpdateType))
        (JD.maybe
            (JD.field "location"
                (JD.list JD.float
                    |> JD.andThen
                        (\lst ->
                            case lst of
                                [ a, b ] ->
                                    JD.succeed ( a, b )

                                _ ->
                                    JD.fail "location requires exactly two elements, [x,y]"
                        )
                )
            )
        )
        (JD.maybe (JD.field "label" JD.string))


jsUpdateType : String -> JD.Decoder UpdateType
jsUpdateType ut =
    case ut of
        "Press" ->
            JD.succeed Press

        "Unpress" ->
            JD.succeed Unpress

        _ ->
            JD.succeed Unpress



-- get mouse/whatever location from the json message,
-- compute xy location from that.


getLocation : Model -> JD.Value -> Result String ( Float, Float )
getLocation model v =
    let
        xr =
            case JD.decodeValue getX v of
                Ok i ->
                    Ok
                        (toFloat (i - model.rect.x)
                            / toFloat model.rect.w
                        )

                Err e ->
                    Err (JD.errorToString e)

        yr =
            case JD.decodeValue getY v of
                Ok i ->
                    Ok
                        (toFloat (i - model.rect.y)
                            / toFloat model.rect.h
                        )

                Err e ->
                    Err (JD.errorToString e)
    in
    xr
        |> Result.andThen
            (\x ->
                Result.map (\y -> ( x, y )) yr
            )


update : Msg -> Model -> ( Model, Command )
update msg model =
    case msg of
        SvgPress v ->
            case getLocation model v of
                Ok l ->
                    updsend model (Just Press) l

                _ ->
                    ( model, None )

        SvgUnpress v ->
            case model.pressed of
                True ->
                    updsend model (Just Unpress) model.location

                False ->
                    ( model, None )

        NoOp ->
            ( model, None )

        Reply s ->
            ( { model | name = s }, None )

        SvgMoved v ->
            case model.pressed of
                True ->
                    case getLocation model v of
                        Ok l ->
                            updsend model Nothing l

                        _ ->
                            ( model, None )

                False ->
                    ( model, None )

        SvgUpdate um ->
            -- sanity check for ids?  or don't.
            let
                mod =
                    { model
                        | pressed =
                            case um.updateType of
                                Just Press ->
                                    True

                                Just Unpress ->
                                    False

                                _ ->
                                    model.pressed
                        , location =
                            case um.location of
                                Just loc ->
                                    loc

                                Nothing ->
                                    model.location
                    }

                mod2 =
                    case um.label of
                        Just txt ->
                            { mod
                                | label = txt
                                , textSvg = []
                                , stringWidth = Nothing
                            }

                        Nothing ->
                            mod
            in
            ( mod2, resizeCommand mod2 )

        SvgTouch stm ->
            case ST.extractFirstRectTouchSE stm model.rect of
                Nothing ->
                    if model.pressed then
                        updsend model (Just Unpress) model.location

                    else
                        ( model, None )

                Just touch ->
                    let
                        locx =
                            (touch.x - toFloat model.rect.x)
                                / toFloat model.rect.w

                        locy =
                            (touch.y - toFloat model.rect.y)
                                / toFloat model.rect.h

                        loc =
                            ( locx, locy )
                    in
                    if model.pressed then
                        updsend model (Just Press) loc

                    else
                        updsend model Nothing loc


updsend : Model -> Maybe UpdateType -> ( Float, Float ) -> ( Model, Command )
updsend model mbut ( x, y ) =
    let
        lim =
            \loc ->
                if loc > 1.0 then
                    1.0

                else if loc < 0.0 then
                    0.0

                else
                    loc

        limloc =
            ( lim x, lim y )

        prest =
            mbut /= Just Unpress
    in
    -- if nothing changed, no message.
    if model.location == limloc && model.pressed == prest then
        ( model, None )

    else
        let
            um =
                JE.encode 0
                    (encodeUpdateMessage
                        (UpdateMessage model.cid mbut (Just limloc) Nothing)
                    )
        in
        ( { model | location = limloc, pressed = prest }
        , Send um
        )


resize : Model -> SvgThings.Rect -> ( Model, Command )
resize model rect =
    let
        newmodel =
            { model
                | rect = rect
                , srect =
                    SvgThings.SRect (String.fromInt rect.x)
                        (String.fromInt rect.y)
                        (String.fromInt rect.w)
                        (String.fromInt rect.h)
                , textSvg = []
                , stringWidth = Nothing
            }
    in
    ( newmodel, resizeCommand newmodel )



-- VIEW
-- try VD.onWithOptions for preventing scrolling on touchscreens and
-- etc. See virtualdom docs.


sliderEvt : String -> (JD.Value -> Msg) -> VD.Attribute Msg
sliderEvt evtname mkmsg =
    -- VD.onWithOptions evtname (VD.Options True True) (JD.map (\v -> mkmsg v) JD.value)
    VD.on evtname <|
        VD.Custom
            (JD.map
                (\v ->
                    { stopPropagation = True, preventDefault = True, message = mkmsg v }
                )
                JD.value
            )


onMouseDown =
    sliderEvt "mousedown" SvgPress


onMouseMove =
    sliderEvt "mousemove" SvgMoved


onMouseLeave =
    sliderEvt "mouseleave" SvgUnpress


onMouseUp =
    sliderEvt "mouseup" SvgUnpress


onTouchStart =
    sliderEvt "touchstart" (\e -> SvgTouch (ST.SvgTouchStart e))


onTouchEnd =
    sliderEvt "touchend" (\e -> SvgTouch (ST.SvgTouchEnd e))


onTouchCancel =
    sliderEvt "touchcancel" (\e -> SvgTouch (ST.SvgTouchCancel e))


onTouchLeave =
    sliderEvt "touchleave" (\e -> SvgTouch (ST.SvgTouchLeave e))


onTouchMove =
    sliderEvt "touchmove" (\e -> SvgTouch (ST.SvgTouchMove e))


buildEvtHandlerList : Bool -> List (VD.Attribute Msg)
buildEvtHandlerList touchonly =
    let
        te =
            [ onTouchStart
            , onTouchEnd
            , onTouchCancel
            , onTouchLeave
            , onTouchMove
            ]

        me =
            [ onMouseDown
            , onMouseUp
            , onMouseLeave
            , onMouseMove
            ]
    in
    if touchonly then
        te

    else
        List.append me te


view : UiTheme -> Model -> Svg Msg
view theme model =
    let
        size =
            10

        hsize =
            round (size * 0.5)

        sx =
            String.fromInt (round (Tuple.first model.location * toFloat model.rect.w) + model.rect.x - hsize)

        sy =
            String.fromInt (round (Tuple.second model.location * toFloat model.rect.h) + model.rect.y - hsize)

        evtlist =
            buildEvtHandlerList model.touchonly
    in
    g evtlist
        [ rect
            [ x model.srect.x
            , y model.srect.y
            , width model.srect.w
            , height model.srect.h
            , rx "2"
            , ry "2"
            , style ("fill: #" ++ theme.colorString Controls ++ ";")
            ]
            []
        , VD.map (\_ -> NoOp) (g [] model.textSvg)
        , rect
            [ x sx
            , y sy
            , width "10"
            , height "10"
            , rx "2"
            , ry "2"
            , style
                ("fill: #"
                    ++ theme.colorString
                        (if model.pressed then
                            Pressed

                         else
                            Unpressed
                        )
                    ++ ";"
                )
            ]
            []
        ]