forked from allegory-software/allegory-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx-widgets.js
3355 lines (2794 loc) · 79.1 KB
/
x-widgets.js
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
/*
X-WIDGETS: Web Components in JavaScript.
Written by Cosmin Apreutesei. Public Domain.
WIDGETS
tooltip
button
menu
widget_placeholder
tabs
split vsplit
slides
toaster
action_band
dialog
toolbox
pagenav
richtext
if
setglobal
GLOBALS
component(tag[, category], cons)
notify(text, ['search'|'info'|'error'], [timeout])
set_theme(theme|null)
*/
DEBUG_ATTACH_TIME = false
function set_theme(theme) {
on_dom_load(function() {
theme = repl(theme, 'default', null)
document.body.attr('theme', theme)
document.fire('theme_changed', theme)
})
}
/* ---------------------------------------------------------------------------
// creating & setting up web components
// ---------------------------------------------------------------------------
publishes:
e.isinstance: t
e.iswidget: t
e.type
e.initialized: t|f
calls:
e.init()
fires:
^window.'widget_bind' (e, on)
^window.'ID.bind' (e, on)
^window.'ID.init' (e)
--------------------------------------------------------------------------- */
{
let set_attr_func = function(e, k, opt) {
if (opt.to_attr)
return (v) => e.attr(k, v)
if (opt.type == 'bool')
return (v) => e.bool_attr(k, v || null)
return (v) => e.attr(k, v)
}
let from_bool_attr = v => repl(repl(v, '', true), 'false', false)
let from_attr_func = function(opt) {
return opt.from_attr
|| (opt.type == 'bool' && from_bool_attr)
|| (opt.type == 'number' && num)
}
let attr_val_opt = function(e) {
let opt = obj()
for (let attr of e.attrs) {
let k = attr.name
// TODO: not cool that we must add all built-in attrs that we use for
// custom components here (so that they aren't set twice, and wrong too
// because text attr vals don't convert well to bool props, eg. `hidden`).
if (k != 'id' && k != 'class' && k != 'style' && k != 'hidden') {
let v = attr.value
let popt = e.get_prop_attrs(k)
if (popt && popt.from_attr)
v = popt.from_attr(v)
if (!(k in opt))
opt[k] = v
}
}
return opt
}
// component(tag[, category], cons) -> create({option: value}) -> element.
function component(tag, category, cons) {
if (!isstr(category)) {
cons = category
category = 'Other'
}
let type = tag.replace(/^x-/, '').replaceAll('-', '_')
// override cons() so that calling `parent_widget.construct()` always
// sets the css class for parent_widget.
function construct(e, ...args) {
e.class(tag)
return cons(e, ...args)
}
function initialize(e, ...args) {
e.bool_attr('_bind', true)
e.do_bind = function(on) {
if (on) {
let t0 = DEBUG_ATTACH_TIME && time()
this.fire('bind', true)
if (this.id) {
window.fire('widget_bind', this, true)
window.fire(this.id+'.bind', this, true)
}
if (DEBUG_ATTACH_TIME) {
let t1 = time()
let dt = (t1 - t0) * 1000
if (dt > 10)
debug((dt).dec().padStart(3, ' ')+'ms', this.debug_name())
}
this.end_update()
} else {
this.begin_update()
this.fire('bind', false)
if (this.id) {
window.fire('widget_bind', this, false)
window.fire(this.id+'.bind', this, false)
}
}
}
component_deferred_updating(e)
e.begin_update() // bind(true) calls end_update().
e.update() // ...deferred on first call to bind().
if (DEBUG_ATTACH_TIME)
e.debug_name = function(prefix) {
prefix = (prefix && prefix + ' < ' || '') + this.type + (this.id ? ' ' + this.id : '')
let p = this; do { p = p.popup_target || p.parent } while (p && !p.debug_name)
if (!(p && p.debug_name))
return prefix
return p.debug_name(prefix)
}
e.initialized = null // for log_add_event().
// combine initial prop values from multiple sources, in overriding order:
// - html attributes.
// - constructor return value.
// - constructor args.
let opt = assign_opt(obj(), ...args)
component_props(e, opt.props)
e.isinstance = true // because we can have non-widget instances.
e.iswidget = true // to diff from normal html elements.
e.type = type // for serialization.
e.init = noop // init point after all props are set.
e.class('x-widget')
disablable_widget(e)
let cons_opt = construct(e)
opt = assign_opt(attr_val_opt(e), cons_opt, opt)
e.initialized = false // setter barrier to delay init to e.init().
// register events from the options directly.
if (opt.on) {
for (let k in opt.on)
e.on(k, opt.on[k])
delete opt.on
}
// set props to combined initial values.
if (window.xmodule) {
xmodule.init_instance(e, opt)
} else {
for (let k in opt)
e.set_prop(k, opt[k])
}
// call the after-properties-are-set init function.
e.initialized = true
e.init()
if (e.id)
window.fire(e.id+'.init', e)
}
bind_component(tag, initialize)
function create(...args) {
let e = document.createElement(tag)
initialize(e, ...args)
return e
}
create.type = type
create.construct = construct
attr(component.categories, category, array).push(create)
component.types[type] = create
window[type] = create
return create
}
component.types = {} // {type -> create}
component.categories = {} // {cat -> {craete1,...}}
component.create = function(t, e0) {
if (iselem(t) || (isobject(t) && t.isinstance))
return t // instances pass through.
let id = isstr(t) ? t : t.id
if (e0 && e0.id == id)
return e0 // already created (called from a prop's `convert()`).
if (isstr(t)) // t is an id
t = {id: t}
if (!t.type) {
t.type = xmodule.instance_type(id)
if (!t.type) {
warn('component id not found:', id)
return
}
}
let create = component.types[t.type]
if (!create) {
warn('component type not found', t.type, t.id)
return
}
return create(t)
}
/* ---------------------------------------------------------------------------
// component partial deferred updating mixin
// ---------------------------------------------------------------------------
publishes:
e.updating
e.begin_update()
e.end_update()
e.update([opt])
calls:
e.do_update([opt])
--------------------------------------------------------------------------- */
let component_deferred_updating = function(e) {
e.updating = 0
e.begin_update = function() {
e.updating++
}
e.end_update = function() {
assert(e.updating)
e.updating--
if (!e.updating)
if (opt)
e.update(opt)
}
e.do_update = noop
let opt, in_update
e.update = function(opt1) {
if (in_update)
return
if (opt)
if (opt1)
assign_opt(opt, opt1)
else
opt.val = true
else if (opt1)
opt = opt1
else
opt = {val: true}
if (e.updating)
return
in_update = true
e.do_update(opt)
in_update = false
opt = null
}
}
/* ---------------------------------------------------------------------------
// component property system mixin
// ---------------------------------------------------------------------------
uses:
e.property(name, get, [set])
e.prop(name, attrs)
publishes:
e.<prop>
e.props: {prop -> attrs}
store: 'var'|'attr'|, private, default, convert,
type, ...,
style, style_format, style_parse, bind, resolve.
calls:
e.get_<prop>() -> v
e.set_<prop>(v1, v0)
fires:
document.'prop_changed' (e, prop, v1, v0, slot)
--------------------------------------------------------------------------- */
let component_props = function(e, iprops) {
e.xon = function() { e.xmodule_noupdate = false }
e.xoff = function() { e.xmodule_noupdate = true }
function fire_prop_changed(e, prop, v1, v0, slot) {
document.fire('prop_changed', e, prop, v1, v0, slot)
}
function resolve_widget_id(id) {
let e = window[id]
return isobject(e) && e.iswidget && e.bound && e.can_select_widget != false ? e : null
}
e.props = {}
e.prop = function(prop, opt) {
opt = opt || {}
assign_opt(opt, e.props[prop], iprops && iprops[prop]) // class & instance overrides
let getter = 'get_'+prop
let setter = 'set_'+prop
let type = opt.type
opt.name = prop
let convert = opt.convert || return_arg
let priv = opt.private
let serialize = opt.serialize
if (!e[setter])
e[setter] = noop
let prop_changed = fire_prop_changed
let slot = opt.slot
let dv = opt.default
opt.from_attr = from_attr_func(opt)
let set_attr = opt.attr && set_attr_func(e, prop, opt)
if (opt.store == 'var') {
let v = dv
function get() {
return v
}
function set(v1) {
let v0 = v
v1 = convert(v1, v0)
if (v1 === v0)
return
v = v1
e.begin_update()
e[setter](v1, v0)
if (set_attr)
set_attr(v1)
if (!priv)
prop_changed(e, prop, v1, v0, slot)
e.update()
e.end_update()
}
if (set_attr && !e.hasattr(prop))
set_attr(dv)
} else if (opt.style) {
let style = opt.style
let format = opt.style_format || return_arg
let parse = opt.style_parse || type == 'number' && num || (v => repl(v, '', undefined))
if (dv != null && parse(e.style[style]) == null)
e.style[style] = format(dv)
function get() {
return parse(e.style[style])
}
function set(v) {
let v0 = get.call(e)
v = convert(v, v0)
if (v == v0)
return
e.style[style] = format(v)
v = get.call(e) // take it again (browser only sets valid values)
if (v == v0)
return
e.begin_update()
e[setter](v, v0)
if (!priv)
prop_changed(e, prop, v, v0, slot)
e.update()
e.end_update()
}
} else {
assert(!('default' in opt))
function get() {
return e[getter]()
}
function set(v) {
let v0 = e[getter]()
v = convert(v, v0)
if (v === v0)
return
e.begin_update()
e[setter](v, v0)
if (!priv)
prop_changed(e, prop, v, v0, slot)
e.update()
e.end_update()
}
}
// id-based dynamic binding.
if (opt.bind_id) {
let resolve = opt.resolve || resolve_widget_id
let ID = prop
let REF = opt.bind_id
function widget_bind(te, on) {
if (e[ID] == te.id)
e[REF] = on ? te : null
}
function bind(on) {
e[REF] = on ? resolve(e[ID]) : null
window.on('widget_bind', widget_bind, on)
}
function id_changed(id1, id0) {
if (e.bound)
e[REF] = resolve(id1)
if ((id1 != null) != (id0 != null)) {
e.on('bind', bind, id1 != null)
}
}
prop_changed = function(e, k, v1, v0, slot) {
fire_prop_changed(e, k, v1, v0, slot)
if (k == ID)
id_changed(v1, v0)
}
if (e[ID] != null)
id_changed(e[ID])
}
e.property(prop, get, set)
if (!priv)
e.props[prop] = opt
}
// dynamic properties.
e.set_prop = function(k, v) { e[k] = v } // stub
e.get_prop = k => e[k] // stub
e.get_prop_attrs = k => e.props[k] // stub
// prop serialization.
e.serialize_prop = function(k, v, even_if_default) {
let def = e.props[k]
if (def && !even_if_default && v === def.default)
return undefined // undefined is not stored.
if (def && def.serialize)
v = def.serialize(v)
else if (isobject(v) && v.serialize)
v = v.serialize()
return v
}
}
}
/* ---------------------------------------------------------------------------
// undo stack, selected widgets, editing widget and clipboard.
// ---------------------------------------------------------------------------
publishes:
undo_stack, redo_stack
editing_widget
selected_widgets
copied_widgets
focused_widget(e)
unselect_all_widgets()
copy_selected_widgets()
cut_selected_widgets()
paste_copied_widgets()
undo()
redo()
push_undo(f)
behavior:
uncaptured clicks and escape unselect all widgets.
ctrl+x/+c/+v/(+shift)+z/+y do the usual thing with selected widgets.
--------------------------------------------------------------------------- */
undo_stack = []
redo_stack = []
function push_undo(f) {
undo_stack.push(f)
}
function undo() {
let f = undo_stack.pop()
if (!f)
return
redo_stack.push(f)
f()
}
function redo() {
;[undo_stack, redo_stack] = [redo_stack, undo_stack]
undo()
;[undo_stack, redo_stack] = [redo_stack, undo_stack]
}
function focused_widget(e) {
e = e || document.activeElement
return e && e.iswidget && e || (e.parent && e.parent != e && focused_widget(e.parent))
}
editing_widget = null
selected_widgets = new Set()
function unselect_all_widgets() {
if (editing_widget)
editing_widget.widget_editing = false
for (let e of selected_widgets)
e.widget_selected = false
}
copied_widgets = new Set()
function copy_selected_widgets() {
copied_widgets = new Set(selected_widgets)
}
function cut_selected_widgets() {
copy_selected_widgets()
for (let e of selected_widgets)
e.remove_widget()
}
function paste_copied_widgets() {
if (!copied_widgets.size)
return
if (editing_widget) {
editing_widget.add_widgets(copied_widgets)
copied_widgets.clear()
} else {
for (let e of selected_widgets) {
let ce = copied_widgets.values().next().value
if (!ce)
break
let pe = e.parent_widget
if (pe)
pe.replace_child_widget(e, ce)
copied_widgets.delete(ce)
}
}
}
document.on('keydown', function(key, shift, ctrl) {
if (key == 'Escape')
unselect_all_widgets()
else if (ctrl && key == 'c')
copy_selected_widgets()
else if (ctrl && key == 'x')
cut_selected_widgets()
else if (ctrl && key == 'v')
paste_copied_widgets()
else if (ctrl && key == 'z')
if (shift)
redo()
else
undo()
else if (ctrl && key == 'y')
redo()
})
document.on('pointerdown', function() {
unselect_all_widgets()
})
/* ---------------------------------------------------------------------------
// selectable widget mixin
// ---------------------------------------------------------------------------
uses:
e.can_select_widget
publishes:
e.parent_widget
e.selectable_parent_widget
e.widget_selected
e.set_widget_selected()
e.remove_widget()
calls:
e.do_select_widget()
e.do_unselect_widget()
calls from the first parent which has e.child_widgets:
p.can_select_widget
p.remove_child_widget()
behavior:
enters widget editing mode and/or selects widget with ctrl(+shift)+click.
--------------------------------------------------------------------------- */
function parent_widget_which(e, which) {
assert(e != window)
e = e.popup_target || e.parent
while (e) {
if (e.iswidget && which(e))
return e
e = e.popup_target || e.parent
}
}
function up_widget_which(e, which) {
return which(e) ? e : parent_widget_which(e, which)
}
function selectable_widget(e) {
e.property('parent_widget', function() {
return parent_widget_which(this, p => p.child_widgets)
})
e.property('selectable_parent_widget', function() {
return parent_widget_which(e, p => p.child_widgets && p.can_select_widget)
})
e.can_select_widget = true
e.set_widget_selected = function(select, focus, fire_changed_event) {
select = select !== false
if (e.widget_selected == select)
return
if (select) {
selected_widgets.add(e)
e.do_select_widget(focus)
} else {
selected_widgets.delete(e)
e.do_unselect_widget(focus)
}
e.class('widget-selected', select)
if (fire_changed_event !== false)
document.fire('selected_widgets_changed')
}
e.property('widget_selected',
() => selected_widgets.has(e),
function(v, ...args) { e.set_widget_selected(v, ...args) })
e.do_select_widget = function(focus) {
// make widget unfocusable: the overlay will be focusable instead.
e.focusable = false
let overlay = div({class: 'x-widget-selected-overlay', tabindex: 0})
e.widget_selected_overlay = overlay
e.add(overlay)
overlay.on('keydown', function(key) {
if (key == 'Delete') {
e.remove_widget()
return false
}
})
overlay.on('pointerdown', function(ev) {
if (ev.ctrlKey && ev.shiftKey) {
if (!overlay.focused)
overlay.focus()
if (selected_widgets.size == 1) {
unselect_all_widgets()
let p = e.selectable_parent_widget
if (p)
p.widget_selected = true
} else
e.widget_selected = !e.widget_selected
return false
} else
unselect_all_widgets()
})
if (focus !== false)
overlay.focus()
}
e.do_unselect_widget = function(focus_prev) {
e.focusable = true
e.widget_selected_overlay.remove()
e.widget_selected_overlay = null
if (focus_prev !== false && selected_widgets.size)
[...selected_widgets].last.widget_selected_overlay.focus()
}
e.remove_widget = function() {
let p = e.parent_widget
if (!p) return
e.widget_selected = false
p.remove_child_widget(e)
}
e.hit_test_widget_editing = return_true
e.on('pointerdown', function(ev, mx, my) {
if (!e.can_select_widget)
return
if (e.widget_selected)
return false // prevent dropdown from opening.
if (!ev.ctrlKey)
return
if (ev.shiftKey) {
// prevent accidentally clicking on the parent of any of the selected widgets.
for (let e1 of selected_widgets) {
let p = e1.selectable_parent_widget
while (p) {
if (p == e)
return false
p = p.selectable_parent_widget
}
}
e.widget_editing = false
e.widget_selected = true
} else {
unselect_all_widgets()
if (e.can_edit_widget && !selected_widgets.size)
if (e.hit_test_widget_editing(ev, mx, my)) {
e.widget_editing = true
// don't prevent default to let the caret land under the mouse.
ev.stopPropagation()
return
}
}
return false
})
e.on('bind', function(on) {
if (!on)
e.widget_selected = false
})
}
/* ---------------------------------------------------------------------------
// editable widget mixin
// ---------------------------------------------------------------------------
uses:
e.can_edit_widget
publishes:
e.widget_editing
calls:
e.set_widget_editing()
--------------------------------------------------------------------------- */
function editable_widget(e) {
e.can_edit_widget = true
e.set_widget_editing = noop
e.property('widget_editing',
() => editing_widget == e,
function(v) {
v = !!v
if (e.widget_editing == v)
return
e.class('widget-editing', v)
if (v) {
assert(editing_widget != e)
if (editing_widget)
editing_widget.widget_editing = false
assert(editing_widget == null)
e.focusable = false
editing_widget = e
} else {
e.focusable = true
editing_widget = null
}
getSelection().removeAllRanges()
e.set_widget_editing(v)
})
e.on('bind', function(on) {
if (!on)
e.widget_editing = false
})
}
// ---------------------------------------------------------------------------
// cssgrid item widget mixin
// ---------------------------------------------------------------------------
function cssgrid_item_widget(e) {
e.prop('pos_x' , {style: 'grid-column-start' , type: 'number'})
e.prop('pos_y' , {style: 'grid-row-start' , type: 'number'})
e.prop('span_x' , {style: 'grid-column-end' , type: 'number', style_format: v => 'span '+v, style_parse: v => num((v || 'span 1').replace('span ', '')) })
e.prop('span_y' , {style: 'grid-row-end' , type: 'number', style_format: v => 'span '+v, style_parse: v => num((v || 'span 1').replace('span ', '')) })
e.prop('align_x', {style: 'justify-self' , type: 'enum', enum_values: ['start', 'end', 'center', 'stretch']})
e.prop('align_y', {style: 'align-self' , type: 'enum', enum_values: ['start', 'end', 'center', 'stretch']})
let do_select_widget = e.do_select_widget
let do_unselect_widget = e.do_unselect_widget
e.do_select_widget = function(focus) {
do_select_widget(focus)
let p = e.parent_widget
if (p && p.iswidget && p.type == 'cssgrid') {
cssgrid_item_widget_editing(e)
e.cssgrid_item_do_select_widget()
}
}
e.do_unselect_widget = function(focus_prev) {
let p = e.parent_widget
if (p && p.iswidget && p.type == 'cssgrid')
e.cssgrid_item_do_unselect_widget()
do_unselect_widget(focus_prev)
}
}
function contained_widget(e) {
cssgrid_item_widget(e)
}
// ---------------------------------------------------------------------------
// editable widget protocol for cssgrid item
// ---------------------------------------------------------------------------
function cssgrid_item_widget_editing(e) {
function track_bounds() {
let i = e.pos_x-1
let j = e.pos_y-1
return e.parent_widget.cssgrid_track_bounds(i, j, i + e.span_x, j + e.span_y)
}
function set_span(axis, i1, i2) {
if (i1 !== false)
e['pos_'+axis] = i1+1
if (i2 !== false)
e['span_'+axis] = i2 - (i1 !== false ? i1 : e['pos_'+axis]-1)
}
function toggle_stretch_for(horiz) {
let attr = horiz ? 'align_x' : 'align_y'
let align = e[attr]
if (align == 'stretch')
align = e['_'+attr] || 'center'
else {
e['_'+attr] = align
align = 'stretch'
}
e[horiz ? 'w' : 'h'] = align == 'stretch' ? 'auto' : null
e[attr] = align
return align
}
function toggle_stretch(horiz, vert) {
if (horiz && vert) {
let stretch_x = e.align_x == 'stretch'
let stretch_y = e.align_y == 'stretch'
if (stretch_x != stretch_y) {
toggle_stretch(!stretch_x, !stretch_y)
} else {
toggle_stretch(true, false)
toggle_stretch(false, true)
}
} else if (horiz)
toggle_stretch_for(true)
else if (vert)
toggle_stretch_for(false)
}
e.cssgrid_item_do_select_widget = function() {
let p = e.parent_widget
if (!(p && p.iswidget && p.type == 'cssgrid'))
return
p.widget_editing = true
e.widget_selected_overlay.on('focus', function() {
p.widget_editing = true
})
let span_outline = div({class: 'x-cssgrid-span'},
div({class: 'x-cssgrid-span-handle', side: 'top'}),
div({class: 'x-cssgrid-span-handle', side: 'left'}),
div({class: 'x-cssgrid-span-handle', side: 'right'}),
div({class: 'x-cssgrid-span-handle', side: 'bottom'}),
)
span_outline.on('pointerdown', so_pointerdown)
p.add(span_outline)
function update_so() {
for (let s of ['grid-column-start', 'grid-column-end', 'grid-row-start', 'grid-row-end'])
span_outline.style[s] = e.style[s]
}
update_so()
function prop_changed(te, k) {
if (te == e)
if (k == 'pos_x' || k == 'span_x' || k == 'pos_y' || k == 'span_y')
update_so()
}
e.on('bind', function(on) {
document.on('prop_changed', prop_changed, on)
})
// drag-resize item's span outline => change item's grid area ----------
let drag_mx, drag_my, side
function resize_span(mx, my) {
let horiz = side == 'left' || side == 'right'
let axis = horiz ? 'x' : 'y'
let second = side == 'right' || side == 'bottom'
mx = horiz ? mx - drag_mx : my - drag_my
let i1 = e['pos_'+axis]-1
let i2 = e['pos_'+axis]-1 + e['span_'+axis]
let dx = 1/0
let closest_i
e.parent_widget.each_cssgrid_line(axis, function(i, x) {
if (second ? i > i1 : i < i2) {
if (abs(x - mx) < dx) {
dx = abs(x - mx)
closest_i = i
}
}
})
set_span(axis,
!second ? closest_i : i1,
second ? closest_i : i2
)
}
function so_pointerdown(ev, mx, my) {
let handle = ev.target.closest('.x-cssgrid-span-handle')
if (!handle) return
side = handle.attr('side')
let [bx1, by1, bx2, by2] = track_bounds()
let second = side == 'right' || side == 'bottom'
drag_mx = mx - (second ? bx2 : bx1)
drag_my = my - (second ? by2 : by1)
resize_span(mx, my)
return this.capture_pointer(ev, so_pointermove)
}
function so_pointermove(ev, mx, my) {
resize_span(mx, my)
}
function overlay_keydown(key, shift, ctrl) {
if (key == 'Enter') { // toggle stretch
toggle_stretch(!shift, !ctrl)
return false
}
if (key == 'ArrowLeft' || key == 'ArrowRight' || key == 'ArrowUp' || key == 'ArrowDown') {
let horiz = key == 'ArrowLeft' || key == 'ArrowRight'
let fw = key == 'ArrowRight' || key == 'ArrowDown'
if (ctrl) { // change alignment
let attr = horiz ? 'align_x' : 'align_y'
let align = e[attr]
if (align == 'stretch')
align = toggle_stretch(horiz, !horiz)
let align_indices = {start: 0, center: 1, end: 2}
let align_map = keys(align_indices)
align = align_map[align_indices[align] + (fw ? 1 : -1)]
e[attr] = align