/* * Copyright (C) 2015 Binux * * This file is part of YAAW (https://github.com/binux/yaaw). * * YAAW is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * YAAW is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You may get a copy of the GNU Lesser General Public License * from http://www.gnu.org/licenses/lgpl.txt * */ var YAAW = (function() { var selected_tasks = false; var selected_range_start = null; var selected_range_close = null; var on_gid = null; var torrent_file = null, file_type = null; return { init: function() { $('#main-control').show(); this.tpl.init(); this.setting.init(); this.contextmenu.init(); this.event_init(); this.aria2_init(); }, aria2_init: function() { ARIA2.init(this.setting.jsonrpc_path, function() { if (YAAW.setting.add_task_option) { $("#add-task-option-wrap").empty().append(YAAW.tpl.add_task_option(YAAW.setting.add_task_option)); } else { ARIA2.init_add_task_option(); } ARIA2.refresh(); ARIA2.auto_refresh(YAAW.setting.refresh_interval); ARIA2.finish_notification = YAAW.setting.finish_notification; ARIA2.get_version(); ARIA2.global_stat(); }); }, event_init: function() { $("#add-task-submit").live("click", function() { YAAW.add_task.submit();return false; }); $("#add-task-uri").submit(function() { YAAW.add_task.submit();return false; }); $("#saveSettings").live("click", function() { YAAW.setting.submit();return false; }); $("#setting-form").submit(function() { YAAW.setting.submit();return false; }); $("#add-task-clear").live("click", function() { YAAW.add_task.clean(); }); $("#btnRemove").live("click", function() { YAAW.tasks.remove();YAAW.tasks.unSelectAll(); }); $("#btnPause").live("click", function() { YAAW.tasks.pause();YAAW.tasks.unSelectAll(); }); $("#btnUnPause").live("click", function() { YAAW.tasks.unpause();YAAW.tasks.unSelectAll(); }); $("#btnClearAlert").live("click", function() { $('#main-alert').hide(); }); $("#btnSelectActive").live("click", function() { YAAW.tasks.selectActive(); }); $("#btnSelectWaiting").live("click", function() { YAAW.tasks.selectWaiting(); }); $("#btnSelectPaused").live("click", function() { YAAW.tasks.selectPaused(); }); $("#btnSelectStopped").live("click", function() { YAAW.tasks.selectStopped(); }); $("#btnStartAll").live("click", function() { ARIA2.unpause_all(); }); $("#btnPauseAll").live("click", function() { ARIA2.pause_all(); }); $("#btnRemoveFinished").live("click", function() { ARIA2.purge_download_result(); }); $("#closeAlert").live("click", function() { $('#add-task-alert').hide(); }); $("#menuMoveTop").live("click", function() { if (selected_tasks) { YAAW.tasks.movetop(); YAAW.tasks.unSelectAll(); } else { YAAW.contextmenu.movetop(); } }); $("#menuMoveUp").live("click", function() { if (selected_tasks) { YAAW.tasks.moveup(); YAAW.tasks.unSelectAll(); } else { YAAW.contextmenu.moveup(); } }); $("#menuMoveDown").live("click", function() { if (selected_tasks) { YAAW.tasks.movedown(); YAAW.tasks.unSelectAll(); } else { YAAW.contextmenu.movedown(); } }); $("#menuMoveEnd").live("click", function() { if (selected_tasks) { YAAW.tasks.moveend(); YAAW.tasks.unSelectAll(); } else { YAAW.contextmenu.moveend(); } }); $("#menuRestart").live("click", function() { if (selected_tasks) { YAAW.tasks.restart(); YAAW.tasks.unSelectAll(); } else { YAAW.contextmenu.restart(); } }); $("#menuStart").live("click", function() { if (selected_tasks) { YAAW.tasks.unpause(); YAAW.tasks.unSelectAll(); } else { YAAW.contextmenu.unpause(); } }); $("#menuPause").live("click", function() { if (selected_tasks) { YAAW.tasks.pause(); YAAW.tasks.unSelectAll(); } else { YAAW.contextmenu.pause(); } }); $("#menuRemove").live("click", function() { if (selected_tasks) { YAAW.tasks.remove(); YAAW.tasks.unSelectAll(); } else { YAAW.contextmenu.remove(); } }); $("[rel=tooltip]").tooltip({"placement": "bottom"}); $(".task .select-box").live("click", function(e) { if (!e.shiftKey) { YAAW.tasks.toggle($(this).parents(".task")); selected_range_start = $(this).parents(".task").hasClass("selected") ? $(this).parents(".task")[0] : null; selected_range_close = null; } else { YAAW.tasks.select($(this).parents(".task")); if (!selected_range_start) selected_range_start = $(this).parents(".task")[0]; else if (!selected_range_close) selected_range_close = $(this).parents(".task")[0]; } if (selected_range_start && selected_range_close) { if (selected_range_start == selected_range_close) { selected_range_close = null; } else { var task_in_range = false; $(".tasks-table .task").each(function (i, n) { if (n == selected_range_start || n == selected_range_close) task_in_range = !task_in_range; if (task_in_range) YAAW.tasks.select(n); }); selected_range_start = selected_range_close; selected_range_close = null; } } YAAW.tasks.check_select(); }); $(".task .task-name > span").live("click", function() { var task = $(this).parents(".task"); if (task.hasClass("info-open")) { YAAW.tasks.info_close(); } else { YAAW.tasks.info_close(); YAAW.tasks.info(task); } }); $("#uri-more").click(function() { $("#add-task-uri .input-append").toggle(); $("#uri-textarea").toggle(); $("#uri-more .or-and").toggle(); $("#uri-input").val(""); $("#uri-textarea").val(""); $("#ati-out").parents(".control-group").val("").toggle(); }); $("#ib-files .ib-file-title, #ib-files .select-box").live("click", function() { if ($(this).parent().find(".select-box:first").hasClass("icon-ok")) { $(this).parent().find(".select-box").removeClass("icon-ok"); } else { $(this).parent().find(".select-box").addClass("icon-ok"); } }); $("#ib-file-save").live("click", function() { var indexes = []; $("#ib-files .select-box.icon-ok[data-index]").each(function(i, n) { indexes.push(n.getAttribute("data-index")); }); if (indexes.length == 0) { ARIA2.main_alert("alert-error", "At least one file should be selected. Or just stop the task.", 5000); } else { var options = { "select-file": indexes.join(","), }; ARIA2.change_option($(this).parents(".info-box").attr("data-gid"), options); }; }); $("#ib-options-a").live("click", function() { ARIA2.get_options($(".info-box").attr("data-gid")); }); $("#ib-peers-a").live("click", function() { ARIA2.get_peers($(".info-box").attr("data-gid")); }); var active_task_allowed_options = ["max-download-limit", "max-upload-limit"]; $("#ib-options-save").live("click", function() { var options = {}; var gid = $(this).parents(".info-box").attr("data-gid"); var status = $("#task-gid-"+gid).attr("data-status"); $.each($("#ib-options-form input"), function(n, e) { if (status == "active" && active_task_allowed_options.indexOf(e.name) == -1) return; options[e.name] = e.value; }); ARIA2.change_options($(".info-box").attr("data-gid"), options); }); $("#select-all-btn").click(function() { if (selected_tasks) { YAAW.tasks.unSelectAll(); } else { YAAW.tasks.selectAll(); } }); $("#refresh-btn").click(function() { YAAW.tasks.unSelectAll(); YAAW.tasks.info_close(); $("#main-alert").hide(); ARIA2.refresh(); return false; }); $("#setting-modal").on("show", function() { ARIA2.get_global_option(); }); if (window.FileReader) { var holder = $("#add-task-modal .modal-body").get(0); holder.ondragover = function() { $(this).addClass("hover"); return false; } holder.ondragend = function() { $(this).removeClass("hover"); return false; } holder.ondrop = function(e) { $(this).removeClass("hover"); e.preventDefault(); var file = e.dataTransfer.files[0]; YAAW.add_task.upload(file); return false; } var tup = $("#torrent-up-input").get(0); tup.onchange = function(e) { var file = e.target.files[0]; YAAW.add_task.upload(file); } } else { $("#torrent-up-input").remove(); $("#torrent-up-btn").addClass("disabled").tooltip({title: "File API is Not Supported."}); } if (window.applicationCache) { var appcache = window.applicationCache; $(document).ready(function() { if (appcache.status == appcache.IDLE) $("#offline-cached").text("cached"); }); appcache.addEventListener("cached", function(){ $("#offline-cached").text("cached"); }); } }, tpl: { init: function() { var _this = this; $("script[type='text/mustache-template']").each(function(i, n) { var key = n.getAttribute("id").replace(/-tpl$/, "").replace(/-/g, "_"); _this[key] = function() { var tpl = Mustache.compile($(n).text()); return function(view) { view._v = _this.view; return tpl(view); }; }(); }); }, files_tree: function(files) { var file_dict = {}, f; for (var i = 0; i < files.length; i++) { var at = files[i].title.split('/'); f = file_dict; for (var j = 0; j < at.length; j++) { f[at[j]] = f[at[j]] || {}; f = f[at[j]]; } f['_file'] = files[i]; } function render(f) { var content = ''; return content; } //console.log(file_dict); return render(file_dict); }, view: { bitfield: function() { var graphic = "░▒▓█"; return function(text) { var len = text.length; var result = ""; for (var i=0; i= len) p += 16; else p += parseInt(text[i*part_len+j], 16); } result += graphic[Math.floor(p/part_len/4)] + "​"; } return result; }; }, bitfield_to_percent: function() { return function(text) { var len = text.length - 1; var p, one = 0; for (var i=0; i>= 1; } } return Math.floor(one/(4*len)*100).toString(); }; }, format_size: function() { var format_text = ["B", "KB", "MB", "GB", "TB", ]; return function format_size(size) { if (size === '') return ''; size = parseInt(size); var i = 0; while (size >= 1024) { size /= 1024; i++; } if (size==0) { return "0 KB"; } else { return size.toFixed(2)+" "+format_text[i]; } }; }, format_size_0: function() { var format_text = ["B", "KB", "MB", "GB", "TB", ]; return function format_size(size) { if (size === '') return ''; size = parseInt(size); var i = 0; while (size >= 1024) { size /= 1024; i++; } if (size==0) { return "0 KB"; } else { return size.toFixed(0)+" "+format_text[i]; } }; }, format_time: function() { var time_interval = [60, 60, 24]; var time_text = ["s", "m", "h"]; return function format_time(time) { if (time == Infinity) { return "INF"; } else if (time == 0) { return "0s"; } time = Math.floor(time); var i = 0; var result = ""; while (time > 0 && i < 3) { result = time % time_interval[i] + time_text[i] + result; time = Math.floor(time/time_interval[i]); i++; } if (time > 0) { result = time + "d" + result; } return result; }; }, format_peerid: function() { return function format_peerid(peerid) { try { var ret = window.format_peerid(peerid); if (ret.client == 'unknown') throw 'unknown'; return ret.client+(ret.version ? '-'+ret.version : ''); } catch(e) { if (peerid == '%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00') return 'unknown'; var ret = unescape(peerid).split('-'); for (var i=0; i 10) { this.jsonrpc_history.shift(); } $.Storage.set("jsonrpc_history", JSON.stringify(this.jsonrpc_history)); } $.Storage.set("refresh_interval", String(this.refresh_interval)); $.Storage.set("finish_notification", String(this.finish_notification)); }, update: function() { $("#setting-form #rpc-path").val(this.jsonrpc_path); $("#setting-form input:radio[name=refresh_interval][value="+this.refresh_interval+"]").attr("checked", true); $("#setting-form input:radio[name=finish_notification][value="+this.finish_notification+"]").attr("checked", true); if (this.jsonrpc_history.length) { $(".rpc-path-wrap .dropdown-menu").remove(); var content = ''; $(".rpc-path-wrap").append(content).on("click", "li>a", function() { $("#setting-form #rpc-path").val($(this).text()); }); $(".rpc-path-wrap .dropdown-toggle").removeAttr("disabled").dropdown(); } if (this.finish_notification && window.Notification && Notification.permission !== "granted") { Notification.requestPermission(); } }, submit: function() { _this = $("#setting-form"); var _jsonrpc_path = _this.find("#rpc-path").val(); var _refresh_interval = Number(_this.find("input:radio[name=refresh_interval]:checked").val()); var _finish_notification = Number(_this.find("input:radio[name=finish_notification]:checked").val()); var changed = false; if (_jsonrpc_path !== undefined && this.jsonrpc_path != _jsonrpc_path) { this.jsonrpc_path = _jsonrpc_path; YAAW.tasks.unSelectAll(); $("#main-alert").hide(); YAAW.aria2_init(); changed = true; } if (_refresh_interval !== undefined && this.refresh_interval != _refresh_interval) { this.refresh_interval = _refresh_interval; ARIA2.auto_refresh(this.refresh_interval); changed = true; } if (_finish_notification !== undefined && this.finish_notification != _finish_notification) { this.finish_notification = _finish_notification; ARIA2.finish_notification = _finish_notification; changed = true; } if (changed) { this.save(); } if (this.finish_notification && window.Notification && Notification.permission !== "granted") { Notification.requestPermission(); } // submit aria2 global setting var options = {}; $("#aria2-gs-form input[name]").each(function(i, n) { var name = n.getAttribute("name"); var value = n.value; if (name && value) options[name] = value; }); ARIA2.change_global_option(options); $("#setting-modal").modal('hide'); }, }, notification: function(title, content) { if (!window.Notification) { return false; } if (Notification.permission !== "granted") Notification.requestPermission(); var notification = new Notification(title, { body: content, }); return notification; }, } })(); YAAW.init();