Skip to content

Commit

Permalink
Extract Chrome bookmarks fetching from collection and now the fetchin…
Browse files Browse the repository at this point in the history
…g will update existing URLs and remove deleted ones (related to issue iplanwebsites#5)
  • Loading branch information
SBoudrias committed Dec 30, 2012
1 parent fd9e1fa commit 0634475
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 130 deletions.
4 changes: 2 additions & 2 deletions app/app_build.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require.config({
jquery : "../libs/jquery", // v 1.8.3
lodash : "../libs/lodash.underscore", // v 1.0.0-rc.3
backbone : "../libs/backbone", // v 0.9.9
handlebars : "../libs/handlebars", // v 1.0.beta.6
handlebars : "../libs/handlebars", // v 1.0.rc.1
fuzzy : "../libs/fuzzy",

// Backbone plugins
Expand Down
37 changes: 8 additions & 29 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,23 @@ function( app, $, _, Backbone, router, settings, bookmarksCollection ) {
var modelsFetching = new $.Deferred();
bookmarksCollection.fetch({
success: function( collection, response ) {
if ( collection.length < 1 ) {
console.log('Empty local Collection, fetch Chrome books: ', response);
bookmarksCollection.importChromeBookmarks();
} else {
console.log('Loading bookmarkss from Localstorage cache: '+ collection.length);
bookmarksCollection.importNewChromeBookmarks(); //check if new bookmarks have been added
}
modelsFetching.resolve();
},
error: function( collection, response ) {
console.log('Error in fetching Collection: ', response);
bookmarksCollection.importChromeBookmarks();
modelsFetching.reject();
require(["modules/bookmarks.chrome"], function( chromeBookmarks ) {
chromeBookmarks.fetch().then(function() {
modelsFetching.resolve();
});

});
}
});


// ---
// Fetch data

modelsFetching.done(function() {

_.delay(function() {
//will start fetching HTML content, and indexing it...
var fbEnabled = bookmarksCollection.updateFacebookLinks();
if ( !fbEnabled ) {
// @TODO: show bar to incite user to add Facebook stuff!
}
}, 2000 );

chrome.omnibox.onInputChanged.addListener(function( str ) {
window.alert( str );
});
//bookmarksCollection.updateFacebookLinks();

// chrome.history.search({ text: '' }, function( items ) {
// // @TODO: sort bookmarks that are recent or highlight them?
// // http://developer.chrome.com/extensions/history.html
// });
});


Expand Down
56 changes: 0 additions & 56 deletions app/models/collection-bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,62 +94,6 @@ function( app, $, _, Backbone, settings, Bookmark, fuzzy, utils ) {
return _.uniq(_.pluck(_.pluck(that.models, 'attributes'), prop));
},


// ---
// Chrome importer

importChromeBookmarks: function() {
var that = this;

chrome.bookmarks.getTree(function( bookmarkTreeNodes ) {
console.log( bookmarkTreeNodes );
that.parseChromeBookmarkTree( bookmarkTreeNodes, []); //start the recursive process
});
},

//recursive function
parseChromeBookmarkTree: function( tree, folder ) {
var that= this;
console.log(folder.join('>'));
console.log('tree',tree);
_.each( tree, function( n ) {
if ( n.children && n.children.length > 0 ) {
//it's a folder
var path = _.uniq( folder );
if ( n.title ) {
//add the folder name to the list
path.push( n.title );
}
that.parseChromeBookmarkTree( n.children, path );
} else {
//it's a URL
that.addChromeBookmark( n, folder );
}
});
},

addChromeBookmark: function( tree, folder ) {
// @TODO: cleanup the garbage in this object...
// @TODO: only add if it doesn't exists...

//add to collection
this.add({
title : tree.title,
url : tree.url,
id : tree.id,
type : 'chrome',
dateAdded : tree.dateAdded,
folder : folder
});

},

// a collection is present, we check if the counts match...
importNewChromeBookmarks: function() {
var alreadyThere = this.where({ type: "chrome" });
console.log( alreadyThere.length + ' chrome bookmarks already there...' );
},


// ---
// Delicious
Expand Down
55 changes: 33 additions & 22 deletions app/models/single-bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,32 @@ function( app, $, _, Backbone, utils, BookmarkView ) {
var Bookmark = Backbone.Model.extend({

defaults: {
url : "",
title : "",
type : "",
dateAdded : "",
folder : [],
domain : "",
content_type : "",
url : "",
title : "",
type : "",
dateAdded : "",
folder : [],
domain : false,
content_type : "web",
thumbnail_url : "",
score: 0
score : 0,
keep : false
},

localStorage: new Backbone.LocalStorage('whatever2'),

initialize: function() {

//set the domain if it's a new object...
if ( this.isNew() ) {
var url = this.get('url');
if ( utils.isURL(url) ) {
this.set( 'domain', utils.getDomain(url) );
} else {
//this can be a bookmarklet, a FTP, or special page bookmark...
this.set( 'domain', false );
console.log( 'not a URL:', url );
}

//set the main set_content_type
this.set_content_type();

var url = this.get('url');
if ( utils.isURL(url) ) {
this.set( 'domain', utils.getDomain(url) );
} else {
//this can be a bookmarklet, a FTP, or special page bookmark...
this.set( 'domain', false );
}

//set the main set_content_type
this.set_content_type();

//if there's no title, use domain name
if ( !this.get('title').length ) {
Expand All @@ -58,6 +54,21 @@ function( app, $, _, Backbone, utils, BookmarkView ) {
this.save();

},

save: function(attrs, options) {
options || (options = {});

attrs = attrs || this.toJSON();

// Filter the data to send to the server
delete attrs.keep;

// Set data to be saved
options.data = JSON.stringify(attrs);

// Proxy the call to the original save function
Backbone.Model.prototype.save.call(this, attrs, options);
},

set_content_type: function() {
// @TODO add all types, find less awfull algorithm to sort websites...
Expand Down
94 changes: 94 additions & 0 deletions app/modules/bookmarks.chrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Chrome bookmarks fetcher
*
* @info: Collection of helper functions
*
*/
/*global require:true, define:true, chrome: true */

define([
"jquery",
"underscore",
"instances/all-bookmarks"
],
function( $, _, allBookmarks ) {
"use strict";

var chromeAdapter = {

fetch: function() {
return this.importChromeBookmarks();
},

importChromeBookmarks: function() {
var def = new $.Deferred(),
self = this;

chrome.bookmarks.getTree(function( tree, folder ) {
self.parseChromeBookmarkTree( tree, folder, def );
});
def.done(function() {
var unkeptBookmarks = allBookmarks.filter(function( model ) {
if ( model.get('type') !== 'chrome' ) {
return false;
}
return !model.get('keep');
});

// Delete models who've been removed from Chrome bookmarks
_.each( unkeptBookmarks, function( model ) {
model.destroy();
});
});

return def.promise();
},

// Recurse over the bookmarks tree
parseChromeBookmarkTree: function( tree, folder, def ) {
var self = this,
folder = folder || [];

_.each( tree, function( node ) {
if ( node.children && node.children.length > 0 ) {
//it's a folder
var path = _.uniq( folder );
if ( node.title ) {
//add the folder name to the list
path.push( node.title );
}
// Recurse over the subtree
self.parseChromeBookmarkTree( node.children, path );
} else {
//it's a URL
self.addChromeBookmark( node, folder );
}
});

def && def.resolve();
},

addChromeBookmark: function( bookmark, folder ) {
var actual = allBookmarks.where({ url: bookmark.url })[0],
data = {
title : bookmark.title,
url : bookmark.url,
id : bookmark.id,
type : 'chrome',
dateAdded : bookmark.dateAdded,
folder : folder,
keep : true // mark this model to be kept
};

// Add or update collection
if ( actual ) {
actual.set( data );
} else {
allBookmarks.add( data );
}

}
};

return chromeAdapter;
});
2 changes: 1 addition & 1 deletion app/templates/footer-zoomLevel.hbs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<input type="range" min="0" max="100" id="zoom_level" value="50" />
<input type="range" min="0" max="100" id="zoom_level" value="50" />
15 changes: 15 additions & 0 deletions app/views/single-bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ function( app, $, _, Backbone ) {

data: function() {
return this.model.toJSON();
},

afterRender: function() {
this.listenTo( this.model, "change", function( model ) {
// Ignore `keep` value changes and `folder` (as array are referenced by instances,
// not value)
var realChanged = _.omit( model.changed, [ "keep", "folder" ]);

if ( _.keys(realChanged).length ) {
this.render();
}
});
this.listenTo( this.model, "destroy", function() {
this.$el.animate({ "opacity" : 0.4 });
});
}
});

Expand Down
5 changes: 2 additions & 3 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ module.exports = function( grunt ) {
facebook: "empty:",

// Only replace Handlebars with the runtime if you're not loading any template after builds
handlebars : "../libs/handlebars.runtime", // v 1.0.beta.6
handlebars : "../libs/handlebars.runtime",

},

// Includes script to inline
include: [

"modules/bookmarks.chrome"
],

//findNestedDependencies: true,
Expand Down Expand Up @@ -198,7 +198,6 @@ module.exports = function( grunt ) {
});

grunt.loadNpmTasks('grunt-contrib');
grunt.loadNpmTasks('grunt-smushit');

// @todo: add back `lint` task sometime
grunt.registerTask("default", "clean:all handlebars requirejs concat clean:build");
Expand Down
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.11",
"description": "A browser action with a popup dump of all bookmarks, including search, add, edit and delete.",
"permissions": [
"bookmarks",
"bookmarks",
"tabs",
"history",
"storage",
Expand All @@ -28,11 +28,11 @@
"chrome_url_overrides": {
"newtab": "pages/newtab.html"
},
"content_security_policy": "script-src 'self' https://connect.facebook.net https://ajax.googleapis.com https://connect.facebook.net/en_US/all.js; object-src 'self' ",
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self' ",
"omnibox" : {
"keyword": "*"
},
"background" : {
"page":"pages/background.html"
}
}
}
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name" : "NewTab-Bookmarks",
"version" : "0.0.1",
"private" : true,
"dependencies" : {},
"devDependencies" : {
"grunt-contrib" : "0.3.0",
"grunt-smushit" : "0.3.7"
}
}
"name": "NewTab-Bookmarks",
"version": "0.0.1",
"private": true,
"dependencies": {},
"devDependencies": {
"grunt-contrib": "*",
"grunt": "~0.3"
}
}
Loading

0 comments on commit 0634475

Please sign in to comment.