{"size": 3106, "ext": "js", "max_stars_repo_path": "src/app/pages/torrents/torrentsList.js", "max_stars_repo_name": "liviuflore/rpi-admin", "max_stars_repo_stars_event_min_datetime": "2016-08-28T21:53:37.000Z", "max_stars_repo_stars_event_max_datetime": "2016-08-28T21:53:37.000Z", "max_issues_repo_path": "src/app/pages/torrents/torrentsList.js", "max_issues_repo_name": "liviuflore/rpi-admin", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/app/pages/torrents/torrentsList.js", "max_forks_repo_name": "liviuflore/rpi-admin", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.0425531915, "max_line_length": 115, "alphanum_fraction": 0.4191886671}
/**
 * @author a.demeshko
 * created on 12/29/15
 */
(function () {
    'use strict';

    angular.module('BlurAdmin.pages.torrents')
        .service('torrentsList', torrentsList);
    /** @ngInject */
    function torrentsList($q, mwAPI, log) {

        var tabs = [{
            label: 'all',
            name: 'All',
            statusIds: []
        }, {
            label: 'active',
            name: 'Active',
            statusIds: [1, 2, 3, 4, 5, 6]
        }, {
            label: 'finished',
            name: 'Finished',
            statusIds: [5, 6]
        }, {
            label: 'downloading',
            name: 'Downloading',
            statusIds: [3, 4]
        }, {
            label: 'paused',
            name: 'Paused',
            statusIds: [0]
        }, {
            label: 'error',
            name: 'Error',
            statusIds: []
        }];
        function runRequest(req) {
            var deferredQ = $q.defer();
            mwAPI.getReq(req).then(function (response) {
                deferredQ.resolve(response.torrents);
            });
            return deferredQ.promise;
        };

        return {
            getTorrents: function () {
                log.d("get torrents");
                return runRequest('/api/torrents/list');
            },
            deleteTorrents: function (list, deleteData) {
                deleteData = typeof deleteData !== 'undefined' ? deleteData : false;
                log.d("delete torrents " + list);
                return runRequest('/api/torrents/delete/' + (list.length == 0 ? 'none' : list) + '/' + deleteData);
            },
            stopTorrents: function (list) {
                log.d("stop torrents " + list);
                return runRequest('/api/torrents/stop/' + (list.length == 0 ? 'none' : list));
            },
            startTorrents: function (list) {
                log.d("start torrents " + list);
                return runRequest('/api/torrents/start/' + (list.length == 0 ? 'none' : list));
            },
            getTabs: function () {
                return tabs;
            },
            filterByLabel: function (torrents, label) {
                torrents.sort(function (a, b) {
                    if (a.date > b.date) return 1;
                    if (a.date < b.date) return -1;
                }).reverse();
                return torrents.filter(function (t) {
                    if (label == 'all')
                        return true;
                    else {
                        var tab = $.grep(tabs, function (e) { return e.label == label; })[0];
                        return (tab.statusIds.indexOf(t.status) > -1);
                    }
                });
            },
            filterById: function (torrents, id) {
                torrents.sort(function (a, b) {
                    if (a.date > b.date) return 1;
                    if (a.date < b.date) return -1;
                }).reverse();
                return torrents.filter(function (m) {
                    return m.id == id;
                })[0];
            }
        }

    }

})();