Unit.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. _setIcon: function(){
  158. this.iconNode.setStyle("background-image", "url("+"/x_component_Selector/$Selector/default/icon/departmenticon.png)");
  159. },
  160. loadSubItem: function(){
  161. if( !this.selector.options.expandSubEnable )return;
  162. this.isExpand = (this.selector.options.expand);
  163. if (this.data.subDirectUnitCount){
  164. if (this.selector.options.expand){
  165. if (this.level===1){
  166. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
  167. this.loadSubItems();
  168. }else{
  169. this.isExpand = false;
  170. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
  171. }
  172. }else{
  173. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
  174. }
  175. this.levelNode.addEvent("click", function(e){
  176. if (this.isExpand){
  177. this.children.setStyle("display", "none");
  178. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
  179. this.isExpand = false;
  180. }else{
  181. this.loadSubItems();
  182. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
  183. this.isExpand = true;
  184. }
  185. e.stopPropagation();
  186. }.bind(this));
  187. if( this.selector.css.selectorItemLevelNode_expand_over && this.selector.css.selectorItemLevelNode_collapse_over ){
  188. this.levelNode.addEvents({
  189. mouseover : function(e){
  190. var styles = this.isExpand ? this.selector.css.selectorItemLevelNode_expand_over : this.selector.css.selectorItemLevelNode_collapse_over;
  191. this.levelNode.setStyles(styles);
  192. }.bind(this),
  193. mouseout : function(e){
  194. var styles = this.isExpand ? this.selector.css.selectorItemLevelNode_expand : this.selector.css.selectorItemLevelNode_collapse;
  195. this.levelNode.setStyles(styles);
  196. }.bind(this)
  197. })
  198. }
  199. if( !this.selectAllNode && this.selector.options.count.toInt() !== 1 && this.selector.options.style!=="blue_flat" ){
  200. this.selectAllNode = new Element("div", {
  201. "styles": this.selector.css.selectorItemCategoryActionNode_selectAll,
  202. "title" : "全选下级"
  203. }).inject(this.textNode, "before");
  204. this.selectAllNode.addEvent( "click", function(ev){
  205. if( this.isSelectedAll ){
  206. this.unselectAll(ev);
  207. this.selector.fireEvent("unselectCatgory",[this])
  208. }else{
  209. this.selectAll(ev);
  210. this.selector.fireEvent("selectCatgory",[this])
  211. }
  212. ev.stopPropagation();
  213. }.bind(this));
  214. if( this.selector.css.selectorItemCategoryActionNode_selectAll_over ){
  215. this.selectAllNode.addEvents( {
  216. "mouseover" : function(ev){
  217. if( !this.isSelectedAll )this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll_over );
  218. //ev.stopPropagation();
  219. }.bind(this),
  220. "mouseout" : function(ev){
  221. if( !this.isSelectedAll )this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll );
  222. //ev.stopPropagation();
  223. }.bind(this)
  224. })
  225. }
  226. }
  227. }
  228. //this.actionNode.setStyles((this.selector.options.expand) ? this.selector.css.selectorItemCategoryActionNode_expand : this.selector.css.selectorItemCategoryActionNode_collapse);
  229. },
  230. unselectAll : function(ev, exclude){
  231. //( this.subItems || [] ).each( function(item){
  232. // if(item.isSelected)item.unSelected();
  233. //}.bind(this));
  234. var excludeList = exclude || [];
  235. if( exclude && typeOf(exclude) !== "array" )excludeList = [exclude];
  236. ( this.subItems || [] ).each( function(item){
  237. if(item.isSelected && !excludeList.contains(item) ){
  238. item.unSelected();
  239. }
  240. }.bind(this));
  241. if( this.selectAllNode && this.selector.css.selectorItemCategoryActionNode_selectAll ){
  242. this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll );
  243. }
  244. this.isSelectedAll = false;
  245. },
  246. unselectAllNested : function( ev, exclude ){
  247. this.unselectAll(ev, exclude );
  248. if( this.subCategorys && this.subCategorys.length ){
  249. this.subCategorys.each( function( category ){
  250. if(category.unselectAllNested)category.unselectAllNested( ev, exclude )
  251. })
  252. }
  253. if( this.subItems && this.subItems.length ){
  254. this.subItems.each( function( item ){
  255. if(item.unselectAllNested)item.unselectAllNested( ev, exclude )
  256. })
  257. }
  258. },
  259. selectAllNested : function(){
  260. this.selectAll();
  261. if( this.subCategorys && this.subCategorys.length ){
  262. this.subCategorys.each( function( category ){
  263. if(category.selectAllNested)category.selectAllNested()
  264. })
  265. }
  266. if( this.subItems && this.subItems.length ){
  267. this.subItems.each( function( item ){
  268. if(item.selectAllNested)item.selectAllNested()
  269. })
  270. }
  271. },
  272. selectAll: function(ev){
  273. if( this.loaded ){
  274. this._selectAll( ev )
  275. }else{
  276. this.loadSubItems(function(){
  277. this._selectAll( ev )
  278. }.bind(this));
  279. this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
  280. this.isExpand = true;
  281. }
  282. },
  283. _selectAll : function( ev ){
  284. if( !this.subItems || !this.subItems.length )return;
  285. var count = this.selector.options.maxCount || this.selector.options.count;
  286. if (!count) count = 0;
  287. var selectedSubItemCount = 0;
  288. this.subItems.each( function(item){
  289. if(item.isSelected)selectedSubItemCount++
  290. }.bind(this));
  291. if ((count.toInt()===0) || (this.selector.selectedItems.length+(this.subItems.length-selectedSubItemCount))<=count){
  292. this.subItems.each( function(item){
  293. if(!item.isSelected)item.selected();
  294. }.bind(this))
  295. if( this.selectAllNode && this.selector.css.selectorItemCategoryActionNode_selectAll_selected ){
  296. this.selectAllNode.setStyles( this.selector.css.selectorItemCategoryActionNode_selectAll_selected );
  297. }
  298. this.isSelectedAll = true;
  299. }else{
  300. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "最多可选择"+count+"个选项", this.node);
  301. }
  302. },
  303. loadSubItems: function( callback ){
  304. if (!this.loaded){
  305. if (!this.children){
  306. this.children = new Element("div", {
  307. "styles": this.selector.css.selectorItemCategoryChildrenNode
  308. }).inject(this.node, "after");
  309. }
  310. this.children.setStyle("display", "block");
  311. // if (!this.selector.options.expand) this.children.setStyle("display", "none");
  312. this.selector.orgAction.listSubUnitDirect(function(subJson){
  313. subJson.data.each(function(subData){
  314. if( !this.selector.isExcluded( subData ) ) {
  315. var category = this.selector._newItem(subData, this.selector, this.children, this.level + 1, this);
  316. if( !this.subItems )this.subItems = [];
  317. this.subItems.push( category );
  318. }
  319. }.bind(this));
  320. this.loaded = true;
  321. if(callback)callback();
  322. }.bind(this), null, this.data.distinguishedName);
  323. }else{
  324. this.children.setStyle("display", "block");
  325. }
  326. },
  327. getData: function(callback){
  328. if (callback) callback();
  329. },
  330. postLoad : function(){
  331. if( this.selector.options.style === "blue_flat" ){
  332. if( this.level === 1 ){
  333. var indent = 26;
  334. this.levelNode.setStyle("width", ""+indent+"px");
  335. }else{
  336. var indent = 26 + ( this.level -1 ) * this.selector.options.indent ;
  337. this.levelNode.setStyle("width", ""+indent+"px");
  338. }
  339. }
  340. }
  341. });
  342. MWF.xApplication.Selector.Unit.SearchItem = new Class({
  343. Extends: MWF.xApplication.Selector.Unit.Item,
  344. _getShowName: function(){
  345. return this.data.levelName || this.data.name;
  346. }
  347. });
  348. MWF.xApplication.Selector.Unit.ItemSelected = new Class({
  349. Extends: MWF.xApplication.Selector.Identity.ItemSelected,
  350. getData: function(callback){
  351. if (callback) callback();
  352. },
  353. _getTtiteText: function(){
  354. return this.data.levelName || this.data.name;
  355. },
  356. _getShowName: function(){
  357. return this.data.name+((this.data.levelName) ? "("+this.data.levelName+")" : "");
  358. },
  359. _setIcon: function(){
  360. this.iconNode.setStyle("background-image", "url("+"/x_component_Selector/$Selector/default/icon/departmenticon.png)");
  361. }
  362. });
  363. MWF.xApplication.Selector.Unit.ItemCategory = new Class({
  364. Extends: MWF.xApplication.Selector.Identity.ItemCategory,
  365. loadSub: function(callback){
  366. if (!this.loaded){
  367. this.selector.orgAction.listSubUnitDirect(function(subJson){
  368. subJson.data.each(function(subData){
  369. if( !this.selector.isExcluded( subData ) ) {
  370. var category = this.selector._newItem(subData, this.selector, this.children, this.level+1, this);
  371. if(this.subItems)this.subItems.push( category );
  372. this.subCategorys.push( category );
  373. }
  374. //var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1);
  375. }.bind(this));
  376. this.loaded = true;
  377. if (callback) callback();
  378. }.bind(this), null, this.data.distinguishedName);
  379. }else{
  380. if (callback) callback();
  381. }
  382. },
  383. _hasChild: function(){
  384. var uCount = (this.data.subDirectUnitCount) ? this.data.subDirectUnitCount : 0;
  385. //var iCount = (this.data.subDirectIdentityCount) ? this.data.subDirectIdentityCount : 0;
  386. return uCount;
  387. },
  388. _hasChildItem: function(){
  389. var uCount = (this.data.subDirectUnitCount) ? this.data.subDirectUnitCount : 0;
  390. return uCount;
  391. }
  392. });
  393. MWF.xApplication.Selector.Unit.Filter = new Class({
  394. Implements: [Options, Events],
  395. options: {
  396. "style": "default",
  397. "units": []
  398. },
  399. initialize: function(value, options){
  400. this.setOptions(options);
  401. this.value = value;
  402. this.orgAction = MWF.Actions.get("x_organization_assemble_control");
  403. },
  404. filter: function(value, callback){
  405. this.value = value;
  406. var key = this.value;
  407. if (this.options.units.length){
  408. var units = [];
  409. this.options.units.each(function(u){
  410. if (typeOf(u)==="string"){
  411. units.push(u);
  412. }
  413. if (typeOf(u)==="object"){
  414. units.push(u.distinguishedName);
  415. }
  416. });
  417. key = {"key": key, "unitList": units};
  418. }
  419. this.orgAction.listUnitByKey(function(json){
  420. data = json.data;
  421. if (callback) callback(data)
  422. }.bind(this), null, key);
  423. }
  424. });