allowing for custom youtube-dl options
This commit is contained in:
33
src/css/settings.scss
Normal file
33
src/css/settings.scss
Normal file
@@ -0,0 +1,33 @@
|
||||
.ncdownloader-personal-settings,.ncdownloader-admin-settings {
|
||||
position: relative;
|
||||
|
||||
#ncdownloader-message-banner {
|
||||
position : fixed;
|
||||
top : 50px;
|
||||
text-align : center;
|
||||
padding : 15px;
|
||||
margin-bottom : 20px;
|
||||
border : 1px solid transparent;
|
||||
border-radius : 4px;
|
||||
text-shadow : 0 1px 0 rgba(255, 255, 255, .2);
|
||||
-webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, .25), 0 1px 2px rgba(0, 0, 0, .05);
|
||||
box-shadow : inset 0 1px 0 rgba(255, 255, 255, .25), 0 1px 2px rgba(0, 0, 0, .05);
|
||||
}
|
||||
|
||||
#ncdownloader-message-banner.success,
|
||||
.message-banner.success {
|
||||
color : #3c763d;
|
||||
background-color: #dff0d8;
|
||||
border-color : #d6e9c6;
|
||||
width : 100%;
|
||||
}
|
||||
|
||||
#ncdownloader-message-banner.error,
|
||||
.message-banner.error {
|
||||
color : #a94442;
|
||||
background-color: #f2dede;
|
||||
border-color : #ebccd1;
|
||||
width : 100%;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,10 @@ class settingsForm {
|
||||
static getInstance() {
|
||||
return new this();
|
||||
}
|
||||
setParent(selector) {
|
||||
this.parent = selector;
|
||||
return this;
|
||||
}
|
||||
create(parent, element) {
|
||||
let label = this._createLabel(element.name, element.id)
|
||||
let input = this._createInput(element);
|
||||
@@ -15,11 +19,8 @@ class settingsForm {
|
||||
[label, input, cancelBtn].forEach(ele => {
|
||||
container.appendChild(ele);
|
||||
})
|
||||
let button;
|
||||
if (button = parent.querySelector('button.add-custom-aria2-settings')) {
|
||||
return parent.insertBefore(container, button);
|
||||
}
|
||||
return parent.appendChild(container);
|
||||
|
||||
return parent.prepend(container);
|
||||
}
|
||||
|
||||
createCustomInput(keyId, valueId) {
|
||||
@@ -85,14 +86,14 @@ class settingsForm {
|
||||
_createInput(data) {
|
||||
let input = document.createElement('input');
|
||||
let type = data.type || "text";
|
||||
let placeholder = data.placeholder || '';
|
||||
let value = data.value || placeholder;
|
||||
let placeholder = data.placeholder || 'Leave empty if no value needed';
|
||||
let value = data.value || '';
|
||||
input.setAttribute('type', type);
|
||||
input.setAttribute('id', data.id);
|
||||
input.setAttribute("name", data.name || data.id);
|
||||
if (type === 'text') {
|
||||
input.setAttribute('value', value);
|
||||
input.setAttribute('placeholder', value);
|
||||
input.setAttribute('placeholder', placeholder);
|
||||
}
|
||||
input.classList.add('form-input-' + type);
|
||||
return input;
|
||||
|
||||
121
src/settings.js
121
src/settings.js
@@ -7,67 +7,76 @@ import settingsForm from './lib/settingsForm'
|
||||
import autoComplete from './lib/autoComplete';
|
||||
import eventHandler from './lib/eventHandler';
|
||||
import aria2Options from './utils/aria2Options';
|
||||
import { names as ytdOptions } from './utils/youtubedlOptions';
|
||||
import helper from './utils/helper';
|
||||
import './css/autoComplete.css'
|
||||
import './css/style.scss'
|
||||
|
||||
|
||||
'use strict';
|
||||
window.addEventListener('DOMContentLoaded', function () {
|
||||
|
||||
eventHandler.add('click', '.ncdownloader-admin-settings', 'input[type="button"]', function (event) {
|
||||
event.stopPropagation();
|
||||
OC_msg.startSaving('#ncdownloader-message-banner',"Saving");
|
||||
const target = this.getAttribute("data-rel");
|
||||
let inputData = helper.getData(target);
|
||||
const path = inputData.url || "/apps/ncdownloader/admin/save";
|
||||
let url = generateUrl(path);
|
||||
Http.getInstance(url).setData(helper.getData(target)).setHandler(function () {
|
||||
OC_msg.finishedSuccess('#ncdownloader-message-banner', "OK");
|
||||
}).send();
|
||||
});
|
||||
eventHandler.add('click', '.ncdownloader-personal-settings', 'input[type="button"]', function (event) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (event.target.matches('.custom-aria2-settings-container')) {
|
||||
let customOptions = ['ncd_downloader_dir', 'ncd_torrents_dir', 'ncd_seed_ratio', 'ncd_seed_time', 'ncd_rpctoken', 'ncd_yt_binary', 'ncd_aria2_binary'];
|
||||
const saveHandler = (e, name) => {
|
||||
e.stopImmediatePropagation();
|
||||
let element = e.target;
|
||||
let data = helper.getData(element.getAttribute("data-rel"));
|
||||
let url = generateUrl(data.path);
|
||||
delete data.path;
|
||||
OC_msg.startSaving('#ncdownloader-message-banner');
|
||||
helper.makePair(data, name);
|
||||
let badOptions = [];
|
||||
if (name === 'youtube-dl-settings') {
|
||||
for (let key in data) {
|
||||
if (!ytdOptions.includes(key) && !customOptions.includes(key)) {
|
||||
delete data[key];
|
||||
badOptions.push(key)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (let key in data) {
|
||||
if (!aria2Options.includes(key) && !customOptions.includes(key)) {
|
||||
delete data[key];
|
||||
badOptions.push(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (badOptions.length > 0) {
|
||||
OC_msg.finishedError('#ncdownloader-message-banner', 'invalid options: ' + badOptions.join(','));
|
||||
return;
|
||||
}
|
||||
OC_msg.startSaving('#ncdownloader-message-banner');
|
||||
const target = this.getAttribute("data-rel");
|
||||
let inputData = helper.getData(target);
|
||||
const path = inputData.url || "/apps/ncdownloader/personal/save";
|
||||
let url = generateUrl(path);
|
||||
Http.getInstance(url).setData(inputData).setHandler(function (data) {
|
||||
OC_msg.finishedSuccess('#ncdownloader-message-banner', "OK");
|
||||
Http.getInstance(url).setData(data).setHandler(function (data) {
|
||||
if (data.hasOwnProperty("error"))
|
||||
OC_msg.finishedError('#ncdownloader-message-banner', data.error);
|
||||
else if (data.hasOwnProperty("message"))
|
||||
OC_msg.finishedSuccess('#ncdownloader-message-banner', data.message);
|
||||
else {
|
||||
OC_msg.finishedSuccess('#ncdownloader-message-banner', "DONE");
|
||||
}
|
||||
}).send();
|
||||
});
|
||||
eventHandler.add('click', '#custom-aria2-settings-container', "button.add-custom-aria2-settings", function (e) {
|
||||
}
|
||||
const addOption = (e, name, options) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
let baseName = `${name}-settings`;
|
||||
let element = e.target;
|
||||
let selector = "#aria2-settings-key-1";
|
||||
let selector = `#${baseName}-key-1`;
|
||||
let form = settingsForm.getInstance();
|
||||
let nodeList, key, value;
|
||||
nodeList = document.querySelectorAll("[id^='aria2-settings-key']")
|
||||
nodeList = document.querySelectorAll(`[id^='${baseName}-key']`)
|
||||
if (nodeList.length === 0) {
|
||||
key = "aria2-settings-key-1";
|
||||
value = "aria2-settings-value-1";
|
||||
key = `${baseName}-key-1`;
|
||||
value = `${baseName}-value-1`;
|
||||
} else {
|
||||
let index = nodeList.length + 1;
|
||||
key = "aria2-settings-key-" + index;
|
||||
value = "aria2-settings-value-" + index;
|
||||
selector = "[id^='aria2-settings-key']";
|
||||
key = `${baseName}-key-${index}`;
|
||||
value = `${baseName}-value-${index}`;
|
||||
selector = `[id^='${baseName}-key']`;
|
||||
}
|
||||
element.before(form.createCustomInput(key, value));
|
||||
//appended the latest one
|
||||
nodeList = document.querySelectorAll("[id^='aria2-settings-key']")
|
||||
try {
|
||||
autoComplete.getInstance({
|
||||
selector: (nodeList.length !== 0) ? nodeList : selector,
|
||||
selector: `[id^='${baseName}-key']`,
|
||||
minChars: 1,
|
||||
source: function (term, suggest) {
|
||||
term = term.toLowerCase();
|
||||
let suggestions = [], data = aria2Options;
|
||||
let suggestions = [], data = options;
|
||||
for (const item of data) {
|
||||
if (item.toLowerCase().indexOf(term, 0) !== -1) {
|
||||
suggestions.push(item);
|
||||
@@ -77,22 +86,19 @@ window.addEventListener('DOMContentLoaded', function () {
|
||||
}
|
||||
}).run();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
OC_msg.finishedError('#ncdownloader-message-banner', error);;
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
eventHandler.add("click", "#custom-aria2-settings-container", "button.save-custom-aria2-settings", function (e) {
|
||||
e.stopImmediatePropagation();
|
||||
let data = helper.getData(this.getAttribute("data-rel"));
|
||||
let url = generateUrl(data.path);
|
||||
delete data.path;
|
||||
OC_msg.startSaving('.message-banner');
|
||||
helper.makePair(data);
|
||||
Http.getInstance(url).setData(data).setHandler(function (data) {
|
||||
OC_msg.finishedSuccess('.message-banner', "OK");
|
||||
}).send();
|
||||
})
|
||||
eventHandler.add('click', '.ncdownloader-admin-settings', 'input[type="button"]', (e) => saveHandler(e));
|
||||
eventHandler.add('click', '.ncdownloader-personal-settings', 'input[type="button"]', (e) => saveHandler(e));
|
||||
eventHandler.add("click", "#custom-aria2-settings-container", "button.save-custom-aria2-settings", (e) => saveHandler(e))
|
||||
eventHandler.add("click", "#custom-youtube-dl-settings-container", "button.save-custom-youtube-dl-settings", (e) => saveHandler(e, 'youtube-dl-settings'))
|
||||
|
||||
eventHandler.add('click', '#custom-aria2-settings-container', "button.add-custom-aria2-settings", (e) => addOption(e, 'aria2', aria2Options))
|
||||
eventHandler.add('click', '#custom-youtube-dl-settings-container', "button.add-custom-youtube-dl-settings", (e) => addOption(e, 'youtube-dl', ytdOptions))
|
||||
|
||||
|
||||
eventHandler.add('click', '.ncdownloader-personal-settings', 'button.icon-close', function (e) {
|
||||
e.stopImmediatePropagation();
|
||||
e.preventDefault();
|
||||
@@ -109,4 +115,15 @@ window.addEventListener('DOMContentLoaded', function () {
|
||||
}
|
||||
settingsForm.getInstance().render(input);
|
||||
}).send();
|
||||
|
||||
Http.getInstance(generateUrl("/apps/ncdownloader/personal/youtube-dl/get")).setHandler(function (data) {
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
let input = [];
|
||||
for (let key in data) {
|
||||
input.push({ name: key, value: data[key], id: key });
|
||||
}
|
||||
settingsForm.getInstance().setParent("custom-youtube-dl-settings-container").render(input);
|
||||
}).send();
|
||||
});
|
||||
@@ -115,7 +115,7 @@ const helper = {
|
||||
let index;
|
||||
if ((index = key.indexOf(prefix + "-key-")) !== -1) {
|
||||
let valueKey = prefix + "-value-" + key.substring(key.lastIndexOf('-') + 1);
|
||||
if (!data[valueKey]) continue;
|
||||
if (data[valueKey] === undefined) continue;
|
||||
let newkey = data[key];
|
||||
data[newkey] = data[valueKey];
|
||||
delete data[key];
|
||||
|
||||
3
src/utils/youtubedlOptions.js
Normal file
3
src/utils/youtubedlOptions.js
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user