Skip to content

Commit

Permalink
added auto run
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlyArtist committed Jul 14, 2024
1 parent a9e784c commit d0f157f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 31 deletions.
4 changes: 4 additions & 0 deletions css/buttons.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ button, .fakeButton {
color: inherit;
padding: 0;
font-size: 1rem;
user-select: none;
-webkit-user-select: none; /* Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+/Edge */
}

.complexButton {
Expand Down
71 changes: 48 additions & 23 deletions js/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2015,7 +2015,7 @@ class Flow {
if (fileInfo.starred) {
addLocalPage(fileInfo.item.name, fileInfo.item.link, fileInfo.item.code);
} else {
// Unstarring input is disabled
// Empty because unstarring input is currently not in development
}
}
Flow.closeImportDialog();
Expand Down Expand Up @@ -2206,27 +2206,30 @@ class Flow {

static openStarDialog() {
const page = Flow.getPage();
const isLinked = getPathFromHash() == 'external' && linkedPages.has(url);
const url = getHashQueryVariable('url');
const isLinked = getPathFromHash() == 'extern' && linkedPages.has(url);
const linkedPage = linkedPages.get(url);

Flow.starData.name = page.name ?? '';
Flow.starData.name = isLinked ? linkedPage.name : page.name ?? '';
Flow.starData.link = page.link ?? '';
Flow.starData.hasChanged = false;

Flow.starData.linkedCheckbox.checked = isLinked;
Flow.starData.autoRunCheckbox.checked = isLinked ? linkedPage.autoRun : !!page.autoRun;

Flow.starData.nameInputElement.value = Flow.starData.name;
Flow.starData.linkInputElement.value = Flow.starData.link;
if (isLinked) Flow.starData.linkInputElement.classList.add('hide');
else Flow.starData.linkInputElement.classList.remove('hide');
Flow.starData.urlInputElement.value = Flow.urlEditorElement.textContent;
Flow.starData.urlInputElement.value = getHashQueryVariable('url');
if (isLinked) Flow.starData.urlInputElement.classList.remove('hide');
else Flow.starData.urlInputElement.classList.add('hide');

if (getPathFromHash() == 'extern') {
Flow.starData.toggleBar.checked = false;
Flow.starData.toggleBar.classList.remove('hide');
Flow.starData.linkedToggleBar.checked = false;
Flow.starData.linkedToggleBar.classList.remove('hide');
} else {
Flow.starData.toggleBar.classList.add('hide');
Flow.starData.linkedToggleBar.classList.add('hide');
}

Flow.updateSaveButton();
Expand All @@ -2237,33 +2240,37 @@ class Flow {
static saveStarSettings() {
const page = Flow.getPage();
const name = getPathFromHash();
const url = Flow.urlEditorElement.textContent;
const isLinked = name == 'external' && linkedPages.has(url);
const url = getHashQueryVariable('url');
const isLinked = name == 'extern' && linkedPages.has(url);
const linked = Flow.starData.linkedCheckbox.checked;
const autoRun = Flow.starData.autoRunCheckbox.checked;
if (linked) {
const newUrl = Flow.starData.urlInputElement.value;
if (isLinked && newUrl.trim() == '') {
if (isLinked && url.trim() == '') {
deleteLinkedPage(url);
} else {
addLinkedPage(Flow.starData.name, newUrl);
openPage('extern?url=' + newUrl);
addLinkedPage(Flow.starData.name, url, {autoRun});
openPage('extern?url=' + url);
}
} else {
if (!defaultPages.has(name)) {
if (Flow.starData.link.trim() == '') {
// Delete bookmark
deleteLocalPage(page);
openPage();
} else if (Flow.starData.link == page.link) {
page.autoRun = autoRun;
updateLocalPage(page);
} else {
// Move bookmark
page.name = Flow.starData.name;
page.autoRun = autoRun;
moveLocalPage(page, Flow.starData.link);
openPage(Flow.starData.link);
}
} else {
if (Flow.starData.link.trim() != '') {
// Add bookmark
addLocalPage(Flow.starData.name, Flow.starData.link, page.code);
addLocalPage(Flow.starData.name, Flow.starData.link, page.code, {autoRun});
}
}
}
Expand All @@ -2275,6 +2282,7 @@ class Flow {
const page = Flow.getPage();
const name = getPathFromHash();
const linked = Flow.starData.linkedCheckbox.checked;
const autoRun = Flow.starData.autoRunCheckbox.checked;

function enableSave() {
Flow.starData.saveButton.removeAttribute('disabled');
Expand All @@ -2288,8 +2296,9 @@ class Flow {

if (name == 'extern') {
if (linked) {
const newUrl = Flow.starData.urlInputElement.value;
if (linkedPages.has(newUrl) && Flow.starData.name == linkedPages.get(newUrl).name) {
const newUrl = getHashQueryVariable('url');
const linkedPage = linkedPages.get(newUrl);
if (linkedPages.has(newUrl) && Flow.starData.name == linkedPage.name && !!linkedPage.autoRun == autoRun) {
disableSave();
} else {
enableSave();
Expand All @@ -2302,7 +2311,7 @@ class Flow {
}
}
} else {
if ((Flow.starData.link == page.link && Flow.starData.name == page.name) || (name == 'flow' && Flow.starData.link.trim() == '')) {
if ((Flow.starData.link == page.link && Flow.starData.name == page.name && !!linkedPage.autoRun == autoRun) || (name == 'flow' && Flow.starData.link.trim() == '')) {
disableSave();
} else {
enableSave();
Expand All @@ -2328,16 +2337,16 @@ class Flow {
fileNameElement.textContent = 'Bookmark As:';
settingsElement.appendChild(fileNameElement);

// Toggle bar
const toggleBar = fromHTML(`<div class="listHorizontal">`);
starData.toggleBar = toggleBar;
// Linked toggle bar
const linkedToggleBar = fromHTML(`<div class="listHorizontal">`);
starData.linkedToggleBar = linkedToggleBar;
const label = fromHTML(`<div>Link to External Url`);
label.setAttribute('tooltip', `If checked, the script will be saved in a linked state, meaning you can't change it. In return, it is always updated to the latest version.`);
toggleBar.appendChild(label);
linkedToggleBar.appendChild(label);
const linkedCheckbox = fromHTML(`<input type="checkbox">`);
starData.linkedCheckbox = linkedCheckbox;
toggleBar.appendChild(linkedCheckbox);
element.appendChild(toggleBar);
linkedToggleBar.appendChild(linkedCheckbox);
element.appendChild(linkedToggleBar);

// Name, url and link inputs
const nameInputElement = fromHTML(`<input type="text" tooltip="Enter name. Discarded if link is empty." placeholder="Enter name here...">`);
Expand Down Expand Up @@ -2390,6 +2399,22 @@ class Flow {
settingsElement.appendChild(linkInputElement);
settingsElement.appendChild(urlInputElement);
element.appendChild(settingsElement);


// Auto run toggle bar
const autoRunToggleBar = fromHTML(`<div class="listHorizontal">`);
starData.autoRunToggleBar = autoRunToggleBar;
const autoRunLabel = fromHTML(`<div>Auto Run`);
label.setAttribute('tooltip', `If checked, the sidebar link will open the script in run mode.`);
autoRunToggleBar.appendChild(autoRunLabel);
const autoRunCheckbox = fromHTML(`<input type="checkbox">`);
starData.autoRunCheckbox = autoRunCheckbox;
autoRunToggleBar.appendChild(autoRunCheckbox);
element.appendChild(autoRunToggleBar);
autoRunCheckbox.addEventListener('input', e => {
Flow.updateSaveButton();
});

element.appendChild(hb(8));

// Footer
Expand Down
51 changes: 43 additions & 8 deletions js/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ function saveLocalPages() {
updatePagesSidebar();
}

function addLocalPage(name, link, code, prompt = null) {
function addLocalPage(name, link, code, options = null) {
if (defaultPages.has(link)) return;

options ??= {};
if (link.trim() == '') {
Flow.updateDefaultCode(code);
return;
Expand All @@ -52,7 +53,11 @@ function addLocalPage(name, link, code, prompt = null) {
id = generateUniqueId();
} while (ids.has(id));

localPages.set(link, {name, link, code, prompt, securityId, id});
localPages.set(link, {
securityId, id, name, link, code,
prompt: options.prompt,
autoRun: options.autoRun,
});
saveLocalPages();
}

Expand All @@ -76,7 +81,7 @@ function deleteLocalPage(page) {
}

function moveLocalPage(page, newLink) {
addLocalPage(page.name, newLink, page.code, page.prompt);
addLocalPage(page.name, newLink, page.code, page);
deleteLocalPage(page);
saveLocalPages();
}
Expand Down Expand Up @@ -120,8 +125,12 @@ function saveLinkedPages() {
updatePagesSidebar();
}

function addLinkedPage(name, url) {
linkedPages.set(url, {name, link: url});
function addLinkedPage(name, url, options = null) {
options ??= {};
linkedPages.set(url, {
name, link: url,
autoRun: options.autoRun,
});
saveLinkedPages();
}

Expand All @@ -144,20 +153,46 @@ function getValidHashUrls(){
}

function updatePagesSidebar() {
const link = getPathFromHash();
const name = getPathFromHash();
const url = getHashQueryVariable('url');

// Local pages
const localPagesList = document.getElementById('localPagesList');
localPagesList.innerHTML = '';
localPages.values().forEach(p => localPagesList.appendChild(fromHTML(`<a class="element sidebarElement hoverable" href="#${escapeHTML(p.link)}" ${p.link == link ? 'title="You are here. F5 to reload." disabled' : ''}>${escapeHTML(p.name)}</a>`)));
localPages.values().forEach(p => {
let link = '#' + p.link;
if (p.autoRun) link += '?mode=run';

const element = fromHTML(`<a class="element sidebarElement hoverable">`);
element.textContent = p.name;
element.setAttribute('href', link);
if (p.link == name) {
element.setAttribute('title', "You are here. F5 to reload.");
element.setAttribute('disabled', '');
}

localPagesList.appendChild(element)
});
if (localPages.size == 0) localPagesList.classList.add('hide');
else localPagesList.classList.remove('hide');

// Linked pages
const linkedPagesList = document.getElementById('linkedPagesList');
linkedPagesList.innerHTML = '';
linkedPages.values().forEach(p => linkedPagesList.appendChild(fromHTML(`<a class="element sidebarElement hoverable" href="#extern?url=${escapeHTML(p.link)}" ${p.link == url ? 'title="You are here. F5 to reload." disabled' : ''}>${escapeHTML(p.name)}</a>`)));
linkedPages.values().forEach(p => {
let link = '#extern?url=' + p.link;
if (p.autoRun) link += '&mode=run';

const element = fromHTML(`<a class="element sidebarElement hoverable">`);
element.textContent = p.name;
element.setAttribute('href', link);
if (p.link == getHashQueryVariable('url')) {
element.setAttribute('title', "You are here. F5 to reload.");
element.setAttribute('disabled', '');
}

linkedPagesList.appendChild(element)
});
if (linkedPages.size == 0) linkedPagesList.classList.add('hide');
else linkedPagesList.classList.remove('hide');
}
Expand Down

0 comments on commit d0f157f

Please sign in to comment.