/* 移动版 */ var system = { version: "1.6.0 beta", rootPath: "tr-web-control/", codeupdate: "20180906", configHead: "transmission-web-control", config: { autoReload: true, reloadStep: 5000, 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 (label, text) { if (window.console) { if (window.console.log) { window.console.log(label, text); } } }, setlang: function (lang, callback) { // 如果未指定语言,则获取当前浏览器默认语言 if (!lang) { if (this.config.defaultLang) lang = this.config.defaultLang; else lang = navigator.language || navigator.browserLanguage; //this.debug("lang",lang); } if (!lang) lang = "zh-CN"; // 如果语言代码中包含-,则需要将后半部份转为大写 if (lang.indexOf("-") != -1) { // 因linux对文件有大小写限制,故重新赋值 lang = lang.split("-")[0].toLocaleLowerCase() + "-" + lang.split("-")[1].toLocaleUpperCase(); } // 如果该语言包没有定义,则使用英文 if (!this.languages[lang]) { lang = "en"; } // 统一使用 _ 替代 - lang = lang.replace("-", "_"); $.getJSON(system.rootPath + "i18n/" + lang + ".json", function (result) { if (result) { system.lang = $.extend(true, system.defaultLang, result); } system.resetLangText(); if (callback) callback(); }); }, // 设置语言信息 resetLangText: function () { var items = $("*[system-lang]"); $.each(items, function (key, item) { var name = $(item).attr("system-lang"); $(item).html(eval("system.lang." + name)); }); }, init: function (lang, islocal) { this.readConfig(); transmission.options.getFolders = false; if (this.lang == null) { this.setlang(lang, function () { system.initdata() }); } else { this.initdata(); } }, initdata: function () { $(document).attr("title", this.lang.system.title + " " + this.version); //this.control.torrentlist = $("#content-torrent-list ul"); this.control.torrentlist = $("#torrent-list"); this.connect(); }, // 从 cookies 里加载配置 readConfig: function () { // 将原来的cookies的方式改为本地存储的方式 var config = this.getStorageData(this.configHead + '.system'); if (config) { this.config = $.extend(this.config, JSON.parse(config)); } }, // 在 cookies 里保存参数 saveConfig: function () { this.setStorageData(this.configHead + '.system', JSON.stringify(this.config)); }, getStorageData: function (key, defaultValue) { return (window.localStorage[key] == null ? defaultValue : window.localStorage[key]); }, setStorageData: function (key, value) { window.localStorage[key] = value; }, // 连接服务器 connect: function () { // 当种子总数发生变化时,重新获取种子信息 transmission.on.torrentCountChange = function () { system.reloadTorrentBaseInfos(); }; // 提交错误时 transmission.on.postError = function () { //system.reloadTorrentBaseInfos(); }; // 初始化连接 transmission.init({ islocal: true }, function () { system.reloadSession(true); system.getServerStatus(); }); }, // 重新加载服务器信息 reloadSession: function (isinit) { transmission.getSession(function (result) { system.serverConfig = result; if (result["alt-speed-enabled"] == true) { $("#status_alt_speed").show(); } else { $("#status_alt_speed").hide(); } system.downloadDir = result["download-dir"]; // rpc-version 版本为 15 起,不再提供 download-dir-free-space 参数,需从新的方法获取 if (parseInt(system.serverConfig["rpc-version"]) >= 15) { transmission.getFreeSpace(system.downloadDir, function (result) { system.serverConfig["download-dir-free-space"] = result.arguments["size-bytes"]; system.showFreeSpace(result.arguments["size-bytes"]); }); } else { system.showFreeSpace(system.serverConfig["download-dir-free-space"]); } }); }, showFreeSpace: function (size) { var tmp = size; if (tmp == -1) { tmp = system.lang["public"]["text-unknown"]; } else { tmp = formatSize(tmp); } $("#status_freespace").text(tmp); }, // 获取服务器当前状态 getServerStatus: function () { if (this.reloading) return; clearTimeout(this.autoReloadTimer); this.reloading = true; transmission.getStatus(function (data) { system.reloading = false; $("#status_downloadspeed").html(formatSize(data["downloadSpeed"], false, "speed")); $("#status_uploadspeed").html(formatSize(data["uploadSpeed"], false, "speed")); system.serverSessionStats = data; }); }, // 重新获取种子信息 reloadTorrentBaseInfos: function (ids) { if (this.reloading) return; clearTimeout(this.autoReloadTimer); this.reloading = true; var oldInfos = { trackers: transmission.trackers, folders: transmission.torrents.folders } // 获取所有种子id信息 transmission.torrents.getallids(function (resultTorrents) { var ignore = new Array(); for (var index in resultTorrents) { var item = resultTorrents[index]; ignore.push(item.id); } // 错误的编号列表 var errorIds = transmission.torrents.getErrorIds(ignore, true); if (errorIds.length > 0) { transmission.torrents.getallids(function () { system.resetTorrentInfos(oldInfos); }, errorIds); } else { system.resetTorrentInfos(oldInfos); } }, ids); }, // resetTorrentInfos: function (oldInfos) { var currentTorrentId = this.currentTorrentId; // 已暂停 if (transmission.torrents.status[transmission._status.stopped]) { this.updateCount("paused", transmission.torrents.status[transmission._status.stopped].length); } else { this.updateCount("paused", 0); } // 做种 if (transmission.torrents.status[transmission._status.seed]) { this.updateCount("sending", transmission.torrents.status[transmission._status.seed].length); } else { this.updateCount("sending", 0); } // 校验 if (transmission.torrents.status[transmission._status.check]) { this.updateCount("check", transmission.torrents.status[transmission._status.check].length); } else { this.updateCount("check", 0); } // 下载中 if (transmission.torrents.status[transmission._status.download]) { this.updateCount("downloading", transmission.torrents.status[transmission._status.download].length); } else { 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 _config = this.currentContentConfig; _config.reload = true; this.showContent(_config); } }, // 更新状态中 updateCount: function (nodeId, count) { var item = $("#count-" + nodeId); item.text(count); if (count == 0) { item.hide(); } else item.show(); }, // 重新加载数据 reloadData: function () { this.reloadSession(); this.reloading = false; this.getServerStatus(); this.reloading = false; this.reloadTorrentBaseInfos(); }, // 显示指定的内容 showContent: function (target) { var _default = { page: "", type: "", data: "", title: this.lang.system.title, reload: false, callback: null }; var config = null; if (typeof (target) == "string") { _default.page = target; config = _default; } else config = jQuery.extend(_default, target); if (config.page == this.currentContentPage && !config.reload) { return; } $("#content-" + config.page).show(); if (config.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(); if (!this.torrentListChecked) $("#torrent-toolbar").hide(); this.currentContentPage = config.page; switch (config.type) { case "torrent-list": config.title = this.lang.tree[config.data]; this.loadTorrentToList({ target: config.data }); break; } $("#page-title").text(config.title); config.reload = false; this.currentContentConfig = config; if (config.callback) config.callback(); }, getTorrentFromType: function (type) { var torrents = null; switch (type) { case "torrent-all": case "all": case "servers": torrents = transmission.torrents.all; break; case "paused": torrents = transmission.torrents.status[transmission._status.stopped]; break; case "sending": torrents = transmission.torrents.status[transmission._status.seed]; break; case "seedwait": torrents = transmission.torrents.status[transmission._status.seedwait]; break; case "check": torrents = transmission.torrents.status[transmission._status.check]; break; case "checkwait": torrents = transmission.torrents.status[transmission._status.checkwait]; break; case "downloading": torrents = transmission.torrents.status[transmission._status.download]; break; case "downloadwait": torrents = transmission.torrents.status[transmission._status.downloadwait]; break; case "actively": torrents = transmission.torrents.actively; break; case "error": torrents = transmission.torrents.error; break; case "warning": torrents = transmission.torrents.warning; break; case "search-result": torrents = transmission.torrents.searchResult; break; default: break; } return torrents; }, // 加载种子列表 loadTorrentToList: function (config) { // 如果有种子选中,则不重新加载列表 if (this.torrentListChecked) return; if (!transmission.torrents.all) { return; } var def = { node: null, page: 1, target: "all" }; jQuery.extend(def, config); if (!config.target) return; var torrents = this.getTorrentFromType(config.target); this.config.defaultSelectNode = config.target; this.saveConfig(); var datas = new Array(); this.control.torrentlist.empty(); for (var index in torrents) { if (!torrents[index]) { continue; } var percentDone = parseFloat(torrents[index].percentDone * 100).toFixed(2); var status = this.lang.torrent["status-text"][torrents[index].status]; if (torrents[index].error != 0) { status = "" + status + ""; } else if (torrents[index].warning) { status = "" + status + ""; } var data = { id: torrents[index].id, name: this.getTorrentNameBar(torrents[index]), totalSize: torrents[index].totalSize, percentDone: this.getTorrentProgressBar(percentDone, torrents[index]), percentDoneNumber: percentDone, status: status, addedDate: formatLongTime(torrents[index].addedDate), completeSize: (torrents[index].totalSize - torrents[index].leftUntilDone), rateDownload: torrents[index].rateDownload, rateUpload: torrents[index].rateUpload, leecherCount: torrents[index].leecher, seederCount: torrents[index].seeder, uploadRatio: torrents[index].uploadRatio, uploadedEver: torrents[index].uploadedEver }; datas.push(data); //this.appendTorrentToList(data); } if (datas.length == 0) { setTimeout(function () { system.showContent('home'); }, 100); return; } if (this.torrentPager.onGotoPage == null) { this.torrentPager.onGotoPage = function (datas) { system.control.torrentlist.empty(); $("#torrent-toolbar").hide(); for (var key in datas) { system.appendTorrentToList(datas[key]); } // 刷新列表并指定事件 $(system.control.torrentlist).listview('refresh').find("input[type='checkbox']").click(function () { system.changeTorrentToolbar(this, data); if (system.torrentListChecked) { system.control.torrentlist.find("a[name='torrent']").css("marginLeft", "0px"); } else { system.control.torrentlist.find("a[name='torrent']").css("marginLeft", "0px"); } }).checkboxradio(); } } this.torrentPager.setDatas(datas, config.target); }, // 添加种子信息到列表 appendTorrentToList: function (data) { var replaces = { id: data.id, name: data.name, rateDownload: formatSize(data.rateDownload, false, "speed"), rateUpload: formatSize(data.rateUpload, false, "speed"), completeSize: formatSize(data.completeSize), totalSize: formatSize(data.totalSize), percentDone: data.percentDone }; // 由于不能以对象的方式来创建 listview 子项,所以只能用拼接字符串的方式 var templates = "