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-base" 8 on-pointerdown="down" on-pointerup="up" on-pointerout="out" 9 attributes="accents char invert repeat superscript toKeyset toLayout"> 10 <script> 11 /** 12 * The long-press delay in milliseconds before long-press handler is 13 * invoked. 14 * @const 15 * @type {number} 16 */ 17 var LONGPRESS_DELAY_MSEC = 500; 18 19 /** 20 * The maximum number of elements in one keyset rule. 21 * @const 22 * @type {number} 23 */ 24 var MAXIMUM_NUM_OF_RULE_ELEMENTS = 3; 25 26 /** 27 * The minumum number of elements in one keyset rule. 28 * @const 29 * @type {number} 30 */ 31 var MINIMUM_NUM_OF_RULE_ELEMENTS = 2; 32 33 /** 34 * The index of event type element in the splitted keyset rule. 35 * @const 36 * @type {number} 37 */ 38 var EVENT_TYPE = 0; 39 40 /** 41 * The index of toKeyset element in the splitted keyset rule. 42 * @const 43 * @type {number} 44 */ 45 var TO_KEYSET = 1; 46 47 /** 48 * The index of nextKeyset element in the splitted keyset rule. 49 * @const 50 * @type {number} 51 */ 52 var NEXT_KEYSET = 2; 53 54 /** 55 * The index offset of toKeyset and nextKeyset elements in splitted keyset 56 * rule array and the array in keysetRules. 57 * @const 58 * @type {number} 59 */ 60 var OFFSET = 1; 61 62 /** 63 * The minumum number of elements in one keyset rule. 64 * @const {number} 65 */ 66 var MINIMUM_NUM_OF_RULE_ELEMENTS = 2; 67 68 Polymer('kb-key-base', { 69 repeat: false, 70 invert: false, 71 longPressTimer: null, 72 73 /** 74 * The keyset transition rules. It defines which keyset to transit to on 75 * which key events. It consists at most four rules (down, up, long, dbl). 76 * If no rule is defined for a key event, the event wont trigger a keyset 77 * change. 78 * @type {Object.<string, Array.<string>} 79 */ 80 keysetRules: null, 81 82 ready: function() { 83 if (this.toKeyset) { 84 // Parsing keyset rules from toKeyset attribute string. 85 // An rule can be defined as: (down|up|long|dbl):keysetid[:keysetid] 86 // and each rule are separated by a semicolon. The first element 87 // defines the event this rule applies to. The second element defines 88 // what keyset to transit to after event received. The third optional 89 // element defines what keyset to transit to after a key is pressed in 90 // the new keyset. It is useful when you want to transit to a not 91 // locked keyset. For example, after transit to a upper case keyset, 92 // it may make sense to transit back to lower case after user typed 93 // any key at the upper case keyset. 94 var rules = 95 this.toKeyset.replace(/(\r\n|\n|\r| )/g, '').split(';'); 96 this.keysetRules = {}; 97 var self = this; 98 rules.forEach(function(element) { 99 if (element == '') 100 return; 101 var keyValues = element.split(':', MAXIMUM_NUM_OF_RULE_ELEMENTS); 102 if (keyValues.length < MINIMUM_NUM_OF_RULE_ELEMENTS) { 103 console.error('Invalid keyset rules: ' + element); 104 return; 105 } 106 self.keysetRules[keyValues[EVENT_TYPE]] = [keyValues[TO_KEYSET], 107 (keyValues[NEXT_KEYSET] ? keyValues[NEXT_KEYSET] : null)]; 108 }); 109 } 110 }, 111 down: function(event) { 112 var detail = { 113 char: this.char || this.textContent, 114 toLayout: this.toLayout, 115 repeat: this.repeat 116 }; 117 if (this.keysetRules && this.keysetRules.down != undefined) { 118 detail.toKeyset = this.keysetRules.down[TO_KEYSET - OFFSET]; 119 detail.nextKeyset = this.keysetRules.down[NEXT_KEYSET - OFFSET]; 120 } 121 this.fire('key-down', detail); 122 this.longPressTimer = this.asyncMethod(function() { 123 var detail = { 124 char: this.char || this.textContent, 125 superscript: this.superscript 126 }; 127 if (this.keysetRules && this.keysetRules.long != undefined) { 128 detail.toKeyset = this.keysetRules.long[TO_KEYSET - OFFSET]; 129 detail.nextKeyset = this.keysetRules.long[NEXT_KEYSET - OFFSET]; 130 } 131 this.fire('key-longpress', detail); 132 }, null, LONGPRESS_DELAY_MSEC); 133 }, 134 out: function(event) { 135 clearTimeout(this.longPressTimer); 136 }, 137 up: function(event) { 138 clearTimeout(this.longPressTimer); 139 var detail = { 140 char: this.char || this.textContent, 141 toLayout: this.toLayout 142 }; 143 if (this.keysetRules && this.keysetRules.up != undefined) { 144 detail.toKeyset = this.keysetRules.up[TO_KEYSET - OFFSET]; 145 detail.nextKeyset = this.keysetRules.up[NEXT_KEYSET - OFFSET]; 146 } 147 this.fire('key-up', detail); 148 } 149 }); 150 </script> 151 </polymer-element> 152