-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
258 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var onInputChange = (function($){ | ||
|
||
/* | ||
ie9 doesn't trigger oninput event when content is removed with BACKSPACE, ctrl+x etc... | ||
I will not bother with feature check. | ||
*/ | ||
var onInputSupport = (!$.browser.msie); | ||
|
||
function OnInputChange(element, callback, options) { | ||
this.element = element; | ||
this.$element = $(element); | ||
this.value = element.value; | ||
this._callback = callback; | ||
this.options = $.extend({ | ||
time: 150 | ||
}, options); | ||
$(element).on('focus', $.proxy(this._listen, this)); | ||
$(element).on('blur', $.proxy(this._unlisten, this)); | ||
} | ||
|
||
OnInputChange.prototype = { | ||
|
||
_listen: function(){ | ||
if(onInputSupport){ | ||
this.$element.on('input', $.proxy(this._run, this)); | ||
} | ||
else { | ||
this._interval = window.setInterval($.proxy(this._check, this), this.options.time); | ||
} | ||
return true; | ||
}, | ||
|
||
_unlisten: function(){ | ||
if(onInputSupport){ | ||
this.$element.off('input', this._run); | ||
} | ||
else { | ||
window.clearInterval(this._interval); | ||
} | ||
return true; | ||
}, | ||
|
||
_run: function(){ | ||
this.value = this.element.value; | ||
this._callback(this.value, this.element); | ||
}, | ||
|
||
_check: function(){ | ||
if(this.element.value != this.value) { | ||
this._run(); | ||
} | ||
} | ||
}; | ||
|
||
$.fn.onInputChange = function(callback, options){ | ||
return this.each(function(){ | ||
new OnInputChange(this, callback, options); | ||
}); | ||
}; | ||
|
||
return { | ||
Constructor: OnInputChange, | ||
support:onInputSupport | ||
}; | ||
|
||
})(window.jQuery); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>On input change Test Suite</title> | ||
<!-- Load local jQuery. --> | ||
<script src="../libs/jquery/jquery.js"></script> | ||
<!-- Remove access to $ for better real-world testing. --> | ||
<script>jQuery.noConflict()</script> | ||
<!-- Load local QUnit (grunt requires QUnit v1.0.0 or newer). --> | ||
<link rel="stylesheet" href="../libs/qunit/qunit.css" media="screen"> | ||
<script src="../libs/qunit/qunit.js"></script> | ||
<!-- Load local lib and tests. --> | ||
<script src="../src/on-input-change.js"></script> | ||
<script src="on-input-change_test.js"></script> | ||
</head> | ||
<body> | ||
<h1 id="qunit-header">Ftools Events Test Suite</h1> | ||
<h2 id="qunit-banner"></h2> | ||
<div id="qunit-testrunner-toolbar"></div> | ||
<h2 id="qunit-userAgent"></h2> | ||
<ol id="qunit-tests"></ol> | ||
<div id="qunit-fixture"> | ||
<input id="textInput" type="text"> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/*global QUnit:false, module:false, test:false, asyncTest:false, expect:false*/ | ||
/*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/ | ||
/*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/ | ||
(function($) { | ||
|
||
/* | ||
======== A Handy Little QUnit Reference ======== | ||
http://docs.jquery.com/QUnit | ||
Test methods: | ||
expect(numAssertions) | ||
stop(increment) | ||
start(decrement) | ||
Test assertions: | ||
ok(value, [message]) | ||
equal(actual, expected, [message]) | ||
notEqual(actual, expected, [message]) | ||
deepEqual(actual, expected, [message]) | ||
notDeepEqual(actual, expected, [message]) | ||
strictEqual(actual, expected, [message]) | ||
notStrictEqual(actual, expected, [message]) | ||
raises(block, [expected], [message]) | ||
*/ | ||
|
||
|
||
|
||
|
||
// I din't find a way to trigger input change event. I will test methods. | ||
module('on input change', { | ||
// This will run before each test in this module. | ||
setup: function() { | ||
|
||
} | ||
}); | ||
|
||
test("functionality", function() { | ||
|
||
var element = $('#textInput')[0]; | ||
var self = this; | ||
var instance = new onInputChange.Constructor(element, function(value, element) { | ||
self.resultValue = value; | ||
self.resultElement = element; | ||
}, {time:100}); | ||
|
||
equal(instance.options.time, 100, 'time option passed'); | ||
|
||
element.value = 'hello world'; | ||
var listening = instance._listen(); | ||
|
||
ok(listening, 'assume listening'); | ||
|
||
instance._check(); | ||
|
||
equal(this.resultValue, 'hello world', 'result should be passed and has new value'); | ||
equal(this.resultElement, element, 'element passed'); | ||
|
||
element.value = 'hello mars'; | ||
|
||
instance._check(); | ||
|
||
equal(this.resultValue, 'hello mars', 'result has new value'); | ||
|
||
var unlistening = instance._unlisten(); | ||
|
||
equal(unlistening, true, 'assume unlistening'); | ||
}); | ||
|
||
// Test interval version | ||
if (!onInputChange.support) { | ||
|
||
test("interval", function() { | ||
|
||
var element = $('#textInput')[0]; | ||
var self = this; | ||
var instance = new onInputChange.Constructor(element, function(value, element) { | ||
self.resultValue = value; | ||
self.resultElement = element; | ||
}, {time:100}); | ||
|
||
equal(instance.options.time, 100, 'time option passed'); | ||
|
||
element.focus(); | ||
|
||
element.value = 'hello world'; | ||
|
||
stop(); | ||
|
||
setTimeout(function () { | ||
equal(self.resultValue, 'hello world', 'result should be passed and has new value'); | ||
equal(self.resultElement, element, 'element passed'); | ||
|
||
element.value = 'hello mars'; | ||
|
||
setTimeout(function () { | ||
equal(self.resultValue, 'hello mars', 'result should have a new value'); | ||
|
||
element.blur(); | ||
element.value = 'hello there'; | ||
|
||
setTimeout(function () { | ||
equal(self.resultValue, 'hello mars', 'result should not change'); | ||
start(); | ||
}, 150); | ||
}, 150); | ||
}, 150); | ||
}); | ||
|
||
} | ||
|
||
|
||
|
||
}(jQuery)); |