You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Platform/wwwroot/js/app.js

84 lines
2.8 KiB

//eval解析vue组件
function parseModel(response) {
var html = new DOMParser().parseFromString(response.data, 'text/html');
var template = html.getElementsByTagName('template')[0].innerHTML;
var script = html.getElementsByTagName('script')[0].innerHTML;
script = '(' + script.replace(/^\s*export\s*default\s*/, '').replace(/;?\s*$/, '') + ')\n//# sourceURL=' + response.config.url;
var model = eval(script);
model.template = template;
return model;
}
//循环添加vue组件
function addVueComponents(config) {
for (var i in config.list) {
var item = config.list[i];
var url = config.prefix + item + '.html';
var name = url.substring(1, url.length - 5).replace(/\//g, "-");
Vue.component(name, function (resolve, reject) {
axios.get(url, { headers: { 'Cache-Control': 'no-cache' } }).then(function (response) {
resolve(parseModel(response));
});
});
}
}
//pubsub批量订阅取消订阅
PubSub.subscribes = function (events, action) {
if (!events || !action) {
return;
}
for (var i = 0; i < events.length; i++) {
PubSub.subscribe(events[i], function (m, d) { action(m, d); });
}
}
PubSub.unsubscribes = function (events) {
if (!events) {
return;
}
for (var i = 0; i < events.length; i++) {
PubSub.unsubscribe(events[i]);
}
}
//
function getIoTValueByName(device, name) {
return Enumerable.from(device.data).where(o => o.name === name).select(o => o.value).firstOrDefault();
}
function getIoTValueByKey(device, key) {
return Enumerable.from(device.data).where(o => o.key === key).select(o => o.value).firstOrDefault();
}
function getIoTValueByDesc(device, desc) {
return Enumerable.from(device.data).where(o => o.description === desc).select(o => o.value).firstOrDefault();
}
//
var routes = [];
var home = '/components/views/home/index.html';
var login = '/components/views/account/login.html';
const router = new VueRouter();
router.beforeEach((to, from, next) => {
var url = to.path === '/' ? home : to.path;
var name = url.substring(1, url.length - 5).replace(/\//g, "-");
var route = routes[name];
if (!route) {
axios.get(url, { headers: { 'Cache-Control': 'no-cache' } }).then(function (response) {
var model = parseModel(response);
route = {
name: name,
path: to.path,
component: model,
meta: model.meta
};
router.addRoutes([route]);
routes[name] = route;
router.push({ path: to.path, query: to.query });
});
}
else {
next();
}
});
//
addVueComponents({
prefix: "/components/devices/",
list: [
'socket'
]
});