Custom.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // this.options.selectableItems = [
  6. // {
  7. // "name": "项目1",
  8. // "id": "item1",
  9. // "isItem" : true //第一层的item需要isItem = true
  10. // },
  11. // {
  12. // "name": "项目2",
  13. // "id": "item2",
  14. // "isItem" : true //第一层的item需要isItem = true
  15. // },
  16. // {
  17. // "name": "分类1",
  18. // "id": "category1",
  19. // "subItemList": [
  20. // {
  21. // "id": "item1.1",
  22. // "name": "项目1.1"
  23. // },
  24. // {
  25. // "id": "item1.2",
  26. // "name": "项目1.2"
  27. // }
  28. // ],
  29. // "subCategoryList" : [
  30. // {
  31. // "name": "分类1.1",
  32. // "id": "category1.1",
  33. // "subItemList" : [
  34. // {
  35. // "id": "item1.1.1",
  36. // "name": "项目1.1.1"
  37. // }
  38. // ]
  39. // }
  40. // ]
  41. // }
  42. // ];
  43. MWF.xApplication.Template.Selector.Custom = new Class({
  44. Extends: MWF.xApplication.Selector.Person,
  45. options: {
  46. "style": "default",
  47. "count": 0,
  48. "title": "选择列表",
  49. "values": [],
  50. "selectableItems": [],
  51. "names": [],
  52. "category": false,
  53. "expand": true,
  54. "expandSubEnable" : true
  55. },
  56. initialize: function (container, options) {
  57. this.setOptions(options);
  58. this.path = "../x_component_Selector/$Selector/";
  59. this.cssPath = "../x_component_Selector/$Selector/" + this.options.style + "/css.wcss";
  60. this._loadCss(true);
  61. this.container = $(container);
  62. this.selectedItems = [];
  63. this.items = [];
  64. this.categorys = [];
  65. },
  66. loadSelectItems: function (addToNext) {
  67. debugger;
  68. if (!this.options.category) {
  69. this.options.selectableItems.each(function (it) {
  70. var name = typeOf(it) === "string" ? it : it.name;
  71. var id = typeOf(it) === "string" ? it : it.id;
  72. var item = this._newItem({name: name, id: id}, this, this.itemAreaNode);
  73. this.items.push(item);
  74. }.bind(this))
  75. } else {
  76. this.options.selectableItems.each(function (item, index) {
  77. if (item.isItem) {
  78. var item = this._newItem(item, this, this.itemAreaNode);
  79. this.items.push(item);
  80. }
  81. if ( (item.subItemList && item.subItemList.length > 0) || item.subCategoryList && item.subCategoryList.length > 0 ) {
  82. var category = this._newItemCategory(item, this, this.itemAreaNode);
  83. this.categorys.push( category );
  84. // item.subItemList.each(function (subItem, index) {
  85. // var item = this._newItem(subItem, this, category.children, 2, category);
  86. // this.items.push(item);
  87. // category.subItems.push(item);
  88. // }.bind(this));
  89. }
  90. }.bind(this));
  91. }
  92. },
  93. _scrollEvent: function (y) {
  94. return true;
  95. },
  96. _getChildrenItemIds: function (data) {
  97. return data.subItemList || [];
  98. },
  99. _newItemCategory: function (data, selector, item, level) {
  100. return new MWF.xApplication.Template.Selector.Custom.ItemCategory(data, selector, item, level)
  101. },
  102. _listItemByKey: function (callback, failure, key) {
  103. if (key) {
  104. this.initSearchArea(true);
  105. this.searchInItems(key);
  106. } else {
  107. this.initSearchArea(false);
  108. }
  109. },
  110. _newItemSelected: function (data, selector, item) {
  111. return new MWF.xApplication.Template.Selector.Custom.ItemSelected(data, selector, item)
  112. },
  113. _listItemByPinyin: function (callback, failure, key) {
  114. if (key) {
  115. this.initSearchArea(true);
  116. this.searchInItems(key);
  117. } else {
  118. this.initSearchArea(false);
  119. }
  120. },
  121. _getItem: function (callback, failure, id, async) {
  122. if (callback) callback.apply(id, [{"id": id}]);
  123. },
  124. _newItem: function (data, selector, container, level, category) {
  125. return new MWF.xApplication.Template.Selector.Custom.Item(data, selector, container, level, category);
  126. },
  127. createItemsSearchData: function (callback) {
  128. if (!this.itemsSearchData) {
  129. this.itemsSearchData = [];
  130. MWF.require("MWF.widget.PinYin", function () {
  131. var initIds = [];
  132. this.items.each(function (item) {
  133. if (initIds.indexOf(item.data.name) == -1) {
  134. var text = item._getShowName().toLowerCase();
  135. var pinyin = text.toPY().toLowerCase();
  136. var firstPY = text.toPYFirst().toLowerCase();
  137. this.itemsSearchData.push({
  138. "text": text,
  139. "pinyin": pinyin,
  140. "firstPY": firstPY,
  141. "data": item.data
  142. });
  143. initIds.push(item.data.name);
  144. }
  145. }.bind(this));
  146. delete initIds;
  147. if (callback) callback();
  148. }.bind(this));
  149. } else {
  150. if (callback) callback();
  151. }
  152. }
  153. });
  154. MWF.xApplication.Template.Selector.Custom.Item = new Class({
  155. Extends: o2.xApplication.Selector.Person.Item,
  156. _getShowName: function () {
  157. return this.data.name;
  158. },
  159. _setIcon: function () {
  160. var style = this.selector.options.style;
  161. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  162. },
  163. _getTtiteText: function () {
  164. return this.data.name;
  165. },
  166. loadSubItem: function () {
  167. return false;
  168. },
  169. checkSelectedSingle: function () {
  170. var selectedItem = this.selector.options.values.filter(function (item, index) {
  171. if (typeOf(item) === "object") return (this.data.id === item.id) || (this.data.name === item.name);
  172. if (typeOf(item) === "string") return (this.data.id === item) || (this.data.name === item);
  173. return false;
  174. }.bind(this));
  175. if (selectedItem.length) {
  176. this.selectedSingle();
  177. }
  178. },
  179. checkSelected: function () {
  180. var selectedItem = this.selector.selectedItems.filter(function (item, index) {
  181. return (item.data.id === this.data.id) || (item.data.name === this.data.name);
  182. }.bind(this));
  183. if (selectedItem.length) {
  184. //selectedItem[0].item = this;
  185. selectedItem[0].addItem(this);
  186. this.selectedItem = selectedItem[0];
  187. this.setSelected();
  188. }
  189. }
  190. });
  191. MWF.xApplication.Template.Selector.Custom.ItemSelected = new Class({
  192. Extends: o2.xApplication.Selector.Person.ItemSelected,
  193. _getShowName: function () {
  194. return this.data.name;
  195. },
  196. _setIcon: function () {
  197. var style = this.selector.options.style;
  198. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  199. },
  200. _getTtiteText: function () {
  201. return this.data.name;
  202. },
  203. check: function () {
  204. if (this.selector.items.length) {
  205. var items = this.selector.items.filter(function (item, index) {
  206. return (item.data.id === this.data.id) || (item.data.name === this.data.name);
  207. }.bind(this));
  208. this.items = items;
  209. if (items.length) {
  210. items.each(function (item) {
  211. item.selectedItem = this;
  212. item.setSelected();
  213. }.bind(this));
  214. }
  215. }
  216. }
  217. });
  218. MWF.xApplication.Template.Selector.Custom.ItemCategory = new Class({
  219. Extends: o2.xApplication.Selector.Person.ItemCategory,
  220. _getShowName: function () {
  221. return this.data.name;
  222. },
  223. createNode: function () {
  224. this.node = new Element("div", {
  225. "styles": this.selector.css.selectorItemCategory_department
  226. }).inject(this.container);
  227. },
  228. _setIcon: function () {
  229. var style = this.selector.options.style;
  230. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  231. },
  232. _getTtiteText: function () {
  233. return this.data.name;
  234. },
  235. clickItem: function (callback) {
  236. if (this._hasChild()) {
  237. var firstLoaded = !this.loaded;
  238. debugger;
  239. this.loadSub(function () {
  240. if (firstLoaded) {
  241. if (!this.selector.isFlatCategory) {
  242. this.children.setStyles({"display": "block", "height": "auto"});
  243. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  244. this.isExpand = true;
  245. }
  246. } else {
  247. var display = this.children.getStyle("display");
  248. if (display === "none") {
  249. this.children.setStyles({"display": "block", "height": "auto"});
  250. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  251. this.isExpand = true;
  252. } else {
  253. this.children.setStyles({"display": "none", "height": "0px"});
  254. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  255. this.isExpand = false;
  256. }
  257. }
  258. if (callback) callback();
  259. }.bind(this));
  260. }
  261. },
  262. loadSub: function (callback) {
  263. debugger;
  264. if (!this.loaded) {
  265. if( this._hasChild() ){
  266. this.data.subItemList.each(function (subItem, index) {
  267. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  268. this.selector.items.push(item);
  269. if(this.subItems)this.subItems.push( item );
  270. }.bind(this));
  271. }
  272. if ( this._hasChildCategory() ) {
  273. this.data.subCategoryList.each(function (subCategory, index) {
  274. var category = this.selector._newItemCategory(subCategory, this.selector, this.children, this.level + 1, this);
  275. this.subCategorys.push( category );
  276. }.bind(this));
  277. }
  278. this.loaded = true;
  279. if (callback) callback();
  280. } else {
  281. if (callback) callback();
  282. }
  283. },
  284. _hasChildCategory: function () {
  285. return (this.data.subCategoryList && this.data.subCategoryList.length);
  286. },
  287. _hasChildItem: function () {
  288. return this._hasChild();
  289. },
  290. _hasChild: function () {
  291. return (this.data.subItemList && this.data.subItemList.length);
  292. },
  293. check: function () {
  294. }
  295. });