home_friendselector.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. [Discuz!] (C)2001-2099 Comsenz Inc.
  3. This is NOT a freeware, use is subject to license terms
  4. $Id: home_friendselector.js 26733 2011-12-21 07:18:01Z zhengqingpeng $
  5. */
  6. (function() {
  7. friendSelector = function(parameter) {
  8. this.dataSource = {};
  9. this.selectUser = {};
  10. this.prompterUser = [];
  11. this.showObj = $(isUndefined(parameter['showId']) ? 'selectorBox' : parameter['showId']);
  12. if(!this.showObj) return;
  13. this.handleObj = $(isUndefined(parameter['searchId']) ? 'valueId' : parameter['searchId']);
  14. this.showType = isUndefined(parameter['showType']) ? 0 : parameter['showType'];
  15. this.searchStr = null;
  16. this.selectNumber = 0;
  17. this.maxSelectNumber = isUndefined(parameter['maxSelectNumber']) ? 0 : parseInt(parameter['maxSelectNumber']);
  18. this.allNumber = 0;
  19. this.notInDataSourceNumber = 0;
  20. this.handleKey = isUndefined(parameter['handleKey']) ? 'this' : parameter['handleKey'];
  21. this.selectTabId = isUndefined(parameter['selectTabId']) ? 'selectTabId' : parameter['selectTabId'];
  22. this.unSelectTabId = isUndefined(parameter['unSelectTabId']) ? 'unSelectTabId' : parameter['unSelectTabId'];
  23. this.maxSelectTabId = isUndefined(parameter['maxSelectTabId']) ? 'maxSelectTabId' : parameter['maxSelectTabId'];
  24. this.formId = isUndefined(parameter['formId']) ? '' : parameter['formId'];
  25. this.filterUser = isUndefined(parameter['filterUser']) ? {} : parameter['filterUser'];
  26. this.showAll = true;
  27. this.newPMUser = {};
  28. this.interlaced = true;
  29. this.handover = true;
  30. this.parentKeyCode = 0;
  31. this.pmSelBoxState = 0;
  32. this.selBoxObj = isUndefined(parameter['selBox']) ? null : $(parameter['selBox']);
  33. this.containerBoxObj = isUndefined(parameter['selBoxMenu']) ? null : $(parameter['selBoxMenu']);
  34. this.imgBtn = null;
  35. this.initialize();
  36. return this;
  37. };
  38. friendSelector.prototype = {
  39. addDataSource : function(data, clear) {
  40. if(typeof data == 'object') {
  41. var userData = data['userdata'];
  42. clear = isUndefined(clear) ? 0: clear;
  43. if(clear) {
  44. this.showObj.innerHTML = "";
  45. if(this.showType == 3) {
  46. this.selBoxObj.innerHTML = '';
  47. }
  48. this.allNumber = 0;
  49. this.dataSource = {};
  50. }
  51. for(var i in userData) {
  52. if(typeof this.filterUser[i] != 'undefined') {
  53. continue;
  54. }
  55. var append = clear ? true : false;
  56. if(typeof this.dataSource[i] == 'undefined') {
  57. this.dataSource[i] = userData[i];
  58. append = true;
  59. this.allNumber++;
  60. }
  61. if(append) {
  62. this.interlaced = !this.interlaced;
  63. if(this.showType == 3) {
  64. this.append(i, 0, 1);
  65. } else {
  66. this.append(i);
  67. }
  68. }
  69. }
  70. if(this.showType == 1) {
  71. this.showSelectNumber();
  72. } else if(this.showType == 2) {
  73. if(this.newPMUser) {
  74. window.setInterval(this.handleKey+".handoverCSS()", 400);
  75. }
  76. }
  77. }
  78. },
  79. addFilterUser : function(data) {
  80. var filterData = {};
  81. if(typeof data != 'object') {
  82. filterData[data] = data;
  83. } else if(typeof data == 'object') {
  84. filterData = data;
  85. } else {
  86. return false;
  87. }
  88. for(var id in filterData) {
  89. this.filterUser[filterData[id]] = filterData[id];
  90. }
  91. return true;
  92. },
  93. handoverCSS : function() {
  94. for(var uid in this.newPMUser) {
  95. $('avt_'+uid).className = this.handover ? 'avt newpm' : 'avt';
  96. }
  97. this.handover = !this.handover;
  98. },
  99. handleEvent : function(key, event) {
  100. var username = '';
  101. this.searchStr = '';
  102. if(key != '') {
  103. if(event.keyCode == 188 || event.keyCode == 13 || event.keyCode == 59) {
  104. if(this.showType == 3) {
  105. if(event.keyCode == 13) {
  106. var currentnum = this.getCurrentPrompterUser();
  107. if(currentnum != -1) {
  108. key = this.dataSource[this.prompterUser[currentnum]]['username'];
  109. }
  110. }
  111. if(this.parentKeyCode != 229) {
  112. this.selectUserName(this.trim(key));
  113. this.showObj.style.display = 'none';
  114. $(this.handleObj.id+'_menu').style.display = 'none';
  115. this.showObj.innerHTML = "";
  116. }
  117. }
  118. } else if(event.keyCode == 38 || event.keyCode == 40) {
  119. } else {
  120. if(this.showType == 3) {
  121. this.showObj.innerHTML = "";
  122. var result = false;
  123. var reg = new RegExp(key, "ig");
  124. this.searchStr = key;
  125. this.prompterUser = [];
  126. for(var uid in this.dataSource) {
  127. username = this.dataSource[uid]['username'];
  128. if(username.match(reg)) {
  129. this.prompterUser.push(uid);
  130. this.append(uid, 1);
  131. result = true;
  132. }
  133. }
  134. if(!result) {
  135. $(this.handleObj.id+'_menu').style.display = 'none';
  136. } else {
  137. showMenu({'showid':this.showObj.id, 'duration':3, 'pos':'43'});
  138. showMenu({'showid':this.handleObj.id, 'duration':3, 'pos':'43'});
  139. }
  140. }
  141. }
  142. } else if(this.showType != 3) {
  143. this.showObj.innerHTML = "";
  144. for(var uid in this.dataSource) {
  145. this.append(uid);
  146. }
  147. } else {
  148. $(this.handleObj.id+'_menu').style.display = 'none';
  149. this.showObj.innerHTML = "";
  150. }
  151. },
  152. selectUserName:function(userName) {
  153. this.handleObj.value = '';
  154. if(userName != '') {
  155. var uid = this.isFriend(userName);
  156. if(uid && typeof this.selectUser[uid] == 'undefined' || uid === 0 && typeof this.selectUser[userName] == 'undefined') {
  157. var spanObj = document.createElement("span");
  158. if(uid) {
  159. this.selectUser[uid] = this.dataSource[uid];
  160. spanObj.id = 'uid' + uid;
  161. if($('chk'+uid) != null) {
  162. $('chk'+uid).checked = true;
  163. }
  164. } else {
  165. var id = 'str' + Math.floor(Math.random() * 10000);
  166. spanObj.id = id;
  167. this.selectUser[userName] = userName;
  168. }
  169. this.selectNumber++;
  170. spanObj.innerHTML= '<a href="javascript:;" class="x" onclick="'+this.handleKey+'.delSelUser(\''+(spanObj.id)+'\');">删除</a><em class="z" title="' + userName + '">' + userName + '</em><input type="hidden" name="users[]" value="'+userName+'" uid="uid'+uid+'" />';
  171. this.handleObj.parentNode.insertBefore(spanObj, this.handleObj);
  172. this.showObj.style.display = 'none';
  173. } else {
  174. alert('已经存在'+userName);
  175. }
  176. }
  177. },
  178. delSelUser:function(id) {
  179. id = isUndefined(id) ? 0 : id;
  180. var uid = id.substring(0, 3) == 'uid' ? parseInt(id.substring(3)) : 0;
  181. var spanObj;
  182. if(uid) {
  183. spanObj = $(id);
  184. delete this.selectUser[uid];
  185. if($('chk'+uid) != null) {
  186. $('chk'+uid).checked = false;
  187. }
  188. } else if(id.substring(0, 3) == 'str') {
  189. spanObj = $(id);
  190. delete this.selectUser[spanObj.getElementsByTagName('input')[0].value];
  191. }
  192. if(spanObj != null) {
  193. this.selectNumber--;
  194. spanObj.parentNode.removeChild(spanObj);
  195. }
  196. },
  197. trim:function(str) {
  198. return str.replace(/\s|,|;/g, '');
  199. },
  200. isFriend:function(userName) {
  201. var id = 0;
  202. for(var uid in this.dataSource) {
  203. if(this.dataSource[uid]['username'] === userName) {
  204. id = uid;
  205. break;
  206. }
  207. }
  208. return id;
  209. },
  210. directionKeyDown : function(event) {},
  211. clearDataSource : function() {
  212. this.dataSource = {};
  213. this.selectUser = {};
  214. },
  215. showUser : function(type) {
  216. this.showObj.innerHTML = '';
  217. type = isUndefined(type) ? 0 : parseInt(type);
  218. this.showAll = true;
  219. if(type == 1) {
  220. for(var uid in this.selectUser) {
  221. this.append(uid);
  222. }
  223. this.showAll = false;
  224. } else {
  225. for(var uid in this.dataSource) {
  226. if(type == 2) {
  227. if(typeof this.selectUser[uid] != 'undefined') {
  228. continue;
  229. }
  230. this.showAll = false;
  231. }
  232. this.append(uid);
  233. }
  234. }
  235. if(this.showType == 1) {
  236. for(var i = 0; i < 3; i++) {
  237. $('showUser_'+i).className = '';
  238. }
  239. $('showUser_'+type).className = 'a brs';
  240. }
  241. },
  242. append : function(uid, filtrate, form) {
  243. filtrate = isUndefined(filtrate) ? 0 : filtrate;
  244. form = isUndefined(form) ? 0 : form;
  245. var liObj = document.createElement("li");
  246. var appendUserData = this.dataSource[uid] || this.selectUser[uid];
  247. var username = appendUserData['username'];
  248. liObj.userid = appendUserData['uid'];
  249. if(typeof this.selectUser[uid] != 'undefined') {
  250. liObj.className = "a";
  251. }
  252. if(filtrate) {
  253. var reg = new RegExp("(" + this.searchStr + ")","ig");
  254. username = username.replace(reg , "<strong>$1</strong>");
  255. }
  256. if(this.showType == 1) {
  257. liObj.innerHTML = '<a href="javascript:;" id="' + liObj.userid + '" onclick="' + this.handleKey + '.select(this.id)" class="cl"><span class="avt brs" style="background-image: url(' + appendUserData['avatar'] + ');"><span></span></span><span class="d">' + username + '</span></a>';
  258. } else if(this.showType == 2) {
  259. if(appendUserData['new'] && typeof this.newPMUser[uid] == 'undefined') {
  260. this.newPMUser[uid] = uid;
  261. }
  262. liObj.className = this.interlaced ? 'alt' : '';
  263. liObj.innerHTML = '<div id="avt_' + liObj.userid + '" class="avt"><a href="home.php?mod=spacecp&ac=pm&op=showmsg&handlekey=showmsg_' + liObj.userid + '&touid=' + liObj.userid + '&pmid='+appendUserData['pmid']+'&daterange='+appendUserData['daterange']+'" title="'+username+'" id="avatarmsg_' + liObj.userid + '" onclick="'+this.handleKey+'.delNewFlag(' + liObj.userid + ');showWindow(\'showMsgBox\', this.href, \'get\', 0);"><img src="' + appendUserData['avatar'] + '" alt="'+username+'" /></a></div><p><a class="xg1" href="home.php?mod=spacecp&ac=pm&op=showmsg&handlekey=showmsg_' + liObj.userid + '&touid=' + liObj.userid + '&pmid='+appendUserData['pmid']+'&daterange='+appendUserData['daterange']+'" title="'+username+'" id="usernamemsg_' + liObj.userid + '" onclick="'+this.handleKey+'.delNewFlag(' + liObj.userid + ');showWindow(\'showMsgBox\', this.href, \'get\', 0);">'+username+'</a></p>';
  264. } else {
  265. if(form) {
  266. var checkstate = typeof this.selectUser[uid] == 'undefined' ? '' : ' checked="checked" ';
  267. liObj.innerHTML = '<label><input type="checkbox" name="selUsers[]" id="chk'+uid+'" value="'+ appendUserData['username'] +'" onclick="if(this.checked) {' + this.handleKey + '.selectUserName(this.value);} else {' + this.handleKey + '.delSelUser(\'uid'+uid+'\');}" '+checkstate+' class="pc" /> <span class="xi2">' + username + '</span></label>';
  268. this.selBoxObj.appendChild(liObj);
  269. return true;
  270. } else {
  271. liObj.innerHTML = '<a href="javascript:;" username="' + this.dataSource[uid]['username'] + '" onmouseover="' + this.handleKey + '.mouseOverPrompter(this);" onclick="' + this.handleKey + '.selectUserName(this.getAttribute(\'username\'));$(\'username\').focus();" class="cl" id="prompter_' + uid + '">' + username + '</a>';
  272. }
  273. }
  274. this.showObj.appendChild(liObj);
  275. },
  276. select : function(uid) {
  277. uid = parseInt(uid);
  278. if(uid){
  279. var select = false;
  280. if(typeof this.selectUser[uid] == 'undefined') {
  281. if(this.maxSelectNumber && this.selectNumber >= this.maxSelectNumber) {
  282. alert('最多只允许选择'+this.maxSelectNumber+'个用户');
  283. return false;
  284. }
  285. this.selectUser[uid] = this.dataSource[uid];
  286. this.selectNumber++;
  287. if(this.showType == '1') {
  288. $(uid).parentNode.className = 'a';
  289. }
  290. select = true;
  291. } else {
  292. delete this.selectUser[uid];
  293. this.selectNumber--;
  294. $(uid).parentNode.className = '';
  295. }
  296. if(this.formId != '') {
  297. var formObj = $(this.formId);
  298. var opId = 'selUids_' + uid;
  299. if(select) {
  300. var inputObj = document.createElement("input");
  301. inputObj.type = 'hidden';
  302. inputObj.id = opId;
  303. inputObj.name = 'uids[]';
  304. inputObj.value = uid;
  305. formObj.appendChild(inputObj);
  306. } else {
  307. formObj.removeChild($(opId));
  308. }
  309. }
  310. if(this.showType == 1) {
  311. this.showSelectNumber();
  312. }
  313. }
  314. },
  315. delNewFlag : function(uid) {
  316. delete this.newPMUser[uid];
  317. },
  318. showSelectNumber:function() {
  319. if($(this.selectTabId) != null && typeof $(this.selectTabId) != 'undefined') {
  320. $(this.selectTabId).innerHTML = this.selectNumber;
  321. }
  322. if($(this.unSelectTabId) != null && typeof $(this.unSelectTabId) != 'undefined') {
  323. this.notInDataSourceNumber = 0;
  324. for(var i in this.selectUser) {
  325. if(typeof this.dataSource[i] == 'undefined') {
  326. this.notInDataSourceNumber++;
  327. }
  328. }
  329. $(this.unSelectTabId).innerHTML = this.allNumber + this.notInDataSourceNumber - this.selectNumber;
  330. }
  331. if($(this.maxSelectTabId) != null && this.maxSelectNumber && typeof $(this.maxSelectTabId) != 'undefined') {
  332. $(this.maxSelectTabId).innerHTML = this.maxSelectNumber - this.selectNumber;
  333. }
  334. },
  335. getCurrentPrompterUser:function() {
  336. var len = this.prompterUser.length;
  337. var selectnum = -1;
  338. if(len) {
  339. for(var i = 0; i < len; i++) {
  340. var obj = $('prompter_' + this.prompterUser[i]);
  341. if(obj != null && obj.className == 'a') {
  342. selectnum = i;
  343. }
  344. }
  345. }
  346. return selectnum;
  347. },
  348. mouseOverPrompter:function(obj) {
  349. var len = this.prompterUser.length;
  350. if(len) {
  351. for(var i = 0; i < len; i++) {
  352. $('prompter_' + this.prompterUser[i]).className = 'cl';
  353. }
  354. obj.className = 'a';
  355. }
  356. },
  357. initialize:function() {
  358. var instance = this;
  359. this.handleObj.onkeyup = function(event) {
  360. event = event ? event : window.event;
  361. instance.handleEvent(this.value, event);
  362. };
  363. if(this.showType == 3) {
  364. this.handleObj.onkeydown = function(event) {
  365. event = event ? event : window.event;
  366. instance.parentKeyCode = event.keyCode;
  367. instance.showObj.style.display = '';
  368. if(event.keyCode == 8 && this.value == '') {
  369. var preNode = this.previousSibling;
  370. if(preNode.tagName == 'SPAN') {
  371. var uid = preNode.getElementsByTagName('input')[0].getAttribute('uid');
  372. if(parseInt(uid.substring(3))) {
  373. instance.delSelUser(uid);
  374. } else {
  375. delete instance.selectUser[preNode.getElementsByTagName('input')[0].value];
  376. instance.selectNumber--;
  377. this.parentNode.removeChild(preNode);
  378. }
  379. }
  380. } else if(event.keyCode == 38) {
  381. if(!instance.prompterUser.length) {
  382. doane(event);
  383. }
  384. var currentnum = instance.getCurrentPrompterUser();
  385. if(currentnum != -1) {
  386. var nextnum = (currentnum == 0) ? (instance.prompterUser.length-1) : currentnum - 1;
  387. $('prompter_' + instance.prompterUser[currentnum]).className = "cl";
  388. $('prompter_' + instance.prompterUser[nextnum]).className = "a";
  389. } else {
  390. $('prompter_' + instance.prompterUser[0]).className = "a";
  391. }
  392. } else if(event.keyCode == 40) {
  393. if(!instance.prompterUser.length) {
  394. doane(event);
  395. }
  396. var currentnum = instance.getCurrentPrompterUser();
  397. if(currentnum != -1) {
  398. var nextnum = (currentnum == (instance.prompterUser.length - 1)) ? 0 : currentnum + 1;
  399. $('prompter_' + instance.prompterUser[currentnum]).className = "cl";
  400. $('prompter_' + instance.prompterUser[nextnum]).className = "a";
  401. } else {
  402. $('prompter_' + instance.prompterUser[0]).className = "a";
  403. }
  404. } else if(event.keyCode == 13) {
  405. doane(event);
  406. }
  407. if(typeof instance != "undefined" && instance.pmSelBoxState) {
  408. instance.pmSelBoxState = 0;
  409. instance.changePMBoxImg(instance.imgBtn);
  410. instance.containerBoxObj.style.display = 'none';
  411. }
  412. };
  413. }
  414. },
  415. changePMBoxImg:function(obj) {
  416. var btnImg = new Image();
  417. btnImg.src = IMGDIR + '/' + (this.pmSelBoxState ? 'icon_top.gif' : 'icon_down.gif');
  418. if(obj != null) {
  419. obj.src = btnImg.src;
  420. }
  421. },
  422. showPMFriend:function(boxId, listId, obj) {
  423. this.pmSelBoxState = !this.pmSelBoxState;
  424. this.imgBtn = obj;
  425. this.changePMBoxImg(obj);
  426. if(this.pmSelBoxState) {
  427. this.selBoxObj.innerHTML = '';
  428. for(var uid in this.dataSource) {
  429. this.append(uid, 0, 1);
  430. }
  431. }
  432. this.containerBoxObj.style.display = this.pmSelBoxState ? '' : 'none';
  433. this.showObj.innerHTML = "";
  434. },
  435. showPMBoxUser:function() {
  436. this.selBoxObj.innerHTML = '';
  437. for(var uid in this.dataSource) {
  438. this.append(uid, 0, 1);
  439. }
  440. },
  441. extend:function (obj) {
  442. for (var i in obj) {
  443. this[i] = obj[i];
  444. }
  445. }
  446. };
  447. })();