2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * @extends {WebInspector.VBox}
30 WebInspector
.DatabaseTableView = function(database
, tableName
)
32 WebInspector
.VBox
.call(this);
34 this.database
= database
;
35 this.tableName
= tableName
;
37 this.element
.classList
.add("storage-view", "table");
39 this._visibleColumnsSetting
= WebInspector
.settings
.createSetting("databaseTableViewVisibleColumns", {});
41 this.refreshButton
= new WebInspector
.ToolbarButton(WebInspector
.UIString("Refresh"), "refresh-toolbar-item");
42 this.refreshButton
.addEventListener("click", this._refreshButtonClicked
, this);
43 this._visibleColumnsInput
= new WebInspector
.ToolbarInput(WebInspector
.UIString("Visible columns"), 1);
44 this._visibleColumnsInput
.addEventListener(WebInspector
.ToolbarInput
.Event
.TextChanged
, this._onVisibleColumnsChanged
, this);
47 WebInspector
.DatabaseTableView
.prototype = {
54 * @return {!Array.<!WebInspector.ToolbarItem>}
56 toolbarItems: function()
58 return [this.refreshButton
, this._visibleColumnsInput
];
62 * @param {string} tableName
65 _escapeTableName: function(tableName
)
67 return tableName
.replace(/\"/g, "\"\"");
72 this.database
.executeSql("SELECT rowid, * FROM \"" + this._escapeTableName(this.tableName
) + "\"", this._queryFinished
.bind(this), this._queryError
.bind(this));
75 _queryFinished: function(columnNames
, values
)
77 this.detachChildWidgets();
78 this.element
.removeChildren();
80 this._dataGrid
= WebInspector
.SortableDataGrid
.create(columnNames
, values
);
81 this._visibleColumnsInput
.setVisible(!!this._dataGrid
);
82 if (!this._dataGrid
) {
83 this._emptyWidget
= new WebInspector
.EmptyWidget(WebInspector
.UIString("The ā%sā\ntable is empty.", this.tableName
));
84 this._emptyWidget
.show(this.element
);
87 this._dataGrid
.show(this.element
);
88 this._dataGrid
.autoSizeColumns(5);
90 this._columnsMap
= new Map();
91 for (var i
= 1; i
< columnNames
.length
; ++i
)
92 this._columnsMap
.set(columnNames
[i
], String(i
));
93 this._lastVisibleColumns
= "";
94 var visibleColumnsText
= this._visibleColumnsSetting
.get()[this.tableName
] || "";
95 this._visibleColumnsInput
.setValue(visibleColumnsText
);
96 this._onVisibleColumnsChanged();
99 _onVisibleColumnsChanged: function()
103 var text
= this._visibleColumnsInput
.value();
104 var parts
= text
.split(/[\s,]+/);
105 var matches
= new Set();
106 var columnsVisibility
= {};
107 columnsVisibility
["0"] = true;
108 for (var i
= 0; i
< parts
.length
; ++i
) {
110 if (this._columnsMap
.has(part
)) {
112 columnsVisibility
[this._columnsMap
.get(part
)] = true;
115 var newVisibleColumns
= matches
.valuesArray().sort().join(", ");
116 if (newVisibleColumns
.length
=== 0) {
117 for (var v
of this._columnsMap
.values())
118 columnsVisibility
[v
] = true;
120 if (newVisibleColumns
=== this._lastVisibleColumns
)
122 var visibleColumnsRegistry
= this._visibleColumnsSetting
.get();
123 visibleColumnsRegistry
[this.tableName
] = text
;
124 this._visibleColumnsSetting
.set(visibleColumnsRegistry
);
125 this._dataGrid
.setColumnsVisiblity(columnsVisibility
);
126 this._lastVisibleColumns
= newVisibleColumns
;
129 _queryError: function(error
)
131 this.detachChildWidgets();
132 this.element
.removeChildren();
134 var errorMsgElement
= createElement("div");
135 errorMsgElement
.className
= "storage-table-error";
136 errorMsgElement
.textContent
= WebInspector
.UIString("An error occurred trying to\nread the ā%sā table.", this.tableName
);
137 this.element
.appendChild(errorMsgElement
);
140 _refreshButtonClicked: function(event
)
145 __proto__
: WebInspector
.VBox
.prototype