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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 1× 1× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 8× 2× 8× 28× 8× 8× 196× 90× 8× 8× 8× 8× 28× 28× 28× 28× 80× 80× 28× 8× 8× 22× 22× 22× 4× 4× 4× 2× 2× 2× 2× 4× 4× 2× 2× 22× 8× 8× 8× 8× 8× 8× 8× 8× | 'use strict'; import * as _ from 'lodash'; import * as p from 'path'; import u from 'url'; import configs from '../configs/default.js'; import { isTruthy } from '../helpers/utils.js'; import { SPRITE_LAYOUT } from '../helpers/utils.js'; const EXT = 'png'; class CYSprite { constructor(rule, path, options) { options = CYSprite.mergeOptions(rule, options); this.cacheBuster = new Date().getTime(); this.originalSelector = rule.selector; this.selector = rule.selector.replace(configs.NAMESPACE, ''); this.name = this.selector.substring(1); this.filename = `${this.name}.${EXT}`; this._inputDir = path; this._outputDir = options.dest; this.output = p.resolve(this._outputDir, this.filename); this.url = CYSprite.makeUrl( this.filename, this._outputDir, options.httpDest, options.relativeTo, options.cacheBuster, this.cacheBuster ); this._size = { width: 0, height: 0 }; this._cssBackgroundSize = ''; this._images = { pathList: [], imageList: [] }; this.rule = rule; this._options = options; } get size() { return this._size; } set size(size) { this._size = size; let {width, height} = size; width = this.isRetina ? width / this.ratio : width; height = this.isRetina ? height / this.ratio : height; this.cssBackgroundSize = `${width + 'px'} ${height + 'px'}`; } get cssBackgroundSize() { return this._cssBackgroundSize; } set cssBackgroundSize(size) { this._cssBackgroundSize = size; } get includeSize() { return this._options.includeSize; } get decorate() { return this._options.decorate; } get debug() { return this._options.debug; } get isRetina() { return this._options.retina; } get ratio() { return this._options.ratio; } get padding() { return this._options.padding; } get layout() { return this._options.layout; } get imageList() { return this._images.imageList; } get imagePaths() { return this._images.pathList; } get length() { return this._images.pathList.length; } addImage(image) { this._images.pathList.push(image.path); image.sprite = this; this._images.imageList.push(image); } getImage(absPath) { for (var i = 0, l = this._images.imageList.length; i < l; i++) { var image = this._images.imageList[i]; if (image.path === absPath) return image; } return false; } getTime() { return new Date(this.cacheBuster).toLocaleString(); } static mergeOptions(rule, options) { let _options = _.merge({}, options, { layout: SPRITE_LAYOUT.default, debug: options.verbose }); rule.walkDecls(configs.NAMESPACE_REGEX, function(decl) { let prop = (decl.prop || '') .replace(/(')|(")/gi, '') .replace(configs.NAMESPACE, '') .replace(/^-/, '') .toLowerCase() ; let value = (decl.value || '') .replace(/(')|(")/gi, '') .toLowerCase() ; switch (prop) { case 'debug': _options.debug = isTruthy(value); break; case 'padding': let padding = parseInt(value, 10); isNaN(padding) || (_options.padding = padding); break; case 'retina': _options.retina = isTruthy(value); break; case 'cacheBuster': case 'cache': case 'cache-buster': _options.cacheBuster = isTruthy(value); break; case 'ratio': _options.ratio = isNaN(value) ? _options.ratio : Number(value); break; case 'layout': _options.layout = SPRITE_LAYOUT.hasOwnProperty(value) ? value : SPRITE_LAYOUT.default; break; case 'decorate': _options.decorate = isTruthy(value); break; case 'include-size': case 'includesize': _options.includeSize = isTruthy(value); break; } decl.remove(); }); return _options; } static makeUrl(filename, outputDir, httpDest, relativeTo, cacheBuster, ts) { let url; Iif (httpDest) { (httpDest[httpDest.length - 1] === '/') || (httpDest += '/'); url = u.resolve(httpDest, filename); } else Eif (relativeTo) { url = p.join(p.relative(relativeTo, outputDir), filename); } else { url = p.join(outputDir, filename); } Iif(cacheBuster) url += `?${ts}`; return url; } static Factory(rule, path, options) { return new CYSprite(rule, path, options); } } export default CYSprite.Factory; |