-
Notifications
You must be signed in to change notification settings - Fork 0
/
notebook.js
76 lines (69 loc) · 1.5 KB
/
notebook.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
var notes;
var notebook_sorted = false;
var notebook_changed = false;
var dbtag = 'notebookdb';
var defaultdb = [
{
title: 'Welcome',
body: 'Thank you for using the OpenStudy Notebook extension.'
}
];
function createNote(title, body) {
notes.push({
title: title,
body: body
});
return notes.length - 1;
}
function saveNotebook() {
localStorage[dbtag] = JSON.stringify(notes);
}
function loadNotebook() {
var data = localStorage[dbtag];
if (data) {
notes = JSON.parse(data).sort(compareNote);
} else {
notes = defaultdb;
}
}
function importNotebook(data) {
try {
var list = JSON.parse(data);
if (validNotebook(list)) {
notes = list;
notebook_sorted = false;
$('#notebook-search').trigger('keyup');
} else {
alert('Imported library was invalid.');
}
} catch(e) {
alert('Failed to parse JSON.');
}
}
function validNotebook(list) {
if (!(list instanceof Array)) {
return false;
}
for (var i = 0; i < list.length; i++) {
if (!list[i].hasOwnProperty('title') || !list[i].hasOwnProperty('body')) {
return false;
}
}
return true;
}
function compareNote(note1, note2) {
var cmp = note1.title.localeCompare(note2.title);
if (cmp == 0) {
cmp = note1.body.localeCompare(note2.body);
}
return cmp;
}
function matchingNote(note, data) {
if (note.title.toLowerCase().search(data) != -1) {
return true;
}
if (note.body.toLowerCase().search(data) != -1) {
return true;
}
return false;
}