Custom.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. "uniqueFlag" : false,
  58. "defaultExpandLevel" : 1
  59. },
  60. initialize: function (container, options) {
  61. this.setOptions(options);
  62. this.path = "../x_component_Selector/$Selector/";
  63. this.cssPath = "../x_component_Selector/$Selector/" + this.options.style + "/css.wcss";
  64. this._loadCss(true);
  65. this.container = $(container);
  66. this.selectedItems = [];
  67. this.items = [];
  68. // this.categorys = [];
  69. this.subCategorys = []; //直接的分类
  70. this.subItems = []; //直接的选择项
  71. this.availableStatusTypes = ["identity","custom"];
  72. this._init();
  73. },
  74. _init : function(){
  75. this.selectType = "custom";
  76. this.className = "Custom"
  77. },
  78. loadSelectItems: function (addToNext) {
  79. this.loadingCount = "done";
  80. if (!this.options.category) {
  81. this.options.selectableItems.each(function (it) {
  82. var name = typeOf(it) === "string" ? it : it.name;
  83. var id = typeOf(it) === "string" ? it : it.id;
  84. var item = this._newItem({name: name, id: id}, this, this.itemAreaNode);
  85. this.items.push(item);
  86. this.subItems.push( item );
  87. }.bind(this))
  88. } else {
  89. if( this.isCheckStatusOrCount() ){
  90. this.loadCount();
  91. }
  92. this.options.selectableItems.each(function (item, index) {
  93. if (item.isItem) {
  94. var item = this._newItem(item, this, this.itemAreaNode);
  95. this.items.push(item);
  96. this.subItems.push( item );
  97. }else{
  98. // if ( (item.subItemList && item.subItemList.length > 0) || item.subCategoryList && item.subCategoryList.length > 0 ) {
  99. if( this.options.categorySelectable ){
  100. var category = this._newItemCategorySelectable(item, this, this.itemAreaNode);
  101. this.items.push(category);
  102. this.subItems.push( category );
  103. this.subCategorys.push( category );
  104. }else{
  105. var category = this._newItemCategory(item, this, this.itemAreaNode);
  106. this.subCategorys.push( category );
  107. }
  108. // item.subItemList.each(function (subItem, index) {
  109. // var item = this._newItem(subItem, this, category.children, 2, category);
  110. // this.items.push(item);
  111. // category.subItems.push(item);
  112. // }.bind(this));
  113. // }
  114. }
  115. }.bind(this));
  116. }
  117. if(this.afterLoadSelectItem)this.afterLoadSelectItem();
  118. },
  119. _scrollEvent: function (y) {
  120. return true;
  121. },
  122. _getChildrenItemIds: function (data) {
  123. return data.subItemList || [];
  124. },
  125. _newItemCategory: function (data, selector, container, level, parentCategory, delay) {
  126. return new MWF.xApplication.Template.Selector.Custom.ItemCategory(data, selector, container, level, parentCategory, delay)
  127. },
  128. _newItemCategorySelectable: function (data, selector, container, level, category, delay) {
  129. return new MWF.xApplication.Template.Selector.Custom.ItemCategorySelectable(data, selector, container, level, category, delay)
  130. },
  131. _listItemByKey: function (callback, failure, key) {
  132. if (key) {
  133. this.initSearchArea(true);
  134. this.searchInItems(key);
  135. } else {
  136. this.initSearchArea(false);
  137. }
  138. },
  139. _newItemSelected: function (data, selector, container, level, category, delay) {
  140. return new MWF.xApplication.Template.Selector.Custom.ItemSelected(data, selector, container, level, category, delay)
  141. },
  142. _listItemByPinyin: function (callback, failure, key) {
  143. if (key) {
  144. this.initSearchArea(true);
  145. this.searchInItems(key);
  146. } else {
  147. this.initSearchArea(false);
  148. }
  149. },
  150. nestData : function( data, isItem ){
  151. if( !this.nestedData )this.nestedData = {};
  152. var setNest = function (d, isItem) {
  153. if( isItem ){
  154. this.nestedData[ d["id"] || d["name"] ] = d;
  155. }else if( this.options.categorySelectable ){
  156. this.nestedData[ d["id"] || d["name"] ] = { id : d.id , name : d.name };
  157. if( d.subItemList )this.nestData( d.subItemList, true );
  158. if( d.subCategoryList )this.nestData( d.subCategoryList );
  159. }else{
  160. if( d.subItemList )this.nestData( d.subItemList, true );
  161. if( d.subCategoryList )this.nestData( d.subCategoryList );
  162. }
  163. }.bind(this);
  164. if( data ){
  165. for( var i=0; i<data.length; i++ ){
  166. var d = data[i];
  167. setNest(d, isItem );
  168. }
  169. }else{
  170. for( var i=0; i<this.options.selectableItems.length; i++ ){
  171. var d = this.options.selectableItems[i];
  172. setNest(d, d.isItem);
  173. }
  174. }
  175. },
  176. _getItem: function (callback, failure, id, async, data) {
  177. if( !this.nestedData )this.nestData();
  178. if (callback) callback.apply(id, [{ "data": this.nestedData[id] || {"id": id} }]);
  179. },
  180. _newItem: function (data, selector, container, level, category, delay) {
  181. return new MWF.xApplication.Template.Selector.Custom.Item(data, selector, container, level, category, delay);
  182. },
  183. createItemsSearchData: function (callback) {
  184. // if (!this.itemsSearchData) {
  185. this.itemsSearchData = [];
  186. this.getAllItemData();
  187. MWF.require("MWF.widget.PinYin", function () {
  188. var initIds = [];
  189. this.itemDataList.each(function (item) {
  190. if (initIds.indexOf(item.name) === -1) {
  191. var text = item.name.toLowerCase();
  192. var pinyin = text.toPY().toLowerCase();
  193. var firstPY = text.toPYFirst().toLowerCase();
  194. this.itemsSearchData.push({
  195. "text": text,
  196. "pinyin": pinyin,
  197. "firstPY": firstPY,
  198. "data": item
  199. });
  200. initIds.push(item.name);
  201. }
  202. }.bind(this));
  203. delete initIds;
  204. if (callback) callback();
  205. }.bind(this));
  206. // } else {
  207. // if (callback) callback();
  208. // }
  209. },
  210. getAllItemData: function(){
  211. this.itemDataList = [];
  212. this.options.selectableItems.each(function (item, index) {
  213. if (item.isItem) {
  214. this.itemDataList.push(item);
  215. }else{
  216. if( this.options.categorySelectable ){
  217. this.itemDataList.push(item);
  218. this.flatItemData(item);
  219. }else{
  220. this.flatItemData(item);
  221. }
  222. }
  223. }.bind(this));
  224. },
  225. flatItemData: function( c ){
  226. if( o2.typeOf( c.subItemList ) === "array" ){
  227. c.subItemList.each(function (item) {
  228. this.itemDataList.push(item)
  229. }.bind(this))
  230. }
  231. if( o2.typeOf( c.subCategoryList ) === "array" ){
  232. c.subCategoryList.each(function (item) {
  233. if( this.options.categorySelectable )this.itemDataList.push(item);
  234. this.flatItemData( item );
  235. }.bind(this));
  236. }
  237. },
  238. loadCount: function(){
  239. this.allCategoryDataObject = {};
  240. this.getValueString(this.options.values);
  241. this.calculateCount();
  242. },
  243. reloadCount: function(){
  244. var map = this.allCategoryDataObject;
  245. for(var key in map){
  246. map[key].subNestedItemCount = 0;
  247. map[key].selectedNestedItemCount = 0;
  248. }
  249. var values = this.selectedItems.map(function (item) { return item.data; }.bind(this));
  250. this.getValueString(values);
  251. this.calculateCount();
  252. },
  253. getValueString: function(values){
  254. this.valueStringList = values.map(function (item) {
  255. switch (o2.typeOf(item)) {
  256. case "string":
  257. return item;
  258. case "object":
  259. return this.options.uniqueFlag ? item[this.options.uniqueFlag] : (item.id || item.name);
  260. default:
  261. return "";
  262. }
  263. }.bind(this));
  264. },
  265. calculateCount: function( data, parentLevelName ){
  266. var map = this.allCategoryDataObject;
  267. if( data ){
  268. var levelName = parentLevelName ? (parentLevelName+"/"+data.name) : data.name;
  269. if( !map[levelName] )map[levelName] = data;
  270. if( data.subItemList && data.subItemList.length ){
  271. var selectedItems = data.subItemList.filter(function(item){
  272. var string = this.options.uniqueFlag ? item[this.options.uniqueFlag] : (item.id || item.name);
  273. return this.valueStringList.contains(string);
  274. }.bind(this));
  275. var nameList = [];
  276. levelName.split("/").each(function(name){
  277. nameList.push(name);
  278. var lName = nameList.join("/");
  279. var d = map[lName];
  280. if( d ){
  281. d.subNestedItemCount = (d.subNestedItemCount || 0) + data.subItemList.length;
  282. d.selectedNestedItemCount = (d.selectedNestedItemCount || 0) + selectedItems.length;
  283. }
  284. });
  285. }
  286. if( data.subCategoryList && data.subCategoryList.length ){
  287. data.subCategoryList.each(function (category) {
  288. this.calculateCount( category, levelName );
  289. }.bind(this))
  290. }
  291. }else{
  292. this.options.selectableItems.each(function (item, i) {
  293. if( !item.isItem )this.calculateCount( item, "" );
  294. }.bind(this))
  295. }
  296. },
  297. addSelectedCount: function( itemOrItemSelected, count, items ){
  298. var itemData = itemOrItemSelected.data;
  299. debugger;
  300. items.each(function(item){
  301. if(item.category && item.category._addSelectedCount){
  302. item.category._addSelectedCount( count, true );
  303. }
  304. }.bind(this));
  305. }
  306. });
  307. MWF.xApplication.Template.Selector.Custom.Item = new Class({
  308. Extends: o2.xApplication.Selector.Person.Item,
  309. _getShowName: function () {
  310. return this.data.name;
  311. },
  312. _setIcon: function () {
  313. var style = this.selector.options.style;
  314. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  315. },
  316. _getTtiteText: function () {
  317. return this.data.name;
  318. },
  319. loadSubItem: function () {
  320. return false;
  321. },
  322. checkSelectedSingle: function () {
  323. var selectedItem = this.selector.options.values.filter(function (item, index) {
  324. if( this.selector.options.uniqueFlag ){
  325. var flag = this.selector.options.uniqueFlag;
  326. if (typeOf(item) === "object") return ( this.data[flag] && this.data[flag] === item[flag] );
  327. if (typeOf(item) === "string") return ( this.data[flag] && this.data[flag] === item );
  328. }else{
  329. if (typeOf(item) === "object") return ( this.data.id && this.data.id === item.id) || (this.data.name && this.data.name === item.name);
  330. if (typeOf(item) === "string") return ( this.data.id && this.data.id === item) || (this.data.name && this.data.name === item);
  331. }
  332. return false;
  333. }.bind(this));
  334. if (selectedItem.length) {
  335. this.selectedSingle();
  336. }
  337. },
  338. checkSelected: function () {
  339. var selectedItem = this.selector.selectedItems.filter(function (item, index) {
  340. if( this.selector.options.uniqueFlag ){
  341. var flag = this.selector.options.uniqueFlag;
  342. return ( item.data[flag] && item.data[flag] === this.data[flag]);
  343. }else{
  344. return ( item.data.id && item.data.id === this.data.id) || (item.data.name && item.data.name === this.data.name);
  345. }
  346. }.bind(this));
  347. if (selectedItem.length) {
  348. //selectedItem[0].item = this;
  349. selectedItem[0].addItem(this);
  350. this.selectedItem = selectedItem[0];
  351. this.setSelected();
  352. }
  353. },
  354. destroy: function(){
  355. if( this.isSelected )this.unSelected();
  356. this.selector.items.erase( this );
  357. if( this.category ){
  358. if( this.category.subCategorys && this.category.subCategorys.length ){
  359. this.category.subCategorys.erase( this );
  360. }
  361. if( this.category.subItems && this.category.subItems.length ){
  362. this.category.subItems.erase( this );
  363. }
  364. }
  365. if(this.node)this.node.destroy();
  366. delete this;
  367. }
  368. });
  369. MWF.xApplication.Template.Selector.Custom.ItemSelected = new Class({
  370. Extends: o2.xApplication.Selector.Person.ItemSelected,
  371. _getShowName: function () {
  372. return this.data.name;
  373. },
  374. _setIcon: function () {
  375. var style = this.selector.options.style;
  376. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/processicon.png)");
  377. },
  378. _getTtiteText: function () {
  379. return this.data.name;
  380. },
  381. check: function () {
  382. if (this.selector.items.length) {
  383. var items = this.selector.items.filter(function (item, index) {
  384. if( this.selector.options.uniqueFlag ){
  385. var flag = this.selector.options.uniqueFlag;
  386. return ( item.data[flag] && item.data[flag] === this.data[flag]);
  387. }else{
  388. return ( item.data.id && item.data.id === this.data.id) || (item.data.name && item.data.name === this.data.name);
  389. }
  390. }.bind(this));
  391. this.items = items;
  392. if (items.length) {
  393. items.each(function (item) {
  394. item.selectedItem = this;
  395. item.setSelected();
  396. }.bind(this));
  397. }
  398. }
  399. debugger;
  400. if( !this.isFromValues ){
  401. if( this.selector.isCheckStatusOrCount() ){
  402. this.selector.addSelectedCount(this, 1, items||[]);
  403. }
  404. }
  405. if( this.afterCheck )this.afterCheck();
  406. }
  407. });
  408. MWF.xApplication.Template.Selector.Custom.ItemCategory = new Class({
  409. Extends: o2.xApplication.Selector.Person.ItemCategory,
  410. _getShowName: function () {
  411. return this.data.name;
  412. },
  413. createNode: function () {
  414. if( !this.levelName ){
  415. this.levelName = this.category ? (this.category.levelName + "/" + this.data.name) : this.data.name;
  416. }
  417. this.node = new Element("div", {
  418. "styles": this.selector.css.selectorItemCategory_department
  419. }).inject(this.container);
  420. },
  421. _setIcon: function () {
  422. var style = this.selector.options.style;
  423. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  424. },
  425. _getTtiteText: function () {
  426. return this.data.name;
  427. },
  428. _addSelectedCount : function( count, nested ){ //增加数字并向上回溯
  429. if( this.selector.loadingCount === "done" ){
  430. var c = ( this._getSelectedCount() || 0 ) + count;
  431. this.selectedCount = c;
  432. this._checkCountAndStatus( c );
  433. }else{
  434. this.selectedCount_wait = (this.selectedCount_wait || 0) + count;
  435. }
  436. if( nested && this.category && this.category._addSelectedCount ){
  437. this.category._addSelectedCount(count, nested);
  438. }
  439. },
  440. _getTotalCount : function(){
  441. // return this.subItems.length;
  442. if( !this.selector.allCategoryDataObject )return 0;
  443. var d = this.selector.allCategoryDataObject[this.levelName];
  444. return d ? d.subNestedItemCount : 0;
  445. },
  446. _getSelectedCount : function(){
  447. if( typeOf(this.selectedCount) === "number" )return this.selectedCount;
  448. if( !this.selector.allCategoryDataObject )return 0;
  449. var d = this.selector.allCategoryDataObject[this.levelName];
  450. var count = d ? d.selectedNestedItemCount : 0;
  451. this.selectedCount = count + (this.selectedCount_wait || 0);
  452. this.selectedCount_wait = 0;
  453. return this.selectedCount;
  454. },
  455. // _getSelectedCount : function(){
  456. // var list = this.subItems.filter( function (item) { return item.isSelected; });
  457. // return list.length;
  458. // },
  459. clickItem: function (callback) {
  460. if (this._hasChild() || this.selector.options.expandEmptyCategory ) {
  461. var firstLoaded = !this.loaded;
  462. if( !firstLoaded || this.selector.options.expandEmptyCategory ){
  463. if(this.isExpand){
  464. this.selector.fireEvent("collapse", [this] );
  465. }else{
  466. this.selector.fireEvent("expand", [this] );
  467. }
  468. }
  469. this.loadSub(function () {
  470. if (firstLoaded && this._hasChild() ) {
  471. if (!this.selector.isFlatCategory) {
  472. this.children.setStyles({"display": "block", "height": "auto"});
  473. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  474. this.isExpand = true;
  475. }
  476. // this.checkSelectAll();
  477. } else {
  478. var display = this.children.getStyle("display");
  479. if (display === "none") {
  480. // this.selector.fireEvent("expand", [this] );
  481. this.children.setStyles({"display": "block", "height": "auto"});
  482. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_expand);
  483. this.isExpand = true;
  484. } else {
  485. // this.selector.fireEvent("collapse", [this] );
  486. this.children.setStyles({"display": "none", "height": "0px"});
  487. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  488. this.isExpand = false;
  489. }
  490. }
  491. if (callback) callback();
  492. }.bind(this));
  493. }
  494. },
  495. destroy : function(){
  496. while( this.subItems.length )this.subItems[0].destroy();
  497. while( this.subCategorys.length )this.subCategorys[0].destroy();
  498. if( this.category && this.category.subCategorys && this.category.subCategorys.length ){
  499. this.category.subCategorys.erase( this );
  500. }
  501. if(this.node)this.node.destroy();
  502. delete this;
  503. },
  504. reloadSub : function(callback){
  505. while( this.subItems.length )this.subItems[0].destroy();
  506. this.subItems = [];
  507. while( this.subCategorys.length )this.subCategorys[0].destroy();
  508. this.subCategorys = [];
  509. this.loaded = false;
  510. this.loadSub( callback )
  511. },
  512. loadSub: function (callback) {
  513. if (!this.loaded) {
  514. if( this._hasChildItem() ){
  515. this.data.subItemList.each(function (subItem, index) {
  516. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  517. this.selector.items.push(item);
  518. if(this.subItems)this.subItems.push( item );
  519. }.bind(this));
  520. }
  521. if ( this._hasChildCategory() ) {
  522. this.data.subCategoryList.each(function (subCategory, index) {
  523. var category = this.selector._newItemCategory(subCategory, this.selector, this.children, this.level + 1, this);
  524. this.subCategorys.push( category );
  525. }.bind(this));
  526. }
  527. this.loaded = true;
  528. if (callback) callback();
  529. } else {
  530. if (callback) callback();
  531. }
  532. },
  533. _hasChildCategory: function () {
  534. return (this.data.subCategoryList && this.data.subCategoryList.length);
  535. },
  536. _hasChildItem: function () {
  537. return (this.data.subItemList && this.data.subItemList.length);
  538. },
  539. _hasChild: function () {
  540. return this._hasChildCategory() || this._hasChildItem();
  541. },
  542. check: function () {
  543. },
  544. afterLoad: function(){
  545. if ( this.level <= this.selector.options.defaultExpandLevel && (this._hasChild()) ){
  546. this.clickItem();
  547. }else{
  548. this.children.hide();
  549. this.actionNode.setStyles(this.selector.css.selectorItemCategoryActionNode_collapse);
  550. this.isExpand = false;
  551. }
  552. debugger;
  553. if( this.selector.isCheckStatusOrCount() ){
  554. if( this.selector.loadingCount === "done" ){
  555. this.checkCountAndStatus();
  556. }
  557. }
  558. },
  559. reloadCount: function () {
  560. if( this.selector.isCheckStatusOrCount() ){
  561. this.selector.reloadCount();
  562. if( this.selector.loadingCount === "done" ){
  563. this.checkCountAndStatus();
  564. }
  565. }
  566. }
  567. });
  568. MWF.xApplication.Template.Selector.Custom.ItemCategorySelectable = new Class({
  569. Extends: o2.xApplication.Selector.Unit.Item,
  570. _getShowName: function () {
  571. return this.data.name;
  572. },
  573. createNode: function () {
  574. // this.node = new Element("div", {
  575. // "styles": this.selector.css.selectorItemCategory_department //this.selector.css.selectorItemCategory_department
  576. // }).inject(this.container);
  577. },
  578. _setIcon: function () {
  579. var style = this.selector.options.style;
  580. this.iconNode.setStyle("background-image", "url(" + "../x_component_Selector/$Selector/" + style + "/icon/applicationicon.png)");
  581. },
  582. _getTtiteText: function () {
  583. return this.data.name;
  584. },
  585. _addSelectedCount : function( count, nested ){ //增加数字并向上回溯
  586. },
  587. _getTotalCount : function(){
  588. },
  589. _getSelectedCount : function(){
  590. },
  591. destroy : function(){
  592. if( this.isSelected )this.unSelected();
  593. this.selector.items.erase( this );
  594. while( this.subItems.length )this.subItems[0].destroy();
  595. while( this.subCategorys.length )this.subCategorys[0].destroy();
  596. if( this.category ){
  597. if( this.category.subCategorys && this.category.subCategorys.length ){
  598. this.category.subCategorys.erase( this );
  599. }
  600. if( this.category.subItems && this.category.subItems.length ){
  601. this.category.subItems.erase( this );
  602. }
  603. }
  604. if(this.node)this.node.destroy();
  605. delete this;
  606. },
  607. reloadSub : function(callback){
  608. while( this.subItems.length )this.subItems[0].destroy();
  609. this.subItems = [];
  610. while( this.subCategorys.length )this.subCategorys[0].destroy();
  611. this.subCategorys = [];
  612. this.loaded = false;
  613. this.loadSubItems( callback );
  614. },
  615. loadSubItems: function( callback ){
  616. if (!this.loaded){
  617. if (!this.children){
  618. this.children = new Element("div", {
  619. "styles": this.selector.css.selectorItemCategoryChildrenNode
  620. }).inject(this.node, "after");
  621. }
  622. this.children.setStyle("display", "block");
  623. // if (!this.selector.options.expand) this.children.setStyle("display", "none");
  624. if( this._hasChildItem() ){
  625. this.data.subItemList.each(function (subItem, index) {
  626. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  627. this.selector.items.push(item);
  628. if(this.subItems)this.subItems.push( item );
  629. }.bind(this));
  630. }
  631. if ( this._hasChildCategory() ) {
  632. this.data.subCategoryList.each(function (subCategory, index) {
  633. var category = this.selector._newItemCategorySelectable(subCategory, this.selector, this.children, this.level + 1, this);
  634. this.selector.items.push(category);
  635. this.subCategorys.push( category );
  636. }.bind(this));
  637. }
  638. this.loaded = true;
  639. if(callback)callback();
  640. }else{
  641. this.children.setStyle("display", "block");
  642. }
  643. },
  644. loadCategoryChildren : function( callback ){
  645. if (!this.categoryLoaded){
  646. if ( this._hasChildCategory() ) {
  647. this.data.subCategoryList.each(function (subCategory, index) {
  648. var category = this.selector._newItemCategorySelectable(subCategory, this.selector, this.children, this.level + 1, this);
  649. this.selector.items.push(category);
  650. this.subCategorys.push( category );
  651. }.bind(this));
  652. }
  653. this.categoryLoaded = true;
  654. if(callback)callback();
  655. }else{
  656. if(callback)callback();
  657. }
  658. },
  659. loadItemChildren : function( callback ){
  660. if (!this.itemLoaded){
  661. if( this._hasChildItem() ){
  662. this.data.subItemList.each(function (subItem, index) {
  663. var item = this.selector._newItem(subItem, this.selector, this.children, this.level + 1, this);
  664. this.selector.items.push(item);
  665. if(this.subItems)this.subItems.push( item );
  666. }.bind(this));
  667. }
  668. this.itemLoaded = true;
  669. if(callback)callback();
  670. }else{
  671. if(callback)callback();
  672. }
  673. },
  674. _hasChildCategory: function () {
  675. return (this.data.subCategoryList && this.data.subCategoryList.length);
  676. },
  677. _hasChildItem: function () {
  678. return (this.data.subItemList && this.data.subItemList.length);
  679. },
  680. _hasChild: function () {
  681. return this._hasChildCategory() || this._hasChildItem();
  682. },
  683. checkSelectedSingle: function () {
  684. var selectedItem = this.selector.options.values.filter(function (item, index) {
  685. if( this.selector.options.uniqueFlag ){
  686. var flag = this.selector.options.uniqueFlag;
  687. if (typeOf(item) === "object") return ( this.data[flag] && this.data[flag] === item[flag] );
  688. if (typeOf(item) === "string") return ( this.data[flag] && this.data[flag] === item );
  689. }else{
  690. if (typeOf(item) === "object") return ( this.data.id && this.data.id === item.id) || (this.data.name && this.data.name === item.name);
  691. if (typeOf(item) === "string") return ( this.data.id && this.data.id === item) || (this.data.name && this.data.name === item);
  692. }
  693. return false;
  694. }.bind(this));
  695. if (selectedItem.length) {
  696. this.selectedSingle();
  697. }
  698. },
  699. checkSelected: function () {
  700. var selectedItem = this.selector.selectedItems.filter(function (item, index) {
  701. if( this.selector.options.uniqueFlag ){
  702. var flag = this.selector.options.uniqueFlag;
  703. return ( item.data[flag] && item.data[flag] === this.data[flag]);
  704. }else{
  705. return ( item.data.id && item.data.id === this.data.id) || (item.data.name && item.data.name === this.data.name);
  706. }
  707. }.bind(this));
  708. if (selectedItem.length) {
  709. //selectedItem[0].item = this;
  710. selectedItem[0].addItem(this);
  711. this.selectedItem = selectedItem[0];
  712. this.setSelected();
  713. }
  714. },
  715. check: function () {
  716. this.checkSelected();
  717. },
  718. afterLoad : function () {
  719. },
  720. reloadCount: function () {
  721. }
  722. });