GithubHelp home page GithubHelp logo

a6brgeuka / ember-img-manager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from huafu/ember-img-manager

0.0 2.0 0.0 355 KB

An Ember addon to handle image load/fail/fallbacks/batches/delays/...

Home Page: http://huafu.github.io/#/ember?name=ember-img-manager

License: MIT License

JavaScript 95.70% HTML 4.25% CSS 0.06%

ember-img-manager's Introduction

ember-img-manager Build Status

This Ember addon lets you control how images are loaded (batch size, delay, ...), setup events for success or error, re-use DOM img nodes by cloning them so that external images with no cache are cached, and use different image sources while an image is loading or becomes error.

The batchSize is particularly helpful when dealing with Google contacts library for example, where Google limits each client to 10 requests per second for contact images.

You can also use this addon to avoid loading any external images on dev environment for example if you are used to work disconnected from internet.

By default images are lazy loaded, which means that if they have not yet been loaded, the load will trigger only when the image will appear in the viewport.

Example configuration

// config/environment.js

// all settings are optionals
ENV.imgManager = {
  // how many times to try to load an image (default: 1)
  maxTries: 3,
  // wait 10 milliseconds before trying to load more images (default: 1)
  delay: 10,
  // how many images to try to load in a raw (if 0 then it'll load all at once) (default: 0)
  batchSize: 0,
  // should we start loading a source image only when it appear in the viewport (default: true)
  lazyLoad: false,
  // the image to use while loading the real image (default: null)
  loadingSrc: 'assets/loading-img.png',
  // the image to use when an image has failed to load (default: null)
  errorSrc: 'assets/error-img.png',
  // in the `rules`, you can define specific settings for each image depending on its `src` (default: null)
  rules: [
    // for use with google contacts photos for example:
    {match: 'www.google.com/m8/feeds/photos', delay: 1000, batchSize: 10},
    // do not try to load any external image (for dev env for example):
    {match: /^https?:\/\//, maxTries: 0}
  ],
  // ------ global only settings (show with their default values) -----
  // css class to use when loading an image
  loadingClass: 'loading',
  // css class to use when the load was successful
  successClass: 'success',
  // css class to use when the load has failed
  errorClass: 'error'
};

Installation

  • npm install --save-dev ember-img-manager
  • or, with the latest ember-cli, ember install:addon ember-img-manager

Usage:

  • Simply replace any <img ...> tag that you want to be handled by the manager with the equivalent {{img-wrap ...}} handlebars tag. All HTML attributes are supported.

    {{!-- old tag was <img {{bind-attr src=photoUrl}} alt="Landscape"> --}}
    {{img-wrap src=photoUrl alt="Landscape"}}

    There are a few properties you can use:

    • loadingClass, successClass, errorClass: the css classes to use for each status (using the default ones from config if they are not set)
    • isLoading, isSuccess, isError: whether the image is in one of those status
    • progress: the value representing the percentage of the image which has been loaded already
    • load-error: name of an Ember action to trigger when the image fails to load
    • load-success: name of an Ember action to trigger when the image loads successfully
    • lazyLoad: whether to directly load the image or wait until it appears in the viewport (will override anything set in the rules)
  • One thing to note is that the <img> is then wrapped inside a <span> which has display: inline-block. It should not break your design in most of the cases.

  • This <span> tag has then a img-wrap class, plus a loading, error or success class depending on the state of the load.

  • Any other class defined in the hash of the handlebars tag is defined on the <span> element instead of the <img> itself.

Author

Huafu Gandon @huafu_g - my website


For more information on using ember-cli, visit http://www.ember-cli.com/.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.