An open API service indexing awesome lists of open source software.

https://github.com/wbotelhos/active_storage_preview

:camera: Simple Active Storage script with Direct Upload and Image Preview
https://github.com/wbotelhos/active_storage_preview

active-storage activestorage direct-upload preview

Last synced: 8 months ago
JSON representation

:camera: Simple Active Storage script with Direct Upload and Image Preview

Awesome Lists containing this project

README

          

# Active Storage Preview

[![CI](https://github.com/wbotelhos/active_storage_preview/workflows/CI/badge.svg)](https://github.com/wbotelhos/active_storage_preview/actions)
[![NPM Version](https://badge.fury.io/js/active_storage_preview.svg)](https://badge.fury.io/js/active_storage_preview)
[![Maintainability](https://codeclimate.com/github/wbotelhos/active_storage_preview.png)](https://codeclimate.com/github/wbotelhos/active_storage_preview)
[![Sponsor](https://img.shields.io/badge/sponsor-%3C3-green)](https://www.patreon.com/wbotelhos)

Simple Active Storage script with Direct Upload and Image Preview

## Options

|Attribute |Default |Description |
|--------------|-----------|-------------------------------------------------|
|`attribute` |`'src'` |The attribute that will receive the image preview|
|`form` |`undefined`|The form that contain the file uplod field |
|`target` |`undefined`|The target for the image(s) |
|`template` |`undefined`|Callback used to build the preview element |
|`uploadButton`|`undefined`|The button to trigger the upload file selection |
|`uploadField` |`undefined`|The file field |

## Usage

### Single Upload

#### With no image on page

Appends the template on target.

```html

Select Image

```

```js
new ActiveStoragePreview({
form: document.querySelector('[data-form]'),
target: document.querySelector('[data-target]'),
template: function(src, _id, _file) { return '' },
uploadButton: document.querySelector('[data-upload-button]'),
uploadField: document.querySelector('[data-upload-field]'),
}).create();
```

#### With image on page

Change the `src` attribute of image ignoring the template content.

```html



Select Image

```

```js
new ActiveStoragePreview({
form: document.querySelector('[data-form]'),
target: document.querySelector('[data-target]'),
template: function(src, _id, _file) { return '' },
uploadButton: document.querySelector('[data-upload-button]'),
uploadField: document.querySelector('[data-upload-field]'),
}).create();
```

#### When image is on background

Ignores template and changes the property `background-image` of the `style` tag of the target. To enable it, you must set `attribute: 'style'.

```html

Select Image

```

```js
new ActiveStoragePreview({
attribute: 'style',
form: document.querySelector('[data-form]'),
target: document.querySelector('[data-target]'),
uploadButton: document.querySelector('[data-upload-button]'),
uploadField: document.querySelector('[data-upload-field]'),
}).create();
```

### Multiple Uploads

When using multiple images, sets the target on a wrapper. Images will be appended. Do not forget the `multiple` attribute on field. :)

```html

Select Images

```

```js
new ActiveStoragePreview({
form: document.querySelector('[data-form]'),
target: document.querySelector('[data-target]'),
template: function(src, _id, _file) { return '' },
uploadButton: document.querySelector('[data-upload-button]'),
uploadField: document.querySelector('[data-upload-field]'),
}).create();
```