UNPKG

@shopify/app-bridge-core

Version:

**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**

200 lines (197 loc) 6.99 kB
import { actionWrapper, getMergedProps } from '../helper.js'; import { ActionSet } from '../ActionSet.js'; import { Group } from '../types.js'; var Action; (function (Action) { Action["OPEN"] = "APP::RESOURCE_PICKER::OPEN"; Action["SELECT"] = "APP::RESOURCE_PICKER::SELECT"; // Deprecated in 0.5.0 use 'APP::RESOURCE_PICKER::CANCEL' instead Action["CLOSE"] = "APP::RESOURCE_PICKER::CLOSE"; Action["UPDATE"] = "APP::RESOURCE_PICKER::UPDATE"; Action["CANCEL"] = "APP::RESOURCE_PICKER::CANCEL"; })(Action || (Action = {})); var CollectionSortOrder; (function (CollectionSortOrder) { CollectionSortOrder["Manual"] = "MANUAL"; CollectionSortOrder["BestSelling"] = "BEST_SELLING"; CollectionSortOrder["AlphaAsc"] = "ALPHA_ASC"; CollectionSortOrder["AlphaDesc"] = "ALPHA_DESC"; CollectionSortOrder["PriceDesc"] = "PRICE_DESC"; CollectionSortOrder["PriceAsc"] = "PRICE_ASC"; CollectionSortOrder["CreatedDesc"] = "CREATED_DESC"; CollectionSortOrder["Created"] = "CREATED"; CollectionSortOrder["MostRelevant"] = "MOST_RELEVANT"; })(CollectionSortOrder || (CollectionSortOrder = {})); var FulfillmentServiceType; (function (FulfillmentServiceType) { FulfillmentServiceType["GiftCard"] = "GIFT_CARD"; FulfillmentServiceType["Manual"] = "MANUAL"; FulfillmentServiceType["ThirdParty"] = "THIRD_PARTY"; })(FulfillmentServiceType || (FulfillmentServiceType = {})); var WeightUnit; (function (WeightUnit) { WeightUnit["Kilograms"] = "KILOGRAMS"; WeightUnit["Grams"] = "GRAMS"; WeightUnit["Pounds"] = "POUNDS"; WeightUnit["Ounces"] = "OUNCES"; })(WeightUnit || (WeightUnit = {})); var ProductVariantInventoryPolicy; (function (ProductVariantInventoryPolicy) { ProductVariantInventoryPolicy["Deny"] = "DENY"; ProductVariantInventoryPolicy["Continue"] = "CONTINUE"; })(ProductVariantInventoryPolicy || (ProductVariantInventoryPolicy = {})); var ProductVariantInventoryManagement; (function (ProductVariantInventoryManagement) { ProductVariantInventoryManagement["Shopify"] = "SHOPIFY"; ProductVariantInventoryManagement["NotManaged"] = "NOT_MANAGED"; ProductVariantInventoryManagement["FulfillmentService"] = "FULFILLMENT_SERVICE"; })(ProductVariantInventoryManagement || (ProductVariantInventoryManagement = {})); var ProductStatus; (function (ProductStatus) { ProductStatus["Active"] = "ACTIVE"; ProductStatus["Archived"] = "ARCHIVED"; ProductStatus["Draft"] = "DRAFT"; })(ProductStatus || (ProductStatus = {})); var ResourceType; (function (ResourceType) { ResourceType["Product"] = "product"; ResourceType["ProductVariant"] = "variant"; ResourceType["Collection"] = "collection"; })(ResourceType || (ResourceType = {})); var ActionVerb; (function (ActionVerb) { ActionVerb["Add"] = "add"; ActionVerb["Select"] = "select"; })(ActionVerb || (ActionVerb = {})); function select(payload) { return actionWrapper({ payload, group: Group.ResourcePicker, type: Action.SELECT, }); } function open(payload) { return actionWrapper({ payload, group: Group.ResourcePicker, type: Action.OPEN, }); } function cancel(payload) { return actionWrapper({ payload, group: Group.ResourcePicker, type: Action.CANCEL, }); } function close(payload) { return actionWrapper({ payload, group: Group.ResourcePicker, type: Action.CANCEL, }); } function update(payload) { return actionWrapper({ payload, group: Group.ResourcePicker, type: Action.UPDATE, }); } class ResourcePicker extends ActionSet { resourceType; initialQuery; filterQuery; selectMultiple; initialSelectionIds = []; selection = []; showHidden; showVariants; showDraft; showArchived; showDraftBadge; showArchivedBadge; actionVerb; constructor(app, options, resourceType) { super(app, Group.ResourcePicker, Group.ResourcePicker); this.resourceType = resourceType; this.set(options, false); } get payload() { return { ...this.options, id: this.id, resourceType: this.resourceType, }; } get options() { const options = { initialQuery: this.initialQuery, filterQuery: this.filterQuery, selectMultiple: this.selectMultiple, initialSelectionIds: this.initialSelectionIds, showHidden: this.showHidden, actionVerb: this.actionVerb, }; if (this.resourceType === ResourceType.Product) { const productOptions = { ...options, showVariants: this.showVariants, showDraft: this.showDraft, showArchived: this.showArchived, showDraftBadge: this.showDraftBadge, showArchivedBadge: this.showArchivedBadge, }; return productOptions; } return options; } set(options, shouldUpdate = true) { const mergedOptions = getMergedProps(this.options, options); const { initialQuery, filterQuery, initialSelectionIds = [], showHidden = true, showVariants = true, showDraft = true, showArchived = true, showDraftBadge = false, showArchivedBadge = false, selectMultiple = true, actionVerb = ActionVerb.Add, } = mergedOptions; this.initialQuery = initialQuery; this.filterQuery = filterQuery; this.initialSelectionIds = initialSelectionIds; this.showHidden = showHidden; this.showVariants = showVariants; this.showDraft = showDraft; this.showArchived = showArchived; this.showDraftBadge = showDraftBadge; this.showArchivedBadge = showArchivedBadge; this.selectMultiple = selectMultiple; this.actionVerb = actionVerb; if (shouldUpdate) { this.update(); } return this; } dispatch(action, selection) { if (action === Action.OPEN) { this.open(); } else if (action === Action.UPDATE) { this.update(); } else if (action === Action.CLOSE || action === Action.CANCEL) { this.cancel(); } else if (action === Action.SELECT) { this.selection = selection; this.app.dispatch(select({ id: this.id, selection: this.selection })); } return this; } update() { this.app.dispatch(update(this.payload)); } open() { this.app.dispatch(open(this.payload)); } cancel() { this.app.dispatch(cancel({ id: this.id })); } close() { this.cancel(); } } export { Action, ActionVerb, CollectionSortOrder, FulfillmentServiceType, ProductStatus, ProductVariantInventoryManagement, ProductVariantInventoryPolicy, ResourcePicker, ResourceType, WeightUnit, cancel, close, open, select, update };