Custom.js 18 KB

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