processTriggerEvent.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. function createCustomEvent() {
  3. var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  4. var e = {
  5. target: {
  6. dataset: {}
  7. },
  8. currentTarget: {
  9. dataset: {},
  10. // 组件的id
  11. id: props.id
  12. }
  13. };
  14. Object.keys(props).forEach(function (prop) {
  15. if (prop.match(/^data-/)) {
  16. var originProp = prop;
  17. prop = prop.replace(/[A-Z]/g, function ($) {
  18. return $.toLowerCase();
  19. });
  20. prop = prop.split('-');
  21. prop.shift();
  22. prop = prop.join('');
  23. e.target.dataset[prop] = props[originProp];
  24. e.currentTarget.dataset[prop] = props[originProp];
  25. }
  26. });
  27. return e;
  28. }
  29. function processTriggerEvent() {
  30. this.triggerEvent = function (event, data) {
  31. var eventType = event[0].toLowerCase() + event.substring(1);
  32. event = "on".concat(event[0].toUpperCase()).concat(event.substring(1));
  33. var e = createCustomEvent(this.props);
  34. e.type = eventType;
  35. e.detail = data;
  36. event = event.replace(/-\w+/, function (name) {
  37. name = name[1].toUpperCase() + name.substring(2);
  38. return name;
  39. });
  40. if (typeof this.props[event] === 'function') {
  41. this.props[event](e);
  42. }
  43. };
  44. }
  45. module.exports = processTriggerEvent;