Last active
April 10, 2020 21:43
-
-
Save luissifu/c5f24454c24f1a6debb937bf78f3dbd4 to your computer and use it in GitHub Desktop.
Layouts
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
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; | |
} | |
} |
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
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; | |
} | |
} |
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
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; | |
} | |
} |
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
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%; | |
} |
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
{ | |
"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