Set default ports automatically on Insight URLs

This commit is contained in:
Matias Pando 2014-10-15 18:16:37 -03:00
parent bdbaee561e
commit 28d888b75f
4 changed files with 93 additions and 8 deletions

View File

@ -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,

View File

@ -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;

View File

@ -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 = {

View File

@ -15,10 +15,13 @@
</fieldset>
<fieldset>
<legend translate>Insight API server</legend>
<label for="insight-livenet">Livenet</label>
<input type="text" ng-model="insightLivenet" class="form-control" name="insight-livenet">
<label for="insight-testnet">Testnet</label>
<input type="text" ng-model="insightTestnet" class="form-control" name="insight-testnet">
<label for="insightLivenet">Livenet</label>
<small translate class="has-error" ng-show="settingsForm.insightLivenet.$invalid">not valid</small>
<input type="text" ng-model="insightLivenet" class="form-control" name="insightLivenet" valid-url required>
<label for="insightTestnet">Testnet</label>
<small translate class="has-error" ng-show="settingsForm.insightTestnet.$invalid">not valid</small>
<input type="text" ng-model="insightTestnet" class="form-control" name="insightTestnet" valid-url required>
<p translate class="small">
Insight API server is open-source software. You can run your own instances, check <a href="http://insight.is" target="_blank">Insight API Homepage</a>
@ -26,7 +29,7 @@
</fieldset>
<div class="text-right m20t">
<a class="back-button text-white m20r" href="#!/">&laquo; <span translate>Back</span></a>
<button translate type="submit" class="button primary m0 ng-binding" ng-disabled="setupForm.$invalid || loading" disabled="disabled" ng-click="save()">
<button translate type="submit" class="button primary m0 ng-binding" ng-disabled="settingsForm.$invalid || loading" ng-click="save()">
Save
</button>
</div>