summaryrefslogtreecommitdiff
path: root/node_modules/@jet-app/app-store/tmp/src/common/arcade/breakouts-common.js
blob: 85d4bb4f0fcd6ab968df97a7b08ff0b5b1de1eff (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
//  breakouts-common.ts
//  AppStoreKit
//
//  Created by Jonathan Ellenbogen on 11/19/19.
//  Copyright (c) 2016 Apple Inc. All rights reserved.
//
import { isNothing } from "@jet/environment";
import * as models from "../../api/models";
import * as serverData from "../../foundation/json-parsing/server-data";
import * as mediaAttributes from "../../foundation/media/attributes";
import * as platformAttributes from "../../foundation/media/platform-attributes";
import * as mediaRelationship from "../../foundation/media/relationships";
import * as color from "../../foundation/util/color-util";
import { pageRouter } from "../builders/routing";
import * as contentAttributes from "../content/attributes";
import * as heroCommon from "../grouping/hero/hero-common";
import * as offers from "../offers/offers";
import * as productPageUtil from "../product-page/product-page-util";
import { makeProductPageIntent } from "../../api/intents/product-page-intent";
import { makeRoutableArticlePageIntent } from "../../api/intents/routable-article-page-intent";
import { getLocale } from "../locale";
import { makeRoutableArticlePageCanonicalUrl } from "../today/routable-article-page-url-utils";
import { getPlatform } from "../preview-platform";
export function detailBackgroundStyleFromData(objectGraph, breakoutData, supportsMaterial = true, isFirstShelf, isUpsell) {
    const heroVideoData = heroCommon.heroVideoFromData(objectGraph, breakoutData);
    const heroArtworkData = heroCommon.heroArtworkFromData(objectGraph, breakoutData);
    const backgroundColor = heroVideoData.backgroundColor || heroArtworkData.backgroundColor;
    const detailPosition = detailPositionFromData(objectGraph, breakoutData, isFirstShelf, isUpsell);
    const displayBreakoutMaterial = breakoutData.type === "marketing-items"
        ? serverData.asBooleanOrFalse(templateParametersFromData(objectGraph, breakoutData), "displayMaterial")
        : mediaAttributes.attributeAsBooleanOrFalse(breakoutData, "displayBreakoutMaterial");
    const shouldUseMaterialBackground = displayBreakoutMaterial || (objectGraph.client.isTV && detailPosition === "center");
    if (shouldUseMaterialBackground && supportsMaterial) {
        return "material";
    }
    else {
        return detailBackgroundStyleFromColor(objectGraph, backgroundColor);
    }
}
export function detailBackgroundStyleFromColor(objectGraph, backgroundColor) {
    if (!backgroundColor) {
        return "dark";
    }
    return color.isDarkColor(backgroundColor, 50) ? "dark" : "light";
}
export function detailPositionFromData(objectGraph, data, isFirstShelf, isUpsell) {
    // Upsells always position the details in the center.
    if (objectGraph.client.isPhone || isUpsell) {
        return "center";
    }
    if (objectGraph.client.isTV) {
        return isFirstShelf ? "center" : "leading";
    }
    const breakoutDetailsString = data.type === "marketing-items"
        ? serverData.asString(templateParametersFromData(objectGraph, data), "textPosition")
        : mediaAttributes.attributeAsString(data, "breakoutTextAlignment");
    if (isNothing(breakoutDetailsString) || breakoutDetailsString.length === 0) {
        return objectGraph.client.isMac ? "center" : "leading";
    }
    switch (breakoutDetailsString.toLowerCase()) {
        case "left":
            return "leading";
        case "center":
            return "center";
        case "right":
            return "trailing";
        default:
            return "leading";
    }
}
/**
 * The text alignment to use for a particular detail position on large breakouts
 * @param position The positioning of the details within the breakout.
 */
export function detailTextAlignmentForDetailPosition(objectGraph, position, breakoutData, isUpsell = false) {
    const isTV = objectGraph.client.isTV;
    switch (position) {
        case "leading":
            return isTV ? "center" : "leading";
        case "trailing":
            return isTV ? "center" : "leading";
        case "center":
            if (isUpsell && !isTV) {
                return "center";
            }
            else if (isTV) {
                return "leading";
            }
            else {
                return breakoutData.type === "marketing-items"
                    ? "center"
                    : textAlignmentFromData(objectGraph, breakoutData);
            }
        default:
            return "leading";
    }
}
function textAlignmentFromData(objectGraph, data) {
    var _a;
    const breakoutTextAlignmentString = (_a = mediaAttributes.attributeAsString(data, "breakoutTextAlignment")) !== null && _a !== void 0 ? _a : "";
    switch (breakoutTextAlignmentString.toLowerCase()) {
        case "left":
            return "leading";
        case "center":
            return "center";
        case "right":
            return "trailing";
        default:
            return objectGraph.client.isMac ? "center" : "leading";
    }
}
export function templateParametersFromData(objectGraph, data) {
    if (data.type !== "marketing-items") {
        return null;
    }
    return mediaAttributes.attributeAsDictionary(data, "display.templateParameters");
}
export function artworkDictionaryFromData(objectGraph, data) {
    switch (data.type) {
        case "editorial-items":
            return mediaAttributes.attributeAsDictionary(data, "editorialArtwork");
        case "marketing-items":
            return mediaAttributes.attributeAsDictionary(data, "marketingArtwork");
        default:
            const attributePlatform = contentAttributes.bestAttributePlatformFromData(objectGraph, data);
            return platformAttributes.platformAttributeAsDictionary(data, attributePlatform, "editorialArtwork");
    }
}
export function videoDictionaryFromData(objectGraph, data) {
    switch (data.type) {
        case "editorial-items":
            return mediaAttributes.attributeAsDictionary(data, "editorialVideo");
        case "marketing-items":
            return mediaAttributes.attributeAsDictionary(data, "marketingVideo");
        default:
            if (preprocessor.GAMES_TARGET) {
                return mediaAttributes.attributeAsDictionary(data, "editorialVideo");
            }
            else {
                const attributePlatform = contentAttributes.bestAttributePlatformFromData(objectGraph, data);
                return platformAttributes.platformAttributeAsDictionary(data, attributePlatform, "marketingVideo");
            }
    }
}
export function callToActionLabelFromData(objectGraph, data) {
    switch (data.type) {
        case "marketing-items":
            const offerData = offers.offerDataFromMarketingItem(objectGraph, data);
            return serverData.asString(offerData, "callToActionLabel");
        default:
            return mediaAttributes.attributeAsString(data, "breakoutCallToActionLabel");
    }
}
export function requiresPrimaryContent(objectGraph, data) {
    const linkData = mediaAttributes.attributeAsDictionary(data, "link");
    const isLinkAction = serverData.isDefinedNonNullNonEmpty(linkData);
    const isStoryAction = mediaAttributes.attributeAsBooleanOrFalse(data, "isCanvasAvailable");
    return !isLinkAction && !isStoryAction;
}
export function wantsBlur(objectGraph, backgroundStyle, isInHeroPosition) {
    return backgroundStyle !== "material" && isInHeroPosition;
}
/**
 * The action to use when displaying a breakout's call to action button
 * @param data The breakout node data from MAPI
 */
export function actionFromData(objectGraph, data) {
    const linkData = mediaAttributes.attributeAsDictionary(data, "link");
    const isLinkAction = serverData.isDefinedNonNullNonEmpty(linkData);
    const isProductAction = mediaAttributes.attributeAsString(data, "kind") === "App";
    const isStoryAction = mediaAttributes.attributeAsBooleanOrFalse(data, "isCanvasAvailable");
    const primaryContent = mediaRelationship.relationshipData(objectGraph, data, "primary-content");
    if (!isLinkAction && !isStoryAction && !mediaAttributes.hasAttributes(primaryContent)) {
        return null;
    }
    let actionUrl = null;
    if (isLinkAction) {
        actionUrl = serverData.asString(linkData, "url");
    }
    else if (isStoryAction) {
        actionUrl = mediaAttributes.attributeAsString(data, "url");
    }
    else {
        actionUrl = mediaAttributes.attributeAsString(primaryContent, "url");
    }
    if (serverData.isNull(actionUrl)) {
        return null;
    }
    let action = null;
    if (isLinkAction && serverData.asString(linkData, "target") === "external") {
        const externalUrlAction = new models.ExternalUrlAction(actionUrl);
        action = externalUrlAction;
    }
    else if (objectGraph.isAvailable(pageRouter)) {
        const flowPage = objectGraph.required(pageRouter).fetchFlowPage(actionUrl);
        const flowAction = new models.FlowAction(flowPage);
        flowAction.pageUrl = actionUrl;
        if (flowPage === "product") {
            flowAction.pageData = productPageUtil.createProductPageSidePackFromResponse(objectGraph, primaryContent);
        }
        action = flowAction;
    }
    else if (objectGraph.client.isWeb && isStoryAction) {
        const routableArticlePageIntent = makeRoutableArticlePageIntent({
            ...getLocale(objectGraph),
            ...getPlatform(objectGraph),
            id: data.id,
        });
        const flowAction = new models.FlowAction("article");
        flowAction.title = mediaAttributes.attributeAsString(data, "breakoutCallToActionLabel");
        flowAction.pageUrl = makeRoutableArticlePageCanonicalUrl(objectGraph, routableArticlePageIntent);
        flowAction.destination = routableArticlePageIntent;
        action = flowAction;
    }
    else if (objectGraph.client.isWeb && isProductAction) {
        const flowAction = new models.FlowAction("product");
        flowAction.title = mediaAttributes.attributeAsString(data, "breakoutCallToActionLabel");
        flowAction.pageUrl = actionUrl;
        flowAction.destination = makeProductPageIntent({
            ...getLocale(objectGraph),
            ...getPlatform(objectGraph),
            id: primaryContent.id,
        });
        action = flowAction;
    }
    if (action) {
        action.title = callToActionLabelFromData(objectGraph, data);
    }
    return action;
}
export function titleEffectFromArtwork(objectGraph, editorialArtworkData, isUpsell = false) {
    if (serverData.isNullOrEmpty(editorialArtworkData)) {
        return null;
    }
    let titleEffect = null;
    const white = color.named("white");
    const black = color.named("black");
    const backgroundColor = color.fromHex(serverData.asString(editorialArtworkData, "bgColor"));
    const gradientColors = serverData
        .asArrayOrEmpty(editorialArtworkData, "textGradient")
        .map((hexColor) => {
        return color.fromHex(hexColor);
    });
    const backgroundStyle = detailBackgroundStyleFromColor(objectGraph, backgroundColor);
    const hasGradient = gradientColors.length === 2;
    if (hasGradient) {
        titleEffect = new models.TitleEffect("horizontalGradient");
        const startColor = gradientColors[0];
        const endColor = gradientColors[1];
        titleEffect.gradientStartColor = color.dynamicWith(startColor, startColor);
        titleEffect.gradientEndColor = color.dynamicWith(endColor, endColor);
        titleEffect.filter = "plusLight";
        if (!isUpsell) {
            titleEffect.accessoryColor = titleEffect.gradientEndColor;
        }
    }
    else {
        titleEffect = new models.TitleEffect("color");
        titleEffect.color = backgroundStyle === "dark" ? white : black;
        if (!isUpsell) {
            titleEffect.accessoryColor = titleEffect.color;
        }
        titleEffect.isFallbackStyle = true;
    }
    // The accessory on tvOS should always be white resulting in a dark accessory.
    if (objectGraph.client.isTV) {
        titleEffect.accessoryColor = white;
    }
    return titleEffect;
}
//# sourceMappingURL=breakouts-common.js.map