Created
January 14, 2018 21:45
-
-
Save kamioftea/d7de7a4f7ac318bd1cdff04d8a4e613e to your computer and use it in GitHub Desktop.
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
var keyBuffer = []; | |
var clear; | |
jQuery.fn.extend({ | |
insertAtCaret: function(insertText){ | |
return this.each(function(i) { | |
// if (CKEDITOR.instances[$(document.activeElement).attr('id')] !== undefined) | |
// { | |
// CKEDITOR.instances[$(document.activeElement).attr('id')].insertText(insertText) | |
// } else | |
if (document.selection) { | |
//For browsers like Internet Explorer | |
this.focus(); | |
sel = document.selection.createRange(); | |
sel.text = myValue; | |
this.focus(); | |
} | |
else if (this.selectionStart || this.selectionStart == '0') { | |
//For browsers like Firefox and Webkit based | |
var startPos = this.selectionStart; | |
var endPos = this.selectionEnd; | |
var scrollTop = this.scrollTop; | |
this.value = this.value.substring(0, startPos)+insertText+this.value.substring(endPos,this.value.length); | |
this.focus(); | |
this.selectionStart = startPos + insertText.length; | |
this.selectionEnd = startPos + insertText.length; | |
this.scrollTop = scrollTop; | |
} else { | |
this.value += insertText; | |
this.focus(); | |
} | |
}); | |
} | |
}); | |
$(function(){ | |
if ($('#barcode').length == 1) | |
{ | |
$(document).keypress(function(e){ | |
var pattern = /^[a-zA-Z0-9\-]$/; | |
var charString = String.fromCharCode(e.charCode); | |
if (e.charCode == 13) | |
{ | |
flushKeyBuffer(); | |
} | |
else if (charString.match(pattern)) | |
{ | |
e.preventDefault(); | |
keyBuffer.push(e.charCode); | |
if (keyBuffer.length > 2) | |
{ | |
$('#barcode').focus().val(''); | |
} | |
if(clear) | |
{ | |
clearTimeout(clear); | |
clear = null; | |
} | |
clear = setTimeout(flushKeyBuffer, 50); | |
} | |
else | |
{ | |
if(clear) | |
{ | |
clearTimeout(clear); | |
clear = null; | |
} | |
flushKeyBuffer(); | |
} | |
}); | |
} | |
}); | |
function flushKeyBuffer() | |
{ | |
var el = document.activeElement; | |
if ($(el).val() !== undefined) | |
{ | |
var charCode; | |
while ((charCode = keyBuffer.shift())) | |
{ | |
$(el).insertAtCaret(String.fromCharCode(charCode)); | |
} | |
} | |
keyBuffer = []; | |
clearTimeout(clear); | |
clear = null; | |
}var keyBuffer = []; | |
var clear; | |
jQuery.fn.extend({ | |
insertAtCaret: function(insertText){ | |
return this.each(function(i) { | |
// if (CKEDITOR.instances[$(document.activeElement).attr('id')] !== undefined) | |
// { | |
// CKEDITOR.instances[$(document.activeElement).attr('id')].insertText(insertText) | |
// } else | |
if (document.selection) { | |
//For browsers like Internet Explorer | |
this.focus(); | |
sel = document.selection.createRange(); | |
sel.text = myValue; | |
this.focus(); | |
} | |
else if (this.selectionStart || this.selectionStart == '0') { | |
//For browsers like Firefox and Webkit based | |
var startPos = this.selectionStart; | |
var endPos = this.selectionEnd; | |
var scrollTop = this.scrollTop; | |
this.value = this.value.substring(0, startPos)+insertText+this.value.substring(endPos,this.value.length); | |
this.focus(); | |
this.selectionStart = startPos + insertText.length; | |
this.selectionEnd = startPos + insertText.length; | |
this.scrollTop = scrollTop; | |
} else { | |
this.value += insertText; | |
this.focus(); | |
} | |
}); | |
} | |
}); | |
$(function(){ | |
if ($('#barcode').length == 1) | |
{ | |
$(document).keypress(function(e){ | |
var pattern = /^[a-zA-Z0-9\-]$/; | |
var charString = String.fromCharCode(e.charCode); | |
if (e.charCode == 13) | |
{ | |
flushKeyBuffer(); | |
} | |
else if (charString.match(pattern)) | |
{ | |
e.preventDefault(); | |
keyBuffer.push(e.charCode); | |
if (keyBuffer.length > 2) | |
{ | |
$('#barcode').focus().val(''); | |
} | |
if(clear) | |
{ | |
clearTimeout(clear); | |
clear = null; | |
} | |
clear = setTimeout(flushKeyBuffer, 50); | |
} | |
else | |
{ | |
if(clear) | |
{ | |
clearTimeout(clear); | |
clear = null; | |
} | |
flushKeyBuffer(); | |
} | |
}); | |
} | |
}); | |
function flushKeyBuffer() | |
{ | |
var el = document.activeElement; | |
if ($(el).val() !== undefined) | |
{ | |
var charCode; | |
while ((charCode = keyBuffer.shift())) | |
{ | |
$(el).insertAtCaret(String.fromCharCode(charCode)); | |
} | |
} | |
keyBuffer = []; | |
clearTimeout(clear); | |
clear = null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment