-
Notifications
You must be signed in to change notification settings - Fork 1
/
editable.js
131 lines (120 loc) · 4.64 KB
/
editable.js
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
import React, { Component } from 'react';
import { cn, NavLink, withRouter, withApollo, gql } from 'olymp';
import { Icon } from 'antd';
import { SortableContainer as sortableContainer, SortableElement as sortableElement, SortableHandle as sortableHandle, arrayMove } from 'react-sortable-hoc';
// const Handle = sortableHandle(() => <span className="grippy" style={{ zIndex: 1, position: 'absolute', right: 0, top: 'calc(50% - 9px)', width: '15px', height: '100%' }} />);
const Handle = sortableHandle(() => <Icon type="appstore" className="sortHandle" />);
const Li = sortableElement((props) => {
const { editable, children, page, location, router, isSubmenu } = props;
const { query, pathname } = location;
const navigation = !!query && query.navigation;
let matches = [];
page.children.forEach((e) => {
matches = matches.concat(e.children.filter(child => child.id === navigation));
});
const visible = navigation === page.id || !!matches.length;
return (
<div className={cn(isSubmenu ? 'dropdown-item' : 'nav-item', page.path === pathname ? 'active' : null, visible ? 'focus' : null)}>
{page.blocks || editable ? (
<NavLink to={page.path || page.slug} className="nav-link">
{page.name}
</NavLink>
) : (
<span
onClick={() => router.push({ pathname, query: { navigation: page.id } })}
className="nav-link"
>
{page.name}
</span>
)}
{children}
{editable && !page.noOrdering ? <Handle /> : null}
</div>
);
});
const Ul = sortableContainer(({ axis, useDragHandle, onSortEnd, sortEndCreator, hideSortableGhost, ...rest }) => <div {...rest} />);
@withRouter
@withApollo
export default class Navbar extends Component {
onSortEnd = children => ({ oldIndex, newIndex }) => {
const { client } = this.props;
const newChildren = arrayMove(children, oldIndex, newIndex);
client.mutate({
mutation: gql`
mutation reorderPages($ids: [String]) {
reorderPages(ids: $ids) {
id, order
}
}
`,
variables: {
ids: newChildren.map(x => x.id),
},
optimisticResponse: {
__typename: 'Mutation',
reorderPages: newChildren.map(({ id }, order) => ({ id, order })),
},
});
};
getMenu = (page, i, isSubmenu) => {
const { location, editable } = this.props;
return (
<Li {...this.props} page={page} isSubmenu={isSubmenu} index={i} key={page.id}>
{editable || (page.children && page.children.length) ? (
<Ul
onSortEnd={this.onSortEnd(page.children)}
sortEndCreator={this.onSortEnd}
axis="y"
useDragHandle
className="dropdown-menu"
>
{(page.children || []).map((child, i) => this.getMenu(child, i, true))}
{editable ? (
<li className="nav-item">
<NavLink to={{ ...location, query: { '@new-page': page.id } }} className="nav-link">
<Icon type="plus-circle" />
</NavLink>
</li>
) : null}
</Ul>
) : null}
</Li>
);
}
render() {
const { editable, router, brand, fill, pages, location, dark, light } = this.props;
const { query, pathname } = location;
const navigation = !!query && query.navigation !== undefined; // null/page.id => true, undefined => false
return (
<div className="bootstrap">
<nav className={cn('navbar navbar-toggleable-sm', { 'navbar-inverse': dark }, { 'navbar-light': light })}>
{brand ? <NavLink to="/" className="navbar-brand">{brand}</NavLink> : null}
<button
onClick={() => router.push({ pathname, query: { navigation: navigation ? undefined : null } })}
className="navbar-toggler hidden-md-up"
>
<span className="navbar-toggler-icon"></span>
</button>
<div className={`navbar-collapse${!navigation ? ' hidden-sm-down' : ''}`}>
<Ul
onSortEnd={this.onSortEnd(pages)}
sortEndCreator={this.onSortEnd}
axis="x"
useDragHandle
className={cn('navbar-nav', { 'nav-fill': fill })}
>
{(pages || []).map((page, i) => this.getMenu(page, i))}
{editable ? (
<div className="nav-item">
<NavLink to={{ ...location, query: { '@new-page': null } }} className="nav-link">
<Icon type="plus-circle" />
</NavLink>
</div>
) : null}
</Ul>
</div>
</nav>
</div>
);
}
}