Custom.js 20 KB

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