@ -1,4 +1,27 @@
import { shortcut , Widget , getDate , each , range , extend , isLeapYear , Date , StartOfWeek , checkDateVoid , map , createWidget , createItems , LogicFactory , Controller , getShortDayName , getOffsetDate , isNotEmptyString , parseInt } from "@/core" ;
import { Label , ButtonGroup } from "@/base" ;
import {
CenterLayout ,
shortcut ,
Widget ,
getDate ,
each ,
range ,
extend ,
isLeapYear ,
Date ,
StartOfWeek ,
checkDateVoid ,
map ,
createWidget ,
createItems ,
LogicFactory ,
Controller ,
getShortDayName ,
getOffsetDate ,
isNotEmptyString ,
parseInt
} from "@/core" ;
import { CalendarDateItem } from "./calendar.date.item" ;
/ * *
/ * *
* Created by GUY on 2015 / 8 / 28.
* Created by GUY on 2015 / 8 / 28.
@ -8,17 +31,17 @@ import { shortcut, Widget, getDate, each, range, extend, isLeapYear, Date, Start
@ shortcut ( )
@ shortcut ( )
export class Calendar extends Widget {
export class Calendar extends Widget {
static xtype = "bi.calendar" ;
static xtype = "bi.calendar" ;
static getPageByDateJSON ( json ) {
static getPageByDateJSON ( json ) {
const year = getDate ( ) . getFullYear ( ) ;
const year = getDate ( ) . getFullYear ( ) ;
const month = getDate ( ) . getMonth ( ) ;
const month = getDate ( ) . getMonth ( ) ;
let page = ( json . year - year ) * 12 ;
let page = ( json . year - year ) * 12 ;
page += json . month - 1 - month ;
page += json . month - 1 - month ;
return page ;
return page ;
}
}
static getDateJSONByPage ( v ) {
static getDateJSONByPage ( v ) {
const months = getDate ( ) . getMonth ( ) ;
const months = getDate ( ) . getMonth ( ) ;
let page = v ;
let page = v ;
@ -29,17 +52,17 @@ export class Calendar extends Widget {
if ( page < 0 && page % 12 !== 0 ) {
if ( page < 0 && page % 12 !== 0 ) {
year -- ;
year -- ;
}
}
const month = page >= 0 ? ( page % 12 ) : ( ( 12 + page % 12 ) % 12 ) ;
const month = page >= 0 ? page % 12 : ( 12 + ( page % 12 ) ) % 12 ;
return {
return {
year : getDate ( ) . getFullYear ( ) + year ,
year : getDate ( ) . getFullYear ( ) + year ,
month : month + 1 ,
month : month + 1 ,
} ;
} ;
}
}
_defaultConfig ( ) {
_defaultConfig ( ) {
const conf = super . _defaultConfig ( ... arguments ) ;
const conf = super . _defaultConfig ( ... arguments ) ;
return extend ( conf , {
return extend ( conf , {
baseCls : "bi-calendar" ,
baseCls : "bi-calendar" ,
logic : {
logic : {
@ -52,8 +75,10 @@ export class Calendar extends Widget {
day : 25 ,
day : 25 ,
} ) ;
} ) ;
}
}
_dateCreator ( Y , M , D ) {
_dateCreator ( Y , M , D ) {
const { min , max } = this . options , log = { } , De = getDate ( ) ;
const { min , max } = this . options ,
log = { } ,
De = getDate ( ) ;
const mins = min . match ( /\d+/g ) ;
const mins = min . match ( /\d+/g ) ;
const maxs = max . match ( /\d+/g ) ;
const maxs = max . match ( /\d+/g ) ;
@ -78,7 +103,9 @@ export class Calendar extends Widget {
const items = [ ] ;
const items = [ ] ;
each ( range ( 42 ) , i => {
each ( range ( 42 ) , i => {
const td = { } ;
const td = { } ;
let YY = log . ymd [ 0 ] , MM = log . ymd [ 1 ] + 1 , DD ;
let YY = log . ymd [ 0 ] ,
MM = log . ymd [ 1 ] + 1 ,
DD ;
// 上个月的日期
// 上个月的日期
if ( i < offSetFDay ) {
if ( i < offSetFDay ) {
td . lastMonth = true ;
td . lastMonth = true ;
@ -103,83 +130,101 @@ export class Calendar extends Widget {
td . text = DD ;
td . text = DD ;
items . push ( td ) ;
items . push ( td ) ;
} ) ;
} ) ;
return items ;
return items ;
}
}
_init ( ) {
_init ( ) {
super . _init ( ... arguments ) ;
super . _init ( ... arguments ) ;
const { year , month , day , logic } = this . options ;
const { year , month , day , logic } = this . options ;
const items = map ( this . _getWeekLabel ( ) , ( i , value ) => {
const items = map ( this . _getWeekLabel ( ) , ( i , value ) => {
return {
return {
type : "bi.label" ,
type : Label . xtype ,
height : BI . SIZE _CONSANTS . LIST _ITEM _HEIGHT ,
height : BI . SIZE _CONSANTS . LIST _ITEM _HEIGHT ,
text : value ,
text : value ,
} ;
} ;
} ) ;
} ) ;
const title = createWidget ( {
const title = createWidget ( {
type : "bi.button_group" ,
type : ButtonGroup . xtype ,
height : 44 ,
height : 44 ,
items ,
items ,
layouts : [ {
layouts : [
type : "bi.center" ,
{
hgap : 5 ,
type : CenterLayout . xtype ,
vgap : 10 ,
hgap : 5 ,
} ] ,
vgap : 10 ,
}
] ,
} ) ;
} ) ;
this . days = createWidget ( {
this . days = createWidget ( {
type : "bi.button_group" ,
type : ButtonGroup . xtype ,
items : createItems ( this . _getItems ( ) , { } ) ,
items : createItems ( this . _getItems ( ) , { } ) ,
value : ` ${ year } - ${ month } - ${ day } ` ,
value : ` ${ year } - ${ month } - ${ day } ` ,
layouts : [ LogicFactory . createLogic ( "table" , extend ( { } , logic , {
layouts : [
columns : 7 ,
LogicFactory . createLogic (
rows : 6 ,
"table" ,
columnSize : [ 1 / 7 , 1 / 7 , 1 / 7 , 1 / 7 , 1 / 7 , 1 / 7 , 1 / 7 ] ,
extend ( { } , logic , {
rowSize : BI . SIZE _CONSANTS . LIST _ITEM _HEIGHT + 8 ,
columns : 7 ,
} ) ) ] ,
rows : 6 ,
columnSize : [ 1 / 7 , 1 / 7 , 1 / 7 , 1 / 7 , 1 / 7 , 1 / 7 , 1 / 7 ] ,
rowSize : BI . SIZE _CONSANTS . LIST _ITEM _HEIGHT + 8 ,
} )
)
] ,
} ) ;
} ) ;
this . days . on ( Controller . EVENT _CHANGE , ( ... args ) => {
this . days . on ( Controller . EVENT _CHANGE , ( ... args ) => {
this . fireEvent ( Controller . EVENT _CHANGE , ... args ) ;
this . fireEvent ( Controller . EVENT _CHANGE , ... args ) ;
} ) ;
} ) ;
createWidget ( extend ( {
createWidget (
element : this ,
extend (
{
} , LogicFactory . createLogic ( "vertical" , extend ( { } , logic , {
element : this ,
items : LogicFactory . createLogicItemsByDirection ( "top" , title , {
} ,
el : this . days ,
LogicFactory . createLogic (
tgap : - 5 ,
"vertical" ,
} ) ,
extend ( { } , logic , {
} ) ) ) ) ;
items : LogicFactory . createLogicItemsByDirection ( "top" , title , {
el : this . days ,
tgap : - 5 ,
} ) ,
} )
)
)
) ;
}
}
_getWeekLabel ( ) {
_getWeekLabel ( ) {
return map ( range ( 0 , 7 ) , ( idx , v ) => getShortDayName ( ( v + StartOfWeek ) % 7 ) ) ;
return map ( range ( 0 , 7 ) , ( idx , v ) => getShortDayName ( ( v + StartOfWeek ) % 7 ) ) ;
}
}
isFrontDate ( ) {
isFrontDate ( ) {
const { year , month , min , max } = this . options ;
const { year , month , min , max } = this . options ;
let Y = year ;
let Y = year ;
const M = month , De = getDate ( ) , day = De . getDay ( ) ;
const M = month ,
De = getDate ( ) ,
day = De . getDay ( ) ;
Y = Y | 0 ;
Y = Y | 0 ;
De . setFullYear ( Y , M , 1 ) ;
De . setFullYear ( Y , M , 1 ) ;
const newDate = getOffsetDate ( De , - 1 * ( day + 1 ) ) ;
const newDate = getOffsetDate ( De , - 1 * ( day + 1 ) ) ;
return ! ! checkDateVoid ( newDate . getFullYear ( ) , newDate . getMonth ( ) , newDate . getDate ( ) , min , max ) [ 0 ] ;
return ! ! checkDateVoid ( newDate . getFullYear ( ) , newDate . getMonth ( ) , newDate . getDate ( ) , min , max ) [ 0 ] ;
}
}
isFinalDate ( ) {
isFinalDate ( ) {
const { year , month , min , max } = this . options ;
const { year , month , min , max } = this . options ;
let Y = year ;
let Y = year ;
const M = month , De = getDate ( ) , day = De . getDay ( ) ;
const M = month ,
De = getDate ( ) ,
day = De . getDay ( ) ;
Y = Y | 0 ;
Y = Y | 0 ;
De . setFullYear ( Y , M , 1 ) ;
De . setFullYear ( Y , M , 1 ) ;
const newDate = getOffsetDate ( De , 42 - day ) ;
const newDate = getOffsetDate ( De , 42 - day ) ;
return ! ! checkDateVoid ( newDate . getFullYear ( ) , newDate . getMonth ( ) , newDate . getDate ( ) , min , max ) [ 0 ] ;
return ! ! checkDateVoid ( newDate . getFullYear ( ) , newDate . getMonth ( ) , newDate . getDate ( ) , min , max ) [ 0 ] ;
}
}
_getItems ( ) {
_getItems ( ) {
const o = this . options ;
const o = this . options ;
const days = this . _dateCreator ( o . year , o . month - 1 , o . day ) ;
const days = this . _dateCreator ( o . year , o . month - 1 , o . day ) ;
const items = [ ] ;
const items = [ ] ;
@ -190,38 +235,40 @@ export class Calendar extends Widget {
items . push ( days . slice ( 28 , 35 ) ) ;
items . push ( days . slice ( 28 , 35 ) ) ;
items . push ( days . slice ( 35 , 42 ) ) ;
items . push ( days . slice ( 35 , 42 ) ) ;
return map ( items , ( i , item ) => map ( item , ( j , td ) => {
return map ( items , ( i , item ) =>
let month = td . lastMonth ? o . month - 1 : ( td . nextMonth ? o . month + 1 : o . month ) ;
map ( item , ( j , td ) => {
let year = o . year ;
let month = td . lastMonth ? o . month - 1 : td . nextMonth ? o . month + 1 : o . month ;
if ( month > 12 ) {
let year = o . year ;
month = 1 ;
if ( month > 12 ) {
year ++ ;
month = 1 ;
} else if ( month < 1 ) {
year ++ ;
month = 12 ;
} else if ( month < 1 ) {
year -- ;
month = 12 ;
}
year -- ;
}
return extend ( td , {
type : "bi.calendar_date_item" ,
return extend ( td , {
once : false ,
type : CalendarDateItem . xtype ,
forceSelected : true ,
once : false ,
value : ` ${ year } - ${ month } - ${ td . text } ` ,
forceSelected : true ,
disabled : td . disabled ,
value : ` ${ year } - ${ month } - ${ td . text } ` ,
cls : td . lastMonth || td . nextMonth ? "bi-tips" : "" ,
disabled : td . disabled ,
lgap : 2 ,
cls : td . lastMonth || td . nextMonth ? "bi-tips" : "" ,
rgap : 2 ,
lgap : 2 ,
tgap : 4 ,
rgap : 2 ,
bgap : 4 ,
tgap : 4 ,
// selected: td.currentDay
bgap : 4 ,
} ) ;
// selected: td.currentDay
} ) ) ;
} ) ;
} )
) ;
}
}
_populate ( ) {
_populate ( ) {
this . days . populate ( this . _getItems ( ) ) ;
this . days . populate ( this . _getItems ( ) ) ;
}
}
setMinDate ( minDate ) {
setMinDate ( minDate ) {
const o = this . options ;
const o = this . options ;
if ( isNotEmptyString ( o . min ) ) {
if ( isNotEmptyString ( o . min ) ) {
o . min = minDate ;
o . min = minDate ;
@ -229,7 +276,7 @@ export class Calendar extends Widget {
}
}
}
}
setMaxDate ( maxDate ) {
setMaxDate ( maxDate ) {
const o = this . options ;
const o = this . options ;
if ( isNotEmptyString ( o . max ) ) {
if ( isNotEmptyString ( o . max ) ) {
o . max = maxDate ;
o . max = maxDate ;
@ -237,13 +284,13 @@ export class Calendar extends Widget {
}
}
}
}
setValue ( ob ) {
setValue ( ob ) {
this . days . setValue ( [ ` ${ ob . year } - ${ ob . month } - ${ ob . day } ` ] ) ;
this . days . setValue ( [ ` ${ ob . year } - ${ ob . month } - ${ ob . day } ` ] ) ;
}
}
getValue ( ) {
getValue ( ) {
const date = this . days . getValue ( ) [ 0 ] . match ( /\d+/g ) ;
const date = this . days . getValue ( ) [ 0 ] . match ( /\d+/g ) ;
return {
return {
year : date [ 0 ] | 0 ,
year : date [ 0 ] | 0 ,
month : date [ 1 ] | 0 ,
month : date [ 1 ] | 0 ,