app-shell.html 3.8 KB
<link rel="import" href="../../bower_components/paper-toolbar/paper-toolbar.html">
<link rel="import" href="../../bower_components/paper-drawer-panel/paper-drawer-panel.html">
<link rel="import" href="../../bower_components/paper-header-panel/paper-header-panel.html">
<link rel="import" href="../../bower_components/app-router/app-router.html">
<link rel="import" href="../../bower_components/paper-toast/paper-toast.html">

<link rel="import" href="../app-styles/app-styles.html">
<link rel="import" href="../app-link/app-link.html">
<link rel="import" href="../app-logo/app-logo.html">
<link rel="import" href="../app-cart/app-cart.html">
<link rel="import" href="../app-bar/app-bar.html">
<link rel="import" href="../app-sidebar/app-sidebar.html">

<link rel="import" href="../pages/page-packages.html">
<link rel="import" href="../pages/page-browse.html">
<link rel="import" href="../pages/page-element.html">
<link rel="import" href="../pages/page-guide.html">
<link rel="import" href="../pages/page-not-found.html">

<dom-module id="app-shell">
  <style>
    #cartpanel::shadow > #scrim {
      z-index: 4;
    }
    #cartpanel::shadow > #drawer {
      z-index: 5;
    }

    paper-toast {
      z-index: 5;
    }
  </style>
  <template>
    <paper-drawer-panel id="cartpanel" class="flex" force-narrow right-drawer disable-edge-swipe disable-swipe drawer-width="320px">
      <div id="main"  main>
        <app-router id="router" class="flex" mode="pushstate" on-activate-route-end="trackPageview">
          <app-route path="/" element="page-packages" bindRouter></app-route>
          <app-route path="/browse" element="page-browse" bindRouter onUrlChange="updateModel"></app-route>
          <app-route path="/elements/:element" element="page-element" bindRouter onUrlChange="updateModel"></app-route>
          <app-route path="/guides/:name" element="page-guide" bindRouter></app-route>
          <app-route path="*" element="page-not-found"></app-route>
        </app-router>
      </div>
      <app-cart id="cart" drawer></app-cart>
    </paper-drawer-panel>
  </template>
</dom-module>
<script>
  Polymer({
    is: 'app-shell',
    listeners: {
      'nav': 'handleNav',
      'page-meta': 'updateTitle',
      'cart-close': 'cartClose',
      'cart-open': 'cartOpen',
      'cart-add': 'cartAdd',
      'cart-remove': 'cartRemove',
      'toast': 'showToast'
    },
    _toast: null,

    toggleSearch: function() {
      this.$.search.active = !this.$.search.active;
    },
    performSearch: function(e) {
      e.preventDefault();
      if (this.query.length) this.$.router.go('/elements?q=' + this.query);
    },
    handleNav: function(_, nav) {
      this.$.router.go(nav.url);
      this.$.cartpanel.closeDrawer();
    },
    updateTitle: function(_, detail) {
      if (detail.title && detail.title.length) {
        document.title = detail.title + " - Polymer Element Catalog";
      } else {
        document.title = "Polymer Element Catalog";
      }
    },
    cartClose: function() {
      this.$.cartpanel.closeDrawer();
    },
    cartOpen: function() {
      this.$.cartpanel.openDrawer();
    },
    cartAdd: function(e, el) {
      this.$.cart.add(el);
    },
    cartRemove: function(e, el) {
      this.$.cart.remove(el);
    },
    showToast: function(e, detail) {
      if (!this._toast) {
        this._toast = document.createElement('paper-toast');
        Polymer.dom(this.$.router).appendChild(this._toast);
      }
      this._toast.text = detail.text;
      this.async(this._toast.show.bind(this._toast), 1);
    },

    trackPageview: function(e, detail) {
      this.debounce('pageview', function() {
        var loc = window.location.pathname + window.location.search;
        ga('send', 'pageview', {
          location: loc,
          title: document.title
        });
        ga('set', 'page', loc);
      }, 2000);
    }
  });
</script>