UNPKG

1.75 kBJavaScriptView Raw
1import LightboxPanel from './lightbox-panel';
2import {$$, assign, data, findIndex, on, uniqueBy} from 'uikit-util';
3
4export default {
5
6 install,
7
8 props: {toggle: String},
9
10 data: {toggle: 'a'},
11
12 computed: {
13
14 toggles: {
15
16 get({toggle}, $el) {
17 return $$(toggle, $el);
18 },
19
20 watch() {
21 this.hide();
22 }
23
24 },
25
26 items() {
27 return uniqueBy(this.toggles.map(toItem), 'source');
28 }
29
30 },
31
32 disconnected() {
33 this.hide();
34 },
35
36 events: [
37
38 {
39
40 name: 'click',
41
42 delegate() {
43 return `${this.toggle}:not(.uk-disabled)`;
44 },
45
46 handler(e) {
47 e.preventDefault();
48 const src = data(e.current, 'href');
49 this.show(findIndex(this.items, ({source}) => source === src));
50 }
51
52 }
53
54 ],
55
56 methods: {
57
58 show(index) {
59
60 this.panel = this.panel || this.$create('lightboxPanel', assign({}, this.$props, {items: this.items}));
61
62 on(this.panel.$el, 'hidden', () => this.panel = false);
63
64 return this.panel.show(index);
65
66 },
67
68 hide() {
69
70 return this.panel && this.panel.hide();
71
72 }
73
74 }
75
76};
77
78function install(UIkit, Lightbox) {
79
80 if (!UIkit.lightboxPanel) {
81 UIkit.component('lightboxPanel', LightboxPanel);
82 }
83
84 assign(
85 Lightbox.props,
86 UIkit.component('lightboxPanel').options.props
87 );
88
89}
90
91function toItem(el) {
92 return ['href', 'caption', 'type', 'poster', 'alt'].reduce((obj, attr) => {
93 obj[attr === 'href' ? 'source' : attr] = data(el, attr);
94 return obj;
95 }, {});
96}