zotero/chrome/chromeFiles/content/scholar/overlay.js

206 lines
5.7 KiB
JavaScript
Raw Normal View History

/*
* This object contains the various functions for the interface
*/
2006-05-30 22:06:33 +00:00
var ScholarPane = new function()
{
var collectionsView;
2006-05-30 22:06:33 +00:00
var itemsView;
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
//Privileged methods
this.onLoad = onLoad;
this.onUnload = onUnload;
this.toggleDisplay = toggleDisplay;
2006-05-30 22:06:33 +00:00
this.newItem = newItem;
this.newCollection = newCollection;
this.onCollectionSelected = onCollectionSelected;
2006-05-30 22:06:33 +00:00
this.itemSelected = itemSelected;
this.deleteItemSelection = deleteItemSelection;
this.deleteCollectionSelection = deleteCollectionSelection;
this.renameSelectedCollection = renameSelectedCollection;
2006-05-30 22:06:33 +00:00
this.search = search;
this.toggleView = toggleView;
2006-05-30 22:06:33 +00:00
/*
* Called when the window is open
*/
function onLoad()
2006-05-30 22:06:33 +00:00
{
//Initialize collections view
collectionsView = new Scholar.CollectionTreeView();
document.getElementById('collections-tree').view = collectionsView;
//select Library
collectionsView.selection.select(0);
2006-05-30 22:06:33 +00:00
//Create the add menu with each item type
2006-05-30 22:06:33 +00:00
var addMenu = document.getElementById('tb-add').firstChild;
var itemTypes = Scholar.ItemTypes.getTypes();
for(var i = 0; i<itemTypes.length; i++)
{
var menuitem = document.createElement("menuitem");
menuitem.setAttribute("label", Scholar.getString("itemTypes."+itemTypes[i]['name']));
menuitem.setAttribute("oncommand","ScholarPane.newItem("+itemTypes[i]['id']+")");
addMenu.appendChild(menuitem);
}
//Drag.init(document.getElementById('scholar-floater-handle'),document.getElementById('scholar-floater'), 0, 400, 0, 500, true, true);
2006-05-30 22:06:33 +00:00
}
/*
* Called when the window closes
*/
function onUnload()
{
collectionsView.unregister();
if(itemsView)
itemsView.unregister();
}
/*
* Hides/displays the Scholar interface
*/
function toggleDisplay()
{
var visible = document.getElementById('scholar-pane').getAttribute('collapsed') == 'true';
document.getElementById('scholar-pane').setAttribute('collapsed',!visible);
document.getElementById('scholar-splitter').setAttribute('collapsed',!visible);
document.getElementById('scholar-floater').hidden = (!visible || itemsView.selection.count != 1);
}
/*
* Called when the window closes
*/
2006-05-30 22:06:33 +00:00
function newItem(typeID)
{
document.getElementById('scholar-floater').hidden=false;
MetadataPane.viewItem(new Scholar.Item(typeID));
MetadataPane.toggleEdit();
2006-05-30 22:06:33 +00:00
}
function newCollection()
2006-05-30 22:06:33 +00:00
{
Scholar.Collections.add(Scholar.getString('pane.collections.untitled'));
2006-05-30 22:06:33 +00:00
}
function onCollectionSelected()
2006-05-30 22:06:33 +00:00
{
if(itemsView)
itemsView.unregister();
if(collectionsView.selection.count == 1 && collectionsView.selection.currentIndex != -1)
2006-05-30 22:06:33 +00:00
{
var collection = collectionsView._getItemAtRow(collectionsView.selection.currentIndex);
itemsView = new Scholar.ItemTreeView(collection);
2006-05-30 22:06:33 +00:00
document.getElementById('items-tree').view = itemsView;
document.getElementById('tb-rename').disabled = collection.isLibrary();
2006-05-30 22:06:33 +00:00
}
else
{
document.getElementById('items-tree').view = itemsView = null;
document.getElementById('tb-rename').disabled = true;
2006-05-30 22:06:33 +00:00
}
}
function itemSelected()
{
var editButton = document.getElementById('metadata-pane-edit-button');
if(itemsView && itemsView.selection.count == 1 && itemsView.selection.currentIndex != -1)
2006-05-30 22:06:33 +00:00
{
var item = itemsView._getItemAtRow(itemsView.selection.currentIndex);
MetadataPane.viewItem(item);
var url = item.getField('source');
if(!validURL(url))
url = 'http://www.google.com/search?q='+encodeURIComponent('"'+item.getField("title")+'"'); //+'&btnI'
// document.getElementById('content').loadURI(url);
document.getElementById('scholar-floater').hidden=false;
2006-05-30 22:06:33 +00:00
}
else
{
document.getElementById('scholar-floater').hidden=true;
2006-05-30 22:06:33 +00:00
}
}
function deleteItemSelection()
2006-05-30 22:06:33 +00:00
{
if(itemsView && itemsView.selection.count > 0 && confirm(Scholar.getString('pane.items.delete')))
2006-05-30 22:06:33 +00:00
itemsView.deleteSelection();
}
function deleteCollectionSelection()
{
if(collectionsView.selection.count > 0 && confirm(Scholar.getString('pane.collections.delete')))
collectionsView.deleteSelection();
}
function renameSelectedCollection()
{
if(collectionsView.selection.count > 0)
{
collection = collectionsView._getItemAtRow(collectionsView.selection.currentIndex);
var newName = prompt(Scholar.getString('pane.collections.rename'),collection.getName());
if(newName)
collection.ref.rename(newName);
}
}
2006-05-30 22:06:33 +00:00
function search()
{
if(itemsView)
itemsView.searchText(document.getElementById('tb-search').value);
}
function toggleView(id)
{
var button = document.getElementById('tb-'+id);
var elem = document.getElementById('scholar-'+id);
button.checked = !button.checked;
elem.hidden = !elem.hidden;
}
//Thanks: http://www.bigbold.com/snippets/posts/show/452
//TODO: move this out of overlay.js, into Scholar.js?
function validURL(s)
{
var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);
2006-05-30 22:06:33 +00:00
}
}
var ScholarItemsDragObserver =
{
onDragStart: function (evt,transferData,action)
{
transferData.data=new TransferData();
transferData.data.addDataForFlavour("text/unicode","random data");
}
};
var ScholarCollectionsDragObserver =
{
getSupportedFlavours : function ()
{
var flavours = new FlavourSet();
flavours.appendFlavour("text/unicode");
return flavours;
},
onDragOver: function (evt,dropdata,session){},
onDrop: function (evt,dropdata,session)
{
alert(dropdata.data);
}
}
window.addEventListener("load", function(e) { ScholarPane.onLoad(e); }, false);
window.addEventListener("unload", function(e) { ScholarPane.onUnload(e); }, false);