提交 b76374b7 编写于 作者: fxy060608's avatar fxy060608

fix(mp): add global method

上级 45940f56
import { capitalize, hasOwn, isArray, toNumber, isPlainObject, isObject, isFunction, extend, NOOP, EMPTY_OBJ, camelize } from '@vue/shared';
import { hasOwn, isArray, toNumber, isPlainObject, isObject, capitalize, isFunction, extend, NOOP, EMPTY_OBJ, camelize } from '@vue/shared';
function setModel(target, key, value, modifiers) {
if (isArray(modifiers)) {
......@@ -50,7 +50,7 @@ const MP_METHODS = [
'createSelectorQuery',
'createIntersectionObserver',
'selectAllComponents',
'selectComponent'
'selectComponent',
];
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
......@@ -85,7 +85,7 @@ function initBaseInstance(instance, options) {
Object.defineProperty(instance, 'slots', {
get() {
return this.$scope && this.$scope.props.$slots;
}
},
});
}
// $emit
......@@ -94,7 +94,7 @@ function initBaseInstance(instance, options) {
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
const ctx = instance.ctx;
MP_METHODS.forEach(method => {
MP_METHODS.forEach((method) => {
ctx[method] = function (...args) {
const mpInstance = ctx.$scope;
if (mpInstance && mpInstance[method]) {
......@@ -115,31 +115,71 @@ function initComponentInstance(instance, options) {
}
function initMocks(instance, mpInstance, mocks) {
const ctx = instance.ctx;
mocks.forEach(mock => {
mocks.forEach((mock) => {
if (hasOwn(mpInstance, mock)) {
ctx[mock] = mpInstance[mock];
}
});
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_BACK_PRESS = 'onBackPress';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [
'onLoad',
'onShow',
ON_LOAD,
ON_SHOW,
ON_HIDE,
ON_UNLOAD,
ON_RESIZE,
ON_TAB_ITEM_TAP,
ON_REACH_BOTTOM,
ON_PULL_DOWN_REFRESH,
ON_ADD_TO_FAVORITES,
// 'onReady', // lifetimes.ready
'onHide',
'onUnload',
'onResize',
// 'onPageScroll', // 影响性能,开发者手动注册
'onTabItemTap',
'onReachBottom',
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites'
// 'onShareAppMessage' // 右上角菜单,开发者手动注册
];
function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) {
Object.keys(vueOptions).forEach(name => {
Object.keys(vueOptions).forEach((name) => {
if (name.indexOf('on') === 0 && isFunction(vueOptions[name])) {
hooks.add(name);
}
......@@ -147,7 +187,7 @@ function findHooks(vueOptions, hooks = new Set()) {
if (__VUE_OPTIONS_API__) {
const { extends: extendsOptions, mixins } = vueOptions;
if (mixins) {
mixins.forEach(mixin => findHooks(mixin, hooks));
mixins.forEach((mixin) => findHooks(mixin, hooks));
}
if (extendsOptions) {
findHooks(extendsOptions, hooks);
......@@ -163,21 +203,21 @@ function initHook(mpOptions, hook, excludes) {
};
}
}
const EXCLUDE_HOOKS = ['onReady'];
const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach(hook => initHook(mpOptions, hook, excludes));
hooks.forEach((hook) => initHook(mpOptions, hook, excludes));
}
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach(hook => initHook(mpOptions, hook, excludes));
findHooks(vueOptions).forEach((hook) => initHook(mpOptions, hook, excludes));
}
const HOOKS = [
'onShow',
'onHide',
'onError',
'onThemeChange',
'onPageNotFound',
'onUnhandledRejection'
ON_SHOW,
ON_HIDE,
ON_ERROR,
ON_THEME_CHANGE,
ON_PAGE_NOT_FOUND,
ON_UNHANDLE_REJECTION,
];
function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$;
......@@ -193,11 +233,11 @@ function parseApp(instance, parseAppOptions) {
initBaseInstance(internalInstance, {
mpType: 'app',
mpInstance: this,
slots: []
slots: [],
});
ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options);
}
instance.$callHook(ON_LAUNCH, options);
},
};
const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS);
......@@ -217,26 +257,6 @@ function initCreateApp(parseAppOptions) {
};
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map(key => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter(x => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initVueIds(vueIds, mpInstance) {
if (!vueIds) {
return;
......@@ -294,7 +314,7 @@ function initDefaultProps(isBehavior = false) {
if (!isBehavior) {
properties.vueId = {
type: String,
value: ''
value: '',
};
// 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
properties.vueSlots = {
......@@ -306,9 +326,9 @@ function initDefaultProps(isBehavior = false) {
$slots[slotName] = true;
});
this.setData({
$slots
$slots,
});
}
},
};
}
return properties;
......@@ -321,14 +341,14 @@ function createProperty(key, prop) {
function initProps(mpComponentOptions, rawProps, isBehavior = false) {
const properties = initDefaultProps(isBehavior);
if (isArray(rawProps)) {
rawProps.forEach(key => {
rawProps.forEach((key) => {
properties[key] = createProperty(key, {
type: null
type: null,
});
});
}
else if (isPlainObject(rawProps)) {
Object.keys(rawProps).forEach(key => {
Object.keys(rawProps).forEach((key) => {
const opts = rawProps[key];
if (isPlainObject(opts)) {
// title:{type:String,default:''}
......@@ -340,14 +360,14 @@ function initProps(mpComponentOptions, rawProps, isBehavior = false) {
opts.type = parsePropType(key, type);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
value
value,
});
}
else {
// content:String
const type = parsePropType(key, opts);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
});
}
});
......@@ -359,8 +379,7 @@ function initData(vueOptions) {
let data = vueOptions.data || {};
if (typeof data === 'function') {
try {
const appConfig = getApp().$vm.$.appContext
.config;
const appConfig = getApp().$vm.$.appContext.config;
data = data.call(appConfig.globalProperties);
}
catch (e) {
......@@ -391,7 +410,7 @@ function initBehaviors(vueOptions, initBehavior) {
}
const behaviors = [];
if (isArray(vueBehaviors)) {
vueBehaviors.forEach(behavior => {
vueBehaviors.forEach((behavior) => {
behaviors.push(behavior.replace('uni://', `${__PLATFORM_PREFIX__}://`));
if (behavior === 'uni://form-field') {
if (isArray(vueProps)) {
......@@ -401,24 +420,24 @@ function initBehaviors(vueOptions, initBehavior) {
else {
vueProps.name = {
type: String,
default: ''
default: '',
};
vueProps.value = {
type: [String, Number, Boolean, Array, Object, Date],
default: ''
default: '',
};
}
}
});
}
if (isPlainObject(vueExtends) && vueExtends.props) {
if (vueExtends.props) {
const behavior = {};
initProps(behavior, vueExtends.props, true);
behaviors.push(initBehavior(behavior));
}
if (isArray(vueMixins)) {
vueMixins.forEach(vueMixin => {
if (isPlainObject(vueMixin) && vueMixin.props) {
vueMixins.forEach((vueMixin) => {
if (vueMixin.props) {
const behavior = {};
initProps(behavior, vueMixin.props, true);
behaviors.push(initBehavior(behavior));
......@@ -442,7 +461,7 @@ function getValue(obj, path) {
}
function getExtraValue(instance, dataPathsArray) {
let context = instance;
dataPathsArray.forEach(dataPathArray => {
dataPathsArray.forEach((dataPathArray) => {
const dataPath = dataPathArray[0];
const value = dataPathArray[2];
if (dataPath || typeof value !== 'undefined') {
......@@ -472,12 +491,12 @@ function getExtraValue(instance, dataPathsArray) {
}
else {
if (isArray(vFor)) {
context = vFor.find(vForItem => {
context = vFor.find((vForItem) => {
return getValue(vForItem, propPath) === value;
});
}
else if (isPlainObject(vFor)) {
context = Object.keys(vFor).find(vForKey => {
context = Object.keys(vFor).find((vForKey) => {
return getValue(vFor[vForKey], propPath) === value;
});
}
......@@ -567,7 +586,7 @@ function processEventArgs(instance, event, args = [], extra = [], isCustom, meth
}
const extraObj = processEventExtra(instance, extra, event);
const ret = [];
args.forEach(arg => {
args.forEach((arg) => {
if (arg === '$event') {
if (methodName === '__set_model' && !isCustom) {
// input v-model value
......@@ -609,7 +628,7 @@ function wrapper(event) {
event.detail.markerId = event.markerId;
}
if (isPlainObject(event.detail)) {
event.target = Object.assign({}, event.target, event.detail);
event.target = extend({}, event.target, event.detail);
}
return event;
}
......@@ -719,7 +738,7 @@ function onAliGetAuthorize(method, $event) {
$event.type = 'getphonenumber';
$event.detail.errMsg = 'getPhoneNumber:fail';
this[method]($event);
}
},
});
}
function parse(appOptions) {
......@@ -742,7 +761,7 @@ var parseAppOptions = /*#__PURE__*/Object.freeze({
parse: parse
});
function handleLink(event) {
function handleLink$1(event) {
// detail 是微信,value 是百度(dipatch)
const detail = (event.detail ||
event.value);
......@@ -815,11 +834,11 @@ function customize(str) {
}
function initBehavior({ properties }) {
const props = {};
Object.keys(properties).forEach(key => {
Object.keys(properties).forEach((key) => {
props[key] = properties[key].value;
});
return {
props
props,
};
}
function initRelation(mpInstance, detail) {
......@@ -859,10 +878,10 @@ function initChildVues(mpInstance) {
}
const childVues = mpInstance._$childVues;
if (childVues) {
childVues.forEach(relationOptions => {
childVues.forEach((relationOptions) => {
// 父子关系
handleLink.call(mpInstance, {
detail: relationOptions
handleLink$1.call(mpInstance, {
detail: relationOptions,
});
const { mpInstance: childMPInstance, createComponent } = relationOptions;
childMPInstance.$vm = createComponent(relationOptions.parent);
......@@ -872,7 +891,7 @@ function initChildVues(mpInstance) {
}
initChildVues(childMPInstance);
childMPInstance.$vm.$callHook('mounted');
childMPInstance.$vm.$callHook('onReady');
childMPInstance.$vm.$callHook(ON_READY);
});
}
delete mpInstance._$childVues;
......@@ -904,14 +923,14 @@ function triggerEvent(type, detail) {
const eventOpts = this.props['data-event-opts'];
const target = {
dataset: {
eventOpts
}
eventOpts,
},
};
handler({
type: customize(type),
target,
currentTarget: target,
detail
detail,
});
}
const IGNORES = ['$slots', '$scopedSlots'];
......@@ -922,7 +941,7 @@ function createObserver(isDidUpdate = false) {
if (equal(prevProps, nextProps)) {
return;
}
Object.keys(prevProps).forEach(name => {
Object.keys(prevProps).forEach((name) => {
if (IGNORES.indexOf(name) === -1) {
const prevValue = prevProps[name];
const nextValue = nextProps[name];
......@@ -935,19 +954,19 @@ function createObserver(isDidUpdate = false) {
});
};
}
const handleLink$1 = (function () {
const handleLink = (function () {
if (isComponent2) {
return function handleLink$1(detail) {
return handleLink.call(this, {
detail
return function handleLink(detail) {
return handleLink$1.call(this, {
detail,
});
};
}
return function handleLink$1(detail) {
return function handleLink(detail) {
if (this.$vm && this.$vm.$.isMounted) {
// 父已初始化
return handleLink.call(this, {
detail
return handleLink$1.call(this, {
detail,
});
}
(this._$childVues || (this._$childVues = [])).unshift(detail);
......@@ -956,7 +975,7 @@ const handleLink$1 = (function () {
function createVueComponent(mpType, mpInstance, vueOptions, parent) {
return $createComponent({
type: vueOptions,
props: mpInstance.props
props: mpInstance.props,
}, {
mpType,
mpInstance,
......@@ -964,43 +983,43 @@ function createVueComponent(mpType, mpInstance, vueOptions, parent) {
onBeforeSetup(instance, options) {
initMocks(instance, mpInstance, mocks);
initComponentInstance(instance, options);
}
},
});
}
function createPage(vueOptions) {
function createPage$1(vueOptions) {
vueOptions = vueOptions.default || vueOptions;
const pageOptions = {
onLoad(query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
fullPath: '/' + this.route + stringifyQuery(query),
};
// 初始化 vue 实例
this.$vm = createVueComponent('page', this, vueOptions);
initSpecialMethods(this);
this.$vm.$callHook('onLoad', query);
this.$vm.$callHook(ON_LOAD, query);
},
onReady() {
initChildVues(this);
this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady');
this.$vm.$callHook(ON_READY);
},
onUnload() {
if (this.$vm) {
this.$vm.$callHook('onUnload');
this.$vm.$callHook(ON_UNLOAD);
$destroyComponent(this.$vm);
}
},
events: {
// 支付宝小程序有些页面事件只能放在events下
onBack() {
this.$vm.$callHook('onBackPress');
}
this.$vm.$callHook(ON_BACK_PRESS);
},
},
__r: handleRef,
__e: handleEvent,
__l: handleLink$1
__l: handleLink,
};
if (__VUE_OPTIONS_API__) {
pageOptions.data = initData(vueOptions);
......@@ -1013,14 +1032,14 @@ function createPage(vueOptions) {
function initComponentProps(rawProps) {
const propertiesOptions = {
properties: {}
properties: {},
};
initProps(propertiesOptions, rawProps, false);
const properties = propertiesOptions.properties;
const props = {
onVueInit: function () { }
onVueInit: function () { },
};
Object.keys(properties).forEach(key => {
Object.keys(properties).forEach((key) => {
if (key !== 'vueSlots') {
props[key] = properties[key].value;
}
......@@ -1036,7 +1055,7 @@ function initVm(mpInstance, createComponent) {
const relationOptions = {
vuePid: mpInstance._$vuePid,
mpInstance,
createComponent
createComponent,
};
if (isComponent2) {
// 处理父子关系
......@@ -1057,7 +1076,7 @@ function initVm(mpInstance, createComponent) {
}
}
}
function createComponent(vueOptions) {
function createComponent$1(vueOptions) {
vueOptions = vueOptions.default || vueOptions;
const mpComponentOptions = {
props: initComponentProps(vueOptions.props),
......@@ -1085,9 +1104,9 @@ function createComponent(vueOptions) {
methods: {
__r: handleRef,
__e: handleEvent,
__l: handleLink$1,
triggerEvent
}
__l: handleLink,
triggerEvent,
},
};
if (__VUE_OPTIONS_API__) {
mpComponentOptions.data = initData(vueOptions);
......@@ -1108,6 +1127,9 @@ function createComponent(vueOptions) {
return Component(mpComponentOptions);
}
const createApp = initCreateApp(parseAppOptions);
const createApp = initCreateApp(parseAppOptions);
my.createApp = createApp;
my.createPage = createPage;
my.createComponent = createComponent;
export { createApp, createComponent, createPage };
export { createApp, createComponent$1 as createComponent, createPage$1 as createPage };
......@@ -50,7 +50,7 @@ const MP_METHODS = [
'createSelectorQuery',
'createIntersectionObserver',
'selectAllComponents',
'selectComponent'
'selectComponent',
];
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
......@@ -77,7 +77,7 @@ function initBaseInstance(instance, options) {
{
instance.slots = {};
if (isArray(options.slots) && options.slots.length) {
options.slots.forEach(name => {
options.slots.forEach((name) => {
instance.slots[name] = true;
});
}
......@@ -88,7 +88,7 @@ function initBaseInstance(instance, options) {
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
const ctx = instance.ctx;
MP_METHODS.forEach(method => {
MP_METHODS.forEach((method) => {
ctx[method] = function (...args) {
const mpInstance = ctx.$scope;
if (mpInstance && mpInstance[method]) {
......@@ -106,31 +106,70 @@ function initComponentInstance(instance, options) {
}
function initMocks(instance, mpInstance, mocks) {
const ctx = instance.ctx;
mocks.forEach(mock => {
mocks.forEach((mock) => {
if (hasOwn(mpInstance, mock)) {
ctx[mock] = mpInstance[mock];
}
});
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [
'onLoad',
'onShow',
ON_LOAD,
ON_SHOW,
ON_HIDE,
ON_UNLOAD,
ON_RESIZE,
ON_TAB_ITEM_TAP,
ON_REACH_BOTTOM,
ON_PULL_DOWN_REFRESH,
ON_ADD_TO_FAVORITES,
// 'onReady', // lifetimes.ready
'onHide',
'onUnload',
'onResize',
// 'onPageScroll', // 影响性能,开发者手动注册
'onTabItemTap',
'onReachBottom',
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites'
// 'onShareAppMessage' // 右上角菜单,开发者手动注册
];
function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) {
Object.keys(vueOptions).forEach(name => {
Object.keys(vueOptions).forEach((name) => {
if (name.indexOf('on') === 0 && isFunction(vueOptions[name])) {
hooks.add(name);
}
......@@ -138,7 +177,7 @@ function findHooks(vueOptions, hooks = new Set()) {
if (__VUE_OPTIONS_API__) {
const { extends: extendsOptions, mixins } = vueOptions;
if (mixins) {
mixins.forEach(mixin => findHooks(mixin, hooks));
mixins.forEach((mixin) => findHooks(mixin, hooks));
}
if (extendsOptions) {
findHooks(extendsOptions, hooks);
......@@ -147,28 +186,28 @@ function findHooks(vueOptions, hooks = new Set()) {
}
return hooks;
}
function initHook(mpOptions, hook, excludes) {
function initHook$1(mpOptions, hook, excludes) {
if (excludes.indexOf(hook) === -1 && !hasOwn(mpOptions, hook)) {
mpOptions[hook] = function (args) {
return this.$vm && this.$vm.$callHook(hook, args);
};
}
}
const EXCLUDE_HOOKS = ['onReady'];
const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach(hook => initHook(mpOptions, hook, excludes));
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach(hook => initHook(mpOptions, hook, excludes));
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
const HOOKS = [
'onShow',
'onHide',
'onError',
'onThemeChange',
'onPageNotFound',
'onUnhandledRejection'
ON_SHOW,
ON_HIDE,
ON_ERROR,
ON_THEME_CHANGE,
ON_PAGE_NOT_FOUND,
ON_UNHANDLE_REJECTION,
];
function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$;
......@@ -184,11 +223,11 @@ function parseApp(instance, parseAppOptions) {
initBaseInstance(internalInstance, {
mpType: 'app',
mpInstance: this,
slots: []
slots: [],
});
ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options);
}
instance.$callHook(ON_LAUNCH, options);
},
};
const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS);
......@@ -208,26 +247,6 @@ function initCreateApp(parseAppOptions) {
};
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map(key => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter(x => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) {
return Behavior(options);
}
......@@ -247,7 +266,7 @@ function initVueIds(vueIds, mpInstance) {
}
const EXTRAS = ['externalClasses'];
function initExtraOptions(miniProgramComponentOptions, vueOptions) {
EXTRAS.forEach(name => {
EXTRAS.forEach((name) => {
if (hasOwn(vueOptions, name)) {
miniProgramComponentOptions[name] = vueOptions[name];
}
......@@ -268,12 +287,12 @@ function initRefs(instance, mpInstance) {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach(component => {
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach(component => {
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
$refs[ref] = [];
......@@ -281,7 +300,7 @@ function initRefs(instance, mpInstance) {
$refs[ref].push(component.$vm || component);
});
return $refs;
}
},
});
}
function findVmByVueId(instance, vuePid) {
......@@ -335,7 +354,7 @@ function initDefaultProps(isBehavior = false) {
if (!isBehavior) {
properties.vueId = {
type: String,
value: ''
value: '',
};
// 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
properties.vueSlots = {
......@@ -347,9 +366,9 @@ function initDefaultProps(isBehavior = false) {
$slots[slotName] = true;
});
this.setData({
$slots
$slots,
});
}
},
};
}
return properties;
......@@ -361,14 +380,14 @@ function createProperty(key, prop) {
function initProps(mpComponentOptions, rawProps, isBehavior = false) {
const properties = initDefaultProps(isBehavior);
if (isArray(rawProps)) {
rawProps.forEach(key => {
rawProps.forEach((key) => {
properties[key] = createProperty(key, {
type: null
type: null,
});
});
}
else if (isPlainObject(rawProps)) {
Object.keys(rawProps).forEach(key => {
Object.keys(rawProps).forEach((key) => {
const opts = rawProps[key];
if (isPlainObject(opts)) {
// title:{type:String,default:''}
......@@ -380,14 +399,14 @@ function initProps(mpComponentOptions, rawProps, isBehavior = false) {
opts.type = parsePropType(key, type, value);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
value
value,
});
}
else {
// content:String
const type = parsePropType(key, opts, null);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
});
}
});
......@@ -399,8 +418,7 @@ function initData(vueOptions) {
let data = vueOptions.data || {};
if (typeof data === 'function') {
try {
const appConfig = getApp().$vm.$.appContext
.config;
const appConfig = getApp().$vm.$.appContext.config;
data = data.call(appConfig.globalProperties);
}
catch (e) {
......@@ -431,7 +449,7 @@ function initBehaviors(vueOptions, initBehavior) {
}
const behaviors = [];
if (isArray(vueBehaviors)) {
vueBehaviors.forEach(behavior => {
vueBehaviors.forEach((behavior) => {
behaviors.push(behavior.replace('uni://', `${__PLATFORM_PREFIX__}://`));
if (behavior === 'uni://form-field') {
if (isArray(vueProps)) {
......@@ -441,24 +459,24 @@ function initBehaviors(vueOptions, initBehavior) {
else {
vueProps.name = {
type: String,
default: ''
default: '',
};
vueProps.value = {
type: [String, Number, Boolean, Array, Object, Date],
default: ''
default: '',
};
}
}
});
}
if (isPlainObject(vueExtends) && vueExtends.props) {
if (vueExtends.props) {
const behavior = {};
initProps(behavior, vueExtends.props, true);
behaviors.push(initBehavior(behavior));
}
if (isArray(vueMixins)) {
vueMixins.forEach(vueMixin => {
if (isPlainObject(vueMixin) && vueMixin.props) {
vueMixins.forEach((vueMixin) => {
if (vueMixin.props) {
const behavior = {};
initProps(behavior, vueMixin.props, true);
behaviors.push(initBehavior(behavior));
......@@ -486,7 +504,7 @@ function getValue(obj, path) {
}
function getExtraValue(instance, dataPathsArray) {
let context = instance;
dataPathsArray.forEach(dataPathArray => {
dataPathsArray.forEach((dataPathArray) => {
const dataPath = dataPathArray[0];
const value = dataPathArray[2];
if (dataPath || typeof value !== 'undefined') {
......@@ -516,12 +534,12 @@ function getExtraValue(instance, dataPathsArray) {
}
else {
if (isArray(vFor)) {
context = vFor.find(vForItem => {
context = vFor.find((vForItem) => {
return getValue(vForItem, propPath) === value;
});
}
else if (isPlainObject(vFor)) {
context = Object.keys(vFor).find(vForKey => {
context = Object.keys(vFor).find((vForKey) => {
return getValue(vFor[vForKey], propPath) === value;
});
}
......@@ -611,7 +629,7 @@ function processEventArgs(instance, event, args = [], extra = [], isCustom, meth
}
const extraObj = processEventExtra(instance, extra, event);
const ret = [];
args.forEach(arg => {
args.forEach((arg) => {
if (arg === '$event') {
if (methodName === '__set_model' && !isCustom) {
// input v-model value
......@@ -661,7 +679,7 @@ function wrapper(event) {
}
}
if (isPlainObject(event.detail)) {
event.target = Object.assign({}, event.target, event.detail);
event.target = extend({}, event.target, event.detail);
}
return event;
}
......@@ -731,11 +749,11 @@ function handleEvent(event) {
}
}
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes }) {
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes, }) {
vueOptions = vueOptions.default || vueOptions;
const options = {
multipleSlots: true,
addGlobalClass: true
addGlobalClass: true,
};
if (vueOptions.options) {
extend(options, vueOptions.options);
......@@ -752,12 +770,12 @@ function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handle
},
resize(size) {
this.$vm && this.$vm.$callHook('onPageResize', size);
}
},
},
methods: {
__l: handleLink,
__e: handleEvent
}
__e: handleEvent,
},
};
if (__VUE_OPTIONS_API__) {
applyOptions(mpComponentOptions, vueOptions, initBehavior);
......@@ -797,15 +815,15 @@ function parsePage(vueOptions, parseOptions) {
isPage,
initRelation,
handleLink,
initLifetimes
initLifetimes,
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
fullPath: '/' + this.route + stringifyQuery(query),
};
return this.$vm && this.$vm.$callHook('onLoad', query);
return this.$vm && this.$vm.$callHook(ON_LOAD, query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
......@@ -830,7 +848,7 @@ function initTriggerEvent(mpInstance) {
return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]);
};
}
function initHook$1(name, options) {
function initHook(name, options) {
const oldHook = options[name];
if (!oldHook) {
options[name] = function () {
......@@ -845,36 +863,36 @@ function initHook$1(name, options) {
}
}
Page = function (options) {
initHook$1('onLoad', options);
initHook(ON_LOAD, options);
return MPPage(options);
};
Component = function (options) {
initHook$1('created', options);
initHook('created', options);
return MPComponent(options);
};
function parse(appOptions) {
function parse$2(appOptions) {
// 百度 onShow 竟然会在 onLaunch 之前
appOptions.onShow = function onShow(args) {
if (!this.$vm) {
this.onLaunch(args);
}
this.$vm.$callHook('onShow', args);
this.$vm.$callHook(ON_SHOW, args);
};
}
var parseAppOptions = /*#__PURE__*/Object.freeze({
__proto__: null,
parse: parse
parse: parse$2
});
function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
const relationOptions = {
vuePid: this._$vuePid
vuePid: this._$vuePid,
};
// 处理父子关系
initRelation(this, relationOptions);
......@@ -882,7 +900,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
const mpInstance = this;
this.$vm = $createComponent({
type: vueOptions,
props: properties
props: properties,
}, {
mpType: isPage(mpInstance) ? 'page' : 'component',
mpInstance,
......@@ -892,7 +910,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
initRefs(instance, mpInstance);
initMocks(instance, mpInstance, mocks);
initComponentInstance(instance, options);
}
},
});
},
ready() {
......@@ -900,12 +918,12 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
// https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
if (this.$vm) {
this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady');
this.$vm.$callHook(ON_READY);
}
},
detached() {
this.$vm && $destroyComponent(this.$vm);
}
},
};
}
......@@ -947,8 +965,8 @@ function parse$1(componentOptions) {
const pageInstance = this.pageinstance;
pageInstance.$vm = this.$vm;
if (hasOwn(pageInstance, '_$args')) {
this.$vm.$callHook('onLoad', pageInstance._$args);
this.$vm.$callHook('onShow');
this.$vm.$callHook(ON_LOAD, pageInstance._$args);
this.$vm.$callHook(ON_SHOW);
delete pageInstance._$args;
}
}
......@@ -964,7 +982,7 @@ function parse$1(componentOptions) {
delete lifetimes.ready;
}
componentOptions.messages = {
__l: methods.__l
__l: methods.__l,
};
delete methods.__l;
}
......@@ -979,21 +997,21 @@ var parseComponentOptions = /*#__PURE__*/Object.freeze({
initLifetimes: initLifetimes
});
function parse$2(pageOptions) {
function parse(pageOptions) {
parse$1(pageOptions);
const methods = pageOptions.methods;
// 纠正百度小程序生命周期methods:onShow在methods:onLoad之前触发的问题
methods.onShow = function onShow() {
if (this.$vm && this._$loaded) {
this.$vm.$callHook('onShow');
this.$vm.$callHook(ON_SHOW);
}
};
methods.onLoad = function onLoad(args) {
// 百度 onLoad 在 attached 之前触发,先存储 args, 在 attached 里边触发 onLoad
if (this.$vm) {
this._$loaded = true;
this.$vm.$callHook('onLoad', args);
this.$vm.$callHook('onShow');
this.$vm.$callHook(ON_LOAD, args);
this.$vm.$callHook(ON_SHOW);
}
else {
this.pageinstance._$args = args;
......@@ -1003,7 +1021,7 @@ function parse$2(pageOptions) {
var parsePageOptions = /*#__PURE__*/Object.freeze({
__proto__: null,
parse: parse$2,
parse: parse,
handleLink: handleLink,
initLifetimes: initLifetimes,
mocks: mocks,
......@@ -1013,6 +1031,9 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp(parseAppOptions);
const createPage = initCreatePage(parsePageOptions);
const createComponent = initCreateComponent(parseComponentOptions);
const createComponent = initCreateComponent(parseComponentOptions);
swan.createApp = createApp;
swan.createPage = createPage;
swan.createComponent = createComponent;
export { createApp, createComponent, createPage };
......@@ -50,7 +50,7 @@ const MP_METHODS = [
'createSelectorQuery',
'createIntersectionObserver',
'selectAllComponents',
'selectComponent'
'selectComponent',
];
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
......@@ -77,7 +77,7 @@ function initBaseInstance(instance, options) {
{
instance.slots = {};
if (isArray(options.slots) && options.slots.length) {
options.slots.forEach(name => {
options.slots.forEach((name) => {
instance.slots[name] = true;
});
}
......@@ -88,7 +88,7 @@ function initBaseInstance(instance, options) {
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
const ctx = instance.ctx;
MP_METHODS.forEach(method => {
MP_METHODS.forEach((method) => {
ctx[method] = function (...args) {
const mpInstance = ctx.$scope;
if (mpInstance && mpInstance[method]) {
......@@ -106,31 +106,70 @@ function initComponentInstance(instance, options) {
}
function initMocks(instance, mpInstance, mocks) {
const ctx = instance.ctx;
mocks.forEach(mock => {
mocks.forEach((mock) => {
if (hasOwn(mpInstance, mock)) {
ctx[mock] = mpInstance[mock];
}
});
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [
'onLoad',
'onShow',
ON_LOAD,
ON_SHOW,
ON_HIDE,
ON_UNLOAD,
ON_RESIZE,
ON_TAB_ITEM_TAP,
ON_REACH_BOTTOM,
ON_PULL_DOWN_REFRESH,
ON_ADD_TO_FAVORITES,
// 'onReady', // lifetimes.ready
'onHide',
'onUnload',
'onResize',
// 'onPageScroll', // 影响性能,开发者手动注册
'onTabItemTap',
'onReachBottom',
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites'
// 'onShareAppMessage' // 右上角菜单,开发者手动注册
];
function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) {
Object.keys(vueOptions).forEach(name => {
Object.keys(vueOptions).forEach((name) => {
if (name.indexOf('on') === 0 && isFunction(vueOptions[name])) {
hooks.add(name);
}
......@@ -138,7 +177,7 @@ function findHooks(vueOptions, hooks = new Set()) {
if (__VUE_OPTIONS_API__) {
const { extends: extendsOptions, mixins } = vueOptions;
if (mixins) {
mixins.forEach(mixin => findHooks(mixin, hooks));
mixins.forEach((mixin) => findHooks(mixin, hooks));
}
if (extendsOptions) {
findHooks(extendsOptions, hooks);
......@@ -147,28 +186,28 @@ function findHooks(vueOptions, hooks = new Set()) {
}
return hooks;
}
function initHook(mpOptions, hook, excludes) {
function initHook$1(mpOptions, hook, excludes) {
if (excludes.indexOf(hook) === -1 && !hasOwn(mpOptions, hook)) {
mpOptions[hook] = function (args) {
return this.$vm && this.$vm.$callHook(hook, args);
};
}
}
const EXCLUDE_HOOKS = ['onReady'];
const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach(hook => initHook(mpOptions, hook, excludes));
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach(hook => initHook(mpOptions, hook, excludes));
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
const HOOKS = [
'onShow',
'onHide',
'onError',
'onThemeChange',
'onPageNotFound',
'onUnhandledRejection'
ON_SHOW,
ON_HIDE,
ON_ERROR,
ON_THEME_CHANGE,
ON_PAGE_NOT_FOUND,
ON_UNHANDLE_REJECTION,
];
function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$;
......@@ -184,11 +223,11 @@ function parseApp(instance, parseAppOptions) {
initBaseInstance(internalInstance, {
mpType: 'app',
mpInstance: this,
slots: []
slots: [],
});
ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options);
}
instance.$callHook(ON_LAUNCH, options);
},
};
const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS);
......@@ -208,26 +247,6 @@ function initCreateApp(parseAppOptions) {
};
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map(key => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter(x => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) {
return Behavior(options);
}
......@@ -247,7 +266,7 @@ function initVueIds(vueIds, mpInstance) {
}
const EXTRAS = ['externalClasses'];
function initExtraOptions(miniProgramComponentOptions, vueOptions) {
EXTRAS.forEach(name => {
EXTRAS.forEach((name) => {
if (hasOwn(vueOptions, name)) {
miniProgramComponentOptions[name] = vueOptions[name];
}
......@@ -268,12 +287,12 @@ function initRefs(instance, mpInstance) {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach(component => {
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach(component => {
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
$refs[ref] = [];
......@@ -281,7 +300,7 @@ function initRefs(instance, mpInstance) {
$refs[ref].push(component.$vm || component);
});
return $refs;
}
},
});
}
function findVmByVueId(instance, vuePid) {
......@@ -324,7 +343,7 @@ function initDefaultProps(isBehavior = false) {
if (!isBehavior) {
properties.vueId = {
type: String,
value: ''
value: '',
};
// 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
properties.vueSlots = {
......@@ -336,9 +355,9 @@ function initDefaultProps(isBehavior = false) {
$slots[slotName] = true;
});
this.setData({
$slots
$slots,
});
}
},
};
}
return properties;
......@@ -350,14 +369,14 @@ function createProperty(key, prop) {
function initProps(mpComponentOptions, rawProps, isBehavior = false) {
const properties = initDefaultProps(isBehavior);
if (isArray(rawProps)) {
rawProps.forEach(key => {
rawProps.forEach((key) => {
properties[key] = createProperty(key, {
type: null
type: null,
});
});
}
else if (isPlainObject(rawProps)) {
Object.keys(rawProps).forEach(key => {
Object.keys(rawProps).forEach((key) => {
const opts = rawProps[key];
if (isPlainObject(opts)) {
// title:{type:String,default:''}
......@@ -369,14 +388,14 @@ function initProps(mpComponentOptions, rawProps, isBehavior = false) {
opts.type = parsePropType(key, type);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
value
value,
});
}
else {
// content:String
const type = parsePropType(key, opts);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
});
}
});
......@@ -388,8 +407,7 @@ function initData(vueOptions) {
let data = vueOptions.data || {};
if (typeof data === 'function') {
try {
const appConfig = getApp().$vm.$.appContext
.config;
const appConfig = getApp().$vm.$.appContext.config;
data = data.call(appConfig.globalProperties);
}
catch (e) {
......@@ -420,7 +438,7 @@ function initBehaviors(vueOptions, initBehavior) {
}
const behaviors = [];
if (isArray(vueBehaviors)) {
vueBehaviors.forEach(behavior => {
vueBehaviors.forEach((behavior) => {
behaviors.push(behavior.replace('uni://', `${__PLATFORM_PREFIX__}://`));
if (behavior === 'uni://form-field') {
if (isArray(vueProps)) {
......@@ -430,24 +448,24 @@ function initBehaviors(vueOptions, initBehavior) {
else {
vueProps.name = {
type: String,
default: ''
default: '',
};
vueProps.value = {
type: [String, Number, Boolean, Array, Object, Date],
default: ''
default: '',
};
}
}
});
}
if (isPlainObject(vueExtends) && vueExtends.props) {
if (vueExtends.props) {
const behavior = {};
initProps(behavior, vueExtends.props, true);
behaviors.push(initBehavior(behavior));
}
if (isArray(vueMixins)) {
vueMixins.forEach(vueMixin => {
if (isPlainObject(vueMixin) && vueMixin.props) {
vueMixins.forEach((vueMixin) => {
if (vueMixin.props) {
const behavior = {};
initProps(behavior, vueMixin.props, true);
behaviors.push(initBehavior(behavior));
......@@ -475,7 +493,7 @@ function getValue(obj, path) {
}
function getExtraValue(instance, dataPathsArray) {
let context = instance;
dataPathsArray.forEach(dataPathArray => {
dataPathsArray.forEach((dataPathArray) => {
const dataPath = dataPathArray[0];
const value = dataPathArray[2];
if (dataPath || typeof value !== 'undefined') {
......@@ -505,12 +523,12 @@ function getExtraValue(instance, dataPathsArray) {
}
else {
if (isArray(vFor)) {
context = vFor.find(vForItem => {
context = vFor.find((vForItem) => {
return getValue(vForItem, propPath) === value;
});
}
else if (isPlainObject(vFor)) {
context = Object.keys(vFor).find(vForKey => {
context = Object.keys(vFor).find((vForKey) => {
return getValue(vFor[vForKey], propPath) === value;
});
}
......@@ -600,7 +618,7 @@ function processEventArgs(instance, event, args = [], extra = [], isCustom, meth
}
const extraObj = processEventExtra(instance, extra, event);
const ret = [];
args.forEach(arg => {
args.forEach((arg) => {
if (arg === '$event') {
if (methodName === '__set_model' && !isCustom) {
// input v-model value
......@@ -642,7 +660,7 @@ function wrapper(event) {
event.detail.markerId = event.markerId;
}
if (isPlainObject(event.detail)) {
event.target = Object.assign({}, event.target, event.detail);
event.target = extend({}, event.target, event.detail);
}
return event;
}
......@@ -712,11 +730,11 @@ function handleEvent(event) {
}
}
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes }) {
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes, }) {
vueOptions = vueOptions.default || vueOptions;
const options = {
multipleSlots: true,
addGlobalClass: true
addGlobalClass: true,
};
if (vueOptions.options) {
extend(options, vueOptions.options);
......@@ -733,12 +751,12 @@ function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handle
},
resize(size) {
this.$vm && this.$vm.$callHook('onPageResize', size);
}
},
},
methods: {
__l: handleLink,
__e: handleEvent
}
__e: handleEvent,
},
};
if (__VUE_OPTIONS_API__) {
applyOptions(mpComponentOptions, vueOptions, initBehavior);
......@@ -778,15 +796,15 @@ function parsePage(vueOptions, parseOptions) {
isPage,
initRelation,
handleLink,
initLifetimes
initLifetimes,
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
fullPath: '/' + this.route + stringifyQuery(query),
};
return this.$vm && this.$vm.$callHook('onLoad', query);
return this.$vm && this.$vm.$callHook(ON_LOAD, query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
......@@ -811,7 +829,7 @@ function initTriggerEvent(mpInstance) {
return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]);
};
}
function initHook$1(name, options) {
function initHook(name, options) {
const oldHook = options[name];
if (!oldHook) {
options[name] = function () {
......@@ -826,21 +844,21 @@ function initHook$1(name, options) {
}
}
Page = function (options) {
initHook$1('onLoad', options);
initHook(ON_LOAD, options);
return MPPage(options);
};
Component = function (options) {
initHook$1('created', options);
initHook('created', options);
return MPComponent(options);
};
function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
const relationOptions = {
vuePid: this._$vuePid
vuePid: this._$vuePid,
};
// 处理父子关系
initRelation(this, relationOptions);
......@@ -848,7 +866,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
const mpInstance = this;
this.$vm = $createComponent({
type: vueOptions,
props: properties
props: properties,
}, {
mpType: isPage(mpInstance) ? 'page' : 'component',
mpInstance,
......@@ -858,7 +876,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
initRefs(instance, mpInstance);
initMocks(instance, mpInstance, mocks);
initComponentInstance(instance, options);
}
},
});
},
ready() {
......@@ -866,12 +884,12 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
// https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
if (this.$vm) {
this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady');
this.$vm.$callHook(ON_READY);
}
},
detached() {
this.$vm && $destroyComponent(this.$vm);
}
},
};
}
......@@ -908,6 +926,13 @@ var parseOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp();
const createPage = initCreatePage(parseOptions);
const createComponent = initCreateComponent(parseOptions);
const createComponent = initCreateComponent(parseOptions);
wx.createApp = createApp;
wx.createPage = createPage;
wx.createComponent = createComponent;
qq.createApp = createApp;
qq.createPage = createPage;
qq.createComponent = createComponent;
export { createApp, createComponent, createPage };
......@@ -50,7 +50,7 @@ const MP_METHODS = [
'createSelectorQuery',
'createIntersectionObserver',
'selectAllComponents',
'selectComponent'
'selectComponent',
];
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
......@@ -77,7 +77,7 @@ function initBaseInstance(instance, options) {
{
instance.slots = {};
if (isArray(options.slots) && options.slots.length) {
options.slots.forEach(name => {
options.slots.forEach((name) => {
instance.slots[name] = true;
});
}
......@@ -88,7 +88,7 @@ function initBaseInstance(instance, options) {
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
const ctx = instance.ctx;
MP_METHODS.forEach(method => {
MP_METHODS.forEach((method) => {
ctx[method] = function (...args) {
const mpInstance = ctx.$scope;
if (mpInstance && mpInstance[method]) {
......@@ -106,31 +106,70 @@ function initComponentInstance(instance, options) {
}
function initMocks(instance, mpInstance, mocks) {
const ctx = instance.ctx;
mocks.forEach(mock => {
mocks.forEach((mock) => {
if (hasOwn(mpInstance, mock)) {
ctx[mock] = mpInstance[mock];
}
});
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [
'onLoad',
'onShow',
ON_LOAD,
ON_SHOW,
ON_HIDE,
ON_UNLOAD,
ON_RESIZE,
ON_TAB_ITEM_TAP,
ON_REACH_BOTTOM,
ON_PULL_DOWN_REFRESH,
ON_ADD_TO_FAVORITES,
// 'onReady', // lifetimes.ready
'onHide',
'onUnload',
'onResize',
// 'onPageScroll', // 影响性能,开发者手动注册
'onTabItemTap',
'onReachBottom',
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites'
// 'onShareAppMessage' // 右上角菜单,开发者手动注册
];
function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) {
Object.keys(vueOptions).forEach(name => {
Object.keys(vueOptions).forEach((name) => {
if (name.indexOf('on') === 0 && isFunction(vueOptions[name])) {
hooks.add(name);
}
......@@ -138,7 +177,7 @@ function findHooks(vueOptions, hooks = new Set()) {
if (__VUE_OPTIONS_API__) {
const { extends: extendsOptions, mixins } = vueOptions;
if (mixins) {
mixins.forEach(mixin => findHooks(mixin, hooks));
mixins.forEach((mixin) => findHooks(mixin, hooks));
}
if (extendsOptions) {
findHooks(extendsOptions, hooks);
......@@ -147,31 +186,31 @@ function findHooks(vueOptions, hooks = new Set()) {
}
return hooks;
}
function initHook(mpOptions, hook, excludes) {
function initHook$1(mpOptions, hook, excludes) {
if (excludes.indexOf(hook) === -1 && !hasOwn(mpOptions, hook)) {
mpOptions[hook] = function (args) {
if ( hook === 'onError') {
if (hook === 'onError') {
return getApp().$vm.$callHook(hook, args);
}
return this.$vm && this.$vm.$callHook(hook, args);
};
}
}
const EXCLUDE_HOOKS = ['onReady'];
const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach(hook => initHook(mpOptions, hook, excludes));
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach(hook => initHook(mpOptions, hook, excludes));
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
const HOOKS = [
'onShow',
'onHide',
'onError',
'onThemeChange',
'onPageNotFound',
'onUnhandledRejection'
ON_SHOW,
ON_HIDE,
ON_ERROR,
ON_THEME_CHANGE,
ON_PAGE_NOT_FOUND,
ON_UNHANDLE_REJECTION,
];
function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$;
......@@ -187,11 +226,11 @@ function parseApp(instance, parseAppOptions) {
initBaseInstance(internalInstance, {
mpType: 'app',
mpInstance: this,
slots: []
slots: [],
});
ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options);
}
instance.$callHook(ON_LAUNCH, options);
},
};
const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS);
......@@ -211,26 +250,6 @@ function initCreateApp(parseAppOptions) {
};
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map(key => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter(x => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) {
return Behavior(options);
}
......@@ -250,7 +269,7 @@ function initVueIds(vueIds, mpInstance) {
}
const EXTRAS = ['externalClasses'];
function initExtraOptions(miniProgramComponentOptions, vueOptions) {
EXTRAS.forEach(name => {
EXTRAS.forEach((name) => {
if (hasOwn(vueOptions, name)) {
miniProgramComponentOptions[name] = vueOptions[name];
}
......@@ -271,12 +290,12 @@ function initRefs(instance, mpInstance) {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach(component => {
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach(component => {
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
$refs[ref] = [];
......@@ -284,7 +303,7 @@ function initRefs(instance, mpInstance) {
$refs[ref].push(component.$vm || component);
});
return $refs;
}
},
});
}
function findVmByVueId(instance, vuePid) {
......@@ -327,12 +346,12 @@ function initDefaultProps(isBehavior = false) {
if (!isBehavior) {
properties.vueId = {
type: String,
value: ''
value: '',
};
{
// 用于字节跳动小程序模拟抽象节点
properties.generic = {
type: Object
type: Object,
};
}
// 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
......@@ -345,9 +364,9 @@ function initDefaultProps(isBehavior = false) {
$slots[slotName] = true;
});
this.setData({
$slots
$slots,
});
}
},
};
}
return properties;
......@@ -359,14 +378,14 @@ function createProperty(key, prop) {
function initProps(mpComponentOptions, rawProps, isBehavior = false) {
const properties = initDefaultProps(isBehavior);
if (isArray(rawProps)) {
rawProps.forEach(key => {
rawProps.forEach((key) => {
properties[key] = createProperty(key, {
type: null
type: null,
});
});
}
else if (isPlainObject(rawProps)) {
Object.keys(rawProps).forEach(key => {
Object.keys(rawProps).forEach((key) => {
const opts = rawProps[key];
if (isPlainObject(opts)) {
// title:{type:String,default:''}
......@@ -378,14 +397,14 @@ function initProps(mpComponentOptions, rawProps, isBehavior = false) {
opts.type = parsePropType(key, type);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
value
value,
});
}
else {
// content:String
const type = parsePropType(key, opts);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
});
}
});
......@@ -397,8 +416,7 @@ function initData(vueOptions) {
let data = vueOptions.data || {};
if (typeof data === 'function') {
try {
const appConfig = getApp().$vm.$.appContext
.config;
const appConfig = getApp().$vm.$.appContext.config;
data = data.call(appConfig.globalProperties);
}
catch (e) {
......@@ -429,7 +447,7 @@ function initBehaviors(vueOptions, initBehavior) {
}
const behaviors = [];
if (isArray(vueBehaviors)) {
vueBehaviors.forEach(behavior => {
vueBehaviors.forEach((behavior) => {
behaviors.push(behavior.replace('uni://', `${__PLATFORM_PREFIX__}://`));
if (behavior === 'uni://form-field') {
if (isArray(vueProps)) {
......@@ -439,24 +457,24 @@ function initBehaviors(vueOptions, initBehavior) {
else {
vueProps.name = {
type: String,
default: ''
default: '',
};
vueProps.value = {
type: [String, Number, Boolean, Array, Object, Date],
default: ''
default: '',
};
}
}
});
}
if (isPlainObject(vueExtends) && vueExtends.props) {
if (vueExtends.props) {
const behavior = {};
initProps(behavior, vueExtends.props, true);
behaviors.push(initBehavior(behavior));
}
if (isArray(vueMixins)) {
vueMixins.forEach(vueMixin => {
if (isPlainObject(vueMixin) && vueMixin.props) {
vueMixins.forEach((vueMixin) => {
if (vueMixin.props) {
const behavior = {};
initProps(behavior, vueMixin.props, true);
behaviors.push(initBehavior(behavior));
......@@ -484,7 +502,7 @@ function getValue(obj, path) {
}
function getExtraValue(instance, dataPathsArray) {
let context = instance;
dataPathsArray.forEach(dataPathArray => {
dataPathsArray.forEach((dataPathArray) => {
const dataPath = dataPathArray[0];
const value = dataPathArray[2];
if (dataPath || typeof value !== 'undefined') {
......@@ -514,12 +532,12 @@ function getExtraValue(instance, dataPathsArray) {
}
else {
if (isArray(vFor)) {
context = vFor.find(vForItem => {
context = vFor.find((vForItem) => {
return getValue(vForItem, propPath) === value;
});
}
else if (isPlainObject(vFor)) {
context = Object.keys(vFor).find(vForKey => {
context = Object.keys(vFor).find((vForKey) => {
return getValue(vFor[vForKey], propPath) === value;
});
}
......@@ -609,7 +627,7 @@ function processEventArgs(instance, event, args = [], extra = [], isCustom, meth
}
const extraObj = processEventExtra(instance, extra, event);
const ret = [];
args.forEach(arg => {
args.forEach((arg) => {
if (arg === '$event') {
if (methodName === '__set_model' && !isCustom) {
// input v-model value
......@@ -651,7 +669,7 @@ function wrapper(event) {
event.detail.markerId = event.markerId;
}
if (isPlainObject(event.detail)) {
event.target = Object.assign({}, event.target, event.detail);
event.target = extend({}, event.target, event.detail);
}
return event;
}
......@@ -721,11 +739,11 @@ function handleEvent(event) {
}
}
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes }) {
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes, }) {
vueOptions = vueOptions.default || vueOptions;
const options = {
multipleSlots: true,
addGlobalClass: true
addGlobalClass: true,
};
if (vueOptions.options) {
extend(options, vueOptions.options);
......@@ -742,12 +760,12 @@ function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handle
},
resize(size) {
this.$vm && this.$vm.$callHook('onPageResize', size);
}
},
},
methods: {
__l: handleLink,
__e: handleEvent
}
__e: handleEvent,
},
};
if (__VUE_OPTIONS_API__) {
applyOptions(mpComponentOptions, vueOptions, initBehavior);
......@@ -787,15 +805,15 @@ function parsePage(vueOptions, parseOptions) {
isPage,
initRelation,
handleLink,
initLifetimes
initLifetimes,
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
fullPath: '/' + this.route + stringifyQuery(query),
};
return this.$vm && this.$vm.$callHook('onLoad', query);
return this.$vm && this.$vm.$callHook(ON_LOAD, query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
......@@ -820,7 +838,7 @@ function initTriggerEvent(mpInstance) {
return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]);
};
}
function initHook$1(name, options) {
function initHook(name, options) {
const oldHook = options[name];
if (!oldHook) {
options[name] = function () {
......@@ -835,11 +853,11 @@ function initHook$1(name, options) {
}
}
Page = function (options) {
initHook$1('onLoad', options);
initHook(ON_LOAD, options);
return MPPage(options);
};
Component = function (options) {
initHook$1('created', options);
initHook('created', options);
return MPComponent(options);
};
......@@ -877,15 +895,22 @@ function initProvide(instance) {
provide(internalInstance, key, provides[key]);
}
}
function inject(instance, key, defaultValue) {
function inject(instance, key, defaultValue, treatDefaultAsFactory = false) {
if (instance) {
const provides = instance.provides;
if (key in provides) {
// #2400
// to support `app.use` plugins,
// fallback to appContext's `provides` if the intance is at root
const provides = instance.parent == null
? instance.vnode.appContext && instance.vnode.appContext.provides
: instance.parent.provides;
if (provides && key in provides) {
// TS doesn't allow symbol as index type
return provides[key];
}
else if (arguments.length > 1) {
return defaultValue;
return treatDefaultAsFactory && isFunction(defaultValue)
? defaultValue()
: defaultValue;
}
else if ((process.env.NODE_ENV !== 'production')) {
console.warn(`injection "${String(key)}" not found.`);
......@@ -912,7 +937,7 @@ function initInjections(instance) {
for (const key in injectOptions) {
const opt = injectOptions[key];
if (isObject(opt)) {
ctx[key] = inject(internalInstance, opt.from, opt.default);
ctx[key] = inject(internalInstance, opt.from || key, opt.default, true /* treat default function as factory */);
}
else {
ctx[key] = inject(internalInstance, opt);
......@@ -921,13 +946,13 @@ function initInjections(instance) {
}
}
function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
const relationOptions = {
vuePid: this._$vuePid
vuePid: this._$vuePid,
};
// 初始化 vue 实例
const mpInstance = this;
......@@ -937,7 +962,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
}
this.$vm = $createComponent({
type: vueOptions,
props: properties
props: properties,
}, {
mpType,
mpInstance,
......@@ -947,14 +972,14 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
initRefs(instance, mpInstance);
initMocks(instance, mpInstance, mocks);
initComponentInstance(instance, options);
}
},
});
// 处理父子关系
initRelation(this, relationOptions);
},
detached() {
this.$vm && $destroyComponent(this.$vm);
}
},
};
}
......@@ -962,7 +987,7 @@ const mocks = [
'__route__',
'__webviewId__',
'__nodeId__',
'__nodeid__' /* @Deprecated */
'__nodeid__' /* @Deprecated */,
];
function isPage(mpInstance) {
return (mpInstance.__nodeId__ === 0 || mpInstance.__nodeid__ === 0);
......@@ -978,10 +1003,10 @@ function initRelation(mpInstance, detail) {
mpInstance.triggerEvent('__l', {
vuePid: detail.vuePid,
nodeId,
webviewId
webviewId,
});
}
function handleLink({ detail: { vuePid, nodeId, webviewId } }) {
function handleLink({ detail: { vuePid, nodeId, webviewId }, }) {
const vm = instances[webviewId + '_' + nodeId];
if (!vm) {
return;
......@@ -1005,7 +1030,7 @@ function handleLink({ detail: { vuePid, nodeId, webviewId } }) {
}
vm.$callSyncHook('created');
vm.$callHook('mounted');
vm.$callHook('onReady');
vm.$callHook(ON_READY);
}
function parse(componentOptions, { handleLink }) {
componentOptions.methods.__l = handleLink;
......@@ -1019,16 +1044,16 @@ var parseComponentOptions = /*#__PURE__*/Object.freeze({
initRelation: initRelation,
handleLink: handleLink,
parse: parse,
initLifetimes: initLifetimes
initLifetimes: initLifetimes$1
});
function initLifetimes$1(lifetimesOptions) {
return extend(initLifetimes(lifetimesOptions), {
function initLifetimes(lifetimesOptions) {
return extend(initLifetimes$1(lifetimesOptions), {
ready() {
if (this.$vm && lifetimesOptions.isPage(this)) {
this.$vm.$callSyncHook('created');
this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady');
this.$vm.$callHook(ON_READY);
}
else {
this.is && console.warn(this.is + ' is not ready');
......@@ -1039,12 +1064,12 @@ function initLifetimes$1(lifetimesOptions) {
// 清理
const webviewId = this.__webviewId__;
webviewId &&
Object.keys(instances).forEach(key => {
Object.keys(instances).forEach((key) => {
if (key.indexOf(webviewId + '_') === 0) {
delete instances[key];
}
});
}
},
});
}
......@@ -1055,11 +1080,14 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({
initRelation: initRelation,
handleLink: handleLink,
parse: parse,
initLifetimes: initLifetimes$1
initLifetimes: initLifetimes
});
const createApp = initCreateApp();
const createPage = initCreatePage(parsePageOptions);
const createComponent = initCreateComponent(parseComponentOptions);
const createComponent = initCreateComponent(parseComponentOptions);
tt.createApp = createApp;
tt.createPage = createPage;
tt.createComponent = createComponent;
export { createApp, createComponent, createPage };
......@@ -50,7 +50,7 @@ const MP_METHODS = [
'createSelectorQuery',
'createIntersectionObserver',
'selectAllComponents',
'selectComponent'
'selectComponent',
];
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
......@@ -77,7 +77,7 @@ function initBaseInstance(instance, options) {
{
instance.slots = {};
if (isArray(options.slots) && options.slots.length) {
options.slots.forEach(name => {
options.slots.forEach((name) => {
instance.slots[name] = true;
});
}
......@@ -88,7 +88,7 @@ function initBaseInstance(instance, options) {
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
const ctx = instance.ctx;
MP_METHODS.forEach(method => {
MP_METHODS.forEach((method) => {
ctx[method] = function (...args) {
const mpInstance = ctx.$scope;
if (mpInstance && mpInstance[method]) {
......@@ -106,31 +106,70 @@ function initComponentInstance(instance, options) {
}
function initMocks(instance, mpInstance, mocks) {
const ctx = instance.ctx;
mocks.forEach(mock => {
mocks.forEach((mock) => {
if (hasOwn(mpInstance, mock)) {
ctx[mock] = mpInstance[mock];
}
});
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [
'onLoad',
'onShow',
ON_LOAD,
ON_SHOW,
ON_HIDE,
ON_UNLOAD,
ON_RESIZE,
ON_TAB_ITEM_TAP,
ON_REACH_BOTTOM,
ON_PULL_DOWN_REFRESH,
ON_ADD_TO_FAVORITES,
// 'onReady', // lifetimes.ready
'onHide',
'onUnload',
'onResize',
// 'onPageScroll', // 影响性能,开发者手动注册
'onTabItemTap',
'onReachBottom',
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites'
// 'onShareAppMessage' // 右上角菜单,开发者手动注册
];
function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) {
Object.keys(vueOptions).forEach(name => {
Object.keys(vueOptions).forEach((name) => {
if (name.indexOf('on') === 0 && isFunction(vueOptions[name])) {
hooks.add(name);
}
......@@ -138,7 +177,7 @@ function findHooks(vueOptions, hooks = new Set()) {
if (__VUE_OPTIONS_API__) {
const { extends: extendsOptions, mixins } = vueOptions;
if (mixins) {
mixins.forEach(mixin => findHooks(mixin, hooks));
mixins.forEach((mixin) => findHooks(mixin, hooks));
}
if (extendsOptions) {
findHooks(extendsOptions, hooks);
......@@ -147,28 +186,28 @@ function findHooks(vueOptions, hooks = new Set()) {
}
return hooks;
}
function initHook(mpOptions, hook, excludes) {
function initHook$1(mpOptions, hook, excludes) {
if (excludes.indexOf(hook) === -1 && !hasOwn(mpOptions, hook)) {
mpOptions[hook] = function (args) {
return this.$vm && this.$vm.$callHook(hook, args);
};
}
}
const EXCLUDE_HOOKS = ['onReady'];
const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach(hook => initHook(mpOptions, hook, excludes));
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach(hook => initHook(mpOptions, hook, excludes));
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
const HOOKS = [
'onShow',
'onHide',
'onError',
'onThemeChange',
'onPageNotFound',
'onUnhandledRejection'
ON_SHOW,
ON_HIDE,
ON_ERROR,
ON_THEME_CHANGE,
ON_PAGE_NOT_FOUND,
ON_UNHANDLE_REJECTION,
];
function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$;
......@@ -184,11 +223,11 @@ function parseApp(instance, parseAppOptions) {
initBaseInstance(internalInstance, {
mpType: 'app',
mpInstance: this,
slots: []
slots: [],
});
ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options);
}
instance.$callHook(ON_LAUNCH, options);
},
};
const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS);
......@@ -208,26 +247,6 @@ function initCreateApp(parseAppOptions) {
};
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map(key => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter(x => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) {
return Behavior(options);
}
......@@ -247,7 +266,7 @@ function initVueIds(vueIds, mpInstance) {
}
const EXTRAS = ['externalClasses'];
function initExtraOptions(miniProgramComponentOptions, vueOptions) {
EXTRAS.forEach(name => {
EXTRAS.forEach((name) => {
if (hasOwn(vueOptions, name)) {
miniProgramComponentOptions[name] = vueOptions[name];
}
......@@ -268,12 +287,12 @@ function initRefs(instance, mpInstance) {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach(component => {
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach(component => {
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
$refs[ref] = [];
......@@ -281,7 +300,7 @@ function initRefs(instance, mpInstance) {
$refs[ref].push(component.$vm || component);
});
return $refs;
}
},
});
}
function findVmByVueId(instance, vuePid) {
......@@ -324,7 +343,7 @@ function initDefaultProps(isBehavior = false) {
if (!isBehavior) {
properties.vueId = {
type: String,
value: ''
value: '',
};
// 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
properties.vueSlots = {
......@@ -336,9 +355,9 @@ function initDefaultProps(isBehavior = false) {
$slots[slotName] = true;
});
this.setData({
$slots
$slots,
});
}
},
};
}
return properties;
......@@ -350,14 +369,14 @@ function createProperty(key, prop) {
function initProps(mpComponentOptions, rawProps, isBehavior = false) {
const properties = initDefaultProps(isBehavior);
if (isArray(rawProps)) {
rawProps.forEach(key => {
rawProps.forEach((key) => {
properties[key] = createProperty(key, {
type: null
type: null,
});
});
}
else if (isPlainObject(rawProps)) {
Object.keys(rawProps).forEach(key => {
Object.keys(rawProps).forEach((key) => {
const opts = rawProps[key];
if (isPlainObject(opts)) {
// title:{type:String,default:''}
......@@ -369,14 +388,14 @@ function initProps(mpComponentOptions, rawProps, isBehavior = false) {
opts.type = parsePropType(key, type);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
value
value,
});
}
else {
// content:String
const type = parsePropType(key, opts);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
});
}
});
......@@ -388,8 +407,7 @@ function initData(vueOptions) {
let data = vueOptions.data || {};
if (typeof data === 'function') {
try {
const appConfig = getApp().$vm.$.appContext
.config;
const appConfig = getApp().$vm.$.appContext.config;
data = data.call(appConfig.globalProperties);
}
catch (e) {
......@@ -420,7 +438,7 @@ function initBehaviors(vueOptions, initBehavior) {
}
const behaviors = [];
if (isArray(vueBehaviors)) {
vueBehaviors.forEach(behavior => {
vueBehaviors.forEach((behavior) => {
behaviors.push(behavior.replace('uni://', `${__PLATFORM_PREFIX__}://`));
if (behavior === 'uni://form-field') {
if (isArray(vueProps)) {
......@@ -430,24 +448,24 @@ function initBehaviors(vueOptions, initBehavior) {
else {
vueProps.name = {
type: String,
default: ''
default: '',
};
vueProps.value = {
type: [String, Number, Boolean, Array, Object, Date],
default: ''
default: '',
};
}
}
});
}
if (isPlainObject(vueExtends) && vueExtends.props) {
if (vueExtends.props) {
const behavior = {};
initProps(behavior, vueExtends.props, true);
behaviors.push(initBehavior(behavior));
}
if (isArray(vueMixins)) {
vueMixins.forEach(vueMixin => {
if (isPlainObject(vueMixin) && vueMixin.props) {
vueMixins.forEach((vueMixin) => {
if (vueMixin.props) {
const behavior = {};
initProps(behavior, vueMixin.props, true);
behaviors.push(initBehavior(behavior));
......@@ -475,7 +493,7 @@ function getValue(obj, path) {
}
function getExtraValue(instance, dataPathsArray) {
let context = instance;
dataPathsArray.forEach(dataPathArray => {
dataPathsArray.forEach((dataPathArray) => {
const dataPath = dataPathArray[0];
const value = dataPathArray[2];
if (dataPath || typeof value !== 'undefined') {
......@@ -505,12 +523,12 @@ function getExtraValue(instance, dataPathsArray) {
}
else {
if (isArray(vFor)) {
context = vFor.find(vForItem => {
context = vFor.find((vForItem) => {
return getValue(vForItem, propPath) === value;
});
}
else if (isPlainObject(vFor)) {
context = Object.keys(vFor).find(vForKey => {
context = Object.keys(vFor).find((vForKey) => {
return getValue(vFor[vForKey], propPath) === value;
});
}
......@@ -600,7 +618,7 @@ function processEventArgs(instance, event, args = [], extra = [], isCustom, meth
}
const extraObj = processEventExtra(instance, extra, event);
const ret = [];
args.forEach(arg => {
args.forEach((arg) => {
if (arg === '$event') {
if (methodName === '__set_model' && !isCustom) {
// input v-model value
......@@ -642,7 +660,7 @@ function wrapper(event) {
event.detail.markerId = event.markerId;
}
if (isPlainObject(event.detail)) {
event.target = Object.assign({}, event.target, event.detail);
event.target = extend({}, event.target, event.detail);
}
return event;
}
......@@ -712,11 +730,11 @@ function handleEvent(event) {
}
}
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes }) {
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes, }) {
vueOptions = vueOptions.default || vueOptions;
const options = {
multipleSlots: true,
addGlobalClass: true
addGlobalClass: true,
};
if (vueOptions.options) {
extend(options, vueOptions.options);
......@@ -733,12 +751,12 @@ function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handle
},
resize(size) {
this.$vm && this.$vm.$callHook('onPageResize', size);
}
},
},
methods: {
__l: handleLink,
__e: handleEvent
}
__e: handleEvent,
},
};
if (__VUE_OPTIONS_API__) {
applyOptions(mpComponentOptions, vueOptions, initBehavior);
......@@ -778,15 +796,15 @@ function parsePage(vueOptions, parseOptions) {
isPage,
initRelation,
handleLink,
initLifetimes
initLifetimes,
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
fullPath: '/' + this.route + stringifyQuery(query),
};
return this.$vm && this.$vm.$callHook('onLoad', query);
return this.$vm && this.$vm.$callHook(ON_LOAD, query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
......@@ -811,7 +829,7 @@ function initTriggerEvent(mpInstance) {
return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]);
};
}
function initHook$1(name, options) {
function initHook(name, options) {
const oldHook = options[name];
if (!oldHook) {
options[name] = function () {
......@@ -826,21 +844,21 @@ function initHook$1(name, options) {
}
}
Page = function (options) {
initHook$1('onLoad', options);
initHook(ON_LOAD, options);
return MPPage(options);
};
Component = function (options) {
initHook$1('created', options);
initHook('created', options);
return MPComponent(options);
};
function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
const relationOptions = {
vuePid: this._$vuePid
vuePid: this._$vuePid,
};
// 处理父子关系
initRelation(this, relationOptions);
......@@ -848,7 +866,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
const mpInstance = this;
this.$vm = $createComponent({
type: vueOptions,
props: properties
props: properties,
}, {
mpType: isPage(mpInstance) ? 'page' : 'component',
mpInstance,
......@@ -858,7 +876,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
initRefs(instance, mpInstance);
initMocks(instance, mpInstance, mocks);
initComponentInstance(instance, options);
}
},
});
},
ready() {
......@@ -866,12 +884,12 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
// https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
if (this.$vm) {
this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady');
this.$vm.$callHook(ON_READY);
}
},
detached() {
this.$vm && $destroyComponent(this.$vm);
}
},
};
}
......@@ -908,6 +926,9 @@ var parseOptions = /*#__PURE__*/Object.freeze({
const createApp = initCreateApp();
const createPage = initCreatePage(parseOptions);
const createComponent = initCreateComponent(parseOptions);
const createComponent = initCreateComponent(parseOptions);
wx.createApp = createApp;
wx.createPage = createPage;
wx.createComponent = createComponent;
export { createApp, createComponent, createPage };
......@@ -50,7 +50,7 @@ const MP_METHODS = [
'createSelectorQuery',
'createIntersectionObserver',
'selectAllComponents',
'selectComponent'
'selectComponent',
];
function createEmitFn(oldEmit, ctx) {
return function emit(event, ...args) {
......@@ -77,7 +77,7 @@ function initBaseInstance(instance, options) {
{
instance.slots = {};
if (isArray(options.slots) && options.slots.length) {
options.slots.forEach(name => {
options.slots.forEach((name) => {
instance.slots[name] = true;
});
}
......@@ -88,7 +88,7 @@ function initBaseInstance(instance, options) {
function initComponentInstance(instance, options) {
initBaseInstance(instance, options);
const ctx = instance.ctx;
MP_METHODS.forEach(method => {
MP_METHODS.forEach((method) => {
ctx[method] = function (...args) {
const mpInstance = ctx.$scope;
if (mpInstance && mpInstance[method]) {
......@@ -106,31 +106,70 @@ function initComponentInstance(instance, options) {
}
function initMocks(instance, mpInstance, mocks) {
const ctx = instance.ctx;
mocks.forEach(mock => {
mocks.forEach((mock) => {
if (hasOwn(mpInstance, mock)) {
ctx[mock] = mpInstance[mock];
}
});
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map((key) => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter((x) => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
// lifecycle
// App and Page
const ON_SHOW = 'onShow';
const ON_HIDE = 'onHide';
//App
const ON_LAUNCH = 'onLaunch';
const ON_ERROR = 'onError';
const ON_THEME_CHANGE = 'onThemeChange';
const ON_PAGE_NOT_FOUND = 'onPageNotFound';
const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
//Page
const ON_LOAD = 'onLoad';
const ON_READY = 'onReady';
const ON_UNLOAD = 'onUnload';
const ON_RESIZE = 'onResize';
const ON_TAB_ITEM_TAP = 'onTabItemTap';
const ON_REACH_BOTTOM = 'onReachBottom';
const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
const PAGE_HOOKS = [
'onLoad',
'onShow',
ON_LOAD,
ON_SHOW,
ON_HIDE,
ON_UNLOAD,
ON_RESIZE,
ON_TAB_ITEM_TAP,
ON_REACH_BOTTOM,
ON_PULL_DOWN_REFRESH,
ON_ADD_TO_FAVORITES,
// 'onReady', // lifetimes.ready
'onHide',
'onUnload',
'onResize',
// 'onPageScroll', // 影响性能,开发者手动注册
'onTabItemTap',
'onReachBottom',
'onPullDownRefresh',
// 'onShareTimeline', // 右上角菜单,开发者手动注册
'onAddToFavorites'
// 'onShareAppMessage' // 右上角菜单,开发者手动注册
];
function findHooks(vueOptions, hooks = new Set()) {
if (vueOptions) {
Object.keys(vueOptions).forEach(name => {
Object.keys(vueOptions).forEach((name) => {
if (name.indexOf('on') === 0 && isFunction(vueOptions[name])) {
hooks.add(name);
}
......@@ -138,7 +177,7 @@ function findHooks(vueOptions, hooks = new Set()) {
if (__VUE_OPTIONS_API__) {
const { extends: extendsOptions, mixins } = vueOptions;
if (mixins) {
mixins.forEach(mixin => findHooks(mixin, hooks));
mixins.forEach((mixin) => findHooks(mixin, hooks));
}
if (extendsOptions) {
findHooks(extendsOptions, hooks);
......@@ -147,28 +186,28 @@ function findHooks(vueOptions, hooks = new Set()) {
}
return hooks;
}
function initHook(mpOptions, hook, excludes) {
function initHook$1(mpOptions, hook, excludes) {
if (excludes.indexOf(hook) === -1 && !hasOwn(mpOptions, hook)) {
mpOptions[hook] = function (args) {
return this.$vm && this.$vm.$callHook(hook, args);
};
}
}
const EXCLUDE_HOOKS = ['onReady'];
const EXCLUDE_HOOKS = [ON_READY];
function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
hooks.forEach(hook => initHook(mpOptions, hook, excludes));
hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
findHooks(vueOptions).forEach(hook => initHook(mpOptions, hook, excludes));
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes));
}
const HOOKS = [
'onShow',
'onHide',
'onError',
'onThemeChange',
'onPageNotFound',
'onUnhandledRejection'
ON_SHOW,
ON_HIDE,
ON_ERROR,
ON_THEME_CHANGE,
ON_PAGE_NOT_FOUND,
ON_UNHANDLE_REJECTION,
];
function parseApp(instance, parseAppOptions) {
const internalInstance = instance.$;
......@@ -184,11 +223,11 @@ function parseApp(instance, parseAppOptions) {
initBaseInstance(internalInstance, {
mpType: 'app',
mpInstance: this,
slots: []
slots: [],
});
ctx.globalData = this.globalData;
instance.$callHook('onLaunch', options);
}
instance.$callHook(ON_LAUNCH, options);
},
};
const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS);
......@@ -208,26 +247,6 @@ function initCreateApp(parseAppOptions) {
};
}
const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) {
const res = obj
? Object.keys(obj)
.map(key => {
let val = obj[key];
if (typeof val === undefined || val === null) {
val = '';
}
else if (isPlainObject(val)) {
val = JSON.stringify(val);
}
return encodeStr(key) + '=' + encodeStr(val);
})
.filter(x => x.length > 0)
.join('&')
: null;
return res ? `?${res}` : '';
}
function initBehavior(options) {
return Behavior(options);
}
......@@ -247,7 +266,7 @@ function initVueIds(vueIds, mpInstance) {
}
const EXTRAS = ['externalClasses'];
function initExtraOptions(miniProgramComponentOptions, vueOptions) {
EXTRAS.forEach(name => {
EXTRAS.forEach((name) => {
if (hasOwn(vueOptions, name)) {
miniProgramComponentOptions[name] = vueOptions[name];
}
......@@ -268,12 +287,12 @@ function initRefs(instance, mpInstance) {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach(component => {
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach(component => {
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
$refs[ref] = [];
......@@ -281,7 +300,7 @@ function initRefs(instance, mpInstance) {
$refs[ref].push(component.$vm || component);
});
return $refs;
}
},
});
}
......@@ -305,7 +324,7 @@ function initDefaultProps(isBehavior = false) {
if (!isBehavior) {
properties.vueId = {
type: String,
value: ''
value: '',
};
// 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
properties.vueSlots = {
......@@ -317,9 +336,9 @@ function initDefaultProps(isBehavior = false) {
$slots[slotName] = true;
});
this.setData({
$slots
$slots,
});
}
},
};
}
return properties;
......@@ -331,14 +350,14 @@ function createProperty(key, prop) {
function initProps(mpComponentOptions, rawProps, isBehavior = false) {
const properties = initDefaultProps(isBehavior);
if (isArray(rawProps)) {
rawProps.forEach(key => {
rawProps.forEach((key) => {
properties[key] = createProperty(key, {
type: null
type: null,
});
});
}
else if (isPlainObject(rawProps)) {
Object.keys(rawProps).forEach(key => {
Object.keys(rawProps).forEach((key) => {
const opts = rawProps[key];
if (isPlainObject(opts)) {
// title:{type:String,default:''}
......@@ -350,14 +369,14 @@ function initProps(mpComponentOptions, rawProps, isBehavior = false) {
opts.type = parsePropType(key, type);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
value
value,
});
}
else {
// content:String
const type = parsePropType(key, opts);
properties[key] = createProperty(key, {
type: PROP_TYPES.indexOf(type) !== -1 ? type : null
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
});
}
});
......@@ -369,8 +388,7 @@ function initData(vueOptions) {
let data = vueOptions.data || {};
if (typeof data === 'function') {
try {
const appConfig = getApp().$vm.$.appContext
.config;
const appConfig = getApp().$vm.$.appContext.config;
data = data.call(appConfig.globalProperties);
}
catch (e) {
......@@ -401,7 +419,7 @@ function initBehaviors(vueOptions, initBehavior) {
}
const behaviors = [];
if (isArray(vueBehaviors)) {
vueBehaviors.forEach(behavior => {
vueBehaviors.forEach((behavior) => {
behaviors.push(behavior.replace('uni://', `${__PLATFORM_PREFIX__}://`));
if (behavior === 'uni://form-field') {
if (isArray(vueProps)) {
......@@ -411,24 +429,24 @@ function initBehaviors(vueOptions, initBehavior) {
else {
vueProps.name = {
type: String,
default: ''
default: '',
};
vueProps.value = {
type: [String, Number, Boolean, Array, Object, Date],
default: ''
default: '',
};
}
}
});
}
if (isPlainObject(vueExtends) && vueExtends.props) {
if (vueExtends.props) {
const behavior = {};
initProps(behavior, vueExtends.props, true);
behaviors.push(initBehavior(behavior));
}
if (isArray(vueMixins)) {
vueMixins.forEach(vueMixin => {
if (isPlainObject(vueMixin) && vueMixin.props) {
vueMixins.forEach((vueMixin) => {
if (vueMixin.props) {
const behavior = {};
initProps(behavior, vueMixin.props, true);
behaviors.push(initBehavior(behavior));
......@@ -456,7 +474,7 @@ function getValue(obj, path) {
}
function getExtraValue(instance, dataPathsArray) {
let context = instance;
dataPathsArray.forEach(dataPathArray => {
dataPathsArray.forEach((dataPathArray) => {
const dataPath = dataPathArray[0];
const value = dataPathArray[2];
if (dataPath || typeof value !== 'undefined') {
......@@ -486,12 +504,12 @@ function getExtraValue(instance, dataPathsArray) {
}
else {
if (isArray(vFor)) {
context = vFor.find(vForItem => {
context = vFor.find((vForItem) => {
return getValue(vForItem, propPath) === value;
});
}
else if (isPlainObject(vFor)) {
context = Object.keys(vFor).find(vForKey => {
context = Object.keys(vFor).find((vForKey) => {
return getValue(vFor[vForKey], propPath) === value;
});
}
......@@ -581,7 +599,7 @@ function processEventArgs(instance, event, args = [], extra = [], isCustom, meth
}
const extraObj = processEventExtra(instance, extra, event);
const ret = [];
args.forEach(arg => {
args.forEach((arg) => {
if (arg === '$event') {
if (methodName === '__set_model' && !isCustom) {
// input v-model value
......@@ -623,7 +641,7 @@ function wrapper(event) {
event.detail.markerId = event.markerId;
}
if (isPlainObject(event.detail)) {
event.target = Object.assign({}, event.target, event.detail);
event.target = extend({}, event.target, event.detail);
}
return event;
}
......@@ -693,11 +711,11 @@ function handleEvent(event) {
}
}
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes }) {
function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes, }) {
vueOptions = vueOptions.default || vueOptions;
const options = {
multipleSlots: true,
addGlobalClass: true
addGlobalClass: true,
};
if (vueOptions.options) {
extend(options, vueOptions.options);
......@@ -714,12 +732,12 @@ function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handle
},
resize(size) {
this.$vm && this.$vm.$callHook('onPageResize', size);
}
},
},
methods: {
__l: handleLink,
__e: handleEvent
}
__e: handleEvent,
},
};
if (__VUE_OPTIONS_API__) {
applyOptions(mpComponentOptions, vueOptions, initBehavior);
......@@ -759,15 +777,15 @@ function parsePage(vueOptions, parseOptions) {
isPage,
initRelation,
handleLink,
initLifetimes
initLifetimes,
});
const methods = miniProgramPageOptions.methods;
methods.onLoad = function (query) {
this.options = query;
this.$page = {
fullPath: '/' + this.route + stringifyQuery(query)
fullPath: '/' + this.route + stringifyQuery(query),
};
return this.$vm && this.$vm.$callHook('onLoad', query);
return this.$vm && this.$vm.$callHook(ON_LOAD, query);
};
initHooks(methods, PAGE_HOOKS);
initUnknownHooks(methods, vueOptions);
......@@ -792,7 +810,7 @@ function initTriggerEvent(mpInstance) {
return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]);
};
}
function initHook$1(name, options) {
function initHook(name, options) {
const oldHook = options[name];
if (!oldHook) {
options[name] = function () {
......@@ -807,11 +825,11 @@ function initHook$1(name, options) {
}
}
Page = function (options) {
initHook$1('onLoad', options);
initHook(ON_LOAD, options);
return MPPage(options);
};
Component = function (options) {
initHook$1('created', options);
initHook('created', options);
return MPComponent(options);
};
......@@ -849,15 +867,22 @@ function initProvide(instance) {
provide(internalInstance, key, provides[key]);
}
}
function inject(instance, key, defaultValue) {
function inject(instance, key, defaultValue, treatDefaultAsFactory = false) {
if (instance) {
const provides = instance.provides;
if (key in provides) {
// #2400
// to support `app.use` plugins,
// fallback to appContext's `provides` if the intance is at root
const provides = instance.parent == null
? instance.vnode.appContext && instance.vnode.appContext.provides
: instance.parent.provides;
if (provides && key in provides) {
// TS doesn't allow symbol as index type
return provides[key];
}
else if (arguments.length > 1) {
return defaultValue;
return treatDefaultAsFactory && isFunction(defaultValue)
? defaultValue()
: defaultValue;
}
else if ((process.env.NODE_ENV !== 'production')) {
console.warn(`injection "${String(key)}" not found.`);
......@@ -884,7 +909,7 @@ function initInjections(instance) {
for (const key in injectOptions) {
const opt = injectOptions[key];
if (isObject(opt)) {
ctx[key] = inject(internalInstance, opt.from, opt.default);
ctx[key] = inject(internalInstance, opt.from || key, opt.default, true /* treat default function as factory */);
}
else {
ctx[key] = inject(internalInstance, opt);
......@@ -893,13 +918,13 @@ function initInjections(instance) {
}
}
function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) {
return {
attached() {
const properties = this.properties;
initVueIds(properties.vueId, this);
const relationOptions = {
vuePid: this._$vuePid
vuePid: this._$vuePid,
};
// 初始化 vue 实例
const mpInstance = this;
......@@ -909,7 +934,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
}
this.$vm = $createComponent({
type: vueOptions,
props: properties
props: properties,
}, {
mpType,
mpInstance,
......@@ -919,14 +944,14 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
initRefs(instance, mpInstance);
initMocks(instance, mpInstance, mocks);
initComponentInstance(instance, options);
}
},
});
// 处理父子关系
initRelation(this, relationOptions);
},
detached() {
this.$vm && $destroyComponent(this.$vm);
}
},
};
}
......@@ -935,16 +960,16 @@ function parse(componentOptions, { handleLink }) {
componentOptions.methods.__l = handleLink;
}
function initLifetimes$1(lifetimesOptions) {
return extend(initLifetimes(lifetimesOptions), {
function initLifetimes(lifetimesOptions) {
return extend(initLifetimes$1(lifetimesOptions), {
ready() {
if (this.$vm && lifetimesOptions.isPage(this)) {
if ( this.pageinstance) {
if (this.pageinstance) {
this.__webviewId__ = this.pageinstance.__pageId__;
}
this.$vm.$callSyncHook('created');
this.$vm.$callHook('mounted');
this.$vm.$callHook('onReady');
this.$vm.$callHook(ON_READY);
}
else {
this.is && console.warn(this.is + ' is not ready');
......@@ -955,12 +980,12 @@ function initLifetimes$1(lifetimesOptions) {
// 清理
const webviewId = this.__webviewId__;
webviewId &&
Object.keys(instances).forEach(key => {
Object.keys(instances).forEach((key) => {
if (key.indexOf(webviewId + '_') === 0) {
delete instances[key];
}
});
}
},
});
}
......@@ -976,10 +1001,10 @@ function initRelation(mpInstance) {
instances[webviewId + '_' + nodeId] = mpInstance.$vm;
mpInstance.triggerEvent('__l', {
nodeId,
webviewId
webviewId,
});
}
function handleLink({ detail: { nodeId, webviewId } }) {
function handleLink({ detail: { nodeId, webviewId }, }) {
const vm = instances[webviewId + '_' + nodeId];
if (!vm) {
return;
......@@ -1009,7 +1034,7 @@ function handleLink({ detail: { nodeId, webviewId } }) {
delete vm._$childVues;
}
vm.$callHook('mounted');
vm.$callHook('onReady');
vm.$callHook(ON_READY);
};
// 当 parentVm 已经 mounted 时,直接触发,否则延迟
if (!parentVm || parentVm.$.isMounted) {
......@@ -1019,7 +1044,7 @@ function handleLink({ detail: { nodeId, webviewId } }) {
else {
(parentVm._$childVues || (parentVm._$childVues = [])).push([
createdVm,
mountedVm
mountedVm,
]);
}
}
......@@ -1031,7 +1056,7 @@ var parseComponentOptions = /*#__PURE__*/Object.freeze({
mocks: mocks,
isPage: isPage,
parse: parse,
initLifetimes: initLifetimes
initLifetimes: initLifetimes$1
});
var parsePageOptions = /*#__PURE__*/Object.freeze({
......@@ -1041,11 +1066,14 @@ var parsePageOptions = /*#__PURE__*/Object.freeze({
initRelation: initRelation,
handleLink: handleLink,
parse: parse,
initLifetimes: initLifetimes$1
initLifetimes: initLifetimes
});
const createApp = initCreateApp();
const createPage = initCreatePage(parsePageOptions);
const createComponent = initCreateComponent(parseComponentOptions);
const createComponent = initCreateComponent(parseComponentOptions);
qa.createApp = createApp;
qa.createPage = createPage;
qa.createComponent = createComponent;
export { createApp, createComponent, createPage };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册