-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[IMP] playground: add a new sample #1638
Draft
jpp-odoo
wants to merge
1
commit into
master
Choose a base branch
from
master-add_tictactoe_sample-jpp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
.square { | ||
background: #fff; | ||
border: 1px solid #999; | ||
float: left; | ||
font-size: 24px; | ||
font-weight: bold; | ||
line-height: 34px; | ||
height: 34px; | ||
margin-right: -1px; | ||
margin-top: -1px; | ||
padding: 0; | ||
text-align: center; | ||
width: 34px; | ||
} | ||
|
||
.board-row:after { | ||
clear: both; | ||
content: ''; | ||
display: table; | ||
} | ||
|
||
.status { | ||
margin-bottom: 10px; | ||
} | ||
.game { | ||
display: flex; | ||
flex-direction: row; | ||
} | ||
|
||
.game-info { | ||
margin-left: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// This example is an implementation of the Tic-Tac-Toe game, from | ||
// https://react.dev/learn/tutorial-tic-tac-toe. This is an easy application to start learning owl | ||
// with some interesting user interactions. | ||
// | ||
// In this implementation, we use the owl reactivity mechanism. | ||
import { Component, useState, mount } from "@odoo/owl"; | ||
|
||
|
||
class Square extends Component { | ||
static template = "Square"; | ||
} | ||
|
||
class Board extends Component { | ||
static template = "Board" | ||
static components = { Square }; | ||
|
||
handleClick(i) { | ||
if (this.calculateWinner(this.props.squares) || this.props.squares[i]) { | ||
return; | ||
} | ||
const nextSquares = this.props.squares.slice(); | ||
if (this.props.xIsNext) { | ||
nextSquares[i] = 'X'; | ||
} else { | ||
nextSquares[i] = 'O'; | ||
} | ||
this.props.onPlay(nextSquares); | ||
} | ||
|
||
get status(){ | ||
const winner = this.calculateWinner(this.props.squares); | ||
if (winner) { | ||
return 'Winner: ' + winner; | ||
} else { | ||
if (Object.values(this.props.squares).filter((v) => v === null).length > 0) | ||
return 'Next player: ' + (this.props.xIsNext ? 'X' : 'O'); | ||
else | ||
return 'Draw'; | ||
} | ||
} | ||
|
||
calculateWinner(squares) { | ||
const lines = [ | ||
[0, 1, 2], | ||
[3, 4, 5], | ||
[6, 7, 8], | ||
[0, 3, 6], | ||
[1, 4, 7], | ||
[2, 5, 8], | ||
[0, 4, 8], | ||
[2, 4, 6], | ||
]; | ||
for (let i = 0; i < lines.length; i++) { | ||
const [a, b, c] = lines[i]; | ||
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { | ||
return squares[a]; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
class Game extends Component { | ||
static template = "Game" | ||
static components = { Board }; | ||
|
||
setup() { | ||
this.state = useState({ | ||
currentMove: 0, | ||
history: [Array(9).fill(null)], | ||
}); | ||
} | ||
|
||
get currentSquares() { | ||
return this.state.history[this.state.currentMove]; | ||
} | ||
|
||
get xIsNext() { | ||
return this.state.currentMove % 2 === 0; | ||
} | ||
|
||
jumpTo(nextMove) { | ||
this.state.currentMove = nextMove; | ||
} | ||
|
||
handlePlay(nextSquares) { | ||
const nextHistory = [...this.state.history.slice(0, this.state.currentMove + 1), nextSquares]; | ||
this.state.history = nextHistory; | ||
this.state.currentMove = this.state.history.length - 1; | ||
} | ||
|
||
get moves() { | ||
return this.state.history.map((_squares, move) => { | ||
if (move > 0) { | ||
return {id: move, description: 'Go to move #' + move}; | ||
} else { | ||
return {id: move, description: 'Go to game start'}; | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
// Application setup | ||
mount(Game, document.body, { templates: TEMPLATES, dev: true}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<templates> | ||
<button t-name="Square" class="square" t-on-click="props.onSquareClick"> | ||
<t t-esc="props.value"/> | ||
</button> | ||
|
||
<t t-name="Board"> | ||
<div class="status"> | ||
<t t-esc="status"/> | ||
</div> | ||
<div class="board-row"> | ||
<Square value="props.squares[0]" onSquareClick="() => this.handleClick(0)" /> | ||
<Square value="props.squares[1]" onSquareClick="() => this.handleClick(1)" /> | ||
<Square value="props.squares[2]" onSquareClick="() => this.handleClick(2)" /> | ||
</div> | ||
<div class="board-row"> | ||
<Square value="props.squares[3]" onSquareClick="() => this.handleClick(3)" /> | ||
<Square value="props.squares[4]" onSquareClick="() => this.handleClick(4)" /> | ||
<Square value="props.squares[5]" onSquareClick="() => this.handleClick(5)" /> | ||
</div> | ||
<div class="board-row"> | ||
<Square value="props.squares[6]" onSquareClick="() => this.handleClick(6)" /> | ||
<Square value="props.squares[7]" onSquareClick="() => this.handleClick(7)" /> | ||
<Square value="props.squares[8]" onSquareClick="() => this.handleClick(8)" /> | ||
</div> | ||
</t> | ||
|
||
<div t-name="Game" class="game"> | ||
<div class="game-board"> | ||
<Board xIsNext="xIsNext" squares="currentSquares" onPlay.bind="handlePlay" /> | ||
</div> | ||
<div class="game-info"> | ||
<ol> | ||
<t t-foreach="moves" t-as="move" t-key="move.id"> | ||
<li> | ||
<button t-on-click="() => this.jumpTo(move.id)"> | ||
<t t-esc="move.description"/> | ||
</button> | ||
</li> | ||
</t> | ||
</ol> | ||
</div> | ||
</div> | ||
</templates> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or maybe: