Custom.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. });
  230. MWF.xApplication.Template.Selector.Custom.ItemSelected = new Class({
  231. Extends: o2.xApplication.Selector.Person.ItemSelected,
  232. _getShowName: function () {
  233. return this.data.name;
  234. },
  235. _setIcon: function () {
  236. var style = this.selector.options.style;
  237. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  238. },
  239. _getTtiteText: function () {
  240. return this.data.name;
  241. },
  242. check: function () {
  243. if (this.selector.items.length) {
  244. var items = this.selector.items.filter(function (item, index) {
  245. return (item.data.id === this.data.id) || (item.data.name === this.data.name);
  246. }.bind(this));
  247. this.items = items;
  248. if (items.length) {
  249. items.each(function (item) {
  250. item.selectedItem = this;
  251. item.setSelected();
  252. }.bind(this));
  253. }
  254. }
  255. }
  256. });
  257. MWF.xApplication.Template.Selector.Custom.ItemCategory = new Class({
  258. Extends: o2.xApplication.Selector.Person.ItemCategory,
  259. _getShowName: function () {
  260. return this.data.name;
  261. },
  262. createNode: function () {
  263. this.node = new Element("div", {
  264. "styles": this.selector.css.selectorItemCategory_department
  265. }).inject(this.container);
  266. },
  267. _setIcon: function () {
  268. var style = this.selector.options.style;
  269. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  270. },
  271. _getTtiteText: function () {
  272. return this.data.name;
  273. },
  274. clickItem: function (callback) {
  275. debugger;
  276. if (this._hasChild()) {
  277. var firstLoaded = !this.loaded;
  278. this.loadSub(function () {
  279. if (firstLoaded) {
  280. if (!this.selector.isFlatCategory) {
  281. this.children.setStyles({"display": "block", "height": "auto"});
  282. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  283. this.isExpand = true;
  284. }
  285. } else {
  286. var display = this.children.getStyle("display");
  287. if (display === "none") {
  288. this.children.setStyles({"display": "block", "height": "auto"});
  289. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  290. this.isExpand = true;
  291. } else {
  292. this.children.setStyles({"display": "none", "height": "0px"});
  293. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  294. this.isExpand = false;
  295. }
  296. }
  297. if (callback) callback();
  298. }.bind(this));
  299. }
  300. },
  301. loadSub: function (callback) {
  302. debugger;
  303. if (!this.loaded) {
  304. if( this._hasChildItem() ){
  305. this.data.subItemList.each(function (subItem, index) {
  306. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  307. this.selector.items.push(item);
  308. if(this.subItems)this.subItems.push( item );
  309. }.bind(this));
  310. }
  311. if ( this._hasChildCategory() ) {
  312. this.data.subCategoryList.each(function (subCategory, index) {
  313. var category = this.selector._newItemCategory(subCategory, this.selector, this.children, this.level + 1, this);
  314. this.subCategorys.push( category );
  315. }.bind(this));
  316. }
  317. this.loaded = true;
  318. if (callback) callback();
  319. } else {
  320. if (callback) callback();
  321. }
  322. },
  323. _hasChildCategory: function () {
  324. return (this.data.subCategoryList && this.data.subCategoryList.length);
  325. },
  326. _hasChildItem: function () {
  327. return (this.data.subItemList && this.data.subItemList.length);
  328. },
  329. _hasChild: function () {
  330. return this._hasChildCategory() || this._hasChildItem();
  331. },
  332. check: function () {
  333. }
  334. });
  335. MWF.xApplication.Template.Selector.Custom.ItemCategorySelectable = new Class({
  336. Extends: o2.xApplication.Selector.Unit.Item,
  337. _getShowName: function () {
  338. return this.data.name;
  339. },
  340. createNode: function () {
  341. // this.node = new Element("div", {
  342. // "styles": this.selector.css.selectorItemCategory_department //this.selector.css.selectorItemCategory_department
  343. // }).inject(this.container);
  344. },
  345. _setIcon: function () {
  346. var style = this.selector.options.style;
  347. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  348. },
  349. _getTtiteText: function () {
  350. return this.data.name;
  351. },
  352. loadSubItems: function( callback ){
  353. if (!this.loaded){
  354. if (!this.children){
  355. this.children = new Element("div", {
  356. "styles": this.selector.css.selectorItemCategoryChildrenNode
  357. }).inject(this.node, "after");
  358. }
  359. this.children.setStyle("display", "block");
  360. // if (!this.selector.options.expand) this.children.setStyle("display", "none");
  361. if( this._hasChildItem() ){
  362. this.data.subItemList.each(function (subItem, index) {
  363. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  364. this.selector.items.push(item);
  365. if(this.subItems)this.subItems.push( item );
  366. }.bind(this));
  367. }
  368. if ( this._hasChildCategory() ) {
  369. this.data.subCategoryList.each(function (subCategory, index) {
  370. var category = this.selector._newItemCategorySelectable(subCategory, this.selector, this.children, this.level + 1, this);
  371. this.selector.items.push(category);
  372. this.subCategorys.push( category );
  373. }.bind(this));
  374. }
  375. this.loaded = true;
  376. if(callback)callback();
  377. }else{
  378. this.children.setStyle("display", "block");
  379. }
  380. },
  381. loadCategoryChildren : function( callback ){
  382. if (!this.categoryLoaded){
  383. if ( this._hasChildCategory() ) {
  384. this.data.subCategoryList.each(function (subCategory, index) {
  385. var category = this.selector._newItemCategorySelectable(subCategory, this.selector, this.children, this.level + 1, this);
  386. this.selector.items.push(category);
  387. this.subCategorys.push( category );
  388. }.bind(this));
  389. }
  390. this.categoryLoaded = true;
  391. if(callback)callback();
  392. }else{
  393. if(callback)callback();
  394. }
  395. },
  396. loadItemChildren : function( callback ){
  397. if (!this.itemLoaded){
  398. if( this._hasChildItem() ){
  399. this.data.subItemList.each(function (subItem, index) {
  400. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  401. this.selector.items.push(item);
  402. if(this.subItems)this.subItems.push( item );
  403. }.bind(this));
  404. }
  405. this.itemLoaded = true;
  406. if(callback)callback();
  407. }else{
  408. if(callback)callback();
  409. }
  410. },
  411. _hasChildCategory: function () {
  412. return (this.data.subCategoryList && this.data.subCategoryList.length);
  413. },
  414. _hasChildItem: function () {
  415. return (this.data.subItemList && this.data.subItemList.length);
  416. },
  417. _hasChild: function () {
  418. return this._hasChildCategory() || this._hasChildItem();
  419. },
  420. checkSelectedSingle: function () {
  421. var selectedItem = this.selector.options.values.filter(function (item, index) {
  422. if (typeOf(item) === "object") return ( this.data.id && this.data.id === item.id) || (this.data.name && this.data.name === item.name);
  423. if (typeOf(item) === "string") return ( this.data.id && this.data.id === item) || (this.data.name && this.data.name === item);
  424. return false;
  425. }.bind(this));
  426. if (selectedItem.length) {
  427. this.selectedSingle();
  428. }
  429. },
  430. checkSelected: function () {
  431. var selectedItem = this.selector.selectedItems.filter(function (item, index) {
  432. return ( item.data.id && item.data.id === this.data.id) || (item.data.name && item.data.name === this.data.name);
  433. }.bind(this));
  434. if (selectedItem.length) {
  435. //selectedItem[0].item = this;
  436. selectedItem[0].addItem(this);
  437. this.selectedItem = selectedItem[0];
  438. this.setSelected();
  439. }
  440. },
  441. check: function () {
  442. this.checkSelected();
  443. }
  444. });