Search code examples
javascriptpolymerpolymer-1.0polymer-starter-kit

Polymer - Change paper-toolbar contents depending on selected iron-page


Expanding on the Polymer Starter Kit I'm struggling to figure out how I can change the hamburger(menu) icon to a different button when a different iron-page is active.


index.html

<paper-toolbar class="paper-toolbar-0" id="mainToolbar">

  <paper-icon-button id="paperToggle" icon="menu" paper-drawer-toggle></paper-icon-button>
  <!-- more toolbar content here -->

</paper-toolbar>

<iron-pages attr-for-selected="data-route" class="layout vertical center" selected="{{route}}">

  <!-- Top Stories -->     
  <section data-route="topstories">
    <story-list id="story-list" class="layout vertical center-justified"></story-list>
  </section>

   <!-- Item Info -->     
  <section data-route="item-info" class="fullbleed layout center-justified">
    <item-info firebase-item-id="[[params.firebaseitemid]]" class="fullbleed flex"></item-info>
  </section>

</iron-pages>

When the item-info route is selected, I would like to replace the menu paper-toggle-button with a back button. Possibly changing the toolbar text from the application title to 'Back to Top Stories' say.

Now, I think this is possible by setting an id on each of these controls and then have some Javascript in the item-info element that makes these changes. I'd prefer something a little more modular as this would tie my item-info element very closely to content that is external to it.


Solution

  • I did a custom polymer element to which you pass an array of icons and puts them on the toolbar.

    Here is the code:

    <dom-module id="my-toolbar-icons">
    <template>
        <template is="dom-repeat" items="{{contextualButtons}}" as="button" id="buttonsRepeat">
            <paper-icon-button contextual-action="{{button.action}}" icon="{{button.icon}}" on-tap="onContextualButtonTap"></paper-icon-button>
        </template>
    </template>
    
    <script>
    Polymer({
        is: 'my-toolbar-icons',
        properties: {
            contextualButtons: {
                type: Array,
                value: function () {
                    return [];
                }
            }
        },
        ready: function(){
            //set data just to show an icon
            this.contextualButtons = [{ icon: 'social:person-add', action: 'new' }, { icon: 'delete', action: 'delete' }, { icon: 'cloud-upload', action: 'update' }, { icon: 'image:camera-alt', action: 'takephoto' }, { icon: 'search', action: 'search' }];
        },
        setData: function (newContextualButtons) {
            //set data to the array (usage)
            //var item = document.querySelector("#id-of-this-element")
            //item.setData(someArrayOfIcons)
            this.contextualButtons = newContextualButtons;
        },
        onContextualButtonTap: function (event) {
            var item = this.$.buttonsRepeat.itemForElement(event.target);
            this.fire('customize-listener', item.action);
        }
    });
    </script>
    </dom-module>
    
    <!--Usage-->
    <my-toolbar-icons id="my-toolbar-icons"></my-toolbar-icons> <!--declaration-->
    
    <!--with javascript-->
    this.toolbar= document.querySelector("#my-toolbar-icons");
    this.toolbar.setData([{ icon: 'social:person-add', action: 'new' }]);
    

    This is just an idea that can be used to make you want to do.