Skip to content

Commit

Permalink
Merge pull request #151 from tightenco/allow-custom-ziggy-in-url-builder
Browse files Browse the repository at this point in the history
Allow custom Ziggy to be passed into UrlBuilder (so I can use UrlBuilder in other packages.)
  • Loading branch information
DanielCoulbourne authored Apr 21, 2018
2 parents 6abb70e + d3c45b0 commit 1da970b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
14 changes: 9 additions & 5 deletions dist/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,14 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

var UrlBuilder = function () {
function UrlBuilder(name, absolute) {
var route = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var customZiggy = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;

_classCallCheck(this, UrlBuilder);

this.name = name;
this.route = Ziggy.namedRoutes[this.name];
this.ziggy = customZiggy ? customZiggy : Ziggy;
this.route = route ? route : this.ziggy.namedRoutes[this.name];

if (typeof this.name === 'undefined') {
throw new Error('Ziggy Error: You must provide a route name');
Expand All @@ -286,13 +290,13 @@ var UrlBuilder = function () {
value: function setDomain() {
if (!this.absolute) return '/';

if (!this.route.domain) return Ziggy.baseUrl.replace(/\/?$/, '/');
if (!this.route.domain) return this.ziggy.baseUrl.replace(/\/?$/, '/');

var host = (this.route.domain || Ziggy.baseDomain).replace(/\/+$/, '');
var host = (this.route.domain || this.ziggy.baseDomain).replace(/\/+$/, '');

if (Ziggy.basePort && host.replace(/\/+$/, '') === Ziggy.baseDomain.replace(/\/+$/, '')) host = Ziggy.baseDomain + ':' + Ziggy.basePort;
if (this.ziggy.basePort && host.replace(/\/+$/, '') === this.ziggy.baseDomain.replace(/\/+$/, '')) host = this.ziggy.baseDomain + ':' + this.ziggy.basePort;

return Ziggy.baseProtocol + '://' + host + '/';
return this.ziggy.baseProtocol + '://' + host + '/';
}
}, {
key: 'construct',
Expand Down
2 changes: 1 addition & 1 deletion dist/js/route.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions src/js/UrlBuilder.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
class UrlBuilder {
constructor(name, absolute) {
constructor(name, absolute, route = null, customZiggy = null) {

this.name = name;
this.route = Ziggy.namedRoutes[this.name];
this.name = name;
this.ziggy = customZiggy ? customZiggy : Ziggy;
this.route = route ? route : this.ziggy.namedRoutes[this.name];

if (typeof this.name === 'undefined') {
throw new Error('Ziggy Error: You must provide a route name');
} else if (typeof this.route === 'undefined') {
throw new Error(`Ziggy Error: route '${this.name}' is not found in the route list`);
}

this.absolute = typeof absolute === 'undefined' ? true : absolute;
this.domain = this.setDomain();
this.path = this.route.uri.replace(/^\//, '');
this.absolute = typeof absolute === 'undefined' ? true : absolute;
this.domain = this.setDomain();
this.path = this.route.uri.replace(/^\//, '');
}

setDomain() {
if (! this.absolute)
if (!this.absolute)
return '/';

if (!this.route.domain)
return Ziggy.baseUrl.replace(/\/?$/, '/');
return this.ziggy.baseUrl.replace(/\/?$/, '/');

let host = (this.route.domain || Ziggy.baseDomain).replace(/\/+$/, '');
let host = (this.route.domain || this.ziggy.baseDomain).replace(/\/+$/, '');

if (Ziggy.basePort && (host.replace(/\/+$/, '') === Ziggy.baseDomain.replace(/\/+$/, '')))
host = Ziggy.baseDomain + ':' + Ziggy.basePort;
if (this.ziggy.basePort && (host.replace(/\/+$/, '') === this.ziggy.baseDomain.replace(/\/+$/, '')))
host = this.ziggy.baseDomain + ':' + this.ziggy.basePort;

return Ziggy.baseProtocol + '://' + host + '/';
return this.ziggy.baseProtocol + '://' + host + '/';
}

construct() {
Expand Down

0 comments on commit 1da970b

Please sign in to comment.