Unit.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. MWF.xApplication.Selector = MWF.xApplication.Selector || {};
  2. MWF.xDesktop.requireApp("Selector", "Identity", null, false);
  3. MWF.xApplication.Selector.Unit = new Class({
  4. Extends: MWF.xApplication.Selector.Identity,
  5. options: {
  6. "style": "default",
  7. "count": 0,
  8. "title": MWF.xApplication.Selector.LP.selectUnit,
  9. "units": [],
  10. //"unitTypes": [],
  11. "values": [],
  12. "zIndex": 1000,
  13. "expand": true,
  14. "exclude" : [],
  15. "expandSubEnable" : true, //是否允许展开下一层
  16. "selectAllEnable" : true //分类是否允许全选下一层
  17. },
  18. loadSelectItems: function(addToNext){
  19. if (this.options.units.length){
  20. this.options.units.each(function(unit){
  21. // this.action.listUnitByKey(function(json){
  22. // if (json.data.length){
  23. // json.data.each(function(data){
  24. // if (data.subDirectUnitCount) var category = this._newItemCategory("ItemCategory", data, this, this.itemAreaNode);
  25. // }.bind(this));
  26. // }
  27. // }.bind(this), null, comp);
  28. if (typeOf(unit)==="string"){
  29. // this.orgAction.listUnitByKey(function(json){
  30. // if (json.data.length){
  31. // json.data.each(function(data){
  32. // if (data.subDirectUnitCount) var category = this._newItemCategory("ItemCategory", data, this, this.itemAreaNode);
  33. // }.bind(this));
  34. // }
  35. // }.bind(this), null, unit);
  36. this.orgAction.getUnit(function(json){
  37. json.data = typeOf( json.data ) == "object" ? [json.data] : json.data;
  38. if (json.data.length){
  39. json.data.each( function(data){
  40. if( this.options.expandSubEnable ){
  41. if (data.subDirectUnitCount){
  42. var category = this._newItemCategory("ItemCategory", data, this, this.itemAreaNode);
  43. this.subCategorys.push(category);
  44. }
  45. }else{
  46. var item = this._newItem( data, this, this.itemAreaNode);
  47. this.subItems.push(item);
  48. }
  49. }.bind(this));
  50. }
  51. }.bind(this), null, unit);
  52. }else{
  53. this.orgAction.getUnit(function(json){
  54. json.data = typeOf( json.data ) == "object" ? [json.data] : json.data;
  55. if (json.data.length){
  56. json.data.each( function(data){
  57. if( this.options.expandSubEnable ) {
  58. if (data.subDirectUnitCount){
  59. var category = this._newItemCategory("ItemCategory", data, this, this.itemAreaNode);
  60. this.subCategorys.push(category);
  61. }
  62. }else{
  63. var item = this._newItem(data, this, this.itemAreaNode);
  64. this.subItems.push(item);
  65. }
  66. }.bind(this));
  67. }
  68. }.bind(this), null, unit.id);
  69. //if (unit.subDirectUnitCount) var category = this._newItemCategory("ItemCategory", unit, this, this.itemAreaNode);
  70. }
  71. }.bind(this));
  72. }else{
  73. this.orgAction.listTopUnit(function(json){
  74. json.data.each(function(data){
  75. // var flag = true;
  76. // if (this.options.unitTypes.length){
  77. // flag = data.typeList.some(function(item){
  78. // return (!this.options.unitTypes.length) || (this.options.unitTypes.indexOf(item)!==-1)
  79. // }.bind(this));
  80. // }
  81. // if (flag){
  82. if( !this.isExcluded( data ) ) {
  83. var unit = this._newItem(data, this, this.itemAreaNode, 1);
  84. this.subItems.push(unit);
  85. }
  86. //unit.loadSubItem();
  87. // }else{
  88. // var category = this._newItemCategory("ItemCategory", data, this, this.itemAreaNode);
  89. // }
  90. }.bind(this));
  91. }.bind(this));
  92. }
  93. },
  94. _scrollEvent: function(y){
  95. return true;
  96. },
  97. _getChildrenItemIds: function(){
  98. return null;
  99. },
  100. _newItemCategory: function(type, data, selector, item, level, category){
  101. return new MWF.xApplication.Selector.Unit[type](data, selector, item, level, category)
  102. },
  103. _listItemByKey: function(callback, failure, key){
  104. if (this.options.units.length){
  105. var units = [];
  106. this.options.units.each(function(u){
  107. if (typeOf(u)==="string"){
  108. units.push(u);
  109. }
  110. if (typeOf(u)==="object"){
  111. units.push(u.distinguishedName);
  112. }
  113. });
  114. key = {"key": key, "unitList": units};
  115. }
  116. this.orgAction.listUnitByKey(function(json){
  117. if (callback) callback.apply(this, [json]);
  118. }.bind(this), failure, key);
  119. },
  120. _getItem: function(callback, failure, id, async){
  121. this.orgAction.getUnit(function(json){
  122. if (callback) callback.apply(this, [json]);
  123. }.bind(this), failure, ((typeOf(id)==="string") ? id : id.distinguishedName), async);
  124. },
  125. _newItemSelected: function(data, selector, item){
  126. return new MWF.xApplication.Selector.Unit.ItemSelected(data, selector, item)
  127. },
  128. _listItemByPinyin: function(callback, failure, key){
  129. if (this.options.units.length){
  130. var units = [];
  131. this.options.units.each(function(u){
  132. if (typeOf(u)==="string"){
  133. units.push(u);
  134. }
  135. if (typeOf(u)==="object"){
  136. units.push(u.distinguishedName);
  137. }
  138. });
  139. key = {"key": key, "unitList": units};
  140. }
  141. this.orgAction.listUnitByPinyininitial(function(json){
  142. if (callback) callback.apply(this, [json]);
  143. }.bind(this), failure, key);
  144. },
  145. _newItem: function(data, selector, container, level, category){
  146. return new MWF.xApplication.Selector.Unit.Item(data, selector, container, level, category);
  147. },
  148. _newItemSearch: function(data, selector, container, level){
  149. return new MWF.xApplication.Selector.Unit.SearchItem(data, selector, container, level);
  150. }
  151. });
  152. MWF.xApplication.Selector.Unit.Item = new Class({
  153. Extends: MWF.xApplication.Selector.Identity.Item,
  154. _getShowName: function(){
  155. return this.data.name;
  156. },
  157. _getTtiteText: function(){
  158. return this.data.levelName || this.data.name;
  159. },
  160. _setIcon: function(){
  161. this.iconNode.setStyle("background-image", "url("+"/x_component_Selector/$Selector/default/icon/departmenticon.png)");
  162. },
  163. loadSubItem: function(){
  164. if( !this.selector.options.expandSubEnable )return;
  165. this.isExpand = (this.selector.options.expand);
  166. if (this.data.subDirectUnitCount){
  167. if (this.selector.options.expand){
  168. if (this.level===1){
  169. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
  170. this.loadSubItems();
  171. }else{
  172. this.isExpand = false;
  173. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
  174. }
  175. }else{
  176. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
  177. }
  178. this.levelNode.addEvent("click", function(e){
  179. if (this.isExpand){
  180. this.children.setStyle("display", "none");
  181. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
  182. this.isExpand = false;
  183. }else{
  184. this.loadSubItems();
  185. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
  186. this.isExpand = true;
  187. }
  188. e.stopPropagation();
  189. }.bind(this));
  190. if( this.selector.css.selectorItemLevelNode_expand_over && this.selector.css.selectorItemLevelNode_collapse_over ){
  191. this.levelNode.addEvents({
  192. mouseover : function(e){
  193. var styles = this.isExpand ? this.selector.css.selectorItemLevelNode_expand_over : this.selector.css.selectorItemLevelNode_collapse_over;
  194. this.levelNode.setStyles(styles);
  195. }.bind(this),
  196. mouseout : function(e){
  197. var styles = this.isExpand ? this.selector.css.selectorItemLevelNode_expand : this.selector.css.selectorItemLevelNode_collapse;
  198. this.levelNode.setStyles(styles);
  199. }.bind(this)
  200. })
  201. }
  202. if( !this.selectAllNode && this.selector.options.count.toInt() !== 1 && this.selector.options.style!=="blue_flat" ){
  203. this.selectAllNode = new Element("div", {
  204. "styles": this.selector.css.selectorItemCategoryActionNode_selectAll,
  205. "title" : "全选下级"
  206. }).inject(this.textNode, "before");
  207. this.selectAllNode.addEvent( "click", function(ev){
  208. if( this.isSelectedAll ){
  209. this.unselectAll(ev);
  210. this.selector.fireEvent("unselectCatgory",[this])
  211. }else{
  212. this.selectAll(ev);
  213. this.selector.fireEvent("selectCatgory",[this])
  214. }
  215. ev.stopPropagation();
  216. }.bind(this));
  217. if( this.selector.css.selectorItemCategoryActionNode_selectAll_over ){
  218. this.selectAllNode.addEvents( {
  219. "mouseover" : function(ev){
  220. if( !this.isSelectedAll )this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll_over );
  221. //ev.stopPropagation();
  222. }.bind(this),
  223. "mouseout" : function(ev){
  224. if( !this.isSelectedAll )this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll );
  225. //ev.stopPropagation();
  226. }.bind(this)
  227. })
  228. }
  229. }
  230. }
  231. //this.actionNode.setStyles((this.selector.options.expand) ? this.selector.css.selectorItemCategoryActionNode_expand : this.selector.css.selectorItemCategoryActionNode_collapse);
  232. },
  233. unselectAll : function(ev, exclude){
  234. //( this.subItems || [] ).each( function(item){
  235. // if(item.isSelected)item.unSelected();
  236. //}.bind(this));
  237. var excludeList = exclude || [];
  238. if( exclude && typeOf(exclude) !== "array" )excludeList = [exclude];
  239. ( this.subItems || [] ).each( function(item){
  240. if(item.isSelected && !excludeList.contains(item) ){
  241. item.unSelected();
  242. }
  243. }.bind(this));
  244. if( this.selectAllNode && this.selector.css.selectorItemCategoryActionNode_selectAll ){
  245. this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll );
  246. }
  247. this.isSelectedAll = false;
  248. },
  249. unselectAllNested : function( ev, exclude ){
  250. this.unselectAll(ev, exclude );
  251. if( this.subCategorys && this.subCategorys.length ){
  252. this.subCategorys.each( function( category ){
  253. if(category.unselectAllNested)category.unselectAllNested( ev, exclude )
  254. })
  255. }
  256. if( this.subItems && this.subItems.length ){
  257. this.subItems.each( function( item ){
  258. if(item.unselectAllNested)item.unselectAllNested( ev, exclude )
  259. })
  260. }
  261. },
  262. selectAllNested : function(){
  263. this.selectAll();
  264. if( this.subCategorys && this.subCategorys.length ){
  265. this.subCategorys.each( function( category ){
  266. if(category.selectAllNested)category.selectAllNested()
  267. })
  268. }
  269. if( this.subItems && this.subItems.length ){
  270. this.subItems.each( function( item ){
  271. if(item.selectAllNested)item.selectAllNested()
  272. })
  273. }
  274. },
  275. selectAll: function(ev){
  276. if( this.loaded ){
  277. this._selectAll( ev )
  278. }else{
  279. this.loadSubItems(function(){
  280. this._selectAll( ev )
  281. }.bind(this));
  282. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
  283. this.isExpand = true;
  284. }
  285. },
  286. _selectAll : function( ev ){
  287. if( !this.subItems || !this.subItems.length )return;
  288. var count = this.selector.options.maxCount || this.selector.options.count;
  289. if (!count) count = 0;
  290. var selectedSubItemCount = 0;
  291. this.subItems.each( function(item){
  292. if(item.isSelected)selectedSubItemCount++
  293. }.bind(this));
  294. if ((count.toInt()===0) || (this.selector.selectedItems.length+(this.subItems.length-selectedSubItemCount))<=count){
  295. this.subItems.each( function(item){
  296. if(!item.isSelected)item.selected();
  297. }.bind(this))
  298. if( this.selectAllNode && this.selector.css.selectorItemCategoryActionNode_selectAll_selected ){
  299. this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll_selected );
  300. }
  301. this.isSelectedAll = true;
  302. }else{
  303. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "最多可选择"+count+"个选项", this.node);
  304. }
  305. },
  306. checkSelectAll : function(){
  307. if( !this.isSelectedAll )return;
  308. if( !this.selectAllNode || !this.selector.css.selectorItemCategoryActionNode_selectAll)return;
  309. if( ! this.subItems )return;
  310. var hasSelectedItem = false;
  311. for( var i=0; i< this.subItems.length; i++ ){
  312. if( this.subItems[i].isSelected ){
  313. hasSelectedItem = true;
  314. break;
  315. }
  316. }
  317. if( !hasSelectedItem ){
  318. this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll );
  319. this.isSelectedAll = false;
  320. }
  321. },
  322. loadSubItems: function( callback ){
  323. if (!this.loaded){
  324. if (!this.children){
  325. this.children = new Element("div", {
  326. "styles": this.selector.css.selectorItemCategoryChildrenNode
  327. }).inject(this.node, "after");
  328. }
  329. this.children.setStyle("display", "block");
  330. // if (!this.selector.options.expand) this.children.setStyle("display", "none");
  331. this.selector.orgAction.listSubUnitDirect(function(subJson){
  332. subJson.data.each(function(subData){
  333. if( !this.selector.isExcluded( subData ) ) {
  334. var category = this.selector._newItem(subData, this.selector, this.children, this.level + 1, this);
  335. if( !this.subItems )this.subItems = [];
  336. this.subItems.push( category );
  337. }
  338. }.bind(this));
  339. this.loaded = true;
  340. if(callback)callback();
  341. }.bind(this), null, this.data.distinguishedName);
  342. }else{
  343. this.children.setStyle("display", "block");
  344. }
  345. },
  346. getData: function(callback){
  347. if (callback) callback();
  348. },
  349. postLoad : function(){
  350. if( this.selector.options.style === "blue_flat" ){
  351. if( this.level === 1 ){
  352. var indent = 26;
  353. this.levelNode.setStyle("width", ""+indent+"px");
  354. }else{
  355. var indent = 26 + ( this.level -1 ) * this.selector.options.indent ;
  356. this.levelNode.setStyle("width", ""+indent+"px");
  357. }
  358. }
  359. }
  360. });
  361. MWF.xApplication.Selector.Unit.SearchItem = new Class({
  362. Extends: MWF.xApplication.Selector.Unit.Item,
  363. _getShowName: function(){
  364. return this.data.levelName || this.data.name;
  365. }
  366. });
  367. MWF.xApplication.Selector.Unit.ItemSelected = new Class({
  368. Extends: MWF.xApplication.Selector.Identity.ItemSelected,
  369. getData: function(callback){
  370. if (callback) callback();
  371. },
  372. _getTtiteText: function(){
  373. return this.data.levelName || this.data.name;
  374. },
  375. _getShowName: function(){
  376. return this.data.name+((this.data.levelName) ? "("+this.data.levelName+")" : "");
  377. },
  378. _setIcon: function(){
  379. this.iconNode.setStyle("background-image", "url("+"/x_component_Selector/$Selector/default/icon/departmenticon.png)");
  380. }
  381. });
  382. MWF.xApplication.Selector.Unit.ItemCategory = new Class({
  383. Extends: MWF.xApplication.Selector.Identity.ItemCategory,
  384. loadSub: function(callback){
  385. if (!this.loaded){
  386. this.selector.orgAction.listSubUnitDirect(function(subJson){
  387. subJson.data.each(function(subData){
  388. if( !this.selector.isExcluded( subData ) ) {
  389. var category = this.selector._newItem(subData, this.selector, this.children, this.level+1, this);
  390. if(this.subItems)this.subItems.push( category );
  391. this.subCategorys.push( category );
  392. }
  393. //var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1);
  394. }.bind(this));
  395. this.loaded = true;
  396. if (callback) callback();
  397. }.bind(this), null, this.data.distinguishedName);
  398. }else{
  399. if (callback) callback();
  400. }
  401. },
  402. _hasChild: function(){
  403. var uCount = (this.data.subDirectUnitCount) ? this.data.subDirectUnitCount : 0;
  404. //var iCount = (this.data.subDirectIdentityCount) ? this.data.subDirectIdentityCount : 0;
  405. return uCount;
  406. },
  407. _hasChildItem: function(){
  408. var uCount = (this.data.subDirectUnitCount) ? this.data.subDirectUnitCount : 0;
  409. return uCount;
  410. }
  411. });
  412. MWF.xApplication.Selector.Unit.Filter = new Class({
  413. Implements: [Options, Events],
  414. options: {
  415. "style": "default",
  416. "units": []
  417. },
  418. initialize: function(value, options){
  419. this.setOptions(options);
  420. this.value = value;
  421. this.orgAction = MWF.Actions.get("x_organization_assemble_control");
  422. },
  423. filter: function(value, callback){
  424. this.value = value;
  425. var key = this.value;
  426. if (this.options.units.length){
  427. var units = [];
  428. this.options.units.each(function(u){
  429. if (typeOf(u)==="string"){
  430. units.push(u);
  431. }
  432. if (typeOf(u)==="object"){
  433. units.push(u.distinguishedName);
  434. }
  435. });
  436. key = {"key": key, "unitList": units};
  437. }
  438. this.orgAction.listUnitByKey(function(json){
  439. data = json.data;
  440. if (callback) callback(data)
  441. }.bind(this), null, key);
  442. }
  443. });