all files / lib/models/ CYImage.js

97.14% Statements 68/70
100% Branches 30/30
100% Functions 16/16
92.86% Lines 26/28
2 statements, 2 functions, 6 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85              28×   28× 28×   28× 28×   28×         28×       28× 28×   28× 28×       56×       28×       28× 28×   28×               28×               28×       28× 28×   28×       28×       28×       28×          
'use strict';
 
import * as _ from 'lodash';
import * as p from 'path';
import { computeSelector } from '../helpers/utils.js';
 
class CYImage {
  constructor(filename, dir, sprite) {
    this.sprite = sprite;
 
    this.path = p.resolve(dir, filename);
    this.file = p.parse(this.path);
 
    this._inputDir = dir;
    this.name = this.file.name;
 
    this._position = {
      x: 0,
      y: 0
    };
 
    this._size = {
      width: 0,
      height: 0
    };
    this._cssPosition = '';
    this._cssBackgroundSize = '';
 
    let _selector = computeSelector(this.file.name);
    this.selector = `${sprite.selector}-${_selector}`;
  }
 
  get size() {
    return this._size;
  }
 
  set size(size) {
    this._size = size;
 
    let {width, height} = size;
 
    width = this.sprite.isRetina ? width / this.sprite.ratio : width;
    height = this.sprite.isRetina ? height / this.sprite.ratio : height;
 
    this.cssBackgroundSize = `${width + 'px'} ${height + 'px'}`;
  }
 
  get cssBackgroundSize() {
    return this._cssBackgroundSize;
  }
 
  set cssBackgroundSize(size) {
    this._cssBackgroundSize = size;
  }
 
  get position() {
    return this._position;
  }
 
  set position(position) {
    this._position = position;
 
    let {x, y} = position;
 
    x = (this.sprite.isRetina ? x / this.sprite.ratio : x) * -1;
    y = (this.sprite.isRetina ? y / this.sprite.ratio : y) * -1;
 
    this.cssPosition = `${x ? x + 'px' : x} ${y ? y + 'px' : y}`;
  }
 
  get cssPosition() {
    return this._cssPosition;
  }
 
  set cssPosition(position) {
    this._cssPosition = position;
  }
 
  static Factory(name, path, sprite) {
    return new CYImage(name, path, sprite);
  }
}
 
export default CYImage.Factory;