Skip to content

Commit 26f8e76

Browse files
author
epriestley
committedFeb 23, 2013
When an image is too wide in Pholio, scale it down
Summary: Currently, if an image is too wide for the viewport, we freak out. Instead, scale it down. This means we must also scale down all the rectangles on it, which is why this is tricky. However, all the draw/load separation has made it reasonably straightforward. We'll possibly need to add some kind of "view full size" thing. I'm planning to add an element which shows "85%" or whatever if it's currently scaled. Test Plan: Before: {F33607} After: {F33608} Reviewers: chad, ljalonen Reviewed By: chad CC: aran Differential Revision: https://secure.phabricator.com/D5088
1 parent 8641ef3 commit 26f8e76

File tree

5 files changed

+107
-54
lines changed

5 files changed

+107
-54
lines changed
 

‎externals/javelinjs/src/core/Event.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ JX.install('Event', {
133133
var r = this.getRawEvent();
134134
return r.which == 3 || r.button == 2;
135135
},
136-
136+
137137
/**
138138
* Determine if a mouse event is a normal event (left mouse button, no
139139
* modifier keys).
@@ -142,14 +142,15 @@ JX.install('Event', {
142142
* @task info
143143
*/
144144
isNormalMouseEvent : function() {
145-
var supportedEvents = ['click','mouseup','mousedown'];
145+
var supportedEvents = ['click', 'mouseup', 'mousedown'];
146146

147147
if (supportedEvents.indexOf(this.getType()) == -1) {
148148
return false;
149149
}
150150

151151
var r = this.getRawEvent();
152-
if (r.metaKey || r.altKey || r.ctrlkey || r.shiftKey) {
152+
153+
if (r.metaKey || r.altKey || r.ctrlKey || r.shiftKey) {
153154
return false;
154155
}
155156

‎src/__celerity_resource_map__.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -1873,16 +1873,17 @@
18731873
),
18741874
'javelin-behavior-pholio-mock-view' =>
18751875
array(
1876-
'uri' => '/res/e2778b8e/rsrc/js/application/pholio/behavior-pholio-mock-view.js',
1876+
'uri' => '/res/b7c2b169/rsrc/js/application/pholio/behavior-pholio-mock-view.js',
18771877
'type' => 'js',
18781878
'requires' =>
18791879
array(
18801880
0 => 'javelin-behavior',
1881-
1 => 'javelin-stratcom',
1882-
2 => 'javelin-dom',
1883-
3 => 'javelin-vector',
1884-
4 => 'javelin-magical-init',
1885-
5 => 'javelin-request',
1881+
1 => 'javelin-util',
1882+
2 => 'javelin-stratcom',
1883+
3 => 'javelin-dom',
1884+
4 => 'javelin-vector',
1885+
5 => 'javelin-magical-init',
1886+
6 => 'javelin-request',
18861887
),
18871888
'disk' => '/rsrc/js/application/pholio/behavior-pholio-mock-view.js',
18881889
),
@@ -3217,7 +3218,7 @@
32173218
),
32183219
'pholio-css' =>
32193220
array(
3220-
'uri' => '/res/ecd07cd8/rsrc/css/application/pholio/pholio.css',
3221+
'uri' => '/res/4ca7889f/rsrc/css/application/pholio/pholio.css',
32213222
'type' => 'css',
32223223
'requires' =>
32233224
array(

‎src/applications/pholio/view/PholioMockImagesView.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ public function render() {
1616

1717
$mock = $this->mock;
1818

19-
$main_image_id = celerity_generate_unique_node_id();
2019
require_celerity_resource('javelin-behavior-pholio-mock-view');
2120

2221
$images = array();
22+
$panel_id = celerity_generate_unique_node_id();
23+
$viewport_id = celerity_generate_unique_node_id();
24+
2325
foreach ($mock->getImages() as $image) {
2426
$images[] = array(
2527
'id' => $image->getID(),
@@ -28,34 +30,32 @@ public function render() {
2830
}
2931

3032
$config = array(
31-
'mainID' => $main_image_id,
3233
'mockID' => $mock->getID(),
34+
'panelID' => $panel_id,
35+
'viewportID' => $viewport_id,
3336
'images' => $images,
3437

3538
);
3639
Javelin::initBehavior('pholio-mock-view', $config);
3740

3841
$mockview = '';
3942

40-
$main_image = head($mock->getImages());
41-
42-
$main_image_tag = javelin_tag(
43-
'img',
43+
$mock_wrapper = phutil_tag(
44+
'div',
4445
array(
45-
'id' => $main_image_id,
46-
'sigil' => 'mock-image',
47-
'class' => 'pholio-mock-image',
48-
'style' => 'display: none;',
49-
));
46+
'id' => $viewport_id,
47+
'class' => 'pholio-mock-image-viewport'
48+
),
49+
'');
5050

51-
$main_image_tag = javelin_tag(
51+
$mock_wrapper = javelin_tag(
5252
'div',
5353
array(
54-
'id' => 'mock-wrapper',
55-
'sigil' => 'mock-wrapper',
56-
'class' => 'pholio-mock-wrapper'
54+
'id' => $panel_id,
55+
'sigil' => 'mock-panel',
56+
'class' => 'pholio-mock-image-panel',
5757
),
58-
$main_image_tag);
58+
$mock_wrapper);
5959

6060
$inline_comments_holder = javelin_tag(
6161
'div',
@@ -72,7 +72,7 @@ public function render() {
7272
'class' => 'pholio-mock-image-container',
7373
'id' => 'pholio-mock-image-container'
7474
),
75-
array($main_image_tag, $inline_comments_holder));
75+
array($mock_wrapper, $inline_comments_holder));
7676

7777
if (count($mock->getImages()) > 1) {
7878
$thumbnails = array();

‎webroot/rsrc/css/application/pholio/pholio.css

+9-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
}
3939

4040
.pholio-mock-image {
41-
display: inline-block;
41+
margin: auto;
42+
cursor: crosshair;
4243
}
4344

4445
.pholio-mock-select-border {
@@ -55,12 +56,15 @@
5556
box-sizing: border-box;
5657
}
5758

58-
.pholio-mock-wrapper {
59+
.pholio-mock-image-panel {
60+
padding: 20px;
61+
margin-right: 320px;
62+
}
63+
64+
.pholio-mock-image-viewport {
5965
position: relative;
60-
margin: 20px 340px 20px 20px;
66+
margin: auto;
6167
display: inline-block;
62-
cursor: crosshair;
63-
padding: 0px;
6468
}
6569

6670
.pholio-mock-inline-comments {

‎webroot/rsrc/js/application/pholio/behavior-pholio-mock-view.js

+69-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @provides javelin-behavior-pholio-mock-view
33
* @requires javelin-behavior
4+
* javelin-util
45
* javelin-stratcom
56
* javelin-dom
67
* javelin-vector
@@ -10,9 +11,10 @@
1011
JX.behavior('pholio-mock-view', function(config) {
1112
var is_dragging = false;
1213

13-
var wrapper = JX.$('mock-wrapper');
1414
var drag_begin;
1515
var drag_end;
16+
var panel = JX.$(config.panelID);
17+
var viewport = JX.$(config.viewportID);
1618

1719
var selection_border;
1820
var selection_fill;
@@ -29,16 +31,38 @@ JX.behavior('pholio-mock-view', function(config) {
2931
return null;
3032
}
3133

32-
function select_image(image_id) {
33-
var image = get_image(image_id);
34-
active_image = image;
34+
function onload_image(id) {
35+
if (active_image.id != id) {
36+
// The user has clicked another image before this one loaded, so just
37+
// bail.
38+
return;
39+
}
40+
41+
// If the image is too wide for the viewport, scale it down so it fits.
42+
// (If it is too tall, we just let the user scroll.)
43+
var w = JX.Vector.getDim(panel);
44+
w.x -= 40;
45+
if (w.x < this.naturalWidth) {
46+
var scale = w.x / this.naturalWidth;
47+
this.width = Math.floor(scale * this.naturalWidth);
48+
this.height = Math.floor(scale * this.naturalHeight);
49+
}
3550

36-
var main = JX.$(config.mainID);
37-
main.src = image.fullURI;
38-
JX.DOM.show(main);
51+
active_image.tag = this;
52+
53+
// NOTE: This also clears inline comment reticles.
54+
JX.DOM.setContent(viewport, this);
55+
56+
redraw_inlines(active_image.id);
57+
}
58+
59+
function select_image(image_id) {
60+
active_image = get_image(image_id);
61+
active_image.tag = null;
3962

40-
// NOTE: This is to clear inline comment reticles.
41-
JX.DOM.setContent(wrapper, main);
63+
var img = JX.$N('img', {className: 'pholio-mock-image'});
64+
img.onload = JX.bind(img, onload_image, active_image.id);
65+
img.src = active_image.fullURI;
4266

4367
load_inline_comments();
4468
}
@@ -54,7 +78,7 @@ JX.behavior('pholio-mock-view', function(config) {
5478
// Select and show the first image.
5579
select_image(config.images[0].id);
5680

57-
JX.Stratcom.listen('mousedown', 'mock-wrapper', function(e) {
81+
JX.Stratcom.listen('mousedown', 'mock-panel', function(e) {
5882
if (!e.isNormalMouseEvent()) {
5983
return;
6084
}
@@ -111,8 +135,8 @@ JX.behavior('pholio-mock-view', function(config) {
111135

112136
var dialog = JX.$('pholio-new-inline-comment-dialog');
113137

114-
var wrapperVector = JX.$V(wrapper);
115-
var wrapperDimensions = JX.Vector.getDim(wrapper);
138+
var viewportVector = JX.$V(viewport);
139+
var viewportDimensions = JX.Vector.getDim(viewport);
116140

117141
JX.$V(
118142
// TODO: This is a little funky for now.
@@ -145,6 +169,13 @@ JX.behavior('pholio-mock-view', function(config) {
145169

146170
for (var ii = 0; ii < inlines.length; ii++) {
147171
var inline = inlines[ii];
172+
JX.DOM.appendContent(comment_holder, JX.$H(inline.contentHTML));
173+
174+
if (!active_image.tag) {
175+
// The image itself hasn't loaded yet, so we can't draw the inline
176+
// reticles.
177+
continue;
178+
}
148179

149180
var inlineSelection = JX.$N(
150181
'div',
@@ -158,9 +189,8 @@ JX.behavior('pholio-mock-view', function(config) {
158189
{phid: inline.phid});
159190

160191
JX.Stratcom.addSigil(inlineSelection, "image_selection");
161-
JX.DOM.appendContent(comment_holder, JX.$H(inline.contentHTML));
162192

163-
JX.DOM.appendContent(wrapper, inlineSelection);
193+
JX.DOM.appendContent(viewport, inlineSelection);
164194

165195
position_inline_rectangle(inline, inlineSelection);
166196

@@ -180,29 +210,38 @@ JX.behavior('pholio-mock-view', function(config) {
180210
{phid: inline.phid});
181211

182212
JX.Stratcom.addSigil(inlineDraft, "image_selection");
183-
JX.DOM.appendContent(wrapper, inlineDraft);
213+
JX.DOM.appendContent(viewport, inlineDraft);
184214
}
185215
}
186216
}
187217

188218
function position_inline_rectangle(inline, rect) {
189-
JX.$V(inline.x, inline.y).setPos(rect);
190-
JX.$V(inline.width, inline.height).setDim(rect);
219+
var scale = active_image.tag.width / active_image.tag.naturalWidth;
220+
221+
JX.$V(scale * inline.x, scale * inline.y).setPos(rect);
222+
JX.$V(scale * inline.width, scale * inline.height).setDim(rect);
191223
}
192224

193225
function get_image_xy(p) {
194-
var main = JX.$(config.mainID);
195-
var mainp = JX.$V(main);
226+
var img = active_image.tag;
227+
var imgp = JX.$V(img);
228+
229+
var scale = 1 / get_image_scale();
196230

197-
var x = Math.max(0, Math.min(p.x - mainp.x, main.naturalWidth));
198-
var y = Math.max(0, Math.min(p.y - mainp.y, main.naturalHeight));
231+
var x = scale * Math.max(0, Math.min(p.x - imgp.x, img.width));
232+
var y = scale * Math.max(0, Math.min(p.y - imgp.y, img.height));
199233

200234
return {
201235
x: x,
202236
y: y
203237
};
204238
}
205239

240+
function get_image_scale() {
241+
var img = active_image.tag;
242+
return img.width / img.naturalWidth;
243+
}
244+
206245
function redraw_selection() {
207246
selection_border = selection_border || JX.$N(
208247
'div',
@@ -220,10 +259,17 @@ JX.behavior('pholio-mock-view', function(config) {
220259
Math.max(drag_begin.x, drag_end.x) - p.x,
221260
Math.max(drag_begin.y, drag_end.y) - p.y);
222261

262+
var scale = get_image_scale();
263+
264+
p.x *= scale;
265+
p.y *= scale;
266+
d.x *= scale;
267+
d.y *= scale;
268+
223269
var nodes = [selection_border, selection_fill];
224270
for (var ii = 0; ii < nodes.length; ii++) {
225271
var node = nodes[ii];
226-
wrapper.appendChild(node);
272+
viewport.appendChild(node);
227273
p.setPos(node);
228274
d.setDim(node);
229275
}
@@ -393,4 +439,5 @@ JX.behavior('pholio-mock-view', function(config) {
393439
}
394440

395441
load_inline_comments();
442+
396443
});

0 commit comments

Comments
 (0)
Failed to load comments.