uiSelForDay.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. var Global = require("Global")
  2. var UIHelper = require("UIHelper");
  3. var topTipMsg = require("topTipMsg")
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. mYearContent: {
  8. default: null,
  9. type: cc.ScrollView,
  10. },
  11. mMoonContent: {
  12. default: null,
  13. type: cc.ScrollView,
  14. },
  15. mDayContent: {
  16. default: null,
  17. type: cc.ScrollView,
  18. },
  19. _callFun:null,
  20. _monthDay:null,
  21. _startPos:null,
  22. },
  23. // LIFE-CYCLE CALLBACKS:
  24. onLoad () {
  25. var myDate = new Date();
  26. var off = [0, -1, 0];
  27. for (let index = 1; index < 3; index++) {
  28. var item = this.mYearContent.content.children[index];
  29. if (item) {
  30. item.getChildByName("txt").getComponent(cc.Label).string = "" + (myDate.getFullYear() + off[index]) ;
  31. }
  32. }
  33. myDate = null;
  34. var startPos = this.mYearContent.content.getChildByName("start");
  35. this.mYearContent.setContentPosition(cc.v2(startPos.position.x, -1*startPos.position.y));
  36. startPos = this.mMoonContent.content.getChildByName("start");
  37. this.mMoonContent.setContentPosition(cc.v2(startPos.position.x, -1*startPos.position.y));
  38. this._startPos = this.mDayContent.content.getChildByName("start");
  39. this.mDayContent.setContentPosition(cc.v2(this._startPos.position.x, -1*this._startPos.position.y));
  40. this._monthDay = [31,28,31,30,31,30,31,31,30,31,30,31];
  41. },
  42. start () {
  43. UIHelper.addButtonListener(this.node.getChildByName("rootNode").getChildByName("btClose"), this.node, "uiSelForDay", "onBtnClose");
  44. this.mYearContent.node.on("scroll-ended", this.onBtnScrollEnd, this);
  45. this.mMoonContent.node.on("scroll-ended", this.onBtnScrollEnd, this);
  46. this.mDayContent.node.on("scroll-ended", this.onBtnScrollEnd, this);
  47. },
  48. onBtnScrollEnd: function(event,opt) {
  49. Global.print("uiSelForDay onBtnScrollEnd ");
  50. var pos = event.detail.getContentPosition();
  51. var node = this.getScrollItem(event.detail.content, pos);
  52. //Global.print("uiSelForDay onBtnScrollEnd string = " + node.getChildByName("txt").getComponent(cc.Label).string);
  53. event.detail.setContentPosition(node.position);
  54. if(event.detail.node.name == "moon"){
  55. this.refreshDays();
  56. }
  57. else if(event.detail.node.name == "year")
  58. {
  59. var startPos = this.mMoonContent.getContentPosition();
  60. var moonNode = this.getScrollItem(this.mMoonContent.content, cc.v2(startPos.x, -1*startPos.y));
  61. var moon = moonNode.getChildByName("txt").getComponent(cc.Label).string;
  62. if(parseInt(moon) == 2)
  63. {
  64. this.refreshDays();
  65. }
  66. }
  67. },
  68. refreshDays()
  69. {
  70. var startPos = this.mYearContent.getContentPosition();
  71. var yearNode = this.getScrollItem(this.mYearContent.content, cc.v2(startPos.x, -1*startPos.y));
  72. startPos = this.mMoonContent.getContentPosition();
  73. var moonNode = this.getScrollItem(this.mMoonContent.content, cc.v2(startPos.x, -1*startPos.y));
  74. startPos = this.mDayContent.getContentPosition();
  75. var dayNode = this.getScrollItem(this.mDayContent.content, cc.v2(startPos.x, -1*startPos.y));
  76. var year = yearNode.getChildByName("txt").getComponent(cc.Label).string;
  77. var moon = moonNode.getChildByName("txt").getComponent(cc.Label).string;
  78. var day = dayNode.getChildByName("txt").getComponent(cc.Label).string;
  79. if((year%100 == 0 && year%400 == 0) ||(year%100 != 0 && year%4 == 0))
  80. {
  81. this._monthDay[1] = 29;
  82. }else{
  83. this._monthDay[1] = 28;
  84. }
  85. // this.mDayContent.setContentPosition(cc.v2(this._startPos.position.x, -1*this._startPos.position.y));
  86. var dayNode = this.setDays(this._monthDay[moon - 1],this.mDayContent.content,day);
  87. this.mDayContent.content.height = 260 + this._monthDay[moon - 1] * 80;
  88. this.mDayContent.setContentPosition(cc.v2(dayNode.position.x, -1*dayNode.position.y));
  89. },
  90. setDays(days,content,txt)
  91. {
  92. if(parseInt(txt) > days)
  93. {
  94. txt = ""+days;
  95. }
  96. var temp = null;
  97. for (let index = 0; index < content.childrenCount; index++) {
  98. const element = content.children[index];
  99. element.active = (index <= days || index >= 32 );
  100. if (element.getChildByName("txt")) {
  101. if(element.getChildByName("txt").getComponent(cc.Label).string == txt) {
  102. temp = element;
  103. }
  104. }
  105. }
  106. return temp;
  107. },
  108. getScrollItem(content, pos) {
  109. var node = null;
  110. var offsetMin = content.height;
  111. for (let index = 0; index < content.childrenCount; index++) {
  112. const element = content.children[index];
  113. if(element.active)
  114. {
  115. var offset = Math.abs(pos.y - element.y);
  116. if(offset < offsetMin) {
  117. offsetMin = offset;
  118. node = element;
  119. }
  120. }
  121. }
  122. return node;
  123. },
  124. getScrollItemByTxt(content, txt) {
  125. for (let index = 0; index < content.childrenCount; index++) {
  126. const element = content.children[index];
  127. if(element.active)
  128. {
  129. if (element.getChildByName("txt")) {
  130. if(element.getChildByName("txt").getComponent(cc.Label).string == txt) {
  131. return element;
  132. }
  133. }
  134. }
  135. }
  136. return null;
  137. },
  138. onBtnClose: function(event,opt) {
  139. Global.print("uiSelForDay onBtnClose ");
  140. //var yearIndex = this.node.getChildByName("rootNode").getChildByName("year").getComponent(cc.PageView).getCurrentPageIndex();
  141. //var moonIndex = this.node.getChildByName("rootNode").getChildByName("moon").getComponent(cc.PageView).getCurrentPageIndex();
  142. //var dayIndex = this.node.getChildByName("rootNode").getChildByName("day").getComponent(cc.PageView).getCurrentPageIndex();
  143. var startPos = this.mYearContent.getContentPosition();
  144. var yearNode = this.getScrollItem(this.mYearContent.content, cc.v2(startPos.x, -1*startPos.y));
  145. startPos = this.mMoonContent.getContentPosition();
  146. var moonNode = this.getScrollItem(this.mMoonContent.content, cc.v2(startPos.x, -1*startPos.y));
  147. startPos = this.mDayContent.getContentPosition();
  148. var dayNode = this.getScrollItem(this.mDayContent.content, cc.v2(startPos.x, -1*startPos.y));
  149. var year = yearNode.getChildByName("txt").getComponent(cc.Label).string;
  150. var moon = moonNode.getChildByName("txt").getComponent(cc.Label).string;
  151. var day = dayNode.getChildByName("txt").getComponent(cc.Label).string;
  152. Global.print("uiSelForDay onBtnClose year = " + year + " moon = " + moon + " day = " + day );
  153. if (this._callFun) {
  154. this._callFun(year,moon,day);
  155. this._callFun = null;
  156. }
  157. this.node.active = false;
  158. },
  159. // update (dt) {},
  160. show: function(arr, callFun) {
  161. this._callFun = callFun;
  162. this.node.active = true;
  163. var ani = this.node.getComponent(cc.Animation);
  164. if (ani) {
  165. ani.play();
  166. }
  167. if (arr.length >= 3) {
  168. var yearNode = this.getScrollItemByTxt(this.mYearContent.content, "" + Global.parseIntNotNull(arr[0]));
  169. var moonNode = this.getScrollItemByTxt(this.mMoonContent.content, "" + Global.parseIntNotNull(arr[1]));
  170. var dayNode = this.getScrollItemByTxt(this.mDayContent.content, "" + Global.parseIntNotNull(arr[2]));
  171. this.mYearContent.setContentPosition(cc.v2(yearNode.position.x, -1*yearNode.position.y));
  172. this.mMoonContent.setContentPosition(cc.v2(moonNode.position.x, -1*moonNode.position.y));
  173. this.refreshDays();
  174. //this.mDayContent.setContentPosition(cc.v2(dayNode.position.x, -1*dayNode.position.y));
  175. }
  176. },
  177. });