From 258a46adee99cb054027f2d8072f5b1670b98a64 Mon Sep 17 00:00:00 2001 From: Artem Grygor Date: Wed, 11 Mar 2015 22:36:05 -0400 Subject: [PATCH 1/7] implement export bookmarks to json file from settings window --- js/directives/_module.js | 7 +++++-- js/directives/exportBookmark.js | 36 ++++++++++++++++++++++++++++++++ partials/exportBookmark.tpl.html | 1 + partials/main.tpl.html | 1 + 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 js/directives/exportBookmark.js create mode 100644 partials/exportBookmark.tpl.html 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..6c42deb --- /dev/null +++ b/js/directives/exportBookmark.js @@ -0,0 +1,36 @@ +define( +[ + 'underscore' +], +function(_) { 'use strict'; + +var exportBookmarkFactory = function(bookmarksStorage) { + return { + restrict: 'E', + replace: true, + scope: {}, + templateUrl: '/partials/exportBookmark.tpl.html', + link: function ($scope, $element) { + $element.find('.action-button').on('click', function () { + + bookmarksStorage.getAll(function(bookmarks, setttings) { + + var toJSON = angular.toJson(bookmarks); + var blob = new Blob([toJSON], { type:"application/json;charset=utf-8;" }); + + var downloadLink = angular.element(''); + downloadLink.attr('href',window.URL.createObjectURL(blob)); + downloadLink.attr('download', 'deweyapp.json'); + downloadLink[0].click(); + }); + }); + } + }; +}; + +return [ + 'bookmarksStorage', + exportBookmarkFactory +]; + +}); diff --git a/partials/exportBookmark.tpl.html b/partials/exportBookmark.tpl.html new file mode 100644 index 0000000..c06af48 --- /dev/null +++ b/partials/exportBookmark.tpl.html @@ -0,0 +1 @@ +

Export

\ No newline at end of file diff --git a/partials/main.tpl.html b/partials/main.tpl.html index 4b569a3..5e95688 100644 --- a/partials/main.tpl.html +++ b/partials/main.tpl.html @@ -43,6 +43,7 @@ +

Screenshots by Page2Images

