component.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  4. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  5. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  6. var utils = require('../../api/utils');
  7. var warnLife = utils.warnLife,
  8. fnAppClass = utils.fnAppClass;
  9. var Relations = require('../../api/relations');
  10. var config = require('../../api/config.js');
  11. var _require = require('../../api/my'),
  12. createSelectorQuery = _require.createSelectorQuery,
  13. createIntersectionObserver = _require.createIntersectionObserver;
  14. var _require2 = require('../utils'),
  15. getUrl = _require2.getUrl,
  16. updateData = _require2.updateData,
  17. processRelationPath = _require2.processRelationPath,
  18. _relationNode = _require2._relationNode,
  19. findRelationNode = _require2.findRelationNode,
  20. compatibleLifetime = _require2.compatibleLifetime,
  21. collectObserver = _require2.collectObserver,
  22. collectObservers = _require2.collectObservers,
  23. processTriggerEvent = _require2.processTriggerEvent,
  24. observerHandle = _require2.observerHandle,
  25. handleProps = _require2.handleProps,
  26. handleExternalClasses = _require2.handleExternalClasses,
  27. handleAfterInit = _require2.handleAfterInit,
  28. mergeOptions = _require2.mergeOptions,
  29. copy = _require2.copy,
  30. nextUid = _require2.nextUid;
  31. var SelectComponent = require('./selectComponent');
  32. var processRelationHandle = require('./processRelation');
  33. var createNode = require('./relation');
  34. var _require3 = require('./utils'),
  35. antmoveAction = _require3.antmoveAction;
  36. function getInfo(key, obj) {
  37. var val = {};
  38. Object.keys(obj).forEach(function (item) {
  39. if (key === item || key.indexOf(item) !== -1) {
  40. val = obj[item];
  41. }
  42. });
  43. return val;
  44. }
  45. function processRelations(ctx) {
  46. var relationInfo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  47. var route = ctx.is;
  48. route = route.replace(/\/node_modules\/[a-z-]+\/[a-z-]+/, '');
  49. if (route[0] === '/') {
  50. route = route.substring(1);
  51. }
  52. var info = getInfo(route, relationInfo);
  53. if (info) {
  54. processRelationHandle(info, function (node) {
  55. if (node.$id === 'saveChildRef0') {
  56. ctx[node.$id] = function () {};
  57. node.$index = 0;
  58. node.$route = route;
  59. createNode.call(ctx, ctx, null, node);
  60. return false;
  61. }
  62. ctx[node.$id] = function (ref) {
  63. ctx.$antmove = ctx.$antmove || {};
  64. if (ctx.$antmove[node.$id] === undefined) {
  65. ctx.$antmove[node.$id] = 0;
  66. } else {
  67. ctx.$antmove[node.$id] += 1;
  68. }
  69. this.selectComponentApp.preProcesscomponents(ref);
  70. node.$index = ctx.$antmove[node.$id];
  71. node.$route = route;
  72. createNode.call(ctx, ref, null, node);
  73. };
  74. });
  75. } else {
  76. console.warn('Missing nodes relation of ', route);
  77. }
  78. }
  79. function handleRelations() {
  80. var _this = this;
  81. if (this.props.theRelations) {
  82. Object.keys(this.props.theRelations).forEach(function (relation) {
  83. var _p = processRelationPath(_this, relation);
  84. var relationInfo = _this.props.theRelations[relation];
  85. var nodes = null;
  86. if (relationInfo.type === 'child' || relationInfo.type === 'descendant') {
  87. return false;
  88. }
  89. nodes = findRelationNode(_this.$node, _p, relationInfo.type, true);
  90. if (!nodes || nodes[0] === undefined) {
  91. return false;
  92. }
  93. nodes.forEach(function (n) {
  94. if (!n) {
  95. // console.error('wrong relation reference of ', relationInfo);
  96. // console.error('from: ', this.$node.$self.is, 'to: ', _p);
  97. return false;
  98. }
  99. _relationNode.call(_this, n, {
  100. relationInfo: relationInfo,
  101. _p: _p,
  102. relation: relation
  103. });
  104. });
  105. });
  106. }
  107. }
  108. function processObservers(observersObj, options, param) {
  109. if (options.observers) {
  110. collectObservers.call(this, observersObj, options, param);
  111. }
  112. }
  113. function processInit() {
  114. getUrl();
  115. }
  116. function processIntersectionObserver(context) {
  117. context.createIntersectionObserver = function () {
  118. return createIntersectionObserver.fn.apply(createIntersectionObserver, arguments);
  119. };
  120. }
  121. /**
  122. *
  123. * @param {*} behavior
  124. * @param {*} _opts
  125. * @param {*} mixins
  126. */
  127. module.exports = {
  128. processTransformationComponent: function processTransformationComponent(_opts, options) {
  129. var fnApp = fnAppClass();
  130. options.properties = options.properties || {};
  131. var behaviors = options.behaviors || [];
  132. var mixins = options.mixins || [];
  133. var _export = options["export"] || '';
  134. delete options.behaviors;
  135. delete options.mixins;
  136. var retMixins = {};
  137. _opts.observerObj = {};
  138. _opts.observersObj = {};
  139. _opts.behaviorsArr = [];
  140. processBehavior(retMixins, behaviors, _opts.behaviorsArr);
  141. processBehavior(retMixins, mixins, _opts.behaviorsArr);
  142. mergeOptions(retMixins, options);
  143. processBehaviorId(behaviors);
  144. processBehaviorId(mixins);
  145. Object.keys(options).forEach(function (key) {
  146. _opts[key] = options[key];
  147. });
  148. handleProps(_opts);
  149. handleExternalClasses(_opts);
  150. var _life = compatibleLifetime(options);
  151. if (options.properties) {
  152. collectObserver(_opts.observerObj, options.properties, options);
  153. }
  154. if (!_opts.methods) {
  155. _opts.methods = {};
  156. }
  157. _opts.methods.antmoveAction = antmoveAction;
  158. /**
  159. * 处理组件所在的页面尺寸变化时执行
  160. */
  161. if (_opts.pageLifetimes && _opts.pageLifetimes.resize) {
  162. _opts.methods.antmovePageLifetimes = function (e) {
  163. return _opts.pageLifetimes.resize(e);
  164. };
  165. }
  166. var didMount = function didMount() {
  167. var _this2 = this;
  168. _life.error && warnLife('There is no error life cycle', 'error');
  169. _life.move && warnLife('There is no moved life cycle', 'moved');
  170. _life.pageLifetimes && warnLife('There is no page life cycle where the component resides,including(show,hide,resize)', 'pageLifetimes');
  171. this.props.genericSelectable && warnLife('generic:selectable is Unsupported', 'generic'); // process relations, get relation ast
  172. var relationAst = createNode.call(this, null, null, null, null, true).mountedHandles;
  173. relationAst.push(function () {
  174. handleRelations.call(_this2);
  175. });
  176. };
  177. fnApp.add('onInit', function () {
  178. this.onPageReady = function (p) {
  179. _opts.onPageReady && _opts.onPageReady.call(this, p);
  180. };
  181. });
  182. fnApp.add('deriveDataFromProps', function () {});
  183. fnApp.add('didMount', didMount);
  184. if (_opts.lifetimes && _opts.lifetimes.created) {
  185. fnApp.add('onInit', _opts.lifetimes.created);
  186. } else {
  187. fnApp.add('onInit', _opts.created);
  188. }
  189. fnApp.insert('onInit', function () {
  190. this.__wxExparserNodeId__ = nextUid();
  191. processIntersectionObserver(this);
  192. this.createSelectorQuery = function () {
  193. if (config.env !== 'production') {
  194. console.warn('支付宝createSelectorQuery不支持限定选择器的选择范围,如使用,请保证对应选择器使用的唯一性');
  195. }
  196. return createSelectorQuery.fn();
  197. };
  198. for (var method in this) {
  199. if (typeof this[method] === 'function') {
  200. this[method] = this[method].bind(this);
  201. }
  202. }
  203. this.getRelationNodes = function () {
  204. return [];
  205. };
  206. processComponentExport(_export, behaviors, this);
  207. this.selectComponentApp = new SelectComponent(this);
  208. this.properties = _objectSpread({}, _opts.properties);
  209. processInit.call(this, _opts, options, _life, fnApp);
  210. testBehaviors(behaviors);
  211. updateData.call(this);
  212. processRelations(this, Relations);
  213. this.selectComponentApp.connect();
  214. this.selectOwnerComponent = processSelectOwnerComponent.bind(this);
  215. this.getPageId = processGetPageId.bind(this);
  216. addAntmoveData.call(this);
  217. if (typeof this.triggerEvent !== 'function') {
  218. processTriggerEvent.call(this);
  219. }
  220. processObservers.call(this, _opts.observersObj, options, _opts);
  221. observerHandle(_opts.observerObj, _opts, this, true);
  222. });
  223. fnApp.bind('onInit', _opts);
  224. if (_opts.lifetimes && _opts.lifetimes.attached) {
  225. fnApp.add('didMount', _opts.lifetimes.attached);
  226. } else {
  227. fnApp.add('didMount', _opts.attached);
  228. }
  229. if (_opts.pageLifetimes && _opts.pageLifetimes.show) {
  230. fnApp.add('didMount', _opts.pageLifetimes.show);
  231. }
  232. fnApp.add('didMount', _opts.ready || _opts.lifetimes && _opts.lifetimes.ready);
  233. var didUpdate = function didUpdate() {
  234. for (var _len = arguments.length, param = new Array(_len), _key = 0; _key < _len; _key++) {
  235. param[_key] = arguments[_key];
  236. }
  237. updateData.call(this, param);
  238. processObservers.call(this, _opts.observersObj, options, this.$antmove._data);
  239. observerHandle(_opts.observerObj, this.$antmove._data, this);
  240. addAntmoveData.call(this);
  241. };
  242. fnApp.add('didUpdate', didUpdate);
  243. fnApp.add('didUpdate', function () {
  244. handleAfterInit.call(this);
  245. });
  246. fnApp.bind('deriveDataFromProps', _opts);
  247. fnApp.bind('didUpdate', _opts);
  248. fnApp.bind('didMount', _opts);
  249. if (_opts.lifetimes && _opts.lifetimes.detached) {
  250. fnApp.add('didUnmount', _opts.lifetimes.detached);
  251. } else {
  252. fnApp.add('didUnmount', options.detached);
  253. }
  254. fnApp.add('didUnmount', function () {
  255. // todo: 暂时这样处理使其不报错
  256. if (this.$node && this.$node.$parent) {
  257. this.$node.$parent.removeChild(this.$node);
  258. var refId = this.$node.$relationNode.$id;
  259. this.$antmove[refId]--;
  260. }
  261. });
  262. fnApp.bind('didUnmount', _opts);
  263. }
  264. };
  265. function addAntmoveData() {
  266. var _data = [{}, {}];
  267. var ctx = this;
  268. var _props = {};
  269. for (var i in ctx.properties) {
  270. if (ctx.properties.hasOwnProperty(i)) {
  271. _props[i] = ctx.data[i];
  272. }
  273. }
  274. _data[0] = copy(_props);
  275. _data[1] = copy(ctx.data);
  276. this.$antmove = this.$antmove || {};
  277. this.$antmove._data = _data;
  278. }
  279. /**
  280. * selectOwnerComponent
  281. */
  282. function processSelectOwnerComponent() {
  283. var node = this.$node;
  284. if (node && node.$parent && node.$parent.$self) {
  285. return node.$parent.$self;
  286. }
  287. return {};
  288. }
  289. /**
  290. * getPageId
  291. */
  292. function processGetPageId() {
  293. if (this.$page) {
  294. return "pageId:".concat(this.$page.$id);
  295. }
  296. return 'pageId: undefined';
  297. }
  298. /**
  299. * behavior
  300. */
  301. function processBehavior() {
  302. var _opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  303. var opts = arguments.length > 1 ? arguments[1] : undefined;
  304. var $behaviors = arguments.length > 2 ? arguments[2] : undefined;
  305. var self = this;
  306. if (Array.isArray(opts)) {
  307. opts.forEach(function (item) {
  308. if (_typeof(item) === 'object' && ($behaviors.indexOf(item.$id) === -1 || item.$id === undefined)) {
  309. $behaviors.push(item.$id);
  310. _process.call(self, _opts, item);
  311. }
  312. });
  313. } else if (_typeof(opts) === 'object' && $behaviors.indexOf(opts.$id) === -1) {
  314. $behaviors.push(opts.$id);
  315. _process.call(self, _opts, opts);
  316. }
  317. function _process() {
  318. var __opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  319. var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  320. if (opt.behaviors) {
  321. processBehavior.call(self, __opts, opt.behaviors, $behaviors);
  322. delete opt.behaviors;
  323. }
  324. if (opt.mixins) {
  325. processBehavior(__opts, opt.mixins, $behaviors);
  326. delete opt.mixins;
  327. }
  328. mergeOptions(opt, __opts);
  329. }
  330. }
  331. function processBehaviorId(behavior) {
  332. if (Array.isArray(behavior)) {
  333. behavior.forEach(function (item) {
  334. if (_typeof(item) === 'object' && item.$id) {
  335. delete item.$id;
  336. }
  337. });
  338. } else if (_typeof(behavior) === 'object' && behavior.$id) {
  339. delete behavior.$id;
  340. }
  341. }
  342. function processComponentExport(_export, behaviors, self) {
  343. if (typeof _export !== 'function') {
  344. return;
  345. }
  346. if (Array.isArray(behaviors)) {
  347. behaviors.forEach(function (bhv) {
  348. if (bhv === 'wx://component-export') {
  349. self._this = _export();
  350. }
  351. });
  352. } else if (behaviors === 'wx://component-export') {
  353. self._this = _export();
  354. }
  355. }
  356. function testBehaviors(behaviors) {
  357. if (Array.isArray(behaviors)) {
  358. behaviors.forEach(function (bhv) {
  359. if (bhv === 'wx://form-field') {
  360. warnLife('Wx://form-field in built-in behavior is not supported', 'behavior/form-field');
  361. }
  362. });
  363. } else if (behaviors === 'wx://form-field') {
  364. warnLife('Wx://form-field in built-in behavior is not supported', 'behavior/form-field');
  365. }
  366. }