3.9.0 • Published 3 years ago

ars-arsenal v3.9.0

Weekly downloads
19
License
MIT
Repository
github
Last release
3 years ago

Ars Arsenal

CircleCI

A gallery picker. ArsArsenal makes it easy to quickly select photos and other resources for content management purposes. Additionally, it supports features such as:

  • Table/Gallery view
  • Pagination
  • Sorting
  • Search

Heads up! we recently made some breaking changes to configuration in version 3.0.0. See the CHANGELOG for more information.

Example

Installation

npm install --save ars-arsenal
# or use yarn
yarn add ars-arsenal

Styles

Ars Arsenal ships with a stylesheet. The easiest way to include it is by importing it from the node_modules folder:

/* Sass stylesheet: */
@import './node_modules/ars-arsenal/style/ars-arsenal.scss'; /* or CSS: */
@import './node_modules/ars-arsenal/style.css';

Usage

ArsArsenal can be rendered either as a stand-alone instance or as a React component:

Stand Alone

import ArsArsenal from 'ars-arsenal'

let app = document.getElementById('app')

ArsArsenal.render(app, {
  autoComplete: true, // Show or hide autocomplete results

  resource: 'photo', // the noun used for selection, i.e. "Pick a photo"

  // Configure the root element's HTML attributes. default = {}
  rootAttributes: {
    className: 'my-custom-class another-custom-class',
    'data-test': 'my-integration-selector-helper'
  },

  // The base URL for API interaction
  url: 'photo/resource/endpoint',

  // How to display the items. Can be "table" or "gallery"
  mode: 'gallery',

  // What table columns to display, and in what order
  columns: ['id', 'name', 'caption', 'attribution', 'preview'],

  multiselect: false,

  listUrl: function(url) {
    // Used to build the URL that fetches lists of records.
    return url
  },

  listQuery: function({ search, page, sort }) {
    // Use this function to rename query parameters before building
    // the listUrl URL
    //
    // Any data returned from this function will be stringified into
    // query parameters
    return { search, page, sort }
  },

  showUrl: function(url, id: ID) {
    // Used to build the URL that fetches a single record
    return `${url}/${id}`
  },

  onError: function(response) {
    // format errors before they are sent as a "string" value
    // to the component
    return response.code + ': ' + response.message
  },

  onFetch: function(response) {
    // format the response, useful if you do not control the JSON
    // response from your endpoint
    return response.data
  },

  onChange: function(id) {
    // Whenever a new item is picked, this event is triggered
    console.log('The value was changed to %s', id)
  },

  request: function(url, callback) {
    // Behavior to configure networking. Return an XMLHTTPRequest
    return xhr(url, callback)
  },

  logger: function(level, message) {
    // Override this method to handle usage warnings and issues
    // ArsArsenal considers errors with API interaction. Useful
    // for monitoring.
    switch (level) {
      case 'warning':
        console.warn(message)
        break
      case 'error':
        console.error(message)
        break
      default:
        console.log(message)
        break
    }
  }
})

React

import React from 'react'
import ReactDOM from 'react-dom'
import { Ars } from 'ars-arsenal'

let app = document.getElementById('app')

let options = {
  /* same options as above */
}

ReactDOM.render(<Ars options={options} />, app)

Response format

APIs return different shapes of data. To account for this, ArsArsenal exposes the onFetch option. This option is called whenever data is fetched from your API:

let options = {
  onFetch: function(response) {
    // format the response, useful if you do not control the JSON
    // response from your endpoint
    return response.data
  }
}

ArsArsenal expects the following data format:

[
  {
    "id": 1,
    "attribution": "League of Legends",
    "name": "Alistar",
    "caption": "Lorem ipsum dolor sit amet",
    "url": "images/alistar.jpg",
    "tags": ["blue", "cunning"]
  }
  //...
]

To transpose data, map over it in onFetch like so:

let options = {
  onFetch: function(response) {
    return response.data.map(function(record) {
      return {
        id: record.id,
        attribution: record.credit,
        name: record.title,
        caption: record.caption,
        url: record.imageSrc,
        tags: record.tags
      }
    })
  }
}

Sorting

To enable sorting, take advantage of the sort field passed into the listQuery option. listQuery will automatically stringify the returned object:

function listQuery({ page, search, sort }) {
  // Assuming your API requires a call like:
  // /photos?page=1&q=Dogs&sortKey=breed
  return {
    page: page,
    q: search,
    sortKey: sort
  }
}

Contributing

Take a look at our contributing guide, but the gist of it is:

# Install dependencies
yarn install
# Spin up the example server with:
yarn start

Visit code.viget.com to see more projects from Viget.

3.9.0

3 years ago

3.8.0

3 years ago

3.7.0

6 years ago

3.6.0

6 years ago

3.5.1

6 years ago

3.5.0

6 years ago

3.4.0

6 years ago

3.3.1

6 years ago

3.3.1-rc0

6 years ago

3.3.0

6 years ago

3.3.0-rc.2

6 years ago

3.3.0-rc.1

6 years ago

3.3.0-rc.0

6 years ago

3.2.4

7 years ago

3.2.3

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.1.0-alpha.0

7 years ago

3.0.0

7 years ago

3.0.0-rc.3

7 years ago

3.0.0-rc.2

7 years ago

3.0.0-rc.1

7 years ago

3.0.0-rc.0

7 years ago

2.5.1

7 years ago

2.5.0

8 years ago

2.4.5

8 years ago

2.4.4

8 years ago

2.4.3

8 years ago

2.4.2

8 years ago

2.4.1

8 years ago

2.4.0

8 years ago

2.3.0

8 years ago

2.2.0

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.0.0

10 years ago

0.4.3-rc3

10 years ago

0.4.3-rc2

10 years ago

0.4.3-rc1

10 years ago

0.4.2

10 years ago

0.4.2-rc2

10 years ago

0.4.2-rc

10 years ago

0.4.1

10 years ago

0.4.0-rc2

10 years ago

0.4.0-rc

10 years ago

0.4.0-beta

10 years ago

0.4.0-alpha

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

11 years ago