-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemsPage.js
More file actions
345 lines (303 loc) · 8.14 KB
/
itemsPage.js
File metadata and controls
345 lines (303 loc) · 8.14 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { gPhotoTimeAPI } from './api/photoTimeApi.js';
import { appstate } from './appstate.js'
import { app } from './app.js'
var itemsPage = {};
/**
*
*/
itemsPage.loadItems = function()
{
var applocation = appstate.getLocation();
var group = applocation.item;
if (group.type == "file") {
group = applocation.parents.pop();
applocation.item = group;
}
var onGroupItemsAvail = function(item, items)
{
applocation.parents.push(group);
applocation.item = item;
item.items = items;
// don't change the page, just re-render?
// but then must handle back within the page..
itemsPage.loadItems();
};
renderGroup('groupItemsList', group, {
onSelect: function(idx)
{
console.log('group item selected idx: ' + idx);
var item = group.items[idx];
console.log(`item.type=${item.type}. id=${item.id}`);
group.selectedItemIdx = idx;
if (item.type == "file")
{
applocation.parents.push(group);
applocation.item = item;
$.mobile.changePage('itemView.html#itemView');
} else {
// check if already have items
if (item.items) {
onGroupItemsAvail(item, item.items);
} else {
var gConx = gPhotoTimeAPI.getConnection();
gConx.getItems(item.id, {
onSuccess: function(data) {
var jData = data;
onGroupItemsAvail(item, jData.items);
}
});
}
}
}
});
};
function renderEmptyFolderGroup(lvid, listView, group)
{
var deleteFolderHtml =
`<a href="#"
class="deleteItem"
id="deleteItem-"
data-role="button"
data-icon="delete"
data-photoid="${group.id}">Delete
</a>`;
var emptyFolderHtml =
`<li >
<a href="#">
<div>This folder is empty:</div>
<div>${group.id}</div>
</a>
${deleteFolderHtml}
</li>`;
listView.html(emptyFolderHtml);
$('.deleteItem').click(function(event) {
$.mobile.loading('show');
var photoid = this.getAttribute('data-photoid');
var gConx = gPhotoTimeAPI.getConnection();
var applocation = appstate.getLocation();
gConx.delItem(photoid, {
onSuccess: function(data) {
var parent = applocation.parents[applocation.parents.length-1];
app.removeItemFromParentById(photoid);
listView.empty();
$.mobile.loading('hide');
// go back?
app.goBack();
//loadGroup(lvid, parent, cbks);
// remove item from parent, nav to parent
/*
var test = group.items[idx];
if (test.id == photoid)
{
group.items.splice(idx, 1);
//var item2 = $("#mylist").find("li:contains('item2')");
//item2.remove();
//$('#' + lvid).listview('refresh');
listView.empty();
loadGroup(lvid, group, cbks);
}*/
},
onError: function() {
console.log('error');
}
});
});
$('a[data-rel="custom-back"]').unbind('click');
$('a[data-rel="custom-back"]').click(function(event) {
event.preventDefault();
$.mobile.loading('show');
var applocation = appstate.getLocation();
if (applocation.item.type=='folder' && applocation.parents.length > 0) {
// if have parents and parent is a folder
// than just change state and refresh
// otherwise issue go back
if (applocation.parents[applocation.parents.length-1].type == 'folder') {
app.doBack();
itemsPage.loadItems();
} else {
app.goBack();
}
}
$.mobile.loading('hide');
return false;
});
$('#' + lvid).listview('refresh');
$.mobile.loading('hide');
return;
}
function renderGroup(lvid, group, cbks)
{
$.mobile.loading('show')
var itemClass = 'item';
var listView = $('#' + lvid);
var items = group.items;
if (items.length == 0) {
renderEmptyFolderGroup(lvid, listView, group);
return;
}
//
var tmpl = `<a href="#" style="display:flex" class="__class__" id="${itemClass}-__idx__">
<div class="item-thumb">
<img class="item-thumb" src="__thumb__" />
</div>
<div class="item-label">__label__</div>
</a>`;
var canDelete = true;
if (canDelete)
{
tmpl +=
`<a href="#"
class="deleteItem"
id="deleteItem-__idx__"
data-role="button"
data-icon="delete"
data-photoid="__id__">
Delete
</a>`;
}
var i;
var listItemsHtml = '';
var selectedIdx = group.selectedItemIdx;
var gConx = gPhotoTimeAPI.getConnection();
// TODO: need to "page" through these thumbs as loading
// when directory has a lot of images takes a long time
// or ... lazy load more, TBD
// maybe set timeout to load more? would that help
//var maxLoadItems = Math.min(100, items.length);
const maxLoadItems = items.length;
//listItemsHtml = createListItems(0, maxLoadItems, items, tmpl, gConx, itemClass)
for (i = 0; i < maxLoadItems; ++i) {
let item = items[i];
let h = tmpl.replace(/__idx__/g, i);
h = h.replace(/__id__/g, item.id);
h = h.replace(/__thumb__/g, gConx.getThumbUrl(item.thumb));
h = h.replace(/__label__/g, item.label);
if (i == selectedIdx) {
h = h.replace(/__class__/g, 'selectedItem ' + itemClass);
} else {
h = h.replace(/__class__/g, itemClass);
}
let liHtml = `<li id="list-${itemClass}-${i}">${h}</li>`;
listItemsHtml += liHtml;
}
/*
if (maxLoadItems < items.length) {
setTimeout(() => {
// load more..?
let listView = $(`#${lvid}`);
}, 100);
}
*/
listView.html(listItemsHtml);
$('.' + itemClass).unbind('click');
$('.' + itemClass).click(function(event) {
if (cbks.onSelect)
{
var idx;
idx = this.id.split('-')[1]
cbks.onSelect(idx);
}
});
$('a[data-rel="custom-back"]').unbind('click');
$('a[data-rel="custom-back"]').click(function(event) {
event.preventDefault();
$.mobile.loading('show');
var applocation = appstate.getLocation();
if (applocation.item.type=='folder' && applocation.parents.length > 0) {
// if have parents and parent is a folder
// than just change state and refresh
// otherwise issue go back
if (applocation.parents[applocation.parents.length-1].type == 'folder') {
app.doBack();
itemsPage.loadItems();
} else {
app.goBack();
}
}
$.mobile.loading('hide');
return false;
});
$('.deleteItem').unbind('click');
$('.deleteItem').click(function(event) {
$.mobile.loading('show');
var idx;
idx = this.id.split('-')[1]
//console.log('clicked delete for idx:' + idx);
//console.log(this.getAttribute('data-photoid'));
var photoid = this.getAttribute('data-photoid');
//console.log('TODO: del for photo id - ' + photoid);
//let gConx = gPhotoTimeAPI.getConnection();
gConx.delItem(photoid, {
onSuccess: function(data) {
$.mobile.loading('hide');
//console.log('ok');
//console.log(data);
var test = group.items[idx];
if (test.id == photoid)
{
group.items.splice(idx, 1);
//var item2 = $("#mylist").find("li:contains('item2')");
//item2.remove();
//$('#' + lvid).listview('refresh');
listView.empty();
loadGroup(lvid, group, cbks);
}
},
onError: function() {
console.log('error');
}
});
});
$('#' + lvid).listview('refresh');
$.mobile.loading('hide');
}
class Poller
{
constructor(method, periodMillis) {
this.method = method;
this.periodMillis = periodMillis?periodMillis:30000;
this.boundFn = function() {
this.onTime();
}.bind(this);
setTimeout(this.boundFn, periodMillis);
}
onTime() {
this.method(this);
setTimeout(this.boundFn, this.periodMillis);
}
getPeriodMillis() {
return this.periodMillis;
}
setPeriodMillis(newPeriodMillis) {
this.periodMillis = newPeriodMillis;
}
}
function statusCheck(poller) {
// call server to check thumb status
var gConx = gPhotoTimeAPI.getConnection();
if (!gConx) {
return;
}
gConx.checkStatus().then((data) => {
if (data.activeThumbTasks.length) {
$('#items-page-status-text').html("thumb status: working on it, thumbs remaining: " + data.activeThumbTasks.length);
poller.setPeriodMillis(3000);
} else {
$('#items-page-status-text').html("thumb status: all done");
// slow down check
const periodMillis = poller.getPeriodMillis();
if (periodMillis < 30000) {
poller.setPeriodMillis(periodMillis+5000);
}
}
}, (reason) => {
console.warn('failed to check server status');
}).catch( (reason) => {
console.log(reason);
});
}
var gPoller = new Poller(statusCheck, 5000);
$(document).on('pagebeforeshow',"#items", function() {
itemsPage.loadItems();
});
export { itemsPage }