From 247a28aa9e083302168e485eef1a95424e2c5135 Mon Sep 17 00:00:00 2001 From: Artem Grygor Date: Sat, 14 Mar 2015 14:22:43 -0400 Subject: [PATCH 2/7] add exporter service --- js/directives/exportBookmark.js | 7 ++++--- js/services/_module.js | 8 ++++++-- js/services/booleanSearchEngine.js | 2 +- js/services/exporter.js | 31 ++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 js/services/exporter.js diff --git a/js/directives/exportBookmark.js b/js/directives/exportBookmark.js index 6c42deb..6474d97 100644 --- a/js/directives/exportBookmark.js +++ b/js/directives/exportBookmark.js @@ -4,7 +4,7 @@ define( ], function(_) { 'use strict'; -var exportBookmarkFactory = function(bookmarksStorage) { +var exportBookmarkFactory = function(bookmarksStorage, exporter) { return { restrict: 'E', replace: true, @@ -15,8 +15,8 @@ var exportBookmarkFactory = function(bookmarksStorage) { bookmarksStorage.getAll(function(bookmarks, setttings) { - var toJSON = angular.toJson(bookmarks); - var blob = new Blob([toJSON], { type:"application/json;charset=utf-8;" }); + var fileData = exporter.exportToNetscape(bookmarks); + var blob = new Blob([fileData], { type:"application/json;charset=utf-8;" }); var downloadLink = angular.element(''); downloadLink.attr('href',window.URL.createObjectURL(blob)); @@ -30,6 +30,7 @@ var exportBookmarkFactory = function(bookmarksStorage) { 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..f375778 --- /dev/null +++ b/js/services/exporter.js @@ -0,0 +1,31 @@ +define( +[ + 'underscore' +], +function(_) { "use strict"; + +/* +* Bookmark exporter service. +*/ +var Exporter = function () { + + var exTree + + this.exportToNetscape = function(bookmarks){ + + return angular.toJson(bookmarks); + }; +}; + +/* +* Bookmark exporter service factory method. +*/ +var ExporterFactory = function() { + return new Exporter(); +}; + +return [ + ExporterFactory +]; + +}); \ No newline at end of file From 9cd7b39daf549df853afbe0766113e51f366ba41 Mon Sep 17 00:00:00 2001 From: Artem Grygor Date: Sat, 14 Mar 2015 14:26:18 -0400 Subject: [PATCH 3/7] add controller to drct --- js/directives/exportBookmark.js | 8 ++++---- js/services/exporter.js | 4 +--- partials/exportBookmark.tpl.html | 6 +++++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/js/directives/exportBookmark.js b/js/directives/exportBookmark.js index 6474d97..ed00e89 100644 --- a/js/directives/exportBookmark.js +++ b/js/directives/exportBookmark.js @@ -10,9 +10,9 @@ var exportBookmarkFactory = function(bookmarksStorage, exporter) { replace: true, scope: {}, templateUrl: '/partials/exportBookmark.tpl.html', - link: function ($scope, $element) { - $element.find('.action-button').on('click', function () { + controller: function($scope, $element){ + $scope.export = function(){ bookmarksStorage.getAll(function(bookmarks, setttings) { var fileData = exporter.exportToNetscape(bookmarks); @@ -22,8 +22,8 @@ var exportBookmarkFactory = function(bookmarksStorage, exporter) { downloadLink.attr('href',window.URL.createObjectURL(blob)); downloadLink.attr('download', 'deweyapp.json'); downloadLink[0].click(); - }); - }); + }); + }; } }; }; diff --git a/js/services/exporter.js b/js/services/exporter.js index f375778..b16775e 100644 --- a/js/services/exporter.js +++ b/js/services/exporter.js @@ -9,8 +9,6 @@ function(_) { "use strict"; */ var Exporter = function () { - var exTree - this.exportToNetscape = function(bookmarks){ return angular.toJson(bookmarks); @@ -25,7 +23,7 @@ var ExporterFactory = function() { }; return [ - ExporterFactory + ExporterFactory ]; }); \ No newline at end of file diff --git a/partials/exportBookmark.tpl.html b/partials/exportBookmark.tpl.html index c06af48..dc60166 100644 --- a/partials/exportBookmark.tpl.html +++ b/partials/exportBookmark.tpl.html @@ -1 +1,5 @@ - \ No newline at end of file +
+

+ Export +

+
\ No newline at end of file From 121b13bcdf535a0b8071a1e59e8a5caa6c647a47 Mon Sep 17 00:00:00 2001 From: Artem Grygor Date: Sat, 14 Mar 2015 14:57:01 -0400 Subject: [PATCH 4/7] export to Netscape format without custom folders --- js/directives/exportBookmark.js | 4 ++-- js/services/exporter.js | 41 ++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/js/directives/exportBookmark.js b/js/directives/exportBookmark.js index ed00e89..a64b2ef 100644 --- a/js/directives/exportBookmark.js +++ b/js/directives/exportBookmark.js @@ -16,11 +16,11 @@ var exportBookmarkFactory = function(bookmarksStorage, exporter) { bookmarksStorage.getAll(function(bookmarks, setttings) { var fileData = exporter.exportToNetscape(bookmarks); - var blob = new Blob([fileData], { type:"application/json;charset=utf-8;" }); + 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.json'); + downloadLink.attr('download', 'deweyapp.html'); downloadLink[0].click(); }); }; diff --git a/js/services/exporter.js b/js/services/exporter.js index b16775e..fe3c9fe 100644 --- a/js/services/exporter.js +++ b/js/services/exporter.js @@ -9,9 +9,48 @@ function(_) { "use strict"; */ var Exporter = function () { + // this.exportToNetscape = function(bookmarks){ + + // return angular.toJson(bookmarks); + // }; + + var header = function(){ + return ''+ + ''+ + ''+ + 'Bookmarks'+ + '

Bookmarks

'+ + '

'; + }; + + var footer = function(){ + return '

'; + }; + + var exportBookmark = function(bookmark){ + + //

JavaScript for Automation Release Notes + + var tags = _.pluck(bookmark.tag, 'text').join(','); + return '
' + bookmark.title + ''; + }; + this.exportToNetscape = function(bookmarks){ - return angular.toJson(bookmarks); + var fileData = header(); + _.each(bookmarks, function(bookmark){ + fileData += exportBookmark(bookmark); + }); + fileData += footer(); + return fileData; }; }; From feb43915608524488c288ae754d9e7e3015427d8 Mon Sep 17 00:00:00 2001 From: Artem Grygor Date: Sat, 14 Mar 2015 15:24:15 -0400 Subject: [PATCH 5/7] add spec to exporter --- js/directives/exportBookmark.js | 8 +-- js/services/exporter.js | 18 +++-- test/unit/services/exporter.test.js | 106 ++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 test/unit/services/exporter.test.js diff --git a/js/directives/exportBookmark.js b/js/directives/exportBookmark.js index a64b2ef..58ba903 100644 --- a/js/directives/exportBookmark.js +++ b/js/directives/exportBookmark.js @@ -18,10 +18,10 @@ var exportBookmarkFactory = function(bookmarksStorage, exporter) { 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(); + // var downloadLink = angular.element(''); + // downloadLink.attr('href',window.URL.createObjectURL(blob)); + // downloadLink.attr('download', 'deweyapp.html'); + // downloadLink[0].click(); }); }; } diff --git a/js/services/exporter.js b/js/services/exporter.js index fe3c9fe..fc0da1c 100644 --- a/js/services/exporter.js +++ b/js/services/exporter.js @@ -36,7 +36,10 @@ var Exporter = function () { //PRIVATE="0" //TAGS="javascript,mac,osx,yosemite">JavaScript for Automation Release Notes - var tags = _.pluck(bookmark.tag, 'text').join(','); + var tags = ''; + if(bookmark.tag.length > 0) { + tags = _.pluck(bookmark.tag, 'text').join(','); + } return '
0){ + + _.each(bookmarks, function(bookmark){ + fileData += exportBookmark(bookmark); + }); + } + fileData += footer(); return fileData; }; diff --git a/test/unit/services/exporter.test.js b/test/unit/services/exporter.test.js new file mode 100644 index 0000000..0904527 --- /dev/null +++ b/test/unit/services/exporter.test.js @@ -0,0 +1,106 @@ +define( +[ + 'angular', + 'angular-mocks', + './../../../js/services/_module' +], +function(angular, mocks) { + +describe('exporter.test.js', function() { 'use strict'; + + var engine; + beforeEach(mocks.module('dewey.services')); + + beforeEach(inject(function (exporter) { + engine = exporter; + })); + + it('Should have a exportToNetscape function', function(){ + expect(engine).to.not.be.undefined; + expect(engine.exportToNetscape).to.be.a('function'); + }); + + describe('exportToNetscape:', function(){ + + it('When bookmarks collection is null - result should contains only header and footer', function(){ + + var expected = 'Bookmarks

Bookmarks

'; + + 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 = 'Bookmarks

Bookmarks

'; + + 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 = 'Bookmarks

Bookmarks

Underscore.js
GitHub

'; + + 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 = 'Bookmarks

Bookmarks

Underscore.js
GitHub

'; + + 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 From 24b5cfec7fef850477e6a231949420629bb8677d Mon Sep 17 00:00:00 2001 From: Artem Grygor Date: Sat, 14 Mar 2015 15:25:39 -0400 Subject: [PATCH 6/7] code clean up and fix jshint --- js/services/exporter.js | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/js/services/exporter.js b/js/services/exporter.js index fc0da1c..08c5fed 100644 --- a/js/services/exporter.js +++ b/js/services/exporter.js @@ -9,11 +9,6 @@ function(_) { "use strict"; */ var Exporter = function () { - // this.exportToNetscape = function(bookmarks){ - - // return angular.toJson(bookmarks); - // }; - var header = function(){ return ''+ '