2019-01-24 10:33:11 +01:00
|
|
|
import { Widget } from "../core/widget";
|
2019-01-23 14:28:41 +01:00
|
|
|
import { Env } from "../env";
|
2019-01-24 13:48:31 +01:00
|
|
|
import { Clock } from "./clock";
|
|
|
|
|
import { Counter } from "./counter";
|
2019-01-22 10:38:44 +01:00
|
|
|
|
|
|
|
|
const template = `
|
2019-01-25 10:25:42 +01:00
|
|
|
<div class="o_discuss">
|
2019-01-24 13:48:31 +01:00
|
|
|
<span>DISCUSS!!</span>
|
2019-01-26 08:40:09 +01:00
|
|
|
<button t-on-click="resetCounter">Reset first counter</button>
|
|
|
|
|
<button t-on-click="resetCounterAsync">Reset counter 2 in 3s</button>
|
|
|
|
|
<button t-on-click="toggle">Toggle Clock/counters</button>
|
2019-01-28 12:13:03 +01:00
|
|
|
<button t-on-click="toggleColor">Toggle Color</button>
|
2019-01-26 18:12:54 +01:00
|
|
|
<button t-on-click="updateState({})">Rerender this widget</button>
|
2019-01-29 15:19:02 +01:00
|
|
|
<input t-ref="textinput"/>
|
2019-01-22 10:38:44 +01:00
|
|
|
<t t-if="state.validcounter">
|
|
|
|
|
<t t-widget="Counter" t-ref="counter" t-props="{initialState:4}"/>
|
2019-01-26 08:40:09 +01:00
|
|
|
<t t-widget="Counter" t-ref="counter2" t-props="{initialState:400}"/>
|
2019-01-22 10:38:44 +01:00
|
|
|
</t>
|
|
|
|
|
<t t-else="1">
|
2019-01-24 13:48:31 +01:00
|
|
|
<t t-widget="Clock"/>
|
2019-01-22 10:38:44 +01:00
|
|
|
</t>
|
2019-01-28 12:13:03 +01:00
|
|
|
<t t-widget="ColorWidget" t-props="{color: state.color}"/>
|
2019-01-29 16:13:05 +01:00
|
|
|
<button t-on-click="addNotif(false)">Add notif</button>
|
|
|
|
|
<button t-on-click="addNotif(true)">Add sticky notif</button>
|
2019-01-30 14:56:47 +01:00
|
|
|
<t t-foreach="state.test" t-as="number">
|
|
|
|
|
<t t-widget="ChildWidget" t-key="number"/>
|
2019-01-30 11:29:54 +01:00
|
|
|
</t>
|
2019-01-30 14:56:47 +01:00
|
|
|
|
2019-01-22 10:38:44 +01:00
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
|
2019-01-30 14:56:47 +01:00
|
|
|
let n = 1;
|
|
|
|
|
class ChildWidget extends Widget<Env, never> {
|
|
|
|
|
name = "c";
|
|
|
|
|
template = `<span><t t-esc="state.n"/></span>`;
|
|
|
|
|
constructor(parent) {
|
|
|
|
|
super(parent);
|
|
|
|
|
this.state = { n };
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 21:32:21 +01:00
|
|
|
export class Discuss extends Widget<Env, {}> {
|
2019-01-22 10:38:44 +01:00
|
|
|
name = "discuss";
|
|
|
|
|
template = template;
|
2019-01-30 14:56:47 +01:00
|
|
|
widgets = { Clock, Counter, ColorWidget, ChildWidget };
|
2019-01-30 11:29:54 +01:00
|
|
|
state = { validcounter: true, color: "red", test: [1, 2, 3] };
|
2019-01-22 10:38:44 +01:00
|
|
|
|
2019-01-25 10:25:42 +01:00
|
|
|
mounted() {}
|
2019-01-22 10:38:44 +01:00
|
|
|
resetCounter(ev: MouseEvent) {
|
2019-01-25 10:25:42 +01:00
|
|
|
if (this.refs.counter instanceof Counter) {
|
|
|
|
|
this.refs.counter.updateState({ counter: 3 });
|
|
|
|
|
}
|
2019-01-22 10:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resetCounterAsync(ev: MouseEvent) {
|
|
|
|
|
setTimeout(() => {
|
2019-01-26 08:40:09 +01:00
|
|
|
if (this.refs.counter2 instanceof Counter) {
|
|
|
|
|
this.refs.counter2.updateState({ counter: 300 });
|
2019-01-25 10:25:42 +01:00
|
|
|
}
|
2019-01-22 10:38:44 +01:00
|
|
|
}, 3000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toggle() {
|
2019-01-26 08:40:09 +01:00
|
|
|
this.updateState({ validcounter: !this.state.validcounter });
|
2019-01-22 10:38:44 +01:00
|
|
|
}
|
2019-01-28 12:13:03 +01:00
|
|
|
|
|
|
|
|
toggleColor() {
|
|
|
|
|
const newColor = this.state.color === "red" ? "blue" : "red";
|
|
|
|
|
this.updateState({ color: newColor });
|
|
|
|
|
}
|
2019-01-29 15:19:02 +01:00
|
|
|
|
2019-01-29 16:13:05 +01:00
|
|
|
addNotif(sticky: boolean) {
|
2019-01-29 15:19:02 +01:00
|
|
|
const text = (<any>this.refs.textinput).value;
|
|
|
|
|
const message = `It is now ${new Date().toLocaleTimeString()}.<br/> Msg: ${text}`;
|
2019-01-29 16:13:05 +01:00
|
|
|
this.env.notifications.add({ title: "hey", message: message, sticky });
|
2019-01-29 15:19:02 +01:00
|
|
|
}
|
2019-01-28 12:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-28 21:32:21 +01:00
|
|
|
class ColorWidget extends Widget<Env, { color: "red" | "blue" }> {
|
2019-01-28 12:13:03 +01:00
|
|
|
name = "colorwidget";
|
2019-01-27 22:16:00 +01:00
|
|
|
template = `<div>Current Color: <t t-esc="props.color"/></div>`;
|
2019-01-22 10:38:44 +01:00
|
|
|
}
|