Skip to content

Commit

Permalink
Fix subjurisdiction searches
Browse files Browse the repository at this point in the history
  • Loading branch information
fbennett committed Feb 11, 2017
1 parent a56c230 commit 1255292
Showing 1 changed file with 57 additions and 46 deletions.
103 changes: 57 additions & 46 deletions chrome/content/zotero/bindings/itembox.xml
Expand Up @@ -1984,54 +1984,65 @@
<![CDATA[
return Zotero.spawn(function* () {
var valueElement = document.createElement("menulist");
if (!result) {
var newValue = node.value;
var jurisdictionID = this.item.getField('jurisdiction', true);
var newComment = Zotero.CachedJurisdictionData.courtIdFromName(jurisdictionID, newValue, true);
result = {val:newValue,comment:newComment};
if ("undefined" == typeof result.comment) {
result.comment = result.val;
//valueElement.setAttribute("style", "background: yellow;");
valueElement.classList.add('yellow');
}
} else {
var m = result.val.match(/^([^:]+):\s+(.*)$/);
if (m) {
var jurisdictionStub = m[1];
result.val = m[2];
var xpath = '*[local-name()="row"]/'
+ '*[local-name()="label" and @fieldname="jurisdiction"]/'
+ 'following-sibling::*[local-name()="hbox"]/'
+ '*[local-name()="menulist"]';
var jurisdictionNodes = document.evaluate(xpath, this._dynamicFields, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (jurisdictionNodes.snapshotLength) {
var jurisdictionNode = jurisdictionNodes.snapshotItem(0);
var jurisdictionName = jurisdictionNode.getAttribute('label');
jurisdictionName = jurisdictionName + "|" + jurisdictionStub;
var jurisdictionID = yield Zotero.CachedJurisdictionData.setJurisdictionByIdOrName(jurisdictionName);
yield this.setJurisdictionKey(jurisdictionID);
}
}
if (!result) {
// Used when saving hand-typed content of the field (triggred on blur)
var newValue = node.value;
var jurisdictionID = this.item.getField('jurisdiction', true);
var newComment = Zotero.CachedJurisdictionData.courtIdFromName(jurisdictionID, newValue, true);
result = {val:newValue,comment:newComment};
if ("undefined" == typeof result.comment) {
result.comment = result.val;
//valueElement.setAttribute("style", "background: yellow;");
valueElement.classList.add('yellow');
}
var tabindex = node.getAttribute("ztabindex");
valueElement.setAttribute("id","court-menu");
valueElement.setAttribute("ztabindex", tabindex);
valueElement.setAttribute("flex","1");
valueElement.classList.add("zotero-clicky");
valueElement.setAttribute("onclick","document.getBindingParent(this).showCourtEditor(this);");
valueElement.setAttribute("value",result.comment);
valueElement.setAttribute("label",result.val);
valueElement.setAttribute("tooltiptext",result.val);
node.parentNode.replaceChild(valueElement,node);
this.item.setField("court",result.comment);
this.item.saveTx();
if (this._tabDirection) {
var focusBox = this._dynamicFields;
this._focusNextField(focusBox, this._lastTabIndex, this._tabDirection == -1);
} else {
// Used to adjust values when SAYT result from a subjurisdiction is selected.
var m = result.val.match(/^([^:]+):\s+(.*)$/);
if (m) {
// Get the subjurisdiction element to add to the current jurisdiction
var jurisdictionStub = m[1];
result.val = m[2];
var xpath = '*[local-name()="row"]/'
+ '*[local-name()="label" and @fieldname="jurisdiction"]/'
+ 'following-sibling::*[local-name()="hbox"]/'
+ '*[local-name()="menulist"]';
var jurisdictionNodes = document.evaluate(xpath, this._dynamicFields, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (jurisdictionNodes.snapshotLength) {
// Get the currently displayed name value
var jurisdictionNode = jurisdictionNodes.snapshotItem(0);
var jurisdictionName = jurisdictionNode.getAttribute('label');
// Get the canonical long-form country name (awkward b/c I didn't plan for this)
var countryID = jurisdictionNode.getAttribute('value').split(":").slice(0,1)[0];
yield Zotero.CachedJurisdictionData.setJurisdictionByIdOrName(countryID);
var countryName = Zotero.CachedJurisdictionData.jurisdictionNameFromId(countryID).split("|")[0];
// Splice the name together.
jurisdictionName = countryName + "|" + jurisdictionName + "|" + jurisdictionStub;
// Get the ID from the full (searchable) jurisdiction name
var jurisdictionID = yield Zotero.CachedJurisdictionData.setJurisdictionByIdOrName(jurisdictionName);
// Set the ID in the record, and update the UI
yield this.setJurisdictionKey(jurisdictionID);
}
}
}, this);
}
var tabindex = node.getAttribute("ztabindex");
valueElement.setAttribute("id","court-menu");
valueElement.setAttribute("ztabindex", tabindex);
valueElement.setAttribute("flex","1");
valueElement.classList.add("zotero-clicky");
valueElement.setAttribute("onclick","document.getBindingParent(this).showCourtEditor(this);");
valueElement.setAttribute("value",result.comment);
valueElement.setAttribute("label",result.val);
valueElement.setAttribute("tooltiptext",result.val);
node.parentNode.replaceChild(valueElement,node);
this.item.setField("court",result.comment);
this.item.saveTx();
if (this._tabDirection) {
var focusBox = this._dynamicFields;
this._focusNextField(focusBox, this._lastTabIndex, this._tabDirection == -1);
}
}, this);
]]>
</body>
</method>
Expand Down

0 comments on commit 1255292

Please sign in to comment.