-
Notifications
You must be signed in to change notification settings - Fork 0
/
omni-icon.js
150 lines (149 loc) · 4.17 KB
/
omni-icon.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
class OmniIcon extends HTMLElement {
static #ListFor = null;
static #Order = null;
static isClone = null;
constructor() {
super();
var x1, y1; // Координаты курсора относительно перемещаемого окна
this.root = this.attachShadow({mode: "open"});
this.wrapper = document.createElement("slot");
// Создаём элемент заголовка окна (CAPTION)
this.header = document.createElement("header");
this.header.className = "omni-window-header";
this.root.appendChild(this.header);
this.root.appendChild(this.wrapper);
this.root.append(
document.getElementById('tmpl').content.cloneNode(true));
if(OmniWindow.isClone) {
return;
}
this.header.addEventListener("pointerdown",
(function(evt) {
document.body.setPointerCapture(evt.pointerId);
document.body.onpointermove = onMouseMove.bind(this);
x1 = evt.clientX - this.offsetLeft;
y1 = evt.clientY - this.offsetTop;
evt.preventDefault();
}).bind(this)
);
// Добавляем обработчик события отпускания заголовка окна
document.body.addEventListener("pointerup",
(function(evt) {
document.body.onpointermove = null;
document.body.releasePointerCapture(evt.pointerId);
}).bind(this)
);
setTimeout((function() {
this.style.left = `${this.offsetLeft}px`;
this.style.top = `${this.offsetTop}px`;
}).bind(this), 0);
this.addEventListener("pointerdown", this.bringToTop);
if(OmniWindow.Order)
OmniWindow.Order.push(this);
else
OmniWindow.Order = [this];
function onMouseMove(evt) {
const x = evt.clientX - x1;
const y = evt.clientY - y1;
this.style.left = `${x}px`;
this.style.top = `${y}px`;
evt.preventDefault();
}
}
adoptedCallback() {
alert(this);
}
connectedCallback() {
this.className = "OmniWindow";
this.enumerate();
}
disconnectedCallback() {
for(const i in OmniWindow.Order) {
if(OmniWindow.Order[i] == this) {
OmniWindow.Order.splice(i, 1);
return;
}
}
}
static get observedAttributes() {
return "for title".split(/\s+/);
}
attributeChangedCallback(name, oldValue, newValue) {
// вызывается при изменении одного из перечисленных выше атрибутов
switch(name) {
case "for":
// Если имеется ссылка на элемент стека окон, регистрируем
if(OmniWindow.isClone)
return;
OmniWindow.ListFor = newValue;
this.enumerate();
break;
case "title":
this.header.textContent = this.title;
break;
}
}
getStyle(styleProp) {
if(window.getComputedStyle)
return document.defaultView.getComputedStyle(this, null).getPropertyValue(styleProp);
else
if(this.currentStyle)
return this.currentStyle[styleProp];
}
getZIndex(el) {
if(!el.style)
return 0;
var z = document.defaultView.getComputedStyle(el, null).getPropertyValue("z-index");
if(isNaN(z) && el.parentNode)
return this.getZIndex(el.parentNode);
return z;
}
bringToTop() {
if(!this.hasAttribute("active")) {
var j;
for(var i = 0; i < OmniWindow.Order.length; ++ i) {
if(this == OmniWindow.Order[i])
j = i;
}
if(isNaN(j))
return;
OmniWindow.Order.push(OmniWindow.Order.splice(j, 1)[0]);
j = 100 - OmniWindow.Order.length;
for(var el of OmniWindow.Order) {
el.style.zIndex = ++ j;
el.removeAttribute("active");
}
this.setAttribute("active", null);
this.enumerate();
}
}
enumerate() {
if(OmniWindow.ListFor) {
var hList = document.getElementById(OmniWindow.ListFor);
if(hList) {
hList.length = 0;
for(const el of OmniWindow.Order) {
const hOption = document.createElement("option");
hOption.textContent = el.title;
hOption.value = el.title;
hList.appendChild(hOption);
}
hList.size = hList.length;
}
}
}
arrange() {
var boxes = [];
//
for(const el of [...OmniWindow.Order]) {
boxes.push({el: el, w: el.offsetWidth, h: el.offsetHeight});
}
const {h, w, fill} = potpack(boxes);
for(const box of boxes) {
var el = box.el;
el.style.left = `${box.x}px`;
el.style.top = `${box.y}px`;
}
}
}
customElements.define("omni-icon", OmniIcon);