-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathui-element.js
128 lines (106 loc) · 3.37 KB
/
ui-element.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
import { createElement } from '../util/util.js';
function addElementHTML(htmlData) {
if (typeof htmlData === 'string') {
// Allow developers to provide full svg,
// For example:
// <svg viewBox="0 0 32 32" width="32" height="32" aria-hidden="true" class="pswp__icn">
// <path d="..." />
// <circle ... />
// </svg>
// Can also be any HTML string.
return htmlData;
}
if (!htmlData || !htmlData.isCustomSVG) {
return '';
}
const svgData = htmlData;
let out = '<svg aria-hidden="true" class="pswp__icn" viewBox="0 0 %d %d" width="%d" height="%d">';
out = out.split('%d').join(svgData.size || 32); // replace all %d with size
// Icons may contain outline/shadow,
// to make it we "clone" base icon shape and add border to it.
// Icon itself and border are styled via CSS.
//
// Property shadowID defines ID of element that should be cloned.
if (svgData.outlineID) {
out += '<use class="pswp__icn-shadow" xlink:href="#' + svgData.outlineID + '"/>';
}
out += svgData.inner;
out += '</svg>';
return out;
}
class UIElement {
constructor(pswp, data) {
const name = data.name || data.className;
let elementHTML = data.html;
if (pswp.options[name] === false) {
// exit if element is disabled from options
return;
}
// Allow to override SVG icons from options
if (typeof pswp.options[name + 'SVG'] === 'string') {
// arrowPrevSVG
// arrowNextSVG
// closeSVG
// zoomSVG
elementHTML = pswp.options[name + 'SVG'];
}
pswp.dispatch('uiElementCreate', { data });
let className = '';
if (data.isButton) {
className += 'pswp__button ';
className += (data.className || `pswp__button--${data.name}`);
} else {
className += (data.className || `pswp__${data.name}`);
}
let element;
let tagName = data.isButton ? (data.tagName || 'button') : (data.tagName || 'div');
tagName = tagName.toLowerCase();
element = createElement(className, tagName);
if (data.isButton) {
// create button element
element = createElement(className, tagName);
if (tagName === 'button') {
element.type = 'button';
}
if (typeof pswp.options[name + 'Title'] === 'string') {
element.title = pswp.options[name + 'Title'];
} else if (data.title) {
element.title = data.title;
}
}
element.innerHTML = addElementHTML(elementHTML);
if (data.onInit) {
data.onInit(element, pswp);
}
if (data.onClick) {
element.onclick = (e) => {
if (typeof data.onClick === 'string') {
pswp[data.onClick]();
} else {
data.onClick(e, element, pswp);
}
};
}
// Top bar is default position
const appendTo = data.appendTo || 'bar';
let container;
if (appendTo === 'bar') {
if (!pswp.topBar) {
pswp.topBar = createElement('pswp__top-bar pswp__hide-on-close', false, pswp.scrollWrap);
}
container = pswp.topBar;
} else {
// element outside of top bar gets a secondary class
// that makes element fade out on close
element.classList.add('pswp__hide-on-close');
if (appendTo === 'wrapper') {
container = pswp.scrollWrap;
} else {
// root element
container = pswp.element;
}
}
container.appendChild(element);
}
}
export default UIElement;