Ticket #1278: 02-positioning.patch

File 02-positioning.patch, 10.1 KB (added by fbennett, 8 years ago)
  • chrome/content/zotero/xpcom/integration.js

    Only in zotero-trunk/chrome/content/zotero: addCitationDialog.js.orig
    Only in zotero-trunk/chrome/content/zotero: addCitationDialog.js.rej
    Only in zotero-trunk/chrome/content/zotero/xpcom: csl.js.rej
    diff -r -u zotero-trunk.orig/chrome/content/zotero/xpcom/integration.js zotero-trunk/chrome/content/zotero/xpcom/integration.js
    old new  
    678678                                session.addCitation(vars[i], vars[i+1]); 
    679679                        } 
    680680                } 
    681                  
     681 
    682682                session.updateItemSet(); 
    683683                 
    684684                if(editCitationIndex) {  
     
    727727                 
    728728                return output; 
    729729        } 
    730          
     730 
    731731        /* 
    732732         * restores a session, given all citations 
    733733         * ACCEPTS: version, documentData, styleID, use-endnotes, use-bookmarks(, fieldIndex, fieldName)+ 
     
    827827                if(versionParts.length != 3 || versionParts[1] != COMPAT_API_VERSION) return false; 
    828828                return true; 
    829829        } 
     830 
    830831} 
    831832 
    832833/* 
     
    871872Zotero.Integration.Session.prototype.resetRequest = function() { 
    872873        this.citationsByItemID = new Object(); 
    873874        this.citationsByIndex = new Array(); 
     875        this.firstRef = new Object(); 
     876        this.lastRef = new Object(); 
    874877         
    875878        this.haveMissing = false; 
    876879        this.regenerateAll = false; 
     
    930933 * gets a Zotero.CSL.Citation object given a field name 
    931934 */ 
    932935Zotero.Integration.Session.prototype.addCitation = function(index, arg) { 
     936 
    933937        var index = parseInt(index, 10); 
    934938         
    935939        if(typeof(arg) == "string") {   // text field 
     
    964968                this.updateIndices[index] = true; 
    965969                this.haveMissing = true; 
    966970        } 
    967          
     971 
    968972        citation.properties.index = index; 
    969973        this.citationsByIndex[index] = citation; 
    970974} 
     
    11971201 * sets position attribute on a citation 
    11981202 */ 
    11991203Zotero.Integration.Session.prototype.getCitationPositions = function(citation, update) { 
    1200         for(var previousIndex = citation.properties.index-1; 
    1201                 previousIndex != -1 
    1202                         && (!this.citationsByIndex[previousIndex] 
    1203                         || this.citationsByIndex[previousIndex].properties["delete"]);  
     1204        for(var previousIndex = citation.properties.index-1;  
     1205        previousIndex != -1 
     1206            && (!this.citationsByIndex[previousIndex] 
     1207                || this.citationsByIndex[previousIndex].properties["delete"]);  
    12041208                previousIndex--) {} 
    1205         var previousCitation = (previousIndex == -1 ? false : this.citationsByIndex[previousIndex]); 
     1209    var previousCitation = (previousIndex == -1 ? false : this.citationsByIndex[previousIndex]); 
     1210    // if one source is the same as the last, use ibid 
     1211    // 
     1212    // Case 1: source in previous citation 
     1213    // (1) Threshold conditions 
     1214    //     (a) there must be a previous citation with one item 
     1215    //     (b) this item must be the first in this citation 
     1216    //     (c) the previous citation must contain a reference to the same item ... 
     1217    // Case 2: immediately preceding source in this citation 
     1218    // (1) Threshold conditions 
     1219    //     (a) there must be an imediately preceding reference to  the 
     1220    //         same item in this citation 
     1221    // Evaluation 
     1222    //     (a) if the previous citation had no locator and this citation 
     1223    //         has one, use ibid+pages 
     1224    //     (b) if the previous citation had no locator and this citation 
     1225    //         also has none, use ibid 
     1226    //     (c) if the previous citation had a locator (page number, etc.)  
     1227    //         and this citation has a locator that is identical, use ibid 
     1228    //     (d) if the previous citation had a locator, and this citation 
     1229    //         has one that differs, use ibid+pages 
     1230    //     (e) if the previous citation had a locator and this citation 
     1231    //         has none, use subsequent 
     1232    //     (f) if the current and previous citations are not the same, 
     1233    //         set as FIRST or SUBSEQUENT, as appropriate. 
     1234    // 
     1235    // For Case 1 
     1236    var curr = citation.citationItems[0]; 
     1237        curr.thisRef = citation.properties.index+1; 
     1238    if(          
     1239       previousCitation  
     1240       && previousCitation.citationItems.length == 1 
     1241       && citation.citationItems[0].itemID == previousCitation.citationItems[0].itemID ) { 
     1242                var prev = previousCitation.citationItems[0]; 
     1243                var newPosition = this.checkIbidOrSubsequent(prev, curr, citation); 
     1244    } else { 
     1245                // check outside this note if necessary 
     1246                var newPosition = this.checkFirstOrSubsequent(curr, citation ); 
     1247    } 
     1248    this.updateCitePosition (curr, newPosition, citation, update); 
     1249    citation.citationItems[0].position = newPosition; 
     1250    this.checkCiteContext (citation.citationItems[0], newPosition); 
     1251    // 
     1252    // For Case 2 
     1253        // step through remaining cites in this citation 
     1254           for ( i = 1; i < citation.citationItems.length; i++) { 
     1255                ////////////////////positionIndex++; 
     1256                // step through possible preceding cites within same note, from back to front 
     1257                var curr = citation.citationItems[i]; 
     1258                curr.thisRef = citation.properties.index+1; 
     1259                var newPosition = undefined; 
     1260                var prev = citation.citationItems[i-1]; 
     1261                if ( curr.itemID == prev.itemID ) { 
     1262                        // check immediately preceding cite in this note 
     1263                        newPosition = this.checkIbidOrSubsequent(prev, curr, citation); 
     1264                } else { 
     1265                        // check other preceding cites 
     1266                        newPosition = this.checkFirstOrSubsequent(curr, citation ); 
     1267                } 
     1268                this.updateCitePosition (curr, newPosition, citation, true); 
     1269                citation.citationItems[i].position = newPosition; 
     1270                this.checkCiteContext (citation.citationItems[i], newPosition); 
     1271        } 
     1272} 
     1273 
     1274/* 
     1275 * evaluates position of qualifying cites 
     1276 */ 
     1277 
     1278Zotero.Integration.Session.prototype.checkIbidOrSubsequent = function(prev, curr, citation) { 
     1279    var newPosition; 
    12061280         
    1207         // if only one source, and it's the same as the last, use ibid 
    1208         if(             // there must be a previous citation with one item, and this citation 
    1209                         // may only have one item 
    1210                         previousCitation && citation.citationItems.length == 1 
    1211                         && previousCitation.citationItems.length == 1 
    1212                         // the previous citation must have been a citation of the same item 
    1213                         && citation.citationItems[0].item == previousCitation.citationItems[0].item 
    1214                         // and if the previous citation had a locator (page number, etc.)  
    1215                         // then this citation must have a locator, or else we should do the  
    1216                         // full citation (see Chicago Manual of Style) 
    1217                         && (!previousCitation.citationItems[0].locator || citation.citationItems[0].locator)) { 
    1218                 // use ibid, but check whether to use ibid+pages 
    1219                 var newPosition = (citation.citationItems[0].locator == previousCitation.citationItems[0].locator 
    1220                         && citation.citationItems[0].locatorType == previousCitation.citationItems[0].locatorType 
    1221                         ? Zotero.CSL.POSITION_IBID : Zotero.CSL.POSITION_IBID_WITH_LOCATOR); 
    1222                 // update if desired 
    1223                 if(update && (citation.citationItems[0].position || newPosition) && citation.citationItems[0].position != newPosition) { 
    1224                         this.updateIndices[citation.properties.index] = true; 
     1281        curr.distanceToLastRef = citation.properties.index+1 - this.lastRef[curr.itemID]; 
     1282        curr.firstRef = this.firstRef[curr.itemID]; 
     1283        this.lastRef[curr.itemID] = citation.properties.index+1; 
     1284    if (!prev.locator) { 
     1285                if (curr.locator) { 
     1286                        newPosition = Zotero.CSL.POSITION_IBID_WITH_LOCATOR; 
     1287                } else { 
     1288                        newPosition = Zotero.CSL.POSITION_IBID; 
    12251289                } 
    1226                 citation.citationItems[0].position = newPosition; 
    1227         } else { 
    1228                 // loop through to see which are first citations 
    1229                 for(var i=0; i<citation.citationItems.length; i++) { 
    1230                         var citationItem = citation.citationItems[i]; 
    1231                         var newPosition = (!this.citationsByItemID[citationItem.itemID] 
    1232                                         || this.citationsByItemID[citationItem.itemID][0].properties.index >= citation.properties.index 
    1233                                 ? Zotero.CSL.POSITION_FIRST : Zotero.CSL.POSITION_SUBSEQUENT); 
    1234                          
    1235                         // update if desired 
    1236                         if(update && (citation.citationItems[i].position || newPosition) && citation.citationItems[i].position != newPosition) { 
    1237                                 this.updateIndices[citation.properties.index] = true; 
    1238                         } 
    1239                         citation.citationItems[i].position = newPosition; 
     1290    } else { 
     1291                if (prev.locator == curr.locator) { 
     1292                        newPosition = Zotero.CSL.POSITION_IBID; 
     1293                } else if (curr.locator) { 
     1294                        newPosition = Zotero.CSL.POSITION_IBID_WITH_LOCATOR; 
     1295                } else { 
     1296                        newPosition = Zotero.CSL.POSITION_SUBSEQUENT; 
    12401297                } 
    1241         } 
     1298    } 
     1299    return newPosition; 
    12421300} 
    12431301 
    12441302/* 
     1303 * checks whether a cite is a first or subsequent reference 
     1304 */ 
     1305Zotero.Integration.Session.prototype.checkFirstOrSubsequent = function(curr, citation ) { 
     1306    var newPosition; 
     1307 
     1308    if (!this.citationsByItemID[curr.itemID]) { 
     1309                curr.distanceToLastRef = 0; 
     1310                curr.firstRef = citation.properties.index+1; 
     1311                this.firstRef[curr.itemID] = citation.properties.index+1; 
     1312                this.lastRef[curr.itemID] = citation.properties.index+1; 
     1313                newPosition = Zotero.CSL.POSITION_FIRST; 
     1314        } else if (this.citationsByItemID[curr.itemID][0].properties.index >= citation.properties.index) { 
     1315                curr.distanceToLastRef = 0; 
     1316                curr.firstRef = citation.properties.index+1; 
     1317                this.firstRef[curr.itemID] = citation.properties.index+1; 
     1318                this.lastRef[curr.itemID] = citation.properties.index+1; 
     1319                newPosition = Zotero.CSL.POSITION_FIRST; 
     1320    } else { 
     1321                curr.distanceToLastRef = citation.properties.index+1 - this.lastRef[curr.itemID]; 
     1322                curr.firstRef = this.firstRef[curr.itemID]; 
     1323                this.lastRef[curr.itemID] = citation.properties.index+1; 
     1324                newPosition = Zotero.CSL.POSITION_SUBSEQUENT; 
     1325    } 
     1326    return newPosition; 
     1327} 
     1328 
     1329/* 
     1330 * checks if cite has a prefix with terminal punctuation 
     1331 */ 
     1332Zotero.Integration.Session.prototype.checkCiteContext = function(citationItem, newPosition) { 
     1333    if (citationItem.prefix && citationItem.prefix.match(/.*[:;.?!][\s\"\"\'\']*$/)) { 
     1334                citationItem.prefixPunctuation = true; 
     1335    } else { 
     1336                citationItem.prefixPunctuation = false; 
     1337    } 
     1338} 
     1339     
     1340/* 
     1341 * updates indexes 
     1342 * 
     1343 */ 
     1344Zotero.Integration.Session.prototype.updateCitePosition = function (curr, newPosition, citation, update) { 
     1345    if(update && (curr.position || newPosition) && curr.position != newPosition) { 
     1346                this.updateIndices[citation.properties.index] = true; 
     1347    } 
     1348  } 
     1349 
     1350/* 
    12451351 * marks citations for update, where necessary 
    12461352 */ 
    12471353Zotero.Integration.Session.prototype.updateCitations = function(toIndex) {