at.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. [Discuz!] (C)2001-2099 Comsenz Inc.
  3. This is NOT a freeware, use is subject to license terms
  4. $Id: at.js 31619 2012-09-17 01:05:07Z monkey $
  5. */
  6. if(typeof EXTRAFUNC['keydown'] != "undefined") {
  7. EXTRAFUNC['keydown']['at'] = 'extrafunc_atMenu';
  8. EXTRAFUNC['keyup']['at'] = 'extrafunc_atMenuKeyUp';
  9. EXTRAFUNC['showEditorMenu']['at'] = 'extrafunc_atListMenu';
  10. }
  11. var atKeywords = null, keyMenuObj = null,atResult = [];
  12. var curatli = 0, atliclass = '', atsubmitid = '', atkeypress = 0;
  13. function extrafunc_atMenu() {
  14. if(BROWSER.opera) {
  15. return;
  16. }
  17. if(wysiwyg && EXTRAEVENT.shiftKey && EXTRAEVENT.keyCode == 50 && postaction && (postaction == 'newthread' || postaction == 'reply' || postaction == 'edit')) {
  18. keyMenu('@', atMenu);
  19. ctlent_enable[13] = 0;
  20. doane(EXTRAEVENT);
  21. atkeypress = 1;
  22. }
  23. if($('at_menu') && $('at_menu').style.display == '' && (EXTRAEVENT.keyCode == 38 || EXTRAEVENT.keyCode == 40 || EXTRAEVENT.keyCode == 13)) {
  24. doane(EXTRAEVENT);
  25. }
  26. }
  27. function extrafunc_atMenuKeyUp() {
  28. if(BROWSER.opera) {
  29. return;
  30. }
  31. if(wysiwyg && EXTRAEVENT.shiftKey && EXTRAEVENT.keyCode == 50 && postaction && (postaction == 'newthread' || postaction == 'reply' || postaction == 'edit') && !atkeypress) {
  32. keyBackspace();
  33. keyMenu('@', atMenu);
  34. ctlent_enable[13] = 0;
  35. doane(EXTRAEVENT);
  36. }
  37. if(wysiwyg && $('at_menu') && $('at_menu').style.display == '' && postaction && (postaction == 'newthread' || postaction == 'reply' || postaction == 'edit')) {
  38. if(EXTRAEVENT.keyCode == 32 || EXTRAEVENT.keyCode == 9 || EXTRAEVENT.keyCode == 8 && !keyMenuObj.innerHTML.substr(1).length) {
  39. $('at_menu').style.display = 'none';
  40. ctlent_enable[13] = 1;
  41. } else {
  42. atFilter(keyMenuObj.innerHTML.substr(1), 'at_menu', 'atMenuSet', EXTRAEVENT);
  43. }
  44. }
  45. atkeypress = 0;
  46. }
  47. function extrafunc_atListMenu(tag, op) {
  48. if(tag != 'at') {
  49. return false;
  50. }
  51. if(!op) {
  52. if($('at_menu')) {
  53. $('at_menu').style.display = 'none';
  54. ctlent_enable[13] = 1;
  55. }
  56. curatli = 0;
  57. setTimeout(function() {atFilter('', 'at_list','atListSet');$('atkeyword').focus();}, 100);
  58. return '请输用户名:<br /><input type="text" id="atkeyword" style="width:240px" value="" class="px" onkeydown="atEnter(event, \'atListSet\')" onkeyup="atFilter(this.value, \'at_list\',\'atListSet\',event, true);" /><div class="p_pop" id="at_list" style="width:250px;"><ul><li>@朋友账号,就能提醒他来看帖子</li></ul></div>';
  59. } else {
  60. if($('atkeyword').value) {
  61. str = '@' + $('atkeyword').value + (wysiwyg ? '&nbsp;' : ' ');
  62. insertText(str, strlen(str), 0, true, EXTRASEL);
  63. } else {
  64. insertText('', 0, 0, true, EXTRASEL);
  65. }
  66. checkFocus();
  67. return true;
  68. }
  69. }
  70. function atMenu(x, y) {
  71. if(!$('at_menu')) {
  72. div = document.createElement("div");
  73. div.id = "at_menu";
  74. document.body.appendChild(div);
  75. div.style.position = 'absolute';
  76. div.className = 'p_pop';
  77. div.style.zIndex = '100000';
  78. }
  79. $('at_menu').style.marginTop = (keyMenuObj.offsetHeight + 2) + 'px';
  80. $('at_menu').style.marginLeft = (keyMenuObj.offsetWidth + 2) + 'px';
  81. $('at_menu').style.left = x + 'px';
  82. $('at_menu').style.top = y + 'px';
  83. $('at_menu').style.display = '';
  84. $('at_menu').innerHTML = '<img src="' + IMGDIR + '/loading.gif" class="vm"> 请稍候... ';
  85. }
  86. function atSearch(kw, call) {
  87. if(atKeywords === null) {
  88. atKeywords = '';
  89. var x = new Ajax();
  90. x.get('misc.php?mod=getatuser&inajax=1', function(s) {
  91. if(s) {
  92. atKeywords = s.split(',');
  93. }
  94. if(call) {
  95. if(typeof call == 'function') {
  96. call();
  97. } else {
  98. eval(call);
  99. }
  100. }
  101. });
  102. }
  103. var lsi = 0;
  104. for(i in atKeywords) {
  105. if(atKeywords[i].indexOf(kw) !== -1 || kw === '') {
  106. atResult[lsi] = kw !== '' ? atKeywords[i].replace(kw, '<b>' + kw + '</b>') : atKeywords[i];
  107. lsi++;
  108. if(lsi > 10) {
  109. break;
  110. }
  111. }
  112. }
  113. if(kw && !lsi) {
  114. curatli = -1;
  115. }
  116. }
  117. function atEnter(e, call) {
  118. if(e) {
  119. if(e.keyCode == 38 && curatli > 0) {
  120. curatli--;
  121. return false;
  122. }
  123. if(e.keyCode == 40 && curatli < (atResult.length -1)) {
  124. curatli++;
  125. return false;
  126. }
  127. if(e.keyCode == 13) {
  128. var call = !call ? 'insertText' : call;
  129. if(curatli > -1) {
  130. eval(call+'($(\'atli_'+curatli+'\').innerText)');
  131. }
  132. hideMenu();
  133. doane(e);
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. function atFilter(kw, id, call, e, nae) {
  140. var nae = !nae ? false : nae;
  141. atResult = [];
  142. atSearch(kw, function () { atFilter(kw, id, call); });
  143. if(nae || !atEnter(e, call)) {
  144. var newlist = '';
  145. if(atResult.length) {
  146. $(id).style.visibility = 'visible';
  147. for(i in atResult) {
  148. var atclass = i == curatli ? ' class="a"' : '';
  149. newlist += '<li><a href="javascript:;" id="atli_'+i+'"'+atclass+' onclick="'+call+'(this.innerText)">' + atResult[i] + '</a></li>';
  150. }
  151. $(id).innerHTML = '<ul>' + newlist + '<li class="xg1">@朋友账号,就能提醒他来看帖子</li></ul>';
  152. } else {
  153. $(id).style.visibility = 'hidden';
  154. }
  155. }
  156. }
  157. function atListSet(kw) {
  158. $('atkeyword').value = kw;
  159. if(!atsubmitid) {
  160. $(editorid + '_at_submit').click();
  161. } else {
  162. $(atsubmitid).click();
  163. }
  164. }
  165. function atMenuSet(kw) {
  166. keyMenuObj.innerHTML = '@' + kw + (wysiwyg ? '&nbsp;' : ' ');
  167. $('at_menu').style.display = 'none';
  168. ctlent_enable[13] = 1;
  169. curatli = 0;
  170. if(BROWSER.firefox) {
  171. var selection = editwin.getSelection();
  172. var range = selection.getRangeAt(0);
  173. var tmp = keyMenuObj.firstChild;
  174. range.setStart(tmp, keyMenuObj.innerHTML.length - 5);
  175. range.setEnd(tmp, keyMenuObj.innerHTML.length - 5);
  176. selection.removeAllRanges();
  177. selection.addRange(range);
  178. }
  179. checkFocus();
  180. }