2016-11-06 04:20:00 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
|
2017-02-25 08:01:09 +08:00
|
|
|
// this will be preserved during build
|
|
|
|
var VueFactory = require('./factory');
|
2016-11-06 04:20:00 +08:00
|
|
|
|
2017-02-25 08:01:09 +08:00
|
|
|
var instances = {};
|
2016-11-06 04:20:00 +08:00
|
|
|
|
|
|
|
/**
|
2017-10-13 22:14:31 +08:00
|
|
|
* Prepare framework config.
|
|
|
|
* Nothing need to do actually, just an interface provided to weex runtime.
|
2016-11-06 04:20:00 +08:00
|
|
|
*/
|
2017-10-13 22:14:31 +08:00
|
|
|
function init () {}
|
2016-11-06 04:20:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset framework config and clear all registrations.
|
|
|
|
*/
|
|
|
|
function reset () {
|
2017-02-25 08:01:09 +08:00
|
|
|
clear(instances);
|
2016-11-06 04:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all keys of an object.
|
|
|
|
* @param {object} obj
|
|
|
|
*/
|
2017-02-25 08:01:09 +08:00
|
|
|
function clear (obj) {
|
2016-11-06 04:20:00 +08:00
|
|
|
for (var key in obj) {
|
|
|
|
delete obj[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an instance with id, code, config and external data.
|
|
|
|
* @param {string} instanceId
|
|
|
|
* @param {string} appCode
|
|
|
|
* @param {object} config
|
|
|
|
* @param {object} data
|
2016-12-31 08:36:18 +08:00
|
|
|
* @param {object} env { info, config, services }
|
2016-11-06 04:20:00 +08:00
|
|
|
*/
|
|
|
|
function createInstance (
|
2016-12-31 08:36:18 +08:00
|
|
|
instanceId,
|
|
|
|
appCode,
|
|
|
|
config,
|
|
|
|
data,
|
|
|
|
env
|
|
|
|
) {
|
2016-11-06 04:20:00 +08:00
|
|
|
if ( appCode === void 0 ) appCode = '';
|
|
|
|
if ( config === void 0 ) config = {};
|
2016-12-31 08:36:18 +08:00
|
|
|
if ( env === void 0 ) env = {};
|
2016-11-06 04:20:00 +08:00
|
|
|
|
2017-10-13 22:14:31 +08:00
|
|
|
var weex = env.weex;
|
|
|
|
var document = weex.document;
|
2017-02-25 08:01:09 +08:00
|
|
|
var instance = instances[instanceId] = {
|
2016-11-06 04:20:00 +08:00
|
|
|
instanceId: instanceId, config: config, data: data,
|
2017-07-24 19:32:38 +08:00
|
|
|
document: document
|
2016-11-06 04:20:00 +08:00
|
|
|
};
|
|
|
|
|
2017-10-13 22:14:31 +08:00
|
|
|
var timerAPIs = getInstanceTimer(instanceId, weex.requireModule);
|
2016-12-31 08:36:18 +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
|
|
|
|
2016-12-31 08:36:18 +08:00
|
|
|
// The function which create a closure the JS Bundle will run in.
|
|
|
|
// It will declare some instance variables like `Vue`, HTML5 Timer APIs etc.
|
|
|
|
var instanceVars = Object.assign({
|
2017-02-25 08:01:09 +08:00
|
|
|
Vue: Vue,
|
2017-10-13 22:14:31 +08:00
|
|
|
weex: weex
|
2017-07-24 19:32:38 +08:00
|
|
|
}, timerAPIs, env.services);
|
|
|
|
|
2017-10-13 22:14:31 +08:00
|
|
|
appCode = "(function(global){ \n" + appCode + "\n })(Object.create(this))";
|
|
|
|
|
|
|
|
callFunction(instanceVars, appCode);
|
2016-12-31 08:36:18 +08:00
|
|
|
|
|
|
|
// Send `createFinish` signal to native.
|
2017-10-13 22:14:31 +08:00
|
|
|
document.taskCenter.send('dom', { action: 'createFinish' }, []);
|
|
|
|
|
|
|
|
return instance
|
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.
|
|
|
|
* @param {string} instanceId
|
|
|
|
*/
|
|
|
|
function destroyInstance (instanceId) {
|
2017-02-25 08:01:09 +08:00
|
|
|
var instance = instances[instanceId];
|
|
|
|
if (instance && instance.app instanceof instance.Vue) {
|
2017-07-24 19:32:38 +08:00
|
|
|
instance.document.destroy();
|
2016-11-06 04:20:00 +08:00
|
|
|
instance.app.$destroy();
|
2017-10-13 22:14:31 +08:00
|
|
|
delete instance.document;
|
|
|
|
delete instance.app;
|
2016-11-06 04:20:00 +08:00
|
|
|
}
|
|
|
|
delete instances[instanceId];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param {string} instanceId
|
|
|
|
* @param {object} data
|
|
|
|
*/
|
|
|
|
function refreshInstance (instanceId, data) {
|
2017-02-25 08:01:09 +08:00
|
|
|
var instance = instances[instanceId];
|
|
|
|
if (!instance || !(instance.app instanceof instance.Vue)) {
|
2016-11-06 04:20:00 +08:00
|
|
|
return new Error(("refreshInstance: instance " + instanceId + " not found!"))
|
|
|
|
}
|
|
|
|
for (var key in data) {
|
2017-02-25 08:01:09 +08:00
|
|
|
instance.Vue.set(instance.app, key, data[key]);
|
2016-11-06 04:20:00 +08:00
|
|
|
}
|
|
|
|
// Finally `refreshFinish` signal needed.
|
2017-07-24 19:32:38 +08:00
|
|
|
instance.document.taskCenter.send('dom', { action: 'refreshFinish' }, []);
|
2016-11-06 04:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the JSON object of the root element.
|
|
|
|
* @param {string} instanceId
|
|
|
|
*/
|
|
|
|
function getRoot (instanceId) {
|
2017-02-25 08:01:09 +08:00
|
|
|
var instance = instances[instanceId];
|
|
|
|
if (!instance || !(instance.app instanceof instance.Vue)) {
|
2016-11-06 04:20:00 +08:00
|
|
|
return new Error(("getRoot: instance " + instanceId + " not found!"))
|
|
|
|
}
|
|
|
|
return instance.app.$el.toJSON()
|
|
|
|
}
|
|
|
|
|
2017-02-25 08:01:09 +08:00
|
|
|
/**
|
|
|
|
* Create a fresh instance of Vue for each Weex instance.
|
|
|
|
*/
|
2017-10-13 22:14:31 +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;
|
|
|
|
|
|
|
|
var instance = instances[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;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param {[type]} instanceId [description]
|
|
|
|
* @param {[type]} moduleGetter [description]
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
|
|
|
function getInstanceTimer (instanceId, moduleGetter) {
|
|
|
|
var instance = instances[instanceId];
|
|
|
|
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));
|
|
|
|
};
|
2017-07-24 19:32:38 +08:00
|
|
|
|
2016-11-06 04:20:00 +08:00
|
|
|
timer.setTimeout(handler, args[1]);
|
2017-07-24 19:32:38 +08:00
|
|
|
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));
|
|
|
|
};
|
2017-07-24 19:32:38 +08:00
|
|
|
|
2016-11-06 04:20:00 +08:00
|
|
|
timer.setInterval(handler, args[1]);
|
2017-07-24 19:32:38 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-31 08:36:18 +08:00
|
|
|
/**
|
|
|
|
* Call a new function body with some global objects.
|
|
|
|
* @param {object} globalObjects
|
|
|
|
* @param {string} code
|
|
|
|
* @return {any}
|
|
|
|
*/
|
|
|
|
function callFunction (globalObjects, body) {
|
|
|
|
var globalKeys = [];
|
|
|
|
var globalValues = [];
|
|
|
|
for (var key in globalObjects) {
|
|
|
|
globalKeys.push(key);
|
|
|
|
globalValues.push(globalObjects[key]);
|
|
|
|
}
|
|
|
|
globalKeys.push(body);
|
|
|
|
|
|
|
|
var result = new (Function.prototype.bind.apply( Function, [ null ].concat( globalKeys) ));
|
|
|
|
return result.apply(void 0, globalValues)
|
|
|
|
}
|
|
|
|
|
2016-11-06 04:20:00 +08:00
|
|
|
exports.init = init;
|
|
|
|
exports.reset = reset;
|
|
|
|
exports.createInstance = createInstance;
|
|
|
|
exports.destroyInstance = destroyInstance;
|
|
|
|
exports.refreshInstance = refreshInstance;
|
|
|
|
exports.getRoot = getRoot;
|