dialog-torrent-add.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <div class="easyui-layout" data-options="fit:true" style="width:100%;height:100%;">
  2. <div data-options="region:'center'" style="padding:5px 6px 0px 6px;border:0px;">
  3. <div id="" class="dialog" style="width:100%;padding:0px;">
  4. <div id="dialog-torrent-add-tip-title" style="border:1px #c0c0c0 solid;background-color: #ffffcc;padding:3px;width:99%;color:black;"></div>
  5. <table style="width:100%;">
  6. <tr>
  7. <td width="20%" class="title"><span id="dialog-torrent-add-download-dir"></span></td>
  8. <td width="80%">
  9. <select id="download-dir" style="width:450px;"></select>
  10. <input type="checkbox" id="set-default-download-dir" style="width:20px;"/><label for="set-default-download-dir" id="dialog-torrent-add-set-default-download-dir"></label>
  11. </td>
  12. </tr>
  13. <tr>
  14. <td class="title"><label id="dialog-torrent-add-upload-file" for="torrent_upload_file"></label></td>
  15. <td>
  16. <form action="#" method="post" id="torrent_upload_form" enctype="multipart/form-data" target="torrent_upload_frame">
  17. <input type="file" name="torrent_files[]" id="torrent_upload_file" multiple="multiple" />
  18. </form>
  19. </td>
  20. </tr>
  21. <tr>
  22. <td class="title"><span id="dialog-torrent-add-torrent-url"></span></td>
  23. <td>
  24. <textarea id="torrent_url" style="height:130px;"></textarea>
  25. <span id="dialog-torrent-add-tip-torrent-url"></span>
  26. </td>
  27. </tr>
  28. <tr>
  29. <td colspan="2">
  30. <hr/>
  31. </td>
  32. </tr>
  33. <tr>
  34. <td class="title"><span id="dialog-torrent-add-autostart"></span></td>
  35. <td>
  36. <input type="checkbox" id="chkautostart" style="width:20px;"/><label for="chkautostart" id="dialog-torrent-add-tip-autostart"></label>
  37. </td>
  38. </tr>
  39. </table>
  40. </div>
  41. <!-- label -->
  42. <div data-options="region:'center'" style="padding:3px;border:0px;overflow: hidden;">
  43. <div id="label_page" class="dialog" style="width:100%;padding:0px;height:50%;border-bottom: 1px dotted #ccc;">
  44. <div style="position: absolute;width:50%;height: 100%;">
  45. <div system-lang="dialog['torrent-setLabels']['available']"></div>
  46. <div id="divAvailableList" style="padding-top: 5px;"></div>
  47. </div>
  48. <div style="position: absolute;left:50%;width:50%;height: 50%;border-left: 1px dotted #ccc;padding-left: 5px;">
  49. <div system-lang="dialog['torrent-setLabels']['selected']"></div>
  50. <div id="divSelectedList" style="padding-top: 5px;"></div>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55. <div data-options="region:'south',border:false" style="text-align:right;padding:6px;">
  56. <span id="dialog-torrent-add-nosource" style="display:none;"></span>
  57. <span id="dialog-torrent-add-queue" style="display:none;"></span>
  58. <a id="torrent-button-ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" href="javascript:void(0);">Ok</a>
  59. <a id="torrent-button-cancel" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" href="javascript:void(0);">Cancel</a>
  60. </div>
  61. </div>
  62. <script type="text/javascript">
  63. (function(thisDialog){
  64. var title = "download-dir,torrent-url,tip-torrent-url,autostart,tip-autostart,set-default-download-dir,upload-file,nosource,tip-title".split(",");
  65. $.each(title, function(i, item){
  66. thisDialog.find("#dialog-torrent-add-"+item).html(system.lang.dialog["torrent-add"][item]);
  67. });
  68. title = "button-ok,button-cancel".split(",");
  69. $.each(title, function(i, item){
  70. thisDialog.find("#torrent-"+item).html(system.lang.dialog.public[item]);
  71. });
  72. thisDialog.find("#download-dir").val(system.downloadDir);
  73. var downloadDirs = $.merge([],transmission.downloadDirs);
  74. if (system.dictionary.folders != null && system.dictionary.folders != "")
  75. {
  76. $.merge(downloadDirs,system.dictionary.folders.split("\n"));
  77. }
  78. downloadDirs = downloadDirs.map(item => {
  79. return item[item.length - 1] == '/' ? item.slice(0, -1) : item;
  80. });
  81. downloadDirs = uniq(downloadDirs);
  82. if (system.config.hideSubfolders == false && system.currentListDir != null && system.currentListDir != "") {
  83. // 增加 当前数据目录为第一候选
  84. downloadDirs.unshift(system.currentListDir);
  85. }
  86. if (downloadDirs == null)
  87. {
  88. $("<option/>").text(system.downloadDir).val(system.downloadDir).attr("selected",true).appendTo(thisDialog.find("#download-dir"));
  89. } else {
  90. $.each(downloadDirs, function(i, item){
  91. $("<option/>").text(item).val(item).attr("selected",(item==system.downloadDir?true:false)).appendTo(thisDialog.find("#download-dir"));
  92. });
  93. }
  94. thisDialog.find("#chkautostart").prop("checked", system.serverConfig["start-added-torrents"]);
  95. thisDialog.find("#download-dir").combobox();
  96. // label
  97. var labelPage = {
  98. divSelectedList: $("#divSelectedList", thisDialog),
  99. init: function() {
  100. if(system.config.nav.labels){
  101. system.resetLangText(thisDialog);
  102. this.initLabels();
  103. }
  104. else{
  105. $("#label_page").css("display", "none");
  106. }
  107. },
  108. // 初始化标签列表
  109. initLabels: function() {
  110. var box = $("#divAvailableList", thisDialog);
  111. for (var index = 0; index < system.config.labels.length; index++) {
  112. var item = system.config.labels[index];
  113. if (item) {
  114. item.index = index;
  115. var label = this.createLabel(item);
  116. label.on("click", function(){
  117. labelPage.setLabel("#divAvailableList", "#divSelectedList", $(this).data("label"));
  118. $(this).remove();
  119. }).appendTo(box);
  120. }
  121. }
  122. },
  123. createLabel: function(label) {
  124. return $("<a href='javascript:void(0);' class='user-label-big label-index-"+label.index+"'/>").data("label", label).html(label.name).css({
  125. "background-color": label.color,
  126. "color": (getGrayLevel(label.color) > 0.5 ? "#000" : "#fff")
  127. });
  128. },
  129. setLabel: function(from, to, item) {
  130. var box = $(to, thisDialog);
  131. var label = this.createLabel(item);
  132. label.on("click", function(){
  133. labelPage.setLabel(to, from, $(this).data("label"));
  134. $(this).remove();
  135. }).appendTo(box);
  136. },
  137. getLabels: function() {
  138. if(!system.config.nav.labels)
  139. return null;
  140. var items = $(".user-label-big", this.divSelectedList);
  141. var labels = [];
  142. for (var index = 0; index < items.length; index++) {
  143. var item = $(items[index]);
  144. var label = item.data("label");
  145. labels.push(label.index);
  146. }
  147. return labels;
  148. }
  149. };
  150. labelPage.init();
  151. // Confirm
  152. thisDialog.find("#torrent-button-ok").click(function()
  153. {
  154. var dir = thisDialog.find("#download-dir").combobox("getValue");
  155. var olddir = system.downloadDir;
  156. var isnewdir = system.serverConfig["download-dir"]!=dir;
  157. var isupload = thisDialog.find("#torrent_upload_file")[0].value!="";
  158. var autostart = thisDialog.find("#chkautostart").prop("checked");
  159. if (autostart!=system.serverConfig["start-added-torrents"])
  160. {
  161. system.serverConfig["start-added-torrents"] = autostart;
  162. // Start setting parameters
  163. transmission.exec(
  164. {
  165. method:"session-set"
  166. ,arguments: {
  167. "start-added-torrents": autostart
  168. }
  169. }
  170. );
  171. }
  172. var button = $(this);
  173. var icon = button.linkbutton("options").iconCls;
  174. button.linkbutton({disabled:true,iconCls:"icon-loading"});
  175. if (thisDialog.find("#set-default-download-dir").prop("checked")&&isnewdir)
  176. {
  177. updateDownloadDir(dir,function(){
  178. if (isupload)
  179. {
  180. uploadFile(!autostart,function(){
  181. button.linkbutton({iconCls:icon,disabled:false});
  182. });
  183. thisDialog.dialog("close");
  184. }
  185. });
  186. }
  187. var url = thisDialog.find("#torrent_url").val();
  188. // When there is a file to upload
  189. if (isupload)
  190. {
  191. // rpc-version 15 stopped using the ../upload interface
  192. if (system.serverConfig["rpc-version"]>=15)
  193. {
  194. var uploaded = 0;
  195. system.uploadTorrentFile("torrent_upload_file",dir,!autostart,function(data,count){
  196. button.linkbutton({iconCls:icon,disabled:false});
  197. uploaded++;
  198. thisDialog.find("#dialog-torrent-add-queue").html(uploaded+"/"+count).show();
  199. if (uploaded==count)
  200. {
  201. thisDialog.find("#dialog-torrent-add-queue").html("").hide();
  202. thisDialog.dialog("close");
  203. }
  204. if(system.config.nav.labels && data.hashString != null){
  205. system.saveLabelsConfig(data.hashString, labelPage.getLabels());
  206. system.saveConfig();
  207. }
  208. system.reloadData();
  209. });
  210. return;
  211. }
  212. // New directory, but no hooks to save when you first save the directory
  213. if (isnewdir)
  214. {
  215. updateDownloadDir(dir,function(){
  216. if (isupload)
  217. {
  218. uploadFile(!autostart,function(){
  219. button.linkbutton({iconCls:icon,disabled:false});
  220. updateDownloadDir(olddir);
  221. });
  222. //thisDialog.dialog("close");
  223. }
  224. });
  225. }
  226. else
  227. {
  228. uploadFile(!autostart);
  229. //thisDialog.dialog("close");
  230. }
  231. }
  232. else if (url!="")
  233. {
  234. var urls = url.split("\n");
  235. system.addTorrentsToServer(urls,urls.length,autostart,dir,labelPage.getLabels());
  236. urls = null;
  237. thisDialog.dialog("close");
  238. }
  239. else if(!isupload)
  240. {
  241. thisDialog.find("#dialog-torrent-add-nosource").fadeInAndOut();
  242. }
  243. });
  244. thisDialog.find("#torrent-button-cancel").click(function()
  245. {
  246. thisDialog.dialog("close");
  247. });
  248. // Save the upload directory
  249. function updateDownloadDir(dir,callback)
  250. {
  251. transmission.exec(
  252. {
  253. method:"session-set"
  254. ,arguments:{"download-dir":dir}
  255. }
  256. ,function(data){
  257. if (data.result=="success")
  258. {
  259. system.downloadDir = dir;
  260. system.serverConfig["download-dir"] = dir;
  261. if (callback)
  262. {
  263. callback();
  264. }
  265. }
  266. }
  267. );
  268. }
  269. // Upload files
  270. function uploadFile(paused,callback){
  271. var options = {
  272. // The upload should be by TR interface
  273. url:"../upload?paused=" + paused
  274. ,type:"POST"
  275. ,dataType:"json"
  276. ,data:{
  277. "X-Transmission-Session-Id":transmission.SessionId
  278. }
  279. ,success:function(data){
  280. //alert(data);
  281. if (callback)
  282. callback();
  283. if(system.config.nav.labels && data.hashString != null){
  284. system.saveLabelsConfig(data.hashString, labelPage.getLabels());
  285. system.saveConfig();
  286. }
  287. thisDialog.dialog("close");
  288. system.reloadData();
  289. }
  290. ,error:function(data){
  291. //alert("error:"+data);
  292. console.log("data:",data);
  293. }
  294. };
  295. $('#torrent_upload_form').ajaxSubmit(options);
  296. }
  297. })($("#dialog-torrent-add"));
  298. </script>