events.js

  1. /**
  2. * Wire up any supplied handlers to the corresponding dojo .on events on layer.
  3. * Purpose is to keep Dojo .on events contained in geoApi.
  4. *
  5. * @param {esriObject} esriObject which contains the dojo events to be wrapped
  6. * @param {handlers} handlers is an object which contains all handlers needed
  7. *
  8. * @return {object} evt contains the events created on the object, keyed by same properties as handlers input
  9. */
  10. function wrapEvents(esriObject, handlers) {
  11. const evt = {};
  12. Object.keys(handlers).forEach(ourEventName => {
  13. // replace camelCase name to dojo event name format
  14. const dojoName = ourEventName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
  15. // TODO: Validity checking on inputs for dojoName
  16. // make dojo call
  17. evt[ourEventName] = esriObject.on(dojoName, (e) => {
  18. // check if needs special handling to point at layer calling event
  19. const layerEvents = ['update-start', 'update-end', 'error'];
  20. if (layerEvents.indexOf(dojoName) >= 0) {
  21. e.layer = e.target;
  22. }
  23. handlers[ourEventName](e);
  24. });
  25. });
  26. return evt;
  27. }
  28. module.exports = () => {
  29. return {
  30. wrapEvents
  31. };
  32. };