Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
sizing and drag hint
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Jun 6, 2014
1 parent b25603a commit ae15f82
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
85 changes: 83 additions & 2 deletions elements/design-canvas/design-canvas.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
'drag-start': 'drag'
},

observe: {
'dragInfo.dropTarget': 'updateDropTarget'
},

resizeNubSize: 10,
grid: 10,
selected: null,

Expand Down Expand Up @@ -164,7 +169,12 @@
switch(event.keyCode) {
// ESC
case 27:
this.selectParentElement();
// context handling for esc based on drag state
if (this.dragInfo) {
this.selectParentDropHint();
} else {
this.selectParentElement();
}
break;
// backspace? delete?
case 8:
Expand All @@ -184,6 +194,11 @@
return;
}
dragInfo.origin = dragInfo.element.getBoundingClientRect();
// delegate to size API
if (this.dragShouldSize(dragInfo.event, dragInfo.origin)) {
this.sizeStart(dragInfo);
return;
}
// delegate to drag API
this.dragStart(dragInfo);
// multiplex drop
Expand All @@ -197,6 +212,34 @@
this.selected = dragInfo.element;
},

dragShouldSize: function(event, rect) {
return (Math.abs(rect.right - event.clientX) < this.resizeNubSize) &&
(Math.abs(rect.bottom - event.clientY) < this.resizeNubSize);
},

sizeStart: function(dragInfo) {
this.classList.add('sizing');
this.selected = dragInfo.element;
//
// contextualize utilities
//
var snap = this.snap.bind(this);
var rule = this.ruleForElement(dragInfo.element);
//
// attach handlers to the dragInfo
//
dragInfo.drag = function() {
var p = snap(this.origin.width + this.event.dx, this.origin.height + this.event.dy);
rule.style.width = p.x + 'px';
rule.style.height = p.y + 'px';
};
var canvas = this;
dragInfo.drop = function() {
canvas.classList.remove('sizing');
canvas.notify();
};
},

//
// abstracted drag API
//
Expand All @@ -209,6 +252,7 @@
//

dragStart: function(dragInfo) {
this.dragInfo = dragInfo;
// TODO(sjmiles): if this is an element from the design-canvas, it's immediately
// pulled out of it's original context
if (dragInfo.element.parentNode != this) {
Expand All @@ -231,6 +275,7 @@
var snap = this.snap.bind(this);
var rule = this.ruleForElement(dragInfo.element);
var drop = this.drop.bind(this);
var dropHint = this.dropHint.bind(this);
var scope = function(target) {
return this.scopedTarget(this, target)
}.bind(this);
Expand All @@ -242,14 +287,17 @@
var p = snap(this.start.x + this.event.dx, this.start.y + this.event.dy);
rule.style.left = p.x + 'px';
rule.style.top = p.y + 'px';
dropHint(this);
};
dragInfo.drop = function() {
this.dropTarget = scope(this.event.relatedTarget);
this.dropTarget = this.dropTarget || scope(this.event.relatedTarget);
this.dropTarget.classList.remove('drop-target');
return drop(this);
};
},

drop: function(dragInfo) {
this.dragInfo = null;
// remove dragging affordances (e.g. absolutely positioned)
this.unsetDraggableStyleRules(dragInfo.element);
// TODO(sjmiles): restore clipping (ad hoc)
Expand All @@ -276,6 +324,39 @@
}
},

dropHint: function(dragInfo) {
this.job('hint-job', function() {
var event = {
clientX: dragInfo.event.clientX,
clientY: dragInfo.event.clientY,
target: dragInfo.dropTarget
}
dragInfo.dropTarget = PolymerGestures.targetFinding.findTarget(event);
});
},

selectParentDropHint: function() {
console.log('selectParentDropHint');
var hint = this.dragInfo && this.dragInfo.dropTarget;
if (hint) {
var parent = this.findContainer(this.dragInfo.element, hint.parentNode);
if (parent) {
this.dragInfo.dropTarget = parent;
}
}
console.log(this.dragInfo.dropTarget);
},

updateDropTarget: function(old) {
if (old) {
old.classList.remove('drop-target');
}
var target = this.dragInfo && this.dragInfo.dropTarget;
if (target) {
target.classList.add('drop-target');
}
},

// utility

update: function() {
Expand Down
19 changes: 19 additions & 0 deletions elements/design-frame/design-frame.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,23 @@
#canvas .selected-element {
outline: solid 3px rgba(66, 87, 203, 0.8);
outline-offset: -3px;
}

#canvas *::after {
position: absolute;
content: ' ';
bottom: 0;
right: 0;
height: 10px;
width: 10px;
cursor: se-resize;
}

#canvas.sizing {
cursor: se-resize;
}

#canvas .drop-target {
outline: dotted 2px black;
outline-offset: -2px;
}
7 changes: 6 additions & 1 deletion elements/design-tree/design-tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@
Polymer({
components: null,
canvas: null,
dirty: 0,

observe: {
'canvas selected': 'update'
'canvas selected dirty': 'update'
},

forceUpdate: function() {
this.dirty++;
},

update: function() {
Expand Down
4 changes: 3 additions & 1 deletion elements/designer-element/designer-element.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@
},

designChange: function(event) {
this.$.inspector.inspected = this.designer.selected;
this.$.inspector.selected = this.designer.selected;
this.$.tree.selected = this.designer.selected;
this.$.inspector.forceUpdate();
this.$.tree.forceUpdate();
this.selectedName = this.makeSelectedName(this.designer.selected);
this.recordChange();
},
Expand Down

0 comments on commit ae15f82

Please sign in to comment.