Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyucoding committed Apr 22, 2019
2 parents 8b73c74 + 2462cfc commit aae4ab6
Show file tree
Hide file tree
Showing 34 changed files with 1,625 additions and 746 deletions.
1 change: 1 addition & 0 deletions build/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = {
replace({
__GLOBAL__: platform.prefix,
__PLATFORM_TITLE__: platform.title,
__PLATFORM_PREFIX__: JSON.stringify(platform.prefix),
__PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM)
})
],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"__PLATFORM__": true,
"__VERSION__": true,
"__GLOBAL__": true,
"__PLATFORM_TITLE__": true
"__PLATFORM_TITLE__": true,
"__PLATFORM_PREFIX__":true
},
"rules": {
"no-tabs": 0,
Expand Down
190 changes: 138 additions & 52 deletions packages/uni-app-plus/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}, k
} else if (isPlainObject(keyOption)) { // {name:newName,value:value}可重新指定参数 key:value
toArgs[keyOption.name ? keyOption.name : key] = keyOption.value;
}
} else if (CALLBACKS.includes(key)) {
} else if (CALLBACKS.indexOf(key) !== -1) {
toArgs[key] = processCallback(methodName, fromArgs[key], returnValue);
} else {
if (!keepFromArgs) {
Expand Down Expand Up @@ -309,11 +309,43 @@ Component = function (options = {}) {
return MPComponent(options)
};

const MOCKS = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__', '__webviewId__'];
const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'];

function initMocks (vm) {
function triggerLink (mpInstance, vueOptions) {
mpInstance.triggerEvent('__l', mpInstance.$vm || vueOptions, {
bubbles: true,
composed: true
});
}

function handleLink (event) {
if (event.detail.$mp) { // vm
if (!event.detail.$parent) {
event.detail.$parent = this.$vm;
event.detail.$parent.$children.push(event.detail);

event.detail.$root = this.$vm.$root;
}
} else { // vueOptions
if (!event.detail.parent) {
event.detail.parent = this.$vm;
}
}
}

function initPage$1 (pageOptions) {
initComponent$1(pageOptions);
}

function initComponent$1 (componentOptions) {
componentOptions.methods.$getAppWebview = function () {
return plus.webview.getWebviewById(`${this.__wxWebviewId__}`)
};
}

function initMocks (vm, mocks) {
const mpInstance = vm.$mp[vm.mpType];
MOCKS.forEach(mock => {
mocks.forEach(mock => {
if (hasOwn(mpInstance, mock)) {
vm[mock] = mpInstance[mock];
}
Expand Down Expand Up @@ -347,6 +379,10 @@ function getData (vueOptions, context) {
} catch (e) {}
}

if (!isPlainObject(data)) {
data = {};
}

Object.keys(methods).forEach(methodName => {
if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) {
data[methodName] = methods[methodName];
Expand All @@ -366,9 +402,65 @@ function createObserver (name) {
}
}

function getProperties (props) {
const properties = {
vueSlots: { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
function getBehaviors (vueOptions) {
const vueBehaviors = vueOptions['behaviors'];
const vueExtends = vueOptions['extends'];
const vueMixins = vueOptions['mixins'];

let vueProps = vueOptions['props'];

if (!vueProps) {
vueOptions['props'] = vueProps = [];
}

const behaviors = [];
if (Array.isArray(vueBehaviors)) {
vueBehaviors.forEach(behavior => {
behaviors.push(behavior.replace('uni://', `${"wx"}://`));
if (behavior === 'uni://form-field') {
if (Array.isArray(vueProps)) {
vueProps.push('name');
vueProps.push('value');
} else {
vueProps['name'] = String;
vueProps['value'] = null;
}
}
});
}
if (isPlainObject(vueExtends) && vueExtends.props) {
behaviors.push(
Behavior({
properties: getProperties(vueExtends.props, true)
})
);
}
if (Array.isArray(vueMixins)) {
vueMixins.forEach(vueMixin => {
if (isPlainObject(vueMixin) && vueMixin.props) {
behaviors.push(
Behavior({
properties: getProperties(vueMixin.props, true)
})
);
}
});
}
return behaviors
}

function parsePropType (key, type, defaultValue, file) {
// [String]=>String
if (Array.isArray(type) && type.length === 1) {
return type[0]
}
return type
}

function getProperties (props, isBehavior = false, file = '') {
const properties = {};
if (!isBehavior) {
properties.vueSlots = { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
type: null,
value: [],
observer: function (newVal, oldVal) {
Expand All @@ -380,8 +472,8 @@ function getProperties (props) {
$slots
});
}
}
};
};
}
if (Array.isArray(props)) { // ['title']
props.forEach(key => {
properties[key] = {
Expand All @@ -397,14 +489,18 @@ function getProperties (props) {
if (isFn(value)) {
value = value();
}

opts.type = parsePropType(key, opts.type, value, file);

properties[key] = {
type: PROP_TYPES.includes(opts.type) ? opts.type : null,
type: PROP_TYPES.indexOf(opts.type) !== -1 ? opts.type : null,
value,
observer: createObserver(key)
};
} else { // content:String
const type = parsePropType(key, opts, null, file);
properties[key] = {
type: PROP_TYPES.includes(opts) ? opts : null,
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
observer: createObserver(key)
};
}
Expand All @@ -414,6 +510,11 @@ function getProperties (props) {
}

function wrapper$1 (event) {
// TODO 又得兼容 mpvue 的 mp 对象
try {
event.mp = JSON.parse(JSON.stringify(event));
} catch (e) {}

event.stopPropagation = noop;
event.preventDefault = noop;

Expand All @@ -423,9 +524,6 @@ function wrapper$1 (event) {
event.detail = {};
}

// TODO 又得兼容 mpvue 的 mp 对象
event.mp = event;

if (isPlainObject(event.detail)) {
event.target = Object.assign({}, event.target, event.detail);
}
Expand Down Expand Up @@ -470,7 +568,7 @@ function getExtraValue (vm, dataPathsArray) {
return context
}

function processEventExtra (vm, extra) {
function processEventExtra (vm, extra, event) {
const extraObj = {};

if (Array.isArray(extra) && extra.length) {
Expand All @@ -490,7 +588,13 @@ function processEventExtra (vm, extra) {
if (!dataPath) { // model,prop.sync
extraObj['$' + index] = vm;
} else {
extraObj['$' + index] = vm.__get_value(dataPath);
if (dataPath === '$event') { // $event
extraObj['$' + index] = event;
} else if (dataPath.indexOf('$event.') === 0) { // $event.target.value
extraObj['$' + index] = vm.__get_value(dataPath.replace('$event.', ''), event);
} else {
extraObj['$' + index] = vm.__get_value(dataPath);
}
}
} else {
extraObj['$' + index] = getExtraValue(vm, dataPath);
Expand All @@ -501,6 +605,15 @@ function processEventExtra (vm, extra) {
return extraObj
}

function getObjByArray (arr) {
const obj = {};
for (let i = 1; i < arr.length; i++) {
const element = arr[i];
obj[element[0]] = element[1];
}
return obj
}

function processEventArgs (vm, event, args = [], extra = [], isCustom, methodName) {
let isCustomMPEvent = false; // wxcomponent 组件,传递原始 event 对象
if (isCustom) { // 自定义事件
Expand All @@ -515,7 +628,7 @@ function processEventArgs (vm, event, args = [], extra = [], isCustom, methodNam
}
}

const extraObj = processEventExtra(vm, extra);
const extraObj = processEventExtra(vm, extra, event);

const ret = [];
args.forEach(arg => {
Expand All @@ -530,7 +643,9 @@ function processEventArgs (vm, event, args = [], extra = [], isCustom, methodNam
}
}
} else {
if (typeof arg === 'string' && hasOwn(extraObj, arg)) {
if (Array.isArray(arg) && arg[0] === 'o') {
ret.push(getObjByArray(arg));
} else if (typeof arg === 'string' && hasOwn(extraObj, arg)) {
ret.push(extraObj[arg]);
} else {
ret.push(arg);
Expand Down Expand Up @@ -653,7 +768,7 @@ function createApp (vm) {
{ // 头条的 selectComponent 竟然是异步的
initRefs(this);
}
initMocks(this);
initMocks(this, mocks);
}
},
created () { // 处理 injections
Expand Down Expand Up @@ -688,38 +803,6 @@ function createApp (vm) {
return vm
}

function triggerLink (mpInstance, vueOptions) {
mpInstance.triggerEvent('__l', mpInstance.$vm || vueOptions, {
bubbles: true,
composed: true
});
}

function handleLink (event) {
if (event.detail.$mp) { // vm
if (!event.detail.$parent) {
event.detail.$parent = this.$vm;
event.detail.$parent.$children.push(event.detail);

event.detail.$root = this.$vm.$root;
}
} else { // vueOptions
if (!event.detail.parent) {
event.detail.parent = this.$vm;
}
}
}

function initPage$1 (pageOptions) {
initComponent$1(pageOptions);
}

function initComponent$1 (componentOptions) {
componentOptions.methods.$getAppWebview = function () {
return plus.webview.getWebviewById(`${this.__wxWebviewId__}`)
};
}

const hooks$1 = [
'onShow',
'onHide',
Expand Down Expand Up @@ -830,7 +913,9 @@ function initVm$2 (VueComponent) {
function createComponent (vueOptions) {
vueOptions = vueOptions.default || vueOptions;

const properties = getProperties(vueOptions.props);
const behaviors = getBehaviors(vueOptions);

const properties = getProperties(vueOptions.props, false, vueOptions.__file);

const VueComponent = Vue.extend(vueOptions);

Expand All @@ -840,6 +925,7 @@ function createComponent (vueOptions) {
addGlobalClass: true
},
data: getData(vueOptions, Vue.prototype),
behaviors,
properties,
lifetimes: {
attached () {
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-app-plus/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dcloudio/uni-app-plus",
"version": "0.0.218",
"version": "0.0.228",
"description": "uni-app app-plus",
"main": "dist/index.js",
"scripts": {
Expand Down
Loading

0 comments on commit aae4ab6

Please sign in to comment.