Skip to content

Instantly share code, notes, and snippets.

@luissifu
Last active April 10, 2020 21:43
Show Gist options
  • Save luissifu/c5f24454c24f1a6debb937bf78f3dbd4 to your computer and use it in GitHub Desktop.
Save luissifu/c5f24454c24f1a6debb937bf78f3dbd4 to your computer and use it in GitHub Desktop.
Layouts
import Component from '@ember/component';
export default class extends Component {
classNames = ['layout'];
clickX = -1;
clickY = -1;
minX = 0;
minY = 0;
maxX = 0;
maxY = 0;
dragging = false;
rectId = -1;
didRender() {
this._super(...arguments);
this.minX = this.element.offsetLeft;
this.minY = this.element.offsetTop;
this.maxX = this.minX + this.element.width;
this.maxY = this.minY + this.element.height;
}
mouseDown(event) {
this.clickX = event.pageX - this.minX;
this.clickY = event.pageY - this.minY;
let selectedRectangle = event.target.classList.contains('rectangle');
let rightClick = event.which == 3;
if (selectedRectangle) {
if (rightClick) {
this.deleteRectangle(event.target.id);
} else {
this.dragging = true;
this.rectId = event.target.id;
}
}
}
mouseUp(event) {
let x = event.pageX - this.minX;
let y = event.pageY - this.minY;
if (this.dragging) {
let distX = x - this.clickX;
let distY = y - this.clickY;
this.updateRectangle(this.rectId, distX, distY);
} else {
let rect = {
startX: Math.max(0, Math.min(this.clickX, x)),
startY: Math.max(0, Math.min(this.clickY, y)),
endX: Math.max(this.clickX, x),
endY: Math.max(this.clickY, y),
color: 'blue'
}
this.addRectangle(rect);
}
this.dragging = false;
this.clickX = -1;
this.clickY = -1;
}
}
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
export default class extends Component {
get width() {
return this.args.endX - this.args.startX;
}
get height() {
return this.args.endY - this.args.startY;
}
}
import Controller from '@ember/controller';
import { action } from "@ember/object";
import { tracked } from '@glimmer/tracking';
import { set } from '@ember/object';
export default class ApplicationController extends Controller {
appName = 'Meep';
@tracked
autoId = 3;
@tracked
rectangles = [
{id: 1, startX: 0, startY: 0, endX: 100, endY: 100, color: 'red'},
{id: 2, startX: 300, startY: 300, endX: 500, endY: 500, color: 'green'}
];
@action
addRectangle(rect) {
rect.id = this.autoId;
this.autoId++;
this.rectangles.push(rect);
this.rectangles = this.rectangles;
}
@action
updateRectangle(id, distX, distY) {
let rectId = -1;
for (rectId = 0; rectId < this.rectangles.length; rectId++) {
if (id == this.rectangles[rectId].id) {
break;
}
};
let ogRect = this.rectangles[rectId];
set(ogRect, 'startX', ogRect.startX + distX);
set(ogRect, 'startY', ogRect.startY + distY);
set(ogRect, 'endX', ogRect.endX + distX);
set(ogRect, 'endY', ogRect.endY + distY);
}
@action
deleteRectangle(id) {
let rectId = -1;
for (rectId = 0; rectId < this.rectangles.length; rectId++) {
if (id == this.rectangles[rectId].id) {
break;
}
};
this.rectangles.splice(rectId, 1);
// this.rectangles = this.rectangles;
}
}
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
display: flex;
min-height: 100vh;
flex-direction: column;
}
#main {
display: flex;
flex: 1;
}
.ember-view {
display: flex;
flex: 1;
flex-direction: column;
}
.layout {
border: 1px solid black;
position: relative;
width: 100%;
height: 100%;
}
<h1>Layout</h1>
<Layout @rectangles={{rectangles}}
@addRectangle={{addRectangle}}
@updateRectangle={{updateRectangle}}
@deleteRectangle={{deleteRectangle}}/>
{{#each @rectangles as |rect|}}
<Rectangle @id={{rect.id}}
@startX={{rect.startX}}
@startY={{rect.startY}}
@endX={{rect.endX}}
@endY={{rect.endY}}
@color={{rect.color}}/>
{{/each}}
<div class="rectangle"
id={{@id}}
style="position: absolute;
left:{{@startX}}px;
top:{{@startY}}px;
width:{{this.width}}px;
height:{{this.height}}px;
background-color:{{@color}};">
</div>
{
"version": "0.17.0",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.17.0",
"ember-template-compiler": "3.17.0",
"ember-testing": "3.17.0"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment