vue2/packages/weex-vue-framework/index.js

204 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-11-06 04:20:00 +08:00
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
2018-03-14 06:14:16 +08:00
/* */
2017-02-25 08:01:09 +08:00
// this will be preserved during build
2018-03-14 06:14:16 +08:00
// $flow-disable-line
2017-02-25 08:01:09 +08:00
var VueFactory = require('./factory');
2016-11-06 04:20:00 +08:00
2018-03-14 06:14:16 +08:00
var instanceOptions = {};
2016-11-06 04:20:00 +08:00
/**
2018-03-14 06:14:16 +08:00
* Create instance context.
2016-11-06 04:20:00 +08:00
*/
2018-03-14 06:14:16 +08:00
function createInstanceContext (
instanceId,
2018-03-14 06:14:16 +08:00
runtimeContext,
data
) {
2018-03-14 06:14:16 +08:00
if ( data === void 0 ) data = {};
var weex = runtimeContext.weex;
var instance = instanceOptions[instanceId] = {
instanceId: instanceId,
config: weex.config,
document: weex.document,
data: data
2016-11-06 04:20:00 +08:00
};
2017-03-09 10:32:38 +08:00
// Each instance has a independent `Vue` module instance
2017-10-13 22:14:31 +08:00
var Vue = instance.Vue = createVueModuleInstance(instanceId, weex);
2016-11-06 04:20:00 +08:00
2018-03-14 06:14:16 +08:00
// DEPRECATED
var timerAPIs = getInstanceTimer(instanceId, weex.requireModule);
2018-03-14 06:14:16 +08:00
var instanceContext = Object.assign({ Vue: Vue }, timerAPIs);
Object.freeze(instanceContext);
return instanceContext
2016-11-06 04:20:00 +08:00
}
/**
* Destroy an instance with id. It will make sure all memory of
* this instance released and no more leaks.
*/
function destroyInstance (instanceId) {
2018-03-14 06:14:16 +08:00
var instance = instanceOptions[instanceId];
2017-02-25 08:01:09 +08:00
if (instance && instance.app instanceof instance.Vue) {
2018-03-14 06:14:16 +08:00
try {
instance.app.$destroy();
instance.document.destroy();
} catch (e) {}
2017-10-13 22:14:31 +08:00
delete instance.document;
delete instance.app;
2016-11-06 04:20:00 +08:00
}
2018-03-14 06:14:16 +08:00
delete instanceOptions[instanceId];
2016-11-06 04:20:00 +08:00
}
/**
* Refresh an instance with id and new top-level component data.
* It will use `Vue.set` on all keys of the new data. So it's better
* define all possible meaningful keys when instance created.
*/
2018-03-14 06:14:16 +08:00
function refreshInstance (
instanceId,
data
) {
var instance = instanceOptions[instanceId];
2017-02-25 08:01:09 +08:00
if (!instance || !(instance.app instanceof instance.Vue)) {
2016-11-06 04:20:00 +08:00
return new Error(("refreshInstance: instance " + instanceId + " not found!"))
}
2018-03-14 06:14:16 +08:00
if (instance.Vue && instance.Vue.set) {
for (var key in data) {
instance.Vue.set(instance.app, key, data[key]);
}
2016-11-06 04:20:00 +08:00
}
// Finally `refreshFinish` signal needed.
instance.document.taskCenter.send('dom', { action: 'refreshFinish' }, []);
2016-11-06 04:20:00 +08:00
}
2017-02-25 08:01:09 +08:00
/**
* Create a fresh instance of Vue for each Weex instance.
*/
2018-03-14 06:14:16 +08:00
function createVueModuleInstance (
instanceId,
weex
) {
2017-02-25 08:01:09 +08:00
var exports = {};
2017-10-13 22:14:31 +08:00
VueFactory(exports, weex.document);
2017-02-25 08:01:09 +08:00
var Vue = exports.Vue;
2018-03-14 06:14:16 +08:00
var instance = instanceOptions[instanceId];
2016-11-06 04:20:00 +08:00
2017-02-25 08:01:09 +08:00
// patch reserved tag detection to account for dynamically registered
// components
2017-10-13 22:14:31 +08:00
var weexRegex = /^weex:/i;
2017-02-25 08:01:09 +08:00
var isReservedTag = Vue.config.isReservedTag || (function () { return false; });
2017-10-13 22:14:31 +08:00
var isRuntimeComponent = Vue.config.isRuntimeComponent || (function () { return false; });
2017-02-25 08:01:09 +08:00
Vue.config.isReservedTag = function (name) {
2017-10-13 22:14:31 +08:00
return (!isRuntimeComponent(name) && weex.supports(("@component/" + name))) ||
isReservedTag(name) ||
weexRegex.test(name)
2017-02-25 08:01:09 +08:00
};
2017-10-13 22:14:31 +08:00
Vue.config.parsePlatformTagName = function (name) { return name.replace(weexRegex, ''); };
2016-11-06 04:20:00 +08:00
2017-02-25 08:01:09 +08:00
// expose weex-specific info
Vue.prototype.$instanceId = instanceId;
Vue.prototype.$document = instance.document;
2016-11-06 04:20:00 +08:00
2017-02-25 08:01:09 +08:00
// expose weex native module getter on subVue prototype so that
// vdom runtime modules can access native modules via vnode.context
2017-10-13 22:14:31 +08:00
Vue.prototype.$requireWeexModule = weex.requireModule;
2016-11-06 04:20:00 +08:00
2017-02-25 08:01:09 +08:00
// Hack `Vue` behavior to handle instance information and data
// before root component created.
Vue.mixin({
beforeCreate: function beforeCreate () {
var options = this.$options;
// root component (vm)
if (options.el) {
// set external data of instance
var dataOption = options.data;
var internalData = (typeof dataOption === 'function' ? dataOption() : dataOption) || {};
options.data = Object.assign(internalData, instance.data);
// record instance by id
instance.app = this;
}
2017-12-19 22:54:13 +08:00
},
mounted: function mounted () {
var options = this.$options;
// root component (vm)
2018-03-14 06:14:16 +08:00
if (options.el && weex.document && instance.app === this) {
2017-12-19 22:54:13 +08:00
try {
// Send "createFinish" signal to native.
weex.document.taskCenter.send('dom', { action: 'createFinish' }, []);
} catch (e) {}
}
2016-11-06 04:20:00 +08:00
}
2017-02-25 08:01:09 +08:00
});
2016-11-06 04:20:00 +08:00
2017-02-25 08:01:09 +08:00
/**
* @deprecated Just instance variable `weex.config`
* Get instance config.
* @return {object}
*/
Vue.prototype.$getConfig = function () {
if (instance.app instanceof Vue) {
return instance.config
}
};
return Vue
}
2016-11-06 04:20:00 +08:00
/**
2018-03-14 06:14:16 +08:00
* DEPRECATED
2016-11-06 04:20:00 +08:00
* Generate HTML5 Timer APIs. An important point is that the callback
* will be converted into callback id when sent to native. So the
2017-03-09 10:32:38 +08:00
* framework can make sure no side effect of the callback happened after
2016-11-06 04:20:00 +08:00
* an instance destroyed.
*/
2018-03-14 06:14:16 +08:00
function getInstanceTimer (
instanceId,
moduleGetter
) {
var instance = instanceOptions[instanceId];
2016-11-06 04:20:00 +08:00
var timer = moduleGetter('timer');
var timerAPIs = {
setTimeout: function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var handler = function () {
args[0].apply(args, args.slice(2));
};
2016-11-06 04:20:00 +08:00
timer.setTimeout(handler, args[1]);
return instance.document.taskCenter.callbackManager.lastCallbackId.toString()
2016-11-06 04:20:00 +08:00
},
setInterval: function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var handler = function () {
args[0].apply(args, args.slice(2));
};
2016-11-06 04:20:00 +08:00
timer.setInterval(handler, args[1]);
return instance.document.taskCenter.callbackManager.lastCallbackId.toString()
2016-11-06 04:20:00 +08:00
},
clearTimeout: function (n) {
timer.clearTimeout(n);
},
clearInterval: function (n) {
timer.clearInterval(n);
}
};
return timerAPIs
}
2018-03-14 06:14:16 +08:00
exports.createInstanceContext = createInstanceContext;
2016-11-06 04:20:00 +08:00
exports.destroyInstance = destroyInstance;
exports.refreshInstance = refreshInstance;