-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.ex
executable file
·682 lines (538 loc) · 18.4 KB
/
web.ex
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
defmodule Bonfire.UI.Common.Web do
@moduledoc false
# alias Bonfire.Common.Utils
# TODO: configurable
def static_paths,
do:
~w(public assets css fonts images js favicon.ico pwa pwabuilder-sw.js robots.txt cache_manifest.json source.tar.gz index.html)
def verified_routes do
# TODO: fix this occasional compilation error during dev which gets in the way of using verified routes painlessly: `(UndefinedFunctionError) function Bonfire.Web.Router.__verify_route__/1 is undefined (module Bonfire.Web.Router is not available`
# if Code.ensure_loaded?(Bonfire.Web.Endpoint) and Code.ensure_loaded?(Bonfire.Web.Router) do
# # IO.warn("Enabling...")
# quote do
# # use Phoenix.VerifiedRoutes,
# # endpoint: Bonfire.Web.Endpoint,
# # router: Bonfire.Web.Router,
# # statics: Bonfire.UI.Common.Web.static_paths() ++ ["data"]
# # NOTE: the above is the official way to use VerifiedRoutes but `path` conflicts with our existing reverse routing helper
# Phoenix.VerifiedRoutes.__using__(__MODULE__,
# endpoint: Bonfire.Web.Endpoint,
# router: Bonfire.Web.Router,
# statics: Bonfire.UI.Common.Web.static_paths() ++ ["data"]
# )
# import Phoenix.VerifiedRoutes, except: [path: 2, path: 3]
# end
# else
# IO.warn("Disabling...")
quote do
# fallback for when router and/or endpoint are not available
defmacro sigil_p({:<<>>, _meta, _segments} = route, extra) do
quote do
Phoenix.VerifiedRoutes.unverified_path(%URI{}, nil, unquote(route), unquote(extra))
end
end
end
# end
end
def controller(opts \\ []) do
opts =
Keyword.put_new(
opts,
:namespace,
Bonfire.Common.Config.get(:default_web_namespace, Bonfire.UI.Common)
)
quote do
@moduledoc false
use Phoenix.Controller, unquote(opts)
import Plug.Conn
import Phoenix.LiveView.Controller
import Bonfire.UI.Common.Web, only: [render_inline: 2]
alias Bonfire.UI.Common.Plugs.MustBeGuest
alias Bonfire.UI.Common.Plugs.MustLogIn
unquote(live_view_basic_helpers())
end
end
def layout(opts \\ []) do
opts =
opts
|> Keyword.put_new(:namespace, Bonfire.UI.Common.Web)
|> Keyword.put_new(:root, "lib")
# |> maybe_put_layout(:app)
# |> IO.inspect(label: "layoutzzz")
quote do
@moduledoc false
# use Phoenix.View, unquote(opts)
# TODO: switch away from deprecated Phoenix.View?
use Phoenix.Component
# Import convenience functions from controllers
import Phoenix.Controller,
only: [view_module: 1, view_template: 1, get_csrf_token: 0]
# Include shared imports and aliases for views
# import Surface
unquote(live_view_helpers())
end
end
def view(opts \\ []) do
opts =
opts
|> Keyword.put_new(:namespace, Bonfire.UI.Common.Web)
|> Keyword.put_new(:root, "lib")
# |> maybe_put_layout(:app)
quote do
@moduledoc false
use Phoenix.View, unquote(opts)
# use Phoenix.Component # TODO: switch away from deprecated Phoenix.View?
# Import convenience functions from controllers
import Phoenix.Controller,
only: [view_module: 1, view_template: 1, get_csrf_token: 0]
# Include shared imports and aliases for views
import Surface
use Surface.View, unquote(opts)
# to support Surface components in the app layout and in non-LiveViews ^ - FIXME: not compatible with phx 1.7?
unquote(live_view_helpers())
end
end
# defp maybe_put_layout(opts, file) do
# Keyword.put_new(
# opts,
# :layout,
# {Bonfire.Common.Config.get(
# :default_layout_module,
# Bonfire.UI.Common.LayoutView
# ), file}
# )
# end
def layout_view(opts \\ []) do
view(opts)
end
def live_view(opts \\ []) do
# IO.inspect(live_view: opts)
# maybe_put_layout(opts, :live)
opts =
Keyword.put_new(
opts,
:layout,
{Bonfire.UI.Common.LayoutLive, :live}
)
quote do
@moduledoc false
use Phoenix.LiveView, unquote(opts)
@before_compile {Bonfire.UI.Common.Web, :__live_mount_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_params_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_info_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_event_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__render_before_compile__}
unquote(live_view_helpers())
alias Bonfire.UI.Common.LivePlugs
use_if_enabled(LiveViewNative.LiveView)
# on_mount(PhoenixProfiler)
end
end
def live_component(opts \\ []) do
quote do
@moduledoc false
use Phoenix.LiveComponent, unquote(opts)
@before_compile {Bonfire.UI.Common.Web, :__live_update_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_event_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__render_before_compile__}
unquote(live_view_helpers())
use_if_enabled(LiveViewNative.LiveComponent)
# unquote(source_inspector())
end
end
def function_component(opts \\ []) do
quote do
@moduledoc false
use Phoenix.Component, unquote(opts)
unquote(live_view_helpers())
use_if_enabled(LiveViewNative.Component)
# unquote(source_inspector())
end
end
def live_handler(_opts \\ []) do
quote do
import Phoenix.LiveView
import Phoenix.Component
alias Bonfire.UI.Common.ComponentID
alias Phoenix.LiveView.JS
unquote(view_helpers())
end
end
# def source_inspector() do
# quote do
# require SourceInspector
# # mark the component as inspectable
# SourceInspector.debuggable()
# end
# end
def live_plug(_opts \\ []) do
quote do
unquote(common_helpers())
import Phoenix.LiveView
import Phoenix.Component
end
end
def plug(_opts \\ []) do
quote do
unquote(common_helpers())
import Plug.Conn
import Phoenix.Controller
end
end
def router(opts \\ []) do
quote do
use Phoenix.Router, unquote(opts)
unquote(common_helpers())
import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
# unquote(Bonfire.Common.Extend.quoted_use_if_enabled(Thesis.Router))
end
end
def channel(opts \\ []) do
quote do
use Phoenix.Channel, unquote(opts)
import Untangle
end
end
defp common_helpers do
quote do
use Bonfire.UI.Common
# localisation
require Bonfire.Common.Localise.Gettext
import Bonfire.Common.Localise.Gettext.Helpers
# deprecated: Phoenix's Helpers
alias Bonfire.Web.Router.Helpers, as: Routes
# use instead: Bonfire's voodoo routing, eg: `path(Bonfire.UI.Social.FeedsLive):
import Bonfire.Common.URIs
alias Bonfire.Common.Settings
alias Bonfire.Common.Config
import Config, only: [repo: 0]
import Bonfire.Common.Extend
import Untangle
import Bonfire.UI.Common.ErrorHelpers
end
end
defp basic_view_helpers do
quote do
unquote(common_helpers())
# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML
# unquote(Bonfire.Common.Extend.quoted_use_if_enabled(Thesis.View, Bonfire.PublisherThesis.ContentAreas))
import Bonfire.Common.Modularity.DeclareHelpers
unquote(verified_routes())
end
end
defp view_helpers do
quote do
unquote(basic_view_helpers())
# Import basic rendering functionality (render, render_layout, etc)
# import Phoenix.View
# unquote(Bonfire.Common.Extend.quoted_use_if_enabled(Thesis.View, Bonfire.PublisherThesis.ContentAreas))
end
end
defp live_view_helpers do
quote do
unquote(live_view_basic_helpers())
# Import component helpers
import Phoenix.Component
end
end
defp live_view_basic_helpers do
quote do
unquote(view_helpers())
alias Bonfire.UI.Common.ComponentID
alias Phoenix.LiveView.JS
# Import Surface if any dep is using it
Bonfire.Common.Extend.quoted_import_if_enabled(Surface)
end
end
# TODO: similar as the below for update_many and handle_progress
defmacro __live_mount_before_compile__(env) do
live_mount_before_compile(env)
end
defp live_mount_before_compile(env) do
if Module.defines?(env.module, {:mount, 3}) do
quote do
defoverridable mount: 3
def mount(params, session, socket) do
undead_mount(socket, fn ->
super(params, session, socket)
end)
end
end
end
end
defmacro __handle_params_before_compile__(env) do
handle_params_before_compile(env)
end
defp handle_params_before_compile(env) do
if Module.defines?(env.module, {:handle_params, 3}) do
quote do
defoverridable handle_params: 3
def handle_params(params, uri, socket) do
Bonfire.UI.Common.LiveHandlers.handle_params(params, uri, socket, __MODULE__, fn params,
uri,
socket ->
super(params, uri, socket)
end)
end
end
else
quote do
def handle_params(params, uri, socket) do
Bonfire.UI.Common.LiveHandlers.handle_params(params, uri, socket, __MODULE__)
end
end
end
end
defmacro __handle_info_before_compile__(env) do
handle_info_before_compile(env)
end
defp handle_info_before_compile(env) do
if Module.defines?(env.module, {:handle_info, 2}) do
quote do
defoverridable handle_info: 2
def handle_info(msg, socket) do
Bonfire.UI.Common.LiveHandlers.handle_info(msg, socket, __MODULE__, fn msg, socket ->
super(msg, socket)
end)
end
end
else
quote do
def handle_info(msg, socket) do
Bonfire.UI.Common.LiveHandlers.handle_info(msg, socket, __MODULE__)
end
end
end
end
defmacro __handle_event_before_compile__(env) do
handle_event_before_compile(env)
end
defp handle_event_before_compile(env) do
if Module.defines?(env.module, {:handle_event, 3}) do
quote do
defoverridable handle_event: 3
def handle_event(event, params, socket) do
Bonfire.UI.Common.LiveHandlers.handle_event(event, params, socket, __MODULE__, fn event,
params,
socket ->
super(event, params, socket)
end)
end
end
else
quote do
def handle_event(event, params, socket) do
Bonfire.UI.Common.LiveHandlers.handle_event(event, params, socket, __MODULE__)
end
end
end
end
defmacro __live_update_before_compile__(env) do
live_update_before_compile(env)
end
defp live_update_before_compile(env) do
if Module.defines?(env.module, {:update, 2}) do
quote do
defoverridable update: 2
def update(assigns, socket) do
undead_update(socket, fn ->
super(assigns, socket)
end)
end
end
end
end
defmacro __render_before_compile__(env) do
render_before_compile(env)
end
defp render_before_compile(env) do
if Module.defines?(env.module, {:render, 1}) do
quote do
defoverridable render: 1
def render(assigns) do
undead_render(assigns, fn ->
case assigns do
%{__replace_render__with__: _} ->
Bonfire.UI.Common.ErrorComponentLive.replace(assigns)
_ ->
super(assigns)
end
end)
end
end
end
end
if Bonfire.Common.Extend.module_exists?(Surface) do
def surface_live_view_child(opts \\ []) do
opts =
Keyword.put_new(
opts,
:layout,
{Bonfire.UI.Common.LayoutLive, :live}
)
quote do
@moduledoc false
use Surface.LiveView, unquote(opts)
@before_compile {Bonfire.UI.Common.Web, :__live_mount_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_info_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_event_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__render_before_compile__}
unquote(surface_helpers())
use_if_enabled(LiveViewNative.LiveView)
alias Bonfire.UI.Common.LivePlugs
# on_mount(PhoenixProfiler)
end
end
def surface_live_view(opts \\ []) do
opts =
Keyword.put_new(
opts,
:layout,
{Bonfire.UI.Common.LayoutLive, :live}
)
quote do
@moduledoc false
use Surface.LiveView, unquote(opts)
@before_compile {Bonfire.UI.Common.Web, :__live_mount_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_params_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_info_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_event_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__render_before_compile__}
unquote(surface_helpers())
use_if_enabled(LiveViewNative.LiveView)
alias Bonfire.UI.Common.LivePlugs
# on_mount(PhoenixProfiler)
end
end
def stateful_component(opts \\ []) do
quote do
@moduledoc false
use Surface.LiveComponent, unquote(opts)
@before_compile {Bonfire.UI.Common.Web, :__live_update_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__handle_event_before_compile__}
@before_compile {Bonfire.UI.Common.Web, :__render_before_compile__}
# data current_account, :any, from_context: :current_account
# data current_user, :any, from_context: :current_user
# data socket, :any
unquote(surface_component_helpers())
use_if_enabled(LiveViewNative.LiveComponent)
end
end
def stateless_component(opts \\ []) do
quote do
@moduledoc false
@before_compile {Bonfire.UI.Common.Web, :__render_before_compile__}
use Surface.Component, unquote(opts)
# prop current_account, :any, from_context: :current_account
# prop current_user, :any, from_context: :current_user
prop socket, :any
unquote(surface_component_helpers())
use_if_enabled(LiveViewNative.Component)
end
end
def macro_component(opts \\ []) do
quote do
@moduledoc false
alias Surface.MacroComponent
alias Surface.AST
use MacroComponent, unquote(opts)
import Phoenix.Component
prop socket, :any
unquote(surface_component_helpers())
end
end
defp surface_component_helpers do
quote do
unquote(surface_helpers())
# require SourceInspector
# import Bonfire.UI.Common.Testing.Inspector
alias Bonfire.UI.Common.Web
prop source_inspector_attrs, :map, default: %{}
end
end
defp surface_helpers do
quote do
unquote(live_view_basic_helpers())
# alias Surface.Components.Dynamic
# alias Bonfire.UI.Common.Modular.StatelessComponent
# alias Bonfire.UI.Common.Modular.StatefulComponent
alias Surface.Components.Dynamic.Component, as: StatelessComponent
alias Surface.Components.Dynamic.LiveComponent, as: StatefulComponent
alias Surface.Components.Link
alias Surface.Components.Link.Button
# alias Surface.Components.LivePatch
# alias Surface.Components.LiveRedirect
alias Surface.Components.Form
alias Surface.Components.Form.Field
alias Surface.Components.Form.FieldContext
alias Surface.Components.Form.Label
alias Surface.Components.Form.ErrorTag
alias Surface.Components.Form.Inputs
alias Surface.Components.Form.HiddenInput
alias Surface.Components.Form.HiddenInputs
alias Surface.Components.Form.TextInput
alias Surface.Components.Form.TextArea
alias Surface.Components.Form.NumberInput
alias Surface.Components.Form.RadioButton
alias Surface.Components.Form.Select
alias Surface.Components.Form.MultipleSelect
alias Surface.Components.Form.OptionsForSelect
alias Surface.Components.Form.DateTimeSelect
alias Surface.Components.Form.TimeSelect
alias Surface.Components.Form.Checkbox
alias Surface.Components.Form.ColorInput
alias Surface.Components.Form.DateInput
alias Surface.Components.Form.TimeInput
alias Surface.Components.Form.DateTimeLocalInput
alias Surface.Components.Form.EmailInput
alias Surface.Components.Form.PasswordInput
alias Surface.Components.Form.RangeInput
alias Surface.Components.Form.SearchInput
alias Surface.Components.Form.TelephoneInput
alias Surface.Components.Form.UrlInput
alias Surface.Components.Form.FileInput
alias Surface.Components.Form.TextArea
alias Bonfire.UI.Common.LazyImage
alias Bonfire.UI.Common.LinkLive
alias Bonfire.UI.Common.LinkPatchLive
alias Bonfire.UI.Common.C
alias Iconify.Icon
require Iconify.Icon
end
end
end
@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
defmacro __using__({which, opts}) when is_atom(which) and is_list(opts) do
apply(__MODULE__, which, [opts])
end
@doc """
Renders a HEEx template inline in a controller.
## Example
use Bonfire.UI.Common.Web, :controller
use Phoenix.Component
def index(conn, _) do
render_inline conn, ~H"<u><%= @current_user.name %></u>"
end
"""
defmacro render_inline(conn, template) do
quote do
%Plug.Conn{assigns: var!(assigns)} = conn = unquote(conn)
html(conn, Phoenix.HTML.Safe.to_iodata(unquote(template)))
end
end
defmacro maybe_native_plug do
if Bonfire.Common.Extend.extension_enabled?(:live_view_native) do
quote do
plug LiveViewNative.SessionPlug
end
end
end
end