var system = { version: "1.6.0 beta", rootPath: "tr-web-control/", codeupdate: "20180906", configHead: "transmission-web-control", config: { autoReload: true, reloadStep: 5E3, pageSize: 30, defaultSelectNode: null }, lang: null, reloading: false, autoReloadTimer: null, downloadDir: "", islocal: false, B64: new Base64, currentTorrentId: 0, currentContentPage: "home", currentContentConfig: null, control: { tree: null, torrentlist: null }, serverConfig: null, serverSessionStats: null, torrentListChecked: false, debug: function (a, b) { window.console && window.console.log && window.console.log(a, b) }, setlang: function (a, b) { a || (a = this.config.defaultLang ? this.config.defaultLang : navigator.language || navigator.browserLanguage); a || (a = "zh-CN"); if (a.indexOf("-") != -1) a = a.split("-")[0].toLocaleLowerCase() + "-" + a.split("-")[1].toLocaleUpperCase(); this.languages[a] || (a = "en"); a = a.replace("-", "_"); $.getJSON(system.rootPath + "i18n/" + a + ".json", function (c) { if (c) system.lang = $.extend(true, system.defaultLang, c); system.resetLangText(); b && b() }) }, resetLangText: function () { var a = $("*[system-lang]"); $.each(a, function (b, c) { var d = $(c).attr("system-lang"); $(c).html(eval("system.lang." + d)) }) }, init: function (a) { this.readConfig(); transmission.options.getFolders = false; this.lang == null ? this.setlang(a, function () { system.initdata() }) : this.initdata() }, initdata: function () { $(document).attr("title", this.lang.system.title + " " + this.version); this.control.torrentlist = $("#torrent-list"); this.connect() }, readConfig: function () { var a = this.getStorageData(this.configHead + ".system"); if (a) this.config = $.extend(this.config, JSON.parse(a)) }, saveConfig: function () { this.setStorageData(this.configHead + ".system", JSON.stringify(this.config)) }, getStorageData: function (a, b) { return window.localStorage[a] == null ? b : window.localStorage[a] }, setStorageData: function (a, b) { window.localStorage[a] = b }, connect: function () { transmission.on.torrentCountChange = function () { system.reloadTorrentBaseInfos() }; transmission.on.postError = function () { }; transmission.init({ islocal: true }, function () { system.reloadSession(true); system.getServerStatus() }) }, reloadSession: function () { transmission.getSession(function (a) { system.serverConfig = a; a["alt-speed-enabled"] == true ? $("#status_alt_speed").show() : $("#status_alt_speed").hide(); system.downloadDir = a["download-dir"]; parseInt(system.serverConfig["rpc-version"]) >= 15 ? transmission.getFreeSpace(system.downloadDir, function (b) { system.serverConfig["download-dir-free-space"] = b.arguments["size-bytes"]; system.showFreeSpace(b.arguments["size-bytes"]) }) : system.showFreeSpace(system.serverConfig["download-dir-free-space"]) }) }, showFreeSpace: function (a) { a = a; a = a == -1 ? system.lang["public"]["text-unknown"] : formatSize(a); $("#status_freespace").text(a) }, getServerStatus: function () { if (!this.reloading) { clearTimeout(this.autoReloadTimer); this.reloading = true; transmission.getStatus(function (a) { system.reloading = false; $("#status_downloadspeed").html(formatSize(a.downloadSpeed, false, "speed")); $("#status_uploadspeed").html(formatSize(a.uploadSpeed, false, "speed")); system.serverSessionStats = a }) } }, reloadTorrentBaseInfos: function (a) { if (!this.reloading) { clearTimeout(this.autoReloadTimer); this.reloading = true; var b = { trackers: transmission.trackers, folders: transmission.torrents.folders }; transmission.torrents.getallids(function (c) { var d = [], e; for (e in c) d.push(c[e].id); c = transmission.torrents.getErrorIds(d, true); c.length > 0 ? transmission.torrents.getallids(function () { system.resetTorrentInfos(b) }, c) : system.resetTorrentInfos(b) }, a) } }, resetTorrentInfos: function () { transmission.torrents.status[transmission._status.stopped] ? this.updateCount("paused", transmission.torrents.status[transmission._status.stopped].length) : this.updateCount("paused", 0); transmission.torrents.status[transmission._status.seed] ? this.updateCount("sending", transmission.torrents.status[transmission._status.seed].length) : this.updateCount("sending", 0); transmission.torrents.status[transmission._status.check] ? this.updateCount("check", transmission.torrents.status[transmission._status.check].length) : this.updateCount("check", 0); transmission.torrents.status[transmission._status.download] ? this.updateCount("downloading", transmission.torrents.status[transmission._status.download].length) : this.updateCount("downloading", 0); this.updateCount("actively", transmission.torrents.actively.length); this.updateCount("error", transmission.torrents.error.length); this.updateCount("warning", transmission.torrents.warning.length); system.reloading = false; if (system.config.autoReload) system.autoReloadTimer = setTimeout(function () { system.reloadData() }, system.config.reloadStep); this.updateCount("all", transmission.torrents.count); if (this.currentContentPage == "torrent-list") { var a = this.currentContentConfig; a.reload = true; this.showContent(a) } }, updateCount: function (a, b) { var c = $("#count-" + a); c.text(b); b == 0 ? c.hide() : c.show() }, reloadData: function () { this.reloadSession(); this.reloading = false; this.getServerStatus(); this.reloading = false; this.reloadTorrentBaseInfos() }, showContent: function (a) { var b = { page: "", type: "", data: "", title: this.lang.system.title, reload: false, callback: null }, c = null; if (typeof a == "string") { b.page = a; c = b } else c = jQuery.extend(b, a); if (!(c.page == this.currentContentPage && !c.reload)) { $("#content-" + c.page).show(); if (c.page != this.currentContentPage) { $("#content-" + this.currentContentPage).hide(); this.control.torrentlist.find("input:checked").prop("checked", false).checkboxradio("refresh"); this.torrentListChecked = false } $("#torrent-page-bar").hide(); this.torrentListChecked || $("#torrent-toolbar").hide(); this.currentContentPage = c.page; switch (c.type) { case "torrent-list": c.title = this.lang.tree[c.data]; this.loadTorrentToList({ target: c.data }) } $("#page-title").text(c.title); c.reload = false; this.currentContentConfig = c; c.callback && c.callback() } }, getTorrentFromType: function (a) { var b = null; switch (a) { // 多个 case 共享的情况 case "torrent-all": case "all": case "servers": b = transmission.torrents.all; break; // case "paused": b = transmission.torrents.status[transmission._status.stopped]; break; case "sending": b = transmission.torrents.status[transmission._status.seed]; break; case "seedwait": b = transmission.torrents.status[transmission._status.seedwait]; break; case "check": b = transmission.torrents.status[transmission._status.check]; break; case "checkwait": b = transmission.torrents.status[transmission._status.checkwait]; break; case "downloading": b = transmission.torrents.status[transmission._status.download]; break; case "downloadwait": b = transmission.torrents.status[transmission._status.downloadwait]; break; case "actively": b = transmission.torrents.actively; break; case "error": b = transmission.torrents.error; break; case "warning": b = transmission.torrents.warning; break; case "search-result": b = transmission.torrents.searchResult } return b }, loadTorrentToList: function (a) { // console.log('transmission', transmission) if (!this.torrentListChecked) if (transmission.torrents.all) { jQuery.extend({ node: null, page: 1, target: "all" }, a); if (a.target) { var b = this.getTorrentFromType(a.target); this.config.defaultSelectNode = a.target; this.saveConfig(); var c = []; this.control.torrentlist.empty(); for (var d in b) if (b[d]) { var e = parseFloat(b[d].percentDone * 100).toFixed(2), f = this.lang.torrent["status-text"][b[d].status]; if (b[d].error != 0) f = "" + f + ""; else if (b[d].warning) f = "" + f + ""; var g = { id: b[d].id, name: this.getTorrentNameBar(b[d]), totalSize: b[d].totalSize, percentDone: this.getTorrentProgressBar(e, b[d]), percentDoneNumber: e, status: f, addedDate: formatLongTime(b[d].addedDate), completeSize: b[d].totalSize - b[d].leftUntilDone, rateDownload: b[d].rateDownload, rateUpload: b[d].rateUpload, leecherCount: b[d].leecher, seederCount: b[d].seeder, uploadRatio: b[d].uploadRatio, uploadedEver: b[d].uploadedEver, trackers: b[d].trackers, trackerStats: b[d].trackerStats }; c.push(g) } if (c.length == 0) setTimeout(function () { system.showContent("home") }, 100); else { if (this.torrentPager.onGotoPage == null) this.torrentPager.onGotoPage = function (h) { system.control.torrentlist.empty(); $("#torrent-toolbar").hide(); for (var i in h) system.appendTorrentToList(h[i]); $(system.control.torrentlist).listview("refresh").find("input[type='checkbox']").click(function () { system.changeTorrentToolbar(this, g); system.control.torrentlist.find("a[name='torrent']").css("marginLeft", "0px") }).checkboxradio() }; this.torrentPager.setDatas(c, a.target) } } } }, appendTorrentToList: function (a) { var b = { id: a.id, name: a.name, rateDownload: formatSize(a.rateDownload, false, "speed"), rateUpload: formatSize(a.rateUpload, false, "speed"), completeSize: formatSize(a.completeSize), totalSize: formatSize(a.totalSize), percentDone: a.percentDone, seederCount: a.seederCount, leecherCount: a.leecherCount, trackers: a.trackerStats[0].sitename, pt: this.siteNameToPTname(a.trackerStats[0].sitename), announceRes: a.trackerStats[0].lastAnnounceResult, showAnnounce: a.trackerStats[0].lastAnnounceResult == 'Success'? 'none' : 'block' }; a = "