OLD | NEW |
| (Empty) |
1 <!-- | |
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.g
ithub.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/CONTRI
BUTORS.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/PATEN
TS.txt | |
8 --> | |
9 | |
10 <link rel="import" href="../polymer/polymer.html"> | |
11 | |
12 <style> | |
13 core-drag-avatar { | |
14 position: fixed; | |
15 left: 0; | |
16 top: 0; | |
17 display: block; | |
18 pointer-events: none; | |
19 } | |
20 </style> | |
21 | |
22 <!-- | |
23 @group Polymer Core Elements | |
24 @element core-drag-drop | |
25 @homepage github.io | |
26 --> | |
27 | |
28 <polymer-element name="core-drag-drop"> | |
29 <script> | |
30 | |
31 Polymer('core-drag-drop', { | |
32 | |
33 observe: { | |
34 'x y': 'coordinatesChanged' | |
35 }, | |
36 | |
37 ready: function() { | |
38 if (!this.__proto__.avatar) { | |
39 this.__proto__.avatar = document.createElement('core-drag-avatar'); | |
40 document.body.appendChild(this.avatar); | |
41 } | |
42 this.dragging = false; | |
43 }, | |
44 | |
45 draggingChanged: function() { | |
46 this.avatar.style.display = this.dragging ? '' : 'none'; | |
47 }, | |
48 | |
49 coordinatesChanged: function() { | |
50 var x = this.x, y = this.y; | |
51 this.avatar.style.transform = | |
52 this.avatar.style.webkitTransform = | |
53 'translate(' + x + 'px, ' + y + 'px)'; | |
54 }, | |
55 | |
56 attached: function() { | |
57 var listen = function(event, handler) { | |
58 Polymer.addEventListener(this.parentNode, event, this[handler].bind(this
)); | |
59 }.bind(this); | |
60 // | |
61 listen('trackstart', 'trackStart'); | |
62 listen('track', 'track'); | |
63 listen('trackend', 'trackEnd'); | |
64 // | |
65 var host = this.parentNode.host || this.parentNode; | |
66 host.style.cssText += '; user-select: none; -webkit-user-select: none; -mo
z-user-select: none; -ms-user-select: none;'; | |
67 }, | |
68 | |
69 trackStart: function(event) { | |
70 this.avatar.style.cssText = ''; | |
71 this.dragInfo = { | |
72 event: event, | |
73 avatar: this.avatar | |
74 }; | |
75 this.fire('drag-start', this.dragInfo); | |
76 // flaw #1: what if user doesn't need `drag()`? | |
77 this.dragging = Boolean(this.dragInfo.drag); | |
78 }, | |
79 | |
80 track: function(event) { | |
81 if (this.dragging) { | |
82 this.x = event.pageX; | |
83 this.y = event.pageY; | |
84 this.dragInfo.event = event; | |
85 this.dragInfo.p = {x : this.x, y: this.y}; | |
86 this.dragInfo.drag(this.dragInfo); | |
87 } | |
88 }, | |
89 | |
90 trackEnd: function(event) { | |
91 if (this.dragging) { | |
92 this.dragging = false; | |
93 if (this.dragInfo.drop) { | |
94 this.dragInfo.framed = this.framed(event.relatedTarget); | |
95 this.dragInfo.event = event; | |
96 this.dragInfo.drop(this.dragInfo); | |
97 } | |
98 } | |
99 this.dragInfo = null; | |
100 }, | |
101 | |
102 framed: function(node) { | |
103 var local = node.getBoundingClientRect(); | |
104 return {x: this.x - local.left, y: this.y - local.top}; | |
105 } | |
106 | |
107 }); | |
108 | |
109 </script> | |
110 </polymer-element> | |
OLD | NEW |