|
| 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