1 | import {SerializeOptions, Serializable, SerializePath} from './StructureNodes'
|
2 | import {SerializeError, HELP_URL} from './SerializeError'
|
3 |
|
4 | export function maybeSerializeMenuItemGroup(
|
5 | item: MenuItemGroup | MenuItemGroupBuilder,
|
6 | index: number,
|
7 | path: SerializePath
|
8 | ): MenuItemGroup {
|
9 | return item instanceof MenuItemGroupBuilder ? item.serialize({path, index}) : item
|
10 | }
|
11 |
|
12 | export interface MenuItemGroup {
|
13 | id: string
|
14 | title: string
|
15 | }
|
16 |
|
17 | export class MenuItemGroupBuilder implements Serializable {
|
18 | protected _id: string
|
19 | protected _title: string
|
20 |
|
21 | constructor(spec?: MenuItemGroup) {
|
22 | this._id = spec ? spec.id : ''
|
23 | this._title = spec ? spec.title : ''
|
24 | }
|
25 |
|
26 | id(id: string): MenuItemGroupBuilder {
|
27 | return new MenuItemGroupBuilder({id, title: this._title})
|
28 | }
|
29 |
|
30 | getId() {
|
31 | return this._id
|
32 | }
|
33 |
|
34 | title(title: string): MenuItemGroupBuilder {
|
35 | return new MenuItemGroupBuilder({id: this._id, title})
|
36 | }
|
37 |
|
38 | getTitle() {
|
39 | return this._title
|
40 | }
|
41 |
|
42 | serialize(options: SerializeOptions = {path: []}): MenuItemGroup {
|
43 | const {_id, _title} = this
|
44 | if (!_id) {
|
45 | throw new SerializeError(
|
46 | '`id` is required for a menu item group',
|
47 | options.path,
|
48 | options.index,
|
49 | _title
|
50 | ).withHelpUrl(HELP_URL.ID_REQUIRED)
|
51 | }
|
52 |
|
53 | if (!_title) {
|
54 | throw new SerializeError(
|
55 | '`title` is required for a menu item group',
|
56 | options.path,
|
57 | _id
|
58 | ).withHelpUrl(HELP_URL.TITLE_REQUIRED)
|
59 | }
|
60 |
|
61 | return {
|
62 | id: _id,
|
63 | title: _title
|
64 | }
|
65 | }
|
66 | }
|