Closes #228, Use a unique default "Untitled" name for new saved searches

Collections too

- Also fixed JS strict warning in Item.erase()
This commit is contained in:
Dan Stillman 2006-09-22 23:53:16 +00:00
parent fc2be86681
commit 8303028a85
3 changed files with 48 additions and 6 deletions

View file

@ -155,7 +155,11 @@ var ScholarPane = new function()
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
var newName = { value: Scholar.getString('pane.collections.untitled') };
var untitled = Scholar.getString('pane.collections.untitled');
untitled = Scholar.DB.getNextName('collections', 'collectionName',
Scholar.getString('pane.collections.untitled'));
var newName = { value: untitled };
var result = promptService.prompt(window, "",
Scholar.getString('pane.collections.name'), newName, "", {});
@ -166,7 +170,7 @@ var ScholarPane = new function()
if (!newName.value)
{
newName.value = Scholar.getString('pane.collections.untitled');
newName.value = untitled;
}
Scholar.Collections.add(newName.value);
@ -177,8 +181,10 @@ var ScholarPane = new function()
var s = new Scholar.Search();
s.addCondition('title','contains','');
// TODO: add integer to 'Untitled' if more than one
var io = {dataIn: {search: s, name: 'Untitled'}, dataOut: null};
var untitled = Scholar.getString('pane.collections.untitled');
untitled = Scholar.DB.getNextName('savedSearches', 'savedSearchName',
Scholar.getString('pane.collections.untitled'));
var io = {dataIn: {search: s, name: untitled}, dataOut: null};
window.openDialog('chrome://scholar/content/searchDialog.xul','','chrome,modal',io);
}

View file

@ -1374,8 +1374,10 @@ Scholar.Item.prototype.erase = function(deleteChildren){
// Remove item from parent collections
var parentCollectionIDs = this.getCollections();
for (var i=0; i<parentCollectionIDs.length; i++){
Scholar.Collections.get(parentCollectionIDs[i]).removeItem(this.getID());
if (parentCollectionIDs){
for (var i=0; i<parentCollectionIDs.length; i++){
Scholar.Collections.get(parentCollectionIDs[i]).removeItem(this.getID());
}
}
// Note

View file

@ -15,6 +15,7 @@ Scholar.DB = new function(){
this.getColumns = getColumns;
this.getColumnHash = getColumnHash;
this.getNextID = getNextID;
this.getNextName = getNextName;
this.beginTransaction = beginTransaction;
this.commitTransaction = commitTransaction;
this.rollbackTransaction = rollbackTransaction;
@ -374,6 +375,39 @@ Scholar.DB = new function(){
}
/**
* Find the next lowest numeric suffix for a value in table column
*
* For example, if "Untitled" and "Untitled 2" and "Untitled 4",
* returns "Untitled 3"
*
* If _name_ alone is available, returns that
**/
function getNextName(table, field, name)
{
var sql = "SELECT " + field + " FROM " + table + " WHERE " + field
+ " LIKE ? ORDER BY " + field;
var untitleds = Scholar.DB.columnQuery(sql, name + '%');
if (!untitleds || untitleds[0]!=name){
return name;
}
var i = 1;
var num = 2;
while (untitleds[i] && untitleds[i]==(name + ' ' + num)){
while (untitleds[i+1] && untitleds[i]==untitleds[i+1]){
Scholar.debug('Next ' + i + ' is ' + untitleds[i]);
i++;
}
i++;
num++;
}
return name + ' ' + num;
}
/////////////////////////////////////////////////////////////////