Custom.js 22 KB

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