Merge branch 'master' into master
This commit is contained in:
24
src/App.vue
24
src/App.vue
@@ -56,19 +56,25 @@ export default {
|
||||
let element = event.target;
|
||||
let formWrapper = element.closest("form");
|
||||
let formData = helper.getData(formWrapper);
|
||||
let inputValue = formData["text-input-value"];
|
||||
let inputValue = formData["text-input-value"].trim();
|
||||
let message;
|
||||
if (formData.type === "youtube-dl") {
|
||||
formData["extension"] = "";
|
||||
if (formData["select-value-extension"] !== "defaultext") {
|
||||
formData["extension"] = formData["select-value-extension"];
|
||||
}
|
||||
message = helper.t("Download task started!");
|
||||
}
|
||||
if (!helper.isURL(inputValue) && !helper.isMagnetURI(inputValue)) {
|
||||
helper.error(t("ncdownloader", inputValue + " is Invalid"));
|
||||
return;
|
||||
}
|
||||
if (formData.type === "youtube-dl") {
|
||||
formData["extension"] = "";
|
||||
|
||||
if (formData["select-value-extension"] !== "defaultext") {
|
||||
formData["extension"] = formData["select-value-extension"];
|
||||
}
|
||||
message = helper.t("Download task started!");
|
||||
helper.pollingYoutube();
|
||||
helper.setContentTableType("youtube-dl-downloads");
|
||||
} else {
|
||||
helper.polling();
|
||||
helper.setContentTableType("active-downloads");
|
||||
}
|
||||
if (message) {
|
||||
helper.info(message);
|
||||
}
|
||||
@@ -90,7 +96,7 @@ export default {
|
||||
vm.$data.loading = 0;
|
||||
return;
|
||||
}
|
||||
helper.enabledPolling = 0;
|
||||
helper.disablePolling();
|
||||
contentTable.getInstance().loading();
|
||||
|
||||
let url = formWrapper.getAttribute("action");
|
||||
|
||||
@@ -1,50 +1,40 @@
|
||||
import helper from '../utils/helper'
|
||||
import eventHandler from '../lib/eventHandler';
|
||||
import Http from '../lib/http'
|
||||
|
||||
const basePath = "/apps/ncdownloader/status/";
|
||||
const tableContainer = ".table";
|
||||
export default {
|
||||
run: function () {
|
||||
|
||||
const clickHandler = (event, type) => {
|
||||
const clickHandler = (event) => {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
helper.hideDownload();
|
||||
let container = document.querySelector(tableContainer);
|
||||
let currentType = container.getAttribute("type");
|
||||
let path = basePath + type;
|
||||
if (type === "youtube-dl") {
|
||||
path = "/apps/ncdownloader/youtube/get";
|
||||
}
|
||||
let name = type + "-downloads";
|
||||
let element = event.target;
|
||||
//helper.hideDownload();
|
||||
let currentType = helper.getContentTableType();
|
||||
let path = element.getAttribute("path");
|
||||
let name = element.getAttribute("id");
|
||||
//avoid repeated click
|
||||
if (currentType === name && helper.enabledPolling) {
|
||||
if (currentType === name && helper.isPolling()) {
|
||||
return;
|
||||
}
|
||||
helper.enabledPolling = 1;
|
||||
//$(tableContainer).removeClass().addClass("table " + name);
|
||||
container.setAttribute("type", name);
|
||||
container.className = "table " + name;
|
||||
let delay = 15000;
|
||||
if (['active', 'youtube-dl'].includes(type)) {
|
||||
delay = 1500;
|
||||
helper.setContentTableType(name);
|
||||
let delay;
|
||||
if (!['active-downloads', 'youtube-dl-downloads'].includes(name)) {
|
||||
delay = 15000;
|
||||
}
|
||||
if (name === "youtube-dl-downloads") {
|
||||
helper.pollingYoutube();
|
||||
} else {
|
||||
helper.polling(delay, path);
|
||||
}
|
||||
helper.loop(helper.refresh, delay, ...[path])
|
||||
};
|
||||
eventHandler.add("click",".waiting-downloads a",event => clickHandler(event, 'waiting'));
|
||||
eventHandler.add("click",".complete-downloads a",event => clickHandler(event, 'complete'));
|
||||
eventHandler.add("click",".active-downloads a",event => clickHandler(event, 'active'));
|
||||
eventHandler.add("click",".fail-downloads a",event => clickHandler(event, 'fail'));
|
||||
eventHandler.add("click",".youtube-dl-downloads a",event => clickHandler(event, 'youtube-dl'));
|
||||
eventHandler.add("click", "#ncdownloader-table-wrapper",".download-file-folder", function (event) {
|
||||
eventHandler.add("click", ".download-queue a", event => clickHandler(event));
|
||||
eventHandler.add("click", "#ncdownloader-table-wrapper", ".download-file-folder", function (event) {
|
||||
event.stopPropagation();
|
||||
const path = "/apps/ncdownloader/update";
|
||||
let url = helper.generateUrl(path);
|
||||
Http.getInstance(url).setMethod('GET').send();
|
||||
event.preventDefault();
|
||||
let ele = event.target;
|
||||
let url = ele.getAttribute("href");
|
||||
helper.scanFolder().then(() => {
|
||||
helper.redirect(url);
|
||||
});
|
||||
});
|
||||
helper.polling(function (url) {
|
||||
url = helper.generateUrl(url);
|
||||
Http.getInstance(url).setMethod('GET').send();
|
||||
}, 60000, "/apps/ncdownloader/update");
|
||||
}
|
||||
}
|
||||
@@ -14,23 +14,26 @@ const eventHandler = {
|
||||
});
|
||||
return;
|
||||
}
|
||||
let el = document.querySelector(target);
|
||||
if (!el) {
|
||||
let items = document.querySelectorAll(target);
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
el.addEventListener(eventType, function (e) {
|
||||
let element = e.target as HTMLElement;
|
||||
if (element === this && selector === target) {
|
||||
callback.call(element, e);
|
||||
return;
|
||||
}
|
||||
for (; element && element != this; element = element.parentElement) {
|
||||
if (typeof selector === "string" && element.matches(selector)) {
|
||||
items.forEach(el => {
|
||||
el.addEventListener(eventType, function (e) {
|
||||
let element = e.target as HTMLElement;
|
||||
if (element === this && selector === target) {
|
||||
callback.call(element, e);
|
||||
break;
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
for (; element && element != this; element = element.parentElement) {
|
||||
if (typeof selector === "string" && element.matches(selector)) {
|
||||
callback.call(element, e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
},
|
||||
remove: function (element: target, eventType: string, callback: callback) {
|
||||
|
||||
|
||||
49
src/lib/polling.ts
Normal file
49
src/lib/polling.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
type callback = (...args: any[]) => void
|
||||
|
||||
class Polling {
|
||||
private static instance: Polling;
|
||||
private timeoutID: number;
|
||||
private delay: number = 1500;
|
||||
private enabled: boolean = false;
|
||||
constructor() {
|
||||
this.enabled = false;
|
||||
}
|
||||
static create(): Polling {
|
||||
this.instance = this.instance || new Polling();
|
||||
return this.instance;
|
||||
}
|
||||
|
||||
enable() {
|
||||
this.enabled = true;
|
||||
return this;
|
||||
}
|
||||
disable() {
|
||||
this.enabled = false;
|
||||
return this;
|
||||
}
|
||||
isEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
setDelay(time: number = 1500): Polling {
|
||||
this.delay = time;
|
||||
return this;
|
||||
}
|
||||
|
||||
run(callback: callback, ...args: any[]) {
|
||||
this.clear().enable()
|
||||
callback(...args);
|
||||
let timeoutHandler = () => {
|
||||
if (this.enabled) {
|
||||
this.run(callback, ...args);
|
||||
}
|
||||
}
|
||||
this.timeoutID = window.setTimeout(timeoutHandler, this.delay);
|
||||
}
|
||||
clear() {
|
||||
if (this.timeoutID)
|
||||
window.clearTimeout(this.timeoutID);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export default Polling;
|
||||
@@ -6,7 +6,7 @@ import "toastify-js/src/toastify.css"
|
||||
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
|
||||
import contentTable from '../lib/contentTable';
|
||||
import Http from '../lib/http'
|
||||
|
||||
import Polling from "../lib/polling";
|
||||
const helper = {
|
||||
vue: {},
|
||||
addVue(name, object) {
|
||||
@@ -16,12 +16,45 @@ const helper = {
|
||||
return helper.vue[name];
|
||||
},
|
||||
generateUrl: generateUrl,
|
||||
loop(callback, delay, ...args) {
|
||||
callback(...args);
|
||||
clearTimeout(helper.timeoutID);
|
||||
this.polling(callback, delay, ...args);
|
||||
isPolling() {
|
||||
return Polling.create().isEnabled();
|
||||
},
|
||||
enabePolling() {
|
||||
Polling.create().enable();
|
||||
},
|
||||
disablePolling() {
|
||||
Polling.create().disable().clear();
|
||||
},
|
||||
polling(delay = 1500, path) {
|
||||
Polling.create().setDelay(delay).run(helper.refresh, path);
|
||||
},
|
||||
scanFolder(path = "/apps/ncdownloader/scanfolder") {
|
||||
let url = helper.generateUrl(path);
|
||||
return new Promise((resolve) => {
|
||||
Http.getInstance(url).setMethod('GET').setHandler(function (data) {
|
||||
resolve(data.status);
|
||||
}).send();
|
||||
});
|
||||
},
|
||||
pollingFolder(delay = 1500) {
|
||||
Polling.create().setDelay(delay).run(helper.scanFolder);
|
||||
},
|
||||
pollingYoutube(delay = 1500) {
|
||||
Polling.create().setDelay(delay).run(helper.refresh, "/apps/ncdownloader/youtube/get");
|
||||
},
|
||||
refresh(path) {
|
||||
path = path || "/apps/ncdownloader/status/active";
|
||||
let url = helper.generateUrl(path);
|
||||
Http.getInstance(url).setHandler(function (data) {
|
||||
if (data && data.row) {
|
||||
contentTable.getInstance(data.title, data.row).create();
|
||||
} else {
|
||||
contentTable.getInstance().noData();
|
||||
}
|
||||
if (data.counter)
|
||||
helper.updateCounter(data.counter);
|
||||
}).send();
|
||||
},
|
||||
enabledPolling: 0,
|
||||
trim(string, char) {
|
||||
return string.split(char).filter(Boolean).join(char)
|
||||
},
|
||||
@@ -32,15 +65,6 @@ const helper = {
|
||||
ucfirst(string) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1)
|
||||
},
|
||||
polling(callback, delay, ...args) {
|
||||
self = this;
|
||||
helper.timeoutID = setTimeout(function () {
|
||||
if (self.enabledPolling) {
|
||||
callback(...args);
|
||||
self.polling(callback, delay, ...args);
|
||||
}
|
||||
}, delay);
|
||||
},
|
||||
isURL(url) {
|
||||
let regex = '^((https?|ftp)://)([a-z0-9-]+\.)?(?:[-a-zA-Z0-9()@:%_\+.~#?&/=]+)$';
|
||||
const pattern = new RegExp(regex, 'i');
|
||||
@@ -115,19 +139,6 @@ const helper = {
|
||||
counter.innerHTML = '<div class="number">' + data[key] + '</div>';
|
||||
}
|
||||
},
|
||||
refresh(path) {
|
||||
path = path || "/apps/ncdownloader/status/active";
|
||||
let url = helper.generateUrl(path);
|
||||
Http.getInstance(url).setHandler(function (data) {
|
||||
if (data && data.row) {
|
||||
contentTable.getInstance(data.title, data.row).create();
|
||||
} else {
|
||||
contentTable.getInstance().noData();
|
||||
}
|
||||
if (data.counter)
|
||||
helper.updateCounter(data.counter);
|
||||
}).send();
|
||||
},
|
||||
html2DOM: function (htmlString) {
|
||||
const parser = new window.DOMParser();
|
||||
let doc = parser.parseFromString(htmlString, "text/html")
|
||||
@@ -253,6 +264,18 @@ const helper = {
|
||||
resetSearch: function (vm) {
|
||||
vm.$data.loading = 0;
|
||||
contentTable.getInstance([], []).clear();
|
||||
},
|
||||
redirect(url) {
|
||||
window.location.href = url;
|
||||
},
|
||||
getContentTableType() {
|
||||
let container = document.getElementById("ncdownloader-table-wrapper");
|
||||
return container.getAttribute("type");
|
||||
},
|
||||
setContentTableType(name) {
|
||||
let container = document.getElementById("ncdownloader-table-wrapper");
|
||||
container.setAttribute("type", name);
|
||||
container.className = "table " + name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user