Custom.js 16 KB

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