further refined connection handling in angular and added cookie support

This commit is contained in:
hamza zia 2013-03-11 19:16:48 +01:00
parent 9e63fa2ff6
commit f10cdb0d1c
3 changed files with 34 additions and 8 deletions

View File

@ -26,11 +26,11 @@ var parseFiles = function(files, cb) {
angular angular
.module('webui.ctrls.modal', [ .module('webui.ctrls.modal', [
'webui.services.deps', 'webui.services.modals' 'webui.services.deps', 'webui.services.modals', 'webui.services.rpc'
]) ])
.controller('ModalCtrl', [ .controller('ModalCtrl', [
'$_', '$scope', '$modals', '$_', '$scope', '$modals', '$rpc',
function(_, scope, modals) { function(_, scope, modals, rpc) {
scope.getUris = { scope.getUris = {
shown: false, shown: false,
@ -95,8 +95,12 @@ angular
}, },
init: function(defaults, cb) { init: function(defaults, cb) {
var conf = rpc.getConfiguration();
if (conf) this.conf = conf;
this.cb = cb; this.cb = cb;
this.open = this.shown = true; this.open = this.shown = true;
}, },
success: function() { success: function() {
console.log(this); console.log(this);

View File

@ -1,9 +1,10 @@
angular angular
.module('webui.services.rpc', [ .module('webui.services.rpc', [
'webui.services.rpc.syscall', 'webui.services.constants', 'webui.services.alerts' 'webui.services.rpc.syscall', 'webui.services.constants', 'webui.services.alerts',
'webui.services.utils'
]) ])
.factory('$rpc', ['$syscall', '$globalTimeout', '$alerts', .factory('$rpc', ['$syscall', '$globalTimeout', '$alerts', '$utils',
function(syscall, time, alerts) { function(syscall, time, alerts, utils) {
var subscriptions = [] var subscriptions = []
, configurations = [{ host: 'localhost', port: 6800, encrypt: false }] , configurations = [{ host: 'localhost', port: 6800, encrypt: false }]
@ -11,6 +12,8 @@ function(syscall, time, alerts) {
, timeout = null , timeout = null
, forceNextUpdate = false; , forceNextUpdate = false;
var cookieConf = utils.getCookie('aria2conf');
if (cookieConf) configurations.unshift(cookieConf);
// update is implemented such that // update is implemented such that
// only one syscall at max is ongoing // only one syscall at max is ongoing
@ -52,6 +55,8 @@ function(syscall, time, alerts) {
configurations = []; configurations = [];
} }
utils.setCookie('aria2conf', currentConf);
_.each(data.result, function(d, i) { _.each(data.result, function(d, i) {
var handle = subscriptions[i]; var handle = subscriptions[i];
if (handle) { if (handle) {
@ -80,7 +85,7 @@ function(syscall, time, alerts) {
timeout = setTimeout(update, 0); timeout = setTimeout(update, 0);
} }
else { else {
alerts.addAlert('<strong>Oh Snap!</strong> Could not connect to the aria2 server, retrying after ' + time / 1000 + ' secs', 'error'); alerts.addAlert('<strong>Oh Snap!</strong> Could not connect to the aria2 server, retrying after ' + time / 1000 + ' secs. You might want to recheck the connection settings for your aria2 server by going to settings > connection settings', 'error');
timeout = setTimeout(update, time); timeout = setTimeout(update, time);
} }
} }
@ -101,7 +106,6 @@ function(syscall, time, alerts) {
configurations = conf; configurations = conf;
else else
configurations = [conf]; configurations = [conf];
}, },
// get current configuration being used // get current configuration being used

View File

@ -1,6 +1,24 @@
angular.module('webui.services.utils', []) angular.module('webui.services.utils', [])
.factory('$utils', function() { .factory('$utils', function() {
return { return {
// saves the key value pair in cookies
setCookie: function(key, value) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + 30 * 12);
var cvalue = escape(JSON.stringify(value)) + "; expires=" + exdate.toUTCString();
document.cookie = key + "=" + cvalue;
},
// gets a value for a key stored in cookies
getCookie: function(key) {
var chunks = document.cookie.split(";");
for (var i = 0; i < chunks.length; i++) {
var ckey = chunks[i].substr(0, chunks[i].indexOf("=")).replace(/^\s+|\s+$/g,"");
var cvalue = chunks[i].substr(chunks[i].indexOf("=") + 1);
if (key == ckey) {
return JSON.parse(unescape(cvalue));
}
}
},
getFileName: function(path) { getFileName: function(path) {
var seed = path.split(/[/\\]/); var seed = path.split(/[/\\]/);
return seed[seed.length - 1]; return seed[seed.length - 1];