1/*
2 * Copyright (C) 2007, 2008, 2013 Apple Inc.  All rights reserved.
3 * Copyright (C) 2009 Joseph Pecoraro
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15 *     its contributors may be used to endorse or promote products derived
16 *     from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30WebInspector.ConsoleGroup = function(parentGroup, isNewSession)
31{
32    WebInspector.Object.call(this);
33
34    this.parentGroup = parentGroup;
35
36    var element = document.createElement("div");
37    element.className = "console-group";
38    element.group = this;
39    this.element = element;
40
41    if (isNewSession)
42        element.classList.add("new-session");
43
44    var messagesElement = document.createElement("div");
45    messagesElement.className = "console-group-messages";
46    element.appendChild(messagesElement);
47    this.messagesElement = messagesElement;
48};
49
50WebInspector.ConsoleGroup.prototype = {
51    constructor: WebInspector.ConsoleGroup,
52
53    // Public
54
55    addMessage: function(msg)
56    {
57        var element = msg.toMessageElement();
58
59        var wrapper = document.createElement("div");
60        wrapper.className = WebInspector.LogContentView.ItemWrapperStyleClassName;
61        wrapper.messageElement = wrapper.appendChild(element);
62
63        if (msg.type === WebInspector.ConsoleMessage.MessageType.StartGroup || msg.type === WebInspector.ConsoleMessage.MessageType.StartGroupCollapsed) {
64            this.messagesElement.parentNode.insertBefore(wrapper, this.messagesElement);
65            element.addEventListener("click", this._titleClicked.bind(this));
66            element.addEventListener("mousedown", this._titleMouseDown.bind(this));
67            var groupElement = element.enclosingNodeOrSelfWithClass("console-group");
68            if (groupElement && msg.type === WebInspector.ConsoleMessage.MessageType.StartGroupCollapsed)
69                groupElement.classList.add("collapsed");
70        } else
71            this.messagesElement.appendChild(wrapper);
72    },
73
74    hasMessages: function()
75    {
76        return !!this.messagesElement.childNodes.length;
77    },
78
79    // Private
80
81    _titleMouseDown: function(event)
82    {
83        event.preventDefault();
84    },
85
86    _titleClicked: function(event)
87    {
88        var groupTitleElement = event.target.enclosingNodeOrSelfWithClass("console-group-title");
89        if (groupTitleElement) {
90            var groupElement = groupTitleElement.enclosingNodeOrSelfWithClass("console-group");
91            if (groupElement)
92                if (groupElement.classList.contains("collapsed"))
93                    groupElement.classList.remove("collapsed");
94                else
95                    groupElement.classList.add("collapsed");
96            groupTitleElement.scrollIntoViewIfNeeded(true);
97        }
98    }
99};
100
101WebInspector.ConsoleGroup.prototype.__proto__ = WebInspector.Object.prototype;
102