| OLD | NEW |
| (Empty) |
| 1 <!-- Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 2 Use of this source code is governed by a BSD-style license that can be | |
| 3 found in the LICENSE file. --> | |
| 4 | |
| 5 <!-- | |
| 6 Use to make all links in a component view navigation aware. This will trap | |
| 7 clicks to links in that component and then redirect the navigation to the | |
| 8 <cr-view> component by dispatching a "navigate" event. | |
| 9 | |
| 10 To use just add the component to the <template> inside all components that | |
| 11 use <a href> where the link should switch to a different view. | |
| 12 | |
| 13 This is done instead of having a special <cr-link> element to avoid having | |
| 14 to create hundreds of extra custom elements on list pages, and to make | |
| 15 adding new links to components more obvious. | |
| 16 | |
| 17 ex. | |
| 18 <cr-view-handler></cr-view-handler> | |
| 19 --> | |
| 20 <polymer-element name="cr-view-handler"> | |
| 21 <template> | |
| 22 <style> | |
| 23 :host { display: none; } | |
| 24 </style> | |
| 25 </template> | |
| 26 <script> | |
| 27 Polymer("cr-view-handler", { | |
| 28 created: function() { | |
| 29 this.handleClick = this.handleClick.bind(this); | |
| 30 }, | |
| 31 attached: function() { | |
| 32 this.scope = this.enclosingTreeScope(); | |
| 33 this.scope.addEventListener("click", this.handleClick); | |
| 34 }, | |
| 35 detached: function() { | |
| 36 this.scope.removeEventListener("click", this.handleClick); | |
| 37 }, | |
| 38 handleClick: function(event) { | |
| 39 if (event.metaKey || event.ctrlKey || event.button == 1) | |
| 40 return; | |
| 41 var a = this.findEnclosingLink(event.target); | |
| 42 if (!a) | |
| 43 return; | |
| 44 if (!a.href.startsWith(window.location.origin)) | |
| 45 return; | |
| 46 if (a.getAttribute("is") == "cr-action") | |
| 47 return; | |
| 48 event.preventDefault(); | |
| 49 this.asyncFire("navigate", { | |
| 50 url: a.pathname + a.search + a.hash | |
| 51 }); | |
| 52 }, | |
| 53 findEnclosingLink: function(node) { | |
| 54 while (node && node.nodeName != "A") | |
| 55 node = node.parentNode; | |
| 56 return node; | |
| 57 }, | |
| 58 enclosingTreeScope: function() { | |
| 59 var scope = this; | |
| 60 while (scope.parentNode) | |
| 61 scope = scope.parentNode; | |
| 62 return scope; | |
| 63 }, | |
| 64 }); | |
| 65 </script> | |
| 66 </polymer-element> | |
| OLD | NEW |