wazuh-kibana-app/public/factories/vis2png.js
2019-01-14 17:36:47 +01:00

89 lines
2.1 KiB
JavaScript

/*
* Wazuh app - Fetch png from visualization div
* Copyright (C) 2015-2019 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import domtoimage from 'dom-to-image';
export class Vis2PNG {
/**
* Class constructor
* @param {*} $rootScope
*/
constructor($rootScope) {
this.$rootScope = $rootScope;
this.rawArray = [];
this.htmlObject = {};
this.working = false;
}
/**
* Validate a visualizations array
* @param {Array<Object>} visArray
*/
async checkArray(visArray) {
try {
this.working = true;
const len = visArray.length;
let currentCompleted = 0;
await Promise.all(
visArray.map(async currentValue => {
const tmpNode = this.htmlObject[currentValue];
try {
const tmpResult = await domtoimage.toPng(tmpNode[0]);
this.rawArray.push({
element: tmpResult,
width: tmpNode.width(),
height: tmpNode.height(),
id: currentValue
});
} catch (error) {} // eslint-disable-line
currentCompleted++;
this.$rootScope.reportStatus = `Generating report...${Math.round(
(currentCompleted / len) * 100
)}%`;
if (!this.$rootScope.$$phase) this.$rootScope.$digest();
})
);
this.working = false;
this.$rootScope.reportStatus = `Generating PDF document...`;
return this.rawArray;
} catch (error) {
this.working = false;
return Promise.reject(error);
}
}
/**
* Check if is working
*/
isWorking() {
return this.working;
}
/**
* Clear raw array
*/
clear() {
this.rawArray = [];
this.htmlObject = {};
}
/**
* Set content to a given html item
* @param {String} id
* @param {Object} content
*/
assignHTMLItem(id, content) {
this.htmlObject[id] = content;
}
}