jquery.smilies.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * 表情处理
  3. */
  4. var smilies_array = new Array();
  5. smilies_array[1] = [['1', ':smile:', 'smile.gif', '28', '28', '28', '微笑'], ['2', ':sad:', 'sad.gif', '28', '28', '28', '难过'], ['3', ':biggrin:', 'biggrin.gif', '28', '28', '28', '呲牙'], ['4', ':cry:', 'cry.gif', '28', '28', '28', '大哭'], ['5', ':huffy:', 'huffy.gif', '28', '28', '28', '发怒'], ['6', ':shocked:', 'shocked.gif', '28', '28', '28', '惊讶'], ['7', ':tongue:', 'tongue.gif', '28', '28', '28', '调皮'], ['8', ':shy:', 'shy.gif', '28', '28', '28', '害羞'], ['9', ':titter:', 'titter.gif', '28', '28', '28', '偷笑'], ['10', ':sweat:', 'sweat.gif', '28', '28', '28', '流汗'], ['11', ':mad:', 'mad.gif', '28', '28', '28', '抓狂'], ['12', ':lol:', 'lol.gif', '28', '28', '28', '阴险'], ['13', ':loveliness:', 'loveliness.gif', '28', '28', '28', '可爱'], ['14', ':funk:', 'funk.gif', '28', '28', '28', '惊恐'], ['15', ':curse:', 'curse.gif', '28', '28', '28', '咒骂'], ['16', ':dizzy:', 'dizzy.gif', '28', '28', '28', '晕'], ['17', ':shutup:', 'shutup.gif', '28', '28', '28', '闭嘴'], ['18', ':sleepy:', 'sleepy.gif', '28', '28', '28', '睡'], ['19', ':hug:', 'hug.gif', '28', '28', '28', '拥抱'], ['20', ':victory:', 'victory.gif', '28', '28', '28', '胜利'], ['21', ':sun:', 'sun.gif', '28', '28', '28', '太阳'], ['22', ':moon:', 'moon.gif', '28', '28', '28', '月亮'], ['23', ':kiss:', 'kiss.gif', '28', '28', '28', '示爱'], ['24', ':handshake:', 'handshake.gif', '28', '28', '28', '握手']];
  6. (function ($) {
  7. $.fn.smilies = function (options) {
  8. var settings = $.extend({}, {smilies_id: '_message'}, options);
  9. settings.smilies_div = "#" + settings.smilies_id + "_smilies_div";
  10. $(document).click(function () {
  11. $(settings.smilies_div).html('');
  12. $(settings.smilies_div).hide();
  13. });
  14. $(this).after('<div id="' + settings.smilies_id + '_smilies_div" class="smilies-module"></div>');
  15. $(settings.smilies_div).position({
  16. of: $("body"),
  17. at: "left bottom",
  18. offset: "10 10"
  19. });
  20. $(this).on('click', function () {
  21. //光标处插入代码功能
  22. smiliesshowdiv(this);
  23. return false;
  24. });
  25. //显示和隐藏表情模块
  26. function smiliesshowdiv(btnobj) {
  27. if ($(settings.smilies_div).css("display") == 'none') {
  28. if ($(settings.smilies_div).html() == '') {
  29. smilies_show(8, 'e_' + settings.smilies_id + '_');
  30. }
  31. $(settings.smilies_div).show();
  32. smiliesposition(btnobj);
  33. } else {
  34. $(settings.smilies_div).hide();
  35. }
  36. }
  37. //弹出层位置控制
  38. function smiliesposition(btnobj) {
  39. $(settings.smilies_div).position({
  40. of: btnobj,
  41. at: "left bottom",
  42. offset: "110 57"
  43. });
  44. }
  45. function smilies_show(smcols, seditorkey) {
  46. if (seditorkey && !$("#" + seditorkey + 'sml_menu')[0]) {
  47. var div = document.createElement("div");
  48. div.id = seditorkey + 'sml_menu';
  49. div.className = 'sllt';
  50. $(settings.smilies_div).append(div);
  51. div = document.createElement("div");
  52. div.id = '' + settings.smilies_id + '_smilies_content';
  53. div.style.overflow = 'hidden';
  54. $("#" + seditorkey + 'sml_menu').append(div);
  55. }
  56. smilies_onload(smcols, seditorkey);
  57. //image绑定操作函数
  58. $('#' + settings.smilies_id + '_smilies_content').find("td").bind('click', function () {
  59. insertsmilie(this);
  60. });
  61. }
  62. function insertsmilie(smilieone) {
  63. var code = $(smilieone).attr('codetext');
  64. insertAtCaret(code);
  65. $('#' + settings.smilies_id).focus();
  66. $(settings.smilies_div).html('');
  67. $(settings.smilies_div).hide();
  68. }
  69. function insertAtCaret(textFeildValue) {
  70. var textObj = $('#' + settings.smilies_id).get(0);
  71. if (document.all && textObj.createTextRange && textObj.caretPos) {
  72. var caretPos = textObj.caretPos;
  73. caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ?
  74. textFeildValue + '' : textFeildValue;
  75. }
  76. else if (textObj.setSelectionRange) {
  77. var rangeStart = textObj.selectionStart;
  78. var rangeEnd = textObj.selectionEnd;
  79. var tempStr1 = textObj.value.substring(0, rangeStart);
  80. var tempStr2 = textObj.value.substring(rangeEnd);
  81. textObj.value = tempStr1 + textFeildValue + tempStr2;
  82. textObj.focus();
  83. var len = textFeildValue.length;
  84. textObj.setSelectionRange(rangeStart + len, rangeStart + len);
  85. textObj.blur();
  86. }
  87. else {
  88. textObj.value += textFeildValue;
  89. }
  90. }
  91. function smilies_onload(smcols, seditorkey) {
  92. seditorkey = !seditorkey ? '' : seditorkey;
  93. $('#' + settings.smilies_id + '_smilies_content').html('<div id="' + settings.smilies_id + '_smilies_content_data"></div><div class="sllt-p" id="' + settings.smilies_id + '_smilies_content_page"></div>');
  94. smilies_switch(smcols, seditorkey);
  95. }
  96. function smilies_switch(smcols, seditorkey) {
  97. var page = 1;
  98. if (!smilies_array || !smilies_array[page])
  99. return;
  100. smiliesdata = '<table id="' + settings.smilies_id + '_smilies_content_table" cellpadding="0" cellspacing="0"><tr>';
  101. j = k = 0;
  102. img = [];
  103. for (i in smilies_array[page]) {
  104. if (j >= smcols) {
  105. smiliesdata += '<tr>';
  106. j = 0;
  107. }
  108. var s = smilies_array[page][i];
  109. smilieimg = BASESITEROOT + '/static/plugins/js/smilies/images/' + s[2];
  110. img[k] = new Image();
  111. img[k].src = smilieimg;
  112. smiliesdata += s && s[0] ? '<td id="' + seditorkey + 'smilie_' + s[0] + '_td" codetext="' + s[1] + '"><img id="' + seditorkey + 'smilie_' + s[0] + '" width="' + s[3] + '" height="' + s[4] + '" src="' + smilieimg + '" alt="' + s[1] + '" title="' + s[6] + '" />' : '<td>';
  113. j++;
  114. k++;
  115. }
  116. smiliesdata += '</table>';
  117. $('#' + settings.smilies_id + '_smilies_content_data').html(smiliesdata);
  118. }
  119. }
  120. })(jQuery);