Skip to content

Commit

Permalink
Propagate lostFocus event to the top level picker so it can be subscr…
Browse files Browse the repository at this point in the history
…ibed to from the outside
  • Loading branch information
miroshko committed Jul 28, 2016
1 parent 824f549 commit 1423b2d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
18 changes: 9 additions & 9 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1>Color picker on any element</h1>
<section>
<pre>
var picker = vanillaColorPicker(document.querySelector('div.just-picker'));

picker.on('colorChosen', function(color, targetElem) {
targetElem.style.backgroundColor = color;
});
Expand All @@ -42,7 +42,7 @@ <h1>Color picker on any element</h1>
<script type="text/javascript">
window.addEventListener('load', function() {
var picker = vanillaColorPicker(document.querySelector('div.just-picker'));

picker.on('colorChosen', function(color, targetElem) {
targetElem.style.backgroundColor = color;
});
Expand Down Expand Up @@ -72,7 +72,7 @@ <h1>Appearance customisation with CSS</h1>
<section>
<pre>
var picker = vanillaColorPicker(document.querySelector('div.just-picker3')
picker.set('className', 'my-picker');
picker.set('className', 'my-picker');

<b>CSS:</b>
.my-picker {
Expand All @@ -85,7 +85,7 @@ <h1>Appearance customisation with CSS</h1>
<script type="text/javascript">
window.addEventListener('load', function() {
var picker = vanillaColorPicker(document.querySelector('div.just-picker3'));
picker.set('className', 'my-picker');
picker.set('className', 'my-picker');
});
</script>
</section>
Expand All @@ -102,7 +102,7 @@ <h1>Events</h1>
picker.elem.value = 'No color';
}
});
</pre>
</pre>
<input class="just-picker4">
<script type="text/javascript">
window.addEventListener('load', function() {
Expand All @@ -116,7 +116,7 @@ <h1>Events</h1>
}
});
});
</script>
</script>
</section>

<h1>Default color</h1>
Expand All @@ -125,20 +125,20 @@ <h1>Default color</h1>
var picker = vanillaColorPicker(document.querySelector('input.just-picker5'));
picker.set('customColors', ['#f00','#f22','#f44','#f88','#faa','#fcc','#fee');
picker.set('defaultColor', '#f44');
</pre>
</pre>
<input class="just-picker5">
<script type="text/javascript">
window.addEventListener('load', function() {
var picker = vanillaColorPicker(document.querySelector('input.just-picker5'));
picker.set('customColors', ['#f00','#f22','#f44','#f88','#faa','#fcc','#fee']);
picker.set('defaultColor', '#f44');
});
</script>
</script>
</section>

<br />
<br />

<script src="../src/vanilla-color-picker.js"></script>
<script src="../dist/vanilla-color-picker.js"></script>
</body>
</html>
9 changes: 5 additions & 4 deletions dist/vanilla-color-picker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ============================================================
//
// Vanilla Color Picker v 0.1.7
// Vanilla Color Picker v 0.1.8
//
// http://github.com/miroshko/vanilla-color-picker
//
Expand Down Expand Up @@ -32,7 +32,7 @@
} else {
global.document.head.appendChild(style);
}

}

function MessageMediator() {
Expand Down Expand Up @@ -117,15 +117,15 @@
var _this = this;
this.elem.addEventListener('click', function(e) {
if (e.target.classList.contains('vanilla-color-picker-single-color')) {
_this.emit('colorChosen', e.target.dataset.color);
_this.emit('colorChosen', e.target.dataset.color);
}
});
this.elem.addEventListener('keydown', function(e) {
var ENTER = 13;
var ESC = 27;
var keyCode = e.which || e.keyCode;
if (keyCode == ENTER) {
_this.emit('colorChosen', e.target.dataset.color);
_this.emit('colorChosen', e.target.dataset.color);
}
if(keyCode == ESC) {
_this.emit('lostFocus');
Expand Down Expand Up @@ -196,6 +196,7 @@
this_.emit('colorChosen', color, this_.elem);
});
this_.currentPicker.on('lostFocus', function() {
this_.emit('lostFocus');
this_.destroyPicker();
});
this_.emit('pickerCreated');
Expand Down
16 changes: 8 additions & 8 deletions dist/vanilla-color-picker.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vanilla-color-picker",
"version": "0.1.4",
"version": "0.1.8",
"description": "Minimalistic color picker without dependencies",
"main": "vanilla-color-picker.js",
"directories": {
Expand Down
9 changes: 5 additions & 4 deletions src/vanilla-color-picker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ============================================================
//
// Vanilla Color Picker v 0.1.7
// Vanilla Color Picker v 0.1.8
//
// http://github.com/miroshko/vanilla-color-picker
//
Expand Down Expand Up @@ -32,7 +32,7 @@
} else {
global.document.head.appendChild(style);
}

}

function MessageMediator() {
Expand Down Expand Up @@ -117,15 +117,15 @@
var _this = this;
this.elem.addEventListener('click', function(e) {
if (e.target.classList.contains('vanilla-color-picker-single-color')) {
_this.emit('colorChosen', e.target.dataset.color);
_this.emit('colorChosen', e.target.dataset.color);
}
});
this.elem.addEventListener('keydown', function(e) {
var ENTER = 13;
var ESC = 27;
var keyCode = e.which || e.keyCode;
if (keyCode == ENTER) {
_this.emit('colorChosen', e.target.dataset.color);
_this.emit('colorChosen', e.target.dataset.color);
}
if(keyCode == ESC) {
_this.emit('lostFocus');
Expand Down Expand Up @@ -196,6 +196,7 @@
this_.emit('colorChosen', color, this_.elem);
});
this_.currentPicker.on('lostFocus', function() {
this_.emit('lostFocus');
this_.destroyPicker();
});
this_.emit('pickerCreated');
Expand Down
20 changes: 11 additions & 9 deletions tests/vanilla-color-picker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ define(['../src/vanilla-color-picker'], function(colorPicker) {
});
});

fdescribe('Color pickers events', function() {
describe('Color pickers events', function() {
var input, picker;
beforeEach(function() {
input = document.createElement('input');
Expand All @@ -115,10 +115,7 @@ define(['../src/vanilla-color-picker'], function(colorPicker) {
});

it('pickerCreated is triggered', function(done) {
picker.on('pickerCreated', function() {
expect(true).toEqual(true);
done();
});
picker.on('pickerCreated', done);
effroi.mouse.focus(input);
effroi.mouse.click(input);
});
Expand All @@ -138,13 +135,18 @@ define(['../src/vanilla-color-picker'], function(colorPicker) {
});

it('pickerClosed is triggered', function(done) {
picker.on('pickerClosed', function() {
expect(true).toEqual(true);
done();
});
picker.on('pickerClosed', done);
effroi.mouse.focus(input);
effroi.mouse.click(input);
effroi.keyboard.hit(effroi.keyboard.ESC);
effroi.keyboard.up(effroi.keyboard.ESC);
});

it('lostFocus is triggered', function(done) {
picker.on('lostFocus', done);
effroi.mouse.focus(input);
effroi.mouse.click(input);
effroi.mouse.focus(document.body);
});
});
});

0 comments on commit 1423b2d

Please sign in to comment.