Custom.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. _getItem: function (callback, failure, id, async) {
  133. if (callback) callback.apply(id, [{"id": id}]);
  134. },
  135. _newItem: function (data, selector, container, level, category, delay) {
  136. return new MWF.xApplication.Template.Selector.Custom.Item(data, selector, container, level, category, delay);
  137. },
  138. createItemsSearchData: function (callback) {
  139. if (!this.itemsSearchData) {
  140. this.itemsSearchData = [];
  141. MWF.require("MWF.widget.PinYin", function () {
  142. var initIds = [];
  143. this.items.each(function (item) {
  144. if (initIds.indexOf(item.data.name) == -1) {
  145. var text = item._getShowName().toLowerCase();
  146. var pinyin = text.toPY().toLowerCase();
  147. var firstPY = text.toPYFirst().toLowerCase();
  148. this.itemsSearchData.push({
  149. "text": text,
  150. "pinyin": pinyin,
  151. "firstPY": firstPY,
  152. "data": item.data
  153. });
  154. initIds.push(item.data.name);
  155. }
  156. }.bind(this));
  157. delete initIds;
  158. if (callback) callback();
  159. }.bind(this));
  160. } else {
  161. if (callback) callback();
  162. }
  163. }
  164. });
  165. MWF.xApplication.Template.Selector.Custom.Item = new Class({
  166. Extends: o2.xApplication.Selector.Person.Item,
  167. _getShowName: function () {
  168. return this.data.name;
  169. },
  170. _setIcon: function () {
  171. var style = this.selector.options.style;
  172. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  173. },
  174. _getTtiteText: function () {
  175. return this.data.name;
  176. },
  177. loadSubItem: function () {
  178. return false;
  179. },
  180. checkSelectedSingle: function () {
  181. var selectedItem = this.selector.options.values.filter(function (item, index) {
  182. if (typeOf(item) === "object") return (this.data.id === item.id) || (this.data.name === item.name);
  183. if (typeOf(item) === "string") return (this.data.id === item) || (this.data.name === item);
  184. return false;
  185. }.bind(this));
  186. if (selectedItem.length) {
  187. this.selectedSingle();
  188. }
  189. },
  190. checkSelected: function () {
  191. var selectedItem = this.selector.selectedItems.filter(function (item, index) {
  192. return (item.data.id === this.data.id) || (item.data.name === this.data.name);
  193. }.bind(this));
  194. if (selectedItem.length) {
  195. //selectedItem[0].item = this;
  196. selectedItem[0].addItem(this);
  197. this.selectedItem = selectedItem[0];
  198. this.setSelected();
  199. }
  200. }
  201. });
  202. MWF.xApplication.Template.Selector.Custom.ItemSelected = new Class({
  203. Extends: o2.xApplication.Selector.Person.ItemSelected,
  204. _getShowName: function () {
  205. return this.data.name;
  206. },
  207. _setIcon: function () {
  208. var style = this.selector.options.style;
  209. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  210. },
  211. _getTtiteText: function () {
  212. return this.data.name;
  213. },
  214. check: function () {
  215. if (this.selector.items.length) {
  216. var items = this.selector.items.filter(function (item, index) {
  217. return (item.data.id === this.data.id) || (item.data.name === this.data.name);
  218. }.bind(this));
  219. this.items = items;
  220. if (items.length) {
  221. items.each(function (item) {
  222. item.selectedItem = this;
  223. item.setSelected();
  224. }.bind(this));
  225. }
  226. }
  227. }
  228. });
  229. MWF.xApplication.Template.Selector.Custom.ItemCategory = new Class({
  230. Extends: o2.xApplication.Selector.Person.ItemCategory,
  231. _getShowName: function () {
  232. return this.data.name;
  233. },
  234. createNode: function () {
  235. this.node = new Element("div", {
  236. "styles": this.selector.css.selectorItemCategory_department
  237. }).inject(this.container);
  238. },
  239. _setIcon: function () {
  240. var style = this.selector.options.style;
  241. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  242. },
  243. _getTtiteText: function () {
  244. return this.data.name;
  245. },
  246. clickItem: function (callback) {
  247. if (this._hasChild()) {
  248. var firstLoaded = !this.loaded;
  249. debugger;
  250. this.loadSub(function () {
  251. if (firstLoaded) {
  252. if (!this.selector.isFlatCategory) {
  253. this.children.setStyles({"display": "block", "height": "auto"});
  254. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  255. this.isExpand = true;
  256. }
  257. } else {
  258. var display = this.children.getStyle("display");
  259. if (display === "none") {
  260. this.children.setStyles({"display": "block", "height": "auto"});
  261. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  262. this.isExpand = true;
  263. } else {
  264. this.children.setStyles({"display": "none", "height": "0px"});
  265. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  266. this.isExpand = false;
  267. }
  268. }
  269. if (callback) callback();
  270. }.bind(this));
  271. }
  272. },
  273. loadSub: function (callback) {
  274. debugger;
  275. if (!this.loaded) {
  276. if( this._hasChild() ){
  277. this.data.subItemList.each(function (subItem, index) {
  278. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  279. this.selector.items.push(item);
  280. if(this.subItems)this.subItems.push( item );
  281. }.bind(this));
  282. }
  283. if ( this._hasChildCategory() ) {
  284. this.data.subCategoryList.each(function (subCategory, index) {
  285. var category = this.selector._newItemCategory(subCategory, this.selector, this.children, this.level + 1, this);
  286. this.subCategorys.push( category );
  287. }.bind(this));
  288. }
  289. this.loaded = true;
  290. if (callback) callback();
  291. } else {
  292. if (callback) callback();
  293. }
  294. },
  295. _hasChildCategory: function () {
  296. return (this.data.subCategoryList && this.data.subCategoryList.length);
  297. },
  298. _hasChildItem: function () {
  299. return (this.data.subItemList && this.data.subItemList.length);
  300. },
  301. _hasChild: function () {
  302. return this._hasChildCategory() || this._hasChildItem();
  303. },
  304. check: function () {
  305. }
  306. });
  307. MWF.xApplication.Template.Selector.Custom.ItemCategorySelectable = new Class({
  308. Extends: o2.xApplication.Selector.Unit.Item,
  309. _getShowName: function () {
  310. return this.data.name;
  311. },
  312. createNode: function () {
  313. // this.node = new Element("div", {
  314. // "styles": this.selector.css.selectorItemCategory_department //this.selector.css.selectorItemCategory_department
  315. // }).inject(this.container);
  316. },
  317. _setIcon: function () {
  318. var style = this.selector.options.style;
  319. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  320. },
  321. _getTtiteText: function () {
  322. return this.data.name;
  323. },
  324. loadSubItems: function( callback ){
  325. if (!this.loaded){
  326. if (!this.children){
  327. this.children = new Element("div", {
  328. "styles": this.selector.css.selectorItemCategoryChildrenNode
  329. }).inject(this.node, "after");
  330. }
  331. this.children.setStyle("display", "block");
  332. // if (!this.selector.options.expand) this.children.setStyle("display", "none");
  333. if( this._hasChild() ){
  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._newItemCategorySelectable(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. this.children.setStyle("display", "block");
  350. }
  351. },
  352. loadCategoryChildren : function( callback ){
  353. if (!this.categoryLoaded){
  354. if ( this._hasChildCategory() ) {
  355. this.data.subCategoryList.each(function (subCategory, index) {
  356. var category = this.selector._newItemCategorySelectable(subCategory, this.selector, this.children, this.level + 1, this);
  357. this.subCategorys.push( category );
  358. }.bind(this));
  359. }
  360. this.categoryLoaded = true;
  361. if(callback)callback();
  362. }else{
  363. if(callback)callback();
  364. }
  365. },
  366. loadItemChildren : function( callback ){
  367. if (!this.itemLoaded){
  368. if( this._hasChild() ){
  369. this.data.subItemList.each(function (subItem, index) {
  370. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  371. this.selector.items.push(item);
  372. if(this.subItems)this.subItems.push( item );
  373. }.bind(this));
  374. }
  375. this.itemLoaded = true;
  376. if(callback)callback();
  377. }else{
  378. if(callback)callback();
  379. }
  380. },
  381. _hasChildCategory: function () {
  382. return (this.data.subCategoryList && this.data.subCategoryList.length);
  383. },
  384. _hasChildItem: function () {
  385. return (this.data.subItemList && this.data.subItemList.length);
  386. },
  387. _hasChild: function () {
  388. return this._hasChildCategory() || this._hasChildItem();
  389. },
  390. check: function () {
  391. }
  392. });