Skip to content

Commit

Permalink
add: add multi rule mode
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Feb 18, 2021
1 parent adceb8d commit 72f1f11
Show file tree
Hide file tree
Showing 14 changed files with 569 additions and 180 deletions.
Binary file removed builds/zotero-tag-0.0.3a.xpi
Binary file not shown.
Binary file added builds/zotero-tag-0.0.4.xpi
Binary file not shown.
80 changes: 59 additions & 21 deletions chrome/content/overlay.xul
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,75 @@
type="application/x-javascript"
src="chrome://zoterotag/content/scripts/zoterotag.js"/>

<commandset id="mainCommandSet">
<command id="cmd_zotero_tag_update_all" oncommand="Zotero.ZoteroTag.updateAll();"/>
</commandset>
<commandset id="mainCommandSet">
<command id="cmd_zotero_tag_update_all" oncommand="Zotero.ZoteroTag.updateAll();"/>
</commandset>

<menupopup id="menu_ToolsPopup">
<menuseparator/>
<menuitem
id="zotero-zoterotag-updateall"
label="&zotero.zoterotag.updateAll.label;"
command="cmd_zotero_tag_update_all"
class="menuitem-iconic zotero-zoterotag-icon"/>
</menupopup>
<!-- <menupopup id="menu-tools-popup">
<menuseparator/>
<menuitem
id="zotero-zoterotag-updateall"
label="&zotero.zoterotag.updateAll.label;"
command="cmd_zotero_tag_update_all"
class="menuitem-iconic zotero-zoterotag-icon"/>
</menupopup> -->


<popup id="zotero-collectionmenu">
<menuseparator/>
<menuseparator/>
<menuitem
id="zotero-collectionmenu-zoterotag-add"
label="&zotero.zoterotag.add.label;&zotero.zoterotag.rules.group.default;"
oncommand="Zotero.ZoteroTag.updateSelectedEntity('add', 1)"
class="menuitem-iconic zotero-zoterotag-add-icon"/>
<menuitem
id="zotero-collectionmenu-zoterotag"
label="&zotero.zoterotag.updateCollection.label;"
oncommand="Zotero.ZoteroTag.updateSelectedEntity()"
class="menuitem-iconic zotero-zoterotag-icon"/>
id="zotero-collectionmenu-zoterotag-remove"
label="&zotero.zoterotag.remove.label;&zotero.zoterotag.rules.group.default;"
oncommand="Zotero.ZoteroTag.updateSelectedEntity('remove', 1)"
class="menuitem-iconic zotero-zoterotag-remove-icon"/>
<menu id="zotero-collectionmenu-zoterotag-submenu"
label="&zotero.zoterotag.submenu.label;"
class="menu-iconic zotero-zoterotag-icon">
<menupopup id="zotero-collectionmenu-zoterotag-submenu-popup">
<menuitem label="&zotero.zoterotag.add.label; 2" oncommand="Zotero.ZoteroTag.updateSelectedEntity('add', 2);"/>
<menuitem label="&zotero.zoterotag.remove.label; 2" oncommand="Zotero.ZoteroTag.updateSelectedEntity('remove', 2);"/>
<menuitem label="&zotero.zoterotag.add.label; 3" oncommand="Zotero.ZoteroTag.updateSelectedEntity('add', 3);"/>
<menuitem label="&zotero.zoterotag.remove.label; 3" oncommand="Zotero.ZoteroTag.updateSelectedEntity('remove', 3);"/>
<menuitem label="&zotero.zoterotag.add.label; auto" oncommand="Zotero.ZoteroTag.updateSelectedEntity('add', 0);"/>
<menuitem label="&zotero.zoterotag.remove.label; auto" oncommand="Zotero.ZoteroTag.updateSelectedEntity('remove', 0);"/>
<menuitem label="&zotero.zoterotag.add.label;&zotero.zoterotag.rules.group.all;" oncommand="Zotero.ZoteroTag.updateSelectedEntity('add', -1);"/>
<menuitem label="&zotero.zoterotag.remove.label;&zotero.zoterotag.rules.group.all;" oncommand="Zotero.ZoteroTag.updateSelectedEntity('remove', -1);"/>
</menupopup>
</menu>
</popup>

