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;
}
//update
function updateById(list, item) {
return update(list, item, 'id');
}
function updateByNumber(list, item) {
return update(list, item, 'number');
}
function updateByKey(list, item) {
return update(list, item, 'key');
}
function update(list, item, key) {
key = key || 'number';
var result = false;
var to = Enumerable.from(list).where(function (o) { return o[key] === item[key]; }).firstOrDefault();
if (to) {
copy(item, to);
}
else {
list.push(item);
result = true;
}
//console.log(result ? 'insert' : 'update' + ' data by ' + key);
return result;
}
//delete
function removeById(list, item) {
return remove(list, item, 'id');
}
function removeByKey(list, item) {
return remove(list, item, 'key');
}
function remove(list, item, key) {
var result = false;
for (var i = 0; i < list.length; i++) {
if (list[i][key] === item[key]) {
list.splice(i, 1);
result = true;
break;
}
}
return result;
}
//util
function copy(from, to) {
for (var attr in to) {
if (from.hasOwnProperty(attr)) {
if (!(from[attr] instanceof Array)) {
to[attr] = from[attr];
}
}
}
}
function subscribe(events, action) {
if (!events || !action) {
return;
}
for (var i = 0; i < events.length; i++) {
PubSub.subscribe(events[i], function (m, d) { action(m, d); });
}
}
function unsubscribe(events) {
if (!events) {
return;
}
for (var i = 0; i < events.length; i++) {
PubSub.unsubscribe(events[i]);
}
}
//
function execApi(number, method, query) {
loading.show();
axios.post(config.baseUrl+'/IoTCenter/api/v1/api/execApi', { connectionId, number, method, query })
.then(function (response) {
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
loading.hide();
});
}
function execScene(id) {
loading.show();
axios.post('/IoTCenter/api/v1/api/execScene', '"' + id + '"', { headers: { 'Content-Type': 'application/json;charset=UTF-8' } })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
loading.show();
});
}
function nodePower(number, command) {
loading.show();
axios.post('/IoTCenter/api/v1/node/power' + command, '"' + number + '"', { headers: { 'Content-Type': 'application/json;charset=UTF-8' } })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
loading.show();
});
}
function nodeMethod(number, method) {
loading.show();
axios.post('/IoTCenter/api/v1/node/' + method, '"' + number + '"', { headers: { 'Content-Type': 'application/json;charset=UTF-8' } })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
loading.show();
});
}
//
function Select(e) {
var checkbox = $(e.target);
if (checkbox.hasClass('checkall')) {
if (e.target.checked) {
$('input.item:visible').not(':checked').prop("checked", true);
}
else {
$('input.item:visible').filter(':checked').prop("checked", false);
}
}
else if (checkbox.hasClass('uncheck')) {
$('input.item:visible').each(function () {
$(this).prop("checked", !$(this).prop("checked")).change();
});
}
var parent = $('input.checkall');
if ($('input.item').not(':checked').length === 0) {
parent.prop("indeterminate", false);
parent.prop("checked", true);
}
else if ($('input.item').filter(':checked').length === 0) {
parent.prop("indeterminate", false);
parent.prop("checked", false);
}
else {
parent.prop("indeterminate", true);
}
}