Skip to content

Commit 482f6b5

Browse files
jamesatfishmadoBaker
authored andcommitted
feat(img-loader): pass attributes to img element (#111)
* Passthrough of class for image using imgAttributes * Passthrough of class attribute for _useImg * Build for deployment * Pass through attribute dictionary for _useImg * Attribute object definition * Updated include statement in index.ts, Readme documentation. * Updated include statements * Reverted .gitignore * Remove dist files from git repo * Return newline characters to html and css files * Reverting changes to html and css files * Revert tslint changes * @ihadeed requested changes
1 parent a12a61c commit 482f6b5

File tree

6 files changed

+92
-34
lines changed

6 files changed

+92
-34
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ The `<img-loader>` component takes many attributes that allows you to customize
110110
| backgroundSize | string | Sets the `background-size` CSS property of the `<img-loader>` element. This is ignored if `useImg` is set to `true`. | contain |
111111
| backgroundRepeat | string | Sets the `background-repeat` CSS property of the `<img-loader>` element. This is ignored if `useImg` is set to `true`. | no-repeat |
112112
| fallbackAsPlaceholder | boolean | Use fallback as a placeholder until the image loads. | false |
113+
| imgAttributes | ImageAttribute[] | An array of ImageAttribute objects (element, value). If `useImg == true`, this array will be iterated and each object added as an attribute to the generated `<img>` HTML element. | []] |
113114
114115
115116
**Note:** The default values can be changed using the [global configuration](https://github.com/zyramedia/ionic-image-loader#global-configuration) feature.
@@ -337,6 +338,36 @@ class MyComponent {
337338
338339
```
339340
341+
# Passing HTML / CSS Attributes to a generated image
342+
343+
When using ImageLoader to generate an `<img>` element it may be desirable for the generated element to include additional attributes to provide styling or interaction qualities. The optional `imgAttributes` value can be used to provide such additional attributes which will be included in the generated `<img>` element in the DOM.
344+
345+
Usage:
346+
347+
1. Include the ImageAttribute model in your .ts
348+
```typescript
349+
import { ImageAttribute } from 'ionic-image-loader'
350+
```
351+
352+
2. Generate an array of ImageAttribute objects
353+
```typescript
354+
const imageAttributes: ImageAttribute[] = [];
355+
imageAttributes.push({
356+
element: 'class',
357+
value: 'circle-photo'
358+
})
359+
```
360+
361+
3. Include the `imgAttributes` element in the `img-loader` element of your HTML
362+
```html
363+
<img-loader [src]="..." useImg [imgAttributes]="imageAttributes"> </img-loader>
364+
```
365+
366+
4. The generated `<img>` tag will be rendered with the specified attributes
367+
```html
368+
<img src="..." class="circle-photo">
369+
```
370+
340371
<br><br>
341372
## Contribution
342373
- **Having an issue**? or looking for support? [Open an issue](https://github.com/zyra/ionic-image-loader/issues/new) and we will get you the help you need.

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/image-attribute.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ImageAttribute {
2+
element: string;
3+
value: string;
4+
}

src/components/img-loader.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, Renderer } from '@angular/core';
22
import { ImageLoader } from '../providers/image-loader';
33
import { ImageLoaderConfig } from '../providers/image-loader-config';
4+
import { ImageAttribute } from './image-attribute';
45

56
const propMap: any = {
67
display: 'display',
@@ -30,11 +31,50 @@ export class ImgLoader implements OnInit {
3031
* Whether to show the fallback image instead of a spinner while the image loads
3132
*/
3233
@Input() fallbackAsPlaceholder: boolean = this._config.fallbackAsPlaceholder;
34+
35+
/**
36+
* Use <img> tag
37+
*/
38+
@Input()
39+
set useImg(val: boolean) {
40+
this._useImg = val !== false;
41+
}
42+
43+
private _useImg: boolean = this._config.useImg;
44+
45+
46+
/**
47+
* Attributes to pass through to img tag if _useImg == true
48+
*/
49+
@Input('imgAttributes') imgAttributes: ImageAttribute[] = [];
50+
51+
/**
52+
* Convenience attribute to disable caching
53+
* @param val
54+
*/
55+
@Input()
56+
set noCache(val: boolean) {
57+
this.cache = val !== false;
58+
}
3359
/**
3460
* Enable/Disable caching
3561
* @type {boolean}
3662
*/
3763
@Input() cache: boolean = true;
64+
/**
65+
* The URL of the image to load.
66+
*/
67+
@Input()
68+
set src(imageUrl: string) {
69+
this._src = this.processImageUrl(imageUrl);
70+
this.updateImage(this._src);
71+
};
72+
73+
get src(): string {
74+
return this._src;
75+
}
76+
77+
private _src: string;
3878
/**
3979
* Width of the image. This will be ignored if using useImg.
4080
*/
@@ -83,40 +123,6 @@ export class ImgLoader implements OnInit {
83123
) {
84124
}
85125

86-
private _src: string;
87-
88-
get src(): string {
89-
return this._src;
90-
}
91-
92-
/**
93-
* The URL of the image to load.
94-
*/
95-
@Input()
96-
set src(imageUrl: string) {
97-
this._src = this.processImageUrl(imageUrl);
98-
this.updateImage(this._src);
99-
};
100-
101-
private _useImg: boolean = this._config.useImg;
102-
103-
/**
104-
* Use <img> tag
105-
*/
106-
@Input()
107-
set useImg(val: boolean) {
108-
this._useImg = val !== false;
109-
}
110-
111-
/**
112-
* Convenience attribute to disable caching
113-
* @param val
114-
*/
115-
@Input()
116-
set noCache(val: boolean) {
117-
this.cache = val !== false;
118-
}
119-
120126
ngOnInit(): void {
121127
if (this.fallbackAsPlaceholder && this.fallbackUrl) {
122128
this.setImage(this.fallbackUrl, false);
@@ -185,6 +191,10 @@ export class ImgLoader implements OnInit {
185191
// set it's src
186192
this._renderer.setElementAttribute(this.element, 'src', imageUrl);
187193

194+
// if imgAttributes are defined, add them to our img element
195+
this.imgAttributes.forEach((attribute) => {
196+
this._renderer.setElementAttribute(this.element, attribute.element, attribute.value);
197+
});
188198

189199
if (this.fallbackUrl && !this._imageLoader.nativeAvailable) {
190200
this._renderer.listen(this.element, 'error', () => this._renderer.setElementAttribute(this.element, 'src', this.fallbackUrl));

src/image-loader.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ModuleWithProviders, NgModule } from '@angular/core';
22
import { ImgLoader } from './components/img-loader';
33
import { ImageLoader } from './providers/image-loader';
4+
import { ImageAttribute } from './components/image-attribute';
45
import { ImageLoaderConfig } from './providers/image-loader-config';
56
import { IonicModule } from 'ionic-angular';
67
import { File } from '@ionic-native/file';

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './image-loader.module';
22
export * from './components/img-loader';
33
export * from './providers/image-loader-config';
4+
export * from './components/image-attribute';
45
export * from './providers/image-loader';

0 commit comments

Comments
 (0)