Debian Codegraph @

addons/web/static/lib/hoot/ui/hoot_buttons.js

raw
1 /** @odoo-module */
2
3 import { Component, useState, xml } from "@odoo/owl";
4 import { refresh, subscribeToURLParams } from "../core/url";
5 import { STORAGE, storageSet } from "../hoot_utils";
6 import { HootLink } from "./hoot_link";
7
8 /**
9 * @typedef {{
10 * }} HootButtonsProps
11 */
12
13 //-----------------------------------------------------------------------------
14 // Global
15 //-----------------------------------------------------------------------------
16
17 const {
18 clearTimeout,
19 Object: { keys: $keys },
20 setTimeout,
21 } = globalThis;
22
23 //-----------------------------------------------------------------------------
24 // Internal
25 //-----------------------------------------------------------------------------
26
27 const DISABLE_TIMEOUT = 500;
28
29 //-----------------------------------------------------------------------------
30 // Exports
31 //-----------------------------------------------------------------------------
32
33 /** @extends {Component<HootButtonsProps, import("../hoot").Environment>} */
34 export class HootButtons extends Component {
35 static components = { HootLink };
36
37 static props = {};
38
39 static template = xml`
40 <t t-set="isRunning" t-value="runnerState.status === 'running'" />
41 <t t-set="showAll" t-value="env.runner.hasRemovableFilter" />
42 <t t-set="showFailed" t-value="runnerState.failedIds.size" />
43 <div
44 class="${HootButtons.name} relative"
45 t-on-pointerenter="onPointerEnter"
46 t-on-pointerleave="onPointerLeave"
47 >
48 <div class="flex rounded gap-px overflow-hidden">
49 <button
50 type="button"
51 class="flex items-center bg-btn gap-2 px-2 py-1 transition-colors"
52 t-on-click.stop="onRunClick"
53 t-att-title="isRunning ? 'Stop (Esc)' : 'Run'"
54 t-att-disabled="state.disable"
55 >
56 <i t-attf-class="fa fa-{{ isRunning ? 'stop' : 'play' }}" />
57 <span t-esc="isRunning ? 'Stop' : 'Run'" />
58 </button>
59 <t t-if="showAll or showFailed">
60 <button
61 type="button"
62 class="bg-btn px-2 py-1 transition-colors animate-slide-left"
63 t-on-click.stop="onToggleClick"
64 >
65 <i class="fa fa-caret-down transition" t-att-class="{ 'rotate-180': state.open }" />
66 </button>
67 </t>
68 </div>
69 <t t-if="state.open">
70 <div
71 class="
72 w-fit absolute animate-slide-down
73 flex flex-col end-0
74 bg-base text-base shadow rounded z-2"
75 >
76 <t t-if="showAll">
77 <HootLink
78 class="'p-3 whitespace-nowrap transition-colors hover:bg-gray-300 dark:hover:bg-gray-700'"
79 title="'Run all tests'"
80 >
81 Run <strong class="text-primary">all</strong> tests
82 </HootLink>
83 </t>
84 <t t-if="showFailed">
85 <HootLink
86 class="'p-3 whitespace-nowrap transition-colors hover:bg-gray-300 dark:hover:bg-gray-700'"
87 title="'Run failed tests'"
88 ids="{ id: runnerState.failedIds }"
89 onClick="onRunFailedClick"
90 >
91 Run <strong class="text-rose">failed</strong> tests
92 </HootLink>
93 <HootLink
94 class="'p-3 whitespace-nowrap transition-colors hover:bg-gray-300 dark:hover:bg-gray-700'"
95 title="'Run failed suites'"
96 ids="{ id: getFailedSuiteIds() }"
97 onClick="onRunFailedClick"
98 >
99 Run <strong class="text-rose">failed</strong> suites
100 </HootLink>
101 </t>
102 </div>
103 </t>
104 </div>
105 `;
106
107 setup() {
108 const { runner } = this.env;
109 this.state = useState({
110 disable: false,
111 open: false,
112 });
113 this.runnerState = useState(runner.state);
114 this.disableTimeout = 0;
115
116 subscribeToURLParams(...$keys(runner.config));
117 }
118
119 getFailedSuiteIds() {
120 const { tests } = this.env.runner;
121 const suiteIds = [];
122 for (const id of this.runnerState.failedIds) {
123 const test = tests.get(id);
124 if (test && !suiteIds.includes(test.parent.id)) {
125 suiteIds.push(test.parent.id);
126 }
127 }
128 return suiteIds;
129 }
130
131 /**
132 * @param {PointerEvent} ev
133 */
134 onPointerLeave(ev) {
135 if (ev.pointerType !== "mouse") {
136 return;
137 }
138 this.state.open = false;
139 }
140
141 /**
142 * @param {PointerEvent} ev
143 */
144 onPointerEnter(ev) {
145 if (ev.pointerType !== "mouse") {
146 return;
147 }
148 if (!this.isRunning) {
149 this.state.open = true;
150 }
151 }
152
153 onRunClick() {
154 const { runner } = this.env;
155 switch (runner.state.status) {
156 case "done": {
157 refresh();
158 break;
159 }
160 case "ready": {
161 if (runner.config.manual) {
162 runner.manualStart();
163 } else {
164 refresh();
165 }
166 break;
167 }
168 case "running": {
169 runner.stop();
170 if (this.disableTimeout) {
171 clearTimeout(this.disableTimeout);
172 }
173 this.state.disable = true;
174 this.disableTimeout = setTimeout(
175 () => (this.state.disable = false),
176 DISABLE_TIMEOUT
177 );
178 break;
179 }
180 }
181 }
182
183 onRunFailedClick() {
184 storageSet(STORAGE.failed, []);
185 }
186
187 onToggleClick() {
188 this.state.open = !this.state.open;
189 }
190 }