Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: elements/designer-stage/designer-stage.html

Issue 881243003: Add designer-stage (Closed) Base URL: https://github.com/PolymerLabs/designer2.git@master
Patch Set: Review comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « elements/designer-stage/demo.html ('k') | elements/designer-stage/frame.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 <!--
2 Copyright (c) 2015 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 <link rel="import" href="../../../neoprene/components/x-elements/x-template/x-te mplate.html">
12 <link rel="import" href="../designer-selection/designer-selection.html">
13
14 <dom-module id="designer-stage">
15
16 <style>
17 designer-stage {
18 display: block;
19 box-sizing: border-box;
20 }
21 designer-stage > iframe {
22 position: absolute;
23 top: 0;
24 left: 0;
25 width: 100%;
26 height: 100%;
27 border: none;
28 box-sizing: border-box;
29 background: gray;
30 }
31 designer-stage > #glass {
32 position: absolute;
33 top: 0;
34 left: 0;
35 width: 100%;
36 height: 100%;
37 z-index: 100;
38 }
39 designer-stage > designer-selection {
40 position: absolute;
41 z-index: 101;
42 }
43 </style>
44
45 <template>
46 <designer-selection id="selection"></designer-selection>
47 <div id="glass" on-mousedown="_onMouseDown"></div>
48 <!--
49 Note: src is main document relative, and only works in the demo
50 because it's in the same directory as this file. Need to solve this.
51 -->
52 <iframe id="frame" src="frame.html" sandbox="allow-scripts"></iframe>
53 </template>
54
55 </dom-module>
56
57 <script>
58 Polymer({
59 is: 'designer-stage',
60
61 created: function() {
62 this.token = null;
63 },
64
65 ready: function() {
66 window.addEventListener('message', this._onMessage.bind(this));
67 this.$.selection.addEventListener('designer-selection-resize',
68 this._onSelectionResize.bind(this));
69 var frame = this.$.frame;
70 var token = this.token = this._generateToken();
71 frame.addEventListener('load', function() {
72 frame.contentWindow.postMessage({
73 message_type: 'setToken',
74 token: token
75 }, '*');
76 });
77 frame.addEventListener('load', function(e) {
78 if (e.type == 'load') {
79 console.log("<designer-stage> error loading frame contents");
80 }
81 });
82 },
83
84 _generateToken: function() {
85 var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345 6789";
86 var token = '';
87 for (var i = 0; i < 16; i++) {
88 token += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
89 }
90 return token;
91 },
92
93 _onMessage: function(e) {
94 if (e.data.token != this.token) {
95 throw new Error('Invalid message token', token);
96 }
97
98 switch (e.data.message_type) {
99 case 'updateSelector':
100 this._updateSelector(e);
101 break;
102 case 'seletedElement':
103 this._selectedElement(e);
104 break;
105 }
106 },
107
108 _updateSelector: function(e) {
109 var bounds = e.data.bounds;
110 var style = this.$.selection.style;
111 if (bounds == null) {
112 style.display = 'none';
113 } else {
114 style.display = 'block';
115 style.top = bounds.top;
116 style.left = bounds.left;
117 style.width = bounds.width;
118 style.height = bounds.height;
119 }
120 },
121
122 _selectedElement: function(e) {
123 var data = e.data;
124 var bounds = data.bounds;
125 var selection = this.$.selection;
126 var style = selection.style;
127 if (data.position == 'static') {
128 selection.directions = ResizeDirection.width_height;
129 } else if (data.position == 'absolute') {
130 selection.directions = ResizeDirection.all_directions;
131 }
132 // TODO: factor out common code with _updateSelector.
133 // Possibly implement message batching or extension.
134 if (bounds == null) {
135 style.display = 'none';
136 } else {
137 style.display = 'block';
138 style.top = bounds.top;
139 style.left = bounds.left;
140 style.width = bounds.width;
141 style.height = bounds.height;
142 }
143 },
144
145 _onSelectionResize: function(e) {
146 var frame = this.$.frame;
147
148 frame.contentWindow.postMessage({
149 message_type: 'resizeElement',
150 bounds: e.detail
151 }, '*');
152
153 },
154
155 _onMouseDown: function(e) {
156 var frame = this.$.frame;
157
158 // TODO: transate coordinates to stage relative, here and within
159 // frame.html as needed.
160 frame.contentWindow.postMessage({
161 message_type: 'selectElement',
162 x: e.clientX,
163 y: e.clientY
164 }, "*");
165 }
166
167 });
168 </script>
OLDNEW
« no previous file with comments | « elements/designer-stage/demo.html ('k') | elements/designer-stage/frame.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698