Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement export bookmarks to json file from settings window #133

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions js/directives/_module.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
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', []);

// Register update background directive
module.directive('myUpdateBackground', updateBackgroundFactory);

// Register export bookmark directive
module.directive('exportBookmark', exportBookmarkFactory);
});
37 changes: 37 additions & 0 deletions js/directives/exportBookmark.js
Original file line number Diff line number Diff line change
@@ -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('<a></a>');
downloadLink.attr('href',window.URL.createObjectURL(blob));
downloadLink.attr('download', 'deweyapp.html');
downloadLink[0].click();
});
};
}
};
};

return [
'bookmarksStorage',
'exporter',
exportBookmarkFactory
];

});
8 changes: 6 additions & 2 deletions js/services/_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', []);
Expand All @@ -19,4 +20,7 @@ module.factory('booleanSearchEngine', booleanSearchEngine);
// Register settings service
module.value('appSettings', settings);

// Register export to Netscape service
module.factory('exporter', exporter);

});
2 changes: 1 addition & 1 deletion js/services/booleanSearchEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
68 changes: 68 additions & 0 deletions js/services/exporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
define(
[
'underscore'
],
function(_) { "use strict";

/*
* Bookmark exporter service.
*/
var Exporter = function () {

var header = function(){
return '<!DOCTYPE NETSCAPE-Bookmark-file-1>'+
'<!-- This is an automatically generated file.'+
'It will be read and overwritten.'+
'DO NOT EDIT! -->'+
'<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">'+
'<TITLE>Bookmarks</TITLE>'+
'<H1>Bookmarks</H1>'+
'<DL><p>';
};

var footer = function(){
return '</DL><p>';
};

var exportBookmark = function(bookmark){

var tags = '';
if(bookmark.tag.length > 0) {
tags = _.pluck(bookmark.tag, 'text').join(',');
}
return '<DT><A HREF="' + bookmark.url +
'" ADD_DATE="' + bookmark.date +
'" TAGS="' + tags +
'">' + bookmark.title + '</A>';
};

this.exportToNetscape = function(bookmarks){

var fileData = header();

if(!_.isNull(bookmarks) &&
toString.call(bookmarks) === "[object Array]" &&
bookmarks.length > 0){

_.each(bookmarks, function(bookmark){
fileData += exportBookmark(bookmark);
});
}

fileData += footer();
return fileData;
};
};

/*
* Bookmark exporter service factory method.
*/
var ExporterFactory = function() {
return new Exporter();
};

return [
ExporterFactory
];

});
5 changes: 5 additions & 0 deletions partials/exportBookmark.tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="credits">
<p>
<a class="important" style="cursor: pointer;" ng-click="export();">Export</a>
</p>
</div>
1 change: 1 addition & 0 deletions partials/main.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<input id="hide-folders" class="css-checkbox" type="checkbox" ng-checked="hideTopLevelFolders" />
<label for="hide-folders" class="css-label">Hide Top-level Folders as Tags</label>
</div>
<export-bookmark></export-bookmark>
<div class="credits"><p>Screenshots by <a class="important" href="http://www.page2images.com/" target="_blank">Page2Images</a></p></div>
</form>
</li>
Expand Down
106 changes: 106 additions & 0 deletions test/unit/services/exporter.test.js
Original file line number Diff line number Diff line change
@@ -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 = '<!DOCTYPE NETSCAPE-Bookmark-file-1><!-- This is an automatically generated file.It will be read and overwritten.DO NOT EDIT! --><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><TITLE>Bookmarks</TITLE><H1>Bookmarks</H1><DL><p></DL><p>';

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 = '<!DOCTYPE NETSCAPE-Bookmark-file-1><!-- This is an automatically generated file.It will be read and overwritten.DO NOT EDIT! --><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><TITLE>Bookmarks</TITLE><H1>Bookmarks</H1><DL><p></DL><p>';

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 = '<!DOCTYPE NETSCAPE-Bookmark-file-1><!-- This is an automatically generated file.It will be read and overwritten.DO NOT EDIT! --><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><TITLE>Bookmarks</TITLE><H1>Bookmarks</H1><DL><p><DT><A HREF="http://underscorejs.org/" ADD_DATE="1394679060712" TAGS="">Underscore.js</A><DT><A HREF="https://github.com/" ADD_DATE="1396299213543" TAGS="">GitHub</A></DL><p>';

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 = '<!DOCTYPE NETSCAPE-Bookmark-file-1><!-- This is an automatically generated file.It will be read and overwritten.DO NOT EDIT! --><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><TITLE>Bookmarks</TITLE><H1>Bookmarks</H1><DL><p><DT><A HREF="http://underscorejs.org/" ADD_DATE="1394679060712" TAGS="Other Bookmarks">Underscore.js</A><DT><A HREF="https://github.com/" ADD_DATE="1396299213543" TAGS="Bookmarks Bar,prog">GitHub</A></DL><p>';

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);
});
});
});
});

});