Showing
12 changed files
with
545 additions
and
10 deletions
| @@ -5,15 +5,25 @@ import { Logger } from 'wdKit'; | @@ -5,15 +5,25 @@ import { Logger } from 'wdKit'; | ||
| 5 | import { ENewspaperItemComponent } from './ENewspaperItemComponent'; | 5 | import { ENewspaperItemComponent } from './ENewspaperItemComponent'; |
| 6 | import { ENewspaperListDialog } from '../dialog/ENewspaperListDialog'; | 6 | import { ENewspaperListDialog } from '../dialog/ENewspaperListDialog'; |
| 7 | import display from '@ohos.display'; | 7 | import display from '@ohos.display'; |
| 8 | +import { ENewspaperCalendarDialog } from '../dialog/ENewspaperCalendarDialog'; | ||
| 9 | +import font from '@ohos.font'; | ||
| 8 | 10 | ||
| 9 | @Component | 11 | @Component |
| 10 | export struct ENewspaperPageComponent { | 12 | export struct ENewspaperPageComponent { |
| 11 | @State newspaperListBean: NewspaperListBean = {} as NewspaperListBean | 13 | @State newspaperListBean: NewspaperListBean = {} as NewspaperListBean |
| 12 | @State currentPageNum: string = '01' | 14 | @State currentPageNum: string = '01' |
| 13 | @State pageNumPopup: boolean = false | 15 | @State pageNumPopup: boolean = false |
| 14 | - @State date: string = '2024-01-26' | 16 | + @State calendarPopup: boolean = false |
| 17 | + @State calendarDateTitle: string = '' | ||
| 15 | @State picHeight: number = 0 | 18 | @State picHeight: number = 0 |
| 16 | private swiperController: SwiperController = new SwiperController() | 19 | private swiperController: SwiperController = new SwiperController() |
| 20 | + calendarDialogController: CustomDialogController = new CustomDialogController({ | ||
| 21 | + builder: ENewspaperCalendarDialog(), | ||
| 22 | + alignment: DialogAlignment.Top, | ||
| 23 | + offset: { dx: 0, dy: 80 }, | ||
| 24 | + customStyle: true, | ||
| 25 | + // cornerRadius: 4 | ||
| 26 | + }) | ||
| 17 | listDialogController: CustomDialogController = new CustomDialogController({ | 27 | listDialogController: CustomDialogController = new CustomDialogController({ |
| 18 | builder: ENewspaperListDialog({ newspaperListBean: this.newspaperListBean, | 28 | builder: ENewspaperListDialog({ newspaperListBean: this.newspaperListBean, |
| 19 | currentPageNum: this.currentPageNum, | 29 | currentPageNum: this.currentPageNum, |
| @@ -64,9 +74,10 @@ export struct ENewspaperPageComponent { | @@ -64,9 +74,10 @@ export struct ENewspaperPageComponent { | ||
| 64 | }) | 74 | }) |
| 65 | 75 | ||
| 66 | Row() { | 76 | Row() { |
| 67 | - Text(this.date) | 77 | + Text(this.calendarDateTitle) |
| 68 | .fontSize($r('app.float.font_size_20')) | 78 | .fontSize($r('app.float.font_size_20')) |
| 69 | .fontColor($r('app.color.white')) | 79 | .fontColor($r('app.color.white')) |
| 80 | + .fontFamily('BebasNeue_Regular') | ||
| 70 | 81 | ||
| 71 | Image($r('app.media.icon_triangle')) | 82 | Image($r('app.media.icon_triangle')) |
| 72 | .width($r('app.float.border_radius_6')) | 83 | .width($r('app.float.border_radius_6')) |
| @@ -77,6 +88,14 @@ export struct ENewspaperPageComponent { | @@ -77,6 +88,14 @@ export struct ENewspaperPageComponent { | ||
| 77 | .alignRules({ middle: { anchor: "__container__", align: HorizontalAlign.Center }, | 88 | .alignRules({ middle: { anchor: "__container__", align: HorizontalAlign.Center }, |
| 78 | center: { anchor: "__container__", align: VerticalAlign.Center } }) | 89 | center: { anchor: "__container__", align: VerticalAlign.Center } }) |
| 79 | .id('e_newspaper_date') | 90 | .id('e_newspaper_date') |
| 91 | + .onClick(() => { | ||
| 92 | + this.calendarPopup = !this.calendarPopup | ||
| 93 | + if (this.calendarPopup) { | ||
| 94 | + this.calendarDialogController.open() | ||
| 95 | + } else { | ||
| 96 | + this.calendarDialogController.close() | ||
| 97 | + } | ||
| 98 | + }) | ||
| 80 | 99 | ||
| 81 | Image($r('app.media.icon_share')) | 100 | Image($r('app.media.icon_share')) |
| 82 | .height($r('app.float.top_arrow_size')) | 101 | .height($r('app.float.top_arrow_size')) |
| @@ -193,25 +212,25 @@ export struct ENewspaperPageComponent { | @@ -193,25 +212,25 @@ export struct ENewspaperPageComponent { | ||
| 193 | } | 212 | } |
| 194 | 213 | ||
| 195 | async aboutToAppear() { | 214 | async aboutToAppear() { |
| 215 | + //注册字体 | ||
| 216 | + font.registerFont({ | ||
| 217 | + familyName: 'BebasNeue_Regular', | ||
| 218 | + // familySrc: '/font/roboto_medium_numbers.ttf' // font文件夹与pages目录同级 | ||
| 219 | + familySrc: $rawfile('font/BebasNeue_Regular.otf') | ||
| 220 | + }) | ||
| 196 | let newspaperTimes = await NewspaperViewModel.getNewspaperTime() | 221 | let newspaperTimes = await NewspaperViewModel.getNewspaperTime() |
| 197 | if (newspaperTimes && newspaperTimes.length > 0) { | 222 | if (newspaperTimes && newspaperTimes.length > 0) { |
| 198 | - this.date = newspaperTimes[0].date | 223 | + this.calendarDateTitle = newspaperTimes[0].date |
| 199 | } | 224 | } |
| 200 | try { | 225 | try { |
| 201 | let displayTool = display.getDefaultDisplaySync() | 226 | let displayTool = display.getDefaultDisplaySync() |
| 202 | let screenWidth = displayTool.width | 227 | let screenWidth = displayTool.width |
| 203 | let picWidth = screenWidth - vp2px(52) | 228 | let picWidth = screenWidth - vp2px(52) |
| 204 | this.picHeight = picWidth * 566 / 378 | 229 | this.picHeight = picWidth * 566 / 378 |
| 205 | - let listBean = await NewspaperViewModel.getNewspaperList(this.date, picWidth + 'x' + this.picHeight) | 230 | + let listBean = await NewspaperViewModel.getNewspaperList(this.calendarDateTitle, picWidth + 'x' + this.picHeight) |
| 206 | this.newspaperListBean = listBean; | 231 | this.newspaperListBean = listBean; |
| 207 | } catch (exception) { | 232 | } catch (exception) { |
| 208 | 233 | ||
| 209 | } | 234 | } |
| 210 | - | ||
| 211 | - | ||
| 212 | - } | ||
| 213 | - | ||
| 214 | - aboutToDisappear() { | ||
| 215 | - | ||
| 216 | } | 235 | } |
| 217 | } | 236 | } |
| 1 | +import { RMCalendarBean } from './RMCalendarBean' | ||
| 2 | +import { RMCalenderCell } from './RMCalendarCell' | ||
| 3 | + | ||
| 4 | +const TAG = "RMCalendar" | ||
| 5 | + | ||
| 6 | +@Component | ||
| 7 | +export struct RMCalendar { | ||
| 8 | + @State selectItem: RMCalendarBean = new RMCalendarBean() | ||
| 9 | + private today: Date = new Date() // 当天 | ||
| 10 | + // 开始日期 | ||
| 11 | + startDate: Date = new Date() | ||
| 12 | + // 截止日期 | ||
| 13 | + endDate: Date = new Date() | ||
| 14 | + //当前日期-当前显示的月份的第一天 | ||
| 15 | + private startDay: Date = new Date( | ||
| 16 | + this.today.getFullYear(), | ||
| 17 | + this.today.getMonth(), | ||
| 18 | + 1 | ||
| 19 | + ) | ||
| 20 | + // 是否有上一个月 | ||
| 21 | + @State private hasPre: boolean = true | ||
| 22 | + // 是否有下一个月 | ||
| 23 | + @State private hasNext: boolean = true | ||
| 24 | + // 标题栏高度 | ||
| 25 | + titleHeight: Length = '50vp' | ||
| 26 | + // 星期标题 | ||
| 27 | + weeks: string[] = ["日", "一", "二", "三", "四", "五", "六",] | ||
| 28 | + // 星期标题字体大小 | ||
| 29 | + weekTitleFontSize: number | string | Resource = 12 | ||
| 30 | + // 星期标题字体颜色 | ||
| 31 | + weekTitleFontColor: ResourceColor = "#999999" | ||
| 32 | + // 星期标栏高度 | ||
| 33 | + weekTitleHeight: Length = 30 | ||
| 34 | + weekFontWeight: FontWeight = FontWeight.Bold | ||
| 35 | + // 标题字体大小 | ||
| 36 | + titleFontSize: number | string | Resource = 16 | ||
| 37 | + // 标题字体颜色 | ||
| 38 | + titleFontColor: ResourceColor = "#333333" | ||
| 39 | + titleFontWeight: FontWeight = FontWeight.Bold | ||
| 40 | + // 日期每一项字体大小 | ||
| 41 | + itemFontSize: number | string | Resource = 14 | ||
| 42 | + itemFontColor: ResourceColor = "#333333" | ||
| 43 | + itemFontWeight: FontWeight = FontWeight.Bold | ||
| 44 | + // 今日字体颜色 | ||
| 45 | + todayFontColor: ResourceColor = "#ED2800" | ||
| 46 | + // 不能使用的日期字体颜色 | ||
| 47 | + disabledFontColor: ResourceColor = "#CCCCCC" | ||
| 48 | + // 选中日期字体颜色 | ||
| 49 | + selectFontColor: ResourceColor = "#FFFFFF" | ||
| 50 | + // 选中日期背景颜色, 默认与todayFontColor一致 | ||
| 51 | + selectItemBgColor: ResourceColor = "#ED2800" | ||
| 52 | + @State private title: string = '' | ||
| 53 | + // 计算的总加载 | ||
| 54 | + @State dates: Array<RMCalendarBean> = new Array() | ||
| 55 | + // 已选日期 | ||
| 56 | + @State selectedDates: Array<RMCalendarBean> = new Array() | ||
| 57 | + // 自定义每一项布局 | ||
| 58 | + public cellLayout?: (item: RMCalendarBean) => void | ||
| 59 | + // 仅自定义 今日 样式,当使用cellLayout时,tadayLayout无效 | ||
| 60 | + todayLayout?: (item: RMCalendarBean) => void | ||
| 61 | + // 计算item时,如需添加更多自定义属性时使用 | ||
| 62 | + reBuildDateItem?: (item: RMCalendarBean) => RMCalendarBean | ||
| 63 | + // 选择变化监听, | ||
| 64 | + onDateChange?: (date1: RMCalendarBean) => void | ||
| 65 | + onMonthChange?: (after: Date, befor: Date) => void | ||
| 66 | + // 不可选中项的点击事件 | ||
| 67 | + disableCellClick?: (item: RMCalendarBean) => void | ||
| 68 | + | ||
| 69 | + @Builder | ||
| 70 | + createWeekTitle(item: string) { | ||
| 71 | + Text(item) | ||
| 72 | + .textAlign(TextAlign.Center) | ||
| 73 | + .fontColor(this.weekTitleFontColor) | ||
| 74 | + .fontSize(this.weekTitleFontSize) | ||
| 75 | + .fontWeight(this.weekFontWeight) | ||
| 76 | + .layoutWeight(1) | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + @Builder | ||
| 80 | + createCell() { | ||
| 81 | + ForEach(this.dates, (item: RMCalendarBean) => { | ||
| 82 | + RMCalenderCell({ | ||
| 83 | + item: item, | ||
| 84 | + cellLayout: this.cellLayout, | ||
| 85 | + itemFontSize: this.itemFontSize, | ||
| 86 | + itemFontColor: this.itemFontColor, | ||
| 87 | + today: this.today.getTime(), | ||
| 88 | + itemFontWeight: this.itemFontWeight, | ||
| 89 | + todayFontColor: this.todayFontColor, | ||
| 90 | + todayLayout: this.todayLayout, | ||
| 91 | + selectItem: $selectItem, | ||
| 92 | + selectFontColor: this.selectFontColor, | ||
| 93 | + selectItemBgColor: this.selectItemBgColor, | ||
| 94 | + selectedDates: $selectedDates, | ||
| 95 | + disabledFontColor: this.disabledFontColor, | ||
| 96 | + hasPre: this.hasPre, | ||
| 97 | + hasNext: this.hasNext, | ||
| 98 | + disableClick: (item: RMCalendarBean) => { | ||
| 99 | + if (this.disableCellClick) { | ||
| 100 | + this.disableCellClick(item) | ||
| 101 | + } | ||
| 102 | + }, | ||
| 103 | + cellClick: (item: RMCalendarBean) => { | ||
| 104 | + if (item.isPre) { | ||
| 105 | + this.preMonth() | ||
| 106 | + } else if (item.isNext) { | ||
| 107 | + this.nextMonth() | ||
| 108 | + } | ||
| 109 | + if (this.onDateChange) { | ||
| 110 | + this.onDateChange(item) | ||
| 111 | + if (item.fullYear && item.month) { | ||
| 112 | + this.title = `${item.fullYear}年${item.month + 1}月` | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + }) | ||
| 117 | + .width(`14.28%`) | ||
| 118 | + }, (item: RMCalendarBean) => JSON.stringify(item)) | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * 属性初始化 | ||
| 123 | + */ | ||
| 124 | + initAttr() { | ||
| 125 | + if (!this.selectItemBgColor) { | ||
| 126 | + this.selectItemBgColor = this.todayFontColor | ||
| 127 | + } | ||
| 128 | + this.today = new Date( | ||
| 129 | + this.today.getFullYear(), | ||
| 130 | + this.today.getMonth(), | ||
| 131 | + this.today.getDate(), | ||
| 132 | + ) | ||
| 133 | + | ||
| 134 | + // 开始日期 | ||
| 135 | + if (!this.startDate) { | ||
| 136 | + this.startDate = new Date(1970, 0, 1) | ||
| 137 | + } | ||
| 138 | + // 截止日期 | ||
| 139 | + if (!this.endDate) { | ||
| 140 | + this.endDate = new Date(this.today.getFullYear() + 10, 11, 31) | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + if (this.today.getTime() < this.startDate.getTime()) { | ||
| 144 | + this.startDay.setTime(this.startDate.getTime()) | ||
| 145 | + } else if (this.today.getTime() > this.endDate.getTime()) { | ||
| 146 | + this.startDay.setTime(this.endDate.getTime()) | ||
| 147 | + } else { | ||
| 148 | + this.startDay.setTime(this.today.getTime()) | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + aboutToAppear() { | ||
| 153 | + this.initAttr() | ||
| 154 | + let temp = new RMCalendarBean() | ||
| 155 | + temp.time = this.today.getTime() | ||
| 156 | + this.selectItem = temp | ||
| 157 | + this.calcDatas() | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + /** | ||
| 161 | + * 下一个月 | ||
| 162 | + */ | ||
| 163 | + private nextMonth() { | ||
| 164 | + // this.dates.slice(0, this.dates.length) | ||
| 165 | + this.dates = [] | ||
| 166 | + const beforDate = new Date(this.startDay.getFullYear(), this.startDay.getMonth()) | ||
| 167 | + this.startDay.setMonth(this.startDay.getMonth() + 1) | ||
| 168 | + if (this.onMonthChange) { | ||
| 169 | + this.onMonthChange(new Date(this.startDay.getFullYear(), this.startDay.getMonth()), beforDate) | ||
| 170 | + } | ||
| 171 | + this.calcDatas() | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + /** | ||
| 175 | + * 上一个月 | ||
| 176 | + */ | ||
| 177 | + private preMonth() { | ||
| 178 | + // this.dates.slice(0, this.dates.length) | ||
| 179 | + this.dates = [] | ||
| 180 | + const beforDate = new Date(this.startDay.getFullYear(), this.startDay.getMonth()) | ||
| 181 | + this.startDay.setMonth(this.startDay.getMonth() - 1) | ||
| 182 | + if (this.onMonthChange) { | ||
| 183 | + this.onMonthChange(new Date(this.startDay.getFullYear(), this.startDay.getMonth()), beforDate) | ||
| 184 | + } | ||
| 185 | + this.calcDatas() | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + /** | ||
| 189 | + * 具体计算 | ||
| 190 | + */ | ||
| 191 | + private calcDatas() { | ||
| 192 | + const startDay = this.startDay | ||
| 193 | + this.title = `${startDay.getFullYear()}年${startDay.getMonth() + 1}月` | ||
| 194 | + startDay.setDate(1) | ||
| 195 | + | ||
| 196 | + if (startDay.getFullYear() < this.startDate.getFullYear() | ||
| 197 | + || (startDay.getFullYear() == this.startDate.getFullYear() && startDay.getMonth() <= this.startDate.getMonth())) { | ||
| 198 | + this.hasPre = false | ||
| 199 | + } else { | ||
| 200 | + this.hasPre = true | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + if (startDay.getFullYear() > this.endDate.getFullYear() | ||
| 204 | + || (startDay.getFullYear() == this.endDate.getFullYear() && startDay.getMonth() >= this.endDate.getMonth())) { | ||
| 205 | + this.hasNext = false | ||
| 206 | + } else { | ||
| 207 | + this.hasNext = true | ||
| 208 | + } | ||
| 209 | + | ||
| 210 | + // 计算第一个月 | ||
| 211 | + // 获取第一个月总天数 | ||
| 212 | + let endDay: Date = new Date( | ||
| 213 | + startDay.getFullYear(), | ||
| 214 | + startDay.getMonth() + 1, | ||
| 215 | + 0, 23, 59, 59) | ||
| 216 | + | ||
| 217 | + let tempDate: Date = new Date( | ||
| 218 | + startDay.getFullYear(), | ||
| 219 | + startDay.getMonth(), | ||
| 220 | + startDay.getDate() | ||
| 221 | + ) | ||
| 222 | + | ||
| 223 | + const count = endDay.getDate() | ||
| 224 | + const preCount = startDay.getDay() | ||
| 225 | + // const nextCount = 6 - endDay.getDay() | ||
| 226 | + const nextCount = 0 | ||
| 227 | + const finilCount = count + preCount + nextCount | ||
| 228 | + | ||
| 229 | + // 补齐上一个月 | ||
| 230 | + tempDate.setDate(tempDate.getDate() - preCount) | ||
| 231 | + | ||
| 232 | + // 添加日期 | ||
| 233 | + for (let index = 0; index < finilCount; index++) { | ||
| 234 | + let item = new RMCalendarBean( | ||
| 235 | + tempDate.getFullYear(), | ||
| 236 | + tempDate.getMonth(), | ||
| 237 | + tempDate.getDate(), | ||
| 238 | + tempDate.getDay(), | ||
| 239 | + tempDate.getTime(), | ||
| 240 | + // @ ts-ignore | ||
| 241 | + // LunarCalendar.convertSolarToLunar(tempDate), | ||
| 242 | + (index < preCount ? true : false) || this.startDate.getTime() > tempDate.getTime(), | ||
| 243 | + (index >= preCount + count ? true : false) || this.endDate.getTime() < tempDate.getTime(), | ||
| 244 | + ) | ||
| 245 | + if (this.reBuildDateItem) { | ||
| 246 | + this.reBuildDateItem(item) | ||
| 247 | + } | ||
| 248 | + this.dates.push(item) | ||
| 249 | + tempDate.setDate(tempDate.getDate() + 1) | ||
| 250 | + } | ||
| 251 | + } | ||
| 252 | + | ||
| 253 | + build() { | ||
| 254 | + Column() { | ||
| 255 | + Image($r("app.media.iv_e_news_pager_calendar_arrow_up")) | ||
| 256 | + .width(18).height(8.5) | ||
| 257 | + Column() { | ||
| 258 | + Row() { | ||
| 259 | + Column() { | ||
| 260 | + Image(this.hasPre ? $r("app.media.iv_e_news_pager_calendar_arrow_pre") | ||
| 261 | + : $r("app.media.iv_e_news_pager_calendar_arrow_pre_gray")) | ||
| 262 | + .width(22) | ||
| 263 | + .aspectRatio(1) | ||
| 264 | + } | ||
| 265 | + .justifyContent(FlexAlign.Center) | ||
| 266 | + .height("100%") | ||
| 267 | + .aspectRatio(1) | ||
| 268 | + .onClick(() => { | ||
| 269 | + if (this.hasPre) { | ||
| 270 | + this.preMonth() | ||
| 271 | + } | ||
| 272 | + }) | ||
| 273 | + | ||
| 274 | + Blank() | ||
| 275 | + Row() { | ||
| 276 | + Text(this.title) | ||
| 277 | + .fontSize(this.titleFontSize) | ||
| 278 | + .fontColor(this.titleFontColor) | ||
| 279 | + .fontWeight(this.titleFontWeight) | ||
| 280 | + } | ||
| 281 | + | ||
| 282 | + Blank() | ||
| 283 | + Column() { | ||
| 284 | + Image(this.hasNext ? $r("app.media.iv_e_news_pager_calendar_arrow_next") | ||
| 285 | + : $r("app.media.iv_e_news_pager_calendar_arrow_next_gray")) | ||
| 286 | + .width(22) | ||
| 287 | + .aspectRatio(1) | ||
| 288 | + } | ||
| 289 | + .justifyContent(FlexAlign.Center) | ||
| 290 | + .height("100%") | ||
| 291 | + .aspectRatio(1) | ||
| 292 | + .onClick(() => { | ||
| 293 | + if (this.hasNext) { | ||
| 294 | + this.nextMonth() | ||
| 295 | + } | ||
| 296 | + }) | ||
| 297 | + } | ||
| 298 | + .alignItems(VerticalAlign.Center) | ||
| 299 | + .width("100%") | ||
| 300 | + .height(this.titleHeight) | ||
| 301 | + | ||
| 302 | + // 星期title | ||
| 303 | + Row() { | ||
| 304 | + ForEach(this.weeks, (item: string) => { | ||
| 305 | + this.createWeekTitle(item) | ||
| 306 | + }, (item: string) => { | ||
| 307 | + return item | ||
| 308 | + }) | ||
| 309 | + } | ||
| 310 | + .alignItems(VerticalAlign.Center) | ||
| 311 | + .height(this.weekTitleHeight) | ||
| 312 | + | ||
| 313 | + Flex({ wrap: FlexWrap.Wrap }) { | ||
| 314 | + this.createCell() | ||
| 315 | + } | ||
| 316 | + .width("100%") | ||
| 317 | + } | ||
| 318 | + .backgroundColor(Color.White) | ||
| 319 | + .margin({ | ||
| 320 | + left: 35, | ||
| 321 | + right: 35 | ||
| 322 | + }) | ||
| 323 | + .border({ radius: 4 }) | ||
| 324 | + } | ||
| 325 | + } | ||
| 326 | +} |
| 1 | +@Observed | ||
| 2 | +export class RMCalendarBean { | ||
| 3 | + fullYear?: number | ||
| 4 | + month?: number | ||
| 5 | + date?: number | ||
| 6 | + week?: number | ||
| 7 | + time?: number | ||
| 8 | + isPre?: boolean // 是否是上一个月的 / 在startDate 之前 | ||
| 9 | + isNext?: boolean // 是否是下一个月的 / 在endDate 之后 | ||
| 10 | + | ||
| 11 | + constructor(fullYear?: number, month?: number, | ||
| 12 | + date?: number, day?: number, | ||
| 13 | + time?: number, | ||
| 14 | + isPre?: boolean, | ||
| 15 | + isNext?: boolean) { | ||
| 16 | + this.fullYear = fullYear | ||
| 17 | + this.month = month | ||
| 18 | + this.date = date | ||
| 19 | + this.week = day | ||
| 20 | + this.time = time | ||
| 21 | + this.isPre = isPre | ||
| 22 | + this.isNext = isNext | ||
| 23 | + } | ||
| 24 | +} |
| 1 | +import { RMCalendarBean } from './RMCalendarBean'; | ||
| 2 | +import font from '@ohos.font'; | ||
| 3 | + | ||
| 4 | +@Component | ||
| 5 | +export struct RMCalenderCell { | ||
| 6 | + private selectOpacity: number = 0.15 | ||
| 7 | + itemFontSize: Length = 0 | ||
| 8 | + itemFontColor: ResourceColor = {} as ResourceColor | ||
| 9 | + itemFontWeight: FontWeight = FontWeight.Normal | ||
| 10 | + todayFontColor: ResourceColor = {} as ResourceColor | ||
| 11 | + selectFontColor: ResourceColor = {} as ResourceColor | ||
| 12 | + selectItemBgColor: ResourceColor = {} as ResourceColor | ||
| 13 | + disabledFontColor: ResourceColor = {} as ResourceColor | ||
| 14 | + // 今日时间戳 | ||
| 15 | + today: number = 0 | ||
| 16 | + @Link selectItem: RMCalendarBean | ||
| 17 | + @Link selectedDates: Array<RMCalendarBean> | ||
| 18 | + @Prop hasPre: boolean | ||
| 19 | + @Prop hasNext: boolean | ||
| 20 | + @ObjectLink item: RMCalendarBean | ||
| 21 | + // 自定义每一项布局 | ||
| 22 | + cellLayout?: (item: RMCalendarBean) => void | ||
| 23 | + todayLayout?: (item: RMCalendarBean) => void | ||
| 24 | + cellClick?: (item: RMCalendarBean) => void | ||
| 25 | + disableClick?: (item: RMCalendarBean) => void | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 检测是否包含在数组中 | ||
| 29 | + */ | ||
| 30 | + checkInArrays() { | ||
| 31 | + for (let index = 0; index < this.selectedDates.length; index++) { | ||
| 32 | + if (this.item.time == this.selectedDates[index].time) { | ||
| 33 | + return index | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + return -1 | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + getItemColor() { | ||
| 40 | + if (this.item.isPre) { | ||
| 41 | + return this.disabledFontColor | ||
| 42 | + } else if (this.item.isNext) { | ||
| 43 | + return this.disabledFontColor | ||
| 44 | + } else if (this.selectItem && this.selectItem.time == this.item.time) { | ||
| 45 | + return this.selectFontColor | ||
| 46 | + } else if (this.item.time == this.today) { | ||
| 47 | + return this.todayFontColor | ||
| 48 | + } | ||
| 49 | + return this.itemFontColor | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + getBorder() { | ||
| 53 | + if (this.selectItem && this.selectItem.time == this.item.time) { | ||
| 54 | + return this.border({ | ||
| 55 | + width: 1, | ||
| 56 | + color: this.getItemColor() | ||
| 57 | + }) | ||
| 58 | + } else { | ||
| 59 | + return this.border({ | ||
| 60 | + width: 0, | ||
| 61 | + } | ||
| 62 | + ) | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + /** | ||
| 67 | + * 获取选中项背景颜色 | ||
| 68 | + */ | ||
| 69 | + getSelectItemBg() { | ||
| 70 | + return this.selectItemBgColor | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * 获取选中项背景透明度 | ||
| 75 | + * @returns | ||
| 76 | + */ | ||
| 77 | + getSelectItemBgOpa() { | ||
| 78 | + return this.item.isPre || this.item.isNext ? this.selectOpacity : 1 | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + /** | ||
| 82 | + * 是否需要显示选项背景 | ||
| 83 | + */ | ||
| 84 | + isShowSelectBg() { | ||
| 85 | + return this.selectItem && this.selectItem.time == this.item.time | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + build() { | ||
| 89 | + Column() { | ||
| 90 | + Stack() { | ||
| 91 | + if (!this.item.isPre) { | ||
| 92 | + if (this.isShowSelectBg()) { | ||
| 93 | + Column()// .position({ x: "10%", y: "10%" }) | ||
| 94 | + .height("80%") | ||
| 95 | + .aspectRatio(1)// .borderRadius(999) | ||
| 96 | + .backgroundColor(this.getSelectItemBg()) | ||
| 97 | + .opacity(this.getSelectItemBgOpa()) | ||
| 98 | + .transition({ type: TransitionType.Insert, opacity: 1 }) | ||
| 99 | + .transition({ type: TransitionType.Delete, opacity: 0 }) | ||
| 100 | + } | ||
| 101 | + Text(this.item.date + '') | ||
| 102 | + .fontSize(this.itemFontSize) | ||
| 103 | + .fontColor(this.getItemColor()) | ||
| 104 | + .fontWeight(this.itemFontWeight) | ||
| 105 | + .fontFamily('BebasNeue_Regular') | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + // .justifyContent(FlexAlign.Center) | ||
| 109 | + .aspectRatio(1) | ||
| 110 | + // .border(this.getBorder()) | ||
| 111 | + .width("80%") | ||
| 112 | + } | ||
| 113 | + .justifyContent(FlexAlign.Center) | ||
| 114 | + // .width("100%") | ||
| 115 | + // .aspectRatio(1) | ||
| 116 | + .onClick(() => { | ||
| 117 | + // animateTo({ duration: 200 }, () => { | ||
| 118 | + // | ||
| 119 | + // }) | ||
| 120 | + | ||
| 121 | + if (this.item.isNext) { | ||
| 122 | + if (!this.hasNext) { | ||
| 123 | + if (this.disableClick) { | ||
| 124 | + this.disableClick(this.item) | ||
| 125 | + } | ||
| 126 | + return | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + if (this.item.isPre) { | ||
| 130 | + // if (!this.hasPre) { | ||
| 131 | + if (this.disableClick) { | ||
| 132 | + this.disableClick(this.item) | ||
| 133 | + } | ||
| 134 | + return | ||
| 135 | + // } | ||
| 136 | + } | ||
| 137 | + this.selectItem = this.item | ||
| 138 | + if (this.cellClick) { | ||
| 139 | + this.cellClick(this.item) | ||
| 140 | + } | ||
| 141 | + }) | ||
| 142 | + } | ||
| 143 | +} |
| 1 | +import { RMCalendar } from '../components/calendar/RMCalendar' | ||
| 2 | +import { RMCalendarBean } from '../components/calendar/RMCalendarBean' | ||
| 3 | + | ||
| 4 | +@CustomDialog | ||
| 5 | +export struct ENewspaperCalendarDialog { | ||
| 6 | + calendarDialogController?: CustomDialogController | ||
| 7 | + | ||
| 8 | + build() { | ||
| 9 | + RMCalendar({ | ||
| 10 | + // 开始日期 | ||
| 11 | + startDate: new Date(2023, 8, 1), | ||
| 12 | + // 截止日期 | ||
| 13 | + endDate: new Date(2024, 2, 20), | ||
| 14 | + // 日期选择变化监听 | ||
| 15 | + onDateChange: (date1: RMCalendarBean) => { | ||
| 16 | + console.log("onDateChange", "date1:", JSON.stringify(date1)) | ||
| 17 | + if (this.calendarDialogController) { | ||
| 18 | + this.calendarDialogController.close() | ||
| 19 | + } | ||
| 20 | + } | ||
| 21 | + }) | ||
| 22 | + } | ||
| 23 | +} |
434 Bytes
441 Bytes
462 Bytes
486 Bytes
532 Bytes
No preview for this file type
-
Please register or login to post a comment