/
page.opa
1649 lines (1480 loc) · 62.6 KB
/
page.opa
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
package opages
import opace
import stdlib.crypto
import stdlib.web.template
import stdlib.web.client
import stdlib.upload
import stdlib.widgets.tabs
import stdlib.widgets.hlist
import stdlib.widgets.core
import stdlib.widgets.notification
/**
* {1 About this module}
* This module provides a function for build an xhtml administration
* interface, with this interface OPAges administrator can add, edit,
* remove... OPAges resources. [Page] module provides also a resources
* function that be able to returns resources added by administrator
* or it build default error resource.
*/
/**
* {1 Types defined in this module}
*/
type Page.env = {
current_url : string
}
/**
* A configuration for initialize a page entity
*/
type Page.config('a, 'b) = {
access : Page.config.access
engine : Page.env -> Template.engine('a, 'b)
}
type Page.config.access = {
/** Read function of stored datas. */
select : (
/** Return the last version of page. */
{last : string} /
/** Return the published version of page. */
{published : string} /
/** Return the [rev] revision of page. */
{key : string rev : int}
) -> option({rev : int page : Page.stored})
/** Write function on stored datas. */
save : (
/** Save a new page revision */
{key : string page : Page.stored} /
/** Set [publish] as publihed revision. */
{key : string publish : Page.published}
) -> int
/** Return info on the published page. */
published : string -> option(Page.published)
/** Remove page stored on corresponding key. */
rm : string -> void
/** List all of stored key associated to corresponding published
page. */
ls : -> list((string, option(Page.published)))
/** Return list of all page stored at given key. (Last stored page
as first element of list). */
history : string -> list(Page.stored)
}
/**
* Define what is a page.
* - embedded means that the page should be embedded in the applications.
* - resource is a subset of [resource] that be able to store in database.
*/
type Page.t =
{embedded : {head : xhtml body : xhtml}} /
{resource : resource}
type Page.manager = {
admin: string, string -> xhtml
resource: string -> Page.t
resource_with_env: string, Page.env -> Page.t
try_resource: string -> option(Page.t)
source : string -> option(string)
}
/**
* Type of data stored in database
*/
type Page.stored_content =
{image : image} /
{css : string} /
{source : string mime : string} /
{binary : binary mime : string} /
/* Contains sourced template */
{hcontent : string
bcontent : string}
@abstract type Page.stored =
{author : string
content : Page.stored_content
date : Date.date
parent : int}
@abstract type Page.published =
{author : string
rev : int
date : Date.date}
/**
* A record used for give some primitive to the admin GUI unless
* publish critical primitives.
*/
@private type Page.access = {
save : string, Page.stored_content,option(int) -> int
set_rev : string, int -> void
get : string -> option((Page.stored,int))
get_edit : string -> Page.edit
get_rev_number : string -> Page.published
remove : string -> void
list : -> list((string,Page.published,bool))
history_list : string -> list((string, Date.date, int))
history_edit : string, int -> option(Page.edit)
}
@private type Page.notify = {
subscribe : ({event : Page.Notification.event by : string} -> void) -> void
subscribe_with : string,({event : Page.Notification.event by : string} -> void) -> void
send : Page.Notification.event -> void
}
@private type Page.locker = {
release : string -> Page.Lock.return
try : string -> Page.Lock.return
check : string -> Page.Lock.return
force_release : string -> Page.Lock.return
}
@private type Page.full_access = {
access : Page.access
notify : Page.notify
locker : Page.locker
}
@private type Page.edit =
{image}
/ {binary
mime:string
save:{mime:string} -> int
set_preview:{mime:string} -> void}
/ {source : string; mime : string}
/ {css : string}
/ {hsource : string
bsource : string
save_template : {hsource : string; bsource : string} -> ({fail:Template.failure} / {rev:int})
preview_template : {hsource : string; bsource : string} -> option(Template.failure)}
type Page.Notification.event =
{connect}
/ {publish : (string,int)}
/ {open : string}
/ {save : string}
/ {remove : string}
/ {lock : string}
/ {release : string}
/ {talk : string}
type Page.Lock.return = outcome(
{lock_at:Date.date by : string},
{internal_error}
/ {locked : {lock_at : Date.date by : string lock_by : ThreadContext.client}}
/ {unlocked}
)
Page = {{
/**
* {2 Privates preview utils}
*/
@server @private preview_context = UserContext.make(StringMap.empty:stringmap(Page.stored_content))
@server @private set_preview(key,page) =
UserContext.change(StringMap.add(key,page,_), preview_context)
@server @private has_preview(key) =
UserContext.execute(StringMap.mem(key,_), preview_context)
@server @private get_preview(key) =
UserContext.execute(StringMap.get(key,_), preview_context)
@server @private del_preview(key) =
UserContext.change(StringMap.remove(key,_), preview_context)
/**
* {2 Privates utils}
*/
/**
* Get mode corresponding to the [file].
*/
@private get_suffix(file) =
match file with
| "" | "/" -> some("html")
| _ -> x = Parser.try_parse(Rule.has_suffix(Rule.alphanum_string), file)
Option.map((x -> x.f2), x)
@private template_config = { Template.default_conf with fallback_to_text_node = false }
@private lock_message(r:Page.Lock.return) = match r
| {success = ~{lock_at by}} -> "Success : lock at {Date.to_string(lock_at)} by {by}"
| {failure = {internal_error}} -> "Failure : lock internal error"
| {failure = {unlocked}} -> "Failure : seems already unlocked"
| {failure = {locked = ~{lock_at lock_by by}}} ->
"Failure : already locked at {Date.to_string(lock_at)} by {by} (with context {lock_by})"
@private
search_dead_link_for_resource(access : Page.full_access , resource_id):option(list(string)) = (
inside_href_parser = (parser | t=((!"\"" .)+) -> Text.to_string(t))
href_parser = (parser | "href=\"" url=inside_href_parser "\"" -> url)
sep = (parser | (!"href=\"" .)+ -> void)
link_parser = (parser | lst={Rule.parse_list_sep(true, href_parser, sep)} sep -> lst)
Option.switch( ({content=stored author=_ date=_ parent=_},_) ->
match stored with
| { ~bcontent; hcontent=_ } -> Parser.try_parse(link_parser, bcontent)
| _ -> none
, none
, access.access.get(resource_id))
)
@private
fetch_dead_links(access : Page.full_access) : stringmap(list(string)) = (
List.foldl((el,_,_), acc ->
do Log.debug("Admin", "Search for dead links in {el}")
match search_dead_link_for_resource(access, el) with
| { ~some } -> StringMap.add(el,some, acc)
| {none } -> acc
, access.access.list()
, StringMap_empty )
)
make_absolute(file, special) =
// Add the starting /
file = match String.index("/", file)
{some=0} -> file
_ -> if special then file else "/{file}"
// Remove the ending /
len = String.length(file)
file = if String.get(len-1, file) == "/" then
String.substring(0, len-1, file)
else file
file
/**
* {2 Database access}
*/
//Hack because we no slicer annot on functor
@client @private get_listener() =
handler(state, msg) = match msg
| ~{handler} -> {set = Map.add(Dom.fresh_id(),handler,state)}
| ~{handler id} -> {set = Map.add(id,handler,state)}
| ~{remove} -> {set = Map.remove(remove,state) }
| ~{event by} -> do Scheduler.push(-> Map.iter(_,handler -> handler(~{event by}), state)) {unchanged}
Session.make(Map.empty, handler)
@server @private ServerAccess(~{engine access=caccess}:Page.config('a, 'b)) = {{
/**
* Network used for the notification systemp
*/
network = Network.empty()
: Network.network(
{ by: string; event: Page.Notification.event }
/ { handler: { by: string; event: Page.Notification.event } -> void }
/ { id : string ; handler: { by: string; event: Page.Notification.event } -> void }
/ { remove : string}
)
/**
* {2 Notification system + Lock system for one user}
*/
Notification_Lock =
lock_cell = Cell.make(StringMap.empty,
(map, message ->
by = message.by
match message.action
| ~{lock} ->
match StringMap.get(lock, map)
| ~{some} -> match ThreadContext.get({current}).key
| ~{client} ->
if client == some.lock_by then
{return = {success={lock_at = some.lock_at ~by}} instruction = {unchanged}}
else
{return = {failure={locked = some}} instruction = {unchanged}}
| _ -> {return = {failure={locked = some}} instruction = {unchanged}}
end
| {none} -> match ThreadContext.get({current}).key
| {client=lock_by} ->
lock_at = Date.now()
{return = {success=~{lock_at by}}
instruction = {set = StringMap.add(lock, ~{by lock_by lock_at}, map)}}
| _ -> {return = {failure={internal_error}} instruction = {unchanged}}
end
end
| ~{unlock force} ->
match StringMap.get(unlock, map)
| {none} -> {return = {failure={unlocked}} instruction = {unchanged}}
| ~{some} -> match ThreadContext.get({current}).key
| ~{client} ->
if force || client == some.lock_by then
{return = {success={lock_at = some.lock_at ~by}}
instruction = {set = StringMap.remove(unlock, map)}}
else
{return = {failure={locked = some}}
instruction = {unchanged}}
| _ -> {return = {failure={internal_error}} instruction = {unchanged}}
end
end
| ~{check} ->
match StringMap.get(check, map)
| ~{some} -> match ThreadContext.get({current}).key
| ~{client} ->
if client == some.lock_by then
{return = {success={lock_at = some.lock_at ~by}} instruction = {unchanged}}
else
{return = {failure={locked = some}} instruction = {unchanged}}
| _ -> {return = {failure={internal_error}} instruction = {unchanged}}
end
| {none} -> {return = {success={lock_at = Date.now() ~by}} instruction = {unchanged}}
)
)
by ->
Notification = {{
@private client_listener =
r = Mutable.make(none)
-> match r.get()
| {none} ->
c = get_listener()
do r.set({some = c})
do Network.add(c, network)
c
| ~{some} -> some
send(event:Page.Notification.event) =
Network.broadcast(~{event by}, network)
subscribe(handler) = Session.send(client_listener(), ~{handler})
subscribe_with(id,handler) = Session.send(client_listener(), ~{handler id})
}}
Lock = {{
@private wrap(x) = {action=x ~by}
@private wrap_result(event, result) = match result
| {success=_} -> do Notification.send(event) result
| _ -> result
try(url) = wrap_result({lock=url}, Cell.call(lock_cell, wrap({lock=url})))
release(url) = wrap_result({release=url}, Cell.call(lock_cell, wrap({unlock=url force=false})))
force_release(url) = wrap_result({release=url}, Cell.call(lock_cell, wrap({unlock=url force=true})))
check(url) = Cell.call(lock_cell, wrap({check=url}))
}}
(Notification, Lock)
access(user) : Page.access =
get(key) : option((Page.stored,int)) = Option.map(~{page rev} -> (page, rev), caccess.select({published = key}))
save(key, page : Page.stored_content, rev_opt) : int =
caccess.save(~{key page = {author=user content=page date=Date.now() parent=(rev_opt ? -1)}})
remove(key) = caccess.rm(key)
list() = List.map((x,p) -> (x, p ? Page.not_published, has_preview(x)) ,caccess.ls())
save_as_template(key, ~{hsource bsource}, rev_opt) =
engine = engine({current_url=""})
match Template.try_parse_with_conf(template_config, engine, hsource)
| ~{failure} -> {fail=failure}
| {success = hcontent} ->
match Template.try_parse_with_conf(template_config, engine, bsource)
| ~{failure} -> {fail=failure}
| {success = bcontent} ->
hcontent = Template.to_source(engine, hcontent)
bcontent = Template.to_source(engine, bcontent)
do Log.notice("PageDebug", "Saving headers {hcontent}")
rev=save(key, ~{hcontent bcontent}, rev_opt)
{~rev}
preview_template(key, ~{hsource bsource}) =
engine = engine({current_url=""})
match Template.try_parse_with_conf(template_config, engine, hsource)
| ~{failure} -> some(failure)
| {success = h} ->
match Template.try_parse_with_conf(template_config, engine, bsource)
| ~{failure} -> some(failure)
| {success = b} ->
page = {hcontent=Template.to_source(engine, h)
bcontent=Template.to_source(engine, b)
}:Page.stored_content
do set_preview(key, page)
none
make_edit(key, {content=stored date=_ author=_ parent=_} : Page.stored, rev) = match stored
| {image=_} -> {image}
| {css=_} as k
| {source=_ mime=_} as k -> k
| ~{binary mime} ->
save(~{mime}) = save(key, ~{binary mime}, some(rev))
set_preview(~{mime}) = set_preview(key, ~{binary mime})
{binary ~mime ~save ~set_preview}
| ~{hcontent bcontent} ->
{
hsource = hcontent
bsource = bcontent
save_template = save_as_template(key, _, some(rev))
preview_template = preview_template(key, _)
} : Page.edit
get_edit(key):Page.edit =
match caccess.select({last = key}) with
| {some=~{rev page}} -> make_edit(key, page, rev)
| {none} ->
match get_suffix(key)
| {some="html"} | {some = "xmlt"}-> {
hsource = "<!-- Enter your headers here -->"
bsource = "<!-- Enter your body here --> "
save_template = save_as_template(key, _, none)
preview_template = preview_template(key, _)
}
| {some="css"} -> {css = "/* Enter your css here */"}
| _ -> {source = "A custom resource" mime="text/plain"}
history_list(key) = List.map(~{author date parent ...} -> (author, date, parent), caccess.history(key))
history_edit(key, rev) = Option.map(~{rev page} -> make_edit(key, page, rev), caccess.select(~{key rev}))
set_rev(key, rev) = ignore(caccess.save(~{key publish = {author=user ~rev date=Date.now()}}))
get_rev_number(key) = caccess.published(key) ? Page.not_published
~{save get get_edit remove list history_edit history_list
set_rev get_rev_number
}
full(user) =
(Notification, Lock) = Notification_Lock(user)
{ access = access(user)
locker = {
try=Lock.try
release=Lock.release
force_release=Lock.force_release
check=Lock.check
} : Page.locker
notify = {
subscribe = Notification.subscribe
subscribe_with = Notification.subscribe_with
send = Notification.send
} : Page.notify
}
}}
/**
* {2 Graphical user interface (admin)}
*/
/**
* This modules provides a build function for administration
* interface.
*/
@private AdminGui = {{
/** Just a simple string transformation for xhtml insert. */
@private file_for_xhtml =
| "" | "/" -> "/"
| x -> x
/** A translation that allows to use file on dom identifiers. */
@private file_id = Crypto.Hash.md5
@private buffer_file_id(file) = "admin_buffer_{file_id(file)}"
@private navigator_file_id(file) = "admin_files_navigator_{file_id(file)}"
@private file_line_preview(file) = "admin_files_navigator_preview{file_id(file)}"
@private file_line_edit(file) ="admin_files_navigator_edit{file_id(file)}"
@private file_line_publish(file) ="admin_files_navigator_publish{file_id(file)}"
/** Tabs. */
tabs_file_id = "tabs_file"
@private @client
on_tab_select(_num:int, tab:WTabs.tab) =
match tab.custom_data
{some=(file)} ->
bid = navigator_file_id(file)
do Dom.trigger(#{bid}, {click})
true
_ -> false
@private @client on_tab_add(num:int, tab:WTabs.tab) =
match tab.custom_data
{some=(file)} ->
fid = navigator_file_id(file)
do Dom.set_property_unsafe(Dom.select_id(fid), "tab", "{num}")
some(tab)
_ -> none
@private @client on_tab_remove(_num:int, tab:WTabs.tab) =
match tab.custom_data
{some=(file)} ->
id = buffer_file_id(file)
fid = navigator_file_id(file)
do Dom.set_property_unsafe(Dom.select_id(fid), "tab", "")
// do access.notify.remove(id)
do Dom.remove(#{id})
do del_preview(file)
do file_line_set_edit(file, false)
true
_ -> true
@private @both insert_pos = {at_end}:WTabs.pos
@private @both delete_pos = {after_select}:WTabs.pos
@private @both
tabs_config = {
WTabs.default_config_with_css(tabs_file_id) with
~insert_pos
~delete_pos
on_add=on_tab_add
on_select=on_tab_select
on_remove=on_tab_remove
add_text=none
} : WTabs.config
@private
tabs_html() = WTabs.html(tabs_config, tabs_file_id, [])
@client
add_tab(file : string, title : string) =
do Log.info("[add_tab]", "{title} {file}")
tab = WTabs.make_tab(
_ -> WTabs.make_constant_content(title),
true, true, false, some(file), tabs_config.remove_text, none)
do WTabs.insert_tab(tabs_config, tabs_file_id, tab)
void
/** File navigator. */
admin_files_id = "admin_files"
on_file_remove(key) =
do Log.info("[on_remove]", "{key}")
li_id = WHList.item_id(admin_files_id, key)
do Log.info("[on_remove]", "{li_id}")
//li = Dom.select_id(li_id)
li_sons = WHList.item_sons_class(admin_files_id)
sons = Dom.select_children(Dom.select_raw("li#{li_id} > ul > li.{li_sons} > ul"))
do Log.info("[on_remove]", "Sons : {sons}")
Dom.is_empty(sons)
file_stringify(x:string) = "{x}"
file_config = {
WHList.default_config_with_css(admin_files_id) with
helper = {
stringify = file_stringify
father = _ -> none
}
on_remove=on_file_remove
} : WHList.config
@private file_line_set_preview(file,b) =
s = if b then "[preview]" else ""
Dom.set_text(#{file_line_preview(file)},s)
@private file_line_set_publish(file,pub_opt) =
s = match pub_opt with | {~some} -> " [pub #{some}]" | {none} -> ""
Dom.set_text(#{file_line_publish(file)},s)
@private file_line_set_edit(file, force) =
id = buffer_file_id(file)
buf = Dom.select_raw("#{id} #{id}_select}")
s = if (Dom.is_empty(buf) || Dom.is_enabled(buf)) && not(force) then "" else "[editing]"
Dom.set_text(#{file_line_edit(file)},s)
@private file_line_content(_access:Page.full_access, name, file, published_rev, preview) =
id = buffer_file_id(file)
<span>
{file_for_xhtml(name)}
<span id={file_line_publish(file)} >{match published_rev with | {~some} -> " [pub #{some}]" | {none} -> ""}</span>
<span id={file_line_preview(file)} >{if preview then"[preview]" else ""}</span>
<span id={file_line_edit(file)} >{
buf = Dom.select_raw("#{id} #{id}_select}")
if (Dom.is_empty(buf) || Dom.is_enabled(buf)) then "" else "[editing]"
}</span>
</span>
@client
toggle_file_sons(file, force) =
key = navigator_file_id(file)
li_id = WHList.item_id(admin_files_id, key)
li_sons = WHList.item_sons_class(admin_files_id)
sons = Dom.select_raw("li#{li_id} > ul > li.{li_sons}")
ssons = Dom.select_raw("li#{li_id} > ul > li.{li_sons} > ul > li")
do Dom.toggle_class(#{li_id}, "toggled")
// do Dom.toggle(sons) // does not seem to work as expected
do if Dom.has_class(#{li_id}, "toggled") then
do Dom.hide(sons)
if Dom.length(ssons) > 0 || force then
_ = Dom.set_text(#{"{navigator_file_id(file)}_toggle"}, "+")
void
else
_ = Dom.set_text(#{"{navigator_file_id(file)}_toggle"}, "")
void
else
do Dom.show(sons)
_ = Dom.set_text(#{"{navigator_file_id(file)}_toggle"}, "-")
void
do Log.info("[toggle_file_sons]","toggle {file}")
void
/** Create a file line for table insert. */
@private file_line(access:Page.full_access, opened, name, file, published_rev, preview) =
class = if opened then "on" else "off"
<span class="{class}"
id="{navigator_file_id(file)}"
onclick={_ ->
do Log.info("[file_line]", "click")
Action.open_file(access, file, published_rev)}
options:onclick={[{stop_propagation}]}>
{file_line_content(access, name, file, published_rev, preview)}
</span>
/** Insert if necessary a file line into files navigator. */
@private file_line_insert(access, opened, file_uri, published_rev, preview, collapse) =
match file_uri
{none} -> void
{some=uri} ->
match uri
~{fragment is_directory is_from_root path query} ->
path = if is_from_root then ["" | path] else path
is_from_root = false
do Log.info("[file_line_insert]", "Path {path}")
_ = List.fold(
e, acc ->
file = Uri.to_string({~fragment ~is_directory ~is_from_root path=List.rev([e|acc]) ~query})
key = navigator_file_id(file)
f = if file == "" then "/" else file
content = file_line(access, opened, e, f, published_rev, preview)
value =
<div>
<span id="{navigator_file_id(file)}_toggle" onclick={_->
toggle_file_sons(file, false)}
options:onclick={[{stop_propagation}]}/>
{content}
</div>
item = WHList.make_item(
{title=e ~value},
{selected=opened; checked=false},
{selectable=true; checkable=false; no_sons=false}
)
do Log.info("[file_line_insert]", "Insert {file} {key} in {List.rev(acc)}")
file_config = { file_config with
helper = {
stringify = file_stringify
father = _ ->
if acc == [] then none
else
father_file = Uri.to_string({~fragment ~is_directory ~is_from_root path=List.rev(acc) ~query})
ff = navigator_file_id(father_file)
do Log.info("[file_line_insert]", "father file: {father_file} {ff}")
some(ff)
}
}
do match WHList.insert_item(file_config, admin_files_id, key, item, none, false) with
~{some} ->
do Log.info("[file_line_insert]", "Insert OK @{some}")
do if collapse && List.length(acc) > 0 then
toggle_file_sons(file, true)
else void
void
{none} ->
_ = WHList.select_item(file_config, admin_files_id, key, false)
Log.info("[file_line_insert]", "Insert KO {key}")
[e|acc]
, path, []
)
void
_ -> void
/**
* Build a buffer and insert it on dom node with identifier
* [id]. The buffer can be specialized according to file
* extension.
*
* Note : Client side function because need dom access.
*/
@client build_buffer(access, file, id) =
do access.notify.send({open = file})
select_id = "{id}_select"
error_id = "{id}_error"
lock_button_id = "{id}_button_lock"
save_button_id = "{id}_button_save"
draft_id = "{id}_draft"
preview_id = "{id}_preview"
editor_id = "{id}_editor"
reporting_id = "{id}_reporting"
command_id = "{id}_command"
published_id = "{id}_published"
buffer_mime_id = "{id}_mime_container"
/* Error reporting */
build_reporting() = Dom.transform([#{reporting_id} <- <div id={error_id}/>])
do_report(msg:xhtml) = ignore(Dom.put_inside(Dom.select_id(error_id), Dom.of_xhtml(msg)))
/* Generate some buttons */
/* Generate ace buffer */
common_ace_init(id_buffer, class, content, mode) =
html = <div id={id_buffer} class={class}/>
_ = Dom.transform([#{editor_id} +<- html])
ace = Ace.edit(id_buffer)
do Option.iter(x -> ignore(Ace.set_mode(ace, x)), mode)
_ = Ace.set_content(ace, content)
do Ace.move_cursor(ace, 0, 0)
do Ace.read_only(ace, false)
ace
on_ok_preview(f) =
do do_report(<></>)
_ = Client.winopen(file, {_blank},[], false)
_ = f()
void
/* Generate history selector */
rec build_pub_version(_rev : option(int)) =
xhtml =
<span> Published revision : {r = access.access.get_rev_number(file)
if r.rev == -1
then "none"
else "{r.rev} - {Date.to_formatted_string(Date.date_only_printer, r.date)} @ {Date.to_formatted_string(Date.time_only_printer, r.date)} by {r.author}"}</span>
Dom.transform([#{published_id} <- xhtml])
and draft()=
do Dom.show(#{draft_id})
do Dom.set_enabled(#{select_id},false)
do Dom.set_enabled(#{save_button_id},true)
do file_line_set_edit(file, false)
void
and make_buttons(rev : option(int),action) =
get_selected_rev() =
Int.of_string(Dom.get_value(Dom.select_id(select_id)))
action_change_rev(_:Dom.event)=
if Dom.get_property(#{select_id},"disable")=={some="true"}
then void
else
do build_buffers(some(get_selected_rev()))
file_line_set_edit(file, false)
action_set_rev(_:Dom.event) =
rev = get_selected_rev()
do access.notify.send({publish=(file,rev)})
do access.access.set_rev(file, rev)
do build_buffers(some(rev))
build_pub_version(some(rev))
/* Build the select input, for selecting file revisions */
build_select(rev) =
hist = access.access.history_list(file)
size = List.length(hist)
r = access.access.get_rev_number(file)
/* Build the map of revisions,
Each cell contains :
- its author
- its date
- its parent revision
- its sons revision
/!\ Note that the keys in DB starts at 1 and not 0 /!\
-- Those who do not have parents have the -1 parent, aka root --
@TODO: should be cached maybe? */
//do Log.info("history", "{hist}")
make_map(i, (author, date, parent), acc) =
i = i+1 // Use the same key as in DB
//do jlog("map {i} - {parent}")
acc = IntMap.add(i, (author, date, parent, []), acc)
match IntMap.get(parent, acc)
{some=(author, date, par, sons)} -> IntMap.add(parent, (author, date, par, [i|sons]), acc)
_ -> IntMap.add(parent, (author, date, -1, [i]), acc)
map = List.foldi(make_map, hist, IntMap.empty)
do Log.info("map", "-- begin revision map")
do IntMap.iter(k, v -> Log.info("map", "{k} -> {v}"), map)
do Log.info("map", "-- end revision map")
/* Build one revision of the file for the select input */
build_rev(key, (author, date, parent, sons), (acc, pad)) =
i = key
based = "" // if parent < 0 then "" else " based on {parent}"
pad = String.repeat(pad, "-")
value = "{pad}#{i} | {Date.to_formatted_string(Date.date_only_printer, date)} @ {Date.to_formatted_string(Date.time_only_printer, date)} by {author}{based}"
value = if r.rev == i then value ^ " [pub]" else value
value = if size == i then value ^ " [last]" else value
default(i : int) : xhtml = (<option value="{i}">{value}</option>)
selected(i : int) : xhtml = (<option value="{i}" selected="selected">{value}</option>)
res = match rev
| {none} -> if i == size then selected(i) else default(i)
| {some=rev} ->if i == rev then selected(i) else default(i)
[res|acc]
/* Auxiliary function for building revision.
First treat the parent revision, then folds on its sons.
There will be a padding if the parent revision has more than one sons */
rec aux(map, rev, (acc, pad, p_sons_len)) =
match IntMap.get(rev, map)
{some=(author, date, parent, sons)} ->
//do jlog("rev {rev}")
acc = if rev < 0 then acc else build_rev(rev, (author, date, parent, sons), (acc, pad))
len = List.length(sons)
pad = if p_sons_len > 1 || len > 1 then pad+1 else pad
List.fold(
rev_son, acc -> aux(map, rev_son, (acc, pad, len))
, sons, acc)
_ -> acc
/* Build the file revisions list,
we know the root is revision 1 because no revision is deleted */
build_revisions(map) =
aux(map, -1, ([], 0, 0))
<select id={select_id} onchange={action_change_rev}>
{build_revisions(map)}
</select>
buttons =
common = <>
<button type="button"
onclick={Action.remove_file(access, file)}>Remove</button>
</>
match action with
| {some = action} ->
make_lock_button(lock, x) =
<button type="button" id={lock_button_id}
onclick={lock}>{x}</button>
rec val lock_button = make_lock_button(action_try_lock, "Lock")
and val unlock_button = make_lock_button(action_release, "Release")
and val force_unlock_button = make_lock_button(action_force_release, "Force Release")
and action_try_lock(_:Dom.event) =
result = access.locker.try(file)
do do_report(<>{lock_message(result)}</>)
match result
| {success=_} ->
do ignore(Dom.put_replace(Dom.select_id(lock_button_id),
Dom.of_xhtml(unlock_button)))
do Dom.set_enabled(Dom.select_id(save_button_id), true)
action.read_only(false)
| _ ->
do Dom.set_enabled(Dom.select_id(save_button_id), false)
do ignore(Dom.put_replace(Dom.select_id(lock_button_id),
Dom.of_xhtml(force_unlock_button)))
action.read_only(true)
and action_release(_:Dom.event) =
result = access.locker.release(file)
do do_report(<>Release {lock_message(result)}</>)
_ = Dom.put_replace(Dom.select_id(lock_button_id),
Dom.of_xhtml(lock_button))
do Dom.set_enabled(Dom.select_id(save_button_id), true)
do action.read_only(false)
void
and action_force_release(_:Dom.event) =
result = access.locker.force_release(file)
do do_report(<>{lock_message(result)}</>)
do Dom.set_enabled(Dom.select_id(save_button_id), true)
do ignore(Dom.put_replace(Dom.select_id(lock_button_id),
Dom.of_xhtml(lock_button)))
do action.read_only(false)
void
and make_preview_button(action,title)=
<button type="button" id="{preview_id}" onclick={action}>
{title}
</button>
and val start_preview = make_preview_button(_->action_start_preview(),"Start Preview")
and val stop_preview = make_preview_button(_->action_stop_preview(), "Stop Preview")
and action_start_preview() =
f() =
do file_line_set_preview(file, true)
Dom.put_replace(Dom.select_id(preview_id),
Dom.of_xhtml(stop_preview))
_ = action.preview(f)
void
and action_stop_preview() =
do del_preview(file)
do file_line_set_preview(file, false)
_ = Dom.put_replace(Dom.select_id(preview_id),
Dom.of_xhtml(start_preview))
void
do access.notify.subscribe_with(id,
| {event={~lock} ~by} ->
if lock != file || Outcome.is_success(access.locker.check(file)) //so bad
then void else
b = Dom.select_id(save_button_id)
do Dom.set_enabled(b, false)
do action.read_only(true)
_ = Dom.put_inside(b, Dom.of_xhtml(<>Save : (locked by {by})</>))
do ignore(Dom.put_replace(Dom.select_id(lock_button_id),
Dom.of_xhtml(force_unlock_button)))
void
| {event={~release} by=_} -> if release != file then void else
b = Dom.select_id(save_button_id)
do Dom.set_enabled(b, true)
do action.read_only(false)
_= Dom.put_inside(b, Dom.of_xhtml(<>Save</>))
do ignore(Dom.put_replace(Dom.select_id(lock_button_id),
Dom.of_xhtml(lock_button)))
void
| {event={publish=(mfile,rev)} by=_ } -> if mfile != file then void else
build_pub_version(some(rev))
| {event={~save} by=_} -> if save != file then void else
build_pub_version(rev)
| _ -> void
)
<>
<div>Selected Revision <span style="display:none" id=#{draft_id}>--Draft--</span>: {build_select(rev)}</div>
<div class="button_group" style="display:inline-block; margin-right:40px" >
{match access.locker.check(file)
| {success = _} ->
<button type="button"
id={save_button_id}
onclick={_ ->
do Dom.set_enabled(#{save_button_id}, false)
_ = action.save()
file_line_set_edit(file, false)} >Save</button>
| ~{failure} ->
by = match failure | {locked = {~by ...}} -> by | _ -> "501??!!??"
<button type="button"
disabled="disabled"
id={save_button_id}
onclick={_ ->
do Dom.set_enabled(#{save_button_id}, false)
_ = action.save()
file_line_set_edit(file, false)}>Save (locked by {by})</button>
}
<button type="button" onclick={action_change_rev}> Discard change </button>
<button type="button"
onclick={action_set_rev}>Publish</button>
</div>
{if has_preview(file)
then stop_preview
else start_preview}
<div class="button_group" style="display:inline-block; margin-right:40px">
{match access.locker.check(file)
| {success = _} -> lock_button
| {failure=_} -> force_unlock_button
}
</div>
<div class="button_group" style="display:inline-block; margin-right:40px">
{common}
</div>
</>
| {none} -> common
Dom.transform([#{command_id} <- buttons])
and build_buffers(rev : option(int)) =
//do Log.info("build_buffers","{rev}")
edit = match rev
| {none} -> access.access.get_edit(file)