system.mobile.min.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. var system = {
  2. 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) {
  3. window.console && window.console.log &&
  4. window.console.log(a, b)
  5. }, 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 () {
  6. var a = $("*[system-lang]");
  7. $.each(a, function (b, c) { var d = $(c).attr("system-lang"); $(c).html(eval("system.lang." + d)) })
  8. }, 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 () {
  9. var a = this.getStorageData(this.configHead + ".system"); if (a) this.config = $.extend(this.config,
  10. JSON.parse(a))
  11. }, 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 () {
  12. transmission.getSession(function (a) {
  13. system.serverConfig =
  14. 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"])
  15. })
  16. }, showFreeSpace: function (a) {
  17. a = a; a = a == -1 ? system.lang["public"]["text-unknown"] :
  18. formatSize(a); $("#status_freespace").text(a)
  19. }, 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) {
  20. if (!this.reloading) {
  21. clearTimeout(this.autoReloadTimer); this.reloading = true; var b = {
  22. trackers: transmission.trackers,
  23. folders: transmission.torrents.folders
  24. }; 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)
  25. }
  26. }, resetTorrentInfos: function () {
  27. 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] ?
  28. 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",
  29. 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) }
  30. }, updateCount: function (a, b) {
  31. var c =
  32. $("#count-" + a); c.text(b); b == 0 ? c.hide() : c.show()
  33. }, reloadData: function () { this.reloadSession(); this.reloading = false; this.getServerStatus(); this.reloading = false; this.reloadTorrentBaseInfos() },
  34. showContent: function (a) {
  35. var b = { page: "", type: "", data: "", title: this.lang.system.title, reload: false, callback: null }, c = null;
  36. if (typeof a == "string") { b.page = a; c = b } else c = jQuery.extend(b, a);
  37. if (!(c.page == this.currentContentPage && !c.reload)) {
  38. $("#content-" + c.page).show(); if (c.page != this.currentContentPage) {
  39. $("#content-" + this.currentContentPage).hide();
  40. this.control.torrentlist.find("input:checked").prop("checked", false).checkboxradio("refresh");
  41. this.torrentListChecked = false
  42. }
  43. $("#torrent-page-bar").hide(); this.torrentListChecked || $("#torrent-toolbar").hide();
  44. this.currentContentPage = c.page;
  45. switch (c.type) {
  46. case "torrent-list": c.title = this.lang.tree[c.data];
  47. this.loadTorrentToList({ target: c.data })
  48. }
  49. $("#page-title").text(c.title);
  50. c.reload = false;
  51. this.currentContentConfig = c;
  52. c.callback && c.callback()
  53. }
  54. },
  55. getTorrentFromType: function (a) {
  56. var b = null;
  57. switch (a) {
  58. // 多个 case 共享的情况
  59. case "torrent-all":
  60. case "all":
  61. case "servers":
  62. b = transmission.torrents.all;
  63. break;
  64. //
  65. case "paused":
  66. b = transmission.torrents.status[transmission._status.stopped];
  67. break;
  68. case "sending":
  69. b = transmission.torrents.status[transmission._status.seed];
  70. break;
  71. case "seedwait":
  72. b = transmission.torrents.status[transmission._status.seedwait];
  73. break;
  74. case "check":
  75. b = transmission.torrents.status[transmission._status.check];
  76. break;
  77. case "checkwait":
  78. b = transmission.torrents.status[transmission._status.checkwait];
  79. break;
  80. case "downloading":
  81. b = transmission.torrents.status[transmission._status.download];
  82. break;
  83. case "downloadwait":
  84. b = transmission.torrents.status[transmission._status.downloadwait];
  85. break;
  86. case "actively":
  87. b = transmission.torrents.actively;
  88. break;
  89. case "error":
  90. b = transmission.torrents.error; break;
  91. case "warning":
  92. b = transmission.torrents.warning;
  93. break;
  94. case "search-result":
  95. b = transmission.torrents.searchResult
  96. }
  97. return b
  98. },
  99. loadTorrentToList: function (a) {
  100. // console.log('transmission', transmission)
  101. if (!this.torrentListChecked) if (transmission.torrents.all) {
  102. jQuery.extend({ node: null, page: 1, target: "all" }, a);
  103. if (a.target) {
  104. var b = this.getTorrentFromType(a.target);
  105. this.config.defaultSelectNode = a.target;
  106. this.saveConfig();
  107. var c = [];
  108. this.control.torrentlist.empty();
  109. for (var d in b) if (b[d]) {
  110. var e = parseFloat(b[d].percentDone * 100).toFixed(2), f = this.lang.torrent["status-text"][b[d].status];
  111. if (b[d].error != 0) f = "<span class='text-status-error'>" + f + "</span>";
  112. else if (b[d].warning) f = "<span class='text-status-warning' title='" + b[d].warning + "'>" + f + "</span>";
  113. var g = {
  114. id: b[d].id,
  115. name: this.getTorrentNameBar(b[d]),
  116. totalSize: b[d].totalSize,
  117. percentDone: this.getTorrentProgressBar(e, b[d]),
  118. percentDoneNumber: e,
  119. status: f,
  120. addedDate: formatLongTime(b[d].addedDate),
  121. completeSize: b[d].totalSize - b[d].leftUntilDone,
  122. rateDownload: b[d].rateDownload,
  123. rateUpload: b[d].rateUpload,
  124. leecherCount: b[d].leecher,
  125. seederCount: b[d].seeder,
  126. uploadRatio: b[d].uploadRatio,
  127. uploadedEver: b[d].uploadedEver,
  128. trackers: b[d].trackers,
  129. trackerStats: b[d].trackerStats
  130. }; c.push(g)
  131. }
  132. if (c.length == 0) setTimeout(function () { system.showContent("home") }, 100);
  133. else {
  134. if (this.torrentPager.onGotoPage == null) this.torrentPager.onGotoPage = function (h) {
  135. system.control.torrentlist.empty();
  136. $("#torrent-toolbar").hide();
  137. for (var i in h) system.appendTorrentToList(h[i]);
  138. $(system.control.torrentlist).listview("refresh").find("input[type='checkbox']").click(function () { system.changeTorrentToolbar(this, g);
  139. system.control.torrentlist.find("a[name='torrent']").css("marginLeft", "0px") }).checkboxradio()
  140. };
  141. this.torrentPager.setDatas(c, a.target)
  142. }
  143. }
  144. }
  145. },
  146. appendTorrentToList: function (a) {
  147. var b = {
  148. id: a.id,
  149. name: a.name,
  150. rateDownload: formatSize(a.rateDownload, false, "speed"),
  151. rateUpload: formatSize(a.rateUpload, false, "speed"),
  152. completeSize: formatSize(a.completeSize),
  153. totalSize: formatSize(a.totalSize),
  154. percentDone: a.percentDone,
  155. seederCount: a.seederCount,
  156. leecherCount: a.leecherCount,
  157. trackers: a.trackerStats[0].sitename,
  158. pt: this.siteNameToPTname(a.trackerStats[0].sitename),
  159. announceRes: a.trackerStats[0].lastAnnounceResult,
  160. showAnnounce: a.trackerStats[0].lastAnnounceResult == 'Success'? 'none' : 'block'
  161. };
  162. a = "<li id='li-torrent-$id$' torrentid='$id$' style='padding:0px;'>\
  163. <a name='torrent' style='padding:0px;margin-left:0px;'>\
  164. <label data-corners='false' style='margin:0px;border:0px;padding:0px;'>\
  165. <input type='checkbox' id='torrent-$id$'/>\
  166. <label for='torrent-$id$'>\
  167. <h3 style='margin:0px;'>$name$</h3>\
  168. <div style='padding:0px 10px 5px 0px;'>$percentDone$</div>\
  169. <p class='torrent-list-infos'>↓$rateDownload$ ($leecherCount$) <b>↑$rateUpload$</b> ($seederCount$) | $completeSize$/$totalSize$ | <b>$pt$</b> </p>\
  170. <p style='margin:0px; display:$showAnnounce$; color: red;'>$announceRes$</p>\
  171. </label></label>\
  172. </a><a class='more'></a>\
  173. ";
  174. a = a.replace(/\$([^\$]*)\$/g, function (c, d) { return b[d] });
  175. a = $(a);
  176. a.on("swiperight", function () {
  177. system.control.torrentlist.find("a[name='torrent']").css("marginLeft", "0px") });
  178. a.on("swipeleft", function () {
  179. system.control.torrentlist.find("a[name='torrent']").css("marginLeft", "0px")
  180. });
  181. a.appendTo(this.control.torrentlist)
  182. },
  183. siteNameToPTname: function(a){
  184. var site2PT = {
  185. "hdkylin": "麒麟",
  186. "hdkyl": "麒麟",
  187. "rousi":"肉丝",
  188. "icc2022":"冰淇淋",
  189. "czhch":"冰淇淋",
  190. "carpt":"车站",
  191. "jptv":"JPTV",
  192. "hamsters": "蝴蝶",
  193. "m-team": "馒头",
  194. "piggo": "猪猪",
  195. "pttime": "PTT",
  196. "xingtan": "杏坛",
  197. "gtk": "GTK",
  198. "nextpt": "飞天拉面",
  199. "connects":"飞天拉面",
  200. "crabpt": "蟹黄堡",
  201. "ubits": "U堡",
  202. "0ff": "农场"
  203. }
  204. var pt = site2PT[a]
  205. return pt
  206. },
  207. getTorrentNameBar: function (a) {
  208. var b = "", c = a.name; switch (a.status) {
  209. case transmission._status.stopped: b = "iconlabel icon-pause-small"; break; case transmission._status.check: b = "iconlabel icon-checking";
  210. break; case transmission._status.download: b = "iconlabel icon-down"; break; case transmission._status.seed: b = "iconlabel icon-up"; break; case transmission._status.seedwait: case transmission._status.downloadwait: case transmission._status.checkwait: b = "iconlabel icon-wait"
  211. }if (a.warning) { b = "iconlabel icon-warning-type1"; c += "\n\n" + this.lang["public"]["text-info"] + ": " + a.warning } if (a.error != 0) { b = "iconlabel icon-exclamation"; c += "\n\n" + this.lang["public"]["text-info"] + ": " + a.errorString } return '<span class="' + b +
  212. '" title="' + c + '">' + a.name + "</span>"
  213. }, getTorrentProgressBar: function (a, b) {
  214. a += "%"; var c = ""; switch (b.status) { case transmission._status.stopped: c = "torrent-progress-stop"; break; case transmission._status.checkwait: case transmission._status.check: c = "torrent-progress-check"; break; case transmission._status.downloadwait: case transmission._status.download: c = "torrent-progress-download"; break; case transmission._status.seedwait: case transmission._status.seed: c = "torrent-progress-seed" }if (b.warning) c = "torrent-progress-warning";
  215. if (b.error != 0) c = "torrent-progress-error"; return '<div class="torrent-progress" title="' + a + '"><div class="torrent-progress-text">' + a + '</div><div class="torrent-progress-bar ' + c + '" style="width:' + a + ';"></div></div>'
  216. }, changeTorrentToolbar: function (a, b) {
  217. var c = this.control.torrentlist.find("input:checked"); $("#torrent-checked-count").html(c.length); if (c.length > 0) { this.torrentListChecked = true; $("#torrent-toolbar").show() } else { this.torrentListChecked = false; $("#torrent-toolbar").hide() } if (b) this.currentTorrentId =
  218. b.id
  219. },
  220. torrentPager: {
  221. datas: null, pageSize: 30, pageNumber: 0, pageCount: 0, count: 0, onGotoPage: null, currentDatas: null, pageBar: null, controls: { prev: null, next: null, number: null }, head: "", init: function (a) {
  222. this.pageBar = $("#torrent-page-bar"); this.controls.next = this.pageBar.find("#page-next"); this.controls.next.click(function () { system.torrentPager.gotoPage("next") }); this.controls.prev = this.pageBar.find("#page-prev"); this.controls.prev.click(function () { system.torrentPager.gotoPage("prev") }); this.controls.number = this.pageBar.find("#page-number");
  223. a && this.setDatas(a)
  224. },
  225. setDatas: function (a, b) {
  226. this.datas || this.init();
  227. this.datas = a;
  228. this.pageBar.show();
  229. this.count = this.datas.length;
  230. this.pageCount = parseInt(this.count / this.pageSize);
  231. this.count % this.pageSize > 0 && this.pageCount++;
  232. this.pageCount == 1 && this.pageBar.hide();
  233. this.head == b ? this.gotoPage() : this.gotoPage(1);
  234. this.head = b },
  235. gotoPage: function (a) {
  236. if (typeof a == "number") this.pageNumber = a; else switch (a) { case "next": this.pageNumber++; break; case "prev": this.pageNumber-- }if (this.pageNumber > this.pageCount) this.pageNumber =
  237. this.pageCount; if (this.pageNumber < 1) this.pageNumber = 1; a = (this.pageNumber - 1) * parseInt(this.pageSize); var b = a + parseInt(this.pageSize); this.currentDatas = this.datas.slice(a, b); this.controls.number.text(this.pageNumber + "/" + this.pageCount); this.pageNumber > 1 ? this.controls.prev.show() : this.controls.prev.hide(); this.pageNumber < this.pageCount ? this.controls.next.show() : this.controls.next.hide(); this.onGotoPage && this.onGotoPage(this.currentDatas)
  238. }
  239. }, changeSelectedTorrentStatus: function (a, b, c) {
  240. var d = this.control.torrentlist.find("input:checked"),
  241. e = []; a || (a = "start"); for (var f = 0; f < d.length; f++)e.push(parseInt(d[f].id.replace("torrent-", ""))); if (e.length > 0) {
  242. arguments = { ids: e }; switch (a) { case "remove": arguments["delete-local-data"] = c.removeData; break; case "verify": if (e.length == 1) { if (transmission.torrents.all[e[0]].percentDone > 0) if (confirm(system.lang.toolbar.tip["recheck-confirm"]) == false) return } else if (confirm(system.lang.toolbar.tip["recheck-confirm"]) == false) return }b = $(b); b.attr("disabled", true); transmission.exec({ method: "torrent-" + a, arguments: arguments },
  243. function () { b.attr("disabled", false); system.reloadTorrentBaseInfos() }); this.torrentListChecked = false
  244. }
  245. }, addTorrentsToServer: function (a, b, c, d, e) { var f = b - a.length, g = a.shift(); if (g) { this.showStatus(this.lang.system.status.queue, b - f + 1); transmission.addTorrentFromUrl(g, d, c, function () { system.addTorrentsToServer(a, b, c, d, e) }) } else { this.showStatus(this.lang.system.status.queuefinish); this.getServerStatus(); e && e() } },
  246. showStatus: function (a, b) {
  247. if (a) {
  248. $("#status").show(); $("#status-msg").html(a); $.isNumeric(b) ? $("#status-count").html(b).show() :
  249. $("#status-count").hide()
  250. } else $("#status").hide()
  251. },
  252. uploadTorrentFile: function (fileInputId, savePath, paused, callback) {
  253. if (window.FileReader) {
  254. var files = $("input[id='" + fileInputId + "']")[0].files;
  255. $.each(files, (function (index, file) {
  256. transmission.addTorrentFromFile(file, savePath, paused, callback, files.length)
  257. }))
  258. } else alert(system.lang.public["text-browsers-not-support-features"])
  259. }
  260. }; $(document).ready(function () { $.getJSON(system.rootPath + "i18n/en.json").done(function (a) { system.defaultLang = a }); $.getJSON(system.rootPath + "i18n.json").done(function (a) { system.languages = a; system.init(location.search.getQueryString("lang"), location.search.getQueryString("local")) }) });