<popup
id="zotero-itemmenu"
onpopupshowing="ZoteroPane.buildItemContextMenu();">
<menuseparator/>
<menuseparator/>
<menuitem
id="zotero-itemmenu-zoterotag-add"
label="&zotero.zoterotag.add.label;&zotero.zoterotag.rules.group.default;"
oncommand="Zotero.ZoteroTag.updateSelectedItems('add', 1)"
class="menuitem-iconic zotero-zoterotag-add-icon"/>
<menuitem
id="zotero-itemmenu-zoterotag"
label="&zotero.zoterotag.update.label;"
oncommand="Zotero.ZoteroTag.updateSelectedItems()"
class="menuitem-iconic zotero-zoterotag-icon"/>
id="zotero-itemmenu-zoterotag-remove"
label="&zotero.zoterotag.remove.label;&zotero.zoterotag.rules.group.default;"
oncommand="Zotero.ZoteroTag.updateSelectedItems('remove', 1)"
class="menuitem-iconic zotero-zoterotag-remove-icon"/>
<menu id="zotero-itemmenu-zoterotag-submenu"
label="&zotero.zoterotag.submenu.label;"
class="menu-iconic zotero-zoterotag-icon">
<menupopup id="zotero-itemmenu-zoterotag-submenu-popup">
<menuitem label="&zotero.zoterotag.add.label; 2" oncommand="Zotero.ZoteroTag.updateSelectedItems('add', 2);"/>
<menuitem label="&zotero.zoterotag.remove.label; 2" oncommand="Zotero.ZoteroTag.updateSelectedItems('remove', 2);"/>
<menuitem label="&zotero.zoterotag.add.label; 3" oncommand="Zotero.ZoteroTag.updateSelectedItems('add', 3);"/>
<menuitem label="&zotero.zoterotag.remove.label; 3" oncommand="Zotero.ZoteroTag.updateSelectedItems('remove', 3);"/>
<menuitem label="&zotero.zoterotag.add.label; auto" oncommand="Zotero.ZoteroTag.updateSelectedItems('add', 0);"/>
<menuitem label="&zotero.zoterotag.remove.label; auto" oncommand="Zotero.ZoteroTag.updateSelectedItems('remove', 0);"/>
<menuitem label="&zotero.zoterotag.add.label;&zotero.zoterotag.rules.group.all;" oncommand="Zotero.ZoteroTag.updateSelectedItems('add', -1);"/>
<menuitem label="&zotero.zoterotag.remove.label;&zotero.zoterotag.rules.group.all;" oncommand="Zotero.ZoteroTag.updateSelectedItems('remove', -1);"/>
</menupopup>
</menu>
</popup>
</overlay>
68 changes: 52 additions & 16 deletions chrome/content/preferences.xul
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,60 @@
<preferences id="zotero-preferences-zoterotag">
<preference id="pref-zoterotag-automatic-add-tag" name="extensions.zotero.zoterotag.automatic_add_tag" type="bool"/>
<preference id="pref-zoterotag-tag-name" name="extensions.zotero.zoterotag.tag_name" type="string"/>
<preference id="pref-zoterotag-rules" name="extensions.zotero.zoterotag.rules" type="string"/>
</preferences>
<vbox flex="1">
<grid>
<columns>
<column/>
<column flex="1"/>
</columns>
<rows>
<row>
<label value="Automatic Add Tag"/>
<checkbox id="id-zoterotag-automatic-add-tag" preference="pref-zoterotag-automatic-add-tag"/>
</row>
<row>
<label value="Tags(split with comma【,】):"/>
<textbox id="id-zoterotag-tag-name" preference="pref-zoterotag-tag-name"/>
</row>
</rows>
</grid>
<label value="&zotero.zoterotag.rules.title;" style="font-size: large"/>
<label value="&zotero.zoterotag.rules.info;"/>
<listbox id="zoterotag-rules-listbox" flex="1">
<listhead>
<listheader label="&zotero.zoterotag.rules.listheader.id;"/>
<listheader label="&zotero.zoterotag.rules.listheader.tags;"/>
<listheader label="&zotero.zoterotag.rules.listheader.group;"/>
<listheader label="&zotero.zoterotag.rules.listheader.autoadd;"/>
<listheader label="&zotero.zoterotag.rules.listheader.operation;"/>
<!-- <listheader label="&zotero.zoterotag.rules.listheader.delete;"/> -->
</listhead>
<listcols>
<listcol/>
<listcol width="250"/>
<listcol/>
<listcol/>
<listcol/>
<!-- <listcol width="80"/> -->
</listcols>
<!-- <listitem id="zoterotag-rules-#" allowevents="true">
<listcell>
<label id="zoterotag-rules-#-id" value="#"/>
</listcell>
<listcell>
<textbox id="zoterotag-rules-#-tags"/>
</listcell>
<listcell>
<menulist id="zoterotag-rules-#-group">
<menupopup>
<menuitem value="1" label="1(&zotero.zoterotag.rules.group.default;)"/>
<menuitem value="2" label="2"/>
<menuitem value="3" label="3"/>
<menuitem value="0" label="&zotero.zoterotag.rules.group.other;"/>
</menupopup>
</menulist>
</listcell>
<listcell>
<checkbox id="zoterotag-rules-#-autoadd"/>
</listcell>
<listcell>
<button label="&zotero.zoterotag.rules.add;" tooltiptext="&zotero.zoterotag.rules.addtext;" oncommand="addRule()"/>
</listcell>
<listcell>
<button label="&zotero.zoterotag.rules.remove;" tooltiptext="&zotero.zoterotag.rules.removetext;" oncommand="" disabled="true"/>
</listcell>
</listitem> -->
</listbox>
<hbox style="display: block">
<button label="&zotero.zoterotag.rules.reset;" tooltiptext="&zotero.zoterotag.rules.resettext;" oncommand="resetPreferences()"/>
<label value="&zotero.zoterotag.rules.warningtext;" style="color: red"/>
</hbox>
</vbox>
</prefpane>
</prefwindow>
Expand Down
159 changes: 151 additions & 8 deletions chrome/content/scripts/preferences.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,151 @@
initPreferences = function() {
tag_name = Zotero.ZoteroTag.tag();
automatic_add_tag_bool = Zotero.ZoteroTag.automatic_add_tag();

// Apply setting to
document.getElementById('id-zoterotag-automatic-add-tag').checked = automatic_add_tag_bool
document.getElementById('id-zoterotag-tag-name').value = tag_name
}
initPreferences = function() {
// tag_name = Zotero.ZoteroTag.tag_name();
// automatic_add_tag_bool = Zotero.ZoteroTag.automatic_add_tag();

// // Apply setting to
// document.getElementById('id-zoterotag-automatic-add-tag').checked = automatic_add_tag_bool
// document.getElementById('id-zoterotag-tag-name').value = tag_name

let rules = Zotero.ZoteroTag.rules();
Zotero.debug("ZoteroTag: Initialize preferences.");
Zotero.debug(rules);
refreshPreferencesView(rules);
};

