forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePage.js
More file actions
142 lines (127 loc) · 3.82 KB
/
Copy pathusePage.js
File metadata and controls
142 lines (127 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { reactive } from 'vue'
import { extend, isEqual } from '@opentiny/vue-renderless/common/object'
const DEFAULT_PAGE = {
app: '',
name: '',
route: '',
page_content: {
componentName: 'Page',
css: '',
props: {},
lifeCycles: {},
children: [],
dataSource: {
list: []
},
state: {},
methods: {},
utils: [],
bridge: [],
inputs: [],
outputs: []
},
isHome: false,
parentId: 'none',
isBody: false,
group: 'staticPages'
}
const pageSettingState = reactive({
currentPageDataCopy: {}, // 记录当前页最开始的状态,当用户点击取消按钮的时候恢复到初始状态
currentPageData: {}, // 当前配置页面的数据
pages: [],
oldParentId: null,
pageTreeKey: 0,
isNew: false,
ROOT_ID: '0', // 根节点ID
updateTreeData: null,
treeDataMapping: {}
})
const isTemporaryPage = reactive({
saved: false
})
const isCurrentDataSame = () => {
const data = pageSettingState.currentPageData || {}
const dataCopy = pageSettingState.currentPageDataCopy || {}
let isEqual = true
Object.keys(dataCopy).some((item) => {
// 页面比较是否更改,为了减少判断次数,不需要判断以下字段
if (['children', 'label', 'createdBy', 'assets', 'occupier'].includes(item)) {
return false
} else if (item === 'page_content') {
const obj = {
inputs: dataCopy[item].inputs,
outputs: dataCopy[item].outputs,
lifeCycles: dataCopy[item].lifeCycles
}
const objCopy = {
inputs: data[item].inputs,
outputs: data[item].outputs,
lifeCycles: data[item].lifeCycles
}
if (JSON.stringify(obj) !== JSON.stringify(objCopy)) {
isEqual = false
}
} else {
if (dataCopy[item] !== data[item]) {
isEqual = false
}
}
return !isEqual
})
return isEqual
}
const changeTreeData = (newParentId, oldParentId) => {
if (newParentId && oldParentId && newParentId !== oldParentId) {
const folderData = pageSettingState.treeDataMapping[newParentId]
const parentData = pageSettingState.treeDataMapping[oldParentId]
const currentPageDataId = pageSettingState.currentPageData.id
const curDataIndex = parentData.children?.findIndex?.(({ id }) => id === currentPageDataId)
if (curDataIndex > -1) {
parentData.children.splice(curDataIndex, 1)
if (!folderData.children) {
folderData.children = []
}
folderData.children.unshift(pageSettingState.currentPageData)
pageSettingState.pageTreeKey++
}
}
}
const getPageContent = () => {
return pageSettingState.currentPageData.page_content || {}
}
const initCurrentPageData = (pageDetail) => {
pageSettingState.currentPageData = pageDetail
pageSettingState.currentPageDataCopy = extend(true, {}, pageDetail)
pageSettingState.oldParentId = pageDetail.parentId
}
const resetPageData = () => {
pageSettingState.currentPageData = {}
pageSettingState.currentPageDataCopy = {}
pageSettingState.oldParentId = null
}
// 判断当前页面内容是否有修改
const isChangePageData = () => !isEqual(pageSettingState.currentPageData, pageSettingState.currentPageDataCopy)
export default () => {
return {
DEFAULT_PAGE,
pageSettingState,
isTemporaryPage,
isCurrentDataSame,
changeTreeData,
getPageContent,
resetPageData,
initCurrentPageData,
isChangePageData
}
}