DetailsSectionSimpleRow.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2013 Apple 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.DetailsSectionSimpleRow = function(label, value) {
  26. WebInspector.DetailsSectionRow.call(this);
  27. this.element.classList.add(WebInspector.DetailsSectionSimpleRow.StyleClassName);
  28. this._labelElement = document.createElement("div");
  29. this._labelElement.className = WebInspector.DetailsSectionSimpleRow.LabelElementStyleClassName;
  30. this.element.appendChild(this._labelElement);
  31. this._valueElement = document.createElement("div");
  32. this._valueElement.className = WebInspector.DetailsSectionSimpleRow.ValueElementStyleClassName;
  33. this.element.appendChild(this._valueElement);
  34. // Workaround for <rdar://problem/12668870> Triple-clicking text within a
  35. // <div> set to "display: table-cell" selects text outside the cell.
  36. //
  37. // On triple-click, adjust the selection range to include only the value
  38. // element if the selection extends beyond it.
  39. var valueElementClicked = function(event) {
  40. event.stopPropagation();
  41. if (event.detail < 3)
  42. return;
  43. var currentSelection = window.getSelection();
  44. if (!currentSelection)
  45. return;
  46. var currentRange = currentSelection.getRangeAt(0);
  47. if (!currentRange || currentRange.startContainer == currentRange.endContainer)
  48. return;
  49. var correctedRange = document.createRange();
  50. correctedRange.selectNodeContents(event.currentTarget);
  51. currentSelection.removeAllRanges();
  52. currentSelection.addRange(correctedRange);
  53. };
  54. this._valueElement.addEventListener("click", valueElementClicked);
  55. this.label = label;
  56. this.value = value;
  57. };
  58. WebInspector.DetailsSectionSimpleRow.StyleClassName = "simple";
  59. WebInspector.DetailsSectionSimpleRow.DataStyleClassName = "data";
  60. WebInspector.DetailsSectionSimpleRow.EmptyStyleClassName = "empty";
  61. WebInspector.DetailsSectionSimpleRow.LabelElementStyleClassName = "label";
  62. WebInspector.DetailsSectionSimpleRow.ValueElementStyleClassName = "value";
  63. WebInspector.DetailsSectionSimpleRow.prototype = {
  64. constructor: WebInspector.DetailsSectionSimpleRow,
  65. // Public
  66. get label()
  67. {
  68. return this._labelElement.textContent;
  69. },
  70. set label(label)
  71. {
  72. this._labelElement.textContent = label;
  73. },
  74. get value()
  75. {
  76. return this._value;
  77. },
  78. set value(value)
  79. {
  80. this._value = value || "";
  81. if (this._value) {
  82. this.element.classList.remove(WebInspector.DetailsSectionSimpleRow.EmptyStyleClassName);
  83. // If the value has space characters that cause word wrapping then we don't need the data class.
  84. if (/[\s\u200b]/.test(this._value))
  85. this.element.classList.remove(WebInspector.DetailsSectionSimpleRow.DataStyleClassName);
  86. else
  87. this.element.classList.add(WebInspector.DetailsSectionSimpleRow.DataStyleClassName);
  88. } else {
  89. this.element.classList.add(WebInspector.DetailsSectionSimpleRow.EmptyStyleClassName);
  90. this.element.classList.remove(WebInspector.DetailsSectionSimpleRow.DataStyleClassName);
  91. }
  92. if (value instanceof Node) {
  93. this._valueElement.removeChildren();
  94. this._valueElement.appendChild(this._value);
  95. } else
  96. this._valueElement.textContent = this._value;
  97. }
  98. };
  99. WebInspector.DetailsSectionSimpleRow.prototype.__proto__ = WebInspector.DetailsSectionRow.prototype;