resetPreferences = function() {
Zotero.debug("ZoteroTag: Reset preferences.");
let rules = Zotero.ZoteroTag.resetRules();
Zotero.debug(rules);
refreshPreferencesView(rules);
Zotero.ZoteroTag.showProgressWindow('SUCCESS', 'ZoteroTag rules reset.')
}

addRule = function() {
Zotero.debug("ZoteroTag: Add rule.");
let rule = {};
rule.id = -1;
rule.tags = document.getElementById('zoterotag-rules-#-tags').value.replace(/\s/g,"").split(',');
rule.autoadd = document.getElementById('zoterotag-rules-#-autoadd').checked;
rule.group = document.getElementById('zoterotag-rules-#-group').value;
Zotero.debug(rule);
let rules = Zotero.ZoteroTag.addRule(rule);
refreshPreferencesView(rules);
Zotero.ZoteroTag.showProgressWindow('SUCCESS', 'New ZoteroTag rule modification saved.')
}

refreshRule = async function(id) {
Zotero.debug("ZoteroTag: Refresh rule.");
let rule = {};
rule.id = Number(id);
rule.tags = await document.getElementById(`zoterotag-rules-${id}-tags`).value.replace(/\s/g,"").split(',');
rule.autoadd = await document.getElementById(`zoterotag-rules-${id}-autoadd`).checked;
rule.group = await document.getElementById(`zoterotag-rules-${id}-group`).value;
Zotero.debug(rule);
let rules = Zotero.ZoteroTag.replaceRule(rule, id);
refreshPreferencesView(rules);
Zotero.ZoteroTag.showProgressWindow('SUCCESS', 'ZoteroTag rule modification updated.')
}

