1
0
mirror of https://github.com/MatMoul/quickdial-webext.git synced 2024-12-23 15:36:12 +00:00

Code: Rename var core to app

This commit is contained in:
MatMoul 2017-10-14 22:43:48 +02:00
parent feafd8449e
commit 0f956667a0

View File

@ -1,22 +1,21 @@
var core = {}; // Main app object in background.js var app = {}; // App object
var app = {}; // Shared app object with pages
core.init = function(){ // Init module app.init = function(){ // Init module
core.Settings.init(function(){ app.Settings.init(function(){
core.Messages.init(); app.Messages.init();
browser.runtime.sendMessage({ cmd: core.Messages.Commands.settingsChanged }); browser.runtime.sendMessage({ cmd: app.Messages.Commands.settingsChanged });
browser.browserAction.onClicked.addListener(function(){ browser.browserAction.onClicked.addListener(function(){
browser.tabs.create({}); browser.tabs.create({});
}); });
core.GridNodes.sync(core.node, core.settings.grid.root, function(){ app.GridNodes.sync(app.node, app.settings.grid.root, function(){
browser.runtime.sendMessage({ cmd: core.Messages.Commands.gridNodesLoaded }); browser.runtime.sendMessage({ cmd: app.Messages.Commands.gridNodesLoaded });
core.Bookmarks.initListener(); app.Bookmarks.initListener();
}); });
}); });
}; };
core.Messages = {}; // Messages helper object app.Messages = {}; // Messages helper object
core.Messages.Commands = { app.Messages.Commands = {
getSettings: 0, getSettings: 0,
setSettings: 1, setSettings: 1,
getNode: 2, getNode: 2,
@ -31,68 +30,68 @@ core.Messages.Commands = {
settingsChanged: 100, settingsChanged: 100,
gridNodesLoaded: 101 gridNodesLoaded: 101
}; };
core.Messages.init = function(){ // Init Messages Listeners app.Messages.init = function(){ // Init Messages Listeners
browser.runtime.onMessage.addListener(function(request, sender, sendResponse){ browser.runtime.onMessage.addListener(function(request, sender, sendResponse){
switch(request.cmd){ switch(request.cmd){
case core.Messages.Commands.getSettings: case app.Messages.Commands.getSettings:
sendResponse(core.settings); sendResponse(app.settings);
break; break;
case core.Messages.Commands.setSettings: case app.Messages.Commands.setSettings:
core.settings = request.settings; app.settings = request.settings;
core.Settings.save(); app.Settings.save();
sendResponse(core.settings); sendResponse(app.settings);
browser.runtime.sendMessage( { cmd: core.Messages.Commands.settingsChanged } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.settingsChanged } );
browser.runtime.sendMessage( { cmd: core.Messages.Commands.gridNodesLoaded } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.gridNodesLoaded } );
break; break;
case core.Messages.Commands.getNodeByID: case app.Messages.Commands.getNodeByID:
var nodes = core.GridNodes.getNodeWithParents(request.id); var nodes = app.GridNodes.getNodeWithParents(request.id);
if(nodes) sendResponse(nodes[nodes.length-1]); if(nodes) sendResponse(nodes[nodes.length-1]);
else sendResponse(null); else sendResponse(null);
break; break;
case core.Messages.Commands.updateNode: case app.Messages.Commands.updateNode:
core.GridNodes.updateNode(core.GridNodes.getNodeById(request.id), request.value, function(){ app.GridNodes.updateNode(app.GridNodes.getNodeById(request.id), request.value, function(){
browser.runtime.sendMessage( { cmd: core.Messages.Commands.gridNodesLoaded } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.gridNodesLoaded } );
}); });
break; break;
case core.Messages.Commands.getNode: case app.Messages.Commands.getNode:
sendResponse(core.GridNodes.getNode(core.node, request.path.substr(1))); sendResponse(app.GridNodes.getNode(app.node, request.path.substr(1)));
break; break;
case core.Messages.Commands.setNodeIndex: case app.Messages.Commands.setNodeIndex:
core.GridNodes.setNodeIndex(core.GridNodes.getNode(core.node, request.path.substr(1)), request.index, request.newIndex, function(){ app.GridNodes.setNodeIndex(app.GridNodes.getNode(app.node, request.path.substr(1)), request.index, request.newIndex, function(){
browser.runtime.sendMessage( { cmd: core.Messages.Commands.gridNodesLoaded } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.gridNodesLoaded } );
}); });
break; break;
case core.Messages.Commands.createBookmark: case app.Messages.Commands.createBookmark:
core.GridNodes.createBookmark(core.GridNodes.getNode(core.node, request.path.substr(1)), request.url, request.title, function(){ app.GridNodes.createBookmark(app.GridNodes.getNode(app.node, request.path.substr(1)), request.url, request.title, function(){
browser.runtime.sendMessage( { cmd: core.Messages.Commands.gridNodesLoaded } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.gridNodesLoaded } );
}); });
break; break;
case core.Messages.Commands.createFolder: case app.Messages.Commands.createFolder:
core.GridNodes.createFolder(core.GridNodes.getNode(core.node, request.path.substr(1)), request.name, function(){ app.GridNodes.createFolder(app.GridNodes.getNode(app.node, request.path.substr(1)), request.name, function(){
browser.runtime.sendMessage( { cmd: core.Messages.Commands.gridNodesLoaded } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.gridNodesLoaded } );
}); });
break; break;
case core.Messages.Commands.deleteNode: case app.Messages.Commands.deleteNode:
core.GridNodes.deleteNode(core.GridNodes.getNode(core.node, request.path.substr(1)), request.id, function(){ app.GridNodes.deleteNode(app.GridNodes.getNode(app.node, request.path.substr(1)), request.id, function(){
browser.runtime.sendMessage( { cmd: core.Messages.Commands.gridNodesLoaded } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.gridNodesLoaded } );
}); });
break; break;
case core.Messages.Commands.refreshNode: case app.Messages.Commands.refreshNode:
core.GridNodes.refreshNode(core.GridNodes.getChildNode(core.GridNodes.getNode(core.node, request.path.substr(1)), request.id), function(){ app.GridNodes.refreshNode(app.GridNodes.getChildNode(app.GridNodes.getNode(app.node, request.path.substr(1)), request.id), function(){
browser.runtime.sendMessage( { cmd: core.Messages.Commands.gridNodesLoaded } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.gridNodesLoaded } );
}); });
break; break;
case core.Messages.Commands.capturePage: case app.Messages.Commands.capturePage:
core.GridNodes.capturePage(core.GridNodes.getChildNode(core.GridNodes.getNode(core.node, request.path.substr(1)), request.id), function(){ app.GridNodes.capturePage(app.GridNodes.getChildNode(app.GridNodes.getNode(app.node, request.path.substr(1)), request.id), function(){
browser.runtime.sendMessage( { cmd: core.Messages.Commands.gridNodesLoaded } ); browser.runtime.sendMessage( { cmd: app.Messages.Commands.gridNodesLoaded } );
}); });
break; break;
} }
}); });
}; };
core.Settings = {}; // Settings helper object app.Settings = {}; // Settings helper object
core.Settings.init = function(callback){ // Load settings and nodes app.Settings.init = function(callback){ // Load settings and nodes
browser.storage.local.get().then(function(data){ browser.storage.local.get().then(function(data){
if(Object.keys(data).length == 0) { if(Object.keys(data).length == 0) {
data = { data = {
@ -157,34 +156,34 @@ core.Settings.init = function(callback){ // Load settings and nodes
data.node = oldData.grid.node; data.node = oldData.grid.node;
delete data.settings.version; delete data.settings.version;
delete data.settings.grid.node; delete data.settings.grid.node;
core.settings = data.settings; app.settings = data.settings;
core.node = data.node; app.node = data.node;
browser.storage.local.clear().then(function(){ browser.storage.local.clear().then(function(){
core.Settings.save(); app.Settings.save();
}); });
} }
core.settings = data.settings; app.settings = data.settings;
core.node = data.node; app.node = data.node;
if(callback) callback(); if(callback) callback();
}, function(){ console.log('Error loading data'); }); }, function(){ console.log('Error loading data'); });
}; };
core.Settings.update = function(settings, callback){ // Save new settings app.Settings.update = function(settings, callback){ // Save new settings
core.settings = settings; app.settings = settings;
core.Settings.save(callback); app.Settings.save(callback);
}; };
core.Settings.save = function(callback){ // Save settings app.Settings.save = function(callback){ // Save settings
var data = { version: 3 }; var data = { version: 3 };
data.settings = core.settings; data.settings = app.settings;
data.node = core.node; data.node = app.node;
browser.storage.local.set(data).then(function(){ browser.storage.local.set(data).then(function(){
if(callback) callback(); if(callback) callback();
}, function(){ console.log('Error saving settings'); }); }, function(){ console.log('Error saving settings'); });
} }
core.init(); app.init();
core.ContextMenus = {} // ContextMenu helper Object app.ContextMenus = {} // ContextMenu helper Object
core.ContextMenus.initMenu = function(){ // (Called from core.init) Init context menu in all pages app.ContextMenus.initMenu = function(){ // (Called from app.init) Init context menu in all pages
browser.contextMenus.create({ // Create Context menu browser.contextMenus.create({ // Create Context menu
id: 'AddToQuickDial', id: 'AddToQuickDial',
title: browser.i18n.getMessage("menuAddToQuickDial"), title: browser.i18n.getMessage("menuAddToQuickDial"),
@ -193,22 +192,22 @@ core.ContextMenus.initMenu = function(){ // (Called from core.init) Init context
}, function(){}); }, function(){});
browser.contextMenus.onClicked.addListener(function(info, tab) { // Context menu click event browser.contextMenus.onClicked.addListener(function(info, tab) { // Context menu click event
//if (info.menuItemId == "AddToQuickDial") //if (info.menuItemId == "AddToQuickDial")
//core.GridNodes.createBookmark(app.settings.grid.node, info.pageUrl, tab.title, function(){}); //app.GridNodes.createBookmark(app.settings.grid.node, info.pageUrl, tab.title, function(){});
}); });
} }
core.Bookmarks = {} // Bookmarks helper object app.Bookmarks = {} // Bookmarks helper object
core.Bookmarks._onCreated = function(){ core.GridNodes.sync(core.node, core.settings.grid.root, function(){ browser.runtime.sendMessage({ cmd: core.Messages.Commands.gridNodesLoaded }); }); } app.Bookmarks._onCreated = function(){ app.GridNodes.sync(app.node, app.settings.grid.root, function(){ browser.runtime.sendMessage({ cmd: app.Messages.Commands.gridNodesLoaded }); }); }
core.Bookmarks._onChanged = function(){ core.GridNodes.sync(core.node, core.settings.grid.root, function(){ browser.runtime.sendMessage({ cmd: core.Messages.Commands.gridNodesLoaded }); }); } app.Bookmarks._onChanged = function(){ app.GridNodes.sync(app.node, app.settings.grid.root, function(){ browser.runtime.sendMessage({ cmd: app.Messages.Commands.gridNodesLoaded }); }); }
core.Bookmarks._onMoved = function(){ core.GridNodes.sync(core.node, core.settings.grid.root, function(){ browser.runtime.sendMessage({ cmd: core.Messages.Commands.gridNodesLoaded }); }); } app.Bookmarks._onMoved = function(){ app.GridNodes.sync(app.node, app.settings.grid.root, function(){ browser.runtime.sendMessage({ cmd: app.Messages.Commands.gridNodesLoaded }); }); }
core.Bookmarks._onRemoved = function(){ core.GridNodes.sync(core.node, core.settings.grid.root, function(){ browser.runtime.sendMessage({ cmd: core.Messages.Commands.gridNodesLoaded }); }); } app.Bookmarks._onRemoved = function(){ app.GridNodes.sync(app.node, app.settings.grid.root, function(){ browser.runtime.sendMessage({ cmd: app.Messages.Commands.gridNodesLoaded }); }); }
core.Bookmarks.initListener = function(){ // (Called from core.init) (/!\ Need filter to root tree only) Init listener of bookmarks app.Bookmarks.initListener = function(){ // (Called from app.init) (/!\ Need filter to root tree only) Init listener of bookmarks
browser.bookmarks.onCreated.addListener(core.Bookmarks._onCreated); browser.bookmarks.onCreated.addListener(app.Bookmarks._onCreated);
//browser.bookmarks.onChanged.addListener(core.Bookmarks._onChanged); //browser.bookmarks.onChanged.addListener(app.Bookmarks._onChanged);
browser.bookmarks.onMoved.addListener(core.Bookmarks._onMoved); browser.bookmarks.onMoved.addListener(app.Bookmarks._onMoved);
browser.bookmarks.onRemoved.addListener(core.Bookmarks._onRemoved); browser.bookmarks.onRemoved.addListener(app.Bookmarks._onRemoved);
} }
core.Bookmarks.load = function(rootPath, callback){ // Load root bookmark and create it if not exist app.Bookmarks.load = function(rootPath, callback){ // Load root bookmark and create it if not exist
if(!callback) return; if(!callback) return;
browser.bookmarks.getSubTree('menu________').then(function(bookmarkItems){ browser.bookmarks.getSubTree('menu________').then(function(bookmarkItems){
function getChildItem(bookmarkItem, path, callback){ function getChildItem(bookmarkItem, path, callback){
@ -230,8 +229,8 @@ core.Bookmarks.load = function(rootPath, callback){ // Load root bookmark and cr
}, function(){ callback(); }); }, function(){ callback(); });
} }
core.SiteInfos = {} // Siteinfos helper object app.SiteInfos = {} // Siteinfos helper object
core.SiteInfos.fromNewTab = function(url, callback){ // Retrieve infos from a new tab. callback( { url, title, icon, screenshot } || error: callback() ) app.SiteInfos.fromNewTab = function(url, callback){ // Retrieve infos from a new tab. callback( { url, title, icon, screenshot } || error: callback() )
browser.tabs.create({url: url, active: false}).then(function(tab){ browser.tabs.create({url: url, active: false}).then(function(tab){
browser.tabs.update(tab.id, {muted: true}).then(); browser.tabs.update(tab.id, {muted: true}).then();
function whaitLoaded(){ function whaitLoaded(){
@ -284,7 +283,7 @@ core.SiteInfos.fromNewTab = function(url, callback){ // Retrieve infos from a n
setTimeout(whaitLoaded, 300); setTimeout(whaitLoaded, 300);
}, function(){ if(callback) callback(); }); }, function(){ if(callback) callback(); });
} }
core.SiteInfos.fromFrame = function(url, callback){ // Retrieve infos from an iframe. callback( { url, title, (/!\ Not handled now)icon, screenshot } || error: callback() ) app.SiteInfos.fromFrame = function(url, callback){ // Retrieve infos from an iframe. callback( { url, title, (/!\ Not handled now)icon, screenshot } || error: callback() )
function pageLoaded(){ function pageLoaded(){
if(!iframe) return; if(!iframe) return;
var docTitle = iframe.contentWindow.document.title; var docTitle = iframe.contentWindow.document.title;
@ -335,33 +334,33 @@ core.SiteInfos.fromFrame = function(url, callback){ // Retrieve infos from an if
xmlHttp.ontimeout = function(){ if(callback) callback(); } xmlHttp.ontimeout = function(){ if(callback) callback(); }
xmlHttp.send(); xmlHttp.send();
} }
core.SiteInfos.fromWS = function(url, callback){ // Retrieve infos from a Web Service. callback( { url, title, (/!\ Not handled now)icon, screenshot } || error: callback() ) app.SiteInfos.fromWS = function(url, callback){ // Retrieve infos from a Web Service. callback( { url, title, (/!\ Not handled now)icon, screenshot } || error: callback() )
console.log('Not implemented'); console.log('Not implemented');
return core.SiteInfos.fromFrame(url, callback); return app.SiteInfos.fromFrame(url, callback);
} }
core.GridNodes = {}; // GridNodes helper object app.GridNodes = {}; // GridNodes helper object
core.GridNodes.GridNodeType = { // GridNodeType app.GridNodes.GridNodeType = { // GridNodeType
back: -1, back: -1,
empty: 0, empty: 0,
folder: 1, folder: 1,
bookmark: 2 bookmark: 2
} }
core.GridNodes.sync = function(gridNode, rootPath, callback){ // Sync GridNodes with Bookmarks app.GridNodes.sync = function(gridNode, rootPath, callback){ // Sync GridNodes with Bookmarks
core.Bookmarks.load(rootPath, function(bookmarkItem){ app.Bookmarks.load(rootPath, function(bookmarkItem){
function syncNode(gridNode, bookmarkItem){ function syncNode(gridNode, bookmarkItem){
gridNode.id = bookmarkItem.id; gridNode.id = bookmarkItem.id;
if(!gridNode.title) gridNode.title = bookmarkItem.title; if(!gridNode.title) gridNode.title = bookmarkItem.title;
if(bookmarkItem.url){ if(bookmarkItem.url){
gridNode.type = core.GridNodes.GridNodeType.bookmark; gridNode.type = app.GridNodes.GridNodeType.bookmark;
if(!gridNode.url) gridNode.url = bookmarkItem.url; if(!gridNode.url) gridNode.url = bookmarkItem.url;
} else if(bookmarkItem.children){ } else if(bookmarkItem.children){
gridNode.type = core.GridNodes.GridNodeType.folder; gridNode.type = app.GridNodes.GridNodeType.folder;
var EmptyNodes = []; var EmptyNodes = [];
if(! gridNode.children) gridNode.children = []; if(! gridNode.children) gridNode.children = [];
else { else {
for(var i=gridNode.children.length-1; i>=0; i--){ for(var i=gridNode.children.length-1; i>=0; i--){
if(gridNode.children[i].type==core.GridNodes.GridNodeType.empty){ if(gridNode.children[i].type==app.GridNodes.GridNodeType.empty){
EmptyNodes.unshift(gridNode.children[i]); EmptyNodes.unshift(gridNode.children[i]);
} else { } else {
var found = false; var found = false;
@ -373,7 +372,7 @@ core.GridNodes.sync = function(gridNode, rootPath, callback){ // Sync GridNodes
} }
if(! found){ if(! found){
if(i<gridNode.children.length - 1){ if(i<gridNode.children.length - 1){
gridNode.children[i] = { type: core.GridNodes.GridNodeType.empty }; gridNode.children[i] = { type: app.GridNodes.GridNodeType.empty };
EmptyNodes.unshift(gridNode.children[i]); EmptyNodes.unshift(gridNode.children[i]);
} else { } else {
gridNode.children.pop(); gridNode.children.pop();
@ -383,7 +382,7 @@ core.GridNodes.sync = function(gridNode, rootPath, callback){ // Sync GridNodes
} }
} }
for(var child of bookmarkItem.children){ for(var child of bookmarkItem.children){
var childGridNode = core.GridNodes.getChildNode(gridNode, child.id); var childGridNode = app.GridNodes.getChildNode(gridNode, child.id);
if(!childGridNode){ if(!childGridNode){
if(EmptyNodes.length>0){ if(EmptyNodes.length>0){
childGridNode = EmptyNodes[0]; childGridNode = EmptyNodes[0];
@ -397,31 +396,31 @@ core.GridNodes.sync = function(gridNode, rootPath, callback){ // Sync GridNodes
} }
EmptyNodes.length = 0; EmptyNodes.length = 0;
} else { } else {
gridNode.type = core.GridNodes.GridNodeType.empty; gridNode.type = app.GridNodes.GridNodeType.empty;
} }
} }
syncNode(gridNode, bookmarkItem); syncNode(gridNode, bookmarkItem);
core.GridNodes.save(); app.GridNodes.save();
if(callback) callback(); if(callback) callback();
}); });
} }
core.GridNodes.save = function(callback){ // Save GridNodes app.GridNodes.save = function(callback){ // Save GridNodes
core.Settings.save(callback); app.Settings.save(callback);
} }
core.GridNodes.getNode = function(gridNode, path){ // Return GridNode from RootGridNode path app.GridNodes.getNode = function(gridNode, path){ // Return GridNode from RootGridNode path
if(path.length == 0 || path == '/') return gridNode; if(path.length == 0 || path == '/') return gridNode;
for(var child of gridNode.children) for(var child of gridNode.children)
if(path.startsWith(child.title + '/')) if(path.startsWith(child.title + '/'))
return core.GridNodes.getNode(child, path.substr(child.title.length + 1)); return app.GridNodes.getNode(child, path.substr(child.title.length + 1));
return null; return null;
} }
core.GridNodes.getNodeById = function(id){ app.GridNodes.getNodeById = function(id){
var nodes = core.GridNodes.getNodeWithParents(id); var nodes = app.GridNodes.getNodeWithParents(id);
if(nodes) return nodes[nodes.length-1]; if(nodes) return nodes[nodes.length-1];
return null; return null;
} }
core.GridNodes.getNodeWithParents = function(id){ app.GridNodes.getNodeWithParents = function(id){
var parents = []; var parents = [];
@ -442,125 +441,125 @@ core.GridNodes.getNodeWithParents = function(id){
return null; return null;
} }
findNode(core.node, id); findNode(app.node, id);
if(parents.length>0) return parents; if(parents.length>0) return parents;
return null; return null;
} }
core.GridNodes.updateNode = function(gridNode, value, callback){ app.GridNodes.updateNode = function(gridNode, value, callback){
if(value){ if(value){
if(value.title) gridNode.title = value.title; if(value.title) gridNode.title = value.title;
if(gridNode.type == core.GridNodes.GridNodeType.bookmark && value.url && gridNode.url != value.url){ if(gridNode.type == app.GridNodes.GridNodeType.bookmark && value.url && gridNode.url != value.url){
gridNode.url = value.url; gridNode.url = value.url;
delete gridNode.image; delete gridNode.image;
} }
//gridNode.image = infos.screenshot; //gridNode.image = infos.screenshot;
core.GridNodes.saveNode(gridNode); app.GridNodes.saveNode(gridNode);
} }
if(callback) callback(gridNode); if(callback) callback(gridNode);
} }
core.GridNodes.getChildNode = function(gridNode, id){ // Return child node by ID app.GridNodes.getChildNode = function(gridNode, id){ // Return child node by ID
for(var child of gridNode.children) if(child.id == id) return child; for(var child of gridNode.children) if(child.id == id) return child;
return null; return null;
} }
core.GridNodes.saveNode = function(gridNode, callback){ // Save GridNode app.GridNodes.saveNode = function(gridNode, callback){ // Save GridNode
core.Settings.save(callback); app.Settings.save(callback);
} }
core.GridNodes.setNodeIndex = function(gridNode, index, newIndex, callback){ // Set Child GridNodeIndex. callback(gridNode, node1, node2) app.GridNodes.setNodeIndex = function(gridNode, index, newIndex, callback){ // Set Child GridNodeIndex. callback(gridNode, node1, node2)
while(newIndex>=gridNode.children.length) while(newIndex>=gridNode.children.length)
gridNode.children.push({ type: core.GridNodes.GridNodeType.empty }); gridNode.children.push({ type: app.GridNodes.GridNodeType.empty });
var node1 = gridNode.children[index]; var node1 = gridNode.children[index];
var node2 = gridNode.children[newIndex]; var node2 = gridNode.children[newIndex];
gridNode.children[index] = node2; gridNode.children[index] = node2;
gridNode.children[newIndex] = node1; gridNode.children[newIndex] = node1;
for(var i=gridNode.children.length-1; i>=0; i--){ for(var i=gridNode.children.length-1; i>=0; i--){
if(gridNode.children[i].type != core.GridNodes.GridNodeType.empty) break; if(gridNode.children[i].type != app.GridNodes.GridNodeType.empty) break;
gridNode.children.pop(); gridNode.children.pop();
} }
core.GridNodes.saveNode(gridNode); app.GridNodes.saveNode(gridNode);
if(callback) callback(gridNode, node1, node2); if(callback) callback(gridNode, node1, node2);
} }
core.GridNodes.createBookmark = function(gridNode, url, title, callback){ // Create a new Bookmark in a GridNode. callback(gridNode, newGridNode) app.GridNodes.createBookmark = function(gridNode, url, title, callback){ // Create a new Bookmark in a GridNode. callback(gridNode, newGridNode)
browser.bookmarks.onCreated.removeListener(core.Bookmarks._onCreated); browser.bookmarks.onCreated.removeListener(app.Bookmarks._onCreated);
browser.bookmarks.create({ browser.bookmarks.create({
parentId: gridNode.id, parentId: gridNode.id,
title: title || url, title: title || url,
url: url url: url
}).then(function(bookmarkItem){ }).then(function(bookmarkItem){
if(!gridNode) return; // ??? Why this method are called a second time with gridNode = null ??? if(!gridNode) return; // ??? Why this method are called a second time with gridNode = null ???
browser.bookmarks.onCreated.addListener(core.Bookmarks._onCreated); browser.bookmarks.onCreated.addListener(app.Bookmarks._onCreated);
var newGridNode = { id: bookmarkItem.id, type: core.GridNodes.GridNodeType.bookmark, url: url, title }; var newGridNode = { id: bookmarkItem.id, type: app.GridNodes.GridNodeType.bookmark, url: url, title };
var EmptyCellFound = false; var EmptyCellFound = false;
for(var i=0; i<gridNode.children.length; i++){ for(var i=0; i<gridNode.children.length; i++){
if(gridNode.children[i].type == core.GridNodes.GridNodeType.empty){ if(gridNode.children[i].type == app.GridNodes.GridNodeType.empty){
EmptyCellFound = true; EmptyCellFound = true;
gridNode.children[i] = newGridNode; gridNode.children[i] = newGridNode;
break; break;
} }
} }
if(EmptyCellFound == false) gridNode.children.push(newGridNode); if(EmptyCellFound == false) gridNode.children.push(newGridNode);
core.GridNodes.saveNode(newGridNode); app.GridNodes.saveNode(newGridNode);
if(callback) callback(gridNode, newGridNode); if(callback) callback(gridNode, newGridNode);
}, function(){ }, function(){
browser.bookmarks.onCreated.addListener(core.Bookmarks._onCreated); browser.bookmarks.onCreated.addListener(app.Bookmarks._onCreated);
}); });
} }
core.GridNodes.createFolder = function(gridNode, name, callback){ // Create a new folder in a GridNode. callback(gridNode, newGridNode) app.GridNodes.createFolder = function(gridNode, name, callback){ // Create a new folder in a GridNode. callback(gridNode, newGridNode)
browser.bookmarks.onCreated.removeListener(core.Bookmarks._onCreated); browser.bookmarks.onCreated.removeListener(app.Bookmarks._onCreated);
browser.bookmarks.create({ browser.bookmarks.create({
parentId: gridNode.id, parentId: gridNode.id,
title: name title: name
}).then(function(bookmarkItem){ }).then(function(bookmarkItem){
if(!gridNode) return; // ??? Why this method are called a second time with gridNode = null ??? if(!gridNode) return; // ??? Why this method are called a second time with gridNode = null ???
browser.bookmarks.onCreated.addListener(core.Bookmarks._onCreated); browser.bookmarks.onCreated.addListener(app.Bookmarks._onCreated);
var newGridNode = { id: bookmarkItem.id, type: core.GridNodes.GridNodeType.folder, title: name, children: [] }; var newGridNode = { id: bookmarkItem.id, type: app.GridNodes.GridNodeType.folder, title: name, children: [] };
var EmptyCellFound = false; var EmptyCellFound = false;
for(var i=0; i<gridNode.children.length; i++){ for(var i=0; i<gridNode.children.length; i++){
if(gridNode.children[i].type == core.GridNodes.GridNodeType.empty){ if(gridNode.children[i].type == app.GridNodes.GridNodeType.empty){
EmptyCellFound = true; EmptyCellFound = true;
gridNode.children[i] = newGridNode; gridNode.children[i] = newGridNode;
break; break;
} }
} }
if(EmptyCellFound == false) gridNode.children.push(newGridNode); if(EmptyCellFound == false) gridNode.children.push(newGridNode);
core.GridNodes.saveNode(newGridNode); app.GridNodes.saveNode(newGridNode);
if(callback) callback(gridNode, newGridNode); if(callback) callback(gridNode, newGridNode);
}, function(){ }, function(){
browser.bookmarks.onCreated.addListener(core.Bookmarks._onCreated); browser.bookmarks.onCreated.addListener(app.Bookmarks._onCreated);
}); });
} }
core.GridNodes.deleteNode = function(gridNode, id, callback){ // Delete a GridNode. callback(gridNode, id) app.GridNodes.deleteNode = function(gridNode, id, callback){ // Delete a GridNode. callback(gridNode, id)
for(var i=0; i<gridNode.children.length; i++){ for(var i=0; i<gridNode.children.length; i++){
if(gridNode.children[i].id == id){ if(gridNode.children[i].id == id){
gridNode.children[i] = { type: core.GridNodes.GridNodeType.empty }; gridNode.children[i] = { type: app.GridNodes.GridNodeType.empty };
core.GridNodes.saveNode(gridNode); app.GridNodes.saveNode(gridNode);
break; break;
} }
} }
for(var i=gridNode.children.length-1; i>=0; i--){ for(var i=gridNode.children.length-1; i>=0; i--){
if(gridNode.children[i].type != core.GridNodes.GridNodeType.empty) break; if(gridNode.children[i].type != app.GridNodes.GridNodeType.empty) break;
gridNode.children.pop(); gridNode.children.pop();
} }
browser.bookmarks.onRemoved.removeListener(core.Bookmarks._onRemoved); browser.bookmarks.onRemoved.removeListener(app.Bookmarks._onRemoved);
browser.bookmarks.removeTree(id).then(function(){ browser.bookmarks.removeTree(id).then(function(){
browser.bookmarks.onRemoved.addListener(core.Bookmarks._onRemoved); browser.bookmarks.onRemoved.addListener(app.Bookmarks._onRemoved);
}, function(){ }, function(){
browser.bookmarks.onRemoved.addListener(core.Bookmarks._onRemoved); browser.bookmarks.onRemoved.addListener(app.Bookmarks._onRemoved);
}); });
if(callback) callback(gridNode, id); if(callback) callback(gridNode, id);
} }
core.GridNodes.refreshNode = function(gridNode, callback){ // Refresh content of a GridNode app.GridNodes.refreshNode = function(gridNode, callback){ // Refresh content of a GridNode
if(gridNode.__isLoading == true) return; if(gridNode.__isLoading == true) return;
gridNode.__isLoading = true; gridNode.__isLoading = true;
switch(gridNode.type){ switch(gridNode.type){
case core.GridNodes.GridNodeType.folder: case app.GridNodes.GridNodeType.folder:
delete gridNode.image; delete gridNode.image;
delete gridNode.__isLoading; delete gridNode.__isLoading;
core.GridNodes.saveNode(gridNode); app.GridNodes.saveNode(gridNode);
if(callback) callback({ title: gridNode.title, screenshot: gridNode.image }); if(callback) callback({ title: gridNode.title, screenshot: gridNode.image });
break; break;
case core.GridNodes.GridNodeType.bookmark: case app.GridNodes.GridNodeType.bookmark:
core.SiteInfos.fromFrame(gridNode.url, function(infos){ app.SiteInfos.fromFrame(gridNode.url, function(infos){
if(infos){ if(infos){
gridNode.title = infos.title; gridNode.title = infos.title;
gridNode.image = infos.screenshot; gridNode.image = infos.screenshot;
@ -568,35 +567,35 @@ core.GridNodes.refreshNode = function(gridNode, callback){ // Refresh content of
gridNode.image = '0'; gridNode.image = '0';
} }
delete gridNode.__isLoading; delete gridNode.__isLoading;
core.GridNodes.saveNode(gridNode); app.GridNodes.saveNode(gridNode);
if(callback) callback(infos); if(callback) callback(infos);
}); });
break; break;
} }
} }
core.GridNodes.capturePage = function(gridNode, callback){ app.GridNodes.capturePage = function(gridNode, callback){
if(gridNode.__isLoading == true) return; if(gridNode.__isLoading == true) return;
gridNode.__isLoading = true; gridNode.__isLoading = true;
switch(gridNode.type){ switch(gridNode.type){
case core.GridNodes.GridNodeType.folder: case app.GridNodes.GridNodeType.folder:
var nodes = core.GridNodes.getNodeWithParents(gridNode.id); var nodes = app.GridNodes.getNodeWithParents(gridNode.id);
if(nodes){ if(nodes){
var path = ''; var path = '';
for(var i=1; i<nodes.length; i++) path = path + '/' + nodes[i].title; for(var i=1; i<nodes.length; i++) path = path + '/' + nodes[i].title;
core.SiteInfos.fromNewTab('/dial?path=' + path, function(infos){ app.SiteInfos.fromNewTab('/dial?path=' + path, function(infos){
if(infos){ if(infos){
gridNode.image = infos.screenshot; gridNode.image = infos.screenshot;
} else { } else {
delete gridNode.image; delete gridNode.image;
} }
delete gridNode.__isLoading; delete gridNode.__isLoading;
core.GridNodes.saveNode(gridNode); app.GridNodes.saveNode(gridNode);
if(callback) callback({ title: gridNode.title, screenshot: gridNode.image }); if(callback) callback({ title: gridNode.title, screenshot: gridNode.image });
}); });
} }
break; break;
case core.GridNodes.GridNodeType.bookmark: case app.GridNodes.GridNodeType.bookmark:
core.SiteInfos.fromNewTab(gridNode.url, function(infos){ app.SiteInfos.fromNewTab(gridNode.url, function(infos){
if(infos){ if(infos){
gridNode.title = infos.title; gridNode.title = infos.title;
gridNode.image = infos.screenshot; gridNode.image = infos.screenshot;
@ -604,7 +603,7 @@ core.GridNodes.capturePage = function(gridNode, callback){
gridNode.image = '0'; gridNode.image = '0';
} }
delete gridNode.__isLoading; delete gridNode.__isLoading;
core.GridNodes.saveNode(gridNode); app.GridNodes.saveNode(gridNode);
if(callback) callback(infos); if(callback) callback(infos);
}); });
break; break;