|
1 | | -var _ = require('lodash'); |
2 | | -var Promise = require('bluebird'); |
3 | | -var logger = require('../logger'); |
4 | | -var utils = require('../utils'); |
| 1 | +'use strict'; |
5 | 2 |
|
6 | | -var HtmlHandler = require('./html'); |
7 | | -var CssHandler = require('./css'); |
| 3 | +const _ = require('lodash'); |
| 4 | +const Promise = require('bluebird'); |
| 5 | +const logger = require('../logger'); |
| 6 | +const utils = require('../utils'); |
8 | 7 |
|
9 | | -var supportedOptions = ['prettifyUrls', 'sources', 'recursiveSources', 'maxRecursiveDepth', 'defaultFilename']; |
| 8 | +const HtmlHandler = require('./html'); |
| 9 | +const CssHandler = require('./css'); |
10 | 10 |
|
11 | | -function ResourceHandler (options, context) { |
12 | | - this.options = _.pick(options, supportedOptions); |
13 | | - this.context = context; |
| 11 | +const supportedOptions = ['prettifyUrls', 'sources', 'recursiveSources', 'maxRecursiveDepth', 'defaultFilename', 'updateMissingSources']; |
14 | 12 |
|
15 | | - this.htmlHandler = new HtmlHandler(this.options, this.handleChildrenResources.bind(this)); |
16 | | - this.cssHandler = new CssHandler(this.options, this.handleChildrenResources.bind(this)); |
17 | | -} |
| 13 | +class ResourceHandler { |
| 14 | + constructor (options, context) { |
| 15 | + this.options = _.pick(options, supportedOptions); |
| 16 | + this.context = context; |
| 17 | + |
| 18 | + const methods = { |
| 19 | + downloadChildrenPaths: this.downloadChildrenResources.bind(this), |
| 20 | + updateChildrenPaths: this.updateChildrenResources.bind(this) |
| 21 | + }; |
18 | 22 |
|
19 | | -ResourceHandler.prototype.getResourceHandler = function getResourceHandler (resource) { |
20 | | - switch (true) { |
21 | | - case resource.isCss(): |
22 | | - logger.debug('using css handler for ' + resource); |
23 | | - return this.cssHandler; |
24 | | - case resource.isHtml(): |
25 | | - logger.debug('using html handler for ' + resource); |
26 | | - return this.htmlHandler; |
27 | | - default: |
28 | | - logger.debug('using no handler for ' + resource); |
29 | | - return null; |
| 23 | + this.htmlHandler = new HtmlHandler(this.options, methods); |
| 24 | + this.cssHandler = new CssHandler(this.options, methods); |
30 | 25 | } |
31 | | -}; |
32 | | - |
33 | | -/** |
34 | | - * Request all resources from pathContainers paths |
35 | | - * @param pathContainer - instance of ImgSrcsetTag or CommonTag or CssText, contains original paths for resources |
36 | | - * @param {Resource} parentResource |
37 | | - * @returns {Promise} - resolved when all resources from pathContainer were requested |
38 | | - * and original paths in parentResource were updated with local paths for children resources |
39 | | - */ |
40 | | -ResourceHandler.prototype.handleChildrenResources = function handleChildrenResources (pathContainer, parentResource) { |
41 | | - var self = this; |
42 | | - var childrenPaths = pathContainer.getPaths(); |
43 | | - var pathsToUpdate = []; |
44 | | - |
45 | | - var childrenPromises = childrenPaths.map(function loadChildPath (childPath) { |
46 | | - var childResourceUrl = utils.getUrl(parentResource.getUrl(), childPath); |
47 | | - var childResource = parentResource.createChild(childResourceUrl); |
48 | | - |
49 | | - return self.context.requestResource(childResource).then(function updateChildPath (respondedResource) { |
50 | | - if (respondedResource) { |
51 | | - parentResource.updateChild(childResource, respondedResource); |
52 | | - |
53 | | - var relativePath = utils.getRelativePath(parentResource.getFilename(), respondedResource.getFilename()); |
54 | | - if (self.options.prettifyUrls) { |
55 | | - relativePath = relativePath.replace(self.options.defaultFilename, ''); |
56 | | - } |
57 | | - var hash = utils.getHashFromUrl(childPath); |
58 | 26 |
|
59 | | - if (hash) { |
60 | | - relativePath = relativePath.concat(hash); |
| 27 | + getResourceHandler (resource) { |
| 28 | + switch (true) { |
| 29 | + case resource.isCss(): |
| 30 | + logger.debug('using css handler for ' + resource); |
| 31 | + return this.cssHandler; |
| 32 | + case resource.isHtml(): |
| 33 | + logger.debug('using html handler for ' + resource); |
| 34 | + return this.htmlHandler; |
| 35 | + default: |
| 36 | + logger.debug('using no handler for ' + resource); |
| 37 | + return null; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Request all resources from pathContainers paths |
| 43 | + * @param pathContainer - instance of ImgSrcsetTag or CommonTag or CssText, contains original paths for resources |
| 44 | + * @param {Resource} parentResource |
| 45 | + * @param {boolean} updateIfFailed - if true - failed resources will be updated with absolute links |
| 46 | + * @returns {Promise} - resolved when all resources from pathContainer were requested |
| 47 | + * and original paths in parentResource were updated with local paths for children resources |
| 48 | + */ |
| 49 | + downloadChildrenResources (pathContainer, parentResource, updateIfFailed) { |
| 50 | + const self = this; |
| 51 | + const childrenPaths = pathContainer.getPaths(); |
| 52 | + const pathsToUpdate = []; |
| 53 | + |
| 54 | + const childrenPromises = childrenPaths.map((childPath) => { |
| 55 | + const childResourceUrl = utils.getUrl(parentResource.getUrl(), childPath); |
| 56 | + const childResource = parentResource.createChild(childResourceUrl); |
| 57 | + |
| 58 | + return self.context.requestResource(childResource).then((respondedResource) => { |
| 59 | + if (respondedResource) { |
| 60 | + parentResource.updateChild(childResource, respondedResource); |
| 61 | + |
| 62 | + let relativePath = utils.getRelativePath(parentResource.getFilename(), respondedResource.getFilename()); |
| 63 | + if (self.options.prettifyUrls) { |
| 64 | + relativePath = relativePath.replace(self.options.defaultFilename, ''); |
| 65 | + } |
| 66 | + const hash = utils.getHashFromUrl(childPath); |
| 67 | + |
| 68 | + if (hash) { |
| 69 | + relativePath = relativePath.concat(hash); |
| 70 | + } |
| 71 | + |
| 72 | + pathsToUpdate.push({ oldPath: childPath, newPath: relativePath}); |
| 73 | + } else { |
| 74 | + if (updateIfFailed) { |
| 75 | + pathsToUpdate.push({ oldPath: childPath, newPath: childResourceUrl}); |
| 76 | + } |
61 | 77 | } |
| 78 | + return null; // Prevent Bluebird warnings |
| 79 | + }); |
| 80 | + }); |
62 | 81 |
|
63 | | - pathsToUpdate.push({ oldPath: childPath, newPath: relativePath}); |
64 | | - } |
65 | | - return null; // Prevent Bluebird warnings |
| 82 | + return utils.waitAllFulfilled(childrenPromises).then(function updateChildrenPaths () { |
| 83 | + return pathContainer.updateText(pathsToUpdate); |
66 | 84 | }); |
67 | | - }); |
| 85 | + } |
68 | 86 |
|
69 | | - return utils.waitAllFulfilled(childrenPromises).then(function updateChildrenPaths () { |
70 | | - return pathContainer.updateText(pathsToUpdate); |
71 | | - }); |
72 | | -}; |
| 87 | + updateChildrenResources (pathContainer, parentResource, needToUpdate) { |
| 88 | + if (!needToUpdate) { |
| 89 | + return Promise.resolve(pathContainer.updateText([])); |
| 90 | + } |
| 91 | + const parentUrl = parentResource.getUrl(); |
| 92 | + const pathsToUpdate = []; |
| 93 | + pathContainer.getPaths().forEach((path) => { |
| 94 | + const childAbsoluteUrl = utils.getUrl(parentUrl, path); |
| 95 | + pathsToUpdate.push({ oldPath: path, newPath: childAbsoluteUrl }); |
| 96 | + }); |
| 97 | + return Promise.resolve(pathContainer.updateText(pathsToUpdate)); |
| 98 | + } |
73 | 99 |
|
74 | | -ResourceHandler.prototype.handleResource = function handleResource (resource) { |
75 | | - var resourceHandler = this.getResourceHandler(resource); |
76 | | - if (resourceHandler && resourceHandler.handle) { |
77 | | - return resourceHandler.handle(resource); |
| 100 | + handleResource (resource) { |
| 101 | + const resourceHandler = this.getResourceHandler(resource); |
| 102 | + if (resourceHandler && resourceHandler.handle) { |
| 103 | + return resourceHandler.handle(resource); |
| 104 | + } |
| 105 | + return Promise.resolve(resource); |
78 | 106 | } |
79 | | - return Promise.resolve(resource); |
80 | | -}; |
| 107 | +} |
81 | 108 |
|
82 | 109 | module.exports = ResourceHandler; |
0 commit comments