Skip to content

Commit 271834d

Browse files
Merge pull request guardian#24090 from guardian/revert-24071-clj/convert-create-slot-to-ts
Revert "Convert `create-slot` to TypeScript"
2 parents 7e95c26 + dd5038f commit 271834d

File tree

5 files changed

+291
-376
lines changed

5 files changed

+291
-376
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const getAdSize = (width, height) => {
2+
const toString = () =>
3+
width === height && height === 0 ? 'fluid' : `${width},${height}`;
4+
5+
return Object.freeze({
6+
width,
7+
height,
8+
toString,
9+
});
10+
};
11+
12+
/*
13+
14+
mark: 1b109a4a-791c-4214-acd2-2720d7d9f96f
15+
16+
The ad sizes which are hardcoded here are also hardcoded in the source code of
17+
dotcom-rendering.
18+
19+
If/when this file is modified, please make sure that updates, if any, are reported to DCR.
20+
21+
*/
22+
23+
const adSizes = {
24+
// standard ad sizes
25+
billboard: getAdSize(970, 250),
26+
leaderboard: getAdSize(728, 90),
27+
mpu: getAdSize(300, 250),
28+
halfPage: getAdSize(300, 600),
29+
portrait: getAdSize(300, 1050),
30+
skyscraper: getAdSize(160, 600),
31+
mobilesticky: getAdSize(320, 50),
32+
33+
// dfp proprietary ad sizes
34+
fluid: getAdSize(0, 0),
35+
outOfPage: getAdSize(1, 1),
36+
googleCard: getAdSize(300, 274),
37+
38+
// guardian proprietary ad sizes
39+
video: getAdSize(620, 1),
40+
outstreamDesktop: getAdSize(620, 350),
41+
outstreamGoogleDesktop: getAdSize(550, 310),
42+
outstreamMobile: getAdSize(300, 197),
43+
merchandisingHighAdFeature: getAdSize(88, 89),
44+
merchandisingHigh: getAdSize(88, 87),
45+
merchandising: getAdSize(88, 88),
46+
inlineMerchandising: getAdSize(88, 85),
47+
fabric: getAdSize(88, 71),
48+
empty: getAdSize(2, 2),
49+
};
50+
51+
adSizes['970x250'] = adSizes.billboard;
52+
adSizes['728x90'] = adSizes.leaderboard;
53+
adSizes['300x250'] = adSizes.mpu;
54+
adSizes['300x600'] = adSizes.halfPage;
55+
adSizes['300x1050'] = adSizes.portrait;
56+
adSizes['160x600'] = adSizes.skyscraper;
57+
58+
export { adSizes };

static/src/javascripts/projects/commercial/modules/ad-sizes.ts