removeRule = function(id) {
Zotero.debug("ZoteroTag: Refresh rule.");
let rules = Zotero.ZoteroTag.removeRule(id);
refreshPreferencesView(rules);
Zotero.ZoteroTag.showProgressWindow('SUCCESS', 'ZoteroTag rule removed.')
}

refreshPreferencesView = function(rules) {
let listbox = document.getElementById("zoterotag-rules-listbox");
let e, es = document.getElementsByTagName("listitem");
while(es.length>0){
e = es[0];
e.parentElement.removeChild(e);
}
listbox.removeChild
for(let key in rules){
listbox.appendChild(creatRuleListElement(rules[key]));
}
listbox.appendChild(creatRuleBlankListElement());
// Zotero.ZoteroTag.showProgressWindow('SUCCESS', 'ZoteroTag preference view updated.')
};

creatRuleBlankListElement = function() {
let rule = {
'id': '#',
'tags': ['MODIFY HERE'],
'autoadd': false,
'color': 'red',
'group': 1,
'addcmd': 'addRule()',
'addtext': '➕',
'hideRemove': true,
};
return creatRuleListElement(rule);
}

creatRuleListElement = function(rule) {
let listIDHead = "zoterotag-rules";
let listitem, listcell, label, textbox, menulist, menupopup, menuitem, checkbox, button;

listitem = document.createElement("listitem");
listitem.setAttribute("id", `${listIDHead}-${rule.id}`);
listitem.setAttribute("allowevents", "true");

listcell = document.createElement("listcell");
label = document.createElement("label");
label.setAttribute("id", `${listIDHead}-${rule.id}-id`);
label.setAttribute("value", rule.id);
listcell.appendChild(label);
listitem.appendChild(listcell);

listcell = document.createElement("listcell");
textbox = document.createElement("textbox");
textbox.setAttribute("id", `${listIDHead}-${rule.id}-tags`);
textbox.setAttribute("value", `${rule.tags}`);
textbox.setAttribute("style", "width: 240px");
listcell.appendChild(textbox);
listitem.appendChild(listcell);

listcell = document.createElement("listcell");
menulist = document.createElement("menulist");
menulist.setAttribute("id", `${listIDHead}-${rule.id}-group`);
menupopup = document.createElement("menupopup");
let menuValueList = ["1", "2", "3", "0"];
let menuLabelList = ["1(default)", "2", "3", "other"];
for(let i=0; i<4; i++){
menuitem = document.createElement("menuitem");
menuitem.setAttribute("value", menuValueList[i]);
menuitem.setAttribute("label", menuLabelList[i]);
menupopup.appendChild(menuitem);
}
menulist.setAttribute("value", `${rule.group}`);
menulist.appendChild(menupopup);
listcell.appendChild(menulist);
listitem.appendChild(listcell);

listcell = document.createElement("listcell");
checkbox = document.createElement("checkbox");
checkbox.setAttribute("id", `${listIDHead}-${rule.id}-autoadd`);
checkbox.setAttribute("checked", `${rule.autoadd}`);
listcell.appendChild(checkbox);
listitem.appendChild(listcell);

listcell = document.createElement("listcell");
button = document.createElement("button");
button.setAttribute("label", rule.addtext?rule.addtext:"✅");
button.setAttribute("tooltiptext", "Add/Refresh Rule");
button.setAttribute("oncommand", rule.addcmd?rule.addcmd:`refreshRule("${rule.id}")`);
listcell.appendChild(button);
// listitem.appendChild(listcell);

// listcell = document.createElement("listcell");
if(!rule.hideRemove){
button = document.createElement("button");
button.setAttribute("label", rule.removetext?rule.removetext:"⛔");
button.setAttribute("tooltiptext", "Remove Rule");
button.setAttribute("oncommand", rule.removecmd?rule.removecmd:`removeRule("${rule.id}")`);
listcell.appendChild(button);
}
listitem.appendChild(listcell);

return listitem;
};
Loading

0 comments on commit 72f1f11

Please sign in to comment.