mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-08-04 02:48:41 +00:00
Restyled by prettier
This commit is contained in:
107
web/main.js
107
web/main.js
@ -14,8 +14,7 @@ async function get_entities() {
|
||||
Authorization: `Bearer ${api_token}`,
|
||||
},
|
||||
mode: 'cors',
|
||||
body :
|
||||
`{"template":"[{% for entity in states %}[\\"{{ entity.entity_id }}\\",\\"{{ entity.name }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
body: `{"template":"[{% for entity in states %}[\\"{{ entity.entity_id }}\\",\\"{{ entity.name }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
});
|
||||
if (res.status == 401 || res.status == 403) {
|
||||
document.querySelector('#api_token').classList.add('invalid');
|
||||
@ -42,8 +41,7 @@ async function get_devices() {
|
||||
Authorization: `Bearer ${api_token}`,
|
||||
},
|
||||
mode: 'cors',
|
||||
body :
|
||||
`{"template":"{% set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq', None) | list %}[{% for device in devices %}[\\"{{ device }}\\",\\"{{ device_attr(device, 'name') }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
body: `{"template":"{% set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq', None) | list %}[{% for device in devices %}[\\"{{ device }}\\",\\"{{ device_attr(device, 'name') }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
});
|
||||
if (res.status == 401 || res.status == 403) {
|
||||
document.querySelector('#api_token').classList.add('invalid');
|
||||
@ -70,8 +68,7 @@ async function get_areas() {
|
||||
Authorization: `Bearer ${api_token}`,
|
||||
},
|
||||
mode: 'cors',
|
||||
body :
|
||||
`{"template":"[{% for area in areas() %}[\\"{{ area }}\\",\\"{{ area_name(area) }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
body: `{"template":"[{% for area in areas() %}[\\"{{ area }}\\",\\"{{ area_name(area) }}\\"]{% if not loop.last %},{% endif %}{% endfor %}]"}`,
|
||||
});
|
||||
if (res.status == 401 || res.status == 403) {
|
||||
document.querySelector('#api_token').classList.add('invalid');
|
||||
@ -127,7 +124,8 @@ async function get_services() {
|
||||
*/
|
||||
async function get_schema() {
|
||||
const res = await fetch(
|
||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json');
|
||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json'
|
||||
);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@ -425,8 +423,7 @@ async function loadSchema() {
|
||||
}
|
||||
try {
|
||||
schema = await generate_schema(entities, devices, areas, services, schema);
|
||||
} catch {
|
||||
}
|
||||
} catch {}
|
||||
console.log(schema);
|
||||
if (window.m && window.modelUri) {
|
||||
// configure the JSON language support with schemas and schema associations
|
||||
@ -434,8 +431,7 @@ async function loadSchema() {
|
||||
validate: true,
|
||||
schemas: [
|
||||
{
|
||||
uri :
|
||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||
uri: 'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||
fileMatch: [window.modelUri.toString()],
|
||||
schema,
|
||||
},
|
||||
@ -453,8 +449,7 @@ require.config({
|
||||
});
|
||||
require(['vs/editor/editor.main'], async () => {
|
||||
window.m = monaco;
|
||||
var modelUri = monaco.Uri.parse(
|
||||
'/config/www/garmin/menu.json'); // a made up unique URI for our model
|
||||
var modelUri = monaco.Uri.parse('/config/www/garmin/menu.json'); // a made up unique URI for our model
|
||||
window.modelUri = modelUri;
|
||||
|
||||
if (schema) {
|
||||
@ -463,8 +458,7 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
validate: true,
|
||||
schemas: [
|
||||
{
|
||||
uri :
|
||||
'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||
uri: 'https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json',
|
||||
fileMatch: [modelUri.toString()],
|
||||
schema,
|
||||
},
|
||||
@ -629,13 +623,18 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
|
||||
setInterval(checkRemoteMenu, 30000);
|
||||
|
||||
var model = monaco.editor.createModel(localStorage.getItem('json') ?? '{}',
|
||||
'json', modelUri);
|
||||
var model = monaco.editor.createModel(
|
||||
localStorage.getItem('json') ?? '{}',
|
||||
'json',
|
||||
modelUri
|
||||
);
|
||||
|
||||
monaco.editor.defineTheme(
|
||||
'mocha',
|
||||
await fetch('https://josephabbey.github.io/catppuccin-monaco/mocha.json')
|
||||
.then((r) => r.json()));
|
||||
await fetch(
|
||||
'https://josephabbey.github.io/catppuccin-monaco/mocha.json'
|
||||
).then((r) => r.json())
|
||||
);
|
||||
|
||||
monaco.languages.registerCompletionItemProvider('json', {
|
||||
triggerCharacters: ['.'],
|
||||
@ -648,7 +647,8 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
endColumn: position.column,
|
||||
});
|
||||
var match = /"content"\s*:\s*"[^"]*[^\w]?\w+\.[^.\s{}()[\]'"]*$/.test(
|
||||
textUntilPosition);
|
||||
textUntilPosition
|
||||
);
|
||||
if (!match) {
|
||||
return { suggestions: [] };
|
||||
}
|
||||
@ -659,8 +659,10 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
}
|
||||
do {
|
||||
i--;
|
||||
} while (i >= 0 &&
|
||||
word.word[i].toUpperCase() != word.word[i].toLowerCase());
|
||||
} while (
|
||||
i >= 0 &&
|
||||
word.word[i].toUpperCase() != word.word[i].toLowerCase()
|
||||
);
|
||||
i++;
|
||||
var range = {
|
||||
startLineNumber: position.lineNumber,
|
||||
@ -669,8 +671,7 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
endColumn: word.endColumn,
|
||||
};
|
||||
return {
|
||||
suggestions : Object.entries(entities).map(
|
||||
([ entity, name ]) => ({
|
||||
suggestions: Object.entries(entities).map(([entity, name]) => ({
|
||||
label: entity,
|
||||
kind: monaco.languages.CompletionItemKind.Variable,
|
||||
documentation: name,
|
||||
@ -692,7 +693,8 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
if (e.key == 's' && e.ctrlKey) {
|
||||
e.preventDefault();
|
||||
model.setValue(
|
||||
JSON.stringify(JSON.parse(editor.getValue()), undefined, 2) + '\n');
|
||||
JSON.stringify(JSON.parse(editor.getValue()), undefined, 2) + '\n'
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@ -700,7 +702,9 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
|
||||
let markers = [];
|
||||
|
||||
const renderTemplate = editor.addCommand(0, async function(_, template) {
|
||||
const renderTemplate = editor.addCommand(
|
||||
0,
|
||||
async function (_, template) {
|
||||
const t = toast({
|
||||
text: 'Rendering template...',
|
||||
});
|
||||
@ -740,9 +744,13 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
});
|
||||
document.querySelector('#api_url').classList.add('invalid');
|
||||
}
|
||||
}, '');
|
||||
},
|
||||
''
|
||||
);
|
||||
|
||||
const runAction = editor.addCommand(0, async function(_, action) {
|
||||
const runAction = editor.addCommand(
|
||||
0,
|
||||
async function (_, action) {
|
||||
const service = action.tap_action.service.split('.');
|
||||
let data = action.tap_action.data;
|
||||
if (data) {
|
||||
@ -756,15 +764,17 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
text: 'Running action...',
|
||||
});
|
||||
try {
|
||||
const res =
|
||||
await fetch(api_url + '/services/' + service[0] + '/' + service[1], {
|
||||
const res = await fetch(
|
||||
api_url + '/services/' + service[0] + '/' + service[1],
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${api_token}`,
|
||||
},
|
||||
mode: 'cors',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
);
|
||||
t.hideToast();
|
||||
if (res.status == 200) {
|
||||
toast({
|
||||
@ -801,15 +811,21 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
});
|
||||
document.querySelector('#api_url').classList.add('invalid');
|
||||
}
|
||||
}, '');
|
||||
},
|
||||
''
|
||||
);
|
||||
|
||||
const toggle = editor.addCommand(0, async function(_, item) {
|
||||
const toggle = editor.addCommand(
|
||||
0,
|
||||
async function (_, item) {
|
||||
const entity = item.entity.split('.');
|
||||
const t = toast({
|
||||
text: 'Toggling...',
|
||||
});
|
||||
try {
|
||||
const res = await fetch(api_url + '/services/' + entity[0] + '/toggle', {
|
||||
const res = await fetch(
|
||||
api_url + '/services/' + entity[0] + '/toggle',
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${api_token}`,
|
||||
@ -818,7 +834,8 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
body: JSON.stringify({
|
||||
entity_id: item.entity,
|
||||
}),
|
||||
});
|
||||
}
|
||||
);
|
||||
t.hideToast();
|
||||
if (res.status == 200) {
|
||||
toast({
|
||||
@ -855,7 +872,9 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
});
|
||||
document.querySelector('#api_url').classList.add('invalid');
|
||||
}
|
||||
}, '');
|
||||
},
|
||||
''
|
||||
);
|
||||
|
||||
async function makeMarkers() {
|
||||
try {
|
||||
@ -1016,8 +1035,7 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
await Promise.all(templates.map((t) => testTemplate(...t)));
|
||||
toggles.forEach((t) => testToggle(...t));
|
||||
monaco.editor.setModelMarkers(model, 'template', markers);
|
||||
} catch {
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
window.makeMarkers = makeMarkers;
|
||||
makeMarkers();
|
||||
@ -1075,8 +1093,10 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
arguments: [node.value[0].value],
|
||||
},
|
||||
});
|
||||
} else if (node.key[0].value === 'type' &&
|
||||
node.value[0].value === 'toggle') {
|
||||
} else if (
|
||||
node.key[0].value === 'type' &&
|
||||
node.value[0].value === 'toggle'
|
||||
) {
|
||||
lenses.push({
|
||||
range: {
|
||||
startLineNumber: node.key[0].range.start.line + 1,
|
||||
@ -1105,13 +1125,14 @@ require([ 'vs/editor/editor.main' ], async () => {
|
||||
}
|
||||
}
|
||||
recurse(ast.body[0], []);
|
||||
} catch {
|
||||
}
|
||||
} catch {}
|
||||
return {
|
||||
lenses,
|
||||
dispose: () => {},
|
||||
};
|
||||
},
|
||||
resolveCodeLens : function(model, codeLens, token) { return codeLens; },
|
||||
resolveCodeLens: function (model, codeLens, token) {
|
||||
return codeLens;
|
||||
},
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user