2019-10-01 11:35:00 +00:00
|
|
|
const common = require('./common')
|
2019-09-13 13:33:10 +00:00
|
|
|
|
2019-10-01 03:45:44 +00:00
|
|
|
const Tree = window.__nfptree || []
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
url: '/api/pages/' + id + '?includes=media,banner,children,news,news.media',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|