@dingdaoos/lucid-utils
Version:
Lucid utils
1,389 lines (1,297 loc) • 419 kB
JavaScript
var isDragging = false;
var dragEvent = (function (el, options) {
var dragMove = function dragMove(e) {
options.dragMove && options.dragMove(e);
};
var dragStop = function dragStop(e) {
document.removeEventListener('mousemove', dragMove);
document.removeEventListener('mouseup', dragStop);
document.onselectstart = null;
document.ondragstart = null;
isDragging = false;
options.dragStop && options.dragStop(e);
};
el && el.addEventListener('mousedown', function (e) {
if (isDragging) return;
document.onselectstart = function () {
return false;
};
document.ondragstart = function () {
return false;
};
document.addEventListener('mousemove', dragMove);
document.addEventListener('mouseup', dragStop);
isDragging = true;
options.dragStart && options.dragStart(e);
});
});
var globalClick = function globalClick(callback) {
document.addEventListener('click', callback);
};
var getRect = function getRect(el) {
return el.getBoundingClientRect();
};
var padZero = function padZero(str) {
return String(str).padStart(2, '0');
};
function _classCallCheck$5(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties$5(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass$5(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties$5(Constructor.prototype, protoProps); if (staticProps) _defineProperties$5(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var DraggerFoundation = /*#__PURE__*/function () {
function DraggerFoundation(options) {
_classCallCheck$5(this, DraggerFoundation);
this.elWrapper = options.elWrapper;
this.elHandle = options.elHandle;
this.dragHandle = options.dragHandle;
this.percent = 0;
this.dragInit();
}
_createClass$5(DraggerFoundation, [{
key: "dragInit",
value: function dragInit() {
var _this = this;
var dragConfig = {
dragMove: function dragMove(e) {
_this.dragMove(e);
},
dragStop: function dragStop(e) {
_this.dragStop(e);
}
};
dragEvent(this.elWrapper, dragConfig);
}
}, {
key: "dragMove",
value: function dragMove(e) {
var pleft = this.elWrapper.getBoundingClientRect().left;
var pwidth = this.elWrapper.getBoundingClientRect().width;
var percentData = e.clientX < pleft ? 0 : (e.clientX - pleft) / pwidth * 100;
this.percent = percentData > 100 ? 100 : percentData;
this.dragHandle(true, this.percent);
}
}, {
key: "dragStop",
value: function dragStop(e) {
this.dragHandle(false, this.percent);
}
}]);
return DraggerFoundation;
}();
var index$3 = /*#__PURE__*/Object.freeze({
__proto__: null,
DraggerFoundation: DraggerFoundation
});
function _classCallCheck$4(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties$4(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass$4(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties$4(Constructor.prototype, protoProps); if (staticProps) _defineProperties$4(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var NormalDragger = /*#__PURE__*/function () {
function NormalDragger(options) {
var _this = this;
_classCallCheck$4(this, NormalDragger);
this.rect = getRect(options.elWrapper);
this.elWrapper = options.elWrapper;
this.elHandle = options.elHandle;
this.value = options.value;
this.height = this.rect.height - 4;
this.startTop = 0;
this.adapter = options.adapter;
this.handleHeight = this.value / 100 * this.height;
this.setHeight();
this.dragInit();
globalClick(function (e) {
var _this$elWrapper;
if ((_this$elWrapper = _this.elWrapper) !== null && _this$elWrapper !== void 0 && _this$elWrapper.parentNode && !e.path.includes(_this.elWrapper.parentNode)) {
_this.adapter.toggleBar(false);
_this.elWrapper.classList.remove('high-warning');
}
});
} // change slide height, and emit value
_createClass$4(NormalDragger, [{
key: "setHeight",
value: function setHeight() {
if (this.elHandle) this.elHandle.style.height = "".concat(this.handleHeight, "px");
this.adapter.emitValue('input', this.value);
this.adapter.emitValue('change', this.value);
} // get slide percent height
}, {
key: "getValue",
value: function getValue() {
var value = this.handleHeight / this.height * 100;
return ~~value;
} // toggle hight-warning class
}, {
key: "handleHighWarning",
value: function handleHighWarning() {
var highWarning = this.adapter.getValue('highWarning');
if (highWarning <= 0) return;
if (this.value > highWarning) {
this.elWrapper && this.elWrapper.classList.add('high-warning');
} else {
this.elWrapper && this.elWrapper.classList.remove('high-warning');
}
} // init slider drag event
}, {
key: "dragInit",
value: function dragInit() {
var _this2 = this;
var dragConfig = {
dragMove: function dragMove(e) {
_this2.dragMove(e);
},
dragStop: function dragStop(e) {
_this2.dragMove(e);
}
};
dragEvent(this.elWrapper, dragConfig);
} // drag event handler
}, {
key: "dragMove",
value: function dragMove(e) {
var mouseY = e.clientY - this.rect.top;
var height = this.height - mouseY;
height = Math.min(height, this.height);
height = Math.max(height, 0);
this.handleHeight = height;
this.value = this.getValue();
this.setHeight();
this.handleHighWarning();
}
}]);
return NormalDragger;
}();
function _classCallCheck$3(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties$3(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass$3(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties$3(Constructor.prototype, protoProps); if (staticProps) _defineProperties$3(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var DamperDragger = /*#__PURE__*/function () {
function DamperDragger(options) {
var _this = this;
_classCallCheck$3(this, DamperDragger);
var rect = getRect(options.elWrapper);
this.elWrapper = options.elWrapper;
this.elHandle = options.elHandle;
this.elDots = options.elDots;
this.damperList = options.damperList;
var _value = this.damperList.find(function (item) {
return item === options.value;
});
this.value = _value || this.damperList[0];
this.index = this.damperList.findIndex(function (item) {
return item === _this.value;
});
this.height = rect.height - 24;
this.startTop = 0;
this.adapter = options.adapter;
this.dampersY = this.getDampersY();
this.fragment = (this.dampersY[1] - this.dampersY[0]) / 2;
this.bindEvent();
this.setTop();
this.dragInit();
globalClick(function (e) {
var _this$elWrapper;
if ((_this$elWrapper = _this.elWrapper) !== null && _this$elWrapper !== void 0 && _this$elWrapper.parentNode && !e.path.includes(_this.elWrapper.parentNode)) {
_this.adapter.toggleBar(false);
}
});
}
_createClass$3(DamperDragger, [{
key: "bindEvent",
value: function bindEvent() {
var _this2 = this;
this.elDots && this.elDots.addEventListener('click', function (e) {
if ((e === null || e === void 0 ? void 0 : e.target.tagName) === 'SPAN') {
var value = Number(e.target.getAttribute('data-damp'));
_this2.index = _this2.damperList.findIndex(function (item) {
return item === value;
});
_this2.setTop();
}
});
} // set damper top value, and emit value
}, {
key: "setTop",
value: function setTop() {
this.startTop = this.index / (this.damperList.length - 1) * this.height;
var _top = this.startTop;
_top = Math.max(_top, 0);
_top = Math.min(_top, this.height);
if (this.elHandle) this.elHandle.style.top = "".concat(_top, "px");
this.adapter.emitValue('input', this.damperList[this.index]);
this.adapter.emitValue('change', this.damperList[this.index]);
} // get all damper y
}, {
key: "getDampersY",
value: function getDampersY() {
var res = [];
if (this.elDots) Array.from(this.elDots.childNodes).filter(function (item) {
return item.nodeType === 1;
}).forEach(function (item) {
res.push(getRect(item).y);
});
return res;
} // get damper top
}, {
key: "getTop",
value: function getTop() {
return this.elHandle ? parseInt(this.elHandle.style.top) : 0;
} // init drag event
}, {
key: "dragInit",
value: function dragInit() {
var _this3 = this;
var dragConfig = {
dragMove: function dragMove(e) {
_this3.dragMove(e);
},
dragStart: function dragStart(e) {
_this3.dragStart();
},
dragStop: function dragStop(e) {
_this3.dragStop();
}
};
dragEvent(this.elHandle, dragConfig);
} // drag start handler
}, {
key: "dragStart",
value: function dragStart() {
this.startTop = this.getTop();
} // drag event handler
}, {
key: "dragMove",
value: function dragMove(e) {
var y = e.clientY;
var damperY = this.dampersY[this.index];
var len = this.damperList.length;
if (y >= damperY + this.fragment) {
this.index = this.index >= len - 2 ? len - 1 : this.index + 1;
} else if (y <= damperY - this.fragment) {
this.index = this.index <= 1 ? 0 : this.index - 1;
}
this.setTop();
} // drag stop handler
}, {
key: "dragStop",
value: function dragStop() {
this.setTop();
}
}]);
return DamperDragger;
}();
var index$2 = /*#__PURE__*/Object.freeze({
__proto__: null,
NormalDragger: NormalDragger,
DamperDragger: DamperDragger
});
function _classCallCheck$2(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties$2(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass$2(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties$2(Constructor.prototype, protoProps); if (staticProps) _defineProperties$2(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var TimeInfo = /*#__PURE__*/function () {
function TimeInfo() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
hourType: 24,
hourDuration: '0-23',
minuteDuration: '0-59',
secondDuration: '0-59',
interval: 1
};
_classCallCheck$2(this, TimeInfo);
this.setOptions(options);
this.getHour();
this.getMinute();
this.getSecond();
this.getTimeList(options.interval);
} // set options
_createClass$2(TimeInfo, [{
key: "setOptions",
value: function setOptions(options) {
this.options = options;
} // get hour function
}, {
key: "getHour",
value: function getHour() {
var hourType = this.options.hourType;
var hourDuration = this.options.hourDuration ? this.options.hourDuration : hourType === 24 ? '0-23' : '0-11';
var duration = hourDuration.split('-');
var start = parseInt(duration[0]);
var end = parseInt(duration[1]);
var hours = [];
for (var i = start; i <= end; i += 1) {
hours.push(padZero(i));
}
if (hourType === 12) {
this.noon = ['AM', 'PM'];
}
this.hours = hours;
} // get minute funcgtion
}, {
key: "getMinute",
value: function getMinute() {
var minuteDuration = this.options.minuteDuration;
var duration = minuteDuration.split('-');
var start = parseInt(duration[0]);
var end = parseInt(duration[1]);
var minutes = [];
if (end >= 60) {
for (var i = start; i < 60; i += 1) {
minutes.push(padZero(i));
}
} else {
for (var _i = start; _i <= end; _i += 1) {
minutes.push(padZero(_i));
}
}
this.minutes = minutes;
} // get second function
}, {
key: "getSecond",
value: function getSecond() {
var secondDuration = this.options.secondDuration;
var duration = secondDuration.split('-');
var start = parseInt(duration[0]);
var end = parseInt(duration[1]);
var seconds = [];
if (end >= 60) {
for (var i = start; i < 60; i += 1) {
seconds.push(padZero(i));
}
} else {
for (var _i2 = start; _i2 <= end; _i2 += 1) {
seconds.push(padZero(_i2));
}
}
this.seconds = seconds;
}
}, {
key: "getTimeList",
value: function getTimeList(interval) {
var size = Math.floor(24 * 60 / interval);
var time = new Array(size).fill('').map(function (item, index) {
var startVal = index * interval;
var startHour = Math.floor(startVal / 60);
var startMinute = startVal % 60;
var startTime = (startHour < 10 ? '0' + startHour : startHour) + ':' + (startMinute < 10 ? '0' + startMinute : startMinute);
return startTime;
});
this.timeList = time;
} // get nowinfo function
}, {
key: "getNowInfo",
value: function getNowInfo() {
var nowInfo = new Date();
var dateInfo = padZero(nowInfo.getFullYear()) + '-' + padZero(nowInfo.getMonth() + 1) + '-' + padZero(nowInfo.getDate());
var timeInfo = padZero(nowInfo.getHours()) + ':' + padZero(nowInfo.getMinutes()) + ':' + padZero(nowInfo.getSeconds());
return {
dateInfo: dateInfo,
timeInfo: timeInfo
};
}
}]);
return TimeInfo;
}();
var time = /*#__PURE__*/Object.freeze({
__proto__: null,
TimeInfo: TimeInfo
});
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
var lunar = {exports: {}};
(function (module) {
(function(root,factory){
if(module.exports){
module.exports = factory();
}else {
var o = factory();
for(var i in o){
root[i] = o[i];
}
}
})(commonjsGlobal,function(){
var ExactDate = (function(){
return {
_:function(date,y,m,d){
if(y<100){
date.setFullYear(y);
date.setMonth(m-1);
date.setDate(d);
}
date.setMilliseconds(0);
return date;
},
fromYmd:function(y,m,d){
return this.fromYmdHms(y,m,d,0,0,0);
},
fromYmdHms:function(y,m,d,hour,minute,second){
return this._(new Date(y+'/'+m+'/'+d+' '+hour+':'+minute+':'+second),y,m,d);
},
getDaysBetweenYmd:function(ay, am, ad, by, bm, bd){
var n;
var days;
var i;
if (ay == by) {
n = SolarUtil.getDaysInYear(by, bm, bd) - SolarUtil.getDaysInYear(ay, am, ad);
} else if (ay > by) {
days = SolarUtil.getDaysOfYear(by) - SolarUtil.getDaysInYear(by, bm, bd);
for (i = by + 1; i < ay; i++) {
days += SolarUtil.getDaysOfYear(i);
}
days += SolarUtil.getDaysInYear(ay, am, ad);
n = -days;
} else {
days = SolarUtil.getDaysOfYear(ay) - SolarUtil.getDaysInYear(ay, am, ad);
for (i = ay + 1; i < by; i++) {
days += SolarUtil.getDaysOfYear(i);
}
days += SolarUtil.getDaysInYear(by, bm, bd);
n = days;
}
return n;
},
getDaysBetween:function(date0, date1){
return this.getDaysBetweenYmd(date0.getFullYear(), date0.getMonth() + 1, date0.getDate(), date1.getFullYear(), date1.getMonth() + 1, date1.getDate());
}
};
})();
var Solar = (function(){
var _fromDate = function(date){
return _fromYmdHms(date.getFullYear(),date.getMonth()+1,date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds());
};
var _fromJulianDay = function(julianDay){
var d = Math.floor(julianDay + 0.5);
var f = julianDay + 0.5 - d;
var c;
if (d >= 2299161) {
c = Math.floor((d - 1867216.25) / 36524.25);
d += 1 + c - Math.floor(c / 4);
}
d += 1524;
var year = Math.floor((d - 122.1) / 365.25);
d -= Math.floor(365.25 * year);
var month = Math.floor(d / 30.601);
d -= Math.floor(30.601 * month);
var day = d;
if (month > 13) {
month -= 13;
year -= 4715;
} else {
month -= 1;
year -= 4716;
}
f *= 24;
var hour = Math.floor(f);
f -= hour;
f *= 60;
var minute = Math.floor(f);
f -= minute;
f *= 60;
var second = Math.round(f);
if(second>59){
second-=60;
minute++;
}
if(minute>59){
minute-=60;
hour++;
}
return _fromYmdHms(year,month,day,hour,minute,second);
};
var _fromYmdHms = function(y,m,d,hour,minute,second){
if(y===1582&&m===10){
if(d>=15){
d -= 10;
}
}
return {
_p:{
year:y,
month:m,
day:d,
hour:hour,
minute:minute,
second:second,
calendar:ExactDate.fromYmdHms(y,m,d,hour,minute,second)
},
getYear:function(){
return this._p.year;
},
getMonth:function(){
return this._p.month;
},
getDay:function(){
return this._p.day;
},
getHour:function(){
return this._p.hour;
},
getMinute:function(){
return this._p.minute;
},
getSecond:function(){
return this._p.second;
},
getWeek:function(){
return this._p.calendar.getDay();
},
getWeekInChinese:function(){
return SolarUtil.WEEK[this.getWeek()];
},
/**
* 获取当天的阳历周
* @param start 星期几作为一周的开始,1234560分别代表星期一至星期天
*/
getSolarWeek:function(start){
return SolarWeek.fromDate(this._p.calendar,start);
},
isLeapYear:function(){
return SolarUtil.isLeapYear(this._p.year);
},
getFestivals:function(){
var l = [];
var f = SolarUtil.FESTIVAL[this._p.month+'-'+this._p.day];
if(f){
l.push(f);
}
var weeks = Math.ceil(this._p.day/7);
var week = this.getWeek();
f = SolarUtil.WEEK_FESTIVAL[this._p.month+'-'+weeks+'-'+week];
if(f){
l.push(f);
}
if (this._p.day + 7 > SolarUtil.getDaysOfMonth(this._p.year, this._p.month)) {
f = SolarUtil.WEEK_FESTIVAL[this._p.month + '-0-' + week];
if (f) {
l.push(f);
}
}
return l;
},
getOtherFestivals:function(){
var l=[];
var fs=SolarUtil.OTHER_FESTIVAL[this._p.month+'-'+this._p.day];
if(fs){
l=l.concat(fs);
}
return l;
},
getXingzuo:function(){
return this.getXingZuo();
},
getXingZuo:function(){
var index = 11;
var y = this._p.month*100+this._p.day;
if (y >= 321 && y <= 419) {
index = 0;
} else if (y >= 420 && y <= 520) {
index = 1;
} else if (y >= 521 && y <= 621) {
index = 2;
} else if (y >= 622 && y <= 722) {
index = 3;
} else if (y >= 723 && y <= 822) {
index = 4;
} else if (y >= 823 && y <= 922) {
index = 5;
} else if (y >= 923 && y <= 1023) {
index = 6;
} else if (y >= 1024 && y <= 1122) {
index = 7;
} else if (y >= 1123 && y <= 1221) {
index = 8;
} else if (y >= 1222 || y <= 119) {
index = 9;
} else if (y <= 218) {
index = 10;
}
return SolarUtil.XINGZUO[index];
},
toYmd:function(){
var d = this._p.day;
if(this._p.year===1582&&this._p.month===10){
if(d>=5){
d += 10;
}
}
var y = this._p.year + '';
while (y.length < 4) {
y = '0' + y;
}
return [y,(this._p.month<10?'0':'')+this._p.month,(d<10?'0':'')+d].join('-');
},
toYmdHms:function(){
return this.toYmd()+' '+[(this._p.hour<10?'0':'')+this._p.hour,(this._p.minute<10?'0':'')+this._p.minute,(this._p.second<10?'0':'')+this._p.second].join(':');
},
toString:function(){
return this.toYmd();
},
toFullString:function(){
var s = this.toYmdHms();
if(this.isLeapYear()){
s += ' 闰年';
}
s += ' 星期'+this.getWeekInChinese();
var festivals = this.getFestivals();
for(var i=0,j=festivals.length;i<j;i++){
s += ' ('+festivals[i]+')';
}
s += ' '+this.getXingZuo()+'座';
return s;
},
next:function(days, onlyWorkday){
var date = ExactDate.fromYmdHms(this._p.year,this._p.month,this._p.day,this._p.hour,this._p.minute,this._p.second);
if(0!=days){
if (!onlyWorkday){
date.setDate(date.getDate() + days);
}else {
var rest = Math.abs(days);
var add = days < 1 ? -1 : 1;
while(rest > 0){
date.setDate(date.getDate() + add);
var work = true;
var holiday = HolidayUtil.getHoliday(date.getFullYear(), date.getMonth() + 1, date.getDate());
if(!holiday){
var week = date.getDay();
if(0 === week || 6 === week){
work = false;
}
}else {
work = holiday.isWork();
}
if(work){
rest--;
}
}
}
}
return _fromDate(date);
},
getLunar:function(){
return Lunar.fromDate(this._p.calendar);
},
getJulianDay:function(){
var y = this._p.year;
var m = this._p.month;
var d = this._p.day + ((this._p.second / 60 + this._p.minute) / 60 + this._p.hour) / 24;
var n = 0;
var g = false;
if (y * 372 + m * 31 + Math.floor(d) >= 588829) {
g = true;
}
if (m <= 2) {
m += 12;
y--;
}
if (g) {
n = Math.floor(y / 100);
n = 2 - n + Math.floor(n / 4);
}
return Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + d + n - 1524.5;
},
getCalendar:function(){
return this._p.calendar;
}
};
};
var _fromBaZi=function(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,sect,baseYear){
sect = (1==sect)?1:2;
baseYear = (undefined==baseYear)?1900:baseYear;
var l = [];
var today = _fromDate(new Date());
var lunar = today.getLunar();
var offsetYear = LunarUtil.getJiaZiIndex(lunar.getYearInGanZhiExact())-LunarUtil.getJiaZiIndex(yearGanZhi);
if(offsetYear<0){
offsetYear = offsetYear+60;
}
var startYear = lunar.getYear() - offsetYear;
var hour = 0;
var timeZhi = timeGanZhi.substr(1);
for(var i=0,j=LunarUtil.ZHI.length;i<j;i++){
if(LunarUtil.ZHI[i]===timeZhi){
hour = (i-1)*2;
}
}
while(startYear>=baseYear){
var year = startYear-1;
var counter = 0;
var month = 12;
var day;
var solar;
var found = false;
while (counter < 15) {
if(year>=baseYear){
day = 1;
solar = _fromYmdHms(year, month, day, hour, 0, 0);
lunar = solar.getLunar();
if(lunar.getYearInGanZhiExact()===yearGanZhi && lunar.getMonthInGanZhiExact()===monthGanZhi){
found = true;
break;
}
}
month++;
if(month > 12){
month = 1;
year++;
}
counter++;
}
if(found){
counter = 0;
month--;
if(month<1){
month = 12;
year--;
}
day = 1;
solar = _fromYmdHms(year, month, day, hour, 0, 0);
while (counter < 61) {
lunar = solar.getLunar();
var dgz = (2==sect)?lunar.getDayInGanZhiExact2():lunar.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact()===yearGanZhi && lunar.getMonthInGanZhiExact()===monthGanZhi && dgz===dayGanZhi && lunar.getTimeInGanZhi()===timeGanZhi) {
l.push(solar);
break;
}
solar = solar.next(1);
counter++;
}
}
startYear -= 60;
}
return l;
};
return {
J2000:2451545,
fromYmd:function(y,m,d){return _fromYmdHms(y,m,d,0,0,0);},
fromYmdHms:function(y,m,d,hour,minute,second){return _fromYmdHms(y,m,d,hour,minute,second);},
fromDate:function(date){return _fromDate(date);},
fromJulianDay:function(julianDay){return _fromJulianDay(julianDay);},
fromBaZi:function(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,sect,baseYear){return _fromBaZi(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,sect,baseYear);}
};
})();
var Lunar = (function(){
var _computeJieQi = function(o,ly) {
o['jieQiList'] = [];
o['jieQi'] = {};
var julianDays = ly.getJieQiJulianDays();
for (var i = 0, j = Lunar.JIE_QI_IN_USE.length; i < j; i++) {
var key = Lunar.JIE_QI_IN_USE[i];
o['jieQiList'].push(key);
o['jieQi'][key] = Solar.fromJulianDay(julianDays[i]);
}
};
var _computeYear = function(o,solar,year){
//以正月初一开始
var offset = year - 4;
var yearGanIndex = offset % 10;
var yearZhiIndex = offset % 12;
if (yearGanIndex < 0) {
yearGanIndex += 10;
}
if (yearZhiIndex < 0) {
yearZhiIndex += 12;
}
//以立春作为新一年的开始的干支纪年
var g = yearGanIndex;
var z = yearZhiIndex;
//精确的干支纪年,以立春交接时刻为准
var gExact = yearGanIndex;
var zExact = yearZhiIndex;
var solarYear = solar.getYear();
var solarYmd = solar.toYmd();
var solarYmdHms = solar.toYmdHms();
//获取立春的阳历时刻
var liChun = o['jieQi']['立春'];
if (liChun.getYear() != solarYear) {
liChun = o['jieQi']['LI_CHUN'];
}
var liChunYmd = liChun.toYmd();
var liChunYmdHms = liChun.toYmdHms();
//阳历和阴历年份相同代表正月初一及以后
if(year===solarYear){
//立春日期判断
if(solarYmd<liChunYmd) {
g--;
z--;
}
//立春交接时刻判断
if(solarYmdHms<liChunYmdHms) {
gExact--;
zExact--;
}
}else if (year < solarYear){
if(solarYmd>=liChunYmd) {
g++;
z++;
}
if(solarYmdHms>=liChunYmdHms) {
gExact++;
zExact++;
}
}
o['yearGanIndex'] = yearGanIndex;
o['yearZhiIndex'] = yearZhiIndex;
o['yearGanIndexByLiChun'] = (g<0?g+10:g)%10;
o['yearZhiIndexByLiChun'] = (z<0?z+12:z)%12;
o['yearGanIndexExact'] = (gExact<0?gExact+10:gExact)%10;
o['yearZhiIndexExact'] = (zExact<0?zExact+12:zExact)%12;
};
var _computeMonth = function(o,solar){
var start = null;
var i;
var end;
var size = Lunar.JIE_QI_IN_USE.length;
//序号:大雪以前-3,大雪到小寒之间-2,小寒到立春之间-1,立春之后0
var index = -3;
for(i=0;i<size;i+=2){
end = o.jieQi[Lunar.JIE_QI_IN_USE[i]];
var ymd = solar.toYmd();
var symd = null==start?ymd:start.toYmd();
if(ymd>=symd&&ymd<end.toYmd()){
break;
}
start = end;
index++;
}
var offset = (((o.yearGanIndexByLiChun+(index<0?1:0)) % 5 + 1) * 2) % 10;
o['monthGanIndex'] = ((index<0?index+10:index) + offset) % 10;
o['monthZhiIndex'] = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
start = null;
index = -3;
for(i=0;i<size;i+=2){
end = o.jieQi[Lunar.JIE_QI_IN_USE[i]];
var time = solar.toYmdHms();
var stime = null==start?time:start.toYmdHms();
if(time>=stime&&time<end.toYmdHms()){
break;
}
start = end;
index++;
}
offset = (((o.yearGanIndexExact+(index<0?1:0)) % 5 + 1) * 2) % 10;
o['monthGanIndexExact'] = ((index<0?index+10:index) + offset) % 10;
o['monthZhiIndexExact'] = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
};
var _computeDay = function(o,solar,hour,minute){
var noon = Solar.fromYmdHms(solar.getYear(), solar.getMonth(), solar.getDay(), 12, 0, 0);
var offset = Math.floor(noon.getJulianDay()) - 11;
var dayGanIndex = offset % 10;
var dayZhiIndex = offset % 12;
o['dayGanIndex'] = dayGanIndex;
o['dayZhiIndex'] = dayZhiIndex;
var dayGanExact = dayGanIndex;
var dayZhiExact = dayZhiIndex;
o['dayGanIndexExact2'] = dayGanExact;
o['dayZhiIndexExact2'] = dayZhiExact;
var hm = (hour<10?'0':'')+hour+':'+(minute<10?'0':'')+minute;
if(hm>='23:00'&&hm<='23:59'){
dayGanExact++;
if(dayGanExact>=10){
dayGanExact -= 10;
}
dayZhiExact++;
if(dayZhiExact>=12){
dayZhiExact -= 12;
}
}
o['dayGanIndexExact'] = dayGanExact;
o['dayZhiIndexExact'] = dayZhiExact;
};
var _computeTime = function(o,hour,minute){
var timeZhiIndex = LunarUtil.getTimeZhiIndex((hour<10?'0':'')+hour+':'+(minute<10?'0':'')+minute);
o['timeZhiIndex'] = timeZhiIndex;
o['timeGanIndex'] = (o['dayGanIndexExact']%5*2+timeZhiIndex)%10;
};
var _computeWeek = function(o,solar){
o['weekIndex'] = solar.getWeek();
};
var _compute = function(year,hour,minute,second,solar,ly){
var o = {};
_computeJieQi(o, ly);
_computeYear(o, solar, year);
_computeMonth(o, solar);
_computeDay(o, solar, hour, minute);
_computeTime(o, hour, minute);
_computeWeek(o, solar);
return o;
};
var _fromDate = function(date){
var currentYear = date.getFullYear();
var currentMonth = date.getMonth() + 1;
var currentDay = date.getDate();
var lunarYear = 0;
var lunarMonth = 0;
var lunarDay = 0;
var ly = LunarYear.fromYear(currentYear);
var lms = ly.getMonths();
for (var i = 0, j = lms.length; i < j; i++) {
var m = lms[i];
// 初一
var firstDay = Solar.fromJulianDay(m.getFirstJulianDay());
var days = ExactDate.getDaysBetweenYmd(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), currentYear, currentMonth, currentDay);
if (days < m.getDayCount()) {
lunarYear = m.getYear();
lunarMonth = m.getMonth();
lunarDay = days + 1;
break;
}
}
return _new(lunarYear, lunarMonth, lunarDay, date.getHours(), date.getMinutes(), date.getSeconds(), Solar.fromDate(date), ly);
};
var _fromYmdHms = function(lunarYear,lunarMonth,lunarDay,hour,minute,second) {
var y = LunarYear.fromYear(lunarYear);
var m = y.getMonth(lunarMonth);
if (null == m) {
throw 'wrong lunar year '+lunarYear+' month '+lunarMonth;
}
if (lunarDay < 1) {
throw 'lunar day must bigger than 0';
}
var days = m.getDayCount();
if (lunarDay > days) {
throw 'only '+days+' days in lunar year '+lunarYear+' month '+lunarMonth;
}
var noon = Solar.fromJulianDay(m.getFirstJulianDay() + lunarDay - 1);
var solar = Solar.fromYmdHms(noon.getYear(), noon.getMonth(), noon.getDay(), hour, minute, second);
return _new(lunarYear, lunarMonth, lunarDay, hour, minute, second, solar, y);
};
var _new = function(year,month,day,hour,minute,second,solar,ly){
var gz = _compute(year,hour,minute,second,solar,ly);
return {
_p:{
year:year,
month:month,
day:day,
hour:hour,
minute:minute,
second:second,
timeGanIndex:gz.timeGanIndex,
timeZhiIndex:gz.timeZhiIndex,
dayGanIndex:gz.dayGanIndex,
dayZhiIndex:gz.dayZhiIndex,
dayGanIndexExact:gz.dayGanIndexExact,
dayZhiIndexExact:gz.dayZhiIndexExact,
dayGanIndexExact2:gz.dayGanIndexExact2,
dayZhiIndexExact2:gz.dayZhiIndexExact2,
monthGanIndex:gz.monthGanIndex,
monthZhiIndex:gz.monthZhiIndex,
monthGanIndexExact:gz.monthGanIndexExact,
monthZhiIndexExact:gz.monthZhiIndexExact,
yearGanIndex:gz.yearGanIndex,
yearZhiIndex:gz.yearZhiIndex,
yearGanIndexByLiChun:gz.yearGanIndexByLiChun,
yearZhiIndexByLiChun:gz.yearZhiIndexByLiChun,
yearGanIndexExact:gz.yearGanIndexExact,
yearZhiIndexExact:gz.yearZhiIndexExact,
weekIndex:gz.weekIndex,
jieQi:gz.jieQi,
jieQiList:gz.jieQiList,
solar:solar,
eightChar:null
},
getYear:function(){return this._p.year;},
getMonth:function(){return this._p.month;},
getDay:function(){return this._p.day;},
getHour:function(){return this._p.hour;},
getMinute:function(){return this._p.minute;},
getSecond:function(){return this._p.second;},
getTimeGanIndex:function(){return this._p.timeGanIndex;},
getTimeZhiIndex:function(){return this._p.timeZhiIndex;},
getDayGanIndex:function(){return this._p.dayGanIndex;},
getDayGanIndexExact:function(){return this._p.dayGanIndexExact;},
getDayGanIndexExact2:function(){return this._p.dayGanIndexExact2;},
getDayZhiIndex:function(){return this._p.dayZhiIndex;},
getDayZhiIndexExact:function(){return this._p.dayZhiIndexExact;},
getDayZhiIndexExact2:function(){return this._p.dayZhiIndexExact2;},
getMonthGanIndex:function(){return this._p.monthGanIndex;},
getMonthGanIndexExact:function(){return this._p.monthGanIndexExact;},
getMonthZhiIndex:function(){return this._p.monthZhiIndex;},
getMonthZhiIndexExact:function(){return this._p.monthZhiIndexExact;},
getYearGanIndex:function(){return this._p.yearGanIndex;},
getYearGanIndexByLiChun:function(){return this._p.yearGanIndexByLiChun;},
getYearGanIndexExact:function(){return this._p.yearGanIndexExact;},
getYearZhiIndex:function(){return this._p.yearZhiIndex;},
getYearZhiIndexByLiChun:function(){return this._p.yearZhiIndexByLiChun;},
getYearZhiIndexExact:function(){return this._p.yearZhiIndexExact;},
getGan:function(){return this.getYearGan();},
getZhi:function(){return this.getYearZhi();},
getYearGan:function(){return LunarUtil.GAN[this._p.yearGanIndex+1];},
getYearGanByLiChun:function(){return LunarUtil.GAN[this._p.yearGanIndexByLiChun+1];},
getYearGanExact:function(){return LunarUtil.GAN[this._p.yearGanIndexExact+1];},
getYearZhi:function(){return LunarUtil.ZHI[this._p.yearZhiIndex+1];},
getYearZhiByLiChun:function(){return LunarUtil.ZHI[this._p.yearZhiIndexByLiChun+1];},
getYearZhiExact:function(){return LunarUtil.ZHI[this._p.yearZhiIndexExact+1];},
getYearInGanZhi:function(){return this.getYearGan()+this.getYearZhi();},
getYearInGanZhiByLiChun:function(){return this.getYearGanByLiChun()+this.getYearZhiByLiChun();},
getYearInGanZhiExact:function(){return this.getYearGanExact()+this.getYearZhiExact();},
getMonthGan:function(){return LunarUtil.GAN[this._p.monthGanIndex+1];},
getMonthGanExact:function(){return LunarUtil.GAN[this._p.monthGanIndexExact+1];},
getMonthZhi:function(){return LunarUtil.ZHI[this._p.monthZhiIndex+1];},
getMonthZhiExact:function(){return LunarUtil.ZHI[this._p.monthZhiIndexExact+1];},
getMonthInGanZhi:function(){return this.getMonthGan()+this.getMonthZhi();},
getMonthInGanZhiExact:function(){return this.getMonthGanExact()+this.getMonthZhiExact();},
getDayGan:function(){return LunarUtil.GAN[this._p.dayGanIndex+1];},
getDayGanExact:function(){return LunarUtil.GAN[this._p.dayGanIndexExact+1];},
getDayGanExact2:function(){return LunarUtil.GAN[this._p.dayGanIndexExact2+1];},
getDayZhi:function(){return LunarUtil.ZHI[this._p.dayZhiIndex+1];},
getDayZhiExact:function(){return LunarUtil.ZHI[this._p.dayZhiIndexExact+1];},
getDayZhiExact2:function(){return LunarUtil.ZHI[this._p.dayZhiIndexExact2+1];},
getDayInGanZhi:function(){return this.getDayGan()+this.getDayZhi();},
getDayInGanZhiExact:function(){return this.getDayGanExact()+this.getDayZhiExact();},
getDayInGanZhiExact2:function(){return this.getDayGanExact2()+this.getDayZhiExact2();},
getTimeGan:function(){return LunarUtil.GAN[this._p.timeGanIndex+1];},
getTimeZhi:function(){return LunarUtil.ZHI[this._p.timeZhiIndex+1];},
getTimeInGanZhi:function(){return this.getTimeGan()+this.getTimeZhi();},
getShengxiao:function(){return this.getYearShengXiao();},
getYearShengXiao:function(){return LunarUtil.SHENGXIAO[this._p.yearZhiIndex+1];},
getYearShengXiaoByLiChun:function(){return LunarUtil.SHENGXIAO[this._p.yearZhiIndexByLiChun+1];},
getYearShengXiaoExact:function(){return LunarUtil.SHENGXIAO[this._p.yearZhiIndexExact+1];},
getMonthShengXiao:function(){return LunarUtil.SHENGXIAO[this._p.monthZhiIndex+1];},
getMonthShengXiaoExact:function(){return LunarUtil.SHENGXIAO[this._p.monthZhiIndexExact+1];},
getDayShengXiao:function(){return LunarUtil.SHENGXIAO[this._p.dayZhiIndex+1];},
getTimeShengXiao:function(){return LunarUtil.SHENGXIAO[this._p.timeZhiIndex+1];},
getYearInChinese:function(){
var y = this._p.year+'';
var s = '';
var zero = '0'.charCodeAt(0);
for(var i=0,j=y.length;i<j;i++){
s+=LunarUtil.NUMBER[y.charCodeAt(i)-zero];
}
return s;
},
getMonthInChinese:function(){
var month = this._p.month;
return (month<0?'闰':'')+LunarUtil.MONTH[Math.abs(month)];
},
getDayInChinese:function(){
return LunarUtil.DAY[this._p.day];
},
getPengZuGan:function(){
return LunarUtil.PENGZU_GAN[this._p.dayGanIndex+1];
},
getPengZuZhi:function(){
return LunarUtil.PENGZU_ZHI[this._p.dayZhiIndex+1];
},
getPositionXi:function(){
return this.getDayPositionXi();
},
getPositionXiDesc:function(){
return this.getDayPositionXiDesc();
},
getPositionYangGui:function(){
return this.getDayPositionYangGui();
},
getPositionYangGuiDesc:function(){
return this.getDayPositionYangGuiDesc();
},
getPositionYinGui:function(){
return this.getDayPositionYinGui();
},
getPositionYinGuiDesc:function(){
return this.getDayPositionYinGuiDesc();
},
getPositionFu:function(){
return this.getDayPositionFu();
},
getPositionFuDesc:function(){
return this.getDayPositionFuDesc();
},
getPositionCai:function(){
return this.getDayPositionCai();
},
getPositionCaiDesc:function(){
return this.getDayPositionCaiDesc();
},
getDayPositionXi:function(){
return LunarUtil.POSITION_XI[this._p.dayGanIndex+1];
},
getDayPositionXiDesc:function(){
return LunarUtil.POSITION_DESC[this.getDayPositionXi()];
},
getDayPositionYangGui:function(){
return LunarUtil.POSITION_YANG_GUI[this._p.dayGanIndex+1];
},
getDayPositionYangGuiDesc:function(){
return LunarUtil.POSITION_DESC[this.getDayPositionYangGui()];
},
getDayPositionYinGui:function(){
return LunarUtil.POSITION_YIN_GUI[this._p.dayGanIndex+1];
},
getDayPositionYinGuiDesc:function(){
return LunarUtil.POSITION_DESC[this.getDayPositionYinGui()];
},
getDayPositionFu:function(sect){
return (1===sect?LunarUtil.POSITION_FU:LunarUtil.POSITION_FU_2)[this._p.dayGanIndex+1];
},
getDayPositionFuDesc:function(sect){
return LunarUtil.POSITION_DESC[this.getDayPositionFu(sect)];
},
getDayPositionCai:function(){
return LunarUtil.POSITION_CAI[this._p.dayGanIndex+1];
},
getDayPositionCaiDesc:function(){
return LunarUtil.POSITION_DESC[this.getDayPositionCai()];
},
getTimePositionXi:function(){
return LunarUtil.POSITION_XI[this._p.timeGanIndex+1];
},
getTimePositionXiDesc:function(){
return LunarUtil.POSITION_DESC[this.getTimePositionXi()];
},
getTimePositionYangGui:function(){
return LunarUtil.POSITION_YANG_GUI[this._p.timeGanIndex+1];
},
getTimePositionYangGuiDesc:function(){
return LunarUtil.POSITION_DESC[this.getTimePositionYangGui()];
},
getTimePositionYinGui:function(){
return LunarUtil.POSITION_YIN_GUI[this._p.timeGanIndex+1];
},
getTimePositionYinGuiDesc:function(){
return LunarUtil.POSITION_DESC[this.getTimePositionYinGui()];
},
getTimePositionFu:function(sect){
return (1===sect?LunarUtil.POSITION_FU:LunarUtil.POSITION_FU_2)[this._p.timeGanIndex+1];
},
getTimePositionFuDesc:function(sect){
return LunarUtil.POSITION_DESC[this.getTimePositionFu(sect)];
},
getTimePositionCai:function(){
return LunarUtil.POSITION_CAI[this._p.timeGanIndex+1];
},
getTimePositionCaiDesc:function(){
return LunarUtil.POSITION_DESC[this.getTimePositionCai()];
},
_getDayPositionTaiSui:function(dayInGanZhi, yearZhiIndex){
var p = '';
switch (dayInGanZhi) {
case '甲子':
case '乙丑':
case '丙寅':
case '丁卯':
case '戊辰':
case '已巳':
p = '震';
break;
case '丙子':
case '丁丑':
case '戊寅':
case '已卯':
case '庚辰':
case '辛巳':
p = '离';
break;
case '戊子':
case '已丑':
case '庚寅':
case '辛卯':
case '壬辰':
case '癸巳':
p = '中';
break;
case '庚子':
case '辛丑':
case '壬寅':
case '癸卯':
case '甲辰':
case '乙巳':
p = '兑';
break;
case '壬子':
case '癸丑':
case '甲寅':
case '乙卯':
case '丙辰':
case '丁巳':
p = '坎';
break;
default:
p = LunarUtil.POSITION_TAI_SUI_YEAR[yearZhiIndex];
}
return p;
},
getDayPositionTaiSui:function(sect){
var dayInGanZhi;
var yearZhiIndex;
switch (sect) {
case 1:
dayInGanZhi = this.getDayInGanZhi();
yearZhiIndex = this._p.yearZhiIndex;
break;
case 3:
dayInGanZhi = this.getDayInGanZhi();
yearZhiIndex = this._p.yearZhiIndexExact;
break;
default:
dayInGanZhi = this.getDayInGanZhiExact2();
yearZhiIndex = this._p.yearZhiIndexByLiChun;
}
return this._getDayPositionTaiSui(dayInGanZhi, yearZhiIndex);
},
getDayPositionTaiSuiDesc:function(sect){
return LunarUtil.POSITION_DESC[this.getDayPositionTaiSui(sect)];
},
_getMonthPositionTaiSui:function(monthZhiIndex, monthGanIndex){
var p = '';
var m = monthZhiIndex - LunarUtil.BASE_MONTH_ZHI_INDEX;
if (m < 0) {
m += 12;
}
switch(m) {
case 0:
case 4:
case 8:
p = '艮';
break;
case 2:
case 6:
case 10:
p = '坤';
break;
case 3:
case 7:
case 11:
p = '巽';
break;
default:
p = LunarUtil.POSITION_GAN[monthGanIndex];
}
return p;
},
getMonthPositionTaiSui:function(sect){
var monthZhiIndex;
var monthGanIndex;
switch (sect) {
case 3:
monthZhiIndex = this._p.monthZhiIndexExact;
monthGanIndex = this._p.monthGanIndexExact;
break;
default:
monthZhiIndex = this._p.monthZhiIndex;
monthGanIndex = this._p.monthGanIndex;
}
return this._getMonthPositionTaiSui(monthZhiIndex, monthGanIndex);
},
getMonthPositionTaiSuiDesc:function(sect){
return LunarUtil.POSITION_DESC[this.getMonthPositionTaiSui(sect)];
},
getYearPositionTaiSui:function(sect){
var yearZhiIndex;
switch (sect) {
case 1:
yearZhiIndex = this._p.yearZhiIndex;
break;
case 3:
yearZhiIndex = this._p.yearZhiIndexExact;
break;
default:
yearZhiIndex = this._p.yearZhiIndexByLiChun;
}
return LunarUtil.POSITION_TAI_SUI_YEAR[yearZhiIndex];
},
getYearPositionTaiSuiDesc:function(sect){
return LunarUtil.POSITION_DESC[this.getYearPositionTaiSui(sect)];
},
getChong:function(){
return this.getDayChong();
},
getChongGan:function(){
return this.getDayChongGan();
},
getChongGanTie:function(){
re