2013-01-30 07:13:38 +01:00
|
|
|
var parseFiles = function(files, cb) {
|
|
|
|
var cnt = 0;
|
|
|
|
var txts = [];
|
|
|
|
var onload = function(res) {
|
|
|
|
var txt = res.target.result;
|
|
|
|
txts.push(txt.split(',')[1]);
|
|
|
|
cnt--;
|
|
|
|
if (!cnt) {
|
|
|
|
cb(txts);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
_.each(files, function(file) {
|
|
|
|
cnt++;
|
|
|
|
console.log('starting file reader');
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = onload;
|
|
|
|
reader.onerror = function(err) {
|
|
|
|
// return error
|
|
|
|
// TODO: find a better way to propogate error upstream
|
|
|
|
console.log('got back error', err);
|
|
|
|
cb([]);
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-01-29 13:47:40 +01:00
|
|
|
angular
|
|
|
|
.module('webui.ctrls.modal', [
|
2014-06-07 15:34:32 +02:00
|
|
|
"ui.bootstrap", 'webui.services.deps', 'webui.services.modals', 'webui.services.rpc',
|
|
|
|
'webui.services.configuration'
|
2013-01-29 13:47:40 +01:00
|
|
|
])
|
2013-01-29 18:52:30 +01:00
|
|
|
.controller('ModalCtrl', [
|
2014-06-07 15:34:32 +02:00
|
|
|
'$_', '$scope', '$modal', "$modals", '$rpc','$fileSettings', '$downloadProps',
|
|
|
|
function(_, scope, $modal, modals, rpc, fsettings, dprops) {
|
2013-01-29 18:52:30 +01:00
|
|
|
|
|
|
|
scope.getUris = {
|
2014-02-21 02:13:31 +01:00
|
|
|
open: function(cb) {
|
|
|
|
var self = this;
|
|
|
|
this.uris = "";
|
2017-08-26 15:17:52 +02:00
|
|
|
this.downloadSettingsCollapsed = true;
|
2017-01-08 20:21:55 +01:00
|
|
|
this.advancedSettingsCollapsed = true;
|
2014-05-31 19:32:46 +02:00
|
|
|
this.settings = {};
|
2014-06-07 15:34:32 +02:00
|
|
|
this.fsettings = _.cloneDeep(fsettings);
|
2013-02-26 15:02:26 +01:00
|
|
|
this.cb = cb;
|
2014-06-07 15:34:32 +02:00
|
|
|
|
|
|
|
// fill in default download properties
|
|
|
|
_.forEach(dprops, function(p) {
|
|
|
|
self.settings[p] = self.fsettings[p];
|
2014-06-10 01:59:23 +02:00
|
|
|
delete self.fsettings[p];
|
2014-06-07 15:34:32 +02:00
|
|
|
});
|
|
|
|
|
2014-02-21 02:13:31 +01:00
|
|
|
this.inst = $modal.open({
|
|
|
|
templateUrl: "getUris.html",
|
2014-06-10 01:59:23 +02:00
|
|
|
scope: scope,
|
|
|
|
windowClass: "modal-large"
|
2014-02-21 02:13:31 +01:00
|
|
|
});
|
|
|
|
this.inst.result.then(function() {
|
|
|
|
delete self.inst;
|
|
|
|
if (self.cb) {
|
2014-05-31 19:32:46 +02:00
|
|
|
var settings = {};
|
2014-07-02 19:04:03 +02:00
|
|
|
// no need to send in default values, just the changed ones
|
2014-05-31 19:32:46 +02:00
|
|
|
for (var i in self.settings) {
|
2014-07-02 19:04:03 +02:00
|
|
|
if (fsettings[i].val != self.settings[i].val)
|
|
|
|
settings[i] = self.settings[i].val;
|
2014-05-31 19:32:46 +02:00
|
|
|
}
|
2014-06-10 01:59:23 +02:00
|
|
|
for (var i in self.fsettings) {
|
2014-07-02 19:04:03 +02:00
|
|
|
if (fsettings[i].val != self.fsettings[i].val)
|
|
|
|
settings[i] = self.fsettings[i].val;
|
2014-06-10 01:59:23 +02:00
|
|
|
}
|
2014-05-31 19:32:46 +02:00
|
|
|
|
|
|
|
console.log('sending settings:', settings);
|
|
|
|
self.cb(self.parse(), settings);
|
2014-02-21 02:13:31 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
delete self.inst;
|
|
|
|
});
|
2013-02-26 15:02:26 +01:00
|
|
|
},
|
2013-01-29 18:52:30 +01:00
|
|
|
parse: function() {
|
|
|
|
return _
|
2014-06-02 10:06:39 +02:00
|
|
|
.chain(this.uris.trim().split(/\r?\n/g))
|
2017-09-20 08:43:43 +02:00
|
|
|
.map(function(d) {
|
2017-07-27 04:26:13 +02:00
|
|
|
return _(d)
|
2017-09-20 08:43:43 +02:00
|
|
|
.replace(/["'][^"]*['"]/g, function(c) {
|
|
|
|
return c.replace(/%/g,'%25').replace(/ /g,'%20');
|
2017-07-27 04:26:13 +02:00
|
|
|
})
|
|
|
|
.trim()
|
|
|
|
.split(/\s+/g)
|
|
|
|
.map(function(c) {
|
2017-09-20 08:43:43 +02:00
|
|
|
return c.replace(/%20/g,' ').replace(/%25/g,'%').replace(/["']/g,'');
|
2017-07-27 04:26:13 +02:00
|
|
|
});
|
|
|
|
})
|
2013-01-29 18:52:30 +01:00
|
|
|
.filter(function(d) { return d.length })
|
|
|
|
.value();
|
|
|
|
}
|
2013-01-28 15:18:21 +01:00
|
|
|
};
|
2013-01-30 07:13:38 +01:00
|
|
|
|
2013-02-26 15:02:26 +01:00
|
|
|
scope.settings = {
|
2014-05-31 19:32:46 +02:00
|
|
|
open: function(settings, title, actionText, cb) {
|
2014-02-21 02:13:31 +01:00
|
|
|
var self = this;
|
2013-02-26 15:02:26 +01:00
|
|
|
this.settings = settings;
|
2014-02-21 02:13:31 +01:00
|
|
|
this.title = title;
|
2014-05-31 19:32:46 +02:00
|
|
|
this.actionText = actionText;
|
2014-02-21 02:13:31 +01:00
|
|
|
this.inst = $modal.open({
|
|
|
|
templateUrl: "settings.html",
|
2014-06-10 01:59:23 +02:00
|
|
|
scope: scope,
|
|
|
|
windowClass: "modal-large"
|
2014-02-21 02:13:31 +01:00
|
|
|
});
|
|
|
|
this.inst.result.then(function() {
|
|
|
|
delete self.inst;
|
|
|
|
if (cb) {
|
|
|
|
cb(self.settings);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
delete self.inst;
|
|
|
|
});
|
2013-02-26 15:02:26 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-01 21:15:33 +02:00
|
|
|
scope.selectFiles = {
|
|
|
|
open: function(files, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.files = _.cloneDeep(files);
|
2017-07-27 12:31:18 +02:00
|
|
|
var groupFiles = function (files) {
|
2017-07-31 14:58:49 +02:00
|
|
|
// sort files alphabetically
|
|
|
|
files.sort(function (a, b) {
|
|
|
|
if (a.relpath < b.relpath) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
});
|
2017-07-27 22:31:33 +02:00
|
|
|
function OrganizedFolder () {
|
|
|
|
this.dirs = {};
|
|
|
|
this.files = [];
|
|
|
|
this.show = false;
|
2017-07-29 14:42:06 +02:00
|
|
|
this.selected = true;
|
2017-07-27 22:31:33 +02:00
|
|
|
}
|
|
|
|
var folder = new OrganizedFolder(), tmp;
|
2017-07-27 12:31:18 +02:00
|
|
|
for (var i = 0; i < files.length; i++) {
|
2017-07-25 23:03:52 +02:00
|
|
|
tmp = folder;
|
2017-07-27 12:31:18 +02:00
|
|
|
var str = files[i].relpath;
|
|
|
|
var arr = str.split("/");
|
|
|
|
for (var j = 0; j < arr.length - 1; j++) {
|
|
|
|
if (!tmp.dirs[arr[j]]) {
|
2017-07-27 22:31:33 +02:00
|
|
|
tmp.dirs[arr[j]] = new OrganizedFolder();
|
2017-07-27 12:31:18 +02:00
|
|
|
}
|
|
|
|
tmp = tmp.dirs[arr[j]];
|
|
|
|
}
|
2017-07-27 22:31:33 +02:00
|
|
|
tmp.files.push(files[i]);
|
2017-07-25 23:03:52 +02:00
|
|
|
}
|
|
|
|
return folder;
|
|
|
|
};
|
2017-07-27 12:31:18 +02:00
|
|
|
this.groupedFiles = groupFiles(this.files);
|
2014-09-01 21:15:33 +02:00
|
|
|
this.inst = $modal.open({
|
|
|
|
templateUrl: "selectFiles.html",
|
|
|
|
scope: scope,
|
|
|
|
windowClass: "modal-large"
|
|
|
|
});
|
|
|
|
|
|
|
|
this.inst.result.then(function() {
|
|
|
|
delete self.inst;
|
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb(self.files);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
delete self.inst;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-26 15:02:26 +01:00
|
|
|
scope.connection = {
|
2014-02-21 02:13:31 +01:00
|
|
|
open: function(defaults, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// XXX We need to actually clone this!
|
|
|
|
this.conf = rpc.getConfiguration();
|
|
|
|
this.inst = $modal.open({
|
|
|
|
templateUrl: "connection.html",
|
2014-06-10 01:59:23 +02:00
|
|
|
scope: scope,
|
|
|
|
windowClass: "modal-large",
|
2014-02-21 02:13:31 +01:00
|
|
|
});
|
2014-09-01 21:15:33 +02:00
|
|
|
|
2014-02-21 02:13:31 +01:00
|
|
|
this.inst.result.then(function() {
|
|
|
|
delete self.inst;
|
|
|
|
if (cb) {
|
|
|
|
cb(self.conf);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
delete self.inst;
|
|
|
|
});
|
2013-02-26 15:02:26 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-30 07:13:38 +01:00
|
|
|
_.each(['getTorrents', 'getMetalinks'], function(name) {
|
2014-02-21 02:13:31 +01:00
|
|
|
scope[name] = {
|
|
|
|
open: function(cb) {
|
2013-01-30 07:13:38 +01:00
|
|
|
var self = this;
|
2014-02-21 02:13:31 +01:00
|
|
|
this.files = [];
|
2014-06-10 01:59:23 +02:00
|
|
|
this.collapsed = true;
|
2014-05-31 19:32:46 +02:00
|
|
|
this.settings = {};
|
2014-06-10 01:59:23 +02:00
|
|
|
this.fsettings = _.cloneDeep(fsettings);
|
|
|
|
|
|
|
|
// fill in default download properties
|
|
|
|
_.forEach(dprops, function(p) {
|
|
|
|
self.settings[p] = self.fsettings[p];
|
|
|
|
delete self.fsettings[p];
|
|
|
|
});
|
|
|
|
|
2014-02-21 02:13:31 +01:00
|
|
|
this.inst = $modal.open({
|
|
|
|
templateUrl: name + ".html",
|
2014-06-10 01:59:23 +02:00
|
|
|
scope: scope,
|
|
|
|
windowClass: "modal-large",
|
2014-02-21 02:13:31 +01:00
|
|
|
});
|
2014-09-01 21:15:33 +02:00
|
|
|
|
2014-02-21 02:13:31 +01:00
|
|
|
this.inst.result.then(function() {
|
|
|
|
delete self.inst;
|
|
|
|
if (cb) {
|
|
|
|
parseFiles(self.files, function(txts) {
|
2014-05-31 19:32:46 +02:00
|
|
|
var settings = {};
|
2014-07-02 19:04:03 +02:00
|
|
|
|
|
|
|
// no need to send in default values, just the changed ones
|
2014-05-31 19:32:46 +02:00
|
|
|
for (var i in self.settings) {
|
2014-07-02 19:04:03 +02:00
|
|
|
if (fsettings[i].val != self.settings[i].val)
|
|
|
|
settings[i] = self.settings[i].val;
|
2014-05-31 19:32:46 +02:00
|
|
|
}
|
2014-06-10 01:59:23 +02:00
|
|
|
for (var i in self.fsettings) {
|
2014-07-02 19:04:03 +02:00
|
|
|
if (fsettings[i].val != self.fsettings[i].val)
|
|
|
|
settings[i] = self.fsettings[i].val;
|
2014-06-10 01:59:23 +02:00
|
|
|
}
|
2014-05-31 19:32:46 +02:00
|
|
|
|
|
|
|
console.log('sending settings:', settings);
|
|
|
|
cb(txts, settings);
|
2014-02-21 02:13:31 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
delete self.inst;
|
2013-01-30 07:13:38 +01:00
|
|
|
});
|
|
|
|
}
|
2013-01-29 18:52:30 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2014-02-21 02:13:31 +01:00
|
|
|
_.each(["about", "server_info"], function(name) {
|
|
|
|
scope[name] = {
|
|
|
|
open: function() {
|
|
|
|
var self = this;
|
|
|
|
this.inst = $modal.open({
|
|
|
|
templateUrl: name + ".html",
|
|
|
|
scope: scope
|
|
|
|
});
|
|
|
|
this.inst.result.then(function() {
|
|
|
|
delete self.inst;
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
delete self.inst;
|
|
|
|
});
|
2014-02-17 01:13:25 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
rpc.once('getVersion', [], function(data) {
|
|
|
|
scope.miscellaneous = data[0];
|
2014-09-01 21:15:33 +02:00
|
|
|
});
|
2014-02-21 02:13:31 +01:00
|
|
|
|
2013-02-20 01:13:06 +01:00
|
|
|
_.each([
|
2014-09-01 21:15:33 +02:00
|
|
|
'getUris', 'getTorrents', 'getMetalinks', 'selectFiles',
|
2014-02-17 01:13:25 +01:00
|
|
|
'settings', 'connection', 'server_info', 'about'
|
2013-02-20 01:13:06 +01:00
|
|
|
], function(name) {
|
2014-02-21 02:13:31 +01:00
|
|
|
modals.register(name, function() {
|
|
|
|
if (scope[name].inst) {
|
|
|
|
// Already open.
|
2013-02-26 15:02:26 +01:00
|
|
|
return;
|
2013-01-30 07:13:38 +01:00
|
|
|
}
|
2014-02-21 02:13:31 +01:00
|
|
|
var args = Array.prototype.slice.call(arguments, 0);
|
|
|
|
scope[name].open.apply(scope[name], args);
|
2013-01-30 07:13:38 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-01-15 09:24:09 +01:00
|
|
|
}]);
|