ngx-kit 1.0.0
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/README.md +2 -0
- package/fesm2022/ngx-input-angle-selector.mjs +338 -0
- package/fesm2022/ngx-input-angle-selector.mjs.map +1 -0
- package/fesm2022/ngx-input-box-shadow.mjs +422 -0
- package/fesm2022/ngx-input-box-shadow.mjs.map +1 -0
- package/fesm2022/ngx-input-color-picker.mjs +1348 -0
- package/fesm2022/ngx-input-color-picker.mjs.map +1 -0
- package/fesm2022/ngx-input-gradient-picker.mjs +607 -0
- package/fesm2022/ngx-input-gradient-picker.mjs.map +1 -0
- package/fesm2022/ngx-input-shared.mjs +915 -0
- package/fesm2022/ngx-input-shared.mjs.map +1 -0
- package/fesm2022/ngx-input.mjs +8 -0
- package/fesm2022/ngx-input.mjs.map +1 -0
- package/package.json +58 -0
- package/types/ngx-input-angle-selector.d.ts +79 -0
- package/types/ngx-input-box-shadow.d.ts +104 -0
- package/types/ngx-input-color-picker.d.ts +244 -0
- package/types/ngx-input-gradient-picker.d.ts +86 -0
- package/types/ngx-input-shared.d.ts +227 -0
- package/types/ngx-input.d.ts +3 -0
|
@@ -0,0 +1,915 @@
|
|
|
1
|
+
import * as i1 from '@angular/common';
|
|
2
|
+
import { CommonModule, isPlatformBrowser, DOCUMENT } from '@angular/common';
|
|
3
|
+
import * as i0 from '@angular/core';
|
|
4
|
+
import { EventEmitter, forwardRef, HostListener, ViewChild, Output, Input, ChangeDetectionStrategy, Component, PLATFORM_ID, Inject, Injectable, inject } from '@angular/core';
|
|
5
|
+
import { NG_VALUE_ACCESSOR, NG_VALIDATORS, FormControl, Validators } from '@angular/forms';
|
|
6
|
+
|
|
7
|
+
function getOffsetPosition(evt, parent) {
|
|
8
|
+
let position = {
|
|
9
|
+
x: 0,
|
|
10
|
+
y: 0,
|
|
11
|
+
};
|
|
12
|
+
if (evt instanceof MouseEvent) {
|
|
13
|
+
position.x = evt.pageX;
|
|
14
|
+
position.y = evt.pageY;
|
|
15
|
+
}
|
|
16
|
+
else if (evt.touches && evt.touches.length > 0) {
|
|
17
|
+
position.x = evt.touches[0].pageX;
|
|
18
|
+
position.y = evt.touches[0].pageY;
|
|
19
|
+
}
|
|
20
|
+
// Adjust for the parent's offset
|
|
21
|
+
let parentRect = parent.getBoundingClientRect();
|
|
22
|
+
position.x -= parentRect.left + window.scrollX;
|
|
23
|
+
position.y -= parentRect.top + window.scrollY;
|
|
24
|
+
return position;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class ValueModel {
|
|
28
|
+
id;
|
|
29
|
+
value;
|
|
30
|
+
x;
|
|
31
|
+
thumb;
|
|
32
|
+
color;
|
|
33
|
+
}
|
|
34
|
+
class RangeSliderComponent {
|
|
35
|
+
changeDetectorRef;
|
|
36
|
+
/**
|
|
37
|
+
* The step value for the slider
|
|
38
|
+
*/
|
|
39
|
+
step = 1;
|
|
40
|
+
/**
|
|
41
|
+
* The minimum value for the slider
|
|
42
|
+
*/
|
|
43
|
+
min = 0;
|
|
44
|
+
/**
|
|
45
|
+
* The maximum value for the slider
|
|
46
|
+
*/
|
|
47
|
+
max = 100;
|
|
48
|
+
/**
|
|
49
|
+
* The background color of the slider
|
|
50
|
+
* - can use css like `background: linear-gradient(to right, red, blue);`
|
|
51
|
+
* - or a solid color like `background: red;`
|
|
52
|
+
*/
|
|
53
|
+
background;
|
|
54
|
+
/**
|
|
55
|
+
* If true, the background will be transparent
|
|
56
|
+
*/
|
|
57
|
+
isBgTransparent = false;
|
|
58
|
+
/**
|
|
59
|
+
* If true, clicking on the slider will add a new range at that position
|
|
60
|
+
*/
|
|
61
|
+
addNewRangeOnClick = false;
|
|
62
|
+
/**
|
|
63
|
+
* The current value of the slider
|
|
64
|
+
*/
|
|
65
|
+
change = new EventEmitter();
|
|
66
|
+
selectedIndex;
|
|
67
|
+
selectedIndexChange = new EventEmitter();
|
|
68
|
+
isDragging = false;
|
|
69
|
+
slider;
|
|
70
|
+
thumb;
|
|
71
|
+
values = [];
|
|
72
|
+
isDisabled = false;
|
|
73
|
+
_onChange = (value) => { };
|
|
74
|
+
_onTouched = () => { };
|
|
75
|
+
_validatorOnChange = () => { };
|
|
76
|
+
sliderRect;
|
|
77
|
+
thumbRect;
|
|
78
|
+
constructor(changeDetectorRef) {
|
|
79
|
+
this.changeDetectorRef = changeDetectorRef;
|
|
80
|
+
}
|
|
81
|
+
ngOnInit() { }
|
|
82
|
+
generateId() {
|
|
83
|
+
let id = 'ngx-thumb-' + Math.random().toString(36).substring(2, 9);
|
|
84
|
+
if (this.values.findIndex((x) => x.id == id) >= 0) {
|
|
85
|
+
return this.generateId();
|
|
86
|
+
}
|
|
87
|
+
return id;
|
|
88
|
+
}
|
|
89
|
+
writeValue(items) {
|
|
90
|
+
this.values = [];
|
|
91
|
+
if (!items || !Array.isArray(items)) {
|
|
92
|
+
items = [];
|
|
93
|
+
}
|
|
94
|
+
if (items.length === 0) {
|
|
95
|
+
items.push({ id: this.generateId(), value: this.min });
|
|
96
|
+
}
|
|
97
|
+
for (let val of items) {
|
|
98
|
+
if (typeof val.value !== 'number' || isNaN(val.value)) {
|
|
99
|
+
throw new Error('RangeSliderComponent: value must be an array of numbers');
|
|
100
|
+
}
|
|
101
|
+
let newVal = +val.value;
|
|
102
|
+
if (newVal < +this.min)
|
|
103
|
+
newVal = +this.min;
|
|
104
|
+
else if (newVal > +this.max)
|
|
105
|
+
newVal = +this.max;
|
|
106
|
+
this.values.push({
|
|
107
|
+
...val,
|
|
108
|
+
id: val.id ?? this.generateId(),
|
|
109
|
+
value: newVal,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
this.updateAllThumbPositions();
|
|
113
|
+
}
|
|
114
|
+
validate(control) {
|
|
115
|
+
return null; // TODO: return errors if any;
|
|
116
|
+
}
|
|
117
|
+
registerOnValidatorChange(fn) {
|
|
118
|
+
this._validatorOnChange = fn;
|
|
119
|
+
}
|
|
120
|
+
registerOnChange(fn) {
|
|
121
|
+
this._onChange = fn;
|
|
122
|
+
}
|
|
123
|
+
registerOnTouched(fn) {
|
|
124
|
+
this._onTouched = fn;
|
|
125
|
+
}
|
|
126
|
+
setDisabledState(disabled) {
|
|
127
|
+
this.isDisabled = disabled;
|
|
128
|
+
}
|
|
129
|
+
updateRects() {
|
|
130
|
+
this.sliderRect = this.slider.nativeElement.getBoundingClientRect();
|
|
131
|
+
if (this.thumb) {
|
|
132
|
+
this.thumbRect = this.thumb.nativeElement.getBoundingClientRect();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
onDrag(ev) {
|
|
136
|
+
if (!this.isDragging)
|
|
137
|
+
return;
|
|
138
|
+
this.updateThumbPosition(ev);
|
|
139
|
+
}
|
|
140
|
+
onResize() {
|
|
141
|
+
this.writeValue(this.values);
|
|
142
|
+
}
|
|
143
|
+
dragStart(ev, index) {
|
|
144
|
+
ev.stopPropagation();
|
|
145
|
+
ev.preventDefault();
|
|
146
|
+
this.isDragging = true;
|
|
147
|
+
this.selectedIndex = index;
|
|
148
|
+
this.updateRects();
|
|
149
|
+
this.updateThumbPosition(ev);
|
|
150
|
+
this.selectedIndexChange.emit(this.selectedIndex);
|
|
151
|
+
}
|
|
152
|
+
addnewRangeOnSliderClick(event) {
|
|
153
|
+
if (!this.addNewRangeOnClick)
|
|
154
|
+
return;
|
|
155
|
+
const position = getOffsetPosition(event, this.slider.nativeElement);
|
|
156
|
+
const newValue = this.min + (position.x / this.sliderRect.width) * (this.max - this.min);
|
|
157
|
+
// must be add with order by position
|
|
158
|
+
const indexByOrderValue = this.values.findIndex((item) => item.value > newValue);
|
|
159
|
+
const insertIndex = indexByOrderValue >= 0 ? indexByOrderValue : this.values.length;
|
|
160
|
+
this.values.splice(insertIndex, 0, {
|
|
161
|
+
id: this.generateId(),
|
|
162
|
+
value: newValue,
|
|
163
|
+
});
|
|
164
|
+
this.dragStart(event, insertIndex);
|
|
165
|
+
// this.updateAllThumbPositions();
|
|
166
|
+
// this.valueChanged();
|
|
167
|
+
}
|
|
168
|
+
updateThumbPosition(ev) {
|
|
169
|
+
if (!this.isDragging || this.selectedIndex == undefined)
|
|
170
|
+
return;
|
|
171
|
+
if (!this.sliderRect || !this.thumbRect)
|
|
172
|
+
this.updateRects();
|
|
173
|
+
let position = getOffsetPosition(ev, this.slider.nativeElement);
|
|
174
|
+
let thumbRec = this.thumbRect;
|
|
175
|
+
position.x -= thumbRec.width / 2;
|
|
176
|
+
let sliderRec = this.sliderRect;
|
|
177
|
+
const thumb = this.values[this.selectedIndex];
|
|
178
|
+
if (position.x < 0) {
|
|
179
|
+
thumb.x = 0;
|
|
180
|
+
}
|
|
181
|
+
else if (position.x > sliderRec.width - thumbRec.width) {
|
|
182
|
+
thumb.x = sliderRec.width - thumbRec.width;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
thumb.x = position.x;
|
|
186
|
+
}
|
|
187
|
+
this.setValueByPosition(thumb, thumbRec, sliderRec);
|
|
188
|
+
}
|
|
189
|
+
updateAllThumbPositions() {
|
|
190
|
+
// wait to add thumbs
|
|
191
|
+
setTimeout(() => {
|
|
192
|
+
this.updateRects();
|
|
193
|
+
const sliderRec = this.sliderRect;
|
|
194
|
+
const thumbRec = this.thumbRect;
|
|
195
|
+
for (let item of this.values) {
|
|
196
|
+
item.x = ((item.value - this.min) * (sliderRec.width - thumbRec.width)) / (this.max - this.min);
|
|
197
|
+
}
|
|
198
|
+
this.changeDetectorRef.detectChanges();
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
setValueByPosition(thumb, thumbRec, sliderRec) {
|
|
202
|
+
const percentage = (thumb.x ?? 0) / (sliderRec.width - thumbRec.width);
|
|
203
|
+
let newValue = this.min + percentage * (this.max - this.min);
|
|
204
|
+
const stepDecimalPlaces = (this.step.toString().split('.')[1] || '').length;
|
|
205
|
+
newValue = parseFloat((Math.round(newValue / this.step) * this.step).toFixed(stepDecimalPlaces));
|
|
206
|
+
let value = Math.min(Math.max(newValue, this.min), this.max);
|
|
207
|
+
if (thumb.value !== value) {
|
|
208
|
+
thumb.value = value;
|
|
209
|
+
this.valueChanged();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
onDragEnd(ev) {
|
|
213
|
+
this.isDragging = false;
|
|
214
|
+
// this.selectedIndex = undefined;
|
|
215
|
+
}
|
|
216
|
+
valueChanged() {
|
|
217
|
+
const v = this.values; // this.values.map(({ x, thumb, ...rest }) => ({ ...rest }));
|
|
218
|
+
this._onChange(v);
|
|
219
|
+
this.change.emit(v);
|
|
220
|
+
}
|
|
221
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: RangeSliderComponent, deps: [{ token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
|
|
222
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.0.4", type: RangeSliderComponent, isStandalone: true, selector: "range-slider", inputs: { step: "step", min: "min", max: "max", background: "background", isBgTransparent: "isBgTransparent", addNewRangeOnClick: "addNewRangeOnClick", selectedIndex: "selectedIndex" }, outputs: { change: "change", selectedIndexChange: "selectedIndexChange" }, host: { listeners: { "document:mousemove": "onDrag($event)", "document:touchmove": "onDrag($event)", "window:resize": "onResize()", "document:mouseup": "onDragEnd($event)", "document:touchend": "onDragEnd($event)" } }, providers: [
|
|
223
|
+
{
|
|
224
|
+
provide: NG_VALUE_ACCESSOR,
|
|
225
|
+
useExisting: forwardRef(() => RangeSliderComponent),
|
|
226
|
+
multi: true,
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
provide: NG_VALIDATORS,
|
|
230
|
+
useExisting: forwardRef(() => RangeSliderComponent),
|
|
231
|
+
multi: true,
|
|
232
|
+
},
|
|
233
|
+
], viewQueries: [{ propertyName: "slider", first: true, predicate: ["slider"], descendants: true, static: true }, { propertyName: "thumb", first: true, predicate: ["thumb"], descendants: true }], ngImport: i0, template: "<div class=\"slider-container\">\r\n <ng-content></ng-content>\r\n <div\r\n #slider\r\n class=\"slider\"\r\n [class.add-range-cursor]=\"addNewRangeOnClick\"\r\n [ngStyle]=\"{ '--ngx-slider-bg': background }\"\r\n [class.bg-transparent]=\"isBgTransparent\"\r\n (mousedown)=\"addnewRangeOnSliderClick($event)\"\r\n (touchstart)=\"addnewRangeOnSliderClick($event)\">\r\n @for (item of values; track $index; let i = $index) {\r\n <div\r\n class=\"thumb\"\r\n [class.is-active]=\"selectedIndex == i\"\r\n #thumb\r\n [style.left.px]=\"item.x\"\r\n [style.background]=\"item.color\"\r\n [title]=\"item.value\"\r\n (mousedown)=\"dragStart($event, i)\"\r\n (touchstart)=\"dragStart($event, i)\"></div>\r\n }\r\n </div>\r\n</div>\r\n\r\n<!-- {{ selectedIndex }}\r\n<pre dir=\"ltr\" style=\"text-align: left\">{{ values | json }}</pre> -->\r\n\r\n", styles: [".slider-container{max-width:100%;padding:1px 0}.slider-container .slider{position:relative;box-shadow:inset #00000013 0 0 0 1px;border-radius:10px;height:12px;width:100%;background:var(--ngx-slider-bg, rgb(140, 51, 250));margin:10px 0}.slider-container .slider.bg-transparent{background:transparent}.slider-container .slider.bg-transparent:before,.slider-container .slider.bg-transparent:after{position:absolute;inset:1px;border-radius:9px}.slider-container .slider.bg-transparent:before{content:\" \";background-image:linear-gradient(45deg,#ccc 25%,transparent 25%),linear-gradient(-45deg,#ccc 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#ccc 75%),linear-gradient(-45deg,transparent 75%,#ccc 75%);background-size:16px 16px;background-position:0 0,0 8px,8px -8px,-8px 0px}.slider-container .slider.bg-transparent:after{content:\" \";background:var(--ngx-slider-bg)}.slider-container .slider.add-range-cursor{cursor:copy}.slider-container .thumb{box-shadow:#00000026 0 0 0 1px,#0000000d 0 10px 10px -5px,inset #fff 0 0 0 6px;height:var(--ngx-thumb-size, 30px);width:var(--ngx-thumb-size, 30px);display:block;border-radius:100%;top:calc(6px - var(--ngx-thumb-size, 30px) / 2);position:absolute;cursor:grab;z-index:100}.slider-container .thumb.is-active{outline:1px rgb(89,0,255) solid}\n"], dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
|
|
234
|
+
}
|
|
235
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: RangeSliderComponent, decorators: [{
|
|
236
|
+
type: Component,
|
|
237
|
+
args: [{ selector: 'range-slider', standalone: true, changeDetection: ChangeDetectionStrategy.OnPush, imports: [CommonModule], providers: [
|
|
238
|
+
{
|
|
239
|
+
provide: NG_VALUE_ACCESSOR,
|
|
240
|
+
useExisting: forwardRef(() => RangeSliderComponent),
|
|
241
|
+
multi: true,
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
provide: NG_VALIDATORS,
|
|
245
|
+
useExisting: forwardRef(() => RangeSliderComponent),
|
|
246
|
+
multi: true,
|
|
247
|
+
},
|
|
248
|
+
], template: "<div class=\"slider-container\">\r\n <ng-content></ng-content>\r\n <div\r\n #slider\r\n class=\"slider\"\r\n [class.add-range-cursor]=\"addNewRangeOnClick\"\r\n [ngStyle]=\"{ '--ngx-slider-bg': background }\"\r\n [class.bg-transparent]=\"isBgTransparent\"\r\n (mousedown)=\"addnewRangeOnSliderClick($event)\"\r\n (touchstart)=\"addnewRangeOnSliderClick($event)\">\r\n @for (item of values; track $index; let i = $index) {\r\n <div\r\n class=\"thumb\"\r\n [class.is-active]=\"selectedIndex == i\"\r\n #thumb\r\n [style.left.px]=\"item.x\"\r\n [style.background]=\"item.color\"\r\n [title]=\"item.value\"\r\n (mousedown)=\"dragStart($event, i)\"\r\n (touchstart)=\"dragStart($event, i)\"></div>\r\n }\r\n </div>\r\n</div>\r\n\r\n<!-- {{ selectedIndex }}\r\n<pre dir=\"ltr\" style=\"text-align: left\">{{ values | json }}</pre> -->\r\n\r\n", styles: [".slider-container{max-width:100%;padding:1px 0}.slider-container .slider{position:relative;box-shadow:inset #00000013 0 0 0 1px;border-radius:10px;height:12px;width:100%;background:var(--ngx-slider-bg, rgb(140, 51, 250));margin:10px 0}.slider-container .slider.bg-transparent{background:transparent}.slider-container .slider.bg-transparent:before,.slider-container .slider.bg-transparent:after{position:absolute;inset:1px;border-radius:9px}.slider-container .slider.bg-transparent:before{content:\" \";background-image:linear-gradient(45deg,#ccc 25%,transparent 25%),linear-gradient(-45deg,#ccc 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#ccc 75%),linear-gradient(-45deg,transparent 75%,#ccc 75%);background-size:16px 16px;background-position:0 0,0 8px,8px -8px,-8px 0px}.slider-container .slider.bg-transparent:after{content:\" \";background:var(--ngx-slider-bg)}.slider-container .slider.add-range-cursor{cursor:copy}.slider-container .thumb{box-shadow:#00000026 0 0 0 1px,#0000000d 0 10px 10px -5px,inset #fff 0 0 0 6px;height:var(--ngx-thumb-size, 30px);width:var(--ngx-thumb-size, 30px);display:block;border-radius:100%;top:calc(6px - var(--ngx-thumb-size, 30px) / 2);position:absolute;cursor:grab;z-index:100}.slider-container .thumb.is-active{outline:1px rgb(89,0,255) solid}\n"] }]
|
|
249
|
+
}], ctorParameters: () => [{ type: i0.ChangeDetectorRef }], propDecorators: { step: [{
|
|
250
|
+
type: Input
|
|
251
|
+
}], min: [{
|
|
252
|
+
type: Input
|
|
253
|
+
}], max: [{
|
|
254
|
+
type: Input
|
|
255
|
+
}], background: [{
|
|
256
|
+
type: Input
|
|
257
|
+
}], isBgTransparent: [{
|
|
258
|
+
type: Input
|
|
259
|
+
}], addNewRangeOnClick: [{
|
|
260
|
+
type: Input
|
|
261
|
+
}], change: [{
|
|
262
|
+
type: Output
|
|
263
|
+
}], selectedIndex: [{
|
|
264
|
+
type: Input
|
|
265
|
+
}], selectedIndexChange: [{
|
|
266
|
+
type: Output
|
|
267
|
+
}], slider: [{
|
|
268
|
+
type: ViewChild,
|
|
269
|
+
args: ['slider', { static: true }]
|
|
270
|
+
}], thumb: [{
|
|
271
|
+
type: ViewChild,
|
|
272
|
+
args: ['thumb', { static: false }]
|
|
273
|
+
}], onDrag: [{
|
|
274
|
+
type: HostListener,
|
|
275
|
+
args: ['document:mousemove', ['$event']]
|
|
276
|
+
}, {
|
|
277
|
+
type: HostListener,
|
|
278
|
+
args: ['document:touchmove', ['$event']]
|
|
279
|
+
}], onResize: [{
|
|
280
|
+
type: HostListener,
|
|
281
|
+
args: ['window:resize']
|
|
282
|
+
}], onDragEnd: [{
|
|
283
|
+
type: HostListener,
|
|
284
|
+
args: ['document:mouseup', ['$event']]
|
|
285
|
+
}, {
|
|
286
|
+
type: HostListener,
|
|
287
|
+
args: ['document:touchend', ['$event']]
|
|
288
|
+
}] } });
|
|
289
|
+
|
|
290
|
+
class SaturationComponent {
|
|
291
|
+
width;
|
|
292
|
+
height;
|
|
293
|
+
color = 'red';
|
|
294
|
+
step = 1;
|
|
295
|
+
min = { x: 0, y: 0 };
|
|
296
|
+
max = { x: 100, y: 100 };
|
|
297
|
+
change = new EventEmitter();
|
|
298
|
+
isDragging = false;
|
|
299
|
+
saturation;
|
|
300
|
+
thumb;
|
|
301
|
+
x = 0;
|
|
302
|
+
y = 0;
|
|
303
|
+
myControl = new FormControl(null);
|
|
304
|
+
isDisabled = false;
|
|
305
|
+
_onChange = (value) => { };
|
|
306
|
+
_onTouched = () => { };
|
|
307
|
+
_validatorOnChange = () => { };
|
|
308
|
+
saturationRect;
|
|
309
|
+
thumbRect;
|
|
310
|
+
constructor() { }
|
|
311
|
+
updateRects() {
|
|
312
|
+
this.saturationRect = this.saturation.nativeElement.getBoundingClientRect();
|
|
313
|
+
this.thumbRect = this.thumb.nativeElement.getBoundingClientRect();
|
|
314
|
+
}
|
|
315
|
+
writeValue(val) {
|
|
316
|
+
if (!val)
|
|
317
|
+
val = { x: 0, y: 0 };
|
|
318
|
+
let value = val;
|
|
319
|
+
this.myControl.setValue(value, { emitEvent: false });
|
|
320
|
+
this.updateRects();
|
|
321
|
+
const saturationRec = this.saturationRect;
|
|
322
|
+
const thumbRec = this.thumbRect;
|
|
323
|
+
this.x = ((value.x - this.min.x) * (saturationRec.width - thumbRec.width / 2)) / (this.max.x - this.min.x);
|
|
324
|
+
this.y = ((value.y - this.min.y) * (saturationRec.height - thumbRec.height / 2)) / (this.max.y - this.min.y);
|
|
325
|
+
if (val !== value) {
|
|
326
|
+
this.valueChanged(value);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
validate(control) {
|
|
330
|
+
return this.myControl.errors;
|
|
331
|
+
}
|
|
332
|
+
registerOnValidatorChange(fn) {
|
|
333
|
+
this._validatorOnChange = fn;
|
|
334
|
+
}
|
|
335
|
+
registerOnChange(fn) {
|
|
336
|
+
this._onChange = fn;
|
|
337
|
+
}
|
|
338
|
+
registerOnTouched(fn) {
|
|
339
|
+
this._onTouched = fn;
|
|
340
|
+
}
|
|
341
|
+
setDisabledState(disabled) {
|
|
342
|
+
this.isDisabled = disabled;
|
|
343
|
+
if (disabled)
|
|
344
|
+
this.myControl.disable();
|
|
345
|
+
else
|
|
346
|
+
this.myControl.enable();
|
|
347
|
+
}
|
|
348
|
+
dragStart(ev) {
|
|
349
|
+
ev.stopPropagation();
|
|
350
|
+
ev.preventDefault();
|
|
351
|
+
this.isDragging = true;
|
|
352
|
+
this.updateRects();
|
|
353
|
+
this.updatePosition(ev);
|
|
354
|
+
}
|
|
355
|
+
onResize() {
|
|
356
|
+
this.writeValue(this.myControl.value);
|
|
357
|
+
}
|
|
358
|
+
onDrag(ev) {
|
|
359
|
+
if (!this.isDragging)
|
|
360
|
+
return;
|
|
361
|
+
this.updatePosition(ev);
|
|
362
|
+
}
|
|
363
|
+
updatePosition(ev) {
|
|
364
|
+
if (!this.isDragging)
|
|
365
|
+
return;
|
|
366
|
+
if (!this.saturationRect || !this.thumbRect)
|
|
367
|
+
this.updateRects();
|
|
368
|
+
let position = getOffsetPosition(ev, this.saturation.nativeElement);
|
|
369
|
+
let thumbRec = this.thumbRect;
|
|
370
|
+
let saturationRec = this.saturationRect;
|
|
371
|
+
if (position.x < 0) {
|
|
372
|
+
this.x = 0;
|
|
373
|
+
}
|
|
374
|
+
else if (position.x > saturationRec.width - (thumbRec.width / 2 - 3)) {
|
|
375
|
+
this.x = saturationRec.width - (thumbRec.width / 2 - 3);
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
this.x = position.x;
|
|
379
|
+
}
|
|
380
|
+
// this.x = this.x - thumbRec.width / 2;
|
|
381
|
+
if (position.y < 0) {
|
|
382
|
+
this.y = 0;
|
|
383
|
+
}
|
|
384
|
+
else if (position.y > saturationRec.height - (thumbRec.height / 2 - 3)) {
|
|
385
|
+
this.y = saturationRec.height - (thumbRec.height / 2 - 3);
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
this.y = position.y;
|
|
389
|
+
}
|
|
390
|
+
// this.y = this.y - thumbRec.height / 2;
|
|
391
|
+
this.setValueByPosition(thumbRec, saturationRec);
|
|
392
|
+
}
|
|
393
|
+
onDragEnd(ev) {
|
|
394
|
+
this.isDragging = false;
|
|
395
|
+
}
|
|
396
|
+
setValueByPosition(thumbRec, saturationRec) {
|
|
397
|
+
const percentageX = this.x / (saturationRec.width - thumbRec.width);
|
|
398
|
+
let newValueX = this.min.x + percentageX * (this.max.x - this.min.x);
|
|
399
|
+
newValueX = Math.round(newValueX / this.step) * this.step;
|
|
400
|
+
let valueX = Math.min(Math.max(newValueX, this.min.x), this.max.x);
|
|
401
|
+
//-----------------------------
|
|
402
|
+
const percentageY = this.y / (saturationRec.height - thumbRec.height);
|
|
403
|
+
let newValueY = this.min.y + percentageY * (this.max.y - this.min.y);
|
|
404
|
+
newValueY = Math.round(newValueY / this.step) * this.step;
|
|
405
|
+
let valueY = Math.min(Math.max(newValueY, this.min.y), this.max.y);
|
|
406
|
+
const newValue = { x: valueX, y: valueY };
|
|
407
|
+
if (!this.myControl.value || this.myControl.value.x !== valueX || this.myControl.value.y !== valueY) {
|
|
408
|
+
this.valueChanged(newValue);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
valueChanged(value) {
|
|
412
|
+
this.myControl.setValue(value, { emitEvent: false });
|
|
413
|
+
this._onChange(value);
|
|
414
|
+
this.change.emit(value);
|
|
415
|
+
}
|
|
416
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: SaturationComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
417
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "22.0.4", type: SaturationComponent, isStandalone: true, selector: "saturation", inputs: { width: "width", height: "height", color: "color", step: "step", min: "min", max: "max" }, outputs: { change: "change" }, host: { listeners: { "window:resize": "onResize()", "document:mousemove": "onDrag($event)", "document:touchmove": "onDrag($event)", "document:mouseup": "onDragEnd($event)", "document:touchend": "onDragEnd($event)" } }, providers: [
|
|
418
|
+
{
|
|
419
|
+
provide: NG_VALUE_ACCESSOR,
|
|
420
|
+
useExisting: forwardRef(() => SaturationComponent),
|
|
421
|
+
multi: true,
|
|
422
|
+
},
|
|
423
|
+
], viewQueries: [{ propertyName: "saturation", first: true, predicate: ["saturation"], descendants: true, static: true }, { propertyName: "thumb", first: true, predicate: ["thumb"], descendants: true, static: true }], ngImport: i0, template: "<div\r\n class=\"saturation-container\"\r\n [style.width.px]=\"width\"\r\n [style.height.px]=\"height\"\r\n (mousedown)=\"dragStart($event)\"\r\n (touchstart)=\"dragStart($event)\">\r\n <div class=\"saturation\" #saturation>\r\n <div class=\"s-bg\" [style.background]=\"color\"></div>\r\n <div class=\"s-white\"></div>\r\n <div class=\"s-black\"></div>\r\n </div>\r\n <div class=\"thumb\" #thumb [style.left.px]=\"x\" [style.top.px]=\"y\"></div>\r\n</div>\r\n", styles: [".saturation-container{position:relative;width:100%;height:200px}.saturation-container .thumb{border:3px #000000 solid;box-shadow:inset 0 0 0 2px #fff;width:12px;height:12px;display:block;border-radius:10px;position:absolute;pointer-events:none;cursor:pointer}.saturation{position:absolute;inset:6px;border-radius:4px;overflow:hidden}.saturation .s-white,.saturation .s-black,.saturation .s-bg{position:absolute;inset:0}.saturation .s-white{background:linear-gradient(to right,#fff,#fff0)}.saturation .s-black{background:linear-gradient(to bottom,#0000,#000)}\n"] });
|
|
424
|
+
}
|
|
425
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: SaturationComponent, decorators: [{
|
|
426
|
+
type: Component,
|
|
427
|
+
args: [{ selector: 'saturation', standalone: true, imports: [], providers: [
|
|
428
|
+
{
|
|
429
|
+
provide: NG_VALUE_ACCESSOR,
|
|
430
|
+
useExisting: forwardRef(() => SaturationComponent),
|
|
431
|
+
multi: true,
|
|
432
|
+
},
|
|
433
|
+
], template: "<div\r\n class=\"saturation-container\"\r\n [style.width.px]=\"width\"\r\n [style.height.px]=\"height\"\r\n (mousedown)=\"dragStart($event)\"\r\n (touchstart)=\"dragStart($event)\">\r\n <div class=\"saturation\" #saturation>\r\n <div class=\"s-bg\" [style.background]=\"color\"></div>\r\n <div class=\"s-white\"></div>\r\n <div class=\"s-black\"></div>\r\n </div>\r\n <div class=\"thumb\" #thumb [style.left.px]=\"x\" [style.top.px]=\"y\"></div>\r\n</div>\r\n", styles: [".saturation-container{position:relative;width:100%;height:200px}.saturation-container .thumb{border:3px #000000 solid;box-shadow:inset 0 0 0 2px #fff;width:12px;height:12px;display:block;border-radius:10px;position:absolute;pointer-events:none;cursor:pointer}.saturation{position:absolute;inset:6px;border-radius:4px;overflow:hidden}.saturation .s-white,.saturation .s-black,.saturation .s-bg{position:absolute;inset:0}.saturation .s-white{background:linear-gradient(to right,#fff,#fff0)}.saturation .s-black{background:linear-gradient(to bottom,#0000,#000)}\n"] }]
|
|
434
|
+
}], ctorParameters: () => [], propDecorators: { width: [{
|
|
435
|
+
type: Input
|
|
436
|
+
}], height: [{
|
|
437
|
+
type: Input
|
|
438
|
+
}], color: [{
|
|
439
|
+
type: Input
|
|
440
|
+
}], step: [{
|
|
441
|
+
type: Input
|
|
442
|
+
}], min: [{
|
|
443
|
+
type: Input
|
|
444
|
+
}], max: [{
|
|
445
|
+
type: Input
|
|
446
|
+
}], change: [{
|
|
447
|
+
type: Output
|
|
448
|
+
}], saturation: [{
|
|
449
|
+
type: ViewChild,
|
|
450
|
+
args: ['saturation', { static: true }]
|
|
451
|
+
}], thumb: [{
|
|
452
|
+
type: ViewChild,
|
|
453
|
+
args: ['thumb', { static: true }]
|
|
454
|
+
}], onResize: [{
|
|
455
|
+
type: HostListener,
|
|
456
|
+
args: ['window:resize']
|
|
457
|
+
}], onDrag: [{
|
|
458
|
+
type: HostListener,
|
|
459
|
+
args: ['document:mousemove', ['$event']]
|
|
460
|
+
}, {
|
|
461
|
+
type: HostListener,
|
|
462
|
+
args: ['document:touchmove', ['$event']]
|
|
463
|
+
}], onDragEnd: [{
|
|
464
|
+
type: HostListener,
|
|
465
|
+
args: ['document:mouseup', ['$event']]
|
|
466
|
+
}, {
|
|
467
|
+
type: HostListener,
|
|
468
|
+
args: ['document:touchend', ['$event']]
|
|
469
|
+
}] } });
|
|
470
|
+
|
|
471
|
+
class SliderComponent {
|
|
472
|
+
step = 1;
|
|
473
|
+
min = 0;
|
|
474
|
+
max = 100;
|
|
475
|
+
background;
|
|
476
|
+
isBgTransparent = false;
|
|
477
|
+
change = new EventEmitter();
|
|
478
|
+
isDragging = false;
|
|
479
|
+
slider;
|
|
480
|
+
thumb;
|
|
481
|
+
x = 0;
|
|
482
|
+
myControl = new FormControl(null);
|
|
483
|
+
isDisabled = false;
|
|
484
|
+
_onChange = (value) => { };
|
|
485
|
+
_onTouched = () => { };
|
|
486
|
+
_validatorOnChange = () => { };
|
|
487
|
+
sliderRect;
|
|
488
|
+
thumbRect;
|
|
489
|
+
constructor() { }
|
|
490
|
+
ngOnInit() {
|
|
491
|
+
this.myControl.setValidators([Validators.min(this.min), Validators.max(this.max)]);
|
|
492
|
+
}
|
|
493
|
+
updateRects() {
|
|
494
|
+
this.sliderRect = this.slider.nativeElement.getBoundingClientRect();
|
|
495
|
+
this.thumbRect = this.thumb.nativeElement.getBoundingClientRect();
|
|
496
|
+
}
|
|
497
|
+
writeValue(val) {
|
|
498
|
+
let value = 0;
|
|
499
|
+
if (!val)
|
|
500
|
+
value = 0;
|
|
501
|
+
else if (+val < +this.min)
|
|
502
|
+
value = +this.min;
|
|
503
|
+
else if (+val > +this.max)
|
|
504
|
+
value = +this.max;
|
|
505
|
+
else
|
|
506
|
+
value = +val;
|
|
507
|
+
this.myControl.setValue(value, { emitEvent: false });
|
|
508
|
+
this.updateRects();
|
|
509
|
+
const sliderRec = this.sliderRect;
|
|
510
|
+
const thumbRec = this.thumbRect;
|
|
511
|
+
this.x = ((value - this.min) * (sliderRec.width - thumbRec.width)) / (this.max - this.min);
|
|
512
|
+
if (val !== value) {
|
|
513
|
+
this.valueChanged(value);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
validate(control) {
|
|
517
|
+
return this.myControl.errors;
|
|
518
|
+
}
|
|
519
|
+
registerOnValidatorChange(fn) {
|
|
520
|
+
this._validatorOnChange = fn;
|
|
521
|
+
}
|
|
522
|
+
registerOnChange(fn) {
|
|
523
|
+
this._onChange = fn;
|
|
524
|
+
}
|
|
525
|
+
registerOnTouched(fn) {
|
|
526
|
+
this._onTouched = fn;
|
|
527
|
+
}
|
|
528
|
+
setDisabledState(disabled) {
|
|
529
|
+
this.isDisabled = disabled;
|
|
530
|
+
if (disabled)
|
|
531
|
+
this.myControl.disable();
|
|
532
|
+
else
|
|
533
|
+
this.myControl.enable();
|
|
534
|
+
}
|
|
535
|
+
dragStart(ev) {
|
|
536
|
+
ev.stopPropagation();
|
|
537
|
+
ev.preventDefault();
|
|
538
|
+
this.isDragging = true;
|
|
539
|
+
this.updateRects();
|
|
540
|
+
this.updatePosition(ev);
|
|
541
|
+
}
|
|
542
|
+
onDrag(ev) {
|
|
543
|
+
if (!this.isDragging)
|
|
544
|
+
return;
|
|
545
|
+
this.updatePosition(ev);
|
|
546
|
+
}
|
|
547
|
+
onResize() {
|
|
548
|
+
this.writeValue(this.myControl.value);
|
|
549
|
+
}
|
|
550
|
+
updatePosition(ev) {
|
|
551
|
+
if (!this.isDragging)
|
|
552
|
+
return;
|
|
553
|
+
if (!this.sliderRect || !this.thumbRect)
|
|
554
|
+
this.updateRects();
|
|
555
|
+
let position = getOffsetPosition(ev, this.slider.nativeElement);
|
|
556
|
+
let thumbRec = this.thumbRect;
|
|
557
|
+
position.x -= thumbRec.width / 2;
|
|
558
|
+
let sliderRec = this.sliderRect;
|
|
559
|
+
if (position.x < 0) {
|
|
560
|
+
this.x = 0;
|
|
561
|
+
}
|
|
562
|
+
else if (position.x > sliderRec.width - thumbRec.width) {
|
|
563
|
+
this.x = sliderRec.width - thumbRec.width;
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
this.x = position.x;
|
|
567
|
+
}
|
|
568
|
+
this.setValueByPosition(thumbRec, sliderRec);
|
|
569
|
+
}
|
|
570
|
+
setValueByPosition(thumbRec, sliderRec) {
|
|
571
|
+
const percentage = this.x / (sliderRec.width - thumbRec.width);
|
|
572
|
+
let newValue = this.min + percentage * (this.max - this.min);
|
|
573
|
+
const stepDecimalPlaces = (this.step.toString().split('.')[1] || '').length;
|
|
574
|
+
newValue = parseFloat((Math.round(newValue / this.step) * this.step).toFixed(stepDecimalPlaces));
|
|
575
|
+
let value = Math.min(Math.max(newValue, this.min), this.max);
|
|
576
|
+
if (this.myControl.value !== value) {
|
|
577
|
+
this.valueChanged(value);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
onDragEnd(ev) {
|
|
581
|
+
this.isDragging = false;
|
|
582
|
+
}
|
|
583
|
+
valueChanged(value) {
|
|
584
|
+
this.myControl.setValue(value, { emitEvent: false });
|
|
585
|
+
this._onChange(value);
|
|
586
|
+
this.change.emit(value);
|
|
587
|
+
}
|
|
588
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: SliderComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
589
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "22.0.4", type: SliderComponent, isStandalone: true, selector: "slider", inputs: { step: "step", min: "min", max: "max", background: "background", isBgTransparent: "isBgTransparent" }, outputs: { change: "change" }, host: { listeners: { "document:mousemove": "onDrag($event)", "document:touchmove": "onDrag($event)", "window:resize": "onResize()", "document:mouseup": "onDragEnd($event)", "document:touchend": "onDragEnd($event)" } }, providers: [
|
|
590
|
+
{
|
|
591
|
+
provide: NG_VALUE_ACCESSOR,
|
|
592
|
+
useExisting: forwardRef(() => SliderComponent),
|
|
593
|
+
multi: true,
|
|
594
|
+
},
|
|
595
|
+
{
|
|
596
|
+
provide: NG_VALIDATORS,
|
|
597
|
+
useExisting: forwardRef(() => SliderComponent),
|
|
598
|
+
multi: true,
|
|
599
|
+
},
|
|
600
|
+
], viewQueries: [{ propertyName: "slider", first: true, predicate: ["slider"], descendants: true, static: true }, { propertyName: "thumb", first: true, predicate: ["thumb"], descendants: true, static: true }], ngImport: i0, template: "<div class=\"slider-container\">\r\n <ng-content></ng-content>\r\n <div\r\n #slider\r\n class=\"slider\"\r\n (mousedown)=\"dragStart($event)\"\r\n (touchstart)=\"dragStart($event)\"\r\n [ngStyle]=\"{ '--ngx-slider-bg': background }\"\r\n [class.bg-transparent]=\"isBgTransparent\">\r\n <div class=\"thumb\" #thumb [style.left.px]=\"x\" [title]=\"myControl.value\"></div>\r\n </div>\r\n</div>\r\n", styles: [".slider-container{max-width:100%;padding:1px 0}.slider-container .slider{position:relative;box-shadow:inset #00000013 0 0 0 1px;border-radius:10px;height:12px;width:100%;background:var(--ngx-slider-bg, rgb(140, 51, 250));margin:10px 0}.slider-container .slider.bg-transparent{background:transparent}.slider-container .slider.bg-transparent:before,.slider-container .slider.bg-transparent:after{position:absolute;inset:1px;border-radius:9px}.slider-container .slider.bg-transparent:before{content:\" \";background-image:linear-gradient(45deg,#ccc 25%,transparent 25%),linear-gradient(-45deg,#ccc 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#ccc 75%),linear-gradient(-45deg,transparent 75%,#ccc 75%);background-size:16px 16px;background-position:0 0,0 8px,8px -8px,-8px 0px}.slider-container .slider.bg-transparent:after{content:\" \";background:var(--ngx-slider-bg)}.slider-container .thumb{box-shadow:#00000026 0 0 0 1px,#0000000d 0 10px 10px -5px,inset #fff 0 0 0 6px;height:var(--ngx-thumb-size, 30px);width:var(--ngx-thumb-size, 30px);display:block;border-radius:100%;top:calc(6px - var(--ngx-thumb-size, 30px) / 2);position:absolute;cursor:grab;z-index:100}\n"], dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
|
|
601
|
+
}
|
|
602
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: SliderComponent, decorators: [{
|
|
603
|
+
type: Component,
|
|
604
|
+
args: [{ selector: 'slider', standalone: true, imports: [CommonModule], changeDetection: ChangeDetectionStrategy.OnPush, providers: [
|
|
605
|
+
{
|
|
606
|
+
provide: NG_VALUE_ACCESSOR,
|
|
607
|
+
useExisting: forwardRef(() => SliderComponent),
|
|
608
|
+
multi: true,
|
|
609
|
+
},
|
|
610
|
+
{
|
|
611
|
+
provide: NG_VALIDATORS,
|
|
612
|
+
useExisting: forwardRef(() => SliderComponent),
|
|
613
|
+
multi: true,
|
|
614
|
+
},
|
|
615
|
+
], template: "<div class=\"slider-container\">\r\n <ng-content></ng-content>\r\n <div\r\n #slider\r\n class=\"slider\"\r\n (mousedown)=\"dragStart($event)\"\r\n (touchstart)=\"dragStart($event)\"\r\n [ngStyle]=\"{ '--ngx-slider-bg': background }\"\r\n [class.bg-transparent]=\"isBgTransparent\">\r\n <div class=\"thumb\" #thumb [style.left.px]=\"x\" [title]=\"myControl.value\"></div>\r\n </div>\r\n</div>\r\n", styles: [".slider-container{max-width:100%;padding:1px 0}.slider-container .slider{position:relative;box-shadow:inset #00000013 0 0 0 1px;border-radius:10px;height:12px;width:100%;background:var(--ngx-slider-bg, rgb(140, 51, 250));margin:10px 0}.slider-container .slider.bg-transparent{background:transparent}.slider-container .slider.bg-transparent:before,.slider-container .slider.bg-transparent:after{position:absolute;inset:1px;border-radius:9px}.slider-container .slider.bg-transparent:before{content:\" \";background-image:linear-gradient(45deg,#ccc 25%,transparent 25%),linear-gradient(-45deg,#ccc 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#ccc 75%),linear-gradient(-45deg,transparent 75%,#ccc 75%);background-size:16px 16px;background-position:0 0,0 8px,8px -8px,-8px 0px}.slider-container .slider.bg-transparent:after{content:\" \";background:var(--ngx-slider-bg)}.slider-container .thumb{box-shadow:#00000026 0 0 0 1px,#0000000d 0 10px 10px -5px,inset #fff 0 0 0 6px;height:var(--ngx-thumb-size, 30px);width:var(--ngx-thumb-size, 30px);display:block;border-radius:100%;top:calc(6px - var(--ngx-thumb-size, 30px) / 2);position:absolute;cursor:grab;z-index:100}\n"] }]
|
|
616
|
+
}], ctorParameters: () => [], propDecorators: { step: [{
|
|
617
|
+
type: Input
|
|
618
|
+
}], min: [{
|
|
619
|
+
type: Input
|
|
620
|
+
}], max: [{
|
|
621
|
+
type: Input
|
|
622
|
+
}], background: [{
|
|
623
|
+
type: Input
|
|
624
|
+
}], isBgTransparent: [{
|
|
625
|
+
type: Input
|
|
626
|
+
}], change: [{
|
|
627
|
+
type: Output
|
|
628
|
+
}], slider: [{
|
|
629
|
+
type: ViewChild,
|
|
630
|
+
args: ['slider', { static: true }]
|
|
631
|
+
}], thumb: [{
|
|
632
|
+
type: ViewChild,
|
|
633
|
+
args: ['thumb', { static: true }]
|
|
634
|
+
}], onDrag: [{
|
|
635
|
+
type: HostListener,
|
|
636
|
+
args: ['document:mousemove', ['$event']]
|
|
637
|
+
}, {
|
|
638
|
+
type: HostListener,
|
|
639
|
+
args: ['document:touchmove', ['$event']]
|
|
640
|
+
}], onResize: [{
|
|
641
|
+
type: HostListener,
|
|
642
|
+
args: ['window:resize']
|
|
643
|
+
}], onDragEnd: [{
|
|
644
|
+
type: HostListener,
|
|
645
|
+
args: ['document:mouseup', ['$event']]
|
|
646
|
+
}, {
|
|
647
|
+
type: HostListener,
|
|
648
|
+
args: ['document:touchend', ['$event']]
|
|
649
|
+
}] } });
|
|
650
|
+
|
|
651
|
+
class BrowserService {
|
|
652
|
+
platformId;
|
|
653
|
+
constructor(platformId) {
|
|
654
|
+
this.platformId = platformId;
|
|
655
|
+
}
|
|
656
|
+
get isBrowser() {
|
|
657
|
+
return isPlatformBrowser(this.platformId);
|
|
658
|
+
}
|
|
659
|
+
get prefersDarkMode() {
|
|
660
|
+
if (!this.isBrowser)
|
|
661
|
+
return false;
|
|
662
|
+
return window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
663
|
+
}
|
|
664
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: BrowserService, deps: [{ token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
665
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: BrowserService, providedIn: 'root' });
|
|
666
|
+
}
|
|
667
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: BrowserService, decorators: [{
|
|
668
|
+
type: Injectable,
|
|
669
|
+
args: [{
|
|
670
|
+
providedIn: 'root',
|
|
671
|
+
}]
|
|
672
|
+
}], ctorParameters: () => [{ type: undefined, decorators: [{
|
|
673
|
+
type: Inject,
|
|
674
|
+
args: [PLATFORM_ID]
|
|
675
|
+
}] }] });
|
|
676
|
+
|
|
677
|
+
class DialogOverlayRef {
|
|
678
|
+
componentRef;
|
|
679
|
+
dialogEl;
|
|
680
|
+
teardownFn;
|
|
681
|
+
constructor(componentRef, dialogEl, teardownFn) {
|
|
682
|
+
this.componentRef = componentRef;
|
|
683
|
+
this.dialogEl = dialogEl;
|
|
684
|
+
this.teardownFn = teardownFn;
|
|
685
|
+
}
|
|
686
|
+
close() {
|
|
687
|
+
this.teardownFn();
|
|
688
|
+
}
|
|
689
|
+
get nativeElement() {
|
|
690
|
+
return this.dialogEl;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
class DialogService {
|
|
695
|
+
openDialogs = new Map();
|
|
696
|
+
idCounter = 0;
|
|
697
|
+
globalKeyDownAdded = false;
|
|
698
|
+
globalResizeAdded = false;
|
|
699
|
+
globalClickAdded = false;
|
|
700
|
+
globalKeyDownListener;
|
|
701
|
+
globalResizeListener;
|
|
702
|
+
globalClickListener;
|
|
703
|
+
doc = inject(DOCUMENT);
|
|
704
|
+
constructor() {
|
|
705
|
+
this.globalKeyDownListener = (e) => {
|
|
706
|
+
if (e.key === 'Escape') {
|
|
707
|
+
const lastDialog = this.getLastDialog();
|
|
708
|
+
if (lastDialog) {
|
|
709
|
+
lastDialog.cleanup();
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
this.globalResizeListener = () => {
|
|
714
|
+
this.repositionAll();
|
|
715
|
+
};
|
|
716
|
+
this.globalClickListener = (e) => {
|
|
717
|
+
this.handleClickOnBackdrop(e);
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
ngOnDestroy() {
|
|
721
|
+
this.closeAll();
|
|
722
|
+
}
|
|
723
|
+
handleGlobalListeners() {
|
|
724
|
+
// مطمئن شویم لیسنرها فقط یک بار اضافه شدهاند
|
|
725
|
+
if (!this.globalKeyDownAdded) {
|
|
726
|
+
this.doc.addEventListener('keydown', this.globalKeyDownListener);
|
|
727
|
+
this.globalKeyDownAdded = true;
|
|
728
|
+
}
|
|
729
|
+
if (!this.globalResizeAdded) {
|
|
730
|
+
window.addEventListener('resize', this.globalResizeListener);
|
|
731
|
+
this.globalResizeAdded = true;
|
|
732
|
+
}
|
|
733
|
+
if (!this.globalClickAdded) {
|
|
734
|
+
this.doc.addEventListener('click', this.globalClickListener);
|
|
735
|
+
this.globalClickAdded = true;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
open(options) {
|
|
739
|
+
const { anchor, component, viewContainerRef, configure, onClosed } = options;
|
|
740
|
+
if (!viewContainerRef) {
|
|
741
|
+
throw new Error('ViewContainerRef is required to render dialog content.');
|
|
742
|
+
}
|
|
743
|
+
this.handleGlobalListeners();
|
|
744
|
+
const id = ++this.idCounter;
|
|
745
|
+
const dialogElement = this.doc.createElement('dialog');
|
|
746
|
+
dialogElement.style.position = 'absolute';
|
|
747
|
+
dialogElement.style.outline = 'none';
|
|
748
|
+
dialogElement.style.padding = '0';
|
|
749
|
+
dialogElement.style.margin = '0';
|
|
750
|
+
dialogElement.style.border = 'none';
|
|
751
|
+
dialogElement.style.background = 'transparent';
|
|
752
|
+
dialogElement.style.maxWidth = '100vw';
|
|
753
|
+
dialogElement.style.maxHeight = '100vh';
|
|
754
|
+
dialogElement.style.overflow = 'visible';
|
|
755
|
+
dialogElement.style.zIndex = `${1000 + this.openDialogs.size}`;
|
|
756
|
+
this.doc.body.appendChild(dialogElement);
|
|
757
|
+
const componentRef = viewContainerRef.createComponent(component);
|
|
758
|
+
if (componentRef.location.nativeElement) {
|
|
759
|
+
dialogElement.appendChild(componentRef.location.nativeElement);
|
|
760
|
+
}
|
|
761
|
+
// تابع پاکسازی اختصاصی برای این دیالوگ
|
|
762
|
+
const cleanup = () => {
|
|
763
|
+
if (!this.openDialogs.has(id))
|
|
764
|
+
return; // جلوگیری از پاکسازی مجدد
|
|
765
|
+
this.openDialogs.delete(id);
|
|
766
|
+
if (componentRef.location.nativeElement?.parentNode) {
|
|
767
|
+
componentRef.location.nativeElement.remove();
|
|
768
|
+
}
|
|
769
|
+
componentRef.destroy();
|
|
770
|
+
dialogElement.remove();
|
|
771
|
+
onClosed?.();
|
|
772
|
+
};
|
|
773
|
+
const instance = {
|
|
774
|
+
id,
|
|
775
|
+
anchor,
|
|
776
|
+
element: dialogElement,
|
|
777
|
+
componentRef: componentRef,
|
|
778
|
+
options: options,
|
|
779
|
+
cleanup,
|
|
780
|
+
};
|
|
781
|
+
this.openDialogs.set(id, instance);
|
|
782
|
+
dialogElement.showModal();
|
|
783
|
+
if (configure) {
|
|
784
|
+
const ref = new DialogOverlayRef(componentRef, dialogElement, cleanup);
|
|
785
|
+
configure(componentRef.instance, ref);
|
|
786
|
+
}
|
|
787
|
+
requestAnimationFrame(() => {
|
|
788
|
+
this.positionDialog(instance);
|
|
789
|
+
});
|
|
790
|
+
setTimeout(() => {
|
|
791
|
+
const focusable = dialogElement.querySelector('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');
|
|
792
|
+
if (focusable) {
|
|
793
|
+
focusable.focus();
|
|
794
|
+
}
|
|
795
|
+
}, 0);
|
|
796
|
+
const ref = new DialogOverlayRef(componentRef, dialogElement, cleanup);
|
|
797
|
+
return ref;
|
|
798
|
+
}
|
|
799
|
+
repositionAll() {
|
|
800
|
+
Array.from(this.openDialogs.values()).forEach((instance) => {
|
|
801
|
+
if (instance.anchor && instance.element.isConnected) {
|
|
802
|
+
this.positionDialog(instance);
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
positionDialog(dialogInstance) {
|
|
807
|
+
const anchor = dialogInstance.anchor;
|
|
808
|
+
const dialog = dialogInstance.element;
|
|
809
|
+
const { placement, alignment, margin = 8 } = dialogInstance.options;
|
|
810
|
+
if (!anchor || !dialog)
|
|
811
|
+
return;
|
|
812
|
+
const anchorRect = anchor.getBoundingClientRect();
|
|
813
|
+
const dialogRect = dialog.getBoundingClientRect();
|
|
814
|
+
const vw = window.innerWidth;
|
|
815
|
+
const vh = window.innerHeight;
|
|
816
|
+
const isRTL = getComputedStyle(this.doc.documentElement).direction === 'rtl';
|
|
817
|
+
let top = 0;
|
|
818
|
+
let left = 'auto';
|
|
819
|
+
let right = 'auto';
|
|
820
|
+
const spaceBelow = vh - anchorRect.bottom;
|
|
821
|
+
const spaceAbove = anchorRect.top;
|
|
822
|
+
let verticalPos = 'above';
|
|
823
|
+
// vertical position
|
|
824
|
+
if (placement === 'bottom' || (placement === 'auto' && spaceBelow >= dialogRect.height + margin)) {
|
|
825
|
+
top = anchorRect.bottom + margin;
|
|
826
|
+
verticalPos = 'above';
|
|
827
|
+
if (top + dialogRect.height > vh - margin) {
|
|
828
|
+
top = vh - dialogRect.height - margin;
|
|
829
|
+
verticalPos = 'bottom';
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
else {
|
|
833
|
+
top = anchorRect.top - dialogRect.height - margin;
|
|
834
|
+
verticalPos = 'bottom';
|
|
835
|
+
if (top > spaceAbove) {
|
|
836
|
+
top = anchorRect.top + anchorRect.height + margin;
|
|
837
|
+
verticalPos = 'above';
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
// horizontal position
|
|
841
|
+
if (alignment === 'center') {
|
|
842
|
+
left = anchorRect.left + anchorRect.width / 2 - dialogRect.width / 2;
|
|
843
|
+
}
|
|
844
|
+
else if (alignment === 'start') {
|
|
845
|
+
if (isRTL) {
|
|
846
|
+
left = 'auto';
|
|
847
|
+
right = vw - anchorRect.right;
|
|
848
|
+
}
|
|
849
|
+
else {
|
|
850
|
+
left = anchorRect.left;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
else {
|
|
854
|
+
// end
|
|
855
|
+
left = isRTL ? anchorRect.left : anchorRect.right - dialogRect.width;
|
|
856
|
+
}
|
|
857
|
+
if (left != 'auto' && left < margin) {
|
|
858
|
+
left = margin;
|
|
859
|
+
}
|
|
860
|
+
if (left != 'auto' && left + dialogRect.width > vw - margin) {
|
|
861
|
+
left = vw - dialogRect.width - margin;
|
|
862
|
+
}
|
|
863
|
+
dialog.className = `tips-${verticalPos}`;
|
|
864
|
+
dialog.style.top = `${top}px`;
|
|
865
|
+
dialog.style.left = left != 'auto' ? `${left}px` : left;
|
|
866
|
+
dialog.style.right = right != 'auto' ? `${right}px` : right;
|
|
867
|
+
dialog.style.transform = 'none';
|
|
868
|
+
}
|
|
869
|
+
getLastDialog() {
|
|
870
|
+
if (this.openDialogs.size === 0)
|
|
871
|
+
return undefined;
|
|
872
|
+
const lastKey = Array.from(this.openDialogs.keys()).pop();
|
|
873
|
+
return lastKey ? this.openDialogs.get(lastKey) : undefined;
|
|
874
|
+
}
|
|
875
|
+
closeAll() {
|
|
876
|
+
const ids = Array.from(this.openDialogs.keys());
|
|
877
|
+
ids.forEach((id) => {
|
|
878
|
+
const instance = this.openDialogs.get(id);
|
|
879
|
+
instance?.cleanup();
|
|
880
|
+
});
|
|
881
|
+
}
|
|
882
|
+
handleClickOnBackdrop(event) {
|
|
883
|
+
const target = event.target;
|
|
884
|
+
const lastDialog = this.getLastDialog();
|
|
885
|
+
if (!lastDialog)
|
|
886
|
+
return;
|
|
887
|
+
// The click target _must_ be the dialog element itself, and not elements underneath or inside.
|
|
888
|
+
if (target !== lastDialog.element || !lastDialog.element?.open)
|
|
889
|
+
return;
|
|
890
|
+
// If the dialog contains a form, do not close the dialog when clicking outside of the dialog
|
|
891
|
+
if (lastDialog.element.querySelector('form'))
|
|
892
|
+
return;
|
|
893
|
+
const rect = lastDialog.element.getBoundingClientRect();
|
|
894
|
+
const clickWasInsideDialog = rect.top <= event.clientY &&
|
|
895
|
+
event.clientY <= rect.top + rect.height &&
|
|
896
|
+
rect.left <= event.clientX &&
|
|
897
|
+
event.clientX <= rect.left + rect.width;
|
|
898
|
+
if (!clickWasInsideDialog) {
|
|
899
|
+
lastDialog.cleanup();
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: DialogService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
903
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: DialogService, providedIn: 'root' });
|
|
904
|
+
}
|
|
905
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.4", ngImport: i0, type: DialogService, decorators: [{
|
|
906
|
+
type: Injectable,
|
|
907
|
+
args: [{ providedIn: 'root' }]
|
|
908
|
+
}], ctorParameters: () => [] });
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* Generated bundle index. Do not edit.
|
|
912
|
+
*/
|
|
913
|
+
|
|
914
|
+
export { BrowserService, DialogOverlayRef, DialogService, RangeSliderComponent, SaturationComponent, SliderComponent, ValueModel, getOffsetPosition };
|
|
915
|
+
//# sourceMappingURL=ngx-input-shared.mjs.map
|