jquery-barcodeListener.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  5. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  6. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  8. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  9. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  10. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  11. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  12. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13. *
  14. * This software consists of voluntary contributions made by many individuals
  15. * and is licensed under the new BSD license.
  16. *
  17. * @author David Zeller <me@zellerda.com>
  18. * @license http://www.opensource.org/licenses/BSD-3-Clause New BSD license
  19. * @version 1.0
  20. */
  21. (function($){
  22. $.barcodeListener = function(context, options){
  23. var $defaults = {
  24. support: [8, 12, 13]
  25. };
  26. var $this = this;
  27. $this.element = $(context);
  28. $this.timeout = 0;
  29. $this.code = '';
  30. $this.settings = {};
  31. $this.init = function(){
  32. $this.settings = $.extend({}, $defaults, options);
  33. $this.element.on('keypress', function(e){
  34. $this.listen(e);
  35. })
  36. };
  37. $this.listen = function(e){
  38. var $char = $this.validateKey(e.which);
  39. if($char === 13){
  40. $this.validate();
  41. } else if($char !== false) {
  42. if($this.code == ''){
  43. setTimeout($this.clear(), 1000);
  44. }
  45. $this.add($char);
  46. }
  47. };
  48. $this.validate = function(){
  49. var $tmp = $this.code;
  50. if($this.settings.support.indexOf($tmp.length) > -1){
  51. var $d = new Date(),
  52. $interval = $d.getTime() - $this.timeout;
  53. $this.clear();
  54. if($interval < 1000){
  55. $this.element.trigger('barcode.valid', [$tmp]);
  56. }
  57. } else {
  58. $this.clear();
  59. }
  60. };
  61. $this.clear = function(){
  62. $this.code = '';
  63. $this.timeout = 0;
  64. };
  65. $this.validateKey = function(keycode){
  66. if(keycode == 13 || (keycode >= 48 && keycode <= 57)){
  67. if(keycode == 13){
  68. return keycode;
  69. } else {
  70. return String.fromCharCode(keycode);
  71. }
  72. } else {
  73. return false;
  74. }
  75. };
  76. $this.add = function(char){
  77. if($this.timeout === 0){
  78. var $d = new Date();
  79. $this.timeout = $d.getTime();
  80. }
  81. $this.code += char;
  82. };
  83. $this.init();
  84. };
  85. $.fn.barcodeListener = function(options) {
  86. return this.each(function(){
  87. if(undefined == $(this).data('barcodeListener')){
  88. var plugin = new $.barcodeListener(this, options);
  89. $(this).data('barcodeListener', plugin);
  90. }
  91. });
  92. }
  93. })(jQuery);