Update display title after item edit

This commit is contained in:
Dan Stillman 2016-03-11 04:50:55 -05:00
parent f310c39162
commit 394aa8dded
4 changed files with 121 additions and 112 deletions

View File

@ -849,6 +849,105 @@ Zotero.Item.prototype.getDisplayTitle = function (includeAuthorAndDate) {
}
/**
* Update the generated display title from the loaded data
*/
Zotero.Item.prototype.updateDisplayTitle = function () {
var title = this.getField('title', false, true);
var itemTypeID = this.itemTypeID;
var itemTypeName = Zotero.ItemTypes.getName(itemTypeID);
if (title === "" && (itemTypeID == 8 || itemTypeID == 10)) { // 'letter' and 'interview' itemTypeIDs
var creatorsData = this.getCreators();
var authors = [];
var participants = [];
for (let i=0; i<creatorsData.length; i++) {
let creatorData = creatorsData[i];
let creatorTypeID = creatorsData[i].creatorTypeID;
if ((itemTypeID == 8 && creatorTypeID == 16) || // 'letter'
(itemTypeID == 10 && creatorTypeID == 7)) { // 'interview'
participants.push(creatorData);
}
else if ((itemTypeID == 8 && creatorTypeID == 1) || // 'letter'/'author'
(itemTypeID == 10 && creatorTypeID == 6)) { // 'interview'/'interviewee'
authors.push(creatorData);
}
}
var strParts = [];
if (participants.length > 0) {
let names = [];
let max = Math.min(4, participants.length);
for (let i=0; i<max; i++) {
names.push(
participants[i].name !== undefined
? participants[i].name
: participants[i].lastName
);
}
switch (names.length) {
case 1:
var str = 'oneParticipant';
break;
case 2:
var str = 'twoParticipants';
break;
case 3:
var str = 'threeParticipants';
break;
default:
var str = 'manyParticipants';
}
strParts.push(Zotero.getString('pane.items.' + itemTypeName + '.' + str, names));
}
else {
strParts.push(Zotero.ItemTypes.getLocalizedString(itemTypeID));
}
title = '[' + strParts.join('; ') + ']';
}
else if (itemTypeID == 17) { // 'case' itemTypeID
if (title) { // common law cases always have case names
var reporter = this.getField('reporter');
if (reporter) {
title = title + ' (' + reporter + ')';
} else {
var court = this.getField('court');
if (court) {
title = title + ' (' + court + ')';
}
}
}
else { // civil law cases have only shortTitle as case name
var strParts = [];
var caseinfo = "";
var part = this.getField('court');
if (part) {
strParts.push(part);
}
part = Zotero.Date.multipartToSQL(this.getField('date', true, true));
if (part) {
strParts.push(part);
}
var creatorData = this.getCreator(0);
if (creatorData && creatorData.creatorTypeID === 1) { // author
strParts.push(creatorData.lastName);
}
title = '[' + strParts.join(', ') + ']';
}
}
this._displayTitle = title;
};
/*
* Returns the number of creators for this item
*/
@ -1836,7 +1935,9 @@ Zotero.Item.prototype.setNote = function(text) {
this._hasNote = text !== '';
this._noteText = text;
this._noteTitle = Zotero.Notes.noteToTitle(text);
this._displayTitle = this._noteTitle;
if (this.isNote()) {
this._displayTitle = this._noteTitle;
}
this._markFieldChange('note', oldText);
this._changed.note = true;
@ -4072,108 +4173,6 @@ Zotero.Item.prototype.toResponseJSON = function (options = {}) {
//
//////////////////////////////////////////////////////////////////////////////
/*
* Load in the field data from the database
*/
Zotero.Item.prototype.loadDisplayTitle = Zotero.Promise.coroutine(function* (reload) {
if (this._displayTitle !== null && !reload) {
return;
}
var title = this.getField('title', false, true);
var itemTypeID = this.itemTypeID;
var itemTypeName = Zotero.ItemTypes.getName(itemTypeID);
if (title === "" && (itemTypeID == 8 || itemTypeID == 10)) { // 'letter' and 'interview' itemTypeIDs
var creatorsData = this.getCreators();
var authors = [];
var participants = [];
for (let i=0; i<creatorsData.length; i++) {
let creatorData = creatorsData[i];
let creatorTypeID = creatorsData[i].creatorTypeID;
if ((itemTypeID == 8 && creatorTypeID == 16) || // 'letter'
(itemTypeID == 10 && creatorTypeID == 7)) { // 'interview'
participants.push(creatorData);
}
else if ((itemTypeID == 8 && creatorTypeID == 1) || // 'letter'/'author'
(itemTypeID == 10 && creatorTypeID == 6)) { // 'interview'/'interviewee'
authors.push(creatorData);
}
}
var strParts = [];
if (participants.length > 0) {
let names = [];
let max = Math.min(4, participants.length);
for (let i=0; i<max; i++) {
names.push(
participants[i].name !== undefined
? participants[i].name
: participants[i].lastName
);
}
switch (names.length) {
case 1:
var str = 'oneParticipant';
break;
case 2:
var str = 'twoParticipants';
break;
case 3:
var str = 'threeParticipants';
break;
default:
var str = 'manyParticipants';
}
strParts.push(Zotero.getString('pane.items.' + itemTypeName + '.' + str, names));
}
else {
strParts.push(Zotero.ItemTypes.getLocalizedString(itemTypeID));
}
title = '[' + strParts.join('; ') + ']';
}
else if (itemTypeID == 17) { // 'case' itemTypeID
if (title) { // common law cases always have case names
var reporter = this.getField('reporter');
if (reporter) {
title = title + ' (' + reporter + ')';
} else {
var court = this.getField('court');
if (court) {
title = title + ' (' + court + ')';
}
}
}
else { // civil law cases have only shortTitle as case name
var strParts = [];
var caseinfo = "";
var part = this.getField('court');
if (part) {
strParts.push(part);
}
part = Zotero.Date.multipartToSQL(this.getField('date', true, true));
if (part) {
strParts.push(part);
}
var creatorData = this.getCreator(0);
if (creatorData && creatorData.creatorTypeID === 1) { // author
strParts.push(creatorData.lastName);
}
title = '[' + strParts.join(', ') + ']';
}
}
return this._displayTitle = title;
});
/**
* Return an item in the specified library equivalent to this item

View File

@ -287,7 +287,6 @@ Zotero.Items = function() {
);
// If 'title' is one of the fields, load in display titles (note titles, letter titles...)
var titleFieldID = Zotero.ItemFields.getID('title');
// Note titles
@ -326,7 +325,7 @@ Zotero.Items = function() {
item._clearChanged('itemData');
// Display titles
yield item.loadDisplayTitle()
item.updateDisplayTitle()
}
});

View File

@ -45,6 +45,17 @@ describe("Zotero.ItemTreeView", function() {
});
})
describe("#getCellText()", function () {
it("should return new value after edit", function* () {
var str = Zotero.Utilities.randomString();
var item = yield createDataObject('item', { title: str });
var row = itemsView.getRowIndexByID(item.id);
assert.equal(itemsView.getCellText(row, { id: 'zotero-items-column-title' }), str);
yield modifyDataObject(item);
assert.notEqual(itemsView.getCellText(row, { id: 'zotero-items-column-title' }), str);
})
})
describe("#notify()", function () {
beforeEach(function () {
sinon.spy(win.ZoteroPane, "itemSelected");

View File

@ -64,13 +64,13 @@ function saveItemsThroughTranslator(translatorType, items) {
* Convert an array of items to an object in which they are indexed by
* their display titles
*/
var itemsArrayToObject = Zotero.Promise.coroutine(function* itemsArrayToObject(items) {
function itemsArrayToObject(items) {
var obj = {};
for (let item of items) {
obj[yield item.loadDisplayTitle(true)] = item;
obj[item.getDisplayTitle()] = item;
}
return obj;
});
}
const TEST_TAGS = [
"manual tag as string",
@ -223,7 +223,7 @@ describe("Zotero.Translate", function() {
}
];
let newItems = yield itemsArrayToObject(yield saveItemsThroughTranslator("import", myItems));
let newItems = itemsArrayToObject(yield saveItemsThroughTranslator("import", myItems));
let noteIDs = newItems["Test Item"].getNotes();
let note1 = yield Zotero.Items.getAsync(noteIDs[0]);
assert.equal(Zotero.ItemTypes.getName(note1.itemTypeID), "note");
@ -261,7 +261,7 @@ describe("Zotero.Translate", function() {
'}'));
let newItems = yield translate.translate();
assert.equal(newItems.length, 3);
newItems = yield itemsArrayToObject(newItems);
newItems = itemsArrayToObject(newItems);
assert.equal(newItems["Not in Collection"].getCollections().length, 0);
let parentCollection = newItems["In Parent Collection"].getCollections();
@ -313,7 +313,7 @@ describe("Zotero.Translate", function() {
"attachments":childAttachments
});
let newItems = yield itemsArrayToObject(yield saveItemsThroughTranslator("import", myItems));
let newItems = itemsArrayToObject(yield saveItemsThroughTranslator("import", myItems));
let containedAttachments = yield Zotero.Items.getAsync(newItems["Container Item"].getAttachments());
assert.equal(containedAttachments.length, 3);
@ -447,7 +447,7 @@ describe("Zotero.Translate", function() {
let newItems = yield saveItemsThroughTranslator("web", myItems);
assert.equal(newItems.length, 1);
let containedAttachments = yield itemsArrayToObject(yield Zotero.Items.getAsync(newItems[0].getAttachments()));
let containedAttachments = itemsArrayToObject(yield Zotero.Items.getAsync(newItems[0].getAttachments()));
let link = containedAttachments["Link to zotero.org"];
assert.equal(link.getField("url"), "http://www.zotero.org/");