This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
224 lines (191 loc) · 7.89 KB
/
index.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!doctype html>
<!--
@license
Copyright (c) 2015 Khaos-Coders. All rights reserved.
This code may only be used under the BSD style license found at http://khaoscoders.github.io/LICENSE.txt
The complete set of authors may be found at http://khaoscoders.github.io/AUTHORS.txt
The complete set of contributors may be found at http://khaoscoders.github.io/CONTRIBUTORS.txt
Code distributed by Khaos-Coders as part of this project is also
subject to an additional IP rights grant found at http://khaoscoders.github.io/PATENTS.txt
-->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>kc-websql Demo</title>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../kc-websql.html">
<link rel="import" href="../kc-websql-select.html">
<link rel="import" href="../kc-websql-insert.html">
<link rel="import" href="../kc-websql-delete.html">
<link rel="import" href="../kc-websql-update.html">
<link rel="import" href="../kc-websql-unsupported.html">
<link rel="import" href="../../paper-input/paper-input.html">
<link rel="import" href="../../paper-button/paper-button.html">
<link rel="stylesheet" href="../../paper-styles/paper-styles.html">
<link rel="import" href="../../paper-styles/demo-pages.html">
<link rel="import" href="../../paper-item/paper-item.html">
<link rel="import" href="../../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../../iron-icons/iron-icons.html">
<link rel="import" href="../../paper-dialog/paper-dialog.html">
<link rel="import" href="../../paper-dialog/paper-dialog.html">
<link rel="import" href="../../array-composer/array-composer.html">
<link rel="import" href="../../kc-timer/kc-timer.html">
<style>
body {
padding: 40px;
}
paper-button.colorful {
color: #4285f4;
}
paper-button[raised].colorful {
background: #4285f4;
color: #fff;
}
div.baseline {
padding-top:4px;
}
div.footer {
position: absolute;
bottom: 0;
left: 0;
height: 30px;
font-size:1.2em;
padding: 4px 0px 0px 40px;
background-color:white;
width:100%;
border-top: 1px solid gray;
}
</style>
</head>
<body>
<!-- Enables polymer data-binding in main page. Yeay!! -->
<template is="dom-bind" id="dom-bind">
<div class="vertical layout">
<H4>An example of <code><kc-websql></code>:</H4>
<div>
This page stores everything you type in below in a local database. <br/>
<ol>
<li>type something below</li>
<li>hit the 'SAVE' button or the return key</li>
<li>close this page</li>
<li><b>then visit it again!</b></li>
<li>fall from chair - please don't hurt yourself. <i>(optional)</i> </li>
<ol>
</div>
<kc-websql-unsupported on-show="showDialog">
<paper-dialog modal id="unsupported-dialog">
<h2> Ooops... </h2>
<div>
WebSQL is not supported by this browser.
Try your luck with another one.
For example the latest version of Google Chrome or Safari.
</div>
</paper-dialog>
</kc-websql-unsupported>
<kc-websql database="demo-db" displayname="Demo-Database for kc-websql" size="52428800" version="1.0">
<kc-websql-table name="texts" version="1.0" primary-key="nr ASC">
<!-- SQLite will make `nr` an alias for `rowid` because it is an INTEGER and PRIMARY KEY -->
<kc-websql-fields>nr INTEGER, text TEXT, ts INTEGER</kc-websql-fields>
<kc-websql-index name="texts_ts">ts DESC</kc-websql-index>
</kc-websql-table>
<!--
Place database actions inside kc-websql to hide them in local dom.
But you don't have to ;)
-->
<kc-websql-select fields="text, ts, nr" from="texts" order="ts DESC" result="{{data}}" auto></kc-websql-select>
<!-- kc-timer: used to get timestamp -->
<kc-timer timestamp="{{ts}}" output="ts" active fire-on-activate></kc-timer>
<!-- array-composer: used to merge fields to array -->
<array-composer length="2" array="{{insertArgs}}" f0="[[inputText]]" f1="[[ts]]" update-on="f0"></array-composer>
<kc-websql-insert fields="text,ts" into="texts" params="[[insertArgs]]" on-finished="sqlFinished"></kc-websql-insert>
<kc-websql-delete from="texts" where="nr=?" on-finished="sqlFinished"></kc-websql-delete>
<array-composer length="2" array="{{updateArgs}}" f0="[[ts]]" f1="[[updateNr]]" update-on="f1"></array-composer>
<kc-websql-update table="texts" fields="ts=?" where="nr=?" params="[[updateArgs]]" on-finished="sqlFinished"></kc-websql-update>
</kc-websql>
<div>
<paper-input-container>
<label>Your text</label>
<input is="iron-input" id="text-input" bind-value="{{inputText}}" value="{{value::input}}">
</paper-input-container>
<paper-button onclick="save()" class="colorful">Save</paper-button>
</div>
<div>
<template is="dom-if" if="{{data.length}}">
<paper-item>
<paper-item-body>
<pre>Go ahead and change the order or delete something!</pre>
</paper-item-body>
<paper-item>
</template>
<h4>What you saved up to now</h4>
<template is="dom-repeat" items="{{data}}">
<div kc-texts-nr="{{item.nr}}">
<span>
<paper-icon-button icon="delete" kcnr="{{item.nr}}" onclick="deleteText(this.kcnr)"><paper-icon-button>
</span>
<span>
<paper-icon-button icon="arrow-drop-up" kcnr="{{item.nr}}" onclick="moveUp(this.kcnr)"><paper-icon-button>
</span>
<span>{{formatTimestamp(item.ts)}}</span>: <span>{{item.text}}</span>
</div>
</template>
<div class="baseline">
<span>{{data.length}}</span> - Records
</div>
</div>
</div>
<div class="footer">
By the way: This demo uses two other PolymerElements published by KC:
<a href="https://github.com/KhaosCoders/array-composer" target="_blank">array-composer</a> &
<a href="https://github.com/KhaosCoders/kc-timer" target="_blank">kc-timer</a>.
Go and have a look!
</div>
<script>
function save() {
// Insert new text, parameters are set via data-bindings
Polymer.dom(document).querySelector('kc-websql-insert').execute();
var input = document.getElementById('text-input');
input.value='';
input.focus();
}
function deleteText(nr) {
// delete text by number index
Polymer.dom(document).querySelector('kc-websql-delete').execute(nr);
}
function moveUp(nr) {
// move text up
Polymer.dom(document).querySelector("#dom-bind").updateNr=nr;
Polymer.dom(document).querySelector('kc-websql-update').execute();
}
function padLeft(nr, n, str){
return Array(n-String(nr).length+1).join(str||'0')+nr;
}
// Define polymer event handlers & formatter for data-bindings
(function() {
var domBind = document.getElementById('dom-bind');
// sqlFinished event handler
domBind.sqlFinished = function () {
// execute select
Polymer.dom(document).querySelector('kc-websql-select').execute();
};
// web sql not supported event handler
domBind.showDialog = function(s) {
Polymer.dom(document).querySelector("#unsupported-dialog").open();
};
domBind.formatTimestamp = function(ts) {
var date = new Date(ts);
return (1900+date.getYear())+'/'+padLeft(date.getDate(),2,'0')+'/'+padLeft(date.getMonth(),2,'0')+' '
+ padLeft(date.getHours(),2,'0')+':'+padLeft(date.getMinutes(),2,'0')+':'+padLeft(date.getSeconds(),2,'0');
};
// Save via return key
document.getElementById('text-input').addEventListener('change', function(e) {
if (document.activeElement == this) {
save();
}
});
})();
</script>
</template>
</body>
</html>