UNPKG

23.7 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/animations'), require('@angular/common'), require('primeng/dom'), require('@angular/forms')) :
3 typeof define === 'function' && define.amd ? define('primeng/colorpicker', ['exports', '@angular/core', '@angular/animations', '@angular/common', 'primeng/dom', '@angular/forms'], factory) :
4 (global = global || self, factory((global.primeng = global.primeng || {}, global.primeng.colorpicker = {}), global.ng.core, global.ng.animations, global.ng.common, global.primeng.dom, global.ng.forms));
5}(this, (function (exports, core, animations, common, dom, forms) { 'use strict';
6
7 var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
8 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
10 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;
11 return c > 3 && r && Object.defineProperty(target, key, r), r;
12 };
13 var COLORPICKER_VALUE_ACCESSOR = {
14 provide: forms.NG_VALUE_ACCESSOR,
15 useExisting: core.forwardRef(function () { return ColorPicker; }),
16 multi: true
17 };
18 var ColorPicker = /** @class */ (function () {
19 function ColorPicker(el, renderer, cd) {
20 this.el = el;
21 this.renderer = renderer;
22 this.cd = cd;
23 this.format = 'hex';
24 this.autoZIndex = true;
25 this.baseZIndex = 0;
26 this.showTransitionOptions = '225ms ease-out';
27 this.hideTransitionOptions = '195ms ease-in';
28 this.onChange = new core.EventEmitter();
29 this.defaultColor = 'ff0000';
30 this.onModelChange = function () { };
31 this.onModelTouched = function () { };
32 }
33 Object.defineProperty(ColorPicker.prototype, "colorSelector", {
34 set: function (element) {
35 this.colorSelectorViewChild = element;
36 },
37 enumerable: true,
38 configurable: true
39 });
40 Object.defineProperty(ColorPicker.prototype, "colorHandle", {
41 set: function (element) {
42 this.colorHandleViewChild = element;
43 },
44 enumerable: true,
45 configurable: true
46 });
47 Object.defineProperty(ColorPicker.prototype, "hue", {
48 set: function (element) {
49 this.hueViewChild = element;
50 },
51 enumerable: true,
52 configurable: true
53 });
54 Object.defineProperty(ColorPicker.prototype, "hueHandle", {
55 set: function (element) {
56 this.hueHandleViewChild = element;
57 },
58 enumerable: true,
59 configurable: true
60 });
61 ColorPicker.prototype.onHueMousedown = function (event) {
62 if (this.disabled) {
63 return;
64 }
65 this.bindDocumentMousemoveListener();
66 this.bindDocumentMouseupListener();
67 this.hueDragging = true;
68 this.pickHue(event);
69 };
70 ColorPicker.prototype.pickHue = function (event) {
71 var top = this.hueViewChild.nativeElement.getBoundingClientRect().top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
72 this.value = this.validateHSB({
73 h: Math.floor(360 * (150 - Math.max(0, Math.min(150, (event.pageY - top)))) / 150),
74 s: this.value.s,
75 b: this.value.b
76 });
77 this.updateColorSelector();
78 this.updateUI();
79 this.updateModel();
80 this.onChange.emit({ originalEvent: event, value: this.getValueToUpdate() });
81 };
82 ColorPicker.prototype.onColorMousedown = function (event) {
83 if (this.disabled) {
84 return;
85 }
86 this.bindDocumentMousemoveListener();
87 this.bindDocumentMouseupListener();
88 this.colorDragging = true;
89 this.pickColor(event);
90 };
91 ColorPicker.prototype.pickColor = function (event) {
92 var rect = this.colorSelectorViewChild.nativeElement.getBoundingClientRect();
93 var top = rect.top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
94 var left = rect.left + document.body.scrollLeft;
95 var saturation = Math.floor(100 * (Math.max(0, Math.min(150, (event.pageX - left)))) / 150);
96 var brightness = Math.floor(100 * (150 - Math.max(0, Math.min(150, (event.pageY - top)))) / 150);
97 this.value = this.validateHSB({
98 h: this.value.h,
99 s: saturation,
100 b: brightness
101 });
102 this.updateUI();
103 this.updateModel();
104 this.onChange.emit({ originalEvent: event, value: this.getValueToUpdate() });
105 };
106 ColorPicker.prototype.getValueToUpdate = function () {
107 var val;
108 switch (this.format) {
109 case 'hex':
110 val = '#' + this.HSBtoHEX(this.value);
111 break;
112 case 'rgb':
113 val = this.HSBtoRGB(this.value);
114 break;
115 case 'hsb':
116 val = this.value;
117 break;
118 }
119 return val;
120 };
121 ColorPicker.prototype.updateModel = function () {
122 this.onModelChange(this.getValueToUpdate());
123 };
124 ColorPicker.prototype.writeValue = function (value) {
125 if (value) {
126 switch (this.format) {
127 case 'hex':
128 this.value = this.HEXtoHSB(value);
129 break;
130 case 'rgb':
131 this.value = this.RGBtoHSB(value);
132 break;
133 case 'hsb':
134 this.value = value;
135 break;
136 }
137 }
138 else {
139 this.value = this.HEXtoHSB(this.defaultColor);
140 }
141 this.updateColorSelector();
142 this.updateUI();
143 };
144 ColorPicker.prototype.updateColorSelector = function () {
145 if (this.colorSelectorViewChild) {
146 var hsb = {};
147 hsb.s = 100;
148 hsb.b = 100;
149 hsb.h = this.value.h;
150 this.colorSelectorViewChild.nativeElement.style.backgroundColor = '#' + this.HSBtoHEX(hsb);
151 }
152 };
153 ColorPicker.prototype.updateUI = function () {
154 if (this.colorHandleViewChild && this.hueHandleViewChild.nativeElement) {
155 this.colorHandleViewChild.nativeElement.style.left = Math.floor(150 * this.value.s / 100) + 'px';
156 this.colorHandleViewChild.nativeElement.style.top = Math.floor(150 * (100 - this.value.b) / 100) + 'px';
157 this.hueHandleViewChild.nativeElement.style.top = Math.floor(150 - (150 * this.value.h / 360)) + 'px';
158 }
159 this.inputBgColor = '#' + this.HSBtoHEX(this.value);
160 };
161 ColorPicker.prototype.onInputFocus = function () {
162 this.onModelTouched();
163 };
164 ColorPicker.prototype.show = function () {
165 this.overlayVisible = true;
166 };
167 ColorPicker.prototype.onOverlayAnimationStart = function (event) {
168 switch (event.toState) {
169 case 'visible':
170 if (!this.inline) {
171 this.overlay = event.element;
172 this.appendOverlay();
173 if (this.autoZIndex) {
174 this.overlay.style.zIndex = String(this.baseZIndex + (++dom.DomHandler.zindex));
175 }
176 this.alignOverlay();
177 this.bindDocumentClickListener();
178 this.updateColorSelector();
179 this.updateUI();
180 }
181 break;
182 case 'void':
183 this.onOverlayHide();
184 break;
185 }
186 };
187 ColorPicker.prototype.appendOverlay = function () {
188 if (this.appendTo) {
189 if (this.appendTo === 'body')
190 document.body.appendChild(this.overlay);
191 else
192 dom.DomHandler.appendChild(this.overlay, this.appendTo);
193 }
194 };
195 ColorPicker.prototype.restoreOverlayAppend = function () {
196 if (this.overlay && this.appendTo) {
197 this.el.nativeElement.appendChild(this.overlay);
198 }
199 };
200 ColorPicker.prototype.alignOverlay = function () {
201 if (this.appendTo)
202 dom.DomHandler.absolutePosition(this.overlay, this.inputViewChild.nativeElement);
203 else
204 dom.DomHandler.relativePosition(this.overlay, this.inputViewChild.nativeElement);
205 };
206 ColorPicker.prototype.hide = function () {
207 this.overlayVisible = false;
208 };
209 ColorPicker.prototype.onInputClick = function () {
210 this.selfClick = true;
211 this.togglePanel();
212 };
213 ColorPicker.prototype.togglePanel = function () {
214 if (!this.overlayVisible)
215 this.show();
216 else
217 this.hide();
218 };
219 ColorPicker.prototype.onInputKeydown = function (event) {
220 switch (event.which) {
221 //space
222 case 32:
223 this.togglePanel();
224 event.preventDefault();
225 break;
226 //escape and tab
227 case 27:
228 case 9:
229 this.hide();
230 break;
231 }
232 };
233 ColorPicker.prototype.onPanelClick = function () {
234 this.selfClick = true;
235 };
236 ColorPicker.prototype.registerOnChange = function (fn) {
237 this.onModelChange = fn;
238 };
239 ColorPicker.prototype.registerOnTouched = function (fn) {
240 this.onModelTouched = fn;
241 };
242 ColorPicker.prototype.setDisabledState = function (val) {
243 this.disabled = val;
244 };
245 ColorPicker.prototype.bindDocumentClickListener = function () {
246 var _this = this;
247 if (!this.documentClickListener) {
248 this.documentClickListener = this.renderer.listen('document', 'click', function () {
249 if (!_this.selfClick) {
250 _this.overlayVisible = false;
251 _this.unbindDocumentClickListener();
252 }
253 _this.selfClick = false;
254 _this.cd.markForCheck();
255 });
256 }
257 };
258 ColorPicker.prototype.unbindDocumentClickListener = function () {
259 if (this.documentClickListener) {
260 this.documentClickListener();
261 this.documentClickListener = null;
262 }
263 };
264 ColorPicker.prototype.bindDocumentMousemoveListener = function () {
265 var _this = this;
266 if (!this.documentMousemoveListener) {
267 this.documentMousemoveListener = this.renderer.listen('document', 'mousemove', function (event) {
268 if (_this.colorDragging) {
269 _this.pickColor(event);
270 }
271 if (_this.hueDragging) {
272 _this.pickHue(event);
273 }
274 });
275 }
276 };
277 ColorPicker.prototype.unbindDocumentMousemoveListener = function () {
278 if (this.documentMousemoveListener) {
279 this.documentMousemoveListener();
280 this.documentMousemoveListener = null;
281 }
282 };
283 ColorPicker.prototype.bindDocumentMouseupListener = function () {
284 var _this = this;
285 if (!this.documentMouseupListener) {
286 this.documentMouseupListener = this.renderer.listen('document', 'mouseup', function () {
287 _this.colorDragging = false;
288 _this.hueDragging = false;
289 _this.unbindDocumentMousemoveListener();
290 _this.unbindDocumentMouseupListener();
291 });
292 }
293 };
294 ColorPicker.prototype.unbindDocumentMouseupListener = function () {
295 if (this.documentMouseupListener) {
296 this.documentMouseupListener();
297 this.documentMouseupListener = null;
298 }
299 };
300 ColorPicker.prototype.validateHSB = function (hsb) {
301 return {
302 h: Math.min(360, Math.max(0, hsb.h)),
303 s: Math.min(100, Math.max(0, hsb.s)),
304 b: Math.min(100, Math.max(0, hsb.b))
305 };
306 };
307 ColorPicker.prototype.validateRGB = function (rgb) {
308 return {
309 r: Math.min(255, Math.max(0, rgb.r)),
310 g: Math.min(255, Math.max(0, rgb.g)),
311 b: Math.min(255, Math.max(0, rgb.b))
312 };
313 };
314 ColorPicker.prototype.validateHEX = function (hex) {
315 var len = 6 - hex.length;
316 if (len > 0) {
317 var o = [];
318 for (var i = 0; i < len; i++) {
319 o.push('0');
320 }
321 o.push(hex);
322 hex = o.join('');
323 }
324 return hex;
325 };
326 ColorPicker.prototype.HEXtoRGB = function (hex) {
327 var hexValue = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16);
328 return { r: hexValue >> 16, g: (hexValue & 0x00FF00) >> 8, b: (hexValue & 0x0000FF) };
329 };
330 ColorPicker.prototype.HEXtoHSB = function (hex) {
331 return this.RGBtoHSB(this.HEXtoRGB(hex));
332 };
333 ColorPicker.prototype.RGBtoHSB = function (rgb) {
334 var hsb = {
335 h: 0,
336 s: 0,
337 b: 0
338 };
339 var min = Math.min(rgb.r, rgb.g, rgb.b);
340 var max = Math.max(rgb.r, rgb.g, rgb.b);
341 var delta = max - min;
342 hsb.b = max;
343 hsb.s = max != 0 ? 255 * delta / max : 0;
344 if (hsb.s != 0) {
345 if (rgb.r == max) {
346 hsb.h = (rgb.g - rgb.b) / delta;
347 }
348 else if (rgb.g == max) {
349 hsb.h = 2 + (rgb.b - rgb.r) / delta;
350 }
351 else {
352 hsb.h = 4 + (rgb.r - rgb.g) / delta;
353 }
354 }
355 else {
356 hsb.h = -1;
357 }
358 hsb.h *= 60;
359 if (hsb.h < 0) {
360 hsb.h += 360;
361 }
362 hsb.s *= 100 / 255;
363 hsb.b *= 100 / 255;
364 return hsb;
365 };
366 ColorPicker.prototype.HSBtoRGB = function (hsb) {
367 var rgb = {
368 r: null, g: null, b: null
369 };
370 var h = hsb.h;
371 var s = hsb.s * 255 / 100;
372 var v = hsb.b * 255 / 100;
373 if (s == 0) {
374 rgb = {
375 r: v,
376 g: v,
377 b: v
378 };
379 }
380 else {
381 var t1 = v;
382 var t2 = (255 - s) * v / 255;
383 var t3 = (t1 - t2) * (h % 60) / 60;
384 if (h == 360)
385 h = 0;
386 if (h < 60) {
387 rgb.r = t1;
388 rgb.b = t2;
389 rgb.g = t2 + t3;
390 }
391 else if (h < 120) {
392 rgb.g = t1;
393 rgb.b = t2;
394 rgb.r = t1 - t3;
395 }
396 else if (h < 180) {
397 rgb.g = t1;
398 rgb.r = t2;
399 rgb.b = t2 + t3;
400 }
401 else if (h < 240) {
402 rgb.b = t1;
403 rgb.r = t2;
404 rgb.g = t1 - t3;
405 }
406 else if (h < 300) {
407 rgb.b = t1;
408 rgb.g = t2;
409 rgb.r = t2 + t3;
410 }
411 else if (h < 360) {
412 rgb.r = t1;
413 rgb.g = t2;
414 rgb.b = t1 - t3;
415 }
416 else {
417 rgb.r = 0;
418 rgb.g = 0;
419 rgb.b = 0;
420 }
421 }
422 return { r: Math.round(rgb.r), g: Math.round(rgb.g), b: Math.round(rgb.b) };
423 };
424 ColorPicker.prototype.RGBtoHEX = function (rgb) {
425 var hex = [
426 rgb.r.toString(16),
427 rgb.g.toString(16),
428 rgb.b.toString(16)
429 ];
430 for (var key in hex) {
431 if (hex[key].length == 1) {
432 hex[key] = '0' + hex[key];
433 }
434 }
435 return hex.join('');
436 };
437 ColorPicker.prototype.HSBtoHEX = function (hsb) {
438 return this.RGBtoHEX(this.HSBtoRGB(hsb));
439 };
440 ColorPicker.prototype.onOverlayHide = function () {
441 this.unbindDocumentClickListener();
442 this.overlay = null;
443 };
444 ColorPicker.prototype.ngOnDestroy = function () {
445 this.restoreOverlayAppend();
446 this.onOverlayHide();
447 };
448 ColorPicker.ctorParameters = function () { return [
449 { type: core.ElementRef },
450 { type: core.Renderer2 },
451 { type: core.ChangeDetectorRef }
452 ]; };
453 __decorate([
454 core.Input()
455 ], ColorPicker.prototype, "style", void 0);
456 __decorate([
457 core.Input()
458 ], ColorPicker.prototype, "styleClass", void 0);
459 __decorate([
460 core.Input()
461 ], ColorPicker.prototype, "inline", void 0);
462 __decorate([
463 core.Input()
464 ], ColorPicker.prototype, "format", void 0);
465 __decorate([
466 core.Input()
467 ], ColorPicker.prototype, "appendTo", void 0);
468 __decorate([
469 core.Input()
470 ], ColorPicker.prototype, "disabled", void 0);
471 __decorate([
472 core.Input()
473 ], ColorPicker.prototype, "tabindex", void 0);
474 __decorate([
475 core.Input()
476 ], ColorPicker.prototype, "inputId", void 0);
477 __decorate([
478 core.Input()
479 ], ColorPicker.prototype, "autoZIndex", void 0);
480 __decorate([
481 core.Input()
482 ], ColorPicker.prototype, "baseZIndex", void 0);
483 __decorate([
484 core.Input()
485 ], ColorPicker.prototype, "showTransitionOptions", void 0);
486 __decorate([
487 core.Input()
488 ], ColorPicker.prototype, "hideTransitionOptions", void 0);
489 __decorate([
490 core.Output()
491 ], ColorPicker.prototype, "onChange", void 0);
492 __decorate([
493 core.ViewChild('input')
494 ], ColorPicker.prototype, "inputViewChild", void 0);
495 __decorate([
496 core.ViewChild('colorSelector')
497 ], ColorPicker.prototype, "colorSelector", null);
498 __decorate([
499 core.ViewChild('colorHandle')
500 ], ColorPicker.prototype, "colorHandle", null);
501 __decorate([
502 core.ViewChild('hue')
503 ], ColorPicker.prototype, "hue", null);
504 __decorate([
505 core.ViewChild('hueHandle')
506 ], ColorPicker.prototype, "hueHandle", null);
507 ColorPicker = __decorate([
508 core.Component({
509 selector: 'p-colorPicker',
510 template: "\n <div [ngStyle]=\"style\" [class]=\"styleClass\" [ngClass]=\"{'ui-colorpicker ui-widget':true,'ui-colorpicker-overlay':!inline,'ui-colorpicker-dragging':colorDragging||hueDragging}\">\n <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}\"\n (focus)=\"onInputFocus()\" (click)=\"onInputClick()\" (keydown)=\"onInputKeydown($event)\" [attr.id]=\"inputId\" [attr.tabindex]=\"tabindex\" [disabled]=\"disabled\"\n [style.backgroundColor]=\"inputBgColor\">\n <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()\"\n [@overlayAnimation]=\"{value: 'visible', params: {showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}\" [@.disabled]=\"inline === true\" (@overlayAnimation.start)=\"onOverlayAnimationStart($event)\">\n <div class=\"ui-colorpicker-content\">\n <div #colorSelector class=\"ui-colorpicker-color-selector\" (mousedown)=\"onColorMousedown($event)\">\n <div class=\"ui-colorpicker-color\">\n <div #colorHandle class=\"ui-colorpicker-color-handle\"></div>\n </div>\n </div>\n <div #hue class=\"ui-colorpicker-hue\" (mousedown)=\"onHueMousedown($event)\">\n <div #hueHandle class=\"ui-colorpicker-hue-handle\"></div>\n </div>\n </div>\n </div>\n </div>\n ",
511 animations: [
512 animations.trigger('overlayAnimation', [
513 animations.state('void', animations.style({
514 transform: 'translateY(5%)',
515 opacity: 0
516 })),
517 animations.state('visible', animations.style({
518 transform: 'translateY(0)',
519 opacity: 1
520 })),
521 animations.transition('void => visible', animations.animate('{{showTransitionParams}}')),
522 animations.transition('visible => void', animations.animate('{{hideTransitionParams}}'))
523 ])
524 ],
525 providers: [COLORPICKER_VALUE_ACCESSOR],
526 changeDetection: core.ChangeDetectionStrategy.Default
527 })
528 ], ColorPicker);
529 return ColorPicker;
530 }());
531 var ColorPickerModule = /** @class */ (function () {
532 function ColorPickerModule() {
533 }
534 ColorPickerModule = __decorate([
535 core.NgModule({
536 imports: [common.CommonModule],
537 exports: [ColorPicker],
538 declarations: [ColorPicker]
539 })
540 ], ColorPickerModule);
541 return ColorPickerModule;
542 }());
543
544 exports.COLORPICKER_VALUE_ACCESSOR = COLORPICKER_VALUE_ACCESSOR;
545 exports.ColorPicker = ColorPicker;
546 exports.ColorPickerModule = ColorPickerModule;
547
548 Object.defineProperty(exports, '__esModule', { value: true });
549
550})));
551//# sourceMappingURL=primeng-colorpicker.umd.js.map