Skip to content

Commit

Permalink
Added tests, changed events name, string repeat polyfill added
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvaincombes committed Jun 28, 2016
1 parent 9b121b0 commit ef4ddbd
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ List of events the plugin triggers :

| Event name | Description |
| ------------- | ------------- |
| initialised | This event is fired when init is done |
| change | This event is fired when the value of visible front element is changed |
| resize | This event is fired when a resize window event has been received and treated |
| imagesCompare:initialised | This event is fired when init is done |
| imagesCompare:changed | This event is fired when the value of visible front element is changed |
| imagesCompare:resized | This event is fired when a resize window event has been received and treated |

#### Example listening to change event

Expand Down
11 changes: 3 additions & 8 deletions examples/assets/css/example.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
* {
margin: 0;
padding: 0;
}

body {
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
background: darkgrey;
Expand All @@ -22,7 +17,7 @@ h2 {
margin-bottom: 15px;
}

button {
button.btn {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
Expand All @@ -42,12 +37,12 @@ button {
margin-top: 10px;
}

button:hover {
button.btn:hover {
background: cornflowerblue;
color: white;
}

button::-moz-focus-inner {
button.btn::-moz-focus-inner {
border: 0;
padding: 0;
}
Expand Down
27 changes: 15 additions & 12 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ <h2>Example</h2>
</div>
</div>
<div>
<button class="js-front-btn">Show front image</button>
<button class="js-back-btn">Show back image</button>
<button class="js-toggle-btn">Toggle</button>
<button class="btn js-front-btn">Show front image</button>
<button class="btn js-back-btn">Show back image</button>
<button class="btn js-toggle-btn">Toggle</button>
</div>

<footer>
Expand All @@ -41,10 +41,13 @@ <h2>Example</h2>
<script type="text/javascript" src="../src/assets/js/jquery.images-compare.js"></script>
<script type="text/javascript">
$(function () {
var test = $('.js-img-compare').imagesCompare().data('imagesCompare');
test.on('change', function (event) {
console.log('change');
console.log(event);
var imagesCompareElement = $('.js-img-compare').imagesCompare();
var imagesCompare = imagesCompareElement.data('imagesCompare');
var events = imagesCompare.events();

imagesCompare.on(events.changed, function (event) {
console.log(events.changed);
console.log(event.ratio);
if (event.ratio < 0.4) {
console.log('We see more than half of the back image');
}
Expand All @@ -63,20 +66,20 @@ <h2>Example</h2>

$('.js-front-btn').on('click', function (event) {
event.preventDefault();
test.setValue(1, true);
imagesCompare.setValue(1, true);
});

$('.js-back-btn').on('click', function (event) {
event.preventDefault();
test.setValue(0, true);
imagesCompare.setValue(0, true);
});

$('.js-toggle-btn').on('click', function (event) {
event.preventDefault();
if (test.getValue() >= 0 && test.getValue() < 1) {
test.setValue(1, true);
if (imagesCompare.getValue() >= 0 && imagesCompare.getValue() < 1) {
imagesCompare.setValue(1, true);
} else {
test.setValue(0, true);
imagesCompare.setValue(0, true);
}
});
});
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-images-compare",
"version": "0.1.3",
"version": "0.2.0",
"description": "A jquery plugin for comparing two images",
"keywords": [
"jquery",
Expand All @@ -27,6 +27,8 @@
"copy-cli": "^1.2.1",
"csslint": "^0.10.0",
"jshint": "^2.9.2",
"node-qunit-phantomjs": "^1.4.0",
"qunitjs": "^2.0.0",
"uglify-js": "^2.6.2"
},
"scripts": {
Expand All @@ -41,7 +43,7 @@
"lint": "npm run lint:js && npm run lint:css",
"copy": "npm run copy:js && npm run copy:css",
"minify": "npm run minify:js && npm run minify:css",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node_modules/node-qunit-phantomjs/bin/node-qunit-phantomjs ./src/tests/test.html"
},
"author": "Sylvain Combes <combes.sylvain@gmail.com> (http://www.sylvaincombes.com)",
"contributors": [
Expand Down
34 changes: 28 additions & 6 deletions src/assets/js/jquery.images-compare.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
;(function ($, window, document, undefined) {
"use strict";

// String repeat polyfill
if (!String.prototype.repeat) {
String.prototype.repeat = function (n) {
n = n || 1;
return new Array(n + 1).join(this);
}
}

var pluginName = 'imagesCompare',
defaults = {
Expand All @@ -18,7 +27,17 @@
options.roundFactor = parseInt('1' + '0'.repeat(options.precision));

this._name = pluginName;
var frontElement, backElement, separator, dragHandle, lastRatio = 1, size = {width: 0, height: 0, maxWidth: 0, maxHeight: 0};

var frontElement, backElement, separator, dragHandle, lastRatio = 1, size = {
width: 0,
height: 0,
maxWidth: 0,
maxHeight: 0
}, events = {
initialised: "imagesCompare:initialised",
changed: "imagesCompare:changed",
resized: "imagesCompare:resized"
};

init();

Expand All @@ -32,7 +51,7 @@

// Let the world know we have done the init
element.trigger({
type: 'initialised'
type: events.initialised
});
}

Expand All @@ -43,7 +62,7 @@
setVisibleRatio(lastRatio);

// Let the world know we have done some resize updates
// element.trigger(event);
element.trigger(events.resized);
});
}

Expand Down Expand Up @@ -212,7 +231,7 @@
var ratio = $(frontElement).attr('ratio');
// Let the world know something has changed
element.trigger({
type: 'change',
type: events.changed,
ratio: ratio,
value: getRatioValue(ratio),
animate: true
Expand Down Expand Up @@ -256,7 +275,7 @@
// Let the world know something has changed
if (lastRatio != ratio) {
element.trigger({
type: 'change',
type: events.changed,
ratio: lastRatio,
value: width,
animate: animate
Expand All @@ -280,7 +299,7 @@
// Let the world know something has changed
if (lastRatio != ratio) {
element.trigger({
type: 'change',
type: events.changed,
ratio: ratio,
value: width,
animate: animate
Expand Down Expand Up @@ -343,6 +362,9 @@
"off": function (eventName, callback) {
element.off(eventName, callback);
return element;
},
"events": function () {
return events;
}
};
}
Expand Down
113 changes: 113 additions & 0 deletions src/tests/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>QUnit Example</title>
<link rel="stylesheet" href="../../examples/assets/css/example.css"/>
<link rel="stylesheet" href="../assets/css/images-compare.css"/>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>

<div class="page-container">
<h1>jQuery Image compare</h1>
<h2>Example</h2>

<!-- Test 1 -->
<div class="js-img-compare">
<div style="display: none;">
<span class="images-compare-label">Before</span>
<img src="../../examples/assets/img/01-celine-skowron-before.jpg" alt="Before">
</div>
<div>
<span class="images-compare-label">After</span>
<img src="../../examples/assets/img/01-celine-skowron-after.jpg" alt="After">
</div>
</div>
<div>
<button class="btn js-front-btn">Show front image</button>
<button class="btn js-back-btn">Show back image</button>
<button class="btn js-toggle-btn">Toggle</button>
</div>

<footer>
Image credits : <a href="http://celine-skowron.fr">Céline Skowron</a>
</footer>

</div>

<script src="https://code.jquery.com/qunit/qunit-2.0.0.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
<script type="text/javascript" src="../assets/js/jquery.images-compare.js"></script>
<script type="text/javascript">
$(function () {
var imagesCompareElement = $('.js-img-compare').imagesCompare();
var imagesCompare = imagesCompareElement.data('imagesCompare');
var events = imagesCompare.events();

// CHANGE EVENT TESTS AND
// GETTER SETTER TESTS
function testElementChangeEvent(event) {
QUnit.test("test change event listener", function (assert) {
assert.equal(event.ratio, 1, 'Change event received and value equal 1');
});
}

function testChangeEvent(event) {
QUnit.test("test change event listener", function (assert) {
assert.equal(event.ratio, 0, 'Change event received and value equal 0');
});
}

// Async tests
QUnit.test("Change event tests and getter setter tests", function (assert) {
var numCalls = 3;
assert.expect(numCalls);
var done = assert.async(numCalls);

// change value 1 and listen to element
setTimeout(function () {
imagesCompareElement.on(events.changed, testElementChangeEvent);
imagesCompare.setValue(1);
assert.equal(imagesCompare.getValue(), 1, "test get and set value from outside : Changed value success (1)");
done();
}, 60);

// change value 0 and listen to "data" object
setTimeout(function () {
imagesCompareElement.off(events.changed, testElementChangeEvent);
imagesCompare.on(events.changed, testChangeEvent);
imagesCompare.setValue(0);
assert.equal(imagesCompare.getValue(), 0, "test get and set value from outside : Changed value success (0)");
done();
}, 120);

setTimeout(function () {
assert.ok(true, 'test get and set value from outside and event completed');
imagesCompare.off(events.changed, testChangeEvent);
done();
}, 180);
});

// RESIZE EVENT TEST
function testResizeEvent(event) {
QUnit.test("test resize event", function (assert) {
assert.ok(true, 'Resize event received');
});
}

imagesCompareElement.on(events.resized, testResizeEvent);
$(window).trigger('resize');
imagesCompareElement.off(events.resized, testResizeEvent);

imagesCompare.on(events.resized, testResizeEvent);
$(window).trigger('resize');
imagesCompare.off(events.resized, testResizeEvent);
});
</script>
</body>
</html>

0 comments on commit ef4ddbd

Please sign in to comment.