ShortcutsScreen.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2010 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. */
  33. WebInspector.ShortcutsScreen = function()
  34. {
  35. this._sections = /** @type {Object.<string, !WebInspector.ShortcutsSection>} */ ({});
  36. }
  37. WebInspector.ShortcutsScreen.prototype = {
  38. /**
  39. * @param {string} name
  40. * @return {!WebInspector.ShortcutsSection}
  41. */
  42. section: function(name)
  43. {
  44. var section = this._sections[name];
  45. if (!section)
  46. this._sections[name] = section = new WebInspector.ShortcutsSection(name);
  47. return section;
  48. },
  49. /**
  50. * @return {!WebInspector.View}
  51. */
  52. createShortcutsTabView: function()
  53. {
  54. var orderedSections = [];
  55. for (var section in this._sections)
  56. orderedSections.push(this._sections[section]);
  57. function compareSections(a, b)
  58. {
  59. return a.order - b.order;
  60. }
  61. orderedSections.sort(compareSections);
  62. var view = new WebInspector.View();
  63. view.element.className = "settings-tab-container";
  64. view.element.createChild("header").createChild("h3").appendChild(document.createTextNode(WebInspector.UIString("Shortcuts")));
  65. var container = view.element.createChild("div", "help-container-wrapper").createChild("div");
  66. container.className = "help-content help-container";
  67. for (var i = 0; i < orderedSections.length; ++i)
  68. orderedSections[i].renderSection(container);
  69. return view;
  70. }
  71. }
  72. /**
  73. * We cannot initialize it here as localized strings are not loaded yet.
  74. * @type {?WebInspector.ShortcutsScreen}
  75. */
  76. WebInspector.shortcutsScreen = null;
  77. /**
  78. * @constructor
  79. * @param {string} name
  80. */
  81. WebInspector.ShortcutsSection = function(name)
  82. {
  83. this.name = name;
  84. this._lines = /** @type {!Array.<{key: !Node, text: string}>} */ ([]);
  85. this.order = ++WebInspector.ShortcutsSection._sequenceNumber;
  86. };
  87. WebInspector.ShortcutsSection._sequenceNumber = 0;
  88. WebInspector.ShortcutsSection.prototype = {
  89. /**
  90. * @param {!WebInspector.KeyboardShortcut.Descriptor} key
  91. * @param {string} description
  92. */
  93. addKey: function(key, description)
  94. {
  95. this._addLine(this._renderKey(key), description);
  96. },
  97. /**
  98. * @param {!Array.<!WebInspector.KeyboardShortcut.Descriptor>} keys
  99. * @param {string} description
  100. */
  101. addRelatedKeys: function(keys, description)
  102. {
  103. this._addLine(this._renderSequence(keys, "/"), description);
  104. },
  105. /**
  106. * @param {!Array.<!WebInspector.KeyboardShortcut.Descriptor>} keys
  107. * @param {string} description
  108. */
  109. addAlternateKeys: function(keys, description)
  110. {
  111. this._addLine(this._renderSequence(keys, WebInspector.UIString("or")), description);
  112. },
  113. /**
  114. * @param {!Node} keyElement
  115. * @param {string} description
  116. */
  117. _addLine: function(keyElement, description)
  118. {
  119. this._lines.push({ key: keyElement, text: description })
  120. },
  121. /**
  122. * @param {!Element} container
  123. */
  124. renderSection: function(container)
  125. {
  126. var parent = container.createChild("div", "help-block");
  127. var headLine = parent.createChild("div", "help-line");
  128. headLine.createChild("div", "help-key-cell");
  129. headLine.createChild("div", "help-section-title help-cell").textContent = this.name;
  130. for (var i = 0; i < this._lines.length; ++i) {
  131. var line = parent.createChild("div", "help-line");
  132. var keyCell = line.createChild("div", "help-key-cell");
  133. keyCell.appendChild(this._lines[i].key);
  134. keyCell.appendChild(this._createSpan("help-key-delimiter", ":"));
  135. line.createChild("div", "help-cell").textContent = this._lines[i].text;
  136. }
  137. },
  138. /**
  139. * @param {!Array.<!WebInspector.KeyboardShortcut.Descriptor>} sequence
  140. * @param {string} delimiter
  141. * @return {!Node}
  142. */
  143. _renderSequence: function(sequence, delimiter)
  144. {
  145. var delimiterSpan = this._createSpan("help-key-delimiter", delimiter);
  146. return this._joinNodes(sequence.map(this._renderKey.bind(this)), delimiterSpan);
  147. },
  148. /**
  149. * @param {!WebInspector.KeyboardShortcut.Descriptor} key
  150. * @return {!Node}
  151. */
  152. _renderKey: function(key)
  153. {
  154. var keyName = key.name;
  155. var plus = this._createSpan("help-combine-keys", "+");
  156. return this._joinNodes(keyName.split(" + ").map(this._createSpan.bind(this, "help-key monospace")), plus);
  157. },
  158. /**
  159. * @param {string} className
  160. * @param {string} textContent
  161. * @return {!Element}
  162. */
  163. _createSpan: function(className, textContent)
  164. {
  165. var node = document.createElement("span");
  166. node.className = className;
  167. node.textContent = textContent;
  168. return node;
  169. },
  170. /**
  171. * @param {!Array.<!Element>} nodes
  172. * @param {!Element} delimiter
  173. * @return {!Node}
  174. */
  175. _joinNodes: function(nodes, delimiter)
  176. {
  177. var result = document.createDocumentFragment();
  178. for (var i = 0; i < nodes.length; ++i) {
  179. if (i > 0)
  180. result.appendChild(delimiter.cloneNode(true));
  181. result.appendChild(nodes[i]);
  182. }
  183. return result;
  184. }
  185. }