emailAutoComplete.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. function EmailAutoComplete(options) {
  2. this.config = {
  3. targetCls: '.inputElem',
  4. parentCls: '.parentCls',
  5. hiddenCls: '.hiddenCls',
  6. searchForm: '.jqtransformdone',
  7. hoverBg: 'hoverBg',
  8. inputValColor: 'black',
  9. mailArr: ["","@qq.com","@163.com","@126.com","@sina.com","@gmail.com","@foxmail.com","@sohu.com","@vip.qq.com","@hotmail.com","@163.net","@sina.com.cn","@139.com","@189.cn","@33.cn"],
  10. isSelectHide: true,
  11. callback: null
  12. };
  13. this.cache = { onlyFlag: true, currentIndex: -1, oldIndex: -1 };
  14. this.init(options)
  15. }
  16. EmailAutoComplete.prototype = { constructor: EmailAutoComplete, init: function(options) { this.config = $.extend(this.config, options || {});
  17. var self = this,
  18. _config = self.config,
  19. _cache = self.cache;
  20. $(_config.targetCls).each(function(index, item) { $(item).keyup(function(e) {
  21. var target = e.target,
  22. targetVal = $.trim($(this).val()),
  23. keycode = e.keyCode,
  24. elemHeight = $(this).outerHeight(),
  25. elemWidth = $(this).outerWidth(),
  26. parentNode = $(this).closest(_config.parentCls);
  27. console.log(targetVal);
  28. if(targetVal.indexOf('script')>-1){
  29. targetVal='';
  30. $(this).val("");
  31. return false;
  32. }
  33. $(parentNode).css({ 'position': 'relative' });
  34. if (targetVal == '') { $(item).attr({ 'data-html': '' });
  35. $(_config.hiddenCls, parentNode).val('');
  36. _cache.currentIndex = -1;
  37. _cache.oldIndex = -1;
  38. $(".auto-tip", parentNode) && !$(".auto-tip", parentNode).hasClass('hidden') && $(".auto-tip", parentNode).addClass('hidden');
  39. self._removeBg(parentNode) } else { $(item).attr({ 'data-html': targetVal });
  40. $(_config.hiddenCls, parentNode).val(targetVal);
  41. $(".auto-tip", parentNode) && $(".auto-tip", parentNode).hasClass('hidden') && $(".auto-tip", parentNode).removeClass('hidden');
  42. self._renderHTML({ keycode: keycode, e: e, target: target, targetVal: targetVal, height: elemHeight, width: elemWidth, parentNode: parentNode }) } }) });
  43. $(_config.searchForm).each(function(index, item) { $(item).keydown(function(e) {
  44. var keyCode = e.keyCode;
  45. if (keyCode == 13) {
  46. return false
  47. }
  48. })
  49. });
  50. $(document).click(function(e) { e.stopPropagation();
  51. var target = e.target,
  52. tagCls = _config.targetCls.replace(/^\./, '');
  53. if (!$(target).hasClass(tagCls)) { $('.auto-tip') && $('.auto-tip').each(function(index, item) {!$(item).hasClass('hidden') && $(item).addClass('hidden') }) } }); }, _renderHTML: function(cfg) {
  54. var self = this,
  55. _config = self.config,
  56. _cache = self.cache,
  57. curVal;
  58. var curIndex = self._keyCode(cfg.keycode);
  59. $('.auto-tip', cfg.parentNode).hasClass('hidden') && $('.auto-tip', cfg.parentNode).removeClass('hidden');
  60. if (curIndex > -1) { self._keyUpAndDown(cfg.targetVal, cfg.e, cfg.parentNode) } else {
  61. if (/@/.test(cfg.targetVal)) { curVal = cfg.targetVal.replace(/@.*/, '') } else { curVal = cfg.targetVal }
  62. if (_cache.onlyFlag) { $(cfg.parentNode).append('<input type="hidden" class="hiddenCls"/>');
  63. var wrap = '<ul class="auto-tip">';
  64. for (var i = 0; i < _config.mailArr.length; i++) { wrap += '<li class="p-index' + i + '">' + '<span class="output-num"></span><em class="em" data-html="' + _config.mailArr[i] + '">' + _config.mailArr[i] + '</em></li>' }
  65. wrap += '</ul>';
  66. _cache.onlyFlag = false;
  67. $(cfg.parentNode).append(wrap);
  68. $('.auto-tip', cfg.parentNode).css({ 'position': 'absolute', 'top': cfg.height, 'width': cfg.width - 2 + 'px', 'left': 0, 'border': '1px solid #ccc', 'z-index': 10000 }) }
  69. $('.auto-tip li', cfg.parentNode).each(function(index, item) { $('.output-num', item).html(curVal);!$('.output-num', item).hasClass(_config.inputValColor) && $('.output-num', item).addClass(_config.inputValColor);
  70. var emVal = $.trim($('.em', item).attr('data-html'));
  71. $(item).attr({ 'data-html': curVal + '' + emVal }) });
  72. self._accurateMate({ target: cfg.target, parentNode: cfg.parentNode });
  73. self._itemHover(cfg.parentNode);
  74. self._executeClick(cfg.parentNode) } }, _accurateMate: function(cfg) {
  75. var self = this,
  76. _config = self.config,
  77. _cache = self.cache;
  78. var curVal = $.trim($(cfg.target, cfg.parentNode).attr('data-html')),
  79. newArrs = [];
  80. if (/@/.test(curVal)) {
  81. var prefix = curVal.replace(/@.*/, ""),
  82. suffix = curVal.replace(/.*@/, "");
  83. $.map(_config.mailArr, function(n) {
  84. var reg = new RegExp(suffix);
  85. if (reg.test(n)) { newArrs.push(n) } });
  86. if (newArrs.length > 0) { $('.auto-tip', cfg.parentNode).html('');
  87. $(".auto-tip", cfg.parentNode) && $(".auto-tip", cfg.parentNode).hasClass('hidden') && $(".auto-tip", cfg.parentNode).removeClass('hidden');
  88. var html = '';
  89. for (var j = 0, jlen = newArrs.length; j < jlen; j++) { html += '<li class="p-index' + j + '">' + '<span class="output-num"></span><em class="em" data-html="' + newArrs[j] + '">' + newArrs[j] + '</em></li>' }
  90. $('.auto-tip', cfg.parentNode).html(html);
  91. $('.auto-tip li', cfg.parentNode).each(function(index, item) { $('.output-num', item).html(prefix);!$('.output-num', item).hasClass(_config.inputValColor) && $('.output-num', item).addClass(_config.inputValColor);
  92. var emVal = $.trim($('.em', item).attr('data-html'));
  93. $(item).attr('data-html', '');
  94. $(item).attr({ 'data-html': prefix + '' + emVal }) });
  95. _cache.currentIndex = -1;
  96. _cache.oldIndex = -1;
  97. $('.auto-tip .output-num', cfg.parentNode).html(prefix);
  98. self._itemHover(cfg.parentNode);
  99. self._executeClick(cfg.parentNode) } else { $(".auto-tip", cfg.parentNode) && !$(".auto-tip", cfg.parentNode).hasClass('hidden') && $(".auto-tip", cfg.parentNode).addClass('hidden');
  100. $('.auto-tip', cfg.parentNode).html('') } } }, _itemHover: function(parentNode) {
  101. var self = this,
  102. _config = self.config,
  103. _cache = self.cache;
  104. $('.auto-tip li', parentNode).hover(function(index, item) {!$(this).hasClass(_config.hoverBg) && $(this).addClass(_config.hoverBg) }, function() { $(this).hasClass(_config.hoverBg) && $(this).removeClass(_config.hoverBg) }) }, _removeBg: function(parentNode) {
  105. var self = this,
  106. _config = self.config;
  107. $(".auto-tip li", parentNode).each(function(index, item) { $(item).hasClass(_config.hoverBg) && $(item).removeClass(_config.hoverBg) }) }, _keyUpAndDown: function(targetVal, e, parentNode) {
  108. var self = this,
  109. _cache = self.cache,
  110. _config = self.config;
  111. if ($('.auto-tip' + ' li', parentNode) && $('.auto-tip' + ' li').length > 0) {
  112. var plen = $('.auto-tip' + ' li', parentNode).length,
  113. keyCode = e.keyCode;
  114. _cache.oldIndex = _cache.currentIndex;
  115. if (keyCode == 38) {
  116. if (_cache.currentIndex == -1) { _cache.currentIndex = plen - 1 } else { _cache.currentIndex = _cache.currentIndex - 1;
  117. if (_cache.currentIndex < 0) { _cache.currentIndex = plen - 1 } }
  118. if (_cache.currentIndex !== -1) {!$('.auto-tip .p-index' + _cache.currentIndex, parentNode).hasClass(_config.hoverBg) && $('.auto-tip .p-index' + _cache.currentIndex, parentNode).addClass(_config.hoverBg).siblings().removeClass(_config.hoverBg);
  119. var curAttr = $('.auto-tip' + ' .p-index' + _cache.currentIndex, parentNode).attr('data-html');
  120. $(_config.targetCls, parentNode).val(curAttr);
  121. $(_config.hiddenCls, parentNode).val(curAttr) } } else if (keyCode == 40) {
  122. if (_cache.currentIndex == plen - 1) { _cache.currentIndex = 0 } else { _cache.currentIndex++;
  123. if (_cache.currentIndex > plen - 1) { _cache.currentIndex = 0 } }
  124. if (_cache.currentIndex !== -1) {!$('.auto-tip .p-index' + _cache.currentIndex, parentNode).hasClass(_config.hoverBg) && $('.auto-tip .p-index' + _cache.currentIndex, parentNode).addClass(_config.hoverBg).siblings().removeClass(_config.hoverBg);
  125. var curAttr = $('.auto-tip' + ' .p-index' + _cache.currentIndex, parentNode).attr('data-html');
  126. $(_config.targetCls, parentNode).val(curAttr);
  127. $(_config.hiddenCls, parentNode).val(curAttr) } } else if (keyCode == 13) {
  128. var curVal = $('.auto-tip' + ' .p-index' + _cache.oldIndex, parentNode).attr('data-html');
  129. $(_config.targetCls, parentNode).val(curVal);
  130. $(_config.hiddenCls, parentNode).val(curVal);
  131. if (_config.isSelectHide) {!$(".auto-tip", parentNode).hasClass('hidden') && $(".auto-tip", parentNode).addClass('hidden') }
  132. _config.callback && $.isFunction(_config.callback) && _config.callback();
  133. _cache.currentIndex = -1;
  134. _cache.oldIndex = -1 } } }, _keyCode: function(code) {
  135. var arrs = ['17', '18', '38', '40', '37', '39', '33', '34', '35', '46', '36', '13', '45', '44', '145', '19', '20', '9'];
  136. for (var i = 0, ilen = arrs.length; i < ilen; i++) {
  137. if (code == arrs[i]) {
  138. return i } }
  139. return -1 }, _executeClick: function(parentNode) {
  140. var _self = this,
  141. _config = _self.config;
  142. $('.auto-tip' + ' li', parentNode).unbind('click');
  143. $('.auto-tip' + ' li', parentNode).bind('click', function(e) {
  144. var dataAttr = $(this).attr('data-html');
  145. $(_config.targetCls, parentNode).val(dataAttr);
  146. if (_config.isSelectHide) {!$(".auto-tip", parentNode).hasClass('hidden') && $(".auto-tip", parentNode).addClass('hidden') }
  147. $(_config.hiddenCls, parentNode).val(dataAttr);
  148. _config.callback && $.isFunction(_config.callback) && _config.callback() }) } };
  149. $(function() { new EmailAutoComplete({}) });