-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement <carbon-location>
#4
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
link rel="import" href="../polymer/polymer.html"> | ||
<link rel="import" href="../iron-page-url/iron-page-url.html"> | ||
<link rel="import" href="../iron-page-url/iron-query-params.html"> | ||
<link rel="import" href="carbon-route-converter.html"> | ||
|
||
<dom-module id="carbon-location"> | ||
<template> | ||
<iron-page-url path="{{__path}}" query="{{__query}}"></iron-page-url> | ||
<iron-query-params | ||
params-string="{{__query}}" | ||
params-object="{{__queryParams}}"> | ||
</iron-query-params> | ||
<carbon-route-converter | ||
path="{{__path}}" | ||
query-params="{{__queryParams}}" | ||
route="{{route}}"> | ||
</carbon-route-converter> | ||
</template> | ||
<script> | ||
'use strict'; | ||
|
||
Polymer({ | ||
is: 'carbon-location', | ||
|
||
properties: { | ||
/** | ||
* A model representing the deserialized path through the route tree, as | ||
* well as the current queryParams. | ||
*/ | ||
route: { | ||
type: Object, | ||
notify: true | ||
}, | ||
|
||
/** | ||
* A set of key/value pairs that are universally accessible to branches | ||
* of the route tree. | ||
*/ | ||
__queryParams: { | ||
type: Object | ||
}, | ||
|
||
/** | ||
* The pathname component of the current URL. | ||
*/ | ||
__path: { | ||
type: String | ||
}, | ||
|
||
/** | ||
* The query string portion of the current URL. | ||
*/ | ||
__query: { | ||
type: String | ||
} | ||
} | ||
}); | ||
</script> | ||
</dom-module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<link rel="import" href="../polymer/polymer.html"> | ||
|
||
<script> | ||
'use strict'; | ||
|
||
Polymer({ | ||
is: 'carbon-route-converter', | ||
|
||
properties: { | ||
/** | ||
* A model representing the deserialized path through the route tree, as | ||
* well as the current queryParams. | ||
* | ||
* A route object is the kernel of the routing system. It is intended to | ||
* be fed into consuming elements such as `carbon-route`. | ||
*/ | ||
route: { | ||
type: Object, | ||
value: null, | ||
notify: true | ||
}, | ||
|
||
/** | ||
* A set of key/value pairs that are universally accessible to branches of | ||
* the route tree. | ||
*/ | ||
queryParams: { | ||
type: Object, | ||
value: null, | ||
notify: true | ||
}, | ||
|
||
/** | ||
* The serialized path through the route tree. This corresponds to the | ||
* `window.location.pathname` value, and will update to reflect changes | ||
* to that valaue. | ||
*/ | ||
path: { | ||
type: String, | ||
notify: true | ||
} | ||
}, | ||
|
||
observers: [ | ||
'_locationChanged(path, queryParams)', | ||
'_routeChanged(route.prefix, route.path)', | ||
'_routeQueryParamsChanged(route.queryParams)' | ||
], | ||
|
||
created: function() { | ||
this.linkPaths('route.queryParams', 'queryParams'); | ||
}, | ||
|
||
_locationChanged: function(path) { | ||
this.route = { | ||
prefix: '', | ||
path: path, | ||
queryParams: this.queryParams | ||
}; | ||
}, | ||
|
||
_routeChanged: function(prefix, path) { | ||
if (!this.route) { | ||
return; | ||
} | ||
this.path = prefix + path; | ||
}, | ||
|
||
_routeQueryParamsChanged: function(queryParams) { | ||
if (!this.route) { | ||
return; | ||
} | ||
this.queryParams = queryParams; | ||
} | ||
}); | ||
</script> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<!doctype html> | ||
<!-- | ||
Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<html> | ||
<head> | ||
<title>carbon-location</title> | ||
|
||
<script src="../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<script src="../../web-component-tester/browser.js"></script> | ||
|
||
<link rel="import" href="../../polymer/polymer.html"> | ||
<link rel="import" href="../carbon-location.html"> | ||
</head> | ||
<body> | ||
<test-fixture id="BasicLocation"> | ||
<template> | ||
<carbon-location></carbon-location> | ||
</template> | ||
</test-fixture> | ||
|
||
<script> | ||
'use strict'; | ||
|
||
function setLocation(url) { | ||
window.history.pushState({}, '', url); | ||
Polymer.Base.fire('location-changed', {}, { node: window }); | ||
} | ||
|
||
function assign(a, b) { | ||
if (Object.assign) { | ||
return Object.assign.apply(Object, arguments); | ||
} | ||
|
||
for (var property in b) { | ||
a[property] = b[property]; | ||
} | ||
|
||
return a; | ||
} | ||
|
||
suite('<carbon-location>', function () { | ||
var carbonLocation; | ||
|
||
setup(function() { | ||
carbonLocation = fixture('BasicLocation'); | ||
}); | ||
|
||
test('it automatically exposes the current route', function() { | ||
expect(carbonLocation.route).to.be.ok; | ||
expect(carbonLocation.route.path).to.be.equal(window.location.pathname); | ||
}); | ||
|
||
suite('manipulating the route', function() { | ||
var originalPath; | ||
var originalQueryParams; | ||
|
||
setup(function() { | ||
originalPath = carbonLocation.route.path; | ||
originalQueryParams = assign({}, carbonLocation.route.queryParams); | ||
}); | ||
|
||
teardown(function() { | ||
carbonLocation.set('route.prefix', ''); | ||
carbonLocation.set('route.path', originalPath); | ||
carbonLocation.set('route.queryParams', originalQueryParams); | ||
}); | ||
|
||
test('it reflects path to location.pathname', function() { | ||
carbonLocation.set('route.path', '/foo/bar'); | ||
expect(window.location.pathname).to.be.equal('/foo/bar'); | ||
}); | ||
|
||
test('it reflects queryParams values to location.search', function() { | ||
carbonLocation.set('route.queryParams.foo', 1); | ||
expect(window.location.search).to.match(/foo=1/); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🍰 |
||
}); | ||
|
||
test('it reflects completely replaced queryParams', function() { | ||
carbonLocation.set('route.queryParams', { bar: 1 }); | ||
expect(window.location.search).to.be.equal('?bar=1'); | ||
}); | ||
|
||
test('it reflects the prefix to location.pathname', function() { | ||
carbonLocation.set('route.prefix', '/fiz'); | ||
expect(window.location.pathname).to.be.equal('/fiz' + originalPath); | ||
}); | ||
}); | ||
|
||
/** | ||
* NOTE: For a more thorough spec describing this behavior, please refer | ||
* to the `iron-page-url` component. | ||
*/ | ||
suite('manipulating the history state', function() { | ||
var originalLocation; | ||
|
||
setup(function() { | ||
originalLocation = window.location.toString(); | ||
}); | ||
|
||
teardown(function() { | ||
setLocation(originalLocation); | ||
}); | ||
|
||
test('it reflects location.pathname to route.path', function() { | ||
setLocation('/fiz/buz'); | ||
expect(carbonLocation.route.path).to.be.equal('/fiz/buz'); | ||
}); | ||
|
||
test('it reflects location.search to route.queryParams', function() { | ||
setLocation('?fiz=buz'); | ||
expect(carbonLocation.route.queryParams).to.be.eql({ | ||
fiz: 'buz' | ||
}); | ||
}); | ||
}); | ||
}); | ||
</script> | ||
</body> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if carbon-location only exposed a
route
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with that, although it will leak the other properties one way or another due to the side-effects of (Polymer) data-binding. Would you prefer that the other properties be made
__private
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question. Yeah, I think that's for the best, just to make the contract for this element super clear.
Do any other elements do that? Name their undeclared and implicitly-created properties in private style?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do have elements that do this, but I'm surprisingly hard-pressed to find a good example. I think you are right about the contract. Please see the updated implementation!