diff --git a/js/directives/_module.js b/js/directives/_module.js index 855fcd9..5d4e383 100644 --- a/js/directives/_module.js +++ b/js/directives/_module.js @@ -1,9 +1,10 @@ define( [ 'angular', -'./updateBackground' +'./updateBackground', +'./exportBookmark' ], -function(angular, updateBackgroundFactory) { 'use strict'; +function(angular, updateBackgroundFactory, exportBookmarkFactory) { 'use strict'; // Creates `dewey.directives` module var module = angular.module('dewey.directives', []); @@ -11,4 +12,6 @@ var module = angular.module('dewey.directives', []); // Register update background directive module.directive('myUpdateBackground', updateBackgroundFactory); +// Register export bookmark directive +module.directive('exportBookmark', exportBookmarkFactory); }); \ No newline at end of file diff --git a/js/directives/exportBookmark.js b/js/directives/exportBookmark.js new file mode 100644 index 0000000..a64b2ef --- /dev/null +++ b/js/directives/exportBookmark.js @@ -0,0 +1,37 @@ +define( +[ + 'underscore' +], +function(_) { 'use strict'; + +var exportBookmarkFactory = function(bookmarksStorage, exporter) { + return { + restrict: 'E', + replace: true, + scope: {}, + templateUrl: '/partials/exportBookmark.tpl.html', + controller: function($scope, $element){ + + $scope.export = function(){ + bookmarksStorage.getAll(function(bookmarks, setttings) { + + var fileData = exporter.exportToNetscape(bookmarks); + var blob = new Blob([fileData], { type:"text/html;charset=utf-8;" }); + + var downloadLink = angular.element(''); + downloadLink.attr('href',window.URL.createObjectURL(blob)); + downloadLink.attr('download', 'deweyapp.html'); + downloadLink[0].click(); + }); + }; + } + }; +}; + +return [ + 'bookmarksStorage', + 'exporter', + exportBookmarkFactory +]; + +}); diff --git a/js/services/_module.js b/js/services/_module.js index 5fd3cc9..529720d 100644 --- a/js/services/_module.js +++ b/js/services/_module.js @@ -3,9 +3,10 @@ define( 'angular', './bookmarksStorage', './booleanSearchEngine', -'./settings' +'./settings', +'./exporter' ], -function(angular, bookmarksStorage, booleanSearchEngine, settings) { 'use strict'; +function(angular, bookmarksStorage, booleanSearchEngine, settings, exporter) { 'use strict'; // Creates new module 'dewey.filters' var module = angular.module('dewey.services', []); @@ -19,4 +20,7 @@ module.factory('booleanSearchEngine', booleanSearchEngine); // Register settings service module.value('appSettings', settings); +// Register export to Netscape service +module.factory('exporter', exporter); + }); \ No newline at end of file diff --git a/js/services/booleanSearchEngine.js b/js/services/booleanSearchEngine.js index 29af2d3..20e4bc9 100644 --- a/js/services/booleanSearchEngine.js +++ b/js/services/booleanSearchEngine.js @@ -18,7 +18,7 @@ var BooleanSearchEngine = function () { var patterns = ['TAG:', 'URL:', 'TITLE:']; - // Trims defined characters from begining and ending of the string. Defaults to whitespace characters. + // Trims defined characters from beginning and ending of the string. Defaults to whitespace characters. var trim = function(input, characters){ if (!_.isString(input)) return input; diff --git a/js/services/exporter.js b/js/services/exporter.js new file mode 100644 index 0000000..08c5fed --- /dev/null +++ b/js/services/exporter.js @@ -0,0 +1,68 @@ +define( +[ + 'underscore' +], +function(_) { "use strict"; + +/* +* Bookmark exporter service. +*/ +var Exporter = function () { + + var header = function(){ + return ''+ + ''+ + ''+ + '
'; + }; + + var footer = function(){ + return '
'; + }; + + var exportBookmark = function(bookmark){ + + var tags = ''; + if(bookmark.tag.length > 0) { + tags = _.pluck(bookmark.tag, 'text').join(','); + } + return '
+ Export +
+Screenshots by Page2Images
'; + + var result = engine.exportToNetscape(null); + expect(result).to.equal(expected); + }); + + it('When bookmarks collection is empty - result should contains only header and footer', function(){ + + var expected = '
'; + + var result = engine.exportToNetscape(null); + expect(result).to.equal(expected); + }); + + describe('When bookmarks collection is not empty', function(){ + + it('and NO tags - result should contains two bookmarks', function(){ + + var expected = '
'; + + var result = engine.exportToNetscape([ + { + date: 1394679060712, + id: '7', + title: 'Underscore.js', + url: 'http://underscorejs.org/', + tag: [] + }, + { + date: 1396299213543, + id: '20', + title: 'GitHub', + url: 'https://github.com/', + tag: [] + } + ]); + expect(result).to.equal(expected); + }); + + it('and NO tags - result should contains two bookmarks', function(){ + + var expected = '
'; + + var result = engine.exportToNetscape([ + { + date: 1394679060712, + id: '7', + title: 'Underscore.js', + url: 'http://underscorejs.org/', + tag: [ + { + custom: false, + text: 'Other Bookmarks' + } + ] + }, + { + date: 1396299213543, + id: '20', + title: 'GitHub', + url: 'https://github.com/', + tag: [ + { + custom: false, + text: 'Bookmarks Bar' + }, + { + custom: false, + text: 'prog' + } + ] + } + ]); + expect(result).to.equal(expected); + }); + }); + }); +}); + +}); \ No newline at end of file