Skip to content

Commit

Permalink
Using a custom validator for url in the NewSourceWindow (needs i18n, c…
Browse files Browse the repository at this point in the history
…loses #48).
  • Loading branch information
tschaub committed Apr 6, 2011
1 parent b78927d commit 94ec32d
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/script/locale/ca.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ GeoExt.Lang.add("ca", {
title: "Afegeix Servidor...",
cancelText: "Cancel·la",
addServerText: "Afegeix Servidor",
invalidURLText: "Enter a valid URL to a WMS endpoint (e.g. http://example.com/geoserver/wms)",
contactingServerText: "Connectant amb el Servidor..."
},

Expand Down
1 change: 1 addition & 0 deletions src/script/locale/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ GeoExt.Lang.add("en", {
title: "Add New Server...",
cancelText: "Cancel",
addServerText: "Add Server",
invalidURLText: "Enter a valid URL to a WMS endpoint (e.g. http://example.com/geoserver/wms)",
contactingServerText: "Contacting Server..."
},

Expand Down
1 change: 1 addition & 0 deletions src/script/locale/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ GeoExt.Lang.add("es", {
title: "Añadir Servidor...",
cancelText: "Cancelar",
addServerText: "Añadir Servidor",
invalidURLText: "Enter a valid URL to a WMS endpoint (e.g. http://example.com/geoserver/wms)",
contactingServerText: "Conectando con el Servidor..."
},

Expand Down
8 changes: 8 additions & 0 deletions src/script/locale/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ GeoExt.Lang.add("fr", {
dataStoreEmptyText: "Magasin de données par défaut"
},

"gxp.NewSourceWindow.prototype": {
title: "Add New Server...",
cancelText: "Cancel",
addServerText: "Add Server",
invalidURLText: "Enter a valid URL to a WMS endpoint (e.g. http://example.com/geoserver/wms)",
contactingServerText: "Contacting Server..."
},

"gxp.ScaleOverlay.prototype": {
zoomLevelText: "Niveau de zoom"
}
Expand Down
8 changes: 8 additions & 0 deletions src/script/locale/nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ GeoExt.Lang.add("nl", {
dataStoreEmptyText: "Standaard archief"
},

"gxp.NewSourceWindow.prototype": {
title: "Add New Server...",
cancelText: "Cancel",
addServerText: "Add Server",
invalidURLText: "Enter a valid URL to a WMS endpoint (e.g. http://example.com/geoserver/wms)",
contactingServerText: "Contacting Server..."
},

"gxp.ScaleOverlay.prototype": {
zoomLevelText: "Zoom niveau"
}
Expand Down
48 changes: 42 additions & 6 deletions src/script/widgets/NewSourceWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ gxp.NewSourceWindow = Ext.extend(Ext.Window, {
*/
addServerText: "Add Server",

/** api: config[invalidURLText]
* ``String``
* Message to display when an invalid URL is entered (i18n).
*/
invalidURLText: "Enter a valid URL to a WMS endpoint (e.g. http://example.com/geoserver/wms)",

/** api: config[contactingServerText]
* ``String``
* Text for server contact (i18n).
Expand Down Expand Up @@ -70,7 +76,6 @@ gxp.NewSourceWindow = Ext.extend(Ext.Window, {
* Fired with the URL that the user provided as a parameter when the form
* is submitted.
*/

initComponent: function() {

this.addEvents("server-added");
Expand All @@ -80,11 +85,7 @@ gxp.NewSourceWindow = Ext.extend(Ext.Window, {
allowBlank: false,
width: 240,
msgTarget: "under",
vtype: "url",
validator: OpenLayers.Function.bind(function() {
// use previous error
return (this.error == null) ? true : this.error;
}, this)
validator: this.urlValidator.createDelegate(this)
});

this.form = new Ext.form.FormPanel({
Expand Down Expand Up @@ -152,6 +153,41 @@ gxp.NewSourceWindow = Ext.extend(Ext.Window, {
}, this);

},

/** private: property[urlRegExp]
* `RegExp`
*
* We want to allow protocol or scheme relative URL
* (e.g. //example.com/). We also want to allow username and
* password in the URL (e.g. http://user:pass@example.com/).
* We also want to support virtual host names without a top
* level domain (e.g. http://localhost:9080/). It also makes sense
* to limit scheme to http and https.
* The Ext "url" vtype does not support any of this.
* This doesn't have to be completely strict. It is meant to help
* the user avoid typos.
*/
urlRegExp: /^(http(s)?:)?\/\/([\w%]+:[\w%]+@)?([^@\/:]+)(:\d+)?\//i,

/** private: method[urlValidator]
* :arg url: `String`
* :returns: `Boolean` The url looks valid.
*
* This method checks to see that a user entered URL looks valid. It also
* does form validation based on the `error` property set when a response
* is parsed.
*/
urlValidator: function(url) {
var valid;
if (!this.urlRegExp.test(url)) {
valid = this.invalidURLText;
} else {
valid = !this.error || this.error;
}
// clear previous error message
this.error = null;
return valid;
},

/** private: method[setLoading]
* Visually signify to the user that we're trying to load the service they
Expand Down
81 changes: 81 additions & 0 deletions tests/script/widgets/NewSourceWindow.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,87 @@

instance.destroy();
}

function test_urlRegExp(t) {

var cases = [{
url: "http://example.com/", valid: true
}, {
url: "http://example.com/geoserver/ows", valid: true
}, {
url: "http://example.com/geoserver/ows?foo=bar", valid: true
}, {
url: "http://user:pass@example.com/", valid: true
}, {
url: "http://hostalias/geoserver/ows", valid: true
}, {
url: "http://localhost:9080/geoserver/ows", valid: true
}, {
url: "//example.com/geoserver/ows?foo=bar", valid: true
}, {
url: "bogus", valid: false
}, {
url: "bogus/geoserver/ows", valid: false
}, {
url: "http://bogus", valid: false
}, {
url: "http://bogus.com", valid: false
}, {
url: "http://bogus.com:80", valid: false
}, {
url: "http://user@bogus.com/", valid: false
}];

var num = cases.length;
t.plan(num);

var regex = gxp.NewSourceWindow.prototype.urlRegExp;

var c;
for (var i=0; i<num; ++i) {
c = cases[i];
t.eq(regex.test(c.url), c.valid, c.url);
}

}

function test_urlValidator(t) {

var cases = [{
url: "http://example.com/geoserver/wms", valid: true
}, {
url: "bogus", valid: false
}, {
url: "http://example.com/geoserver/ows", previousError: "Couldn't load caps doc", valid: false
}];

var num = cases.length;
t.plan(num * 2);

var win = new gxp.NewSourceWindow();
var c, got, exp, msg;
for (var i=0; i<num; ++i) {
c = cases[i];
// set previous error (if any)
win.error = c.previousError;
got = win.urlValidator(c.url);
if (!c.previousError) {
// no previous error, expect true or invalid url text
exp = c.valid || win.invalidURLText;
} else {
// previous error, expect the previous error message
exp = c.previousError;
}
msg = c.url + (c.previousError ? " " + c.previousError : "");
// confirm that validator returned what we expect
t.eq(got, exp, msg);
// assert that the win.error property is set to null after validation
t.eq(win.error, null, "error set to null: " + msg);
}

win.destroy();

}

</script>
</head>
Expand Down

0 comments on commit 94ec32d

Please sign in to comment.