diff --git a/angular.html b/angular.html index 84220ce..2f85599 100644 --- a/angular.html +++ b/angular.html @@ -39,6 +39,7 @@ + @@ -94,10 +95,14 @@
  • - By Metalinks + + By Torrents +
  • - By Torrents + + By Metalinks +
  • @@ -296,7 +301,7 @@ - + + + + + + + diff --git a/js/ctrls/modal.js b/js/ctrls/modal.js index 7a07a82..770c4cb 100644 --- a/js/ctrls/modal.js +++ b/js/ctrls/modal.js @@ -1,3 +1,29 @@ +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); + }); +}; + angular .module('webui.ctrls.modal', [ 'webui.services.rpc', 'webui.services.deps', 'webui.services.modals' @@ -8,8 +34,9 @@ angular scope.getUris = { cb: null, - uris: '', shown: false, + + uris: '', parse: function() { return _ .chain(this.uris.trim().split(/\n\r?/g)) @@ -27,16 +54,40 @@ angular this.shown = false; } }; - modals.register('getUris', function(cb) { - if (scope.getUris.cb != null && scope.getUris.shown) { - // modal already shown, user is busy - // TODO: get a better method of passing this on - cb([]); - } - else { - scope.getUris.cb = cb; - scope.getUris.shown = true; + + _.each(['getTorrents', 'getMetalinks'], function(name) { + scope[name] = { + cb: null, + shown: false, + + files: [], + finish: function() { + var self = this; + console.log('parsing files'); + parseFiles(self.files, function(txts) { + console.log('calling cb', this.cb); + if (self.cb) self.cb(txts); + + self.cb = null; + self.shown = false; + }); + } }; }); + _.each(['getUris', 'getTorrents', 'getMetalinks'], function(name) { + modals.register(name, function(cb) { + if (scope[name].cb != null && scope[name].shown) { + // modal already shown, user is busy + // TODO: get a better method of passing this on + cb([]); + } + else { + console.log('setting cb for ', name, cb); + scope[name].cb = cb; + scope[name].shown = true; + }; + }); + }); + }]); diff --git a/js/ctrls/nav.js b/js/ctrls/nav.js index d0cc8f4..79e29d9 100644 --- a/js/ctrls/nav.js +++ b/js/ctrls/nav.js @@ -13,7 +13,27 @@ angular scope.collapsed = true; scope.addUris = function() { - modals.invoke('getUris', rhelpers.addUris); + modals.invoke( + 'getUris', _.bind(rhelpers.addUris, rhelpers) + ); + }; + + scope.addTorrents = function() { + modals.invoke( + 'getTorrents', _.bind(rhelpers.addTorrents, rhelpers) + ); + }; + + scope.addMetalinks = function() { + modals.invoke( + 'getMetalinks', _.bind(rhelpers.addMetalinks, rhelpers) + ); + }; + + scope.addTorrent = function() { + modals.invoke( + 'getTorrent', _.bind(rhelpers.addTorrents, rhelpers) + ); }; }]); diff --git a/js/directives/fselect.js b/js/directives/fselect.js new file mode 100644 index 0000000..08ca475 --- /dev/null +++ b/js/directives/fselect.js @@ -0,0 +1,12 @@ +// watches changes in the file upload control (input[file]) and +// puts the files selected in an attribute +angular +.module('webui.directives.fselect', ['webui.services.deps']) +.directive('fselect', ['$parse', function(parse) { + return function(scope, elem, attrs) { + var setfiles = parse(attrs.fselect || attrs.files).assign; + elem.bind('change', function() { + setfiles(scope, elem[0].files); + }); + }; +}]); diff --git a/js/init.js b/js/init.js index 614ed79..a83552a 100644 --- a/js/init.js +++ b/js/init.js @@ -2,7 +2,7 @@ angular.module('webui', [ 'webui.services.utils', 'webui.services.deps', 'webui.services.base64', 'webui.services.constants', 'webui.services.rpc', 'webui.services.modals', 'webui.filters.bytes', 'webui.filters.path', - 'webui.directives.chunkbar', 'webui.directives.dgraph', + 'webui.directives.chunkbar', 'webui.directives.dgraph', 'webui.directives.fselect', 'webui.ctrls.download', 'webui.ctrls.nav', 'webui.ctrls.modal' ]); diff --git a/js/services/rpc/helpers.js b/js/services/rpc/helpers.js index 34322cc..b83d997 100644 --- a/js/services/rpc/helpers.js +++ b/js/services/rpc/helpers.js @@ -18,6 +18,42 @@ angular.module('webui.services.rpc.helpers', [ rpc.once('addUri', [uri], cb, true); }); + // now dispatch all addUri syscalls + rpc.forceUpdate(); + }, + addTorrents: function(txts) { + var cnt = 0; + var cb = function(ret) { + cnt--; + if (!cnt) { + // close modal + console.log('closing modal'); + } + }; + _.each(txts, function(txt) { + cnt++; + // passing true to batch all the addUri calls + rpc.once('addTorrent', [txt], cb, true); + }); + + // now dispatch all addUri syscalls + rpc.forceUpdate(); + }, + addMetalinks: function(txts) { + var cnt = 0; + var cb = function(ret) { + cnt--; + if (!cnt) { + // close modal + console.log('closing modal'); + } + }; + _.each(txts, function(txt) { + cnt++; + // passing true to batch all the addUri calls + rpc.once('addMetalink', [txt], cb, true); + }); + // now dispatch all addUri syscalls rpc.forceUpdate(); }