-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
datasette_write.html
182 lines (167 loc) · 6.01 KB
/
datasette_write.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{% extends "base.html" %}
{% block title %}{% if custom_title %}{{ custom_title }}{% else %}Write to {{ database_name }} with SQL{% endif %}{% endblock %}
{% block extra_head %}
<style>
#query-parameters-area {
max-width: 600px;
}
#query-parameters-area label {
display: inline;
max-width: 10em;
font-weight: bold;
}
#query-parameters-area {
display: grid;
grid-template-columns: auto 1fr;
grid-gap: 10px;
align-items: start;
margin-bottom: 10px;
}
#query-parameters-area input[type="text"],
#query-parameters-area textarea {
width: 100%;
box-sizing: border-box;
}
#query-parameters-area textarea {
height: 20em;
}
.submit-container {
padding-top: 1em;
}
.database-select label {
display: inline;
}
</style>
{% endblock %}
{% block crumbs %}
{{ crumbs.nav(request=request, database=database_name) }}
{% endblock %}
{% block content %}
<h1>{% if custom_title %}{{ custom_title }}{% else %}Write to {{ database_name }} with SQL{% endif %}</h1>
<form class="write-form core" action="{{ request.path }}" method="post" style="margin-bottom: 1em">
<input type="hidden" name="csrftoken" value="{{ csrftoken() }}">
{% if custom_title %}<details style="margin-bottom: 1em"><summary>SQL query</summary>{% endif %}
<p><textarea name="sql" style="box-sizing: border-box; width: 100%; padding-right: 10px; max-width: 600px; height: {{ sql_textarea_height }}em; padding: 6px;">{{ sql_from_args }}</textarea></p>
{% if custom_title %}</details>{% endif %}
<div class="query-parameters">
<div id="query-parameters-area">
{% for parameter in parameters %}
<label for="qp_{{ parameter.name }}">{{ parameter.label }}</label>
{% if parameter.type == "text" %}
<input type="text" id="qp_{{ parameter.name }}" name="qp_{{ parameter.name }}" value="{{ parameter.value }}">
{% elif parameter.type == "hidden" %}
<input type="text" disabled id="qp_{{ parameter.name }}" name="qp_{{ parameter.name }}" value="{{ parameter.value }}">
{% elif parameter.type == "textarea" %}
<textarea id="qp_{{ parameter.name }}" name="qp_{{ parameter.name }}">{{ parameter.value }}</textarea>
{% endif %}
{% endfor %}
</div>
</div>
<div class="submit-container">
<input type="hidden" name="_redirect_to" value="{{ redirect_to }}">
<input type="submit" value="Execute query">
</div>
</form>
{% if tables %}
<p><strong>Tables</strong>:
{% for table in tables %}
<a href="{{ urls.table(database_name, table) }}">{{ table }}</a>{% if not loop.last %}, {% endif %}
{% endfor %}
</p>
{% endif %}
{% if views %}
<p><strong>Views</strong>:
{% for view in views %}
<a href="{{ urls.table(database_name, view) }}">{{ view }}</a>{% if not loop.last %}, {% endif %}
{% endfor %}
</p>
{% endif %}
<script>
function buildQueryParametersHtml(parameters) {
let htmlString = '';
var qsParams = new URLSearchParams(location.search);
parameters.forEach(param => {
const value = escapeHtml(qsParams.get(param.name) || '');
if (param.type === 'text') {
htmlString += `
<label for="qp_${param.name}">${param.label}</label>
<input type="text" id="qp_${param.name}" name="qp_${param.name}" value="${value}">
`;
} else if (param.type === 'textarea') {
htmlString += `
<label for="qp_${param.name}">${param.label}</label>
<textarea id="qp_${param.name}" name="qp_${param.name}">${value}</textarea>
`;
} else if (param.type === 'hidden') {
htmlString += `
<span></span>
<input type="hidden" id="qp_${param.name}" name="qp_${param.name}" value="${value}">
`;
}
});
return htmlString;
}
function escapeHtml(str) {
return str.replace(/[&<>"']/g, function(match) {
switch (match) {
case '&': return '&';
case '<': return '<';
case '>': return '>';
case '"': return '"';
case "'": return ''';
default: return match;
}
});
}
const sqlTextArea = document.querySelector('textarea[name="sql"]');
const queryParametersArea = document.getElementById('query-parameters-area');
let lastRequestTime = 0;
let timeoutId = null;
sqlTextArea.addEventListener('input', () => {
const currentTime = Date.now();
if (currentTime - lastRequestTime > 1000) {
lastRequestTime = currentTime;
updateForm(sqlTextArea.value);
} else {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
lastRequestTime = Date.now();
updateForm(sqlTextArea.value);
}, 1000 - (currentTime - lastRequestTime));
}
});
function updateForm(sqlQuery) {
const apiUrl = `/-/write/derive-parameters?sql=${encodeURIComponent(sqlTextArea.value)}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.parameters.length) {
queryParametersArea.innerHTML = buildQueryParametersHtml(data.parameters);
} else {
queryParametersArea.innerHTML = '';
}
})
.catch(error => console.error('Error fetching API:', error));
}
updateForm();
/* If there is a positive message-info, fade and shrink after 2s */
function fadeAndShrink(element) {
if (element) {
element.style.transition = 'opacity 0.2s, height 0.8s, padding-top 0.8s, padding-bottom 0.8s';
element.style.opacity = '0';
element.style.height = '0';
element.style.paddingTop = '0';
element.style.paddingBottom = '0';
setTimeout(() => {
element.remove();
}, 800);
}
}
const messageInfo = document.querySelector('.message-info');
if (messageInfo) {
setTimeout(() => {
fadeAndShrink(messageInfo);
}, 2000);
}
</script>
{% endblock %}