diff --git a/js/controllers/settings.js b/js/controllers/settings.js index 135b5ae34..c9fdede6f 100644 --- a/js/controllers/settings.js +++ b/js/controllers/settings.js @@ -1,6 +1,6 @@ 'use strict'; +angular.module('copayApp.controllers').controller('SettingsController', function($scope, $rootScope, $window, $location, controllerUtils, notification) { -angular.module('copayApp.controllers').controller('SettingsController', function($scope, $rootScope, $window, $location, controllerUtils) { controllerUtils.redirIfLogged(); $scope.title = 'Settings'; @@ -8,6 +8,7 @@ angular.module('copayApp.controllers').controller('SettingsController', function $scope.insightLivenet = config.network.livenet.url; $scope.insightTestnet = config.network.testnet.url; + $scope.availableLanguages = [{ name: 'English', isoCode: 'en', @@ -23,7 +24,12 @@ angular.module('copayApp.controllers').controller('SettingsController', function } } + $scope.save = function() { + $scope.insightLivenet = copay.Insight.setCompleteUrl($scope.insightLivenet); + $scope.insightTestnet = copay.Insight.setCompleteUrl($scope.insightTestnet); + + var insightSettings = { livenet: { url: $scope.insightLivenet, @@ -33,6 +39,7 @@ angular.module('copayApp.controllers').controller('SettingsController', function }, } + localStorage.setItem('config', JSON.stringify({ network: insightSettings, version: copay.version, diff --git a/js/directives.js b/js/directives.js index ea0ac33bb..d9a9a860a 100644 --- a/js/directives.js +++ b/js/directives.js @@ -48,7 +48,30 @@ angular.module('copayApp.directives') }; } ]) - .directive('validAmount', ['$rootScope', '$locale', + .directive('validUrl', [ + + function() { + return { + require: 'ngModel', + link: function(scope, elem, attrs, ctrl) { + var validator = function(value) { + // Regular url + if (/^https?:\/\//.test(value)) { + ctrl.$setValidity('validUrl', true); + return value; + } else { + ctrl.$setValidity('validUrl', false); + return value; + } + }; + + ctrl.$parsers.unshift(validator); + ctrl.$formatters.unshift(validator); + } + }; + } + ]) + .directive('validAmount', ['$rootScope', '$locale', function($rootScope, locale) { var w = $rootScope.wallet; preconditions.checkState(w); @@ -68,7 +91,7 @@ angular.module('copayApp.directives') if (typeof vNum == "number" && vNum > 0) { var decimals = Number(w.settings.unitDecimals); var sep_index = ('' + value).indexOf(formats.DECIMAL_SEP); - var str_value = ('' + value).substring(sep_index+1); + var str_value = ('' + value).substring(sep_index + 1); if (sep_index > 0 && str_value.length > decimals) { ctrl.$setValidity('validAmount', false); scope.notValidAmount = true; diff --git a/js/models/Insight.js b/js/models/Insight.js index 30e2d0e7d..5b6e0e32c 100644 --- a/js/models/Insight.js +++ b/js/models/Insight.js @@ -45,6 +45,58 @@ var Insight = function(opts) { this.socket = this.getSocket(); } +Insight.setCompleteUrl = function(uri) { + + if (!uri) return uri; + + var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/; + + var parts = [ + 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor' + ]; + + function parseuri(str) { + var m = re.exec(str || ''), + uri = {}, + i = 14; + + while (i--) { + uri[parts[i]] = m[i] || ''; + } + + return uri; + }; + + var opts_host; + var opts_secure; + var opts_port; + var opts_protocol; + if (uri) { + uri = parseuri(uri); + opts_host = uri.host; + opts_protocol = uri.protocol; + opts_secure = uri.protocol == 'https' || uri.protocol == 'wss'; + opts_port = uri.port; + } + + var this_secure = null != opts_secure ? opts_secure : + ('https:' == location.protocol); + + var opts_hostname; + if (opts_host) { + var pieces = opts_host.split(':'); + opts_hostname = pieces.shift(); + if (pieces.length) opts_port = pieces.pop(); + } + + var this_port = opts_port || + (this_secure ? 443 : 80); + + var newUri = opts_protocol + '://' + opts_host + ':' + this_port; + + return newUri; +} + util.inherits(Insight, EventEmitter); Insight.prototype.STATUS = { diff --git a/views/settings.html b/views/settings.html index 6c5374b55..3b076405a 100644 --- a/views/settings.html +++ b/views/settings.html @@ -15,10 +15,13 @@
Insight API server - - - - + + not valid + + + + not valid +

Insight API server is open-source software. You can run your own instances, check Insight API Homepage @@ -26,7 +29,7 @@

« Back -