weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
247 lines (211 loc) • 8.99 kB
JavaScript
/** @jsx createElement */
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _rax = require('rax');
var _nukeEnv = require('nuke-env');
var _nukeScrollView = require('nuke-scroll-view');
var _nukeScrollView2 = _interopRequireDefault(_nukeScrollView);
var _nukeRefreshControl = require('nuke-refresh-control');
var _nukeRefreshControl2 = _interopRequireDefault(_nukeRefreshControl);
var _nukeCell = require('nuke-cell');
var _nukeCell2 = _interopRequireDefault(_nukeCell);
var _nukeHeader = require('nuke-header');
var _nukeHeader2 = _interopRequireDefault(_nukeHeader);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* ListView
* @description 列表
*/
var ListView = function (_Component) {
_inherits(ListView, _Component);
function ListView() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, ListView);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ListView.__proto__ || Object.getPrototypeOf(ListView)).call.apply(_ref, [this].concat(args))), _this), _this.resetLoadmore = function () {
_this.refs.list.resetLoadmore && _this.refs.list.resetLoadmore();
}, _this.scrollTo = function (options) {
var x = parseInt(options.x);
var y = parseInt(options.y);
if (_nukeEnv.isWeex) {
var dom = require('@weex-module/dom');
var firstCell = _this.hasHeader ? (0, _rax.findDOMNode)(_this.refs.cell1) : (0, _rax.findDOMNode)(_this.refs.cell0);
dom.scrollToElement(firstCell.ref, {
offset: x || y || 0
});
} else {
_this.refs.list.scrollTo(options);
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(ListView, [{
key: 'getChildContext',
value: function getChildContext() {
return {
isInARecyclerView: true
};
}
}, {
key: 'getChidren',
value: function getChidren() {
var _props = this.props,
children = _props.children,
renderHeader = _props.renderHeader,
renderFooter = _props.renderFooter,
renderRow = _props.renderRow,
_props$dataSource = _props.dataSource,
dataSource = _props$dataSource === undefined ? [] : _props$dataSource;
if (children) {
return !Array.isArray(children) ? [children] : children;
}
var header = typeof renderHeader === 'function' ? renderHeader() : null;
var footer = typeof renderFooter === 'function' ? renderFooter() : null;
var body = dataSource.map(function (i, index) {
return renderRow(i, index);
});
return [].concat(header, body, footer);
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var _props2 = this.props,
onEndReached = _props2.onEndReached,
onEndReachedThreshold = _props2.onEndReachedThreshold,
id = _props2.id,
style = _props2.style,
showScrollbar = _props2.showScrollbar,
_keepScrollPosition = _props2._keepScrollPosition,
_autoWrapCell = _props2._autoWrapCell,
renderRow = _props2.renderRow,
dataSource = _props2.dataSource,
renderHeader = _props2.renderHeader,
renderFooter = _props2.renderFooter,
others = _objectWithoutProperties(_props2, ['onEndReached', 'onEndReachedThreshold', 'id', 'style', 'showScrollbar', '_keepScrollPosition', '_autoWrapCell', 'renderRow', 'dataSource', 'renderHeader', 'renderFooter']);
var children = this.getChidren();
var cells = children.map(function (child, index) {
var ref = 'cell' + index;
if (child && child.type === _nukeRefreshControl2.default) {
_this2.hasHeader = true;
}
if (child) {
if (_autoWrapCell && child.type != _nukeRefreshControl2.default) {
if (child.key) {
return (0, _rax.createElement)(
_nukeCell2.default,
{
keepScrollPosition: _keepScrollPosition,
ref: ref,
key: child.key
},
child
);
}
return (0, _rax.createElement)(
_nukeCell2.default,
{ ref: ref },
child
);
}
return (0, _rax.cloneElement)(child, { ref: ref });
}
return (0, _rax.createElement)(_nukeCell2.default, { ref: ref });
});
if (_nukeEnv.isWeex) {
return (0, _rax.createElement)(
'list',
_extends({
id: id,
ref: 'list',
style: style,
onLoadmore: onEndReached,
loadmoreretry: true,
loadmoreoffset: onEndReachedThreshold,
showScrollbar: showScrollbar
}, others),
cells
);
}
return (0, _rax.createElement)(
_nukeScrollView2.default,
_extends({
id: id,
style: style,
onEndReached: onEndReached,
onEndReachedThreshold: onEndReachedThreshold,
showScrollbar: showScrollbar
}, others),
cells
);
}
}]);
return ListView;
}(_rax.Component);
ListView.childContextTypes = {
isInARecyclerView: _rax.PropTypes.bool
};
ListView.propTypes = {
/**
* 列表底部加载更多的回调函数 onEndReached callback function
*/
onEndReached: _rax.PropTypes.func,
/**
* 列表到底部触发加载更多的位移量 offset of onEndReached been triggered
*/
onEndReachedThreshold: _rax.PropTypes.num,
id: _rax.PropTypes.string,
/**
* 列表外层容器样式 style of list
*/
style: _rax.PropTypes.any,
/**
* 是否显示滚动条 show scrollbar or not when scrolling
*/
showScrollbar: _rax.PropTypes.boolean,
/**
* 是否保持上一次滚动位置 keep last scroll position
*/
_keepScrollPosition: _rax.PropTypes.boolean,
/**
* 是否自动包裹 cell 标签,仅用于非数据源模式 wrap cell tag or not
*/
_autoWrapCell: _rax.PropTypes.boolean,
/**
* 渲染数据的回调函数 render list row function
*/
renderRow: _rax.PropTypes.func,
/**
* 数据源 data souce of the list
*/
dataSource: _rax.PropTypes.array,
/**
* 渲染头部的回调函数 render list header function
*/
renderHeader: _rax.PropTypes.func,
/**
* 渲染尾的回调函数 render list footer function
*/
renderFooter: _rax.PropTypes.func
};
ListView.defaultProps = {
onEndReachedThreshold: 500,
_keepScrollPosition: false,
_autoWrapCell: true,
showScrollbar: false
};
ListView.RefreshControl = _nukeRefreshControl2.default;
ListView.Cell = _nukeCell2.default;
ListView.Header = _nukeHeader2.default;
exports.default = ListView;
module.exports = exports['default'];