|
|
@@ -0,0 +1,212 @@
|
|
|
+import { BasicType, IBestToast, YTAvoid, yTRouter } from "basic"
|
|
|
+import { window } from "@kit.ArkUI"
|
|
|
+import { undoData } from "../model/Index"
|
|
|
+
|
|
|
+@ObservedV2
|
|
|
+export class ClassSchedulePageViewModel{
|
|
|
+ @Trace safeTop: number = 0
|
|
|
+ @Trace table: Array<string[]> = new Array(5).fill(new Array(8).fill('11'))
|
|
|
+ @Trace weekList: string[] = ['周一', '周二', '周三', '周四', '周五'] // ,
|
|
|
+
|
|
|
+ @Trace selectX: number = -1
|
|
|
+ @Trace selectY: number = -1
|
|
|
+
|
|
|
+ conText: UIContext
|
|
|
+ // 控制按钮
|
|
|
+ controlBtn: Array<BasicType> = [
|
|
|
+ {
|
|
|
+ text: '添加行',
|
|
|
+ click: () => {
|
|
|
+ this._onAddRow()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '添加列',
|
|
|
+ click: () => {
|
|
|
+ this._onAddColumn()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '删除行',
|
|
|
+ click: () => {
|
|
|
+ this._onDeleteRow()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '删除列',
|
|
|
+ click: () => {
|
|
|
+ this._onDeleteColumn()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '撤销',
|
|
|
+ click: () => {
|
|
|
+ this._onUndo()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '保存',
|
|
|
+ click: () => {
|
|
|
+ this._onSave()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '返回',
|
|
|
+ click: () => {
|
|
|
+ this._onBackPressed()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ // 上次的操作是不是保存
|
|
|
+ isSave: boolean = false
|
|
|
+ // 撤销数据
|
|
|
+ undoDataList: Array<undoData> = []
|
|
|
+ // 原始的周数据
|
|
|
+ originWeekList: string[] = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
|
|
|
+
|
|
|
+ constructor(conText: UIContext) {
|
|
|
+ this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
|
|
|
+ this.conText = conText
|
|
|
+
|
|
|
+ window.getLastWindow(this.conText.getHostContext())
|
|
|
+ .then(res => {
|
|
|
+ res.setPreferredOrientation(window.Orientation.LANDSCAPE)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获得焦点
|
|
|
+ onFocus(x: number, y: number) {
|
|
|
+ this.selectX = x
|
|
|
+ this.selectY = y
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加行
|
|
|
+ */
|
|
|
+ _onAddRow() {
|
|
|
+ let x = this.table.length
|
|
|
+ let y = this.table[0].length
|
|
|
+
|
|
|
+ if(x == 7) return
|
|
|
+
|
|
|
+ this._addUndoData()
|
|
|
+ this.weekList.push(this.originWeekList[this.weekList.length])
|
|
|
+ this.table.push(new Array(y).fill(''))
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加列
|
|
|
+ */
|
|
|
+ _onAddColumn() {
|
|
|
+ let x = this.table.length
|
|
|
+ let y = this.table[0].length
|
|
|
+ this._addUndoData()
|
|
|
+ for (let index = 0; index < this.table.length; index++) {
|
|
|
+ let arr = [...this.table[index]]
|
|
|
+ arr.push('')
|
|
|
+ this.table[index] = arr
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除行
|
|
|
+ */
|
|
|
+ _onDeleteRow() {
|
|
|
+ let x = this.table.length
|
|
|
+ let y = this.table[0].length
|
|
|
+
|
|
|
+ if(x == 5){
|
|
|
+ IBestToast.show('行数不足5行')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ this._addUndoData()
|
|
|
+ this.weekList.pop()
|
|
|
+ this.table.splice(x-1, 1)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除列
|
|
|
+ */
|
|
|
+ _onDeleteColumn() {
|
|
|
+ let x = this.table.length
|
|
|
+ let y = this.table[0].length
|
|
|
+
|
|
|
+ if(y == 8) {
|
|
|
+ IBestToast.show('列数不足8列')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ this._addUndoData()
|
|
|
+ for (let index = 0; index < x; index++) {
|
|
|
+ let arr = [...this.table[index]]
|
|
|
+ arr.splice(y-1, 1)
|
|
|
+ this.table[index] = arr
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 撤销操作
|
|
|
+ */
|
|
|
+ _onUndo() {
|
|
|
+ if(this.undoDataList.length == 0) return
|
|
|
+
|
|
|
+ const undoDate: undoData = this.undoDataList.pop()!
|
|
|
+ this.table = [...undoDate.arr2!]
|
|
|
+ this.weekList = [...undoDate.arr!]
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存
|
|
|
+ */
|
|
|
+ _onSave() {
|
|
|
+ if(this.isSave) return
|
|
|
+ this.isSave = true
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加 《撤销》 数据 - 仅保存 10 次操作
|
|
|
+ */
|
|
|
+ _addUndoData() {
|
|
|
+ this.isSave = false
|
|
|
+ const undoData: undoData = {
|
|
|
+ arr2: [...this.table],
|
|
|
+ arr: [...this.weekList],
|
|
|
+ }
|
|
|
+ this.undoDataList.push(undoData)
|
|
|
+ if(this.undoDataList.length == 10) {
|
|
|
+ this.undoDataList.splice(0, 1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回正屏幕并返回
|
|
|
+ */
|
|
|
+ _onBack(){
|
|
|
+ window.getLastWindow(this.conText.getHostContext())
|
|
|
+ .then(res => {
|
|
|
+ res.setPreferredOrientation(window.Orientation.UNSPECIFIED)
|
|
|
+ yTRouter.pop('')
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重写的返回逻辑
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ _onBackPressed(){
|
|
|
+ if(!this.isSave) {
|
|
|
+ yTRouter.router2DoubleConfirmDiaLog({
|
|
|
+ text: '编辑内容未保存,确定离开吗?',
|
|
|
+ color: '#7186F9'
|
|
|
+ }, (res) => {
|
|
|
+ if(res && res.result == 'true') {
|
|
|
+ this._onBack()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ this._onBack()
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|