Custom.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. MWF.xApplication.Selector = MWF.xApplication.Selector || {};
  2. MWF.xApplication.Template = MWF.xApplication.Template || {};
  3. MWF.xApplication.Template.Selector = MWF.xApplication.Template.Selector || {};
  4. MWF.xDesktop.requireApp("Selector", "Person", null, false);
  5. MWF.xDesktop.requireApp("Selector", "Unit", null, false);
  6. // this.options.selectableItems = [
  7. // {
  8. // "name": "项目1",
  9. // "id": "item1",
  10. // "isItem" : true //第一层的item需要isItem = true
  11. // },
  12. // {
  13. // "name": "项目2",
  14. // "id": "item2",
  15. // "isItem" : true //第一层的item需要isItem = true
  16. // },
  17. // {
  18. // "name": "分类1",
  19. // "id": "category1",
  20. // "subItemList": [
  21. // {
  22. // "id": "item1.1",
  23. // "name": "项目1.1"
  24. // },
  25. // {
  26. // "id": "item1.2",
  27. // "name": "项目1.2"
  28. // }
  29. // ],
  30. // "subCategoryList" : [
  31. // {
  32. // "name": "分类1.1",
  33. // "id": "category1.1",
  34. // "subItemList" : [
  35. // {
  36. // "id": "item1.1.1",
  37. // "name": "项目1.1.1"
  38. // }
  39. // ]
  40. // }
  41. // ]
  42. // }
  43. // ];
  44. MWF.xApplication.Template.Selector.Custom = new Class({
  45. Extends: MWF.xApplication.Selector.Person,
  46. options: {
  47. "style": "default",
  48. "count": 0,
  49. "title": "选择列表",
  50. "values": [],
  51. "selectableItems": [],
  52. "names": [],
  53. "category": false,
  54. "expand": true,
  55. "categorySelectable" : false,
  56. "expandSubEnable" : true,
  57. "uniqueFlag" : false,
  58. "defaultExpandLevel" : 1
  59. },
  60. initialize: function (container, options) {
  61. this.setOptions(options);
  62. this.path = "../x_component_Selector/$Selector/";
  63. this.cssPath = "../x_component_Selector/$Selector/" + this.options.style + "/css.wcss";
  64. this._loadCss(true);
  65. this.container = $(container);
  66. this.selectedItems = [];
  67. this.items = [];
  68. // this.categorys = [];
  69. this.subCategorys = []; //直接的分类
  70. this.subItems = []; //直接的选择项
  71. },
  72. loadSelectItems: function (addToNext) {
  73. if (!this.options.category) {
  74. this.options.selectableItems.each(function (it) {
  75. var name = typeOf(it) === "string" ? it : it.name;
  76. var id = typeOf(it) === "string" ? it : it.id;
  77. var item = this._newItem({name: name, id: id}, this, this.itemAreaNode);
  78. this.items.push(item);
  79. this.subItems.push( item );
  80. }.bind(this))
  81. } else {
  82. this.options.selectableItems.each(function (item, index) {
  83. if (item.isItem) {
  84. var item = this._newItem(item, this, this.itemAreaNode);
  85. this.items.push(item);
  86. this.subItems.push( item );
  87. }else{
  88. // if ( (item.subItemList && item.subItemList.length > 0) || item.subCategoryList && item.subCategoryList.length > 0 ) {
  89. if( this.options.categorySelectable ){
  90. var category = this._newItemCategorySelectable(item, this, this.itemAreaNode);
  91. this.items.push(category);
  92. this.subItems.push( category );
  93. this.subCategorys.push( category );
  94. }else{
  95. var category = this._newItemCategory(item, this, this.itemAreaNode);
  96. this.subCategorys.push( category );
  97. }
  98. // item.subItemList.each(function (subItem, index) {
  99. // var item = this._newItem(subItem, this, category.children, 2, category);
  100. // this.items.push(item);
  101. // category.subItems.push(item);
  102. // }.bind(this));
  103. // }
  104. }
  105. }.bind(this));
  106. }
  107. if(this.afterLoadSelectItem)this.afterLoadSelectItem();
  108. },
  109. _scrollEvent: function (y) {
  110. return true;
  111. },
  112. _getChildrenItemIds: function (data) {
  113. return data.subItemList || [];
  114. },
  115. _newItemCategory: function (data, selector, container, level, parentCategory, delay) {
  116. return new MWF.xApplication.Template.Selector.Custom.ItemCategory(data, selector, container, level, parentCategory, delay)
  117. },
  118. _newItemCategorySelectable: function (data, selector, container, level, category, delay) {
  119. return new MWF.xApplication.Template.Selector.Custom.ItemCategorySelectable(data, selector, container, level, category, delay)
  120. },
  121. _listItemByKey: function (callback, failure, key) {
  122. if (key) {
  123. this.initSearchArea(true);
  124. this.searchInItems(key);
  125. } else {
  126. this.initSearchArea(false);
  127. }
  128. },
  129. _newItemSelected: function (data, selector, container, level, category, delay) {
  130. return new MWF.xApplication.Template.Selector.Custom.ItemSelected(data, selector, container, level, category, delay)
  131. },
  132. _listItemByPinyin: function (callback, failure, key) {
  133. if (key) {
  134. this.initSearchArea(true);
  135. this.searchInItems(key);
  136. } else {
  137. this.initSearchArea(false);
  138. }
  139. },
  140. nestData : function( data, isItem ){
  141. if( !this.nestedData )this.nestedData = {};
  142. var setNest = function (d, isItem) {
  143. if( isItem ){
  144. this.nestedData[ d["id"] || d["name"] ] = d;
  145. }else if( this.options.categorySelectable ){
  146. this.nestedData[ d["id"] || d["name"] ] = { id : d.id , name : d.name };
  147. if( d.subItemList )this.nestData( d.subItemList, true );
  148. if( d.subCategoryList )this.nestData( d.subCategoryList );
  149. }else{
  150. if( d.subItemList )this.nestData( d.subItemList, true );
  151. if( d.subCategoryList )this.nestData( d.subCategoryList );
  152. }
  153. }.bind(this);
  154. if( data ){
  155. for( var i=0; i<data.length; i++ ){
  156. var d = data[i];
  157. setNest(d, isItem );
  158. }
  159. }else{
  160. for( var i=0; i<this.options.selectableItems.length; i++ ){
  161. var d = this.options.selectableItems[i];
  162. setNest(d, d.isItem);
  163. }
  164. }
  165. },
  166. _getItem: function (callback, failure, id, async, data) {
  167. if( !this.nestedData )this.nestData();
  168. if (callback) callback.apply(id, [{ "data": this.nestedData[id] || {"id": id} }]);
  169. },
  170. _newItem: function (data, selector, container, level, category, delay) {
  171. return new MWF.xApplication.Template.Selector.Custom.Item(data, selector, container, level, category, delay);
  172. },
  173. createItemsSearchData: function (callback) {
  174. if (!this.itemsSearchData) {
  175. this.itemsSearchData = [];
  176. MWF.require("MWF.widget.PinYin", function () {
  177. var initIds = [];
  178. this.items.each(function (item) {
  179. if (initIds.indexOf(item.data.name) == -1) {
  180. var text = item._getShowName().toLowerCase();
  181. var pinyin = text.toPY().toLowerCase();
  182. var firstPY = text.toPYFirst().toLowerCase();
  183. this.itemsSearchData.push({
  184. "text": text,
  185. "pinyin": pinyin,
  186. "firstPY": firstPY,
  187. "data": item.data
  188. });
  189. initIds.push(item.data.name);
  190. }
  191. }.bind(this));
  192. delete initIds;
  193. if (callback) callback();
  194. }.bind(this));
  195. } else {
  196. if (callback) callback();
  197. }
  198. }
  199. });
  200. MWF.xApplication.Template.Selector.Custom.Item = new Class({
  201. Extends: o2.xApplication.Selector.Person.Item,
  202. _getShowName: function () {
  203. return this.data.name;
  204. },
  205. _setIcon: function () {
  206. var style = this.selector.options.style;
  207. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  208. },
  209. _getTtiteText: function () {
  210. return this.data.name;
  211. },
  212. loadSubItem: function () {
  213. return false;
  214. },
  215. checkSelectedSingle: function () {
  216. var selectedItem = this.selector.options.values.filter(function (item, index) {
  217. if( this.selector.options.uniqueFlag ){
  218. var flag = this.selector.options.uniqueFlag;
  219. if (typeOf(item) === "object") return ( this.data[flag] && this.data[flag] === item[flag] );
  220. if (typeOf(item) === "string") return ( this.data[flag] && this.data[flag] === item );
  221. }else{
  222. if (typeOf(item) === "object") return ( this.data.id && this.data.id === item.id) || (this.data.name && this.data.name === item.name);
  223. if (typeOf(item) === "string") return ( this.data.id && this.data.id === item) || (this.data.name && this.data.name === item);
  224. }
  225. return false;
  226. }.bind(this));
  227. if (selectedItem.length) {
  228. this.selectedSingle();
  229. }
  230. },
  231. checkSelected: function () {
  232. var selectedItem = this.selector.selectedItems.filter(function (item, index) {
  233. if( this.selector.options.uniqueFlag ){
  234. var flag = this.selector.options.uniqueFlag;
  235. return ( item.data[flag] && item.data[flag] === this.data[flag]);
  236. }else{
  237. return ( item.data.id && item.data.id === this.data.id) || (item.data.name && item.data.name === this.data.name);
  238. }
  239. }.bind(this));
  240. if (selectedItem.length) {
  241. //selectedItem[0].item = this;
  242. selectedItem[0].addItem(this);
  243. this.selectedItem = selectedItem[0];
  244. this.setSelected();
  245. }
  246. },
  247. destroy: function(){
  248. if( this.isSelected )this.unSelected();
  249. this.selector.items.erase( this );
  250. if( this.category ){
  251. if( this.category.subCategorys && this.category.subCategorys.length ){
  252. this.category.subCategorys.erase( this );
  253. }
  254. if( this.category.subItems && this.category.subItems.length ){
  255. this.category.subItems.erase( this );
  256. }
  257. }
  258. if(this.node)this.node.destroy();
  259. delete this;
  260. }
  261. });
  262. MWF.xApplication.Template.Selector.Custom.ItemSelected = new Class({
  263. Extends: o2.xApplication.Selector.Person.ItemSelected,
  264. _getShowName: function () {
  265. return this.data.name;
  266. },
  267. _setIcon: function () {
  268. var style = this.selector.options.style;
  269. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  270. },
  271. _getTtiteText: function () {
  272. return this.data.name;
  273. },
  274. check: function () {
  275. if (this.selector.items.length) {
  276. var items = this.selector.items.filter(function (item, index) {
  277. if( this.selector.options.uniqueFlag ){
  278. var flag = this.selector.options.uniqueFlag;
  279. return ( item.data[flag] && item.data[flag] === this.data[flag]);
  280. }else{
  281. return ( item.data.id && item.data.id === this.data.id) || (item.data.name && item.data.name === this.data.name);
  282. }
  283. }.bind(this));
  284. this.items = items;
  285. if (items.length) {
  286. items.each(function (item) {
  287. item.selectedItem = this;
  288. item.setSelected();
  289. }.bind(this));
  290. }
  291. }
  292. if( this.afterCheck )this.afterCheck();
  293. }
  294. });
  295. MWF.xApplication.Template.Selector.Custom.ItemCategory = new Class({
  296. Extends: o2.xApplication.Selector.Person.ItemCategory,
  297. _getShowName: function () {
  298. return this.data.name;
  299. },
  300. createNode: function () {
  301. this.node = new Element("div", {
  302. "styles": this.selector.css.selectorItemCategory_department
  303. }).inject(this.container);
  304. },
  305. _setIcon: function () {
  306. var style = this.selector.options.style;
  307. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  308. },
  309. _getTtiteText: function () {
  310. return this.data.name;
  311. },
  312. clickItem: function (callback) {
  313. if (this._hasChild() || this.selector.options.expandEmptyCategory ) {
  314. var firstLoaded = !this.loaded;
  315. if( !firstLoaded || this.selector.options.expandEmptyCategory ){
  316. if(this.isExpand){
  317. this.selector.fireEvent("collapse", [this] );
  318. }else{
  319. this.selector.fireEvent("expand", [this] );
  320. }
  321. }
  322. this.loadSub(function () {
  323. if (firstLoaded && this._hasChild() ) {
  324. if (!this.selector.isFlatCategory) {
  325. this.children.setStyles({"display": "block", "height": "auto"});
  326. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  327. this.isExpand = true;
  328. }
  329. // this.checkSelectAll();
  330. } else {
  331. var display = this.children.getStyle("display");
  332. if (display === "none") {
  333. // this.selector.fireEvent("expand", [this] );
  334. this.children.setStyles({"display": "block", "height": "auto"});
  335. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  336. this.isExpand = true;
  337. } else {
  338. // this.selector.fireEvent("collapse", [this] );
  339. this.children.setStyles({"display": "none", "height": "0px"});
  340. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  341. this.isExpand = false;
  342. }
  343. }
  344. if (callback) callback();
  345. }.bind(this));
  346. }
  347. },
  348. destroy : function(){
  349. while( this.subItems.length )this.subItems[0].destroy();
  350. while( this.subCategorys.length )this.subCategorys[0].destroy();
  351. if( this.category && this.category.subCategorys && this.category.subCategorys.length ){
  352. this.category.subCategorys.erase( this );
  353. }
  354. if(this.node)this.node.destroy();
  355. delete this;
  356. },
  357. reloadSub : function(callback){
  358. while( this.subItems.length )this.subItems[0].destroy();
  359. this.subItems = [];
  360. while( this.subCategorys.length )this.subCategorys[0].destroy();
  361. this.subCategorys = [];
  362. this.loaded = false;
  363. this.loadSub( callback )
  364. },
  365. loadSub: function (callback) {
  366. if (!this.loaded) {
  367. if( this._hasChildItem() ){
  368. this.data.subItemList.each(function (subItem, index) {
  369. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  370. this.selector.items.push(item);
  371. if(this.subItems)this.subItems.push( item );
  372. }.bind(this));
  373. }
  374. if ( this._hasChildCategory() ) {
  375. this.data.subCategoryList.each(function (subCategory, index) {
  376. var category = this.selector._newItemCategory(subCategory, this.selector, this.children, this.level + 1, this);
  377. this.subCategorys.push( category );
  378. }.bind(this));
  379. }
  380. this.loaded = true;
  381. if (callback) callback();
  382. } else {
  383. if (callback) callback();
  384. }
  385. },
  386. _hasChildCategory: function () {
  387. return (this.data.subCategoryList && this.data.subCategoryList.length);
  388. },
  389. _hasChildItem: function () {
  390. return (this.data.subItemList && this.data.subItemList.length);
  391. },
  392. _hasChild: function () {
  393. return this._hasChildCategory() || this._hasChildItem();
  394. },
  395. check: function () {
  396. },
  397. afterLoad: function(){
  398. if ( this.level <= this.selector.options.defaultExpandLevel && (this._hasChild()) ){
  399. this.clickItem();
  400. }else{
  401. this.children.hide();
  402. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  403. this.isExpand = false;
  404. }
  405. }
  406. });
  407. MWF.xApplication.Template.Selector.Custom.ItemCategorySelectable = new Class({
  408. Extends: o2.xApplication.Selector.Unit.Item,
  409. _getShowName: function () {
  410. return this.data.name;
  411. },
  412. createNode: function () {
  413. // this.node = new Element("div", {
  414. // "styles": this.selector.css.selectorItemCategory_department //this.selector.css.selectorItemCategory_department
  415. // }).inject(this.container);
  416. },
  417. _setIcon: function () {
  418. var style = this.selector.options.style;
  419. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  420. },
  421. _getTtiteText: function () {
  422. return this.data.name;
  423. },
  424. destroy : function(){
  425. if( this.isSelected )this.unSelected();
  426. this.selector.items.erase( this );
  427. while( this.subItems.length )this.subItems[0].destroy();
  428. while( this.subCategorys.length )this.subCategorys[0].destroy();
  429. if( this.category ){
  430. if( this.category.subCategorys && this.category.subCategorys.length ){
  431. this.category.subCategorys.erase( this );
  432. }
  433. if( this.category.subItems && this.category.subItems.length ){
  434. this.category.subItems.erase( this );
  435. }
  436. }
  437. if(this.node)this.node.destroy();
  438. delete this;
  439. },
  440. reloadSub : function(callback){
  441. while( this.subItems.length )this.subItems[0].destroy();
  442. this.subItems = [];
  443. while( this.subCategorys.length )this.subCategorys[0].destroy();
  444. this.subCategorys = [];
  445. this.loaded = false;
  446. this.loadSubItems( callback );
  447. },
  448. loadSubItems: function( callback ){
  449. if (!this.loaded){
  450. if (!this.children){
  451. this.children = new Element("div", {
  452. "styles": this.selector.css.selectorItemCategoryChildrenNode
  453. }).inject(this.node, "after");
  454. }
  455. this.children.setStyle("display", "block");
  456. // if (!this.selector.options.expand) this.children.setStyle("display", "none");
  457. if( this._hasChildItem() ){
  458. this.data.subItemList.each(function (subItem, index) {
  459. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  460. this.selector.items.push(item);
  461. if(this.subItems)this.subItems.push( item );
  462. }.bind(this));
  463. }
  464. if ( this._hasChildCategory() ) {
  465. this.data.subCategoryList.each(function (subCategory, index) {
  466. var category = this.selector._newItemCategorySelectable(subCategory, this.selector, this.children, this.level + 1, this);
  467. this.selector.items.push(category);
  468. this.subCategorys.push( category );
  469. }.bind(this));
  470. }
  471. this.loaded = true;
  472. if(callback)callback();
  473. }else{
  474. this.children.setStyle("display", "block");
  475. }
  476. },
  477. loadCategoryChildren : function( callback ){
  478. if (!this.categoryLoaded){
  479. if ( this._hasChildCategory() ) {
  480. this.data.subCategoryList.each(function (subCategory, index) {
  481. var category = this.selector._newItemCategorySelectable(subCategory, this.selector, this.children, this.level + 1, this);
  482. this.selector.items.push(category);
  483. this.subCategorys.push( category );
  484. }.bind(this));
  485. }
  486. this.categoryLoaded = true;
  487. if(callback)callback();
  488. }else{
  489. if(callback)callback();
  490. }
  491. },
  492. loadItemChildren : function( callback ){
  493. if (!this.itemLoaded){
  494. if( this._hasChildItem() ){
  495. this.data.subItemList.each(function (subItem, index) {
  496. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  497. this.selector.items.push(item);
  498. if(this.subItems)this.subItems.push( item );
  499. }.bind(this));
  500. }
  501. this.itemLoaded = true;
  502. if(callback)callback();
  503. }else{
  504. if(callback)callback();
  505. }
  506. },
  507. _hasChildCategory: function () {
  508. return (this.data.subCategoryList && this.data.subCategoryList.length);
  509. },
  510. _hasChildItem: function () {
  511. return (this.data.subItemList && this.data.subItemList.length);
  512. },
  513. _hasChild: function () {
  514. return this._hasChildCategory() || this._hasChildItem();
  515. },
  516. checkSelectedSingle: function () {
  517. var selectedItem = this.selector.options.values.filter(function (item, index) {
  518. if( this.selector.options.uniqueFlag ){
  519. var flag = this.selector.options.uniqueFlag;
  520. if (typeOf(item) === "object") return ( this.data[flag] && this.data[flag] === item[flag] );
  521. if (typeOf(item) === "string") return ( this.data[flag] && this.data[flag] === item );
  522. }else{
  523. if (typeOf(item) === "object") return ( this.data.id && this.data.id === item.id) || (this.data.name && this.data.name === item.name);
  524. if (typeOf(item) === "string") return ( this.data.id && this.data.id === item) || (this.data.name && this.data.name === item);
  525. }
  526. return false;
  527. }.bind(this));
  528. if (selectedItem.length) {
  529. this.selectedSingle();
  530. }
  531. },
  532. checkSelected: function () {
  533. var selectedItem = this.selector.selectedItems.filter(function (item, index) {
  534. if( this.selector.options.uniqueFlag ){
  535. var flag = this.selector.options.uniqueFlag;
  536. return ( item.data[flag] && item.data[flag] === this.data[flag]);
  537. }else{
  538. return ( item.data.id && item.data.id === this.data.id) || (item.data.name && item.data.name === this.data.name);
  539. }
  540. }.bind(this));
  541. if (selectedItem.length) {
  542. //selectedItem[0].item = this;
  543. selectedItem[0].addItem(this);
  544. this.selectedItem = selectedItem[0];
  545. this.setSelected();
  546. }
  547. },
  548. check: function () {
  549. this.checkSelected();
  550. },
  551. afterLoad : function () {
  552. }
  553. });