comparison booket.js @ 19:4a4d9527c06f

Add merge options Allow to merge loaded or imported bookmarks with the existing ones
author Guido Berhoerster <guido+booket@berhoerster.name>
date Thu, 02 Oct 2014 09:08:22 +0200
parents 3642bb668af1
children b19db583b5f8
comparison
equal deleted inserted replaced
18:3642bb668af1 19:4a4d9527c06f
703 this._filterTags, this._searchTerm); 703 this._filterTags, this._searchTerm);
704 } 704 }
705 }; 705 };
706 706
707 BookmarkModel.prototype.parseLoadedBookmarks = function (data) { 707 BookmarkModel.prototype.parseLoadedBookmarks = function (data) {
708 var wasEmpty = !this._bookmarks.size;
708 var parsedData; 709 var parsedData;
709 var bookmarks = []; 710 var bookmarks = [];
711 var bookmark;
712 var oldBookmark;
710 713
711 try { 714 try {
712 parsedData = JSON.parse(data); 715 parsedData = JSON.parse(data);
713 } catch (e) { 716 } catch (e) {
714 this.notify('load-file-error', e.message); 717 this.notify('load-file-error', e.message);
722 } 725 }
723 726
724 // create a temporary list of valid bookmarks 727 // create a temporary list of valid bookmarks
725 parsedData.bookmarks.forEach(function (bookmark) { 728 parsedData.bookmarks.forEach(function (bookmark) {
726 if (isString(bookmark.url) && bookmark.url !== '') { 729 if (isString(bookmark.url) && bookmark.url !== '') {
727 bookmarks.push(new Bookmark(bookmark.url, bookmark.title, 730 bookmark = new Bookmark(bookmark.url, bookmark.title,
728 bookmark.favicon, bookmark.tags, bookmark.ctime, 731 bookmark.favicon, bookmark.tags, bookmark.ctime, bookmark.mtime)
729 bookmark.mtime)); 732 oldBookmark = this.get(bookmark.url);
733 if (oldBookmark === undefined ||
734 oldBookmark.mtime < bookmark.mtime) {
735 bookmarks.push(bookmark);
736 }
730 } 737 }
731 }, this); 738 }, this);
732 739
733 // add each bookmark to the model ordered by the last modification time 740 // add each bookmark to the model ordered by the last modification time
734 this.add(bookmarks.sort(function (bookmark1, bookmark2) { 741 this.add(bookmarks.sort(function (bookmark1, bookmark2) {
735 return bookmark1.ctime - bookmark2.ctime; 742 return bookmark1.ctime - bookmark2.ctime;
736 })); 743 }));
737 this.unsavedChanges = false; 744 if (wasEmpty) {
745 // if there were no bookmarks before there cannot be any unsaved changes
746 this.unsavedChanges = false;
747 }
738 }; 748 };
739 749
740 BookmarkModel.prototype.parseImportedBookmarks = function (data) { 750 BookmarkModel.prototype.parseImportedBookmarks = function (data) {
751 var wasEmpty = (this._bookmarks.size > 0);
741 var bookmarkDoc; 752 var bookmarkDoc;
742 var bookmarkElements; 753 var bookmarkElements;
743 var i; 754 var i;
744 var url; 755 var url;
745 var title; 756 var title;
746 var favicon; 757 var favicon;
747 var tags; 758 var tags;
748 var ctime; 759 var ctime;
749 var mtime; 760 var mtime;
750 var bookmarks = []; 761 var bookmarks = [];
762 var bookmark;
763 var oldBookmark;
751 764
752 bookmarkDoc = document.implementation.createHTMLDocument(); 765 bookmarkDoc = document.implementation.createHTMLDocument();
753 bookmarkDoc.open(); 766 bookmarkDoc.open();
754 bookmarkDoc.write(data); 767 bookmarkDoc.write(data);
755 bookmarkDoc.close(); 768 bookmarkDoc.close();
767 parseInt(bookmarkElements[i].getAttribute('add_date'), 10)) ? 780 parseInt(bookmarkElements[i].getAttribute('add_date'), 10)) ?
768 ctime * 1000 : undefined; 781 ctime * 1000 : undefined;
769 mtime = !isNaN(mtime = 782 mtime = !isNaN(mtime =
770 parseInt(bookmarkElements[i].getAttribute('last_modified'), 783 parseInt(bookmarkElements[i].getAttribute('last_modified'),
771 10)) ? mtime * 1000 : undefined; 784 10)) ? mtime * 1000 : undefined;
772 bookmarks.push(new Bookmark(url, title, favicon, tags, ctime, mtime)); 785 bookmark = new Bookmark(url, title, favicon, tags, ctime, mtime);
786 oldBookmark = this.get(bookmark.url);
787 if (oldBookmark === undefined ||
788 oldBookmark.mtime < bookmark.mtime) {
789 bookmarks.push(bookmark);
790 }
773 } 791 }
774 } 792 }
775 793
776 // add each bookmark to the model ordered by the last modification time 794 // add each bookmark to the model ordered by the last modification time
777 this.add(bookmarks.sort(function (bookmark1, bookmark2) { 795 this.add(bookmarks.sort(function (bookmark1, bookmark2) {
778 return bookmark1.ctime - bookmark2.ctime; 796 return bookmark1.ctime - bookmark2.ctime;
779 })); 797 }));
780 798 if (!wasEmpty) {
781 this.unsavedChanges = false; 799 // if there were no bookmarks before there cannot be any unsaved changes
782 }; 800 this.unsavedChanges = false;
783 801 }
784 BookmarkModel.prototype.loadFile = function (bookmarkFile) { 802 };
785 // delete all existing bookmarks first 803
786 this.delete(this._bookmarks.keys()); 804 BookmarkModel.prototype.loadFile = function (bookmarkFile, merge) {
787 this.unsavedChanges = false; 805 if (!merge) {
806 // delete all existing bookmarks first
807 this.delete(this._bookmarks.keys());
808 this.unsavedChanges = false;
809 }
788 810
789 this.loadFileReader = new FileReader(); 811 this.loadFileReader = new FileReader();
790 this.loadFileReader.addEventListener('error', this); 812 this.loadFileReader.addEventListener('error', this);
791 this.loadFileReader.addEventListener('load', this); 813 this.loadFileReader.addEventListener('load', this);
792 this.loadFileReader.readAsText(bookmarkFile); 814 this.loadFileReader.readAsText(bookmarkFile);
793 }; 815 };
794 816
795 BookmarkModel.prototype.importFile = function (bookmarkFile) { 817 BookmarkModel.prototype.importFile = function (bookmarkFile, merge) {
796 // delete all existing bookmarks first 818 if (!merge) {
797 this.delete(this._bookmarks.keys()); 819 // delete all existing bookmarks first
798 this.unsavedChanges = false; 820 this.delete(this._bookmarks.keys());
821 this.unsavedChanges = false;
822 }
799 823
800 this.importFileReader = new FileReader(); 824 this.importFileReader = new FileReader();
801 this.importFileReader.addEventListener('error', this); 825 this.importFileReader.addEventListener('error', this);
802 this.importFileReader.addEventListener('load', this); 826 this.importFileReader.addEventListener('load', this);
803 this.importFileReader.readAsText(bookmarkFile); 827 this.importFileReader.readAsText(bookmarkFile);
1181 this.notify('save-file'); 1205 this.notify('save-file');
1182 } else if (e.target.id === 'load-form') { 1206 } else if (e.target.id === 'load-form') {
1183 e.preventDefault(); 1207 e.preventDefault();
1184 e.target.blur(); 1208 e.target.blur();
1185 1209
1186 this.notify('load-file', e.target.file.files[0]); 1210 this.notify('load-file', e.target.file.files[0],
1211 e.target.merge.checked);
1187 e.target.reset(); 1212 e.target.reset();
1188 } else if (e.target.id === 'import-form') { 1213 } else if (e.target.id === 'import-form') {
1189 e.preventDefault(); 1214 e.preventDefault();
1190 e.target.blur(); 1215 e.target.blur();
1191 1216
1192 this.notify('import-file', e.target.file.files[0]); 1217 this.notify('import-file', e.target.file.files[0],
1218 e.target.merge.checked);
1193 e.target.reset(); 1219 e.target.reset();
1194 } else if (e.target.id === 'export-form') { 1220 } else if (e.target.id === 'export-form') {
1195 e.preventDefault(); 1221 e.preventDefault();
1196 e.target.blur(); 1222 e.target.blur();
1197 1223
1729 hashData.set('tags', newFilterTags.values().join(',')); 1755 hashData.set('tags', newFilterTags.values().join(','));
1730 hashData.set('search', newSearchTerm); 1756 hashData.set('search', newSearchTerm);
1731 history.pushState(null, null, serializeHash(url, hashData)); 1757 history.pushState(null, null, serializeHash(url, hashData));
1732 }; 1758 };
1733 1759
1734 BooketController.prototype.onLoadFile = function (bookmarkFile) { 1760 BooketController.prototype.onLoadFile = function (bookmarkFile, merge) {
1735 if (this.bookmarkModel.unsavedChanges) { 1761 if (this.bookmarkModel.unsavedChanges) {
1736 if (!this.actionsView.confirmLoadFile()) { 1762 if (!this.actionsView.confirmLoadFile()) {
1737 return; 1763 return;
1738 } 1764 }
1739 this.bookmarkModel.unsavedChanges = false; 1765 this.bookmarkModel.unsavedChanges = false;
1740 } 1766 }
1741 1767
1742 this.bookmarkModel.loadFile(bookmarkFile); 1768 this.bookmarkModel.loadFile(bookmarkFile, merge);
1743 }; 1769 };
1744 1770
1745 BooketController.prototype.onImportFile = function (bookmarkFile) { 1771 BooketController.prototype.onImportFile = function (bookmarkFile, merge) {
1746 if (this.bookmarkModel.unsavedChanges) { 1772 if (this.bookmarkModel.unsavedChanges) {
1747 if (!this.actionsView.confirmLoadFile()) { 1773 if (!this.actionsView.confirmLoadFile()) {
1748 return; 1774 return;
1749 } 1775 }
1750 this.bookmarkModel.unsavedChanges = false; 1776 this.bookmarkModel.unsavedChanges = false;
1751 } 1777 }
1752 1778
1753 this.bookmarkModel.importFile(bookmarkFile); 1779 this.bookmarkModel.importFile(bookmarkFile, merge);
1754 }; 1780 };
1755 1781
1756 BooketController.prototype.onEditBookmark = function (bookmarkUrl) { 1782 BooketController.prototype.onEditBookmark = function (bookmarkUrl) {
1757 this.bookmarkView.displayBookmarkEditor( 1783 this.bookmarkView.displayBookmarkEditor(
1758 this.bookmarkModel.get(bookmarkUrl)); 1784 this.bookmarkModel.get(bookmarkUrl));