const Resource = require('lib/models/Resource.js'); const Entities = require('html-entities').AllHtmlEntities; const htmlentities = new Entities().encode; const utils = {}; utils.getAttr = function(attrs, name, defaultValue = null) { for (let i = 0; i < attrs.length; i++) { if (attrs[i][0] === name) return attrs[i].length > 1 ? attrs[i][1] : null; } return defaultValue; }; utils.notDownloadedResource = function() { return ` `; }; utils.notDownloadedImage = function() { // https://github.com/ForkAwesome/Fork-Awesome/blob/master/src/icons/svg/file-image-o.svg // Height changed to 1795 return ` `; }; utils.notDownloadedFile = function() { // https://github.com/ForkAwesome/Fork-Awesome/blob/master/src/icons/svg/file-o.svg return ` `; }; utils.errorImage = function() { // https://github.com/ForkAwesome/Fork-Awesome/blob/master/src/icons/svg/times-circle.svg return ` `; }; utils.loaderImage = function() { // https://github.com/ForkAwesome/Fork-Awesome/blob/master/src/icons/svg/hourglass-half.svg return ` `; }; utils.resourceStatusImage = function(state) { if (state === 'notDownloaded') return utils.notDownloadedResource(); return utils.resourceStatusFile(state); }; utils.resourceStatusFile = function(state) { if (state === 'notDownloaded') return utils.notDownloadedResource(); if (state === 'downloading') return utils.loaderImage(); if (state === 'encrypted') return utils.loaderImage(); if (state === 'error') return utils.errorImage(); throw new Error('Unknown state: ' + state); }; utils.resourceStatus = function(resourceInfo) { let resourceStatus = 'ready'; if (resourceInfo) { const resource = resourceInfo.item; const localState = resourceInfo.localState; if (localState.fetch_status === Resource.FETCH_STATUS_IDLE) { resourceStatus = 'notDownloaded'; } else if (localState.fetch_status === Resource.FETCH_STATUS_STARTED) { resourceStatus = 'downloading'; } else if (localState.fetch_status === Resource.FETCH_STATUS_DONE) { if (resource.encryption_blob_encrypted || resource.encryption_applied) { resourceStatus = 'encrypted'; } } } else { resourceStatus = 'notDownloaded'; } return resourceStatus; }; utils.imageReplacement = function(src, resources, resourceBaseUrl) { if (!Resource.isResourceUrl(src)) return null; const resourceId = Resource.urlToId(src); const result = resources[resourceId]; const resource = result ? result.item : null; const resourceStatus = utils.resourceStatus(result); if (resourceStatus !== 'ready') { const icon = utils.resourceStatusImage(resourceStatus); return '