Skip to content

Commit

Permalink
#56
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed May 27, 2020
1 parent 3db1625 commit bb7051a
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 68 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ This project exists thanks to all the people who contribute.
Release Notes
-------------

### 2.1.0

- Added ability to clear the hook storage object if it’s empty.
- Added `blur` and `focus` hook that removed `state.e` option.

### 2.0.3

- Added `CP.state` property to set initial state globally.
Expand Down
6 changes: 5 additions & 1 deletion color-picker.button.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
<script src="color-picker.min.js"></script>
<script>

var picker = new CP(document.querySelector('input'), false),
var picker = new CP(document.querySelector('input')),
button = document.querySelector('button'),
text = button.textContent;

// Disable the default blur and focus behavior
picker.on('blur', function() {});
picker.on('focus', function() {});

picker.on('change', function(r, g, b, a) {
this.source.value = this.color(r, g, b, a);
});
Expand Down
3 changes: 3 additions & 0 deletions color-picker.close.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

var picker = new CP(document.querySelector('input'));

// Disable the default blur behavior
picker.on('blur', function() {});

picker.on('change', function(r, g, b, a) {
this.source.value = this.color(r, g, b, a);
});
Expand Down
25 changes: 0 additions & 25 deletions color-picker.e.html

This file was deleted.

36 changes: 36 additions & 0 deletions color-picker.events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html dir="ltr">
<head>
<meta charset="utf-8">
<meta content="width=device-width" name="viewport">
<title>Color Picker</title>
<link href="color-picker.min.css" rel="stylesheet">
</head>
<body>
<p>Show and hide color picker panel with double click.</p>
<p><input type="text"></p>
<script src="color-picker.min.js"></script>
<script>

var picker = new CP(document.querySelector('input'));

// Disable the default blur and focus behavior
picker.on('blur', function() {});
picker.on('focus', function() {});

picker.on('change', function(r, g, b, a) {
this.source.value = this.color(r, g, b, a);
});

picker.source.addEventListener('dblclick', function(e) {
picker.enter();
e.stopPropagation();
}, false);

document.documentElement.addEventListener('dblclick', function() {
picker.exit();
}, false);

</script>
</body>
</html>
45 changes: 24 additions & 21 deletions color-picker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* ==============================================================
* COLOR PICKER 2.0.3
* COLOR PICKER 2.1.0
* ==============================================================
* Author: Taufik Nurrohman <https://github.com/taufik-nurrohman>
* License: MIT
Expand Down Expand Up @@ -185,12 +185,11 @@

(function($$) {

$$.version = '2.0.3';
$$.version = '2.1.0';

$$.state = {
'class': 'color-picker',
'color': HEX,
'e': downEvents,
'parent': null
};

Expand Down Expand Up @@ -230,8 +229,8 @@
$$ = win[name],
hooks = {},
self = doc.createElement('div'),
state = Object.assign({}, $$.state, false === o || o instanceof Array ? {
'e': o
state = Object.assign({}, $$.state, isString(o) ? {
'color': o
} : (o || {})),
cn = state['class'];

Expand Down Expand Up @@ -291,6 +290,10 @@
hooks[name].splice(i, 1);
}
}
// Clean-up empty hook(s)
if (0 === j) {
delete hooks[name];
}
} else {
delete hooks[name];
}
Expand Down Expand Up @@ -328,7 +331,6 @@
body = doc.body,
color = value(),
data = RGB2HSV(color),
events = state.e,
C = self.firstChild,
SV = C[children][0],
H = C[children][1],
Expand Down Expand Up @@ -359,20 +361,23 @@
}

function doClick(e) {
var t = e.target,
isSource = source === closestGet(t, source);
if (isSource) {
!isVisible() && doEnter(state.parent);
if (hooks.focus) {
hookFire('focus', color);
} else {
doExit();
var t = e.target,
isSource = source === closestGet(t, source);
if (isSource) {
!isVisible() && doEnter(state.parent);
} else {
doExit();
}
}
}

function doApply(isFirst, to) {

// Refresh value
data = RGB2HSV(color = value());
events = state.e;

if (!isFirst) {
(to || state.parent || body).appendChild(self), ($.visible = true);
Expand Down Expand Up @@ -442,9 +447,7 @@
ACursorSizeHeight = sizeGet(ACursor)[1];

if (isFirst) {
if (false !== events) {
eventsSet(source, events, doClick);
}
eventsSet(source, downEvents, doClick);
delay(function() {
hookFire('change', color);
}, 1);
Expand Down Expand Up @@ -477,9 +480,11 @@
isSelf = self === closestGet(t, self);
$.current = null;
if (!isSource && !isSelf) {
// Click outside the source or picker element to exit
if (isVisible() && false !== events) {
doExit();
if (hooks.blur) {
hookFire('blur', color);
} else {
// Click outside the source or picker element to exit
isVisible() && doExit();
}
} else {
if (isSelf) {
Expand Down Expand Up @@ -575,9 +580,7 @@
return $; // Already ejected
}
delete source[name];
if (false !== events) {
eventsLet(source, events, doClick);
}
eventsLet(source, downEvents, doClick);
return doExit(), hookFire('pop', color);
};

Expand Down
4 changes: 2 additions & 2 deletions color-picker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bb7051a

Please sign in to comment.