2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10 <link rel=
"import" href=
"../polymer/polymer.html">
23 @group Polymer Core Elements
24 @element core-drag-drop
28 <polymer-element name=
"core-drag-drop">
33 Polymer('core-drag-drop', {
36 'x y': 'coordinatesChanged'
41 avatar
= document
.createElement('core-drag-avatar');
42 document
.body
.appendChild(avatar
);
45 this.dragging
= false;
48 draggingChanged: function() {
49 this.avatar
.style
.display
= this.dragging
? '' : 'none';
52 coordinatesChanged: function() {
53 var x
= this.x
, y
= this.y
;
54 this.avatar
.style
.transform
=
55 this.avatar
.style
.webkitTransform
=
56 'translate(' + x
+ 'px, ' + y
+ 'px)';
59 attached: function() {
60 var listen = function(event
, handler
) {
61 Polymer
.addEventListener(this.parentNode
, event
, this[handler
].bind(this));
64 listen('trackstart', 'trackStart');
65 listen('track', 'track');
66 listen('trackend', 'trackEnd');
68 var host
= this.parentNode
.host
|| this.parentNode
;
69 host
.style
.cssText
+= '; user-select: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none;';
72 trackStart: function(event
) {
73 this.avatar
.style
.cssText
= '';
78 this.fire('drag-start', this.dragInfo
);
79 // flaw #1: what if user doesn't need `drag()`?
80 this.dragging
= Boolean(this.dragInfo
.drag
);
83 track: function(event
) {
87 this.dragInfo
.event
= event
;
88 this.dragInfo
.p
= {x
: this.x
, y
: this.y
};
89 this.dragInfo
.drag(this.dragInfo
);
93 trackEnd: function(event
) {
95 this.dragging
= false;
96 if (this.dragInfo
.drop
) {
97 this.dragInfo
.framed
= this.framed(event
.relatedTarget
);
98 this.dragInfo
.event
= event
;
99 this.dragInfo
.drop(this.dragInfo
);
102 this.dragInfo
= null;
105 framed: function(node
) {
106 var local
= node
.getBoundingClientRect();
107 return {x
: this.x
- local
.left
, y
: this.y
- local
.top
};