Skip to content

Commit 6ec5c0f

Browse files
committed
Avoid unnecessary reloads
1 parent 74a0223 commit 6ec5c0f

File tree

1 file changed

+27
-62
lines changed

1 file changed

+27
-62
lines changed

js/main.js

+27-62
Original file line numberDiff line numberDiff line change
@@ -360,20 +360,8 @@ $(document).ready(function(){
360360
}
361361
});
362362
db.notes.where("Notebook").equals(notebook).each(function(item, cursor){
363-
var content = item.Content;
364-
if(item.Title != "Untitled"){
365-
try{
366-
content = content.replace("<h1>" + item.Title + "</h1>", "").replace(/(<([^>]+)>)/ig, "");
367-
}catch(e){
368-
console.log(e);
369-
}
370-
}else{
371-
content = content.replace(/(<([^>]+)>)/ig, "");
372-
}
373-
374-
content = content.length >= 40 ? content.substring(0, 40) : content.substring(0, content.length);
375363

376-
$(".note-container").append("<article data-color='" + item.Color + "' draggable='true' data-nid='"+item.id+"'><div class='content' ><h2>" + item.Title + "</h2><span>" + content + "</span></div><div class='note-actions'><span class='fa fa-eye' data-view='" + item.id + "'></span><span class='fa-pencil fa' data-note-id='"+item.id+"' data-action='edit'></span><span class='fa-trash fa' data-note-id='"+item.id+"' data-action='delete'></span></div></article>");
364+
$(".note-container").append("<article data-color='" + item.Color + "' draggable='true' data-nid='"+item.id+"'><div class='content' ><h2>" + item.Title + "</h2></div><div class='note-actions'><span class='fa fa-eye' data-view='" + item.id + "'></span><span class='fa-pencil fa' data-note-id='"+item.id+"' data-action='edit'></span><span class='fa-trash fa' data-note-id='"+item.id+"' data-action='delete'></span></div></article>");
377365

378366
$(".list article").each(function(){
379367
$(this).css("border-color", $(this).data("color"));
@@ -516,22 +504,7 @@ $(document).ready(function(){
516504
db.transaction('r', db.notes, function(){
517505
db.notes.where("Notebook").equals(notebook).each(function(item, cursor){
518506
if(item.Content.toLowerCase().indexOf($(".notebook-nav input").val().toLowerCase()) > -1){
519-
var content = item.Content;
520-
if(item.Title != "Untitled"){
521-
try{
522-
content = content.replace("<h1>" + item.Title + "</h1>", "").replace(/(<([^>]+)>)/ig, "");
523-
}catch(e){
524-
console.log(e);
525-
}
526-
}else{
527-
content = content.replace(/(<([^>]+)>)/ig, "");
528-
}
529-
if(content.length >= 40){
530-
content = content.substring(0, 40);
531-
}else{
532-
content = content.substring(0, content.length);
533-
}
534-
$(".note-container").append("<article data-color='" + item.Color + "' ><div class='content' data-view='" + item.id + "'><h2>" + item.Title + "</h2><span>" + content + "</span></div><div class='note-actions'><span class='fa fa-eye' data-view='" + item.id + "'></span><span class='fa-pencil fa' data-note-id='"+item.id+"' data-action='edit'></span><span class='fa-trash fa' data-note-id='"+item.id+"' data-action='delete'></span></div></article>");
507+
$(".note-container").append("<article data-color='" + item.Color + "' ><div class='content' data-view='" + item.id + "'><h2>" + item.Title + "</h2></div><div class='note-actions'><span class='fa fa-eye' data-view='" + item.id + "'></span><span class='fa-pencil fa' data-note-id='"+item.id+"' data-action='edit'></span><span class='fa-trash fa' data-note-id='"+item.id+"' data-action='delete'></span></div></article>");
535508

536509
$(".list article").each(function(){
537510
$(this).css("border-color", $(this).data("color"));
@@ -627,7 +600,6 @@ $(document).ready(function(){
627600
$(".side-nav").removeClass("active");
628601
$(".view-nav").removeClass("active");
629602
$(".notebook-nav").addClass("active");
630-
loadNotes();
631603
$("nav").show();
632604
$(".note-container").show();
633605
});
@@ -790,6 +762,7 @@ $(document).ready(function(){
790762
db.transaction('rw', db.notes, function(){
791763
var h1 = $("#editor h1").first().text().trim();
792764
h1 = h1 != "" ? h1 : "Untitled";
765+
$("[data-nid='" + id + "'] h2").text(h1);
793766
if(html && h1 && date){
794767
db.notes.where("id").equals(id).modify({Content: html, Title: h1, ModificationDate: date});
795768
}
@@ -972,53 +945,45 @@ $(document).ready(function(){
972945
$(".delete-confirmation .ok").click(function(){
973946
db.transaction('rw', db.notes, function(){
974947
db.notes.where("id").equals(deltempid).delete();
948+
$("[data-nid='" + deltempid + "']").remove();
975949
deltempid = null;
976-
loadNotes();
977950
$(".delete-confirmation").removeClass("active");
978951
});
979952
});
980953

981954
$(".notebook-delete-confirmation .ok").click(function(){
982955
switch($(".notebook-delete-confirmation select").val()){
983956
case "Move":
984-
db.transaction('rw', db.notebooks, function(){
985-
db.notebooks.where("id").equals(parseInt(notebook)).delete().then(function(){
986-
loadNotebooks();
987-
});
957+
db.transaction('rw', db.notebooks, db.notes, function(){
958+
db.notebooks.where("id").equals(parseInt(notebook)).delete();
959+
db.notes.where("Notebook").equals(notebook).modify({Notebook: "Inbox"});
960+
988961
}).then(function(){
989-
db.transaction('rw', db.notes, function(){
990-
db.notes.where("Notebook").equals(notebook).modify({Notebook: "Inbox"}).then(function(){
991-
notebook = "Inbox";
992-
$(".notebook-nav h1").text("Inbox");
993-
$(".notebook-nav small").text("A place for any note");
994-
$("[data-action='edit-notebook']").hide();
995-
$("[data-action='delete-notebook']").hide();
996-
loadNotes();
997-
$(".notebook-delete-confirmation").removeClass("active");
998-
});
999-
});
962+
notebook = "Inbox";
963+
$(".notebook-nav h1").text("Inbox");
964+
$(".notebook-nav small").text("A place for any note");
965+
$("[data-action='edit-notebook']").hide();
966+
$("[data-action='delete-notebook']").hide();
967+
loadNotebooks();
968+
loadNotes();
969+
$(".notebook-delete-confirmation").removeClass("active");
1000970
});
1001971
break;
1002972

1003973
case "Delete":
1004-
db.transaction('rw', db.notebooks, function(){
1005-
db.notebooks.where("id").equals(parseInt(notebook)).delete().then(function(){
1006-
loadNotebooks();
1007-
1008-
});
974+
db.transaction('rw', db.notebooks, db.notes, function(){
975+
db.notebooks.where("id").equals(parseInt(notebook)).delete();
976+
db.notes.where("Notebook").equals(notebook).delete();
1009977

1010978
}).then(function(){
1011-
db.transaction('rw', db.notes, function(){
1012-
db.notes.where("Notebook").equals(notebook).delete().then(function(){
1013-
notebook = "Inbox";
1014-
$(".notebook-nav h1").text("Inbox");
1015-
$(".notebook-nav small").text("A place for any note");
1016-
$("[data-action='edit-notebook']").hide();
1017-
$("[data-action='delete-notebook']").hide();
1018-
loadNotes();
1019-
$(".notebook-delete-confirmation").removeClass("active");
1020-
});
1021-
});
979+
notebook = "Inbox";
980+
$(".notebook-nav h1").text("Inbox");
981+
$(".notebook-nav small").text("A place for any note");
982+
$("[data-action='edit-notebook']").hide();
983+
$("[data-action='delete-notebook']").hide();
984+
loadNotebooks();
985+
loadNotes();
986+
$(".notebook-delete-confirmation").removeClass("active");
1022987
});
1023988
break;
1024989
}

0 commit comments

Comments
 (0)