@cat-of-summer/st-script 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +130 -0
- package/dist/app/form.esm.min.js +1 -0
- package/dist/app/form.min.js +2 -0
- package/dist/app.esm.min.js +1 -0
- package/dist/app.min.js +2 -0
- package/dist/cookie.esm.min.js +1 -0
- package/dist/cookie.min.js +2 -0
- package/dist/core.esm.min.js +1 -0
- package/dist/core.min.js +2 -0
- package/dist/index.esm.min.js +1 -0
- package/dist/index.min.js +2 -0
- package/dist/loader.esm.min.js +1 -0
- package/dist/loader.min.js +2 -0
- package/dist/modal.esm.min.js +1 -0
- package/dist/modal.min.js +2 -0
- package/dist/route.esm.min.js +1 -0
- package/dist/route.min.js +2 -0
- package/dist/select.esm.min.js +1 -0
- package/dist/select.min.js +2 -0
- package/dist/st_button_widget.esm.min.js +1 -0
- package/dist/st_button_widget.min.js +2 -0
- package/dist/st_links_widget.esm.min.js +1 -0
- package/dist/st_links_widget.min.js +2 -0
- package/dist/st_mask.esm.min.js +1 -0
- package/dist/st_mask.min.js +2 -0
- package/dist/st_observer.esm.min.js +1 -0
- package/dist/st_observer.min.js +2 -0
- package/dist/st_typograf.esm.min.js +1 -0
- package/dist/st_typograf.min.js +2 -0
- package/dist/st_validator.esm.min.js +1 -0
- package/dist/st_validator.min.js +2 -0
- package/dist/toggle.esm.min.js +1 -0
- package/dist/toggle.min.js +2 -0
- package/dist/uploader.esm.min.js +1 -0
- package/dist/uploader.min.js +2 -0
- package/package.json +35 -0
- package/src/_traits/hasInstanceSymbol.js +29 -0
- package/src/app/form/index.js +357 -0
- package/src/app/index.js +1040 -0
- package/src/cookie/index.js +141 -0
- package/src/core/index.js +159 -0
- package/src/index.js +16 -0
- package/src/loader/index.js +169 -0
- package/src/modal/index.js +297 -0
- package/src/route/index.js +208 -0
- package/src/select/index.js +210 -0
- package/src/st_button_widget/README.txt +62 -0
- package/src/st_button_widget/index.js +322 -0
- package/src/st_links_widget/README.txt +54 -0
- package/src/st_links_widget/index.js +294 -0
- package/src/st_mask/index.js +183 -0
- package/src/st_observer/index.js +426 -0
- package/src/st_typograf/index.js +200 -0
- package/src/st_validator/index.js +57 -0
- package/src/toggle/index.js +181 -0
- package/src/uploader/index.js +297 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { elements, find, own } from '../_traits/hasInstanceSymbol.js';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default class Select {
|
|
5
|
+
|
|
6
|
+
#params = {};
|
|
7
|
+
#methods = {};
|
|
8
|
+
#timers = new WeakMap();
|
|
9
|
+
|
|
10
|
+
static find = find;
|
|
11
|
+
|
|
12
|
+
get params() {return this.#params;}
|
|
13
|
+
|
|
14
|
+
// Текущие значения по управляемым <select>: multiple → массив, иначе — строка.
|
|
15
|
+
get value() {
|
|
16
|
+
return elements(this.#params.target).map(select => select.multiple
|
|
17
|
+
? [...select.selectedOptions].map(o => o.value)
|
|
18
|
+
: select.value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
clone(params) {
|
|
22
|
+
return new Select({
|
|
23
|
+
...this.#params,
|
|
24
|
+
...this.#methods,
|
|
25
|
+
...params,
|
|
26
|
+
parent: this
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
constructor(params) {
|
|
31
|
+
params = {
|
|
32
|
+
target: 'select',
|
|
33
|
+
location: 'bottom center',
|
|
34
|
+
action: 'click',
|
|
35
|
+
|
|
36
|
+
duration: 0,
|
|
37
|
+
allow_interrupt: false,
|
|
38
|
+
|
|
39
|
+
close_on_select: true,
|
|
40
|
+
close_on_outside: true,
|
|
41
|
+
|
|
42
|
+
before_open: () => {},
|
|
43
|
+
on_open: () => {},
|
|
44
|
+
before_close: () => {},
|
|
45
|
+
on_close: () => {},
|
|
46
|
+
|
|
47
|
+
before_change: () => {},
|
|
48
|
+
on_change: () => {},
|
|
49
|
+
|
|
50
|
+
before_init: () => {},
|
|
51
|
+
on_init: () => {},
|
|
52
|
+
|
|
53
|
+
...params
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
for (let [key, value] of Object.entries(params))
|
|
57
|
+
if (typeof value == "function" && key != 'target') {
|
|
58
|
+
this[key] = value.bind(this);
|
|
59
|
+
|
|
60
|
+
this.#methods[key] = value;
|
|
61
|
+
} else
|
|
62
|
+
this.#params[key] = value;
|
|
63
|
+
|
|
64
|
+
this.before_init(this.#params);
|
|
65
|
+
|
|
66
|
+
for (let select of elements(this.#params.target))
|
|
67
|
+
this.#build(select);
|
|
68
|
+
|
|
69
|
+
this.on_init(this.#params);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Расчёт inline-позиции панели опций из location (top/bottom/left/right/center).
|
|
73
|
+
#locate(options) {
|
|
74
|
+
let loc = (this.#params.location || '').toLowerCase().split(/\s+/);
|
|
75
|
+
|
|
76
|
+
Object.assign(options.style, {
|
|
77
|
+
position: 'absolute',
|
|
78
|
+
top: loc.includes('top') ? 'auto' : '100%',
|
|
79
|
+
bottom: loc.includes('top') ? '100%' : 'auto',
|
|
80
|
+
left: loc.includes('right') ? 'auto' : loc.includes('center') ? '50%' : '0',
|
|
81
|
+
right: loc.includes('right') ? '0' : 'auto',
|
|
82
|
+
transform: loc.includes('center') && !loc.includes('right') ? 'translateX(-50%)' : 'none',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Обернуть один нативный <select> кастомной структурой и навесить поведение.
|
|
87
|
+
#build(select) {
|
|
88
|
+
let container = document.createElement('select-container');
|
|
89
|
+
own(container, this);
|
|
90
|
+
Object.assign(container.style, { position: 'relative', display: 'inline-block' });
|
|
91
|
+
|
|
92
|
+
let trigger = document.createElement('select-trigger');
|
|
93
|
+
own(trigger, this);
|
|
94
|
+
Object.assign(trigger.style, { display: 'block', cursor: 'pointer' });
|
|
95
|
+
|
|
96
|
+
let options = document.createElement('select-options');
|
|
97
|
+
own(options, this);
|
|
98
|
+
Object.assign(options.style, { transition: `all ${this.#params.duration}s` });
|
|
99
|
+
this.#locate(options);
|
|
100
|
+
|
|
101
|
+
// Нативный select — держатель значения; визуально скрыт.
|
|
102
|
+
Object.assign(select.style, {
|
|
103
|
+
position: 'absolute', width: '1px', height: '1px',
|
|
104
|
+
overflow: 'hidden', clip: 'rect(0 0 0 0)', clipPath: 'inset(50%)',
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
select.parentNode.insertBefore(container, select);
|
|
108
|
+
container.append(trigger, select, options);
|
|
109
|
+
container.setAttribute('state', 'closed');
|
|
110
|
+
|
|
111
|
+
for (let option of [...select.options]) {
|
|
112
|
+
let item = document.createElement('select-option');
|
|
113
|
+
own(item, this);
|
|
114
|
+
item.textContent = option.textContent;
|
|
115
|
+
item.style.cursor = 'pointer';
|
|
116
|
+
if (option.disabled) item.setAttribute('disabled', '');
|
|
117
|
+
item.toggleAttribute('selected', option.selected);
|
|
118
|
+
|
|
119
|
+
item.addEventListener('click', () => {
|
|
120
|
+
if (!option.disabled) this.#choose(container, select, option, item);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
options.append(item);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.#render(select, trigger);
|
|
127
|
+
|
|
128
|
+
if (this.#params.action == 'hover') {
|
|
129
|
+
container.addEventListener('mouseenter', () => this.open(container));
|
|
130
|
+
container.addEventListener('mouseleave', () => this.close(container));
|
|
131
|
+
} else
|
|
132
|
+
trigger.addEventListener(this.#params.action, () => this.toggle(container));
|
|
133
|
+
|
|
134
|
+
if (this.#params.close_on_outside)
|
|
135
|
+
document.addEventListener('click', (e) => {
|
|
136
|
+
if (!container.contains(e.target)) this.close(container);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Применить выбор опции: синхронизация нативного select + хуки + change.
|
|
141
|
+
#choose(container, select, option, item) {
|
|
142
|
+
if (this.before_change(option, select) === false) return;
|
|
143
|
+
|
|
144
|
+
if (select.multiple) {
|
|
145
|
+
option.selected = !option.selected;
|
|
146
|
+
item.toggleAttribute('selected', option.selected);
|
|
147
|
+
} else {
|
|
148
|
+
select.value = option.value;
|
|
149
|
+
[...container.querySelectorAll('select-option')]
|
|
150
|
+
.forEach(el => el.toggleAttribute('selected', el === item));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
select.dispatchEvent(new Event('change', { bubbles: true }));
|
|
154
|
+
this.#render(select, container.querySelector('select-trigger'));
|
|
155
|
+
|
|
156
|
+
this.on_change(option, select);
|
|
157
|
+
|
|
158
|
+
if (!select.multiple && this.#params.close_on_select) this.close(container);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Отрисовать текст текущего выбора в триггере.
|
|
162
|
+
#render(select, trigger) {
|
|
163
|
+
if (select.multiple)
|
|
164
|
+
trigger.textContent = [...select.selectedOptions].map(o => o.textContent).join(', ');
|
|
165
|
+
else
|
|
166
|
+
trigger.textContent = (select.selectedOptions[0] ?? select.options[0])?.textContent ?? '';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
#transition(el, process, final, before, after) {
|
|
170
|
+
before(el);
|
|
171
|
+
clearTimeout(this.#timers.get(el));
|
|
172
|
+
|
|
173
|
+
requestAnimationFrame(() => {
|
|
174
|
+
el.setAttribute('state', process);
|
|
175
|
+
|
|
176
|
+
requestAnimationFrame(() => {
|
|
177
|
+
this.#timers.set(el, setTimeout(
|
|
178
|
+
() => (el.setAttribute('state', final), after(el)),
|
|
179
|
+
this.#params.duration * 1000
|
|
180
|
+
));
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
#act(container, open) {
|
|
186
|
+
let state = container.getAttribute('state') ?? 'closed', ai = this.#params.allow_interrupt;
|
|
187
|
+
|
|
188
|
+
if (!(open ? state == 'closed' || ai && state == 'closing'
|
|
189
|
+
: state == 'open' || ai && state == 'opening')) return;
|
|
190
|
+
|
|
191
|
+
this.#transition(container,
|
|
192
|
+
open ? 'opening' : 'closing',
|
|
193
|
+
open ? 'open' : 'closed',
|
|
194
|
+
open ? this.before_open : this.before_close,
|
|
195
|
+
open ? this.on_open : this.on_close);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
open(el) { elements(el).map(this.#resolve).forEach(c => c && this.#act(c, true)); }
|
|
199
|
+
close(el) { elements(el).map(this.#resolve).forEach(c => c && this.#act(c, false)); }
|
|
200
|
+
|
|
201
|
+
toggle(el) {
|
|
202
|
+
elements(el).map(this.#resolve).forEach(c => {
|
|
203
|
+
if (c) this.#act(c, !['open', 'opening'].includes(c.getAttribute('state')));
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Нормализовать произвольный элемент к его <select-container>.
|
|
208
|
+
#resolve = (el) => el?.closest?.('select-container')
|
|
209
|
+
?? (el?.tagName?.toLowerCase() == 'select' ? el.parentElement : null);
|
|
210
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Класс позволяет быстро создавать кнопку с различным функционалом.
|
|
2
|
+
|
|
3
|
+
Прототип:
|
|
4
|
+
|
|
5
|
+
Комментарии к параметрам даны в виде: "по умолчанию" // [возможные варианты] ->[условие применения] // дополнительные комментарии
|
|
6
|
+
|
|
7
|
+
static #PARAMETERS_PROTOTYPE = {
|
|
8
|
+
settings: {
|
|
9
|
+
position: "bottom-right" // ["top-left", "top-right", "bottom-left", "bottom-right"]
|
|
10
|
+
},
|
|
11
|
+
condition: {
|
|
12
|
+
name: "scroll", // ["hover", "scroll"]
|
|
13
|
+
options: {
|
|
14
|
+
hiding_type: "opacity", // ["opacity", "visibility", "display"] //Возможные варианты описаны в class.#hidding_types
|
|
15
|
+
transition: "0.3s", // ->[hover, scroll]
|
|
16
|
+
attribute: [], // [[], "attr_name", ["attr_name", "value_1", "value_2"] и т.п.] ->[hover, scroll]
|
|
17
|
+
margin_top: Math.min(window.innerHeight, document.documentElement.clientHeight), // [->scroll] //Отступ, до которого виджет скрыт
|
|
18
|
+
margin_left: undefined, // [->scroll]
|
|
19
|
+
margin_right: undefined, // [->scroll]
|
|
20
|
+
margin_bottom: undefined // [->scroll]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
action: {
|
|
24
|
+
name: "scroll", // ['scroll', 'show_popup', 'function']
|
|
25
|
+
options: {
|
|
26
|
+
selector: undefined, // ->['show_popup'] // Обязательный параметр, кнопка будет менять его свойства (убирать) или менять теги
|
|
27
|
+
hiding_type: "opacity", // ["opacity", "visibility", "display"] //Возможные варианты описаны в class.#hidding_types
|
|
28
|
+
setAttribute: undefined, // [[], "attr_name", ["attr_name", "value_1", "value_2"] и т.п.] ->[Возможна смена тегов для всех]
|
|
29
|
+
removeAttribute: undefined, // // Аналогично предыдущему
|
|
30
|
+
toggleAttribute: undefined, // // Аналогично предыдущему
|
|
31
|
+
top: 0, // ->['scroll'] // Параметр для ScrollTo
|
|
32
|
+
left: undefined, // ->['scroll'] // Параметр для ScrollTo
|
|
33
|
+
behavior: "smooth", // ->['scroll'] // Параметр для ScrollTo
|
|
34
|
+
hook: function(){} // //Функция, привязываемая к нажатию кнопки
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
widget_container: {
|
|
38
|
+
id: undefined, // ['widget_container_id']
|
|
39
|
+
class: undefined, // ['widget_container_class']
|
|
40
|
+
style: {
|
|
41
|
+
//'property1': 'property1_value', // Параметры и далее в таком виде
|
|
42
|
+
//'property2': 'property2_value',
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
widget: {
|
|
46
|
+
id: undefined, // ['widget_id']
|
|
47
|
+
class: undefined, // ['widget_class']
|
|
48
|
+
style: {}
|
|
49
|
+
},
|
|
50
|
+
button_container: {
|
|
51
|
+
id: undefined, // ['button_container_id']
|
|
52
|
+
class: undefined, // ['button_container_class']
|
|
53
|
+
style: {}
|
|
54
|
+
},
|
|
55
|
+
button: {
|
|
56
|
+
src: st_button_widget.DEFAULT_ICONS.arrow // // Либо ссылка на заполнитель, либо контент <svg></svg>
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
Пример:
|
|
61
|
+
|
|
62
|
+
new st_button_widget({});
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
class st_button_widget {
|
|
2
|
+
|
|
3
|
+
static DEFAULT_ICONS = {
|
|
4
|
+
arrow: '<svg viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve"><style type="text/css">.st0{fill:#77B3D4;}.st1{opacity:0.2;}.st2{fill:#231F20;}.st3{fill:#FFFFFF;}</style><g id="Layer_1"><g><circle class="st0" cx="32" cy="32" r="32"/></g><g class="st1"><path class="st2" d="M49.5,33.9L35.3,15.7c-1.8-2.3-4.7-2.3-6.5,0L14.5,33.9c-1.8,2.3-1,4.1,1.7,4.1H24v12c0,2.2,1.8,4,4,4h8c2.2,0,4-1.8,4-4V38h7.8C50.6,38,51.3,36.1,49.5,33.9z"/></g><g><path class="st3" d="M40,48c0,2.2-1.8,4-4,4h-8c-2.2,0-4-1.8-4-4V24c0-2.2,1.8-4,4-4h8c2.2,0,4,1.8,4,4V48z"/></g><g><path class="st3" d="M16.2,36c-2.7,0-3.5-1.9-1.7-4.1l14.3-18.1c1.8-2.3,4.7-2.3,6.5,0l14.3,18.1c1.8,2.3,1,4.1-1.7,4.1H16.2z"/></g></g><g id="Layer_2"></g></svg>',
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
static #PARAMETERS_PROTOTYPE = {
|
|
8
|
+
settings: {
|
|
9
|
+
position: "bottom-right"
|
|
10
|
+
},
|
|
11
|
+
condition: {
|
|
12
|
+
name: "scroll",
|
|
13
|
+
options: {
|
|
14
|
+
hiding_type: "opacity",
|
|
15
|
+
transition: "0.3s",
|
|
16
|
+
attribute: [],
|
|
17
|
+
margin_top: Math.min(window.innerHeight, document.documentElement.clientHeight),
|
|
18
|
+
margin_left: undefined,
|
|
19
|
+
margin_right: undefined,
|
|
20
|
+
margin_bottom: undefined
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
action: {
|
|
24
|
+
name: "scroll",
|
|
25
|
+
options: {
|
|
26
|
+
selector: undefined,
|
|
27
|
+
hiding_type: "opacity",
|
|
28
|
+
setAttribute: undefined,
|
|
29
|
+
removeAttribute: undefined,
|
|
30
|
+
toggleAttribute: undefined,
|
|
31
|
+
top: 0,
|
|
32
|
+
left: undefined,
|
|
33
|
+
behavior: "smooth",
|
|
34
|
+
hook: function(){}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
widget_container: {
|
|
38
|
+
id: undefined,
|
|
39
|
+
class: undefined,
|
|
40
|
+
style: {}
|
|
41
|
+
},
|
|
42
|
+
widget: {
|
|
43
|
+
id: undefined,
|
|
44
|
+
class: undefined,
|
|
45
|
+
style: {}
|
|
46
|
+
},
|
|
47
|
+
button_container: {
|
|
48
|
+
id: undefined,
|
|
49
|
+
class: undefined,
|
|
50
|
+
style: {}
|
|
51
|
+
},
|
|
52
|
+
button: {
|
|
53
|
+
src: st_button_widget.DEFAULT_ICONS.arrow
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static #DEFAULT_STYLE_RULES = {
|
|
58
|
+
"widget_container": {
|
|
59
|
+
"position": "fixed",
|
|
60
|
+
"width": "60px",
|
|
61
|
+
"height": "60px",
|
|
62
|
+
"margin": "30px"
|
|
63
|
+
},
|
|
64
|
+
"widget": {
|
|
65
|
+
"position": "absolute"
|
|
66
|
+
},
|
|
67
|
+
"button_container": {
|
|
68
|
+
"display": "flex",
|
|
69
|
+
"justify-content": "center",
|
|
70
|
+
"align-items": "center",
|
|
71
|
+
"overflow": "hidden",
|
|
72
|
+
"width": "60px",
|
|
73
|
+
"height": "60px",
|
|
74
|
+
"border-radius": "50%",
|
|
75
|
+
"cursor": "pointer"
|
|
76
|
+
},
|
|
77
|
+
"button": {
|
|
78
|
+
"width": "100%",
|
|
79
|
+
"height": "100%",
|
|
80
|
+
"object-fit": "cover"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static #st_assoc_merge(...e){function o(e,o){function t(e,o,f){if("object"==typeof o[e])for(let n in o[e])f[e]||(f[e]={}),t(n,o[e],f[e]);else f[e]=o[e]}for(let f in e)t(f,e,o)}let t={};for(let f of e)o(f,t);return t}
|
|
85
|
+
|
|
86
|
+
static #generate_unique_prefix(length) {
|
|
87
|
+
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
88
|
+
|
|
89
|
+
let result = '';
|
|
90
|
+
for (let i = 0; i < length; i++) {
|
|
91
|
+
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static #hidding_types = {
|
|
97
|
+
"opacity": "0",
|
|
98
|
+
"visibility" : "hidden",
|
|
99
|
+
"display": "none"
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
#class_unique_prefix = "st_" + st_button_widget.#generate_unique_prefix(16);
|
|
103
|
+
|
|
104
|
+
#widget_container_class = this.#class_unique_prefix + "_wc";
|
|
105
|
+
#widget_class = this.#class_unique_prefix + "_w";
|
|
106
|
+
#button_container_class = this.#class_unique_prefix + "_bc";
|
|
107
|
+
#button_class = this.#class_unique_prefix + "_b";
|
|
108
|
+
|
|
109
|
+
#style_CSS = document.createElement('style');
|
|
110
|
+
#widget_container_DOM = document.createElement('div');
|
|
111
|
+
#widget_DOM = document.createElement('div');
|
|
112
|
+
#button_container_DOM = document.createElement('div');
|
|
113
|
+
|
|
114
|
+
#put_style_to_CSS(selector, property, value) {
|
|
115
|
+
if (new RegExp(selector + "{.*?" + property + ":.*?;.*?}").test(this.#style_CSS.innerHTML)) {
|
|
116
|
+
this.#style_CSS.innerHTML = this.#style_CSS.innerHTML.replace(new RegExp("(" + selector + "{.*?" + property + ":).*?(;.*?})"), "$1" + value + "$2");
|
|
117
|
+
} else {
|
|
118
|
+
if (new RegExp(selector + "{.*?}").test(this.#style_CSS.innerHTML)) {
|
|
119
|
+
this.#style_CSS.innerHTML = this.#style_CSS.innerHTML.replace(new RegExp("(" + selector + "{.*?)(})"), "$1" + property + ":" + value + ";" + "$2");
|
|
120
|
+
} else {
|
|
121
|
+
this.#style_CSS.innerHTML += selector + "{" + property + ":" + value + ";" + "}";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#apply_style_rules(selector, style_rules) {
|
|
127
|
+
for (let rule in style_rules) {
|
|
128
|
+
this.#put_style_to_CSS(selector, rule, style_rules[rule]);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
constructor(NEW_PARAMETERS) {
|
|
133
|
+
|
|
134
|
+
function get_display_functions(element_DOM, hiding_type, unset = false) {
|
|
135
|
+
return {
|
|
136
|
+
show: function() {
|
|
137
|
+
element_DOM.style.removeProperty(hiding_type);
|
|
138
|
+
},
|
|
139
|
+
hide: function() {
|
|
140
|
+
element_DOM.style.setProperty(hiding_type, unset ? "unset" : st_button_widget.#hidding_types[hiding_type]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function get_style_rules_for_position(position) {
|
|
146
|
+
switch (position) {
|
|
147
|
+
case "top-left":
|
|
148
|
+
return {
|
|
149
|
+
"top": "0",
|
|
150
|
+
"left": "0"
|
|
151
|
+
}
|
|
152
|
+
case "top-right":
|
|
153
|
+
return {
|
|
154
|
+
"top": "0",
|
|
155
|
+
"right": "0"
|
|
156
|
+
}
|
|
157
|
+
case "bottom-left":
|
|
158
|
+
return {
|
|
159
|
+
"bottom": "0",
|
|
160
|
+
"left": "0"
|
|
161
|
+
}
|
|
162
|
+
case "bottom-right":
|
|
163
|
+
default:
|
|
164
|
+
return {
|
|
165
|
+
"bottom": "0",
|
|
166
|
+
"right": "0"
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function get_setAttribute_correct_array_or_null(setAttribute_param) {
|
|
172
|
+
return setAttribute_param ? ((typeof setAttribute_param == "object")? [setAttribute_param[0] ?? "st_active", setAttribute_param[1] ?? "", setAttribute_param[2] ?? ""] : [setAttribute_param ?? "st_active", "", ""]) : null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const PARAMETERS = st_button_widget.#st_assoc_merge(st_button_widget.#PARAMETERS_PROTOTYPE, NEW_PARAMETERS);
|
|
176
|
+
|
|
177
|
+
this.#apply_style_rules("." + this.#widget_container_class, st_button_widget.#DEFAULT_STYLE_RULES['widget_container']);
|
|
178
|
+
this.#apply_style_rules("." + this.#widget_class, st_button_widget.#DEFAULT_STYLE_RULES['widget']);
|
|
179
|
+
this.#apply_style_rules("." + this.#button_container_class, st_button_widget.#DEFAULT_STYLE_RULES['button_container']);
|
|
180
|
+
this.#apply_style_rules("." + this.#button_class, st_button_widget.#DEFAULT_STYLE_RULES['button']);
|
|
181
|
+
|
|
182
|
+
this.#apply_style_rules("." + this.#widget_container_class, get_style_rules_for_position(PARAMETERS.settings.position));
|
|
183
|
+
this.#apply_style_rules("." + this.#widget_class, get_style_rules_for_position(PARAMETERS.settings.position));
|
|
184
|
+
|
|
185
|
+
this.#widget_container_DOM.classList.add(this.#widget_container_class);
|
|
186
|
+
this.#apply_style_rules("." + this.#widget_container_class, PARAMETERS.widget_container.style);
|
|
187
|
+
if (PARAMETERS.widget_container.class) this.#widget_container_DOM.classList.add(PARAMETERS.widget_container.class);
|
|
188
|
+
if (PARAMETERS.widget_container.id) this.#widget_container_DOM.id = PARAMETERS.widget_container.id;
|
|
189
|
+
|
|
190
|
+
this.#widget_DOM.classList.add(this.#widget_class);
|
|
191
|
+
this.#apply_style_rules("." + this.#widget_class, PARAMETERS.widget.style);
|
|
192
|
+
if (PARAMETERS.widget.class) this.#widget_DOM.classList.add(PARAMETERS.widget.class);
|
|
193
|
+
if (PARAMETERS.widget.id) this.#widget_DOM.id = PARAMETERS.widget.id;
|
|
194
|
+
|
|
195
|
+
this.#button_container_DOM.classList.add(this.#button_container_class);
|
|
196
|
+
this.#apply_style_rules("." + this.#button_container_class, PARAMETERS.button_container.style);
|
|
197
|
+
if (PARAMETERS.button_container.class) this.#button_container_DOM.classList.add(PARAMETERS.button_container.class);
|
|
198
|
+
if (PARAMETERS.button_container.id) this.#button_container_DOM.id = PARAMETERS.button_container.id;
|
|
199
|
+
|
|
200
|
+
if (PARAMETERS.condition) {
|
|
201
|
+
|
|
202
|
+
function get_attr_functions(element_DOM, attribute) {
|
|
203
|
+
let correct_attribute = get_setAttribute_correct_array_or_null(attribute);
|
|
204
|
+
return {
|
|
205
|
+
set: function() {
|
|
206
|
+
if (correct_attribute) correct_attribute[1] ? element_DOM.setAttribute(correct_attribute[0], correct_attribute[1]) : element_DOM.setAttribute(correct_attribute[0], "");
|
|
207
|
+
},
|
|
208
|
+
remove : function() {
|
|
209
|
+
if (correct_attribute) correct_attribute[1] ? (correct_attribute[2] ? element_DOM.setAttribute(correct_attribute[0], correct_attribute[2]) : element_DOM.setAttribute(correct_attribute[0], "")) : element_DOM.removeAttribute(correct_attribute[0]);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (!PARAMETERS.condition.options.hiding_type in st_button_widget.#hidding_types) return;
|
|
215
|
+
|
|
216
|
+
let display_widget = get_display_functions(this.#widget_DOM, PARAMETERS.condition.options.hiding_type);
|
|
217
|
+
let attribute_widget = get_attr_functions(this.#widget_DOM, PARAMETERS.condition.options.attribute);
|
|
218
|
+
|
|
219
|
+
display_widget.hide();
|
|
220
|
+
attribute_widget.remove();
|
|
221
|
+
switch (PARAMETERS.condition.name) {
|
|
222
|
+
case 'hover':
|
|
223
|
+
this.#widget_container_DOM.addEventListener("mouseenter", function() {
|
|
224
|
+
display_widget.show();
|
|
225
|
+
attribute_widget.set();
|
|
226
|
+
});
|
|
227
|
+
this.#widget_container_DOM.addEventListener("mouseleave", function() {
|
|
228
|
+
display_widget.hide();
|
|
229
|
+
attribute_widget.remove();
|
|
230
|
+
});
|
|
231
|
+
break;
|
|
232
|
+
case 'scroll':
|
|
233
|
+
default:
|
|
234
|
+
let window_height = Math.min(window.innerHeight, document.documentElement.clientHeight);
|
|
235
|
+
let window_width = Math.min(window.innerWidth, document.documentElement.clientWidth);
|
|
236
|
+
|
|
237
|
+
let document_height = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.body.clientHeight, document.documentElement.clientHeight);
|
|
238
|
+
let document_width = Math.max(document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth, document.body.clientWidth, document.documentElement.clientWidth);
|
|
239
|
+
|
|
240
|
+
let margin_top = PARAMETERS.condition.options.margin_top;
|
|
241
|
+
let margin_bottom = PARAMETERS.condition.options.margin_bottom;
|
|
242
|
+
let margin_right = PARAMETERS.condition.options.margin_right;
|
|
243
|
+
let margin_left = PARAMETERS.condition.options.margin_left;
|
|
244
|
+
|
|
245
|
+
window.addEventListener('scroll', function() {
|
|
246
|
+
let scroll_y_top = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
|
|
247
|
+
let scroll_y_bottom = scroll_y_top + window_height;
|
|
248
|
+
let scroll_x_left = window.pageXOffset || window.scrollX || document.documentElement.scrollLeft
|
|
249
|
+
let scroll_x_right = scroll_x_left + window_width;
|
|
250
|
+
|
|
251
|
+
if ((scroll_y_top > margin_top) || (scroll_y_bottom < document_height - margin_bottom) || (scroll_x_right < document_width - margin_right) || (scroll_x_left > margin_left)) {
|
|
252
|
+
display_widget.show();
|
|
253
|
+
attribute_widget.set();
|
|
254
|
+
} else {
|
|
255
|
+
display_widget.hide();
|
|
256
|
+
attribute_widget.remove();
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
if (PARAMETERS.condition.options.transition) {this.#widget_DOM.style.setProperty("transition", PARAMETERS.condition.options.transition)}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (PARAMETERS.action) {
|
|
265
|
+
switch (PARAMETERS.action.name) {
|
|
266
|
+
case 'show_popup':
|
|
267
|
+
if (!PARAMETERS.action.options.selector) return
|
|
268
|
+
if (!PARAMETERS.action.options.hiding_type in st_button_widget.#hidding_types) return;
|
|
269
|
+
|
|
270
|
+
this.#button_container_DOM.addEventListener("click", function() {
|
|
271
|
+
for (let popup of document.querySelectorAll(PARAMETERS.action.options.selector)) {
|
|
272
|
+
|
|
273
|
+
let display_widget = get_display_functions(popup, PARAMETERS.action.options.hiding_type, true);
|
|
274
|
+
|
|
275
|
+
if (popup.style[st_button_widget.#hidding_types[PARAMETERS.action.options.hiding_type]])
|
|
276
|
+
display_widget.show();
|
|
277
|
+
else
|
|
278
|
+
display_widget.hide();
|
|
279
|
+
|
|
280
|
+
if (PARAMETERS.action.options.setAttribute) {
|
|
281
|
+
let correct_attribute = get_setAttribute_correct_array_or_null(PARAMETERS.action.options.setAttribute);
|
|
282
|
+
popup.setAttribute(correct_attribute[0], correct_attribute[1]);
|
|
283
|
+
}
|
|
284
|
+
if (PARAMETERS.action.options.removeAttribute) popup.removeAttribute(PARAMETERS.action.options.removeAttribute);
|
|
285
|
+
if (PARAMETERS.action.options.toggleAttribute) popup.toggleAttribute(PARAMETERS.action.options.toggleAttribute);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
break;
|
|
290
|
+
case 'function':
|
|
291
|
+
this.#button_container_DOM.addEventListener("click", PARAMETERS.action.options.hook);
|
|
292
|
+
break;
|
|
293
|
+
case 'scroll':
|
|
294
|
+
default:
|
|
295
|
+
this.#button_container_DOM.addEventListener("click", function() {
|
|
296
|
+
window.scrollTo({
|
|
297
|
+
top: PARAMETERS.action.options.top,
|
|
298
|
+
left: PARAMETERS.action.options.left,
|
|
299
|
+
behavior: PARAMETERS.action.options.behavior
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (new RegExp("^<svg.*?</svg>$").test(PARAMETERS.button.src)) {
|
|
307
|
+
this.#button_container_DOM.innerHTML = PARAMETERS.button.src;
|
|
308
|
+
} else {
|
|
309
|
+
this.#button_container_DOM.innerHTML = '<img src=' + PARAMETERS.button.src + ' alt="icon">';
|
|
310
|
+
}
|
|
311
|
+
this.#button_container_DOM.childNodes[0].classList.add(this.#button_class);
|
|
312
|
+
|
|
313
|
+
this.#style_CSS.setAttribute("widget", this.#class_unique_prefix);
|
|
314
|
+
document.head.appendChild(this.#style_CSS);
|
|
315
|
+
|
|
316
|
+
this.#widget_DOM.appendChild(this.#button_container_DOM);
|
|
317
|
+
this.#widget_container_DOM.appendChild(this.#widget_DOM);
|
|
318
|
+
document.querySelector("body").appendChild(this.#widget_container_DOM);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export default st_button_widget;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Класс позволяет быстро создавать статичный блок иконок-ссылок.
|
|
2
|
+
|
|
3
|
+
Прототип:
|
|
4
|
+
|
|
5
|
+
Комментарии к параметрам даны в виде: "по умолчанию" // [возможные варианты] ->[условие применения] // дополнительные комментарии
|
|
6
|
+
|
|
7
|
+
static #PARAMETERS_PROTOTYPE = {
|
|
8
|
+
settings: {
|
|
9
|
+
position: "bottom-right" // ["top-left", "top-right", "bottom-left", "bottom-right"]
|
|
10
|
+
},
|
|
11
|
+
condition: {
|
|
12
|
+
name: "scroll", // ["hover", "scroll"]
|
|
13
|
+
options: {
|
|
14
|
+
hiding_type: "opacity", // ["opacity", "visibility", "display"] //Возможные варианты описаны в class.#hidding_types
|
|
15
|
+
transition: "0.3s", // ->[hover, scroll]
|
|
16
|
+
attribute: [], // [[], "attr_name", ["attr_name", "value_1", "value_2"] и т.п.] ->[hover, scroll]
|
|
17
|
+
margin_top: Math.min(window.innerHeight, document.documentElement.clientHeight), // [->scroll] //Отступ, до которого виджет скрыт
|
|
18
|
+
margin_left: undefined, // [->scroll]
|
|
19
|
+
margin_right: undefined, // [->scroll]
|
|
20
|
+
margin_bottom: undefined // [->scroll]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
widget_container: {
|
|
24
|
+
id: undefined, // ['widget_container_id']
|
|
25
|
+
class: undefined, // ['widget_container_class']
|
|
26
|
+
style: {
|
|
27
|
+
//'property1': 'property1_value', // Параметры и далее в таком виде
|
|
28
|
+
//'property2': 'property2_value',
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
widget: {
|
|
32
|
+
id: undefined, // ['widget_id']
|
|
33
|
+
class: undefined, // ['widget_class']
|
|
34
|
+
style: {}
|
|
35
|
+
},
|
|
36
|
+
links_container: {
|
|
37
|
+
id: undefined, // ['links_container_id']
|
|
38
|
+
class: undefined, // ['links_container_class']
|
|
39
|
+
style: {}
|
|
40
|
+
},
|
|
41
|
+
links: {
|
|
42
|
+
0: {
|
|
43
|
+
href: 'undefined',
|
|
44
|
+
src: st_links_widget.DEFAULT_ICONS.link, // // Либо ссылка на заполнитель, либо контент <svg></svg>
|
|
45
|
+
id: undefined,
|
|
46
|
+
class: undefined, // ['links_container_group_1']
|
|
47
|
+
style: {} // //Применяется по ID
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
Пример:
|
|
53
|
+
|
|
54
|
+
new st_links_widget({});
|