Skip to content

Commit

Permalink
Fix config ui cache problem
Browse files Browse the repository at this point in the history
  • Loading branch information
matrozov committed Aug 4, 2022
1 parent 41c4ed5 commit 4515cd5
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 135 deletions.
147 changes: 15 additions & 132 deletions homebridge-ui/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -258,137 +258,20 @@ <h5 v-html="defaultName(action.name, 'My Action')" class="m-0 ml-4"></h5>

</div>

<script src="js/jquery.slim.min.js"></script>
<script src="js/vue.js"></script>
<script src="js/common.js"></script>
<script>
(async () => {
function clone(data) {
return JSON.parse(JSON.stringify(data));
}

/** @var {array} */
let platforms = await homebridge.getPluginConfig();

platforms = common.convert_from_1_1_2(platforms);

let errors = {};

let platform = platforms.length ? platforms[0] : clone(common.CONFIG_DEFAULT_PLATFORM);

platform = common.fill(platform, common.CONFIG);
platform = common.validate(platform, common.CONFIG, errors);

await homebridge.updatePluginConfig([
clone(platform)
]);

const app = Vue.createApp({
data() {
return {
platform: platform,
errors: errors
};
},
watch: {
platform: {
async handler(platform) {
let errors = {};

platform = common.validate(platform, common.CONFIG, errors);

await homebridge.updatePluginConfig([
clone(platform),
]);

this.errors = errors;
},
deep: true,
},
},
methods: {
name(name, cIdx = null, mIdx = null, aIdx = null) {
if (aIdx !== null) { name = 'actions.' + aIdx + '.' + name; }
if (mIdx !== null) { name = 'messages.' + mIdx + '.' + name; }
if (cIdx !== null) { name = 'channels.' + cIdx + '.' + name; }

return 'platform.' + name;
},
error(name, cIdx = null, mIdx = null, aIdx = null) {
name = this.name(name, cIdx, mIdx, aIdx);

return (name in this.errors) ? this.errors[name] : false;
},
addChannel() {
if (!Array.isArray(this.platform.channels)) {
this.platform.channels = [];
}

this.platform.channels.push(
clone(common.CONFIG_DEFAULT_CHANNEL)
);
},
removeChannel(cIdx) {
this.platform.channels.splice(cIdx, 1);
},
addMessage(cIdx) {
if (!Array.isArray(this.platform.channels[cIdx].messages)) {
this.platform.channels[cIdx].messages = [];
}

this.platform.channels[cIdx].messages.push(
clone(common.CONFIG_DEFAULT_MESSAGE)
);
},
removeMessage(cIdx, mIdx) {
this.platform.channels[cIdx].messages.splice(mIdx, 1);
},
addImage(cIdx, mIdx) {
if (!Array.isArray(this.platform.channels[cIdx].messages[mIdx].images)) {
this.platform.channels[cIdx].messages[mIdx].images = [];
}

this.platform.channels[cIdx].messages[mIdx].images.push(
clone(common.CONFIG_DEFAULT_IMAGE)
);
},
removeImage(cIdx, mIdx, iIdx) {
this.platform.channels[cIdx].messages[mIdx].images.splice(iIdx, 1);
},
addFile(cIdx, mIdx) {
if (!Array.isArray(this.platform.channels[cIdx].messages[mIdx].files)) {
this.platform.channels[cIdx].messages[mIdx].files = [];
}

this.platform.channels[cIdx].messages[mIdx].files.push(
clone(common.CONFIG_DEFAULT_FILE)
);
},
removeFile(cIdx, mIdx, fIdx) {
this.platform.channels[cIdx].messages[mIdx].files.splice(fIdx, 1);
},
addAction(cIdx, mIdx) {
if (!Array.isArray(this.platform.channels[cIdx].messages[mIdx].actions)) {
this.platform.channels[cIdx].messages[mIdx].actions = [];
}

this.platform.channels[cIdx].messages[mIdx].actions.push(
clone(common.CONFIG_DEFAULT_ACTION)
);
},
removeAction(cIdx, mIdx, aIdx) {
this.platform.channels[cIdx].messages[mIdx].actions.splice(aIdx, 1);
},
defaultName(name, def) {
if (name !== undefined && name.trim().length > 0) {
return name;
}

return '<i class="text-muted">' + def + '</i>';
},
},
});

const vm = app.mount('#app');
})();
const version = window.location.href.match(/v=(.*)$/)[1];

function loadScript(src) {
const script = document.createElement('script');
script.src = src + '?v=' + version;
script.async = false;
document.head.appendChild(script);
}

import('js/vue.js')

loadScript('js/jquery.slim.min.js');
loadScript('js/vue.js');
loadScript('js/common.js');
loadScript('js/index.js');
</script>
2 changes: 1 addition & 1 deletion homebridge-ui/public/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
} else {
return def;
}
}
};

