Home | History | Annotate | Download | only in elements
      1 <!--
      2   -- Copyright 2013 The Chromium Authors. All rights reserved.
      3   -- Use of this source code is governed by a BSD-style license that can be
      4   -- found in the LICENSE file.
      5   -->
      6 
      7 <polymer-element name="kb-key-sequence"
      8     attributes="keys hintTexts keyCodes invert">
      9   <template>
     10     <style>
     11       :host {
     12         display: none;
     13       }
     14     </style>
     15   </template>
     16   <script>
     17     (function() {
     18 
     19       var metadata = null;
     20 
     21       function getKeyCodeAndModifiers(keyCodeIndex) {
     22         if (!metadata)
     23           metadata = document.createElement('kb-key-codes'); 
     24         return metadata.GetKeyCodeAndModifiers(keyCodeIndex);
     25       }
     26 
     27       Polymer('kb-key-sequence', {
     28         /**
     29          * Generates the DOM structure to replace (expand) this kb-key-sequence.
     30          */
     31         generateDom: function() {
     32           var replacement = document.createDocumentFragment();
     33           var newKeys = this.getAttribute('keys');
     34           var newHintTexts = this.getAttribute('hintTexts');
     35           var keyCodes = this.getAttribute('hintCodes') || newKeys;
     36           var invert = this.getAttribute('invert');
     37           if (newKeys) {
     38             if (newHintTexts && newKeys.length != newHintTexts.length) {
     39               console.error('keys and hintTexts do not match');
     40               return;
     41             }
     42             if (keyCodes && newKeys.length != keyCodes.length) {
     43                console.error('keys and keyCodes do not match');
     44               return;
     45             }
     46 
     47             for (var i = 0; i < newKeys.length; i++) {
     48               var key = document.createElement('kb-key');
     49               key.innerText = newKeys[i];
     50               key.accents = newKeys[i];
     51               if (newHintTexts)
     52                 key.hintText = newHintTexts[i];
     53               var keyCodeIndex = keyCodes[i];
     54               if (invert) {
     55                 key.invert = true;
     56                 key.char = newKeys[i];
     57                 keyCodeIndex = key.hintText;
     58               }
     59               var state = getKeyCodeAndModifiers(keyCodeIndex);
     60               if (state) {
     61                 key.keyCode = state.keyCode;
     62                 key.keyName = state.keyName;
     63                 key.shiftModifier = state.shiftModifier;
     64               }
     65               replacement.appendChild(key);
     66             }
     67           }
     68           return replacement;
     69         }
     70       });
     71     })();
     72   </script>
     73 </polymer-element>
     74