2019-10-01 11:35:00 +00:00
|
|
|
const common = require('./common')
|
2019-09-13 13:33:10 +00:00
|
|
|
|
2022-06-21 11:50:03 +00:00
|
|
|
const Tree = window.__nfptree && window.__nfptree.tree || []
|
2019-09-13 13:33:10 +00:00
|
|
|
|
|
|
|
exports.Tree = Tree
|
|
|
|
|
|
|
|
exports.createPage = function(body) {
|
2019-10-01 11:35:00 +00:00
|
|
|
return common.sendRequest({
|
2019-09-13 13:33:10 +00:00
|
|
|
method: 'POST',
|
|
|
|
url: '/api/pages',
|
|
|
|
body: body,
|
|
|
|
}).then(function(res) {
|
|
|
|
res.children = []
|
|
|
|
if (!res.parent_id) {
|
|
|
|
Tree.push(res)
|
|
|
|
} else {
|
|
|
|
for (let i = 0; i < Tree.length; i++) {
|
|
|
|
if (Tree[i].id === res.parent_id) {
|
|
|
|
Tree[i].children.push(res)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-27 08:41:18 +00:00
|
|
|
function processPageBranch(arr, branches, prefix) {
|
|
|
|
branches.forEach((page) => {
|
|
|
|
arr.push({ id: page.id, name: prefix + page.name })
|
|
|
|
if (page.children && page.children.length) {
|
|
|
|
processPageBranch(arr, page.children, page.name + ' -> ')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getFlatTree = function() {
|
|
|
|
let arr = []
|
|
|
|
processPageBranch(arr, Tree, '')
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2019-10-01 03:45:44 +00:00
|
|
|
exports.getTree = function() {
|
2019-10-01 11:35:00 +00:00
|
|
|
return common.sendRequest({
|
2019-09-13 13:33:10 +00:00
|
|
|
method: 'GET',
|
|
|
|
url: '/api/pages?tree=true&includes=children&fields=id,name,path,children(id,name,path)',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.updatePage = function(id, body) {
|
2019-10-01 11:35:00 +00:00
|
|
|
return common.sendRequest({
|
2019-09-13 13:33:10 +00:00
|
|
|
method: 'PUT',
|
|
|
|
url: '/api/pages/' + id,
|
|
|
|
body: body,
|
|
|
|
}).then(function(res) {
|
|
|
|
for (let i = 0; i < Tree.length; i++) {
|
|
|
|
if (Tree[i].id === res.id) {
|
|
|
|
res.children = Tree[i].children
|
|
|
|
Tree[i] = res
|
|
|
|
break
|
|
|
|
} else if (Tree[i].id === res.parent_id) {
|
|
|
|
for (let x = 0; x < Tree[i].children.length; x++) {
|
|
|
|
if (Tree[i].children[x].id === res.id) {
|
|
|
|
res.children = Tree[i].children[x].children
|
|
|
|
Tree[i].children[x] = res
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!res.children) {
|
|
|
|
res.children = []
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getAllPages = function() {
|
2019-10-01 11:35:00 +00:00
|
|
|
return common.sendRequest({
|
2019-09-13 13:33:10 +00:00
|
|
|
method: 'GET',
|
|
|
|
url: '/api/pages',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getPage = function(id) {
|
2019-10-01 11:35:00 +00:00
|
|
|
return common.sendRequest({
|
2019-09-13 13:33:10 +00:00
|
|
|
method: 'GET',
|
2021-02-05 11:50:01 +00:00
|
|
|
url: '/api/pages/' + id + '?includes=media,banner',
|
2019-09-13 13:33:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.removePage = function(page, id) {
|
2019-10-01 11:35:00 +00:00
|
|
|
return common.sendRequest({
|
2019-09-13 13:33:10 +00:00
|
|
|
method: 'DELETE',
|
|
|
|
url: '/api/pages/' + id,
|
|
|
|
}).then(function() {
|
|
|
|
for (let i = 0; i < Tree.length; i++) {
|
|
|
|
if (Tree[i].id === page.id) {
|
|
|
|
Tree.splice(i, 1)
|
|
|
|
break
|
|
|
|
} else if (Tree[i].id === page.parent_id) {
|
|
|
|
for (let x = 0; x < Tree[i].children.length; x++) {
|
|
|
|
if (Tree[i].children[x].id === page.id) {
|
|
|
|
Tree[i].children.splice(x, 1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|