Skip to content

Commit

Permalink
Added attributes width and height for images if a dimensions are …
Browse files Browse the repository at this point in the history
…returned by the API
  • Loading branch information
tg666 committed Apr 4, 2024
1 parent d1eb427 commit c8d78f9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `embed` mode for banners rendering. Embed banners are rendered in iframes and can be enabled via data attribute `data-amp-mode="embed"`.
- Added a new JS client for use in iframes. It is not intended for direct use on a website.
- Added the property `AMPClientFactory.version` that returns the package version in semver format.
- Added attributes `width` and `height` for images if a dimensions are returned by the API.

### Changed
- The package has been refactored from CommonJS to ESM.
Expand Down
2 changes: 1 addition & 1 deletion src/banner/embed/embed-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class EmbedBanner extends Banner {
}

updatePositionData(data) {
const props = ['id', 'name', 'rotationSeconds', 'displayType', 'breakpointType'];
const props = ['id', 'name', 'rotationSeconds', 'displayType', 'breakpointType', 'dimensions'];
const positionData = this.positionData;

for (let prop of props) {
Expand Down
1 change: 1 addition & 0 deletions src/banner/managed/managed-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class ManagedBanner extends Banner {
rotationSeconds: responseData['rotation_seconds'],
displayType: responseData['display_type'],
breakpointType: responseData['breakpoint_type'],
dimensions: responseData['dimensions'] || { width: null, height: null },
});

const banners = [];
Expand Down
12 changes: 11 additions & 1 deletion src/banner/position-data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ export class PositionData {
* @param {int} rotationSeconds
* @param {string|null} displayType
* @param {string|null} breakpointType
* @param {{width: int|null, height: int|null}} dimensions
*/
constructor({ id, code, name, rotationSeconds, displayType, breakpointType }) {
constructor({ id, code, name, rotationSeconds, displayType, breakpointType, dimensions }) {
this.id = id;
this.code = code;
this.name = name;
this.rotationSeconds = rotationSeconds;
this.displayType = displayType;
this.breakpointType = breakpointType;
this.dimensions = {
width: dimensions.width || null,
height: dimensions.height || null,
}
}

static createInitial(code) {
Expand All @@ -24,6 +29,10 @@ export class PositionData {
rotationSeconds: 0,
displayType: null,
breakpointType: null,
dimensions: {
width: null,
height: null,
},
});
}

Expand All @@ -47,6 +56,7 @@ export class PositionData {
rotationSeconds: this.rotationSeconds,
displayType: this.displayType,
breakpointType: this.breakpointType,
dimensions: this.dimensions,
}
}
}
2 changes: 2 additions & 0 deletions src/template/multiple.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default `
src="<%= b.content.src %>"
sizes="<%- b.content.sizes %>"
alt="<%- b.content.alt %>"
<% if(null !== banner.positionData.dimensions.width) { %>width="<%- banner.positionData.dimensions.width %>"<% } %>
<% if(null !== banner.positionData.dimensions.height) { %>height="<%- banner.positionData.dimensions.height %>"<% } %>
<% if('' !== b.content.title) { %>title="<%- b.content.title %>"<% } %>
<% if(banner.options.has('loading') && index >= parseInt(banner.options.get('loading-offset', 0))) { %>loading="<%- banner.options.get('loading') %>"<% } %>>
</picture>
Expand Down
2 changes: 2 additions & 0 deletions src/template/random.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default `
src="<%= data.content.src %>"
sizes="<%- data.content.sizes %>"
alt="<%- data.content.alt %>"
<% if(null !== banner.positionData.dimensions.width) { %>width="<%- banner.positionData.dimensions.width %>"<% } %>
<% if(null !== banner.positionData.dimensions.height) { %>height="<%- banner.positionData.dimensions.height %>"<% } %>
<% if('' !== data.content.title) { %>title="<%- data.content.title %>"<% } %>
<% if(banner.options.has('loading')) { %>loading="<%- banner.options.get('loading') %>"<% } %>>
</picture>
Expand Down
2 changes: 2 additions & 0 deletions src/template/single.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default `
src="<%= data.content.src %>"
sizes="<%- data.content.sizes %>"
alt="<%- data.content.alt %>"
<% if(null !== banner.positionData.dimensions.width) { %>width="<%- banner.positionData.dimensions.width %>"<% } %>
<% if(null !== banner.positionData.dimensions.height) { %>height="<%- banner.positionData.dimensions.height %>"<% } %>
<% if('' !== data.content.title) { %>title="<%- data.content.title %>"<% } %>
<% if(banner.options.has('loading')) { %>loading="<%- banner.options.get('loading') %>"<% } %>>
</picture>
Expand Down

0 comments on commit c8d78f9

Please sign in to comment.