jquery.sinaEmotion.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @author 夏の寒风
  3. * @time 2012-12-14
  4. */
  5. //自定义hashtable
  6. function Hashtable() {
  7. this._hash = new Object();
  8. this.put = function(key, value) {
  9. if (typeof (key) != "undefined") {
  10. if (this.containsKey(key) == false) {
  11. this._hash[key] = typeof (value) == "undefined" ? null : value;
  12. return true;
  13. } else {
  14. return false;
  15. }
  16. } else {
  17. return false;
  18. }
  19. }
  20. this.remove = function(key) { delete this._hash[key]; }
  21. this.size = function() { var i = 0; for (var k in this._hash) { i++; } return i; }
  22. this.get = function(key) { return this._hash[key]; }
  23. this.containsKey = function(key) { return typeof (this._hash[key]) != "undefined"; }
  24. this.clear = function() { for (var k in this._hash) { delete this._hash[k]; } }
  25. }
  26. var emotions = new Array();
  27. var categorys = new Array();// 分组
  28. var uSinaEmotionsHt = new Hashtable();
  29. // 初始化缓存,页面仅仅加载一次就可以了
  30. //替换
  31. function AnalyticEmotion(s) {
  32. if(typeof (s) != "undefined" && s != null) {
  33. var sArr = s.match(/\[.*?\]/g);
  34. if(!sArr){return s}
  35. for(var i = 0; i < sArr.length; i++){
  36. if(uSinaEmotionsHt.containsKey(sArr[i])) {
  37. var reStr = "<img src=\"" + uSinaEmotionsHt.get(sArr[i]) + "\" height=\"20\" width=\"20\" />";
  38. s = s.replace(sArr[i], reStr);
  39. }
  40. }
  41. }
  42. return s;
  43. }
  44. (function($){
  45. $.fn.SinaEmotion = function(target){
  46. var cat_current;
  47. var cat_page;
  48. $(this).click(function(event){
  49. event.stopPropagation();
  50. var eTop = target.offset().top + target.height() + 15;
  51. var eLeft = target.offset().left - 1;
  52. if($('#emotions .categorys')[0]){
  53. $('#emotions').css({top: eTop, left: eLeft});
  54. $('#emotions').toggle();
  55. return;
  56. }
  57. $('body').append('<div id="emotions"></div>');
  58. $('#emotions').css({top: eTop, left: eLeft});
  59. $('#emotions').html('<div>正在加载,请稍候...</div>');
  60. $('#emotions').click(function(event){
  61. event.stopPropagation();
  62. });
  63. $('#emotions').html('<div style="float:right"><a href="javascript:void(0);" id="prev">&laquo;</a><a href="javascript:void(0);" id="next">&raquo;</a></div><div class="categorys"></div><div class="container"></div><div class="page"></div>');
  64. $('#emotions #prev').click(function(){
  65. showCategorys(cat_page - 1);
  66. });
  67. $('#emotions #next').click(function(){
  68. showCategorys(cat_page + 1);
  69. });
  70. showCategorys();
  71. showEmotions();
  72. });
  73. $('body').click(function(){
  74. $('#emotions').remove();
  75. });
  76. $.fn.insertText = function(text){
  77. this.each(function() {
  78. if(this.tagName !== 'INPUT' && this.tagName !== 'TEXTAREA') {return;}
  79. if (document.selection) {
  80. this.focus();
  81. var cr = document.selection.createRange();
  82. cr.text = text;
  83. cr.collapse();
  84. cr.select();
  85. }else if (this.selectionStart || this.selectionStart == '0') {
  86. var
  87. start = this.selectionStart,
  88. end = this.selectionEnd;
  89. this.value = this.value.substring(0, start)+ text+ this.value.substring(end, this.value.length);
  90. this.selectionStart = this.selectionEnd = start+text.length;
  91. }else {
  92. this.value += text;
  93. }
  94. });
  95. return this;
  96. }
  97. function showCategorys(){
  98. var page = arguments[0]?arguments[0]:0;
  99. if(page < 0 || page >= categorys.length / 5){
  100. return;
  101. }
  102. $('#emotions .categorys').html('');
  103. cat_page = page;
  104. for(var i = page * 5; i < (page + 1) * 5 && i < categorys.length; ++i){
  105. $('#emotions .categorys').append($('<a href="javascript:void(0);">' + categorys[i] + '</a>'));
  106. }
  107. $('#emotions .categorys a').click(function(){
  108. showEmotions($(this).text());
  109. });
  110. $('#emotions .categorys a').each(function(){
  111. if($(this).text() == cat_current){
  112. $(this).addClass('current');
  113. }
  114. });
  115. }
  116. function showEmotions(){
  117. var category = arguments[0]?arguments[0]:'默认';
  118. var page = arguments[1]?arguments[1] - 1:0;
  119. $('#emotions .container').html('');
  120. $('#emotions .page').html('');
  121. cat_current = category;
  122. for(var i = page * 72; i < (page + 1) * 72 && i < emotions[category].length; ++i){
  123. $('#emotions .container').append($('<a href="javascript:void(0);" title="' + emotions[category][i].name + '"><img src="' + emotions[category][i].icon + '" alt="' + emotions[category][i].name + '" width="22" height="22" /></a>'));
  124. }
  125. $('#emotions .container a').click(function(){
  126. target.insertText($(this).attr('title'));
  127. $('#emotions').remove();
  128. });
  129. for(var i = 1; i < emotions[category].length / 72 + 1; ++i){
  130. $('#emotions .page').append($('<a href="javascript:void(0);"' + (i == page + 1?' class="current"':'') + '>' + i + '</a>'));
  131. }
  132. $('#emotions .page a').click(function(){
  133. showEmotions(category, $(this).text());
  134. });
  135. $('#emotions .categorys a.current').removeClass('current');
  136. $('#emotions .categorys a').each(function(){
  137. if($(this).text() == category){
  138. $(this).addClass('current');
  139. }
  140. });
  141. }
  142. }
  143. })(jQuery);