-91
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
import config from '../../../../lib/config';
2+
import { adSizes } from '../ad-sizes';
3+
4+
const inlineDefinition = {
5+
sizeMappings: {
6+
mobile: [
7+
adSizes.outOfPage,
8+
adSizes.empty,
9+
adSizes.outstreamMobile,
10+
adSizes.mpu,
11+
adSizes.googleCard,
12+
adSizes.fluid,
13+
],
14+
phablet: [
15+
adSizes.outOfPage,
16+
adSizes.empty,
17+
adSizes.outstreamMobile,
18+
adSizes.mpu,
19+
adSizes.googleCard,
20+
adSizes.outstreamDesktop,
21+
adSizes.outstreamGoogleDesktop,
22+
adSizes.fluid,
23+
],
24+
desktop: [
25+
adSizes.outOfPage,
26+
adSizes.empty,
27+
adSizes.mpu,
28+
adSizes.googleCard,
29+
adSizes.video,
30+
adSizes.outstreamDesktop,
31+
adSizes.outstreamGoogleDesktop,
32+
adSizes.fluid,
33+
],
34+
},
35+
};
36+
37+
/*
38+
39+
mark: 432b3a46-90c1-4573-90d3-2400b51af8d0
40+
41+
The ad sizes which are hardcoded here are also hardcoded in the source code of
42+
dotcom-rendering.
43+
44+
If/when this file is modified, please make sure that updates, if any, are reported to DCR.
45+
46+
*/
47+
48+
const adSlotDefinitions = {
49+
im: {
50+
label: false,
51+
refresh: false,
52+
sizeMappings: {
53+
mobile: [
54+
adSizes.outOfPage,
55+
adSizes.empty,
56+
adSizes.inlineMerchandising,
57+
adSizes.fluid,
58+
],
59+
},
60+
},
61+
'high-merch': {
62+
label: false,
63+
refresh: false,
64+
name: 'merchandising-high',
65+
sizeMappings: {
66+
mobile: [
67+
adSizes.outOfPage,
68+
adSizes.empty,
69+
adSizes.merchandisingHigh,
70+
adSizes.fluid,
71+
],
72+
},
73+
},
74+
'high-merch-lucky': {
75+
label: false,
76+
refresh: false,
77+
name: 'merchandising-high-lucky',
78+
sizeMappings: {
79+
mobile: [adSizes.outOfPage, adSizes.empty, adSizes.fluid],
80+
},
81+
},
82+
'high-merch-paid': {
83+
label: false,
84+
refresh: false,
85+
name: 'merchandising-high',
86+
sizeMappings: {
87+
mobile: [
88+
adSizes.outOfPage,
89+
adSizes.empty,
90+
adSizes.merchandisingHighAdFeature,
91+
adSizes.fluid,
92+
],
93+
},
94+
},
95+
inline: inlineDefinition,
96+
mostpop: inlineDefinition,
97+
comments: inlineDefinition,
98+
'top-above-nav': {
99+
sizeMappings: {
100+
mobile: [
101+
adSizes.outOfPage,
102+
adSizes.empty,
103+
adSizes.fabric,
104+
adSizes.outstreamMobile,
105+
adSizes.mpu,
106+
adSizes.fluid,
107+
],
108+
},
109+
},
110+
carrot: {
111+
label: false,
112+
refresh: false,
113+
name: 'carrot',
114+
sizeMappings: {
115+
mobile: [adSizes.fluid],
116+
},
117+
},
118+
epic: {
119+
label: false,
120+
refresh: false,
121+
name: 'epic',
122+
sizeMappings: {
123+
mobile: [adSizes.fluid],
124+
},
125+
},
126+
'mobile-sticky': {
127+
label: true,
128+
refresh: true,
129+
name: 'mobile-sticky',
130+
sizeMappings: {
131+
mobile: [adSizes.mobilesticky],
132+
},
133+
},
134+
};
135+
136+
/*
137+
Returns an array of adSlot HTMLElement(s) with always at least one HTMLDivElement
138+
which is the main DFP slot.
139+
140+
Insert those elements as siblings at the place
141+
you want adverts to appear.
142+
143+
Note that for the DFP slot to be filled by GTP, you'll have to
144+
use addSlot from add-slot.js
145+
*/
146+
147+
const createAdSlotElements = (
148+
name,
149+
attrs,
150+
classes
151+
) => {
152+
const adSlots = [];
153+
154+
const id = `dfp-ad--${name}`;
155+
156+
// 3562dc07-78e9-4507-b922-78b979d4c5cb
157+
if (config.get('isDotcomRendering', false) && name === 'top-above-nav') {
158+
// This is to prevent a problem that appeared with DCR.
159+
// We are simply making sure that if we are about to
160+
// introduce dfp-ad--top-above-nav then there isn't already one.
161+
const node = document.getElementById(id);
162+
if (node && node.parentNode) {
163+
const pnode = node.parentNode;
164+
console.log(`warning: cleaning up dom node id: dfp-ad--${name}`);
165+
pnode.removeChild(node);
166+
}
167+
}
168+
169+
// The 'main' adSlot
170+
const adSlot = document.createElement('div');
171+
adSlot.id = id;
172+
adSlot.className = `js-ad-slot ad-slot ${classes.join(' ')}`;
173+
adSlot.setAttribute('data-link-name', `ad slot ${name}`);
174+
adSlot.setAttribute('data-name', name);
175+
adSlot.setAttribute('aria-hidden', 'true');
176+
Object.keys(attrs).forEach(attr => {
177+
adSlot.setAttribute(attr, attrs[attr]);
178+
});
179+
180+
adSlots.push(adSlot);
181+
182+
return adSlots;
183+
};
184+
185+
export const createSlots = (type, options = {}) => {
186+
const attributes = {};
187+
const definition = adSlotDefinitions[type];
188+
const slotName = options.name || definition.name || type;
189+
const classes = options.classes
190+
? options.classes.split(' ').map(cn => `ad-slot--${cn}`)
191+
: [];
192+
193+
const sizes = Object.assign({}, definition.sizeMappings);
194+
195+
if (options.sizes) {
196+
Object.keys(options.sizes).forEach(size => {
197+
if (sizes[size]) {
198+
sizes[size] = sizes[size].concat(options.sizes[size]);
199+
} else {
200+
sizes[size] = options.sizes[size];
201+
}
202+
});
203+
}
204+
205+
Object.keys(sizes).forEach(size => {
206+
sizes[size] = sizes[size].join('|');
207+
});
208+
209+
Object.assign(attributes, sizes);
210+
211+
if (definition.label === false) {
212+
attributes.label = 'false';
213+
}
214+
215+
if (definition.refresh === false) {
216+
attributes.refresh = 'false';
217+
}
218+
219+
classes.push(`ad-slot--${slotName}`);
220+
221+
return createAdSlotElements(
222+
slotName,
223+
Object.keys(attributes).reduce(
224+
(result, key) =>
225+
Object.assign({}, result, { [`data-${key}`]: attributes[key] }),
226+
{}
227+
),
228+
classes
229+
);
230+
};

0 commit comments

Comments
 (0)