selectComponent.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. function selectComponent(ctx) {
  3. this.$ctx = ctx;
  4. this.$nodes = {};
  5. this.$cacheNodes = {};
  6. }
  7. selectComponent.prototype = {
  8. _addComponentNode: function _addComponentNode(className, ctx) {
  9. className = ".".concat(className);
  10. var componentNodes = this.$nodes;
  11. if (componentNodes[className]) {
  12. componentNodes[className].push(ctx);
  13. } else {
  14. componentNodes[className] = [ctx];
  15. }
  16. this.$cacheNodes[ctx.$id] = {
  17. className: className
  18. };
  19. },
  20. addComponentNodeId: function addComponentNodeId(id, ctx) {
  21. id = "#".concat(id);
  22. ctx.props && ctx.props.id ? ctx.id = ctx.props.id : ctx.id = '';
  23. var componentNodes = this.$nodes;
  24. if (componentNodes[id]) {
  25. componentNodes[id].push(ctx);
  26. } else {
  27. componentNodes[id] = [ctx];
  28. }
  29. this.$cacheNodes[ctx.$id] = {
  30. id: id
  31. };
  32. },
  33. addComponentNode: function addComponentNode() {
  34. var _this = this;
  35. var className = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  36. var ctx = arguments.length > 1 ? arguments[1] : undefined;
  37. ctx.props && ctx.props.id ? ctx.id = ctx.props.id : ctx.id = '';
  38. var classNameArray = className.split(/\s+/g);
  39. classNameArray.forEach(function (classNameStr) {
  40. _this._addComponentNode(classNameStr, ctx);
  41. });
  42. },
  43. selectComponent: function selectComponent(className) {
  44. var componentNodes = this.$nodes;
  45. return componentNodes[className] && componentNodes[className][0];
  46. },
  47. selectComponents: function selectComponents(className) {
  48. var componentNodes = this.$nodes;
  49. return componentNodes[className];
  50. },
  51. preProcesscomponents: preProcesscomponents,
  52. connect: function connect() {
  53. var ctx = this.$ctx;
  54. var self = this;
  55. ctx.selectComponent = function () {
  56. if (self.selectComponent.apply(self, arguments) && self.selectComponent.apply(self, arguments)._this !== undefined) {
  57. return self.selectComponent.apply(self, arguments)._this;
  58. } else {
  59. return self.selectComponent.apply(self, arguments);
  60. }
  61. };
  62. ctx.selectAllComponents = function () {
  63. var componentsArr = self.selectComponents.apply(self, arguments) || [];
  64. var newArr = [];
  65. componentsArr.forEach(function (item) {
  66. if (item._this !== undefined) {
  67. newArr.push(item._this);
  68. } else {
  69. newArr.push(item);
  70. }
  71. });
  72. return newArr;
  73. };
  74. }
  75. };
  76. function preProcesscomponents(ctx) {
  77. var _this2 = this;
  78. var selectorObj = this.$cacheNodes[ctx.$id];
  79. selectorObj && Object.keys(selectorObj).forEach(function (item) {
  80. _this2.$nodes[item] = [];
  81. });
  82. if (ctx.props.id) {
  83. this.addComponentNodeId(ctx.props.id, ctx);
  84. }
  85. if (ctx.props.className) {
  86. this.addComponentNode(ctx.props.className, ctx);
  87. }
  88. }
  89. module.exports = selectComponent;