0.0.2 • Published 7 years ago

fetch-waterfall v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

waterfall

Javascript waterfall effection with images preloader

Installation && Usage

Browser

In a browser, you can use fetch-waterfall as follows:

<script src='./lib/waterfall.js' charset='utf8'><script>

it also can be a AMD module while using require.js

NodeJS

Install for Node.js using npm

$ npm install fetch-waterfall

Require module use require

const waterfall = require('fetch-waterfall)

API

waterfall(config)

  • config (Object) configure object

configure property details

NameTypeDefault-valueDescription
containerString | HTMLElement''container element
itemClassString'item'item's className
colsNumber1Waterfall effections columns
gutterWidthString | Number10the gaps of horizonta with pixel or percentage unit refering to container width
gutterHeightString | Number10the gaps of verticle with pixel or percentage unit refering to container width
gutterTypeStringspace-betweengaps types,space-aroundspace-between
preloadImageBooleanfalseif need images preload(default to false)
defaultImageStringundefinedthe replace image url when target image loads failly(it effects when preloadImage is true)
preloadPercentFunctionfunction (state) {}images preload function,payload state
preloadCompleteFunctionfunction (promise) {}images loaded calllback function, argument promise is a Promise Object
pathFunction | Stringundefinedrequest request url, when it is a function which return a string. it only supports the method GET request, it uses fetch API to request
maxPageStringundefinedrequest the max number of request
templateString | HTMLElement'no referering templates'the Waterfall items rendering template, as follows template
callbacksObject{ loadingStart: fn, loadingFinished: fn, loadingError: fn }request request callback function, see as follows

config.callbacks details

    callbacks: {
        /**
         *  the begining function of fetch request 
         */
        loadingStart: function () {},

        /**
         *  the end function of fetch request
         * @param {Object} res - return response
         */
        loadingFinished: function (res) {
            console.log(res)
        },

        /**
         *  fetch request error function
         * @param {Object} error - request error message
         */
        loadingError: function (error) {
            console.log(error)
        }
    }

Template

fetch-waterfall it supports a simple template rendering engine, in order to display the items in the waterfall it uses {{}} to contain variable, {{#}} contain the iterable array

template use DOM elements or string straightly.

    <script type="text/template" id="waterfall-tpl">
        {{#results}}
            <div class="item">
                <img src="{{url}}" />
                <p>name: {{name}}</p>
                <p>age: {{age}}</p>
            </div>
        {{/results}}
    </script>

a json data model

    {
        "results": [{
            "name": "Tom",
            "age": 20,
            "url": "./imgs/1.jpg"
        }, {
            "name": "Jack",
            "age": 22,
            "url": "./ims/2.jpg"
        },{
            "name": "Marry",
            "age": 16,
            "url": "./imgs/3.jpg"
        }]
    }

after rendering result, as follows:

    <div class="item">
        <img src="./imgs/1.jpg" />
        <p>name: Tom</p>
        <p>age: 20</p>
    </div>
    <div class="item">
        <img src="./ims/2.jpg" />
        <p>name: Jack</p>
        <p>age: 22</p>
    </div>
    <div class="item">
        <img src="./imgs/3.jpg" />
        <p>name: Marry</p>
        <p>age: 16</p>
    </div>

Examples

  • html structure

      <div id="box"></div>
      <script src='./lib/waterfall.js'>
  • script

      waterfall({
          container: '#box', // container
          itemClass: 'item', // item's className
          cols: 3, // columns
          gutterWidth: 10, // horizonal gutter
          gutterHeight: 20, // vertical gutter
          gutterType: 'space-around', // gutter type
          preloadImage: true, // preload images
          defaultImage: './imgs/default.jpg', // target image load error which to replace it
          preloadPercent: function (state) { // images preloading
              console.log('images has loaded ' + state.current/state.total)
              console.log(state)
          },
          preloadComplete: function (promise) { // images loaded
              promise.then(function (state) {
                  console.log(state)
                  document.getElementById('mask').style.display = 'none'
              })
          },
          path: function (curPage) { // fetch request url
              return './users.json?page=' + curPage
          },
          maxPage: 6, // request max count
          template: document.getElementById('waterfall-tpl'), // tempalte
          callbacks: {
              loadingStart: function () {
                  console.log('ajax request ...')
                  document.getElementById('mask').style.display = 'block'
              },
              loadingFinished: function (res) {
                  console.log('ajax response', res)               
              },
              loadingError: function (error) {
                  console.log(error)
              }
          }
      })

waterfall Demo

License

MIT