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
26WebInspector.DetailsSectionSimpleRow = function(label, value) {
27    WebInspector.DetailsSectionRow.call(this);
28
29    this.element.classList.add(WebInspector.DetailsSectionSimpleRow.StyleClassName);
30
31    this._labelElement = document.createElement("div");
32    this._labelElement.className = WebInspector.DetailsSectionSimpleRow.LabelElementStyleClassName;
33    this.element.appendChild(this._labelElement);
34
35    this._valueElement = document.createElement("div");
36    this._valueElement.className = WebInspector.DetailsSectionSimpleRow.ValueElementStyleClassName;
37    this.element.appendChild(this._valueElement);
38
39    // Workaround for <rdar://problem/12668870> Triple-clicking text within a
40    // <div> set to "display: table-cell" selects text outside the cell.
41    //
42    // On triple-click, adjust the selection range to include only the value
43    // element if the selection extends beyond it.
44    var valueElementClicked = function(event) {
45        event.stopPropagation();
46
47        if (event.detail < 3)
48            return;
49
50        var currentSelection = window.getSelection();
51        if (!currentSelection)
52            return;
53
54        var currentRange = currentSelection.getRangeAt(0);
55        if (!currentRange || currentRange.startContainer == currentRange.endContainer)
56            return;
57
58        var correctedRange = document.createRange();
59        correctedRange.selectNodeContents(event.currentTarget);
60        currentSelection.removeAllRanges();
61        currentSelection.addRange(correctedRange);
62    };
63    this._valueElement.addEventListener("click", valueElementClicked);
64
65    this.label = label;
66    this.value = value;
67};
68
69WebInspector.DetailsSectionSimpleRow.StyleClassName = "simple";
70WebInspector.DetailsSectionSimpleRow.DataStyleClassName = "data";
71WebInspector.DetailsSectionSimpleRow.EmptyStyleClassName = "empty";
72WebInspector.DetailsSectionSimpleRow.LabelElementStyleClassName = "label";
73WebInspector.DetailsSectionSimpleRow.ValueElementStyleClassName = "value";
74
75WebInspector.DetailsSectionSimpleRow.prototype = {
76    constructor: WebInspector.DetailsSectionSimpleRow,
77
78    // Public
79
80    get label()
81    {
82        return this._labelElement.textContent;
83    },
84
85    set label(label)
86    {
87        this._labelElement.textContent = label;
88    },
89
90    get value()
91    {
92        return this._value;
93    },
94
95    set value(value)
96    {
97        this._value = value || "";
98
99        if (this._value) {
100            this.element.classList.remove(WebInspector.DetailsSectionSimpleRow.EmptyStyleClassName);
101
102            // If the value has space characters that cause word wrapping then we don't need the data class.
103            if (/[\s\u200b]/.test(this._value))
104                this.element.classList.remove(WebInspector.DetailsSectionSimpleRow.DataStyleClassName);
105            else
106                this.element.classList.add(WebInspector.DetailsSectionSimpleRow.DataStyleClassName);
107        } else {
108            this.element.classList.add(WebInspector.DetailsSectionSimpleRow.EmptyStyleClassName);
109            this.element.classList.remove(WebInspector.DetailsSectionSimpleRow.DataStyleClassName);
110        }
111
112        if (value instanceof Node) {
113            this._valueElement.removeChildren();
114            this._valueElement.appendChild(this._value);
115        } else
116            this._valueElement.textContent = this._value;
117    }
118};
119
120WebInspector.DetailsSectionSimpleRow.prototype.__proto__ = WebInspector.DetailsSectionRow.prototype;
121