dialog-torrent-addfile.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. <table style="width:100%;">
  5. <tr>
  6. <td width="20%" class="title"><span id="dialog-torrent-add-download-dir"></span></td>
  7. <td width="80%">
  8. <select id="download-dir" style="width:450px;"></select>
  9. <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>
  10. </td>
  11. </tr>
  12. <tr>
  13. <td class="title"><label id="dialog-torrent-add-upload-file" for="torrent_upload_file"></label></td>
  14. <td>
  15. <select id="torrent_upload_file" style="height:120px;" multiple="multiple"></select>
  16. </td>
  17. </tr>
  18. <tr>
  19. <td colspan="2">
  20. <hr/>
  21. </td>
  22. </tr>
  23. <tr>
  24. <td class="title"><span id="dialog-torrent-add-autostart"></span></td>
  25. <td>
  26. <input type="checkbox" id="chkautostart" style="width:20px;"/><label for="chkautostart" id="dialog-torrent-add-tip-autostart"></label>
  27. </td>
  28. </tr>
  29. </table>
  30. </div>
  31. <!-- label -->
  32. <div data-options="region:'center'" style="padding:3px;border:0px;overflow: hidden;">
  33. <div id="label_page" class="dialog" style="width:100%;padding:0px;height:50%;border-bottom: 1px dotted #ccc;">
  34. <div style="position: absolute;width:50%;height: 100%;">
  35. <div system-lang="dialog['torrent-setLabels']['available']"></div>
  36. <div id="divAvailableList" style="padding-top: 5px;"></div>
  37. </div>
  38. <div style="position: absolute;left:50%;width:50%;height: 50%;border-left: 1px dotted #ccc;padding-left: 5px;">
  39. <div system-lang="dialog['torrent-setLabels']['selected']"></div>
  40. <div id="divSelectedList" style="padding-top: 5px;"></div>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. <div data-options="region:'south',border:false" style="text-align:right;padding:6px;">
  46. <span id="dialog-torrent-add-queue" style="display:none;"></span>
  47. <a id="torrent-button-ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" href="javascript:void(0);">Ok</a>
  48. <a id="torrent-button-cancel" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" href="javascript:void(0);">Cancel</a>
  49. </div>
  50. </div>
  51. <script type="text/javascript">
  52. (function(thisDialog){
  53. var title = "download-dir,autostart,tip-autostart,set-default-download-dir,upload-file".split(",");
  54. $.each(title, function(i, item){
  55. thisDialog.find("#dialog-torrent-add-"+item).html(system.lang.dialog["torrent-add"][item]);
  56. });
  57. title = "button-ok,button-cancel".split(",");
  58. $.each(title, function(i, item){
  59. thisDialog.find("#torrent-"+item).html(system.lang.dialog.public[item]);
  60. });
  61. thisDialog.find("#download-dir").val(system.downloadDir);
  62. var downloadDirs = $.merge([],transmission.downloadDirs);
  63. if (system.dictionary.folders != null && system.dictionary.folders != "")
  64. {
  65. $.merge(downloadDirs,system.dictionary.folders.split("\n"));
  66. }
  67. downloadDirs = downloadDirs.map(item => {
  68. return item[item.length - 1] == '/' ? item.slice(0, -1) : item;
  69. });
  70. downloadDirs = uniq(downloadDirs);
  71. if (system.config.hideSubfolders == false && system.currentListDir != null && system.currentListDir != "") {
  72. // 增加 当前数据目录为第一候选
  73. downloadDirs.unshift(system.currentListDir);
  74. }
  75. if (downloadDirs == null)
  76. {
  77. $("<option/>").text(system.downloadDir).val(system.downloadDir).attr("selected",true).appendTo(thisDialog.find("#download-dir"));
  78. } else {
  79. $.each(downloadDirs, function(i, item){
  80. $("<option/>").text(item).val(item).attr("selected",(item==system.downloadDir?true:false)).appendTo(thisDialog.find("#download-dir"));
  81. });
  82. }
  83. thisDialog.find("#download-dir").combobox();
  84. $.each(thisDialog.data("files"),function(i,item){
  85. $("<option/>").text((i+1)+"."+item.name).appendTo(thisDialog.find("#torrent_upload_file"));
  86. });
  87. thisDialog.find("#chkautostart").prop("checked", system.serverConfig["start-added-torrents"]);
  88. // label
  89. var labelPage = {
  90. divSelectedList: $("#divSelectedList", thisDialog),
  91. init: function() {
  92. if(system.config.nav.labels){
  93. system.resetLangText(thisDialog);
  94. this.initLabels();
  95. }
  96. else{
  97. $("#label_page").css("display", "none");
  98. }
  99. },
  100. // 初始化标签列表
  101. initLabels: function() {
  102. var box = $("#divAvailableList", thisDialog);
  103. for (var index = 0; index < system.config.labels.length; index++) {
  104. var item = system.config.labels[index];
  105. if (item) {
  106. item.index = index;
  107. var label = this.createLabel(item);
  108. label.on("click", function(){
  109. labelPage.setLabel("#divAvailableList", "#divSelectedList", $(this).data("label"));
  110. $(this).remove();
  111. }).appendTo(box);
  112. }
  113. }
  114. },
  115. createLabel: function(label) {
  116. return $("<a href='javascript:void(0);' class='user-label-big label-index-"+label.index+"'/>").data("label", label).html(label.name).css({
  117. "background-color": label.color,
  118. "color": (getGrayLevel(label.color) > 0.5 ? "#000" : "#fff")
  119. });
  120. },
  121. setLabel: function(from, to, item) {
  122. var box = $(to, thisDialog);
  123. var label = this.createLabel(item);
  124. label.on("click", function(){
  125. labelPage.setLabel(to, from, $(this).data("label"));
  126. $(this).remove();
  127. }).appendTo(box);
  128. },
  129. getLabels: function() {
  130. var items = $(".user-label-big", this.divSelectedList);
  131. var labels = [];
  132. for (var index = 0; index < items.length; index++) {
  133. var item = $(items[index]);
  134. var label = item.data("label");
  135. labels.push(label.index);
  136. }
  137. return labels;
  138. }
  139. };
  140. labelPage.init();
  141. // Confirm
  142. thisDialog.find("#torrent-button-ok").click(function()
  143. {
  144. var dir = thisDialog.find("#download-dir").combobox("getValue");
  145. var olddir = system.downloadDir;
  146. var isnewdir = system.serverConfig["download-dir"]!=dir;
  147. var autostart = thisDialog.find("#chkautostart").prop("checked");
  148. var button = $(this);
  149. if (dir=="")
  150. {
  151. return;
  152. }
  153. if (autostart!=system.serverConfig["start-added-torrents"])
  154. {
  155. system.serverConfig["start-added-torrents"] = autostart;
  156. // Start setting parameters
  157. transmission.exec(
  158. {
  159. method:"session-set"
  160. ,arguments: {
  161. "start-added-torrents": autostart
  162. }
  163. }
  164. );
  165. }
  166. button.linkbutton({disabled:true});
  167. if (thisDialog.find("#set-default-download-dir").prop("checked")&&isnewdir)
  168. {
  169. updateDownloadDir(dir);
  170. }
  171. var files = thisDialog.data("files");
  172. var uploaded = 0;
  173. var islabelChange = false;
  174. $.each(files,function(i,item){
  175. transmission.addTorrentFromFile(item,dir,!autostart,function(data, count){
  176. uploaded++;
  177. thisDialog.find("#dialog-torrent-add-queue").html(uploaded+"/"+files.length).show();
  178. // save label for torrent
  179. if(system.config.nav.labels && data.hashString != null){
  180. islabelChange = true;
  181. system.saveLabelsConfig(data.hashString, labelPage.getLabels());
  182. }
  183. if (uploaded==files.length)
  184. {
  185. thisDialog.find("#dialog-torrent-add-queue").html("").hide();
  186. button.linkbutton({disabled:false});
  187. thisDialog.dialog("close");
  188. if(islabelChange)
  189. system.saveConfig();
  190. }
  191. system.reloadData();
  192. });
  193. });
  194. //thisDialog.dialog("close");
  195. });
  196. thisDialog.find("#torrent-button-cancel").click(function()
  197. {
  198. thisDialog.dialog("close");
  199. });
  200. // Save the upload directory
  201. function updateDownloadDir(dir,callback)
  202. {
  203. transmission.exec(
  204. {
  205. method:"session-set"
  206. ,arguments:{"download-dir":dir}
  207. }
  208. ,function(data){
  209. if (data.result=="success")
  210. {
  211. system.downloadDir = dir;
  212. system.serverConfig["download-dir"] = dir;
  213. if (callback)
  214. {
  215. callback();
  216. }
  217. }
  218. }
  219. );
  220. }
  221. // Upload files
  222. // 2013/3/11 failure
  223. function uploadFile(paused,callback){
  224. var xhr = new XMLHttpRequest();
  225. var files = thisDialog.data("files");
  226. xhr.open("POST", "../upload?paused=" + paused);
  227. xhr.onload = function() {
  228. if (callback)
  229. callback();
  230. thisDialog.find("#torrent-button-ok").linkbutton({disabled:false});
  231. thisDialog.dialog("close");
  232. system.reloadData();
  233. };
  234. xhr.onerror = function() {
  235. system.debug("this.responseText:",this.responseText);
  236. };
  237. // prepare FormData
  238. var formData = new FormData();
  239. $.each(files,function(i,item){
  240. formData.append("torrent_files[]", item);
  241. });
  242. formData.append("X-Transmission-Session-Id",transmission.SessionId);
  243. xhr.send(formData);
  244. }
  245. })($("#dialog-torrent-addfile"));
  246. </script>