-
Notifications
You must be signed in to change notification settings - Fork 0
/
webwear-history.js
106 lines (83 loc) · 2.53 KB
/
webwear-history.js
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
$(document).ready(loadHistory);
function loadHistory(){
var getUrl = WebWearData+'/_find?batch_size=10000&sort={"startTime":-1}';
var data = {"criteria": JSON.stringify({user: localStorage['username']})};
$.ajax(
{
data: data,
type: 'GET',
url: getUrl,
success: displayHistory,
async:true,
}
);
}
function displayHistory(data){
if (data && 'ok' in data && data.ok == 1){
var h = $('#history');
h.empty();
h.append('<input type="button" id="delete" value="delete selected" /> <br /><br />');
h.append('<input type="checkbox" id="selectall"> <small>select all</small><br />');
$.each(data.results, function(i,r){
var text = '<span><input type="checkbox" id=\''+JSON.stringify(r._id)+'\' class="item" />';
//get date
var d = new Date(r.startTime);
d = d.toLocaleDateString() + ' - '+d.toLocaleTimeString();
text+='<small>'+d+'</small> -- <a href="'+r.rawUrl+'" target="_blank">'+r.title+'</a><br /></span>';
var t = $(text)
t.data('search',r.search);
h.append(t);
//h.find('#'+JSON.stringify(r._id)).data('search',r.search);
});
h.find('#selectall').click(function(){
if ($(this).prop('checked')){
$('.item').prop('checked',true);
}
else{
$('.item').prop('checked',false);
}
});
h.find('#delete').click(function(){
var selected = [];
var selected_search = [];
$('.item').each(function(j,s){
if ($(s).prop('checked')){
selected.push(JSON.parse(s.id));
selected_search.push(decodeURIComponent($(s).parent().data('search')).replace('+', ' '));
}
});
if (selected.length > 0){
var answer = confirm('Are you sure you want to permanently delete the selected items?\n\nPress OK to continue, Cancel to abort.');
if (answer){
var getUrl = WebWearData+'/_remove';
var searchUrl = WebWearData+'search/_remove';
var data = {"criteria": JSON.stringify({_id: {$in:selected}})};
var searchData = {"criteria": JSON.stringify({search: {$in:selected_search}})};
$.ajax(
{
data: searchData,
type: 'POST',
url: searchUrl,
async:true
}
);
$.ajax(
{
data: data,
type: 'POST',
url: getUrl,
success:loadHistory,
async:true,
}
);
}
}
else{
alert('You haven\'t selected anything to delete.');
}
});
}
else{
alert('error getting your history');
}
}