Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions readthedocsext/theme/static/readthedocsext/theme/js/site.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
<readthedocs-item-docs {% if not object.has_good_build %}disabled{% endif %}
label="{% blocktrans with name=object.name %}View documentation for project {{ name }}{% endblocktrans %}">
</readthedocs-item-docs>
<button class="ui dropdown button">
<i class="fa-solid fa-ellipsis icon"></i>
<readthedocs-menu-project-admin class="menu" url-settings="{% url "projects_edit" object.slug %}">
</readthedocs-menu-project-admin>
</button>
{% blocktrans trimmed with name=object.name asvar label_configure %}
Configure project {{ name }}
{% endblocktrans %}
<readthedocs-item-project-admin label="{{ label_configure }}"
url-settings="{% url "projects_edit" object.slug %}">
</readthedocs-item-project-admin>
</div>
</readthedocs-api>
{% endblock list_item_right_menu %}
Expand Down
46 changes: 46 additions & 0 deletions src/js/modules/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,52 @@ export class ItemDocsElement extends APIConsumerElement {
}
customElements.define("readthedocs-item-docs", ItemDocsElement);

export class ItemProjectAdminElement extends APIConsumerElement {
static properties = {
...APIConsumerElement.properties,
urlSettings: { type: String, attribute: "url-settings" },
};

handleClick(event) {
const isAdmin = this.data?.permissions?.admin;
if (this.disabled || isAdmin === false) {
event.preventDefault();
event.stopPropagation();
return;
}

this.queueEvent(event);
}

render() {
const isAdmin = this.data?.permissions?.admin;
const isDisabled = this.disabled || isAdmin === false;
const urlSettings = this.data?.urls?.settings || this.urlSettings || "#";
const label = this.label || msg(`Configure project`);

return html`
<a
class="ui button ${classMap({
disabled: isDisabled,
loading: !isDisabled && this.state === States.LOADING,
})}"
href=${urlSettings}
@click=${this.handleClick}
data-content="${label}"
aria-label="${label}"
aria-disabled="${isDisabled}"
tabindex=${isDisabled ? -1 : 0}
>
<i class="fa-duotone fa-wrench icon"></i>
</a>
`;
}
}
customElements.define(
"readthedocs-item-project-admin",
ItemProjectAdminElement,
);

Comment on lines +350 to +395
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you implemented this as a new class instead of reusing the existing class? Pretty sure this is the only view the menu item is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code doesn't matter here since Eric was trying to show the idea only. He wrote:

Would love review on the concept rather than the code itself, since I haven't dived in there.

export class ItemDownloadsElement extends APIConsumerElement {
render() {
let label = this.label || msg(`Offline formats`);
Expand Down
52 changes: 51 additions & 1 deletion src/js/tests/modules/menus.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
<readthedocs-api url="#">
<readthedocs-item-docs>
</readthedocs-item-docs>
<readthedocs-item-project-admin url-settings="https://example.com/settings/">
</readthedocs-item-project-admin>
</readthedocs-api>

<script type="module">
import { expect, elementUpdated } from "@open-wc/testing";
import { runTests } from "@web/test-runner-mocha";

import { ItemDocsElement, APIProviderElement } from "../../modules/menus";
import {
ItemDocsElement,
ItemProjectAdminElement,
APIProviderElement,
} from "../../modules/menus";

runTests(async () => {
describe("API menu", () => {
Expand Down Expand Up @@ -42,6 +48,50 @@
expect(consumer).dom.to.be.not.null;
expect(element.href).to.equal("https://example.com/#test");
});

it("project admin button enables for admins", async () => {
const provider = document.querySelector("readthedocs-api");
const consumer = document.querySelector(
"readthedocs-item-project-admin",
);

const value = {
permissions: {
admin: true,
},
urls: {
settings: "https://example.com/#settings",
},
};
provider._providerData.setValue(value);
await elementUpdated(consumer);
const element = consumer.querySelector("a");

expect(element.href).to.equal("https://example.com/#settings");
expect(element.classList.contains("disabled")).to.equal(false);
});

it("project admin button disables for non-admins", async () => {
const provider = document.querySelector("readthedocs-api");
const consumer = document.querySelector(
"readthedocs-item-project-admin",
);

const value = {
permissions: {
admin: false,
},
urls: {
settings: "https://example.com/#settings",
},
};
provider._providerData.setValue(value);
await elementUpdated(consumer);
const element = consumer.querySelector("a");

expect(element.classList.contains("disabled")).to.equal(true);
expect(element.getAttribute("tabindex")).to.equal("-1");
});
});
});
</script>
Expand Down