const isIpv4 = function(value) {
const regexp = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gi;
Expand Down
129 changes: 129 additions & 0 deletions homebridge-ui/public/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
(async () => {
function clone(data) {
return JSON.parse(JSON.stringify(data));
}

/** @var {array} */
let platforms = await homebridge.getPluginConfig();

platforms = common.convert_from_1_1_2(platforms);

let errors = {};

let platform = platforms.length ? platforms[0] : clone(common.CONFIG_DEFAULT_PLATFORM);

platform = common.fill(platform, common.CONFIG);
platform = common.validate(platform, common.CONFIG, errors);

await homebridge.updatePluginConfig([
clone(platform)
]);

const app = Vue.createApp({
data() {
return {
platform: platform,
errors: errors
};
},
watch: {
platform: {
async handler(platform) {
let errors = {};

platform = common.validate(platform, common.CONFIG, errors);

await homebridge.updatePluginConfig([
clone(platform),
]);

this.errors = errors;
},
deep: true,
},
},
methods: {
name(name, cIdx = null, mIdx = null, aIdx = null) {
if (aIdx !== null) { name = 'actions.' + aIdx + '.' + name; }
if (mIdx !== null) { name = 'messages.' + mIdx + '.' + name; }
if (cIdx !== null) { name = 'channels.' + cIdx + '.' + name; }

return 'platform.' + name;
},
error(name, cIdx = null, mIdx = null, aIdx = null) {
name = this.name(name, cIdx, mIdx, aIdx);

return (name in this.errors) ? this.errors[name] : false;
},
addChannel() {
if (!Array.isArray(this.platform.channels)) {
this.platform.channels = [];
}

this.platform.channels.push(
clone(common.CONFIG_DEFAULT_CHANNEL)
);
},
removeChannel(cIdx) {
this.platform.channels.splice(cIdx, 1);
},
addMessage(cIdx) {
if (!Array.isArray(this.platform.channels[cIdx].messages)) {
this.platform.channels[cIdx].messages = [];
}

this.platform.channels[cIdx].messages.push(
clone(common.CONFIG_DEFAULT_MESSAGE)
);
},
removeMessage(cIdx, mIdx) {
this.platform.channels[cIdx].messages.splice(mIdx, 1);
},
addImage(cIdx, mIdx) {
if (!Array.isArray(this.platform.channels[cIdx].messages[mIdx].images)) {
this.platform.channels[cIdx].messages[mIdx].images = [];
}

this.platform.channels[cIdx].messages[mIdx].images.push(
clone(common.CONFIG_DEFAULT_IMAGE)
);
},
removeImage(cIdx, mIdx, iIdx) {
this.platform.channels[cIdx].messages[mIdx].images.splice(iIdx, 1);
},
addFile(cIdx, mIdx) {
if (!Array.isArray(this.platform.channels[cIdx].messages[mIdx].files)) {
this.platform.channels[cIdx].messages[mIdx].files = [];
}

this.platform.channels[cIdx].messages[mIdx].files.push(
clone(common.CONFIG_DEFAULT_FILE)
);
},
removeFile(cIdx, mIdx, fIdx) {
this.platform.channels[cIdx].messages[mIdx].files.splice(fIdx, 1);
},
addAction(cIdx, mIdx) {
if (!Array.isArray(this.platform.channels[cIdx].messages[mIdx].actions)) {
this.platform.channels[cIdx].messages[mIdx].actions = [];
}

this.platform.channels[cIdx].messages[mIdx].actions.push(
clone(common.CONFIG_DEFAULT_ACTION)
);
},
removeAction(cIdx, mIdx, aIdx) {
this.platform.channels[cIdx].messages[mIdx].actions.splice(aIdx, 1);
},
defaultName(name, def) {
if (name !== undefined && name.trim().length > 0) {
return name;
}

return '<i class="text-muted">' + def + '</i>';
},
},
});

app.mount('#app');
})();
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Notify.Events",
"name": "homebridge-notifyevents",
"version": "1.3.0",
"version": "1.3.1",
"description": "HomeBridge plugin to send message using Notify.Events service",
"author": "Notify.Events",
"keywords": [
Expand All @@ -22,7 +22,7 @@
"homepage": "https://github.com/notify-events/homebridge#readme",
"main": "src/index.js",
"scripts": {
"lint": "eslint src/* homebridge-ui/public/js/common.js",
"lint": "eslint src/* homebridge-ui/public/js/index.js homebridge-ui/public/js/common.js",
"release": "npm publish --access public"
},
"engines": {
Expand Down

0 comments on commit 4515cd5

Please sign in to comment.