UNPKG

3.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const getExtendedProjection_1 = require("./util/getExtendedProjection");
4const Schema_1 = require("./parts/Schema");
5const Icon_1 = require("./parts/Icon");
6const Sort_1 = require("./Sort");
7const SerializeError_1 = require("./SerializeError");
8const SortIcon = Icon_1.getSortIcon();
9function maybeSerializeMenuItem(item, index, path) {
10 return item instanceof MenuItemBuilder ? item.serialize({ path, index }) : item;
11}
12exports.maybeSerializeMenuItem = maybeSerializeMenuItem;
13class MenuItemBuilder {
14 constructor(spec) {
15 this.spec = spec ? spec : {};
16 }
17 action(action) {
18 return this.clone({ action });
19 }
20 getAction() {
21 return this.spec.action;
22 }
23 intent(intent) {
24 return this.clone({ intent });
25 }
26 getIntent() {
27 return this.spec.intent;
28 }
29 title(title) {
30 return this.clone({ title });
31 }
32 getTitle() {
33 return this.spec.title;
34 }
35 group(group) {
36 return this.clone({ group });
37 }
38 getGroup() {
39 return this.spec.group;
40 }
41 icon(icon) {
42 return this.clone({ icon });
43 }
44 getIcon() {
45 return this.spec.icon;
46 }
47 params(params) {
48 return this.clone({ params });
49 }
50 getParams() {
51 return this.spec.params;
52 }
53 showAsAction(showAsAction) {
54 return this.clone({ showAsAction });
55 }
56 getShowAsAction() {
57 return this.spec.showAsAction;
58 }
59 serialize(options = { path: [] }) {
60 const { title, action, intent } = this.spec;
61 if (!title) {
62 const hint = typeof action === 'string' ? `action: "${action}"` : undefined;
63 throw new SerializeError_1.SerializeError('`title` is required for menu item', options.path, options.index, hint).withHelpUrl(SerializeError_1.HELP_URL.TITLE_REQUIRED);
64 }
65 if (!action && !intent) {
66 throw new SerializeError_1.SerializeError(`\`action\` or \`intent\` required for menu item with title ${this.spec.title}`, options.path, options.index, `"${title}"`).withHelpUrl(SerializeError_1.HELP_URL.ACTION_OR_INTENT_REQUIRED);
67 }
68 if (intent && action) {
69 throw new SerializeError_1.SerializeError('cannot set both `action` AND `intent`', options.path, options.index, `"${title}"`).withHelpUrl(SerializeError_1.HELP_URL.ACTION_AND_INTENT_MUTUALLY_EXCLUSIVE);
70 }
71 return Object.assign({}, this.spec, { title });
72 }
73 clone(withSpec) {
74 const builder = new MenuItemBuilder();
75 builder.spec = Object.assign({}, this.spec, (withSpec || {}));
76 return builder;
77 }
78}
79exports.MenuItemBuilder = MenuItemBuilder;
80function getOrderingMenuItem(ordering, extendedProjection) {
81 return new MenuItemBuilder()
82 .group('sorting')
83 .title(`Sort by ${ordering.title}`)
84 .icon(SortIcon)
85 .action('setSortOrder')
86 .params({ by: ordering.by, extendedProjection });
87}
88exports.getOrderingMenuItem = getOrderingMenuItem;
89function getOrderingMenuItemsForSchemaType(typeName) {
90 const type = typeof typeName === 'string' ? Schema_1.defaultSchema.get(typeName) : typeName;
91 return (type.orderings
92 ? type.orderings.concat(Sort_1.DEFAULT_ORDERING_OPTIONS)
93 : Sort_1.DEFAULT_ORDERING_OPTIONS).map((ordering) => getOrderingMenuItem(ordering, getExtendedProjection_1.getExtendedProjection(type, ordering.by)));
94}
95exports.getOrderingMenuItemsForSchemaType = getOrderingMenuItemsForSchemaType;
96
97//# sourceMappingURL=MenuItem.js.map