-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- `<carbon-router>` has been re-imagined as the more obscure, more unwieldy `<carbon-route-converter>` aka "do not use unless you know what you are doing." - `<carbon-location>` is a macro element that composes `<iron-page-url>`, `<iron-query-params>` and `<carbon-route-converter>`
- Loading branch information
Chris Joel
committed
Feb 11, 2016
1 parent
727e575
commit 6d25d9b
Showing
9 changed files
with
324 additions
and
116 deletions.
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/); | ||
}); | ||
|
||
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.