functional.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.inherit = inherit;
  5. exports.emit = emit;
  6. exports.mount = mount;
  7. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  8. var _vue = _interopRequireDefault(require("vue"));
  9. var inheritKey = ['ref', 'key', 'style', 'class', 'attrs', 'refInFor', 'nativeOn', 'directives', 'staticClass', 'staticStyle'];
  10. var mapInheritKey = {
  11. nativeOn: 'on'
  12. }; // inherit partial context, map nativeOn to on
  13. function inherit(context, inheritListeners) {
  14. var result = inheritKey.reduce(function (obj, key) {
  15. if (context.data[key]) {
  16. obj[mapInheritKey[key] || key] = context.data[key];
  17. }
  18. return obj;
  19. }, {});
  20. if (inheritListeners) {
  21. result.on = result.on || {};
  22. (0, _extends2.default)(result.on, context.data.on);
  23. }
  24. return result;
  25. } // emit event
  26. function emit(context, eventName) {
  27. for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  28. args[_key - 2] = arguments[_key];
  29. }
  30. var listeners = context.listeners[eventName];
  31. if (listeners) {
  32. if (Array.isArray(listeners)) {
  33. listeners.forEach(function (listener) {
  34. listener.apply(void 0, args);
  35. });
  36. } else {
  37. listeners.apply(void 0, args);
  38. }
  39. }
  40. } // mount functional component
  41. function mount(Component, data) {
  42. var instance = new _vue.default({
  43. el: document.createElement('div'),
  44. props: Component.props,
  45. render: function render(h) {
  46. return h(Component, (0, _extends2.default)({
  47. props: this.$props
  48. }, data));
  49. }
  50. });
  51. document.body.appendChild(instance.$el);
  52. return instance;
  53. }