primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 18.1 kB
JavaScript
import { forwardRef, EventEmitter, ElementRef, Renderer2, ChangeDetectorRef, Input, Output, ViewChild, Component, ChangeDetectionStrategy, NgModule } from '@angular/core';
import { trigger, state, style, transition, animate } from '@angular/animations';
import { CommonModule } from '@angular/common';
import { DomHandler } from 'primeng/dom';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const COLORPICKER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ColorPicker),
multi: true
};
let ColorPicker = class ColorPicker {
constructor(el, renderer, cd) {
this.el = el;
this.renderer = renderer;
this.cd = cd;
this.format = 'hex';
this.autoZIndex = true;
this.baseZIndex = 0;
this.showTransitionOptions = '225ms ease-out';
this.hideTransitionOptions = '195ms ease-in';
this.onChange = new EventEmitter();
this.defaultColor = 'ff0000';
this.onModelChange = () => { };
this.onModelTouched = () => { };
}
set colorSelector(element) {
this.colorSelectorViewChild = element;
}
set colorHandle(element) {
this.colorHandleViewChild = element;
}
set hue(element) {
this.hueViewChild = element;
}
set hueHandle(element) {
this.hueHandleViewChild = element;
}
onHueMousedown(event) {
if (this.disabled) {
return;
}
this.bindDocumentMousemoveListener();
this.bindDocumentMouseupListener();
this.hueDragging = true;
this.pickHue(event);
}
pickHue(event) {
let top = this.hueViewChild.nativeElement.getBoundingClientRect().top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
this.value = this.validateHSB({
h: Math.floor(360 * (150 - Math.max(0, Math.min(150, (event.pageY - top)))) / 150),
s: this.value.s,
b: this.value.b
});
this.updateColorSelector();
this.updateUI();
this.updateModel();
this.onChange.emit({ originalEvent: event, value: this.getValueToUpdate() });
}
onColorMousedown(event) {
if (this.disabled) {
return;
}
this.bindDocumentMousemoveListener();
this.bindDocumentMouseupListener();
this.colorDragging = true;
this.pickColor(event);
}
pickColor(event) {
let rect = this.colorSelectorViewChild.nativeElement.getBoundingClientRect();
let top = rect.top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
let left = rect.left + document.body.scrollLeft;
let saturation = Math.floor(100 * (Math.max(0, Math.min(150, (event.pageX - left)))) / 150);
let brightness = Math.floor(100 * (150 - Math.max(0, Math.min(150, (event.pageY - top)))) / 150);
this.value = this.validateHSB({
h: this.value.h,
s: saturation,
b: brightness
});
this.updateUI();
this.updateModel();
this.onChange.emit({ originalEvent: event, value: this.getValueToUpdate() });
}
getValueToUpdate() {
let val;
switch (this.format) {
case 'hex':
val = '#' + this.HSBtoHEX(this.value);
break;
case 'rgb':
val = this.HSBtoRGB(this.value);
break;
case 'hsb':
val = this.value;
break;
}
return val;
}
updateModel() {
this.onModelChange(this.getValueToUpdate());
}
writeValue(value) {
if (value) {
switch (this.format) {
case 'hex':
this.value = this.HEXtoHSB(value);
break;
case 'rgb':
this.value = this.RGBtoHSB(value);
break;
case 'hsb':
this.value = value;
break;
}
}
else {
this.value = this.HEXtoHSB(this.defaultColor);
}
this.updateColorSelector();
this.updateUI();
}
updateColorSelector() {
if (this.colorSelectorViewChild) {
const hsb = {};
hsb.s = 100;
hsb.b = 100;
hsb.h = this.value.h;
this.colorSelectorViewChild.nativeElement.style.backgroundColor = '#' + this.HSBtoHEX(hsb);
}
}
updateUI() {
if (this.colorHandleViewChild && this.hueHandleViewChild.nativeElement) {
this.colorHandleViewChild.nativeElement.style.left = Math.floor(150 * this.value.s / 100) + 'px';
this.colorHandleViewChild.nativeElement.style.top = Math.floor(150 * (100 - this.value.b) / 100) + 'px';
this.hueHandleViewChild.nativeElement.style.top = Math.floor(150 - (150 * this.value.h / 360)) + 'px';
}
this.inputBgColor = '#' + this.HSBtoHEX(this.value);
}
onInputFocus() {
this.onModelTouched();
}
show() {
this.overlayVisible = true;
}
onOverlayAnimationStart(event) {
switch (event.toState) {
case 'visible':
if (!this.inline) {
this.overlay = event.element;
this.appendOverlay();
if (this.autoZIndex) {
this.overlay.style.zIndex = String(this.baseZIndex + (++DomHandler.zindex));
}
this.alignOverlay();
this.bindDocumentClickListener();
this.updateColorSelector();
this.updateUI();
}
break;
case 'void':
this.onOverlayHide();
break;
}
}
appendOverlay() {
if (this.appendTo) {
if (this.appendTo === 'body')
document.body.appendChild(this.overlay);
else
DomHandler.appendChild(this.overlay, this.appendTo);
}
}
restoreOverlayAppend() {
if (this.overlay && this.appendTo) {
this.el.nativeElement.appendChild(this.overlay);
}
}
alignOverlay() {
if (this.appendTo)
DomHandler.absolutePosition(this.overlay, this.inputViewChild.nativeElement);
else
DomHandler.relativePosition(this.overlay, this.inputViewChild.nativeElement);
}
hide() {
this.overlayVisible = false;
}
onInputClick() {
this.selfClick = true;
this.togglePanel();
}
togglePanel() {
if (!this.overlayVisible)
this.show();
else
this.hide();
}
onInputKeydown(event) {
switch (event.which) {
//space
case 32:
this.togglePanel();
event.preventDefault();
break;
//escape and tab
case 27:
case 9:
this.hide();
break;
}
}
onPanelClick() {
this.selfClick = true;
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
this.disabled = val;
}
bindDocumentClickListener() {
if (!this.documentClickListener) {
this.documentClickListener = this.renderer.listen('document', 'click', () => {
if (!this.selfClick) {
this.overlayVisible = false;
this.unbindDocumentClickListener();
}
this.selfClick = false;
this.cd.markForCheck();
});
}
}
unbindDocumentClickListener() {
if (this.documentClickListener) {
this.documentClickListener();
this.documentClickListener = null;
}
}
bindDocumentMousemoveListener() {
if (!this.documentMousemoveListener) {
this.documentMousemoveListener = this.renderer.listen('document', 'mousemove', (event) => {
if (this.colorDragging) {
this.pickColor(event);
}
if (this.hueDragging) {
this.pickHue(event);
}
});
}
}
unbindDocumentMousemoveListener() {
if (this.documentMousemoveListener) {
this.documentMousemoveListener();
this.documentMousemoveListener = null;
}
}
bindDocumentMouseupListener() {
if (!this.documentMouseupListener) {
this.documentMouseupListener = this.renderer.listen('document', 'mouseup', () => {
this.colorDragging = false;
this.hueDragging = false;
this.unbindDocumentMousemoveListener();
this.unbindDocumentMouseupListener();
});
}
}
unbindDocumentMouseupListener() {
if (this.documentMouseupListener) {
this.documentMouseupListener();
this.documentMouseupListener = null;
}
}
validateHSB(hsb) {
return {
h: Math.min(360, Math.max(0, hsb.h)),
s: Math.min(100, Math.max(0, hsb.s)),
b: Math.min(100, Math.max(0, hsb.b))
};
}
validateRGB(rgb) {
return {
r: Math.min(255, Math.max(0, rgb.r)),
g: Math.min(255, Math.max(0, rgb.g)),
b: Math.min(255, Math.max(0, rgb.b))
};
}
validateHEX(hex) {
var len = 6 - hex.length;
if (len > 0) {
var o = [];
for (var i = 0; i < len; i++) {
o.push('0');
}
o.push(hex);
hex = o.join('');
}
return hex;
}
HEXtoRGB(hex) {
let hexValue = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16);
return { r: hexValue >> 16, g: (hexValue & 0x00FF00) >> 8, b: (hexValue & 0x0000FF) };
}
HEXtoHSB(hex) {
return this.RGBtoHSB(this.HEXtoRGB(hex));
}
RGBtoHSB(rgb) {
var hsb = {
h: 0,
s: 0,
b: 0
};
var min = Math.min(rgb.r, rgb.g, rgb.b);
var max = Math.max(rgb.r, rgb.g, rgb.b);
var delta = max - min;
hsb.b = max;
hsb.s = max != 0 ? 255 * delta / max : 0;
if (hsb.s != 0) {
if (rgb.r == max) {
hsb.h = (rgb.g - rgb.b) / delta;
}
else if (rgb.g == max) {
hsb.h = 2 + (rgb.b - rgb.r) / delta;
}
else {
hsb.h = 4 + (rgb.r - rgb.g) / delta;
}
}
else {
hsb.h = -1;
}
hsb.h *= 60;
if (hsb.h < 0) {
hsb.h += 360;
}
hsb.s *= 100 / 255;
hsb.b *= 100 / 255;
return hsb;
}
HSBtoRGB(hsb) {
var rgb = {
r: null, g: null, b: null
};
let h = hsb.h;
let s = hsb.s * 255 / 100;
let v = hsb.b * 255 / 100;
if (s == 0) {
rgb = {
r: v,
g: v,
b: v
};
}
else {
let t1 = v;
let t2 = (255 - s) * v / 255;
let t3 = (t1 - t2) * (h % 60) / 60;
if (h == 360)
h = 0;
if (h < 60) {
rgb.r = t1;
rgb.b = t2;
rgb.g = t2 + t3;
}
else if (h < 120) {
rgb.g = t1;
rgb.b = t2;
rgb.r = t1 - t3;
}
else if (h < 180) {
rgb.g = t1;
rgb.r = t2;
rgb.b = t2 + t3;
}
else if (h < 240) {
rgb.b = t1;
rgb.r = t2;
rgb.g = t1 - t3;
}
else if (h < 300) {
rgb.b = t1;
rgb.g = t2;
rgb.r = t2 + t3;
}
else if (h < 360) {
rgb.r = t1;
rgb.g = t2;
rgb.b = t1 - t3;
}
else {
rgb.r = 0;
rgb.g = 0;
rgb.b = 0;
}
}
return { r: Math.round(rgb.r), g: Math.round(rgb.g), b: Math.round(rgb.b) };
}
RGBtoHEX(rgb) {
var hex = [
rgb.r.toString(16),
rgb.g.toString(16),
rgb.b.toString(16)
];
for (var key in hex) {
if (hex[key].length == 1) {
hex[key] = '0' + hex[key];
}
}
return hex.join('');
}
HSBtoHEX(hsb) {
return this.RGBtoHEX(this.HSBtoRGB(hsb));
}
onOverlayHide() {
this.unbindDocumentClickListener();
this.overlay = null;
}
ngOnDestroy() {
this.restoreOverlayAppend();
this.onOverlayHide();
}
};
ColorPicker.ctorParameters = () => [
{ type: ElementRef },
{ type: Renderer2 },
{ type: ChangeDetectorRef }
];
__decorate([
Input()
], ColorPicker.prototype, "style", void 0);
__decorate([
Input()
], ColorPicker.prototype, "styleClass", void 0);
__decorate([
Input()
], ColorPicker.prototype, "inline", void 0);
__decorate([
Input()
], ColorPicker.prototype, "format", void 0);
__decorate([
Input()
], ColorPicker.prototype, "appendTo", void 0);
__decorate([
Input()
], ColorPicker.prototype, "disabled", void 0);
__decorate([
Input()
], ColorPicker.prototype, "tabindex", void 0);
__decorate([
Input()
], ColorPicker.prototype, "inputId", void 0);
__decorate([
Input()
], ColorPicker.prototype, "autoZIndex", void 0);
__decorate([
Input()
], ColorPicker.prototype, "baseZIndex", void 0);
__decorate([
Input()
], ColorPicker.prototype, "showTransitionOptions", void 0);
__decorate([
Input()
], ColorPicker.prototype, "hideTransitionOptions", void 0);
__decorate([
Output()
], ColorPicker.prototype, "onChange", void 0);
__decorate([
ViewChild('input')
], ColorPicker.prototype, "inputViewChild", void 0);
__decorate([
ViewChild('colorSelector')
], ColorPicker.prototype, "colorSelector", null);
__decorate([
ViewChild('colorHandle')
], ColorPicker.prototype, "colorHandle", null);
__decorate([
ViewChild('hue')
], ColorPicker.prototype, "hue", null);
__decorate([
ViewChild('hueHandle')
], ColorPicker.prototype, "hueHandle", null);
ColorPicker = __decorate([
Component({
selector: 'p-colorPicker',
template: `
<div [ngStyle]="style" [class]="styleClass" [ngClass]="{'ui-colorpicker ui-widget':true,'ui-colorpicker-overlay':!inline,'ui-colorpicker-dragging':colorDragging||hueDragging}">
<input #input type="text" *ngIf="!inline" class="ui-colorpicker-preview ui-inputtext ui-state-default ui-corner-all" readonly="readonly" [ngClass]="{'ui-state-disabled': disabled}"
(focus)="onInputFocus()" (click)="onInputClick()" (keydown)="onInputKeydown($event)" [attr.id]="inputId" [attr.tabindex]="tabindex" [disabled]="disabled"
[style.backgroundColor]="inputBgColor">
<div *ngIf="inline || overlayVisible" [ngClass]="{'ui-colorpicker-panel ui-corner-all': true, 'ui-colorpicker-overlay-panel ui-shadow':!inline, 'ui-state-disabled': disabled}" (click)="onPanelClick()"
[]="{value: 'visible', params: {showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}" [@.disabled]="inline === true" (.start)="onOverlayAnimationStart($event)">
<div class="ui-colorpicker-content">
<div #colorSelector class="ui-colorpicker-color-selector" (mousedown)="onColorMousedown($event)">
<div class="ui-colorpicker-color">
<div #colorHandle class="ui-colorpicker-color-handle"></div>
</div>
</div>
<div #hue class="ui-colorpicker-hue" (mousedown)="onHueMousedown($event)">
<div #hueHandle class="ui-colorpicker-hue-handle"></div>
</div>
</div>
</div>
</div>
`,
animations: [
trigger('overlayAnimation', [
state('void', style({
transform: 'translateY(5%)',
opacity: 0
})),
state('visible', style({
transform: 'translateY(0)',
opacity: 1
})),
transition('void => visible', animate('{{showTransitionParams}}')),
transition('visible => void', animate('{{hideTransitionParams}}'))
])
],
providers: [COLORPICKER_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.Default
})
], ColorPicker);
let ColorPickerModule = class ColorPickerModule {
};
ColorPickerModule = __decorate([
NgModule({
imports: [CommonModule],
exports: [ColorPicker],
declarations: [ColorPicker]
})
], ColorPickerModule);
/**
* Generated bundle index. Do not edit.
*/
export { COLORPICKER_VALUE_ACCESSOR, ColorPicker, ColorPickerModule };
//# sourceMappingURL=primeng-colorpicker.js.map