Person.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. MWF.xApplication.Organization = MWF.xApplication.Organization || {};
  2. MWF.xApplication.Organization.Selector = MWF.xApplication.Organization.Selector || {};
  3. MWF.xDesktop.requireApp("Organization", "lp.zh-cn", null, false);
  4. MWF.xDesktop.requireApp("Organization", "Actions.RestActions", null, false);
  5. MWF.xApplication.Organization.Selector.Person = new Class({
  6. Extends: MWF.widget.Common,
  7. Implements: [Options, Events],
  8. options: {
  9. "style": "default",
  10. "count": 0,
  11. "title": "Select Person",
  12. "groups": [],
  13. "roles": [],
  14. "values": [],
  15. "names": [],
  16. "zIndex": 1000,
  17. "expand": true
  18. },
  19. initialize: function(container, options){
  20. this.setOptions(options);
  21. this.path = "/x_component_Organization/Selector/$Selector/";
  22. this.cssPath = "/x_component_Organization/Selector/$Selector/"+this.options.style+"/css.wcss";
  23. this._loadCss();
  24. this.container = $(container);
  25. this.action = new MWF.xApplication.Organization.Actions.RestActions();
  26. this.lastPeople = "";
  27. this.pageCount = "13";
  28. this.selectedItems = [];
  29. this.items = [];
  30. },
  31. load: function(){
  32. if (layout.mobile){
  33. this.loadMobile();
  34. }else{
  35. this.loadPc();
  36. }
  37. this.fireEvent("load");
  38. },
  39. loadMobile: function(){
  40. this.container.mask({
  41. "destroyOnHide": true,
  42. "style": this.css.maskNode
  43. });
  44. this.node = new Element("div", {"styles": this.css.containerNodeMobile});
  45. this.node.setStyle("z-index", this.options.zIndex.toInt()+1);
  46. this.titleNode = new Element("div", {
  47. "styles": this.css.titleNodeMobile
  48. }).inject(this.node);
  49. this.titleCancelActionNode = new Element("div", {
  50. "styles": this.css.titleCancelActionNodeMobile,
  51. "text": MWF.xApplication.Organization.LP.back
  52. }).inject(this.titleNode);
  53. this.titleOkActionNode = new Element("div", {
  54. "styles": this.css.titleOkActionNodeMobile,
  55. "text": MWF.xApplication.Organization.LP.ok
  56. }).inject(this.titleNode);
  57. this.titleTextNode = new Element("div", {
  58. "styles": this.css.titleTextNodeMobile,
  59. "text": this.options.title
  60. }).inject(this.titleNode);
  61. this.contentNode = new Element("div", {
  62. "styles": this.css.contentNode
  63. }).inject(this.node);
  64. var size = this.container.getSize();
  65. var height = size.y-40;
  66. this.contentNode.setStyle("height", ""+height+"px");
  67. this.loadContent();
  68. this.node.inject(this.container);
  69. this.node.setStyles({
  70. "top": "0px",
  71. "left": "0px"
  72. });
  73. this.setEvent();
  74. },
  75. loadPc: function(){
  76. this.css.maskNode["z-index"] = this.options.zIndex;
  77. this.container.mask({
  78. "destroyOnHide": true,
  79. "style": this.css.maskNode
  80. });
  81. // this.container.setStyle("z-index", this.options.zIndex);
  82. this.node = new Element("div", {
  83. "styles": (this.options.count.toInt()==1) ? this.css.containerNodeSingle : this.css.containerNode
  84. });
  85. this.node.setStyle("z-index", this.options.zIndex.toInt()+1);
  86. this.titleNode = new Element("div", {
  87. "styles": this.css.titleNode
  88. }).inject(this.node);
  89. this.titleActionNode = new Element("div", {
  90. "styles": this.css.titleActionNode
  91. }).inject(this.titleNode);
  92. this.titleTextNode = new Element("div", {
  93. "styles": this.css.titleTextNode,
  94. "text": this.options.title
  95. }).inject(this.titleNode);
  96. this.contentNode = new Element("div", {
  97. "styles": this.css.contentNode
  98. }).inject(this.node);
  99. this.loadContent();
  100. this.actionNode = new Element("div", {
  101. "styles": this.css.actionNode
  102. }).inject(this.node);
  103. if (this.options.count.toInt()==1) this.actionNode.setStyle("text-align", "center");
  104. this.loadAction();
  105. this.node.inject(this.container);
  106. this.node.position({
  107. relativeTo: this.container,
  108. position: "center",
  109. edge: "center"
  110. });
  111. var size = this.container.getSize();
  112. var nodeSize = this.node.getSize();
  113. this.node.makeDraggable({
  114. "handle": this.titleNode,
  115. "limit": {
  116. "x": [0, size.x-nodeSize.x],
  117. "y": [0, size.y-nodeSize.y]
  118. }
  119. });
  120. this.setEvent();
  121. },
  122. setEvent: function(){
  123. if (this.titleActionNode){
  124. this.titleActionNode.addEvent("click", function(){
  125. this.close();
  126. }.bind(this));
  127. }
  128. if (this.titleCancelActionNode){
  129. this.titleCancelActionNode.addEvent("click", function(){
  130. this.close();
  131. }.bind(this));
  132. }
  133. if (this.titleOkActionNode){
  134. this.titleOkActionNode.addEvent("click", function(){
  135. this.fireEvent("complete", [this.selectedItems]);
  136. this.close();
  137. }.bind(this));
  138. }
  139. },
  140. close: function(){
  141. this.fireEvent("close");
  142. this.node.destroy();
  143. this.container.unmask();
  144. MWF.release(this);
  145. delete this;
  146. },
  147. loadAction: function(){
  148. this.okActionNode = new Element("button", {
  149. "styles": this.css.okActionNode,
  150. "text": "确 定"
  151. }).inject(this.actionNode);
  152. this.cancelActionNode = new Element("button", {
  153. "styles": this.css.cancelActionNode,
  154. "text": "取 消"
  155. }).inject(this.actionNode);
  156. this.okActionNode.addEvent("click", function(){
  157. this.fireEvent("complete", [this.selectedItems]);
  158. this.close();
  159. }.bind(this));
  160. this.cancelActionNode.addEvent("click", function(){this.fireEvent("cancel"); this.close();}.bind(this));
  161. },
  162. loadContent: function( contentNode ){
  163. if( contentNode )this.contentNode = contentNode;
  164. if (layout.mobile){
  165. if (this.options.count.toInt()!=1) this.loadSelectedNodeMobile();
  166. this.loadSelectNodeMobile();
  167. }else{
  168. if (this.options.count.toInt()!=1) this.loadSelectedNode();
  169. this.loadSelectNode();
  170. }
  171. },
  172. loadSelectNodeMobile: function(){
  173. this.selectNode = new Element("div", {
  174. "styles": this.css.selectNodeMobile
  175. }).inject(this.contentNode);
  176. var size = this.container.getSize();
  177. var height = size.y-40-20-6;
  178. this.selectNode.setStyle("height", ""+height+"px");
  179. this.searchInputDiv = new Element("div", {
  180. "styles": this.css.searchInputDiv
  181. }).inject(this.selectNode);
  182. this.searchInput = new Element("input", {
  183. "styles": (this.options.count.toInt()==1) ? this.css.searchInputSingle : this.css.searchInput,
  184. "type": "text"
  185. }).inject(this.searchInputDiv);
  186. var width = size.x-20-18;
  187. this.searchInput.setStyle("width", ""+width+"px");
  188. this.searchInput.setStyle("height", "20px");
  189. this.initSearchInput();
  190. this.letterAreaNode = new Element("div", {
  191. "styles": this.css.letterAreaNode
  192. }).inject(this.selectNode);
  193. width = size.x-20-4-18;
  194. this.letterAreaNode.setStyle("width", ""+width+"px");
  195. this.loadLetters();
  196. this.itemAreaScrollNode = new Element("div", {
  197. "styles": this.css.itemAreaScrollNode
  198. }).inject(this.selectNode);
  199. height = size.y-40-20-78;
  200. this.itemAreaScrollNode.setStyle("height", ""+height+"px");
  201. this.itemAreaScrollNode.setStyle("overflow", "auto");
  202. this.itemAreaNode = new Element("div", {
  203. "styles": this.css.itemAreaNode
  204. }).inject(this.itemAreaScrollNode);
  205. this.itemSearchAreaNode = new Element("div", {
  206. "styles": this.css.itemAreaNode
  207. }).inject(this.itemAreaScrollNode);
  208. this.itemSearchAreaNode.setStyle("display", "none");
  209. //MWF.require("MWF.widget.ScrollBar", function(){
  210. // var _self = this;
  211. // new MWF.widget.ScrollBar(this.itemAreaScrollNode, {
  212. // "style":"xApp_Organization_Explorer",
  213. // "where": "before",
  214. // "distance": 30,
  215. // "friction": 4,
  216. // "axis": {"x": false, "y": true},
  217. // "onScroll": function(y){
  218. // _self._scrollEvent(y);
  219. // }
  220. // });
  221. //}.bind(this));
  222. this.initLoadSelectItems();
  223. if (!this.options.groups.length && !this.options.roles.length){
  224. this.loadSelectItems();
  225. }else{
  226. this.loadSelectItemsByCondition();
  227. }
  228. },
  229. loadSelectNode: function(){
  230. this.selectNode = new Element("div", {
  231. "styles": (this.options.count.toInt()==1) ? this.css.selectNodeSingle : this.css.selectNode
  232. }).inject(this.contentNode);
  233. this.searchInputDiv = new Element("div", {
  234. "styles": this.css.searchInputDiv
  235. }).inject(this.selectNode);
  236. this.searchInput = new Element("input", {
  237. "styles": (this.options.count.toInt()==1) ? this.css.searchInputSingle : this.css.searchInput,
  238. "type": "text"
  239. }).inject(this.searchInputDiv);
  240. this.initSearchInput();
  241. this.letterAreaNode = new Element("div", {
  242. "styles": this.css.letterAreaNode
  243. }).inject(this.selectNode);
  244. this.loadLetters();
  245. this.itemAreaScrollNode = new Element("div", {
  246. "styles": this.css.itemAreaScrollNode
  247. }).inject(this.selectNode);
  248. this.itemAreaNode = new Element("div", {
  249. "styles": this.css.itemAreaNode
  250. }).inject(this.itemAreaScrollNode);
  251. this.itemSearchAreaNode = new Element("div", {
  252. "styles": this.css.itemAreaNode
  253. }).inject(this.itemAreaScrollNode);
  254. this.itemSearchAreaNode.setStyle("display", "none");
  255. MWF.require("MWF.widget.ScrollBar", function(){
  256. var _self = this;
  257. new MWF.widget.ScrollBar(this.itemAreaScrollNode, {
  258. "style":"xApp_Organization_Explorer",
  259. "where": "before",
  260. "distance": 30,
  261. "friction": 4,
  262. "axis": {"x": false, "y": true},
  263. "onScroll": function(y){
  264. _self._scrollEvent(y);
  265. }
  266. });
  267. }.bind(this));
  268. this.initLoadSelectItems();
  269. if (!this.options.groups.length && !this.options.roles.length){
  270. this.loadSelectItems();
  271. }else{
  272. this.loadSelectItemsByCondition();
  273. }
  274. },
  275. initSearchInput: function(){
  276. this.searchInput.addEvents({
  277. "keydown": function(e){
  278. var iTimerID = this.searchInput.retrieve("searchTimer", null);
  279. if (iTimerID){
  280. window.clearTimeout(iTimerID);
  281. this.searchInput.eliminate("searchTimer");
  282. }
  283. iTimerID = window.setTimeout(function(){
  284. this.search();
  285. }.bind(this), 800);
  286. this.searchInput.store("searchTimer", iTimerID);
  287. }.bind(this),
  288. "change": function(e){
  289. var key = this.searchInput.get("value");
  290. if (!key) this.initSearchArea(false);
  291. }.bind(this),
  292. "blur": function(){
  293. var key = this.searchInput.get("value");
  294. if (!key) this.initSearchArea(false);
  295. }.bind(this)
  296. });
  297. },
  298. initSearchArea: function(flag){
  299. if (flag){
  300. this.itemSearchAreaNode.empty();
  301. this.itemAreaNode.setStyle("display", "none");
  302. this.itemSearchAreaNode.setStyle("display", "block");
  303. }else{
  304. this.itemAreaNode.setStyle("display", "block");
  305. this.itemSearchAreaNode.setStyle("display", "none");
  306. }
  307. },
  308. search: function(){
  309. if (!this.options.groups.length && !this.options.roles.length){
  310. var key = this.searchInput.get("value");
  311. if (key){
  312. this._listItemByKey(function(json){
  313. this.initSearchArea(true);
  314. json.data.each(function(data){
  315. var flag = true;
  316. if (this.options.departments){
  317. if (this.options.departments.length){
  318. if (this.options.departments.indexOf(data.departmentName)==-1) flag = false;
  319. }
  320. }
  321. if (this.options.companys){
  322. if (this.options.companys.length){
  323. if (this.options.companys.indexOf(data.company)==-1) flag = false;
  324. }
  325. }
  326. if (flag) this._newItem(data, this, this.itemSearchAreaNode);
  327. //this._newItem(data, this, this.itemSearchAreaNode);
  328. }.bind(this));
  329. }.bind(this), null, key);
  330. }else{
  331. this.initSearchArea(false);
  332. }
  333. }else{
  334. var key = this.searchInput.get("value");
  335. if (key){
  336. this.initSearchArea(true);
  337. this.searchInItems(key);
  338. }else{
  339. this.initSearchArea(false);
  340. }
  341. }
  342. },
  343. searchInItems: function(key){
  344. this.createItemsSearchData(function(){
  345. var word = key.toLowerCase();
  346. var createdId = [];
  347. this.itemsSearchData.each(function(obj){
  348. var text = obj.text+"#"+obj.pinyin+"#"+obj.firstPY;
  349. if (text.indexOf(word)!=-1){
  350. if (createdId.indexOf(obj.data.id)==-1){
  351. this._newItem(obj.data, this, this.itemSearchAreaNode);
  352. createdId.push(obj.data.id);
  353. }
  354. }
  355. }.bind(this));
  356. //this.searchItemsData(this.itemsSearchData.name, word, createdId);
  357. //this.searchItemsData(this.itemsSearchData.pinyin, word, createdId);
  358. //this.searchItemsData(this.itemsSearchData.firstPY, word, createdId);
  359. delete createdId;
  360. }.bind(this));
  361. },
  362. createItemsSearchData: function(callback){
  363. if (!this.itemsSearchData){
  364. this.itemsSearchData = [];
  365. MWF.require("MWF.widget.PinYin", function(){
  366. var initIds = [];
  367. this.items.each(function(item){
  368. if (initIds.indexOf(item.data.id)==-1){
  369. var text = item._getShowName().toLowerCase();
  370. var pinyin = text.toPY().toLowerCase();
  371. var firstPY = text.toPYFirst().toLowerCase();
  372. this.itemsSearchData.push({
  373. "text": text,
  374. "pinyin": pinyin,
  375. "firstPY": firstPY,
  376. "data": item.data
  377. });
  378. initIds.push(item.data.id);
  379. }
  380. }.bind(this));
  381. delete initIds;
  382. if (callback) callback();
  383. }.bind(this));
  384. }else{
  385. if (callback) callback();
  386. }
  387. },
  388. loadSelectedNode: function(){
  389. this.selectedContainerNode = new Element("div", {
  390. "styles": this.css.selectedContainerNode
  391. }).inject(this.contentNode);
  392. this.selectedScrollNode = new Element("div", {
  393. "styles": this.css.selectedScrollNode
  394. }).inject(this.selectedContainerNode);
  395. this.selectedNode = new Element("div", {
  396. "styles": this.css.selectedNode
  397. }).inject(this.selectedScrollNode);
  398. this.setSelectedItem();
  399. MWF.require("MWF.widget.ScrollBar", function(){
  400. var _self = this;
  401. new MWF.widget.ScrollBar(this.selectedScrollNode, {
  402. "style":"xApp_Organization_Explorer", "where": "before", "distance": 100, "friction": 4,"axis": {"x": false, "y": true}
  403. });
  404. }.bind(this));
  405. },
  406. loadSelectedNodeMobile: function(){
  407. this.selectedScrollNode = new Element("div", {
  408. "styles": this.css.selectedScrollNode
  409. }).inject(this.contentNode);
  410. this.selectedNode = new Element("div", {
  411. "styles": this.css.selectedNode
  412. }).inject(this.selectedScrollNode);
  413. this.setSelectedItem();
  414. MWF.require("MWF.widget.ScrollBar", function(){
  415. var _self = this;
  416. new MWF.widget.ScrollBar(this.selectedScrollNode, {
  417. "style":"xApp_Organization_Explorer", "where": "before", "distance": 100, "friction": 4,"axis": {"x": false, "y": true}
  418. });
  419. }.bind(this));
  420. this.selectedScrollNode.setStyle("display", "none");
  421. },
  422. setSelectedItem: function(){
  423. if (this.options.values.length){
  424. this.options.values.each(function(v){
  425. this._getItem(function(json){
  426. this.selectedItems.push(this._newItemSelected(json.data, this, null));
  427. }.bind(this), null, v, false);
  428. }.bind(this));
  429. }else if (this.options.names.length){
  430. this.options.names.each(function(v){
  431. if (v){
  432. this._listItemByKey(function(json){
  433. var data = "";
  434. if (json.data.length) data = json.data[0];
  435. if (data){
  436. this.selectedItems.push(this._newItemSelected(data, this, null));
  437. }
  438. }.bind(this), null, v);
  439. }
  440. }.bind(this));
  441. }
  442. },
  443. loadLetters: function(){
  444. var _self = this;
  445. letters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
  446. if (layout.mobile){
  447. var size = this.container.getSize();
  448. var w = (size.x-20-4-18)/13;
  449. //letterNode.setStyle("width", ""+w+"px");
  450. this.css.letterNode.width = ""+w+"px";
  451. this.css.letterNode_over.width = ""+w+"px";
  452. }
  453. letters.each(function(l){
  454. var letterNode = new Element("div", {
  455. "styles": this.css.letterNode,
  456. "text": l
  457. }).inject(this.letterAreaNode);
  458. letterNode.addEvents({
  459. "mouseover": function(e){
  460. e.target.setStyles(this.css.letterNode_over);
  461. var showNode = new Element("div", {
  462. "styles": this.css.letterShowNode,
  463. "text": e.target.get("text"),
  464. }).inject(this.selectNode);
  465. showNode.position({
  466. relativeTo: this.itemAreaScrollNode,
  467. position: "center",
  468. edge: "center"
  469. });
  470. e.target.store("showNode", showNode);
  471. }.bind(this),
  472. "mouseout": function(e){
  473. var showNode = e.target.retrieve("showNode");
  474. showNode.destroy();
  475. e.target.setStyles(this.css.letterNode);
  476. }.bind(this),
  477. "click": function(){
  478. _self.listPersonByPinyin(this);
  479. }
  480. });
  481. }.bind(this));
  482. },
  483. listPersonByPinyin: function(node){
  484. this.searchInput.focus();
  485. var pinyin = this.searchInput.get("value");
  486. pinyin = pinyin+node.get("text");
  487. this.searchInput.set("value", pinyin);
  488. if (!this.options.groups.length && !this.options.roles.length){
  489. if (pinyin){
  490. this._listItemByPinyin(function(json){
  491. this.initSearchArea(true);
  492. json.data.each(function(data){
  493. var flag = true;
  494. if (this.options.departments){
  495. if (this.options.departments.length){
  496. if (this.options.departments.indexOf(data.departmentName)==-1) flag = false;
  497. }
  498. }
  499. if (this.options.companys){
  500. if (this.options.companys.length){
  501. if (this.options.companys.indexOf(data.company)==-1) flag = false;
  502. }
  503. }
  504. if (flag) this._newItem(data, this, this.itemSearchAreaNode);
  505. //this._newItem(data, this, this.itemSearchAreaNode);
  506. }.bind(this));
  507. }.bind(this), null, pinyin.toLowerCase());
  508. }
  509. }else{
  510. if (pinyin){
  511. this.initSearchArea(true);
  512. this.searchInItems(pinyin);
  513. }else{
  514. this.initSearchArea(false);
  515. }
  516. }
  517. },
  518. initLoadSelectItems: function(){
  519. this.loaddingItems = false;
  520. this.isItemLoaded = false;
  521. this.loadItemsQueue = 0;
  522. this.initSearchArea(false);
  523. },
  524. loadSelectItems: function(addToNext){
  525. if (!this.isItemLoaded){
  526. if (!this.loaddingItems){
  527. this.loaddingItems = true;
  528. var count = 20;
  529. this._listItemNext(this.getLastLoadedItemId(), count, function(json){
  530. if (json.data.length){
  531. json.data.each(function(data){
  532. var item = this._newItem(data, this, this.itemAreaNode);
  533. this.items.push(item);
  534. }.bind(this));
  535. this.loaddingItems = false;
  536. if (json.data.length<count){
  537. this.isItemLoaded = true;
  538. }else{
  539. if (this.loadItemsQueue>0){
  540. this.loadItemsQueue--;
  541. this.loadSelectItems();
  542. }
  543. }
  544. }else{
  545. this.isItemLoaded = true;
  546. this.loaddingItems = false;
  547. }
  548. }.bind(this));
  549. }else{
  550. if (addToNext) this.loadItemsQueue++;
  551. }
  552. }
  553. },
  554. getLastLoadedItemId: function(){
  555. return (this.items.length) ? this.items[this.items.length-1].data.id : "(0)";
  556. },
  557. loadSelectItemsByCondition: function(){
  558. this.options.groups.each(function(group){
  559. this.action.listGroupByKey(function(json){
  560. if (json.data.length){
  561. var groupData = json.data[0]
  562. var category = this._newItemCategory("ItemGroupCategory", groupData, this, this.itemAreaNode);
  563. this._getChildrenItemIds(groupData).each(function(id){
  564. this._getItem(function(json){
  565. var item = this._newItem(json.data, this, category.children);
  566. this.items.push(item);
  567. }.bind(this), null, id)
  568. }.bind(this));
  569. }
  570. }.bind(this), null, group);
  571. }.bind(this));
  572. this.options.roles.each(function(role){
  573. this.action.listRoleByKey(function(json){
  574. if (json.data.length){
  575. var roleData = json.data[0]
  576. var category = this._newItemCategory("ItemRoleCategory", roleData, this, this.itemAreaNode);
  577. this._getChildrenItemIds(roleData).each(function(id){
  578. this._getItem(function(json){
  579. var item = this._newItem(json.data, this, category.children);
  580. this.items.push(item);
  581. }.bind(this), null, id)
  582. }.bind(this));
  583. }
  584. }.bind(this), null, role);
  585. }.bind(this));
  586. },
  587. _getChildrenItemIds: function(data){
  588. return data.personList;
  589. },
  590. _newItemCategory: function(type, data, selector, item){
  591. return new MWF.xApplication.Organization.Selector.Person[type](data, selector, item)
  592. },
  593. _listItemByKey: function(callback, failure, key){
  594. this.action.listPersonByKey(function(json){
  595. if (callback) callback.apply(this, [json]);
  596. }.bind(this), failure, key);
  597. },
  598. _getItem: function(callback, failure, id, async){
  599. this.action.getPerson(function(json){
  600. if (callback) callback.apply(this, [json]);
  601. }.bind(this), failure, id, async);
  602. },
  603. _newItemSelected: function(data, selector, item){
  604. return new MWF.xApplication.Organization.Selector.Person.ItemSelected(data, selector, item)
  605. },
  606. _listItemByPinyin: function(callback, failure, key){
  607. this.action.listPersonByPinyin(function(json){
  608. if (callback) callback.apply(this, [json]);
  609. }.bind(this), failure, key);
  610. },
  611. _newItem: function(data, selector, container){
  612. return new MWF.xApplication.Organization.Selector.Person.Item(data, selector, container);
  613. },
  614. _listItemNext: function(last, count, callback){
  615. this.action.listPersonNext(last, count, function(json){
  616. if (callback) callback.apply(this, [json]);
  617. }.bind(this));
  618. },
  619. _scrollEvent: function(y){
  620. if (!this.options.groups.length && !this.options.roles.length){
  621. var scrollSize = this.itemAreaScrollNode.getScrollSize();
  622. var clientSize = this.itemAreaScrollNode.getSize();
  623. var scrollHeight = scrollSize.y-clientSize.y;
  624. if (y+30>scrollHeight) {
  625. if (!this.isItemLoaded) this.loadSelectItems();
  626. }
  627. }
  628. }
  629. });
  630. MWF.xApplication.Organization.Selector.Person.Item = new Class({
  631. initialize: function(data, selector, container, level){
  632. this.data = data;
  633. this.selector = selector;
  634. this.container = container;
  635. this.isSelected = false;
  636. this.level = (level) ? level.toInt() : 1;
  637. this.load();
  638. },
  639. _getShowName: function(){
  640. return this.data.display+"("+this.data.employee+")";
  641. },
  642. _setIcon: function(){
  643. this.iconNode.setStyle("background-image", "url("+"/x_component_Organization/Selector/$Selector/default/icon/personicon.png)");
  644. },
  645. load: function(){
  646. this.node = new Element("div", {
  647. "styles": this.selector.css.selectorItem
  648. }).inject(this.container);
  649. this.levelNode = new Element("div", {
  650. "styles": this.selector.css.selectorItemLevelNode
  651. }).inject(this.node);
  652. var indent = this.level*10;
  653. this.levelNode.setStyle("width", ""+indent+"px");
  654. this.iconNode = new Element("div", {
  655. "styles": this.selector.css.selectorItemIconNode
  656. }).inject(this.node);
  657. this._setIcon();
  658. this.actionNode = new Element("div", {
  659. "styles": this.selector.css.selectorItemActionNode
  660. }).inject(this.node);
  661. this.textNode = new Element("div", {
  662. "styles": this.selector.css.selectorItemTextNode,
  663. "text": this._getShowName()
  664. }).inject(this.node);
  665. var m = this.textNode.getStyle("margin-left").toFloat()+indent;
  666. this.textNode.setStyle("margin-left", ""+m+"px");
  667. this.loadSubItem();
  668. this.setEvent();
  669. this.check();
  670. },
  671. loadSubItem: function(){},
  672. check: function(){
  673. if (this.selector.options.count.toInt()==1){
  674. this.checkSelectedSingle();
  675. }else{
  676. this.checkSelected();
  677. }
  678. },
  679. checkSelectedSingle: function(){
  680. var selectedItem = this.selector.options.values.filter(function(item, index){
  681. return this.data.id == item;
  682. }.bind(this));
  683. if (selectedItem.length){
  684. this.selectedSingle();
  685. }
  686. },
  687. checkSelected: function(){
  688. var selectedItem = this.selector.selectedItems.filter(function(item, index){
  689. return item.data.id == this.data.id;
  690. }.bind(this));
  691. if (selectedItem.length){
  692. //selectedItem[0].item = this;
  693. selectedItem[0].addItem(this);
  694. this.selectedItem = selectedItem[0];
  695. this.setSelected();
  696. }
  697. },
  698. setSelected: function(){
  699. this.isSelected = true;
  700. this.node.setStyles(this.selector.css.selectorItem_selected);
  701. this.textNode.setStyles(this.selector.css.selectorItemTextNode_selected);
  702. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_selected);
  703. },
  704. setEvent: function(){
  705. this.node.addEvents({
  706. "mouseover": function(){
  707. this.overItem();
  708. }.bind(this),
  709. "mouseout": function(){
  710. this.outItem();
  711. }.bind(this),
  712. "click": function(){
  713. this.clickItem();
  714. }.bind(this)
  715. });
  716. },
  717. clickItem: function(){
  718. if (this.selector.options.count.toInt()==1){
  719. this.selectedSingle();
  720. }else{
  721. if (this.isSelected){
  722. this.unSelected();
  723. }else{
  724. this.selected();
  725. }
  726. }
  727. },
  728. overItem: function(){
  729. if (!this.isSelected){
  730. this.node.setStyles(this.selector.css.selectorItem_over);
  731. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_over);
  732. }
  733. },
  734. outItem: function(){
  735. if (!this.isSelected){
  736. this.node.setStyles(this.selector.css.selectorItem);
  737. this.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  738. }
  739. },
  740. selectedSingle: function(){
  741. if (!this.isSelected){
  742. if (this.selector.currentItem) this.selector.currentItem.unSelectedSingle();
  743. this.selector.currentItem = this;
  744. this.isSelected = true;
  745. this.selector.selectedItems.push(this);
  746. this.node.setStyles(this.selector.css.selectorItem_selected);
  747. this.textNode.setStyles(this.selector.css.selectorItemTextNode_selected);
  748. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_selected);
  749. }else {
  750. this.unSelectedSingle();
  751. }
  752. },
  753. unSelectedSingle: function(){
  754. this.selector.currentItem = null;
  755. this.isSelected = false;
  756. this.selector.selectedItems.erase(this);
  757. this.node.setStyles(this.selector.css.selectorItem);
  758. this.textNode.setStyles(this.selector.css.selectorItemTextNode);
  759. this.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  760. },
  761. selected: function(){
  762. if ((this.selector.options.count==0) || (this.selector.selectedItems.length+1)<=this.selector.options.count){
  763. this.isSelected = true;
  764. this.node.setStyles(this.selector.css.selectorItem_selected);
  765. this.textNode.setStyles(this.selector.css.selectorItemTextNode_selected);
  766. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_selected);
  767. this.selectedItem = this.selector._newItemSelected(this.data, this.selector, this);
  768. this.selectedItem.check();
  769. this.selector.selectedItems.push(this.selectedItem);
  770. }else{
  771. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "最多可选择"+this.selector.options.count+"个选项", this.selector.node);
  772. }
  773. },
  774. unSelected: function(){
  775. this.isSelected = false;
  776. this.node.setStyles(this.selector.css.selectorItem);
  777. this.textNode.setStyles(this.selector.css.selectorItemTextNode);
  778. this.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  779. if (this.selectedItem){
  780. this.selector.selectedItems.erase(this.selectedItem);
  781. this.selectedItem.items.each(function(item){
  782. if (item != this){
  783. item.isSelected = false;
  784. item.node.setStyles(this.selector.css.selectorItem);
  785. item.textNode.setStyles(this.selector.css.selectorItemTextNode);
  786. item.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  787. }
  788. }.bind(this));
  789. this.selectedItem.destroy();
  790. this.selectedItem = null;
  791. }
  792. }
  793. });
  794. MWF.xApplication.Organization.Selector.Person.ItemSelected = new Class({
  795. Extends: MWF.xApplication.Organization.Selector.Person.Item,
  796. initialize: function(data, selector, item){
  797. this.data = data;
  798. this.selector = selector;
  799. this.container = this.selector.selectedNode;
  800. this.isSelected = false;
  801. this.items = [];
  802. if (item) this.items.push(item);
  803. this.level = 0;
  804. this.load();
  805. },
  806. clickItem: function(){
  807. if (this.items.length){
  808. this.items.each(function(item){
  809. item.unSelected();
  810. });
  811. }else{
  812. //this.item.selectedItem = null;
  813. //this.item.isSelected = false;
  814. this.destroy();
  815. this.selector.selectedItems.erase(this);
  816. }
  817. },
  818. overItem: function(){
  819. if (!this.isSelected){
  820. this.node.setStyles(this.selector.css.selectorItem_over);
  821. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_selected_over);
  822. }
  823. },
  824. addItem: function(item){
  825. if (this.items.indexOf(item)==-1) this.items.push(item);
  826. },
  827. check: function(){
  828. if (this.selector.items.length){
  829. var items = this.selector.items.filter(function(item, index){
  830. return item.data.id == this.data.id;
  831. }.bind(this));
  832. this.items = items
  833. if (items.length){
  834. items.each(function(item){
  835. item.selectedItem = this;
  836. item.setSelected();
  837. }.bind(this));
  838. }
  839. }
  840. },
  841. destroy: function(){
  842. this.node.destroy();
  843. delete this;
  844. }
  845. });
  846. MWF.xApplication.Organization.Selector.Person.ItemCategory = new Class({
  847. Extends: MWF.xApplication.Organization.Selector.Person.Item,
  848. initialize: function(data, selector, container, level){
  849. this.data = data;
  850. this.selector = selector;
  851. this.container = container;
  852. this.isSelected = false;
  853. this.level = (level) ? level.toInt() : 1;
  854. this.load();
  855. },
  856. createNode: function(){
  857. this.node = new Element("div", {
  858. "styles": this.selector.css.selectorItemCategory
  859. }).inject(this.container);
  860. },
  861. load: function(){
  862. this.createNode();
  863. this.levelNode = new Element("div", {
  864. "styles": this.selector.css.selectorItemLevelNode
  865. }).inject(this.node);
  866. var indent = this.level*10;
  867. this.levelNode.setStyle("width", ""+indent+"px");
  868. this.iconNode = new Element("div", {
  869. "styles": this.selector.css.selectorItemIconNode
  870. }).inject(this.node);
  871. this._setIcon();
  872. this.actionNode = new Element("div", {
  873. "styles": (this.selector.options.expand) ? this.selector.css.selectorItemCategoryActionNode_expand : this.selector.css.selectorItemCategoryActionNode_collapse
  874. }).inject(this.node);
  875. this.textNode = new Element("div", {
  876. "styles": this.selector.css.selectorItemCategoryTextNode,
  877. "text": this._getShowName()
  878. }).inject(this.node);
  879. var m = this.textNode.getStyle("margin-left").toFloat()+indent;
  880. this.textNode.setStyle("margin-left", ""+m+"px");
  881. this.children = new Element("div", {
  882. "styles": this.selector.css.selectorItemCategoryChildrenNode,
  883. }).inject(this.node, "after");
  884. if (!this.selector.options.expand) this.children.setStyle("display", "none");
  885. var subIdList = this.selector._getChildrenItemIds(this.data);
  886. if (subIdList){
  887. var count = subIdList.length;
  888. this.childrenHeight = count*29;
  889. this.children.setStyle("height", ""+this.childrenHeight+"px");
  890. }
  891. if (!this._hasChild()){
  892. this.actionNode.setStyle("background", "transparent");
  893. this.textNode.setStyle("color", "#777");
  894. }
  895. this.setEvent();
  896. this.check();
  897. },
  898. clickItem: function(){
  899. if (this._hasChild()){
  900. if (!this.fx){
  901. this.fx = new Fx.Tween(this.children, {
  902. "duration": 200
  903. // "transition": Fx.Transitions.Cubic.easeIn
  904. });
  905. };
  906. if (!this.fx.isRunning()){
  907. var display = this.children.getStyle("display");
  908. if (display == "none"){
  909. this.children.setStyles({
  910. "display": "block",
  911. "height": "0px"
  912. });
  913. this.fx.start("height", "0px", ""+this.childrenHeight+"px");
  914. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  915. }else{
  916. if (!this.childrenHeight) this.childrenHeight = this.children.getStyle("height").toFloat();
  917. this.fx.start("height", ""+this.childrenHeight+"px", "0px").chain(function(){
  918. this.children.setStyles({
  919. "display": "none",
  920. "height": "0px"
  921. });
  922. }.bind(this));
  923. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  924. }
  925. }
  926. }
  927. },
  928. overItem: function(){
  929. //if (!this.isSelected){
  930. // this.node.setStyles(this.selector.css.selectorItem_over);
  931. // this.actionNode.setStyles(this.selector.css.selectorItemActionNode_over);
  932. //}
  933. },
  934. outItem: function(){
  935. //if (!this.isSelected){
  936. // this.node.setStyles(this.selector.css.selectorItem);
  937. // this.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  938. //}
  939. },
  940. _hasChild: function(){
  941. var subIdList = this.selector._getChildrenItemIds(this.data);
  942. if (subIdList) if (subIdList.length) return true;
  943. return false;
  944. }
  945. });
  946. MWF.xApplication.Organization.Selector.Person.ItemGroupCategory = new Class({
  947. Extends: MWF.xApplication.Organization.Selector.Person.ItemCategory,
  948. _getShowName: function(){
  949. return this.data.name;
  950. },
  951. _setIcon: function(){
  952. this.iconNode.setStyle("background-image", "url("+"/x_component_Organization/Selector/$Selector/default/icon/groupicon.png)");
  953. }
  954. });
  955. MWF.xApplication.Organization.Selector.Person.ItemRoleCategory = new Class({
  956. Extends: MWF.xApplication.Organization.Selector.Person.ItemCategory,
  957. _getShowName: function(){
  958. return this.data.name;
  959. },
  960. _setIcon: function(){
  961. this.iconNode.setStyle("background-image", "url("+"/x_component_Organization/Selector/$Selector/default/icon/roleicon.png)");
  962. }
  963. });