Person.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. debugger;
  633. this.data = data;
  634. this.selector = selector;
  635. this.container = container;
  636. this.isSelected = false;
  637. this.level = (level) ? level.toInt() : 1;
  638. this.load();
  639. },
  640. _getShowName: function(){
  641. return this.data.display+"("+this.data.employee+")";
  642. },
  643. _setIcon: function(){
  644. this.iconNode.setStyle("background-image", "url("+"/x_component_Organization/Selector/$Selector/default/icon/personicon.png)");
  645. },
  646. load: function(){
  647. this.node = new Element("div", {
  648. "styles": this.selector.css.selectorItem
  649. }).inject(this.container);
  650. this.levelNode = new Element("div", {
  651. "styles": this.selector.css.selectorItemLevelNode
  652. }).inject(this.node);
  653. var indent = this.level*10;
  654. this.levelNode.setStyle("width", ""+indent+"px");
  655. this.iconNode = new Element("div", {
  656. "styles": this.selector.css.selectorItemIconNode
  657. }).inject(this.node);
  658. this._setIcon();
  659. this.actionNode = new Element("div", {
  660. "styles": this.selector.css.selectorItemActionNode
  661. }).inject(this.node);
  662. this.textNode = new Element("div", {
  663. "styles": this.selector.css.selectorItemTextNode,
  664. "text": this._getShowName()
  665. }).inject(this.node);
  666. var m = this.textNode.getStyle("margin-left").toFloat()+indent;
  667. this.textNode.setStyle("margin-left", ""+m+"px");
  668. this.loadSubItem();
  669. this.setEvent();
  670. this.check();
  671. },
  672. loadSubItem: function(){},
  673. check: function(){
  674. if (this.selector.options.count.toInt()==1){
  675. this.checkSelectedSingle();
  676. }else{
  677. this.checkSelected();
  678. }
  679. },
  680. checkSelectedSingle: function(){
  681. var selectedItem = this.selector.options.values.filter(function(item, index){
  682. return this.data.id == item;
  683. }.bind(this));
  684. if (selectedItem.length){
  685. this.selectedSingle();
  686. }
  687. },
  688. checkSelected: function(){
  689. var selectedItem = this.selector.selectedItems.filter(function(item, index){
  690. return item.data.id == this.data.id;
  691. }.bind(this));
  692. if (selectedItem.length){
  693. //selectedItem[0].item = this;
  694. selectedItem[0].addItem(this);
  695. this.selectedItem = selectedItem[0];
  696. this.setSelected();
  697. }
  698. },
  699. setSelected: function(){
  700. this.isSelected = true;
  701. this.node.setStyles(this.selector.css.selectorItem_selected);
  702. this.textNode.setStyles(this.selector.css.selectorItemTextNode_selected);
  703. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_selected);
  704. },
  705. setEvent: function(){
  706. this.node.addEvents({
  707. "mouseover": function(){
  708. this.overItem();
  709. }.bind(this),
  710. "mouseout": function(){
  711. this.outItem();
  712. }.bind(this),
  713. "click": function(){
  714. this.clickItem();
  715. }.bind(this)
  716. });
  717. },
  718. clickItem: function(){
  719. if (this.selector.options.count.toInt()==1){
  720. this.selectedSingle();
  721. }else{
  722. if (this.isSelected){
  723. this.unSelected();
  724. }else{
  725. this.selected();
  726. }
  727. }
  728. },
  729. overItem: function(){
  730. if (!this.isSelected){
  731. this.node.setStyles(this.selector.css.selectorItem_over);
  732. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_over);
  733. }
  734. },
  735. outItem: function(){
  736. if (!this.isSelected){
  737. this.node.setStyles(this.selector.css.selectorItem);
  738. this.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  739. }
  740. },
  741. selectedSingle: function(){
  742. if (!this.isSelected){
  743. if (this.selector.currentItem) this.selector.currentItem.unSelectedSingle();
  744. this.selector.currentItem = this;
  745. this.isSelected = true;
  746. this.selector.selectedItems.push(this);
  747. this.node.setStyles(this.selector.css.selectorItem_selected);
  748. this.textNode.setStyles(this.selector.css.selectorItemTextNode_selected);
  749. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_selected);
  750. }else {
  751. this.unSelectedSingle();
  752. }
  753. },
  754. unSelectedSingle: function(){
  755. this.selector.currentItem = null;
  756. this.isSelected = false;
  757. this.selector.selectedItems.erase(this);
  758. this.node.setStyles(this.selector.css.selectorItem);
  759. this.textNode.setStyles(this.selector.css.selectorItemTextNode);
  760. this.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  761. },
  762. selected: function(){
  763. if ((this.selector.options.count==0) || (this.selector.selectedItems.length+1)<=this.selector.options.count){
  764. this.isSelected = true;
  765. this.node.setStyles(this.selector.css.selectorItem_selected);
  766. this.textNode.setStyles(this.selector.css.selectorItemTextNode_selected);
  767. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_selected);
  768. this.selectedItem = this.selector._newItemSelected(this.data, this.selector, this);
  769. this.selectedItem.check();
  770. this.selector.selectedItems.push(this.selectedItem);
  771. }else{
  772. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "最多可选择"+this.selector.options.count+"个选项", this.selector.node);
  773. }
  774. },
  775. unSelected: function(){
  776. this.isSelected = false;
  777. this.node.setStyles(this.selector.css.selectorItem);
  778. this.textNode.setStyles(this.selector.css.selectorItemTextNode);
  779. this.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  780. if (this.selectedItem){
  781. this.selector.selectedItems.erase(this.selectedItem);
  782. this.selectedItem.items.each(function(item){
  783. if (item != this){
  784. item.isSelected = false;
  785. item.node.setStyles(this.selector.css.selectorItem);
  786. item.textNode.setStyles(this.selector.css.selectorItemTextNode);
  787. item.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  788. }
  789. }.bind(this));
  790. this.selectedItem.destroy();
  791. this.selectedItem = null;
  792. }
  793. }
  794. });
  795. MWF.xApplication.Organization.Selector.Person.ItemSelected = new Class({
  796. Extends: MWF.xApplication.Organization.Selector.Person.Item,
  797. initialize: function(data, selector, item){
  798. this.data = data;
  799. this.selector = selector;
  800. this.container = this.selector.selectedNode;
  801. this.isSelected = false;
  802. this.items = [];
  803. if (item) this.items.push(item);
  804. this.level = 0;
  805. this.load();
  806. },
  807. clickItem: function(){
  808. if (this.items.length){
  809. this.items.each(function(item){
  810. item.unSelected();
  811. });
  812. }else{
  813. //this.item.selectedItem = null;
  814. //this.item.isSelected = false;
  815. this.destroy();
  816. this.selector.selectedItems.erase(this);
  817. }
  818. },
  819. overItem: function(){
  820. if (!this.isSelected){
  821. this.node.setStyles(this.selector.css.selectorItem_over);
  822. this.actionNode.setStyles(this.selector.css.selectorItemActionNode_selected_over);
  823. }
  824. },
  825. addItem: function(item){
  826. if (this.items.indexOf(item)==-1) this.items.push(item);
  827. },
  828. check: function(){
  829. if (this.selector.items.length){
  830. var items = this.selector.items.filter(function(item, index){
  831. return item.data.id == this.data.id;
  832. }.bind(this));
  833. this.items = items
  834. if (items.length){
  835. items.each(function(item){
  836. item.selectedItem = this;
  837. item.setSelected();
  838. }.bind(this));
  839. }
  840. }
  841. },
  842. destroy: function(){
  843. this.node.destroy();
  844. delete this;
  845. }
  846. });
  847. MWF.xApplication.Organization.Selector.Person.ItemCategory = new Class({
  848. Extends: MWF.xApplication.Organization.Selector.Person.Item,
  849. initialize: function(data, selector, container, level){
  850. this.data = data;
  851. this.selector = selector;
  852. this.container = container;
  853. this.isSelected = false;
  854. this.level = (level) ? level.toInt() : 1;
  855. this.load();
  856. },
  857. createNode: function(){
  858. this.node = new Element("div", {
  859. "styles": this.selector.css.selectorItemCategory
  860. }).inject(this.container);
  861. },
  862. load: function(){
  863. this.createNode();
  864. this.levelNode = new Element("div", {
  865. "styles": this.selector.css.selectorItemLevelNode
  866. }).inject(this.node);
  867. var indent = this.level*10;
  868. this.levelNode.setStyle("width", ""+indent+"px");
  869. this.iconNode = new Element("div", {
  870. "styles": this.selector.css.selectorItemIconNode
  871. }).inject(this.node);
  872. this._setIcon();
  873. this.actionNode = new Element("div", {
  874. "styles": (this.selector.options.expand) ? this.selector.css.selectorItemCategoryActionNode_expand : this.selector.css.selectorItemCategoryActionNode_collapse
  875. }).inject(this.node);
  876. this.textNode = new Element("div", {
  877. "styles": this.selector.css.selectorItemCategoryTextNode,
  878. "text": this._getShowName()
  879. }).inject(this.node);
  880. var m = this.textNode.getStyle("margin-left").toFloat()+indent;
  881. this.textNode.setStyle("margin-left", ""+m+"px");
  882. this.children = new Element("div", {
  883. "styles": this.selector.css.selectorItemCategoryChildrenNode,
  884. }).inject(this.node, "after");
  885. if (!this.selector.options.expand) this.children.setStyle("display", "none");
  886. var subIdList = this.selector._getChildrenItemIds(this.data);
  887. if (subIdList){
  888. var count = subIdList.length;
  889. this.childrenHeight = count*29;
  890. this.children.setStyle("height", ""+this.childrenHeight+"px");
  891. }
  892. if (!this._hasChild()){
  893. this.actionNode.setStyle("background", "transparent");
  894. this.textNode.setStyle("color", "#777");
  895. }
  896. this.setEvent();
  897. this.check();
  898. },
  899. clickItem: function(){
  900. if (this._hasChild()){
  901. if (!this.fx){
  902. this.fx = new Fx.Tween(this.children, {
  903. "duration": 200
  904. // "transition": Fx.Transitions.Cubic.easeIn
  905. });
  906. };
  907. if (!this.fx.isRunning()){
  908. var display = this.children.getStyle("display");
  909. if (display == "none"){
  910. this.children.setStyles({
  911. "display": "block",
  912. "height": "0px"
  913. });
  914. this.fx.start("height", "0px", ""+this.childrenHeight+"px");
  915. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  916. }else{
  917. if (!this.childrenHeight) this.childrenHeight = this.children.getStyle("height").toFloat();
  918. this.fx.start("height", ""+this.childrenHeight+"px", "0px").chain(function(){
  919. this.children.setStyles({
  920. "display": "none",
  921. "height": "0px"
  922. });
  923. }.bind(this));
  924. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  925. }
  926. }
  927. }
  928. },
  929. overItem: function(){
  930. //if (!this.isSelected){
  931. // this.node.setStyles(this.selector.css.selectorItem_over);
  932. // this.actionNode.setStyles(this.selector.css.selectorItemActionNode_over);
  933. //}
  934. },
  935. outItem: function(){
  936. //if (!this.isSelected){
  937. // this.node.setStyles(this.selector.css.selectorItem);
  938. // this.actionNode.setStyles(this.selector.css.selectorItemActionNode);
  939. //}
  940. },
  941. _hasChild: function(){
  942. var subIdList = this.selector._getChildrenItemIds(this.data);
  943. if (subIdList) if (subIdList.length) return true;
  944. return false;
  945. }
  946. });
  947. MWF.xApplication.Organization.Selector.Person.ItemGroupCategory = new Class({
  948. Extends: MWF.xApplication.Organization.Selector.Person.ItemCategory,
  949. _getShowName: function(){
  950. return this.data.name;
  951. },
  952. _setIcon: function(){
  953. this.iconNode.setStyle("background-image", "url("+"/x_component_Organization/Selector/$Selector/default/icon/groupicon.png)");
  954. }
  955. });
  956. MWF.xApplication.Organization.Selector.Person.ItemRoleCategory = new Class({
  957. Extends: MWF.xApplication.Organization.Selector.Person.ItemCategory,
  958. _getShowName: function(){
  959. return this.data.name;
  960. },
  961. _setIcon: function(){
  962. this.iconNode.setStyle("background-image", "url("+"/x_component_Organization/Selector/$Selector/default/icon/roleicon.png)");
  963. }
  964. });