const Entities = require('html-entities').AllHtmlEntities; const htmlentities = new Entities().encode; // Imported from models/Resource.js const FetchStatuses = { FETCH_STATUS_IDLE: 0, FETCH_STATUS_STARTED: 1, FETCH_STATUS_DONE: 2, FETCH_STATUS_ERROR: 3, }; const utils: any = {}; utils.getAttr = function(attrs: string[], name: string, defaultValue: string = 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(status: string) { if (status === 'notDownloaded') return utils.notDownloadedResource(); return utils.resourceStatusFile(status); }; utils.resourceStatusFile = function(status: string) { if (status === 'notDownloaded') return utils.notDownloadedResource(); if (status === 'downloading') return utils.loaderImage(); if (status === 'encrypted') return utils.loaderImage(); if (status === 'error') return utils.errorImage(); throw new Error(`Unknown status: ${status}`); }; utils.resourceStatusIndex = function(status: string) { if (status === 'error') return -1; if (status === 'notDownloaded') return 0; if (status === 'downloading') return 1; if (status === 'encrypted') return 2; if (status === 'ready') return 10; throw new Error(`Unknown status: ${status}`); }; utils.resourceStatusName = function(index: number) { if (index === -1) return 'error'; if (index === 0) return 'notDownloaded'; if (index === 1) return 'downloading'; if (index === 2) return 'encrypted'; if (index === 10) return 'ready'; throw new Error(`Unknown index: ${index}`); }; utils.resourceStatus = function(ResourceModel: any, resourceInfo: any) { if (!ResourceModel) return 'ready'; let resourceStatus = 'ready'; if (resourceInfo) { const resource = resourceInfo.item; const localState = resourceInfo.localState; if (localState.fetch_status === FetchStatuses.FETCH_STATUS_IDLE) { resourceStatus = 'notDownloaded'; } else if (localState.fetch_status === FetchStatuses.FETCH_STATUS_STARTED) { resourceStatus = 'downloading'; } else if (localState.fetch_status === FetchStatuses.FETCH_STATUS_DONE) { if (resource.encryption_blob_encrypted || resource.encryption_applied) { resourceStatus = 'encrypted'; } } } else { resourceStatus = 'notDownloaded'; } return resourceStatus; }; export type ItemIdToUrlHandler = (resource: any)=> string; utils.imageReplacement = function(ResourceModel: any, src: string, resources: any, resourceBaseUrl: string, itemIdToUrl: ItemIdToUrlHandler = null) { if (!ResourceModel || !resources) return null; if (!ResourceModel.isResourceUrl(src)) return null; const resourceId = ResourceModel.urlToId(src); const result = resources[resourceId]; const resource = result ? result.item : null; const resourceStatus = utils.resourceStatus(ResourceModel, result); if (resourceStatus !== 'ready') { const icon = utils.resourceStatusImage(resourceStatus); return `
` + `` + '
'; } const mime = resource.mime ? resource.mime.toLowerCase() : ''; if (ResourceModel.isSupportedImageMimeType(mime)) { let newSrc = ''; if (itemIdToUrl) { newSrc = itemIdToUrl(resource.id); } else { const temp = []; if (resourceBaseUrl) { temp.push(resourceBaseUrl); } else { temp.push('./'); } temp.push(ResourceModel.filename(resource)); temp.push(`?t=${resource.updated_time}`); newSrc = temp.join(''); } // let newSrc = `./${ResourceModel.filename(resource)}`; // newSrc += `?t=${resource.updated_time}`; return { 'data-resource-id': resource.id, src: newSrc, }; } return null; }; // Used in mobile app when enableLongPress = true. Tells for how long // the resource should be pressed before the menu is shown. utils.longPressDelay = 500; export default utils;