shared.js

  1. // Common functions for use across other geoApi modules
  2. 'use strict';
  3. function getLayerTypeBuilder(esriBundle) {
  4. /**
  5. * Will return a string indicating the type of layer a layer object is.
  6. * @method getLayerType
  7. * @param {Object} layer an ESRI API layer object
  8. * @return {String} layer type
  9. */
  10. return layer => {
  11. if (layer instanceof esriBundle.FeatureLayer) {
  12. return 'FeatureLayer';
  13. } else if (layer instanceof esriBundle.WmsLayer) {
  14. return 'WmsLayer';
  15. } else if (layer instanceof esriBundle.ArcGISDynamicMapServiceLayer) {
  16. return 'ArcGISDynamicMapServiceLayer';
  17. } else if (layer instanceof esriBundle.ArcGISTiledMapServiceLayer) {
  18. return 'ArcGISTiledMapServiceLayer';
  19. } else {
  20. // Can add more types above as we support them
  21. return 'UNKNOWN';
  22. }
  23. };
  24. }
  25. /**
  26. * Get a 'good enough' uuid. For backup purposes if client does not supply its own
  27. * unique layer id
  28. *
  29. * @method generateUUID
  30. * @returns {String} a uuid
  31. */
  32. function generateUUID() {
  33. let d = Date.now();
  34. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
  35. // do math!
  36. /*jslint bitwise: true */
  37. const r = (d + Math.random() * 16) % 16 | 0;
  38. d = Math.floor(d / 16);
  39. return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  40. /*jslint bitwise: false */
  41. });
  42. }
  43. /**
  44. * Convert an image to a canvas element
  45. *
  46. * @param {String} url image url to convert (result from the esri print task)
  47. * @param {Object} canvas [optional = null] canvas to draw the image upon; if not supplied, a new canvas will be made
  48. * @param {Boolean} crossOrigin [optional = true] when set, tries to fetch an image with crossOrigin = anonymous
  49. * @return {Promise} conversion promise resolving into a canvas of the image
  50. */
  51. function convertImageToCanvas(url, canvas = null, crossOrigin = true) {
  52. canvas = canvas || document.createElement('canvas');
  53. const image = document.createElement('img'); // create image node
  54. if (crossOrigin) {
  55. image.crossOrigin = 'Anonymous'; // configure the CORS request
  56. }
  57. const conversionPromise = new Promise((resolve, reject) => {
  58. image.addEventListener('load', () => {
  59. canvas.width = image.width; // changing canvas size will clear all previous content
  60. canvas.height = image.height;
  61. canvas.getContext('2d').drawImage(image, 0, 0); // draw image onto a canvas
  62. // return canvas
  63. resolve(canvas);
  64. });
  65. image.addEventListener('error', error =>
  66. reject(error));
  67. });
  68. // set image source to the one generated from the print task
  69. image.src = url;
  70. return conversionPromise;
  71. }
  72. /**
  73. * Loads an image (as crossing) and converts it to dataURL. If a supplied imageUri is already a dataURL, just return it.
  74. * If an image fails to load with the crossing attribute, return the original imageUri
  75. *
  76. * @function convertImagetoDataURL
  77. * @param {String} imageUri url of the image to load and convert
  78. * @param {String} imageType [optional = 'image/png'] format of the image representation
  79. * @return {Promise} promise resolving with the dataURL of the image
  80. */
  81. function convertImagetoDataURL(imageUri, imageType = 'image/png') {
  82. // this is already a dataUrl, just return
  83. if (imageUri.startsWith('data')) {
  84. console.log('ImageUri is already a data url');
  85. return Promise.resolve(imageUri);
  86. }
  87. const loadingPromise = convertImageToCanvas(imageUri)
  88. .then(canvas => {
  89. console.log('Converting image to dataURL');
  90. return canvas.toDataURL(imageType);
  91. })
  92. .catch(error => {
  93. console.error('Failed to load crossorigin image', imageUri, error);
  94. return imageUri;
  95. });
  96. return loadingPromise;
  97. }
  98. module.exports = esriBundle => ({
  99. getLayerType: getLayerTypeBuilder(esriBundle),
  100. generateUUID,
  101. convertImageToCanvas,
  102. convertImagetoDataURL
  103. });