var Global = require("Global") var UIHelper = require("UIHelper"); var topTipMsg = require("topTipMsg") cc.Class({ extends: cc.Component, properties: { mYearContent: { default: null, type: cc.ScrollView, }, mMoonContent: { default: null, type: cc.ScrollView, }, mDayContent: { default: null, type: cc.ScrollView, }, _callFun:null, _monthDay:null, _startPos:null, }, // LIFE-CYCLE CALLBACKS: onLoad () { var myDate = new Date(); var off = [0, -1, 0]; for (let index = 1; index < 3; index++) { var item = this.mYearContent.content.children[index]; if (item) { item.getChildByName("txt").getComponent(cc.Label).string = "" + (myDate.getFullYear() + off[index]) ; } } myDate = null; var startPos = this.mYearContent.content.getChildByName("start"); this.mYearContent.setContentPosition(cc.v2(startPos.position.x, -1*startPos.position.y)); startPos = this.mMoonContent.content.getChildByName("start"); this.mMoonContent.setContentPosition(cc.v2(startPos.position.x, -1*startPos.position.y)); this._startPos = this.mDayContent.content.getChildByName("start"); this.mDayContent.setContentPosition(cc.v2(this._startPos.position.x, -1*this._startPos.position.y)); this._monthDay = [31,28,31,30,31,30,31,31,30,31,30,31]; }, start () { UIHelper.addButtonListener(this.node.getChildByName("rootNode").getChildByName("btClose"), this.node, "uiSelForDay", "onBtnClose"); this.mYearContent.node.on("scroll-ended", this.onBtnScrollEnd, this); this.mMoonContent.node.on("scroll-ended", this.onBtnScrollEnd, this); this.mDayContent.node.on("scroll-ended", this.onBtnScrollEnd, this); }, onBtnScrollEnd: function(event,opt) { Global.print("uiSelForDay onBtnScrollEnd "); var pos = event.detail.getContentPosition(); var node = this.getScrollItem(event.detail.content, pos); //Global.print("uiSelForDay onBtnScrollEnd string = " + node.getChildByName("txt").getComponent(cc.Label).string); event.detail.setContentPosition(node.position); if(event.detail.node.name == "moon"){ this.refreshDays(); } else if(event.detail.node.name == "year") { var startPos = this.mMoonContent.getContentPosition(); var moonNode = this.getScrollItem(this.mMoonContent.content, cc.v2(startPos.x, -1*startPos.y)); var moon = moonNode.getChildByName("txt").getComponent(cc.Label).string; if(parseInt(moon) == 2) { this.refreshDays(); } } }, refreshDays() { var startPos = this.mYearContent.getContentPosition(); var yearNode = this.getScrollItem(this.mYearContent.content, cc.v2(startPos.x, -1*startPos.y)); startPos = this.mMoonContent.getContentPosition(); var moonNode = this.getScrollItem(this.mMoonContent.content, cc.v2(startPos.x, -1*startPos.y)); startPos = this.mDayContent.getContentPosition(); var dayNode = this.getScrollItem(this.mDayContent.content, cc.v2(startPos.x, -1*startPos.y)); var year = yearNode.getChildByName("txt").getComponent(cc.Label).string; var moon = moonNode.getChildByName("txt").getComponent(cc.Label).string; var day = dayNode.getChildByName("txt").getComponent(cc.Label).string; if((year%100 == 0 && year%400 == 0) ||(year%100 != 0 && year%4 == 0)) { this._monthDay[1] = 29; }else{ this._monthDay[1] = 28; } // this.mDayContent.setContentPosition(cc.v2(this._startPos.position.x, -1*this._startPos.position.y)); var dayNode = this.setDays(this._monthDay[moon - 1],this.mDayContent.content,day); this.mDayContent.content.height = 260 + this._monthDay[moon - 1] * 80; this.mDayContent.setContentPosition(cc.v2(dayNode.position.x, -1*dayNode.position.y)); }, setDays(days,content,txt) { if(parseInt(txt) > days) { txt = ""+days; } var temp = null; for (let index = 0; index < content.childrenCount; index++) { const element = content.children[index]; element.active = (index <= days || index >= 32 ); if (element.getChildByName("txt")) { if(element.getChildByName("txt").getComponent(cc.Label).string == txt) { temp = element; } } } return temp; }, getScrollItem(content, pos) { var node = null; var offsetMin = content.height; for (let index = 0; index < content.childrenCount; index++) { const element = content.children[index]; if(element.active) { var offset = Math.abs(pos.y - element.y); if(offset < offsetMin) { offsetMin = offset; node = element; } } } return node; }, getScrollItemByTxt(content, txt) { for (let index = 0; index < content.childrenCount; index++) { const element = content.children[index]; if(element.active) { if (element.getChildByName("txt")) { if(element.getChildByName("txt").getComponent(cc.Label).string == txt) { return element; } } } } return null; }, onBtnClose: function(event,opt) { Global.print("uiSelForDay onBtnClose "); //var yearIndex = this.node.getChildByName("rootNode").getChildByName("year").getComponent(cc.PageView).getCurrentPageIndex(); //var moonIndex = this.node.getChildByName("rootNode").getChildByName("moon").getComponent(cc.PageView).getCurrentPageIndex(); //var dayIndex = this.node.getChildByName("rootNode").getChildByName("day").getComponent(cc.PageView).getCurrentPageIndex(); var startPos = this.mYearContent.getContentPosition(); var yearNode = this.getScrollItem(this.mYearContent.content, cc.v2(startPos.x, -1*startPos.y)); startPos = this.mMoonContent.getContentPosition(); var moonNode = this.getScrollItem(this.mMoonContent.content, cc.v2(startPos.x, -1*startPos.y)); startPos = this.mDayContent.getContentPosition(); var dayNode = this.getScrollItem(this.mDayContent.content, cc.v2(startPos.x, -1*startPos.y)); var year = yearNode.getChildByName("txt").getComponent(cc.Label).string; var moon = moonNode.getChildByName("txt").getComponent(cc.Label).string; var day = dayNode.getChildByName("txt").getComponent(cc.Label).string; Global.print("uiSelForDay onBtnClose year = " + year + " moon = " + moon + " day = " + day ); if (this._callFun) { this._callFun(year,moon,day); this._callFun = null; } this.node.active = false; }, // update (dt) {}, show: function(arr, callFun) { this._callFun = callFun; this.node.active = true; var ani = this.node.getComponent(cc.Animation); if (ani) { ani.play(); } if (arr.length >= 3) { var yearNode = this.getScrollItemByTxt(this.mYearContent.content, "" + Global.parseIntNotNull(arr[0])); var moonNode = this.getScrollItemByTxt(this.mMoonContent.content, "" + Global.parseIntNotNull(arr[1])); var dayNode = this.getScrollItemByTxt(this.mDayContent.content, "" + Global.parseIntNotNull(arr[2])); this.mYearContent.setContentPosition(cc.v2(yearNode.position.x, -1*yearNode.position.y)); this.mMoonContent.setContentPosition(cc.v2(moonNode.position.x, -1*moonNode.position.y)); this.refreshDays(); //this.mDayContent.setContentPosition(cc.v2(dayNode.position.x, -1*dayNode.position.y)); } }, });