mirror of
https://github.com/nasa/openmct.git
synced 2025-06-15 21:58:13 +00:00
[Testing] Add better dependency injection and error handling
This commit is contained in:
@ -92,7 +92,13 @@ define([
|
|||||||
{
|
{
|
||||||
"key": "ExportImageService",
|
"key": "ExportImageService",
|
||||||
"implementation": ExportImageService,
|
"implementation": ExportImageService,
|
||||||
"depends": []
|
"depends": [
|
||||||
|
"$q",
|
||||||
|
"$timeout",
|
||||||
|
"$log",
|
||||||
|
"EXPORT_IMAGE_TIMEOUT"
|
||||||
|
]
|
||||||
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"constants": [
|
"constants": [
|
||||||
@ -101,6 +107,11 @@ define([
|
|||||||
"value": 900000,
|
"value": 900000,
|
||||||
"priority": "fallback",
|
"priority": "fallback",
|
||||||
"comment": "Fifteen minutes."
|
"comment": "Fifteen minutes."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "EXPORT_IMAGE_TIMEOUT",
|
||||||
|
"value": 500,
|
||||||
|
"priority": "fallback"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"policies": [
|
"policies": [
|
||||||
|
@ -374,7 +374,7 @@ define(
|
|||||||
PlotController.prototype.exportPDF = function () {
|
PlotController.prototype.exportPDF = function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.hideExportButtons = true;
|
self.hideExportButtons = true;
|
||||||
self.ExportImageService.exportPDF(self.$element[0], "plot.pdf", function () {
|
self.ExportImageService.exportPDF(self.$element[0], "plot.pdf").finally(function () {
|
||||||
self.hideExportButtons = false;
|
self.hideExportButtons = false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -385,7 +385,7 @@ define(
|
|||||||
PlotController.prototype.exportPNG = function () {
|
PlotController.prototype.exportPNG = function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.hideExportButtons = true;
|
self.hideExportButtons = true;
|
||||||
self.ExportImageService.exportPNG(self.$element[0], "plot.png", function () {
|
self.ExportImageService.exportPNG(self.$element[0], "plot.png").finally(function () {
|
||||||
self.hideExportButtons = false;
|
self.hideExportButtons = false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -396,7 +396,7 @@ define(
|
|||||||
PlotController.prototype.exportJPG = function () {
|
PlotController.prototype.exportJPG = function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.hideExportButtons = true;
|
self.hideExportButtons = true;
|
||||||
self.ExportImageService.exportJPG(self.$element[0], "plot.jpg", function () {
|
self.ExportImageService.exportJPG(self.$element[0], "plot.jpg").finally(function () {
|
||||||
self.hideExportButtons = false;
|
self.hideExportButtons = false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -29,75 +29,88 @@ define(
|
|||||||
"jsPDF",
|
"jsPDF",
|
||||||
"saveAs"
|
"saveAs"
|
||||||
],
|
],
|
||||||
function (html2canvas, JsPdf, saveAs) {
|
function (
|
||||||
|
html2canvas,
|
||||||
|
JsPdf,
|
||||||
|
saveAs
|
||||||
|
) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The export image service will export any HTML node to
|
* The export image service will export any HTML node to
|
||||||
* PDF, JPG, or PNG.
|
* PDF, JPG, or PNG.
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function ExportImageService() {
|
function ExportImageService($q, $timeout, $log, EXPORT_IMAGE_TIMEOUT) {
|
||||||
|
self.$q = $q;
|
||||||
|
self.$timeout = $timeout;
|
||||||
|
self.$log = $log;
|
||||||
|
self.EXPORT_IMAGE_TIMEOUT = EXPORT_IMAGE_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders an HTML element into a base64 encoded image
|
* Renders an HTML element into a base64 encoded image
|
||||||
* as a BLOB, PNG, or JPG.
|
* as a BLOB, PNG, or JPG.
|
||||||
* @param {node} element that will be converted to an image
|
* @param {node} element that will be converted to an image
|
||||||
* @param {function} callback for passing the resulting image
|
|
||||||
* @param {string} type of image to convert the element to
|
* @param {string} type of image to convert the element to
|
||||||
* @returns {string} the color, in #RRGGBB form
|
* @returns {promise}
|
||||||
*/
|
*/
|
||||||
function renderElement(element, callback, type) {
|
function renderElement(element, type) {
|
||||||
type = type || "jpeg";
|
var defer = self.$q.defer(),
|
||||||
|
renderTimeout;
|
||||||
|
|
||||||
html2canvas(element, {
|
renderTimeout = self.$timeout(function() {
|
||||||
onrendered: function (canvas) {
|
defer.reject("html2canvas timed out");
|
||||||
switch (type.toLowerCase()) {
|
}, self.EXPORT_IMAGE_TIMEOUT);
|
||||||
case "blob":
|
|
||||||
canvas.toBlob(callback);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "png":
|
try {
|
||||||
callback(canvas.toDataURL("image/png", 1.0));
|
html2canvas(element, {
|
||||||
break;
|
onrendered: function (canvas) {
|
||||||
|
switch (type.toLowerCase()) {
|
||||||
|
case "blob":
|
||||||
|
canvas.toBlob(defer.resolve);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
case "png":
|
||||||
case "jpg":
|
defer.resolve(canvas.toDataURL("image/png", 1.0));
|
||||||
case "jpeg":
|
break;
|
||||||
callback(canvas.toDataURL("image/jpeg", 1.0));
|
|
||||||
break;
|
default:
|
||||||
|
case "jpg":
|
||||||
|
case "jpeg":
|
||||||
|
defer.resolve(canvas.toDataURL("image/jpeg", 1.0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
} catch(e) {
|
||||||
|
self.$log.warn("html2canvas failed with error: " + e);
|
||||||
|
defer.reject(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
defer.promise.finally(renderTimeout.cancel);
|
||||||
|
|
||||||
|
return defer.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
ExportImageService.prototype.exportPDF = function (element, filename, callback) {
|
ExportImageService.prototype.exportPDF = function (element, filename) {
|
||||||
callback = typeof callback === "function" ? callback : function () {};
|
return renderElement(element, "jpeg").then(function (img) {
|
||||||
|
|
||||||
renderElement(element, function (img) {
|
|
||||||
var pdf = new JsPdf("l", "px", [element.offsetHeight, element.offsetWidth]);
|
var pdf = new JsPdf("l", "px", [element.offsetHeight, element.offsetWidth]);
|
||||||
pdf.addImage(img, "JPEG", 0, 0, element.offsetWidth, element.offsetHeight);
|
pdf.addImage(img, "JPEG", 0, 0, element.offsetWidth, element.offsetHeight);
|
||||||
pdf.save(filename);
|
pdf.save(filename);
|
||||||
callback();
|
});
|
||||||
}, "jpeg");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ExportImageService.prototype.exportJPG = function (element, filename, callback) {
|
ExportImageService.prototype.exportJPG = function (element, filename) {
|
||||||
callback = typeof callback === "function" ? callback : function () {};
|
return renderElement(element, "blob").then(function (img) {
|
||||||
|
|
||||||
renderElement(element, function (img) {
|
|
||||||
saveAs(img, filename);
|
saveAs(img, filename);
|
||||||
callback();
|
});
|
||||||
}, "blob");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ExportImageService.prototype.exportPNG = function (element, filename, callback) {
|
ExportImageService.prototype.exportPNG = function (element, filename) {
|
||||||
callback = typeof callback === "function" ? callback : function () {};
|
return renderElement(element, "blob").then(function (img) {
|
||||||
|
|
||||||
renderElement(element, function (img) {
|
|
||||||
saveAs(img, filename);
|
saveAs(img, filename);
|
||||||
callback();
|
});
|
||||||
}, "blob");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return ExportImageService;
|
return ExportImageService;
|
||||||
|
Reference in New Issue
Block a user