1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('ntp4', function() {
'use strict';
// We can't pass the currently dragging tile via dataTransfer because of
// http://crbug.com/31037
var currentlyDraggingTile = null;
function getCurrentlyDraggingTile() {
return currentlyDraggingTile;
}
function setCurrentlyDraggingTile(tile) {
currentlyDraggingTile = tile;
if (tile)
ntp4.enterRearrangeMode();
else
ntp4.leaveRearrangeMode();
}
/**
* Creates a new Tile object. Tiles wrap content on a TilePage, providing
* some styling and drag functionality.
* @constructor
* @extends {HTMLDivElement}
*/
function Tile(contents) {
var tile = cr.doc.createElement('div');
tile.__proto__ = Tile.prototype;
tile.initialize(contents);
return tile;
}
Tile.prototype = {
__proto__: HTMLDivElement.prototype,
initialize: function(contents) {
// 'real' as opposed to doppleganger.
this.className = 'tile real';
this.appendChild(contents);
contents.tile = this;
this.addEventListener('dragstart', this.onDragStart_);
this.addEventListener('drag', this.onDragMove_);
this.addEventListener('dragend', this.onDragEnd_);
this.eventTracker = new EventTracker();
},
get index() {
return Array.prototype.indexOf.call(this.parentNode.children, this);
},
get tilePage() {
return findAncestorByClass(this, 'tile-page');
},
/**
* Position the tile at |x, y|, and store this as the grid location, i.e.
* where the tile 'belongs' when it's not being dragged.
* @param {number} x The x coordinate, in pixels.
* @param {number} y The y coordinate, in pixels.
*/
setGridPosition: function(x, y) {
this.gridX = x;
this.gridY = y;
this.moveTo(x, y);
},
/**
* Position the tile at |x, y|.
* @param {number} x The x coordinate, in pixels.
* @param {number} y The y coordinate, in pixels.
*/
moveTo: function(x, y) {
this.style.left = x + 'px';
this.style.top = y + 'px';
},
/**
* The handler for dragstart events fired on |this|.
* @param {Event} e The event for the drag.
* @private
*/
onDragStart_: function(e) {
// The user may start dragging again during a previous drag's finishing
// animation.
if (this.classList.contains('dragging'))
this.finalizeDrag_();
setCurrentlyDraggingTile(this);
e.dataTransfer.effectAllowed = 'copyMove';
// TODO(estade): fill this in.
e.dataTransfer.setData('text/plain', 'foo');
// The drag clone is the node we use as a representation during the drag.
// It's attached to the top level document element so that it floats above
// image masks.
this.dragClone = this.cloneNode(true);
this.dragClone.classList.add('drag-representation');
this.ownerDocument.body.appendChild(this.dragClone);
this.eventTracker.add(this.dragClone, 'webkitTransitionEnd',
this.onDragCloneTransitionEnd_.bind(this));
this.classList.add('dragging');
this.dragOffsetX = e.x - this.offsetLeft - this.parentNode.offsetLeft;
this.dragOffsetY = e.y - this.offsetTop -
// Unlike offsetTop, this value takes scroll position into account.
this.parentNode.getBoundingClientRect().top;
this.onDragMove_(e);
},
/**
* The handler for drag events fired on |this|.
* @param {Event} e The event for the drag.
* @private
*/
onDragMove_: function(e) {
if (e.view != window || (e.x == 0 && e.y == 0)) {
// attribute hidden seems to be overridden by display.
this.dragClone.classList.add('hidden');
return;
}
this.dragClone.classList.remove('hidden');
this.dragClone.style.left = (e.x - this.dragOffsetX) + 'px';
this.dragClone.style.top = (e.y - this.dragOffsetY) + 'px';
},
/**
* The handler for dragend events fired on |this|.
* @param {Event} e The event for the drag.
* @private
*/
onDragEnd_: function(e) {
// The drag clone can still be hidden from the last drag move event.
this.dragClone.classList.remove('hidden');
this.dragClone.classList.add('placing');
setCurrentlyDraggingTile(null);
this.tilePage.positionTile_(this.index);
if (this.tilePage.selected) {
// The tile's contents may have moved following the respositioning;
// adjust for that.
var contentDiffX = this.dragClone.firstChild.offsetLeft -
this.firstChild.offsetLeft;
var contentDiffY = this.dragClone.firstChild.offsetTop -
this.firstChild.offsetTop;
this.dragClone.style.left = (this.gridX + this.parentNode.offsetLeft -
contentDiffX) + 'px';
this.dragClone.style.top =
(this.gridY + this.parentNode.getBoundingClientRect().top -
contentDiffY) + 'px';
} else {
// When we're showing another page and a drag fails or gets cancelled,
// the tile shrinks to a dot.
this.dragClone.firstChild.style.webkitTransform = 'scale(0)';
}
},
/**
* Creates a clone of this node offset by the coordinates. Used for the
* dragging effect where a tile appears to float off one side of the grid
* and re-appear on the other.
* @param {number} x x-axis offset, in pixels.
* @param {number} y y-axis offset, in pixels.
*/
showDoppleganger: function(x, y) {
// We always have to clear the previous doppleganger to make sure we get
// style updates for the contents of this tile.
this.clearDoppleganger();
var clone = this.cloneNode(true);
clone.classList.remove('real');
clone.classList.add('doppleganger');
var clonelets = clone.querySelectorAll('.real');
for (var i = 0; i < clonelets.length; i++) {
clonelets[i].classList.remove('real');
}
this.appendChild(clone);
this.doppleganger_ = clone;
this.doppleganger_.style.WebkitTransform = 'translate(' + x + 'px, ' +
y + 'px)';
},
/**
* Destroys the current doppleganger.
*/
clearDoppleganger: function() {
if (this.doppleganger_) {
this.removeChild(this.doppleganger_);
this.doppleganger_ = null;
}
},
/**
* Returns status of doppleganger.
* @return {boolean} True if there is a doppleganger showing for |this|.
*/
hasDoppleganger: function() {
return !!this.doppleganger_;
},
/**
* Cleans up after the drag is over. This is either called when the
* drag representation finishes animating to the final position, or when
* the next drag starts (if the user starts a 2nd drag very quickly).
* @private
*/
finalizeDrag_: function() {
assert(this.classList.contains('dragging'));
var clone = this.dragClone;
this.dragClone = null;
clone.parentNode.removeChild(clone);
this.eventTracker.remove(clone, 'webkitTransitionEnd');
this.classList.remove('dragging');
},
/**
* Called when the drag representation node is done migrating to its final
* resting spot.
* @param {Event} e The transition end event.
*/
onDragCloneTransitionEnd_: function(e) {
if (this.classList.contains('dragging') &&
(e.propertyName == 'left' || e.propertyName == 'top' ||
e.propertyName == '-webkit-transform')) {
this.finalizeDrag_();
}
},
};
/**
* Gives the proportion of the row width that is devoted to a single icon.
* @param {number} rowTileCount The number of tiles in a row.
* @return {number} The ratio between icon width and row width.
*/
function tileWidthFraction(rowTileCount) {
return rowTileCount +
(rowTileCount - 1) * TILE_SPACING_FRACTION;
}
/**
* Calculates an assortment of tile-related values for a grid with the
* given dimensions.
* @param {number} width The pixel width of the grid.
* @param {number} numRowTiles The number of tiles in a row.
* @return {Object} A mapping of pixel values.
*/
function tileValuesForGrid(width, numRowTiles) {
var tileWidth = width / tileWidthFraction(numRowTiles);
var offsetX = tileWidth * (1 + TILE_SPACING_FRACTION);
var interTileSpacing = offsetX - tileWidth;
return {
tileWidth: tileWidth,
offsetX: offsetX,
interTileSpacing: interTileSpacing,
};
}
// The proportion of the tile width which will be used as spacing between
// tiles.
var TILE_SPACING_FRACTION = 1 / 8;
// The smallest amount of horizontal blank space to display on the sides when
// displaying a wide arrangement. There is an additional 26px of margin from
// the tile page padding.
var MIN_WIDE_MARGIN = 18;
/**
* Creates a new TilePage object. This object contains tiles and controls
* their layout.
* @param {string} name The display name for the page.
* @param {Object} gridValues Pixel values that define the size and layout
* of the tile grid.
* @constructor
* @extends {HTMLDivElement}
*/
function TilePage(name, gridValues) {
var el = cr.doc.createElement('div');
el.pageName = name;
el.gridValues_ = gridValues;
el.__proto__ = TilePage.prototype;
el.initialize();
return el;
}
/**
* Takes a collection of grid layout pixel values and updates them with
* additional tiling values that are calculated from TilePage constants.
* @param {Object} grid The grid layout pixel values to update.
*/
TilePage.initGridValues = function(grid) {
// The amount of space we need to display a narrow grid (all narrow grids
// are this size).
grid.narrowWidth =
grid.minTileWidth * tileWidthFraction(grid.minColCount);
// The minimum amount of space we need to display a wide grid.
grid.minWideWidth =
grid.minTileWidth * tileWidthFraction(grid.maxColCount);
// The largest we will ever display a wide grid.
grid.maxWideWidth =
grid.maxTileWidth * tileWidthFraction(grid.maxColCount);
// Tile-related pixel values for the narrow display.
grid.narrowTileValues = tileValuesForGrid(grid.narrowWidth,
grid.minColCount);
// Tile-related pixel values for the minimum narrow display.
grid.wideTileValues = tileValuesForGrid(grid.minWideWidth,
grid.maxColCount);
};
TilePage.prototype = {
__proto__: HTMLDivElement.prototype,
initialize: function() {
this.className = 'tile-page';
var title = this.ownerDocument.createElement('span');
title.textContent = this.pageName;
title.className = 'tile-page-title';
this.appendChild(title);
// Div that sets the vertical position of the tile grid.
this.topMargin_ = this.ownerDocument.createElement('div');
this.topMargin_.className = 'top-margin';
this.appendChild(this.topMargin_);
// Div that holds the tiles.
this.tileGrid_ = this.ownerDocument.createElement('div');
this.tileGrid_.className = 'tile-grid';
this.appendChild(this.tileGrid_);
// Ordered list of our tiles.
this.tileElements_ = this.tileGrid_.getElementsByClassName('tile real');
// These are properties used in updateTopMargin.
this.animatedTopMarginPx_ = 0;
this.topMarginPx_ = 0;
this.eventTracker = new EventTracker();
this.eventTracker.add(window, 'resize', this.onResize_.bind(this));
this.addEventListener('DOMNodeInsertedIntoDocument',
this.onNodeInsertedIntoDocument_);
this.tileGrid_.addEventListener('dragenter',
this.onDragEnter_.bind(this));
this.tileGrid_.addEventListener('dragover', this.onDragOver_.bind(this));
this.tileGrid_.addEventListener('drop', this.onDrop_.bind(this));
this.tileGrid_.addEventListener('dragleave',
this.onDragLeave_.bind(this));
},
get tileCount() {
return this.tileElements_.length;
},
get selected() {
return Array.prototype.indexOf.call(this.parentNode.children, this) ==
ntp4.getCardSlider().currentCard;
},
/**
* Cleans up resources that are no longer needed after this TilePage
* instance is removed from the DOM.
*/
tearDown: function() {
this.eventTracker.removeAll();
},
/**
* @protected
*/
appendTile: function(tileElement) {
this.addTileAt(tileElement, this.tileElements_.length);
},
/**
* Adds the given element to the tile grid.
* @param {Node} tileElement The tile object/node to insert.
* @param {number} index The location in the tile grid to insert it at.
* @protected
*/
addTileAt: function(tileElement, index) {
var wrapperDiv = new Tile(tileElement);
if (index == this.tileElements_.length) {
this.tileGrid_.appendChild(wrapperDiv);
} else {
this.tileGrid_.insertBefore(wrapperDiv,
this.tileElements_[index]);
}
this.calculateLayoutValues_();
this.positionTile_(index);
this.classList.remove('animating-tile-page');
},
/**
* Makes some calculations for tile layout. These change depending on
* height, width, and the number of tiles.
* @private
*/
calculateLayoutValues_: function() {
var grid = this.gridValues_;
var availableSpace = this.tileGrid_.clientWidth - 2 * MIN_WIDE_MARGIN;
var wide = availableSpace >= grid.minWideWidth;
var numRowTiles = wide ? grid.maxColCount : grid.minColCount;
var effectiveGridWidth = wide ?
Math.min(Math.max(availableSpace, grid.minWideWidth),
grid.maxWideWidth) :
grid.narrowWidth;
var realTileValues = tileValuesForGrid(effectiveGridWidth, numRowTiles);
// leftMargin centers the grid within the avaiable space.
var minMargin = wide ? MIN_WIDE_MARGIN : 0;
var leftMargin =
Math.max(minMargin,
(this.tileGrid_.clientWidth - effectiveGridWidth) / 2);
var rowHeight = this.heightForWidth(realTileValues.tileWidth) +
realTileValues.interTileSpacing;
this.layoutValues_ = {
numRowTiles: numRowTiles,
leftMargin: leftMargin,
colWidth: realTileValues.offsetX,
rowHeight: rowHeight,
tileWidth: realTileValues.tileWidth,
wide: wide,
};
// We need to update the top margin as well.
this.updateTopMargin_();
},
/**
* Calculates the x/y coordinates for an element and moves it there.
* @param {number} index The index of the element to be positioned.
* @param {number} indexOffset If provided, this is added to |index| when
* positioning the tile. The effect is that the tile will be positioned
* in a non-default location.
* @private
*/
positionTile_: function(index, indexOffset) {
var grid = this.gridValues_;
var layout = this.layoutValues_;
indexOffset = typeof indexOffset != 'undefined' ? indexOffset : 0;
// Add the offset _after_ the modulus division. We might want to show the
// tile off the side of the grid.
var col = index % layout.numRowTiles + indexOffset;
var row = Math.floor(index / layout.numRowTiles);
// Calculate the final on-screen position for the tile.
var realX = col * layout.colWidth + layout.leftMargin;
var realY = row * layout.rowHeight;
// Calculate the portion of the tile's position that should be animated.
var animatedTileValues = layout.wide ?
grid.wideTileValues : grid.narrowTileValues;
// Animate the difference between three-wide and six-wide.
var animatedLeftMargin = layout.wide ?
0 : (grid.minWideWidth - MIN_WIDE_MARGIN - grid.narrowWidth) / 2;
var animatedX = col * animatedTileValues.offsetX + animatedLeftMargin;
var animatedY = row * (this.heightForWidth(animatedTileValues.tileWidth) +
animatedTileValues.interTileSpacing);
var tile = this.tileElements_[index];
tile.setGridPosition(animatedX, animatedY);
tile.firstChild.setBounds(layout.tileWidth,
realX - animatedX,
realY - animatedY);
// This code calculates whether the tile needs to show a clone of itself
// wrapped around the other side of the tile grid.
var offTheRight = col == layout.numRowTiles ||
(col == layout.numRowTiles - 1 && tile.hasDoppleganger());
var offTheLeft = col == -1 || (col == 0 && tile.hasDoppleganger());
if (this.isCurrentDragTarget_ && (offTheRight || offTheLeft)) {
var sign = offTheRight ? 1 : -1;
tile.showDoppleganger(-layout.numRowTiles * layout.colWidth * sign,
layout.rowHeight * sign);
} else {
tile.clearDoppleganger();
}
if (index == this.tileElements_.length - 1)
this.tileGrid_.style.height = (realY + layout.rowHeight) + 'px';
},
/**
* Gets the index of the tile that should occupy coordinate (x, y). Note
* that this function doesn't care where the tiles actually are, and will
* return an index even for the space between two tiles. This function is
* effectively the inverse of |positionTile_|.
* @param {number} x The x coordinate, in pixels, relative to the top left
* of tileGrid_.
* @param {number} y The y coordinate.
* @private
*/
getWouldBeIndexForPoint_: function(x, y) {
var grid = this.gridValues_;
var layout = this.layoutValues_;
var col = Math.floor((x - layout.leftMargin) / layout.colWidth);
if (col < 0 || col >= layout.numRowTiles)
return -1;
var row = Math.floor(
(y - this.tileGrid_.getBoundingClientRect().top) / layout.rowHeight);
return row * layout.numRowTiles + col;
},
/**
* Window resize event handler. Window resizes may trigger re-layouts.
* @param {Object} e The resize event.
*/
onResize_: function(e) {
if (this.lastWidth_ == this.clientWidth &&
this.lastHeight_ == this.clientHeight) {
return;
}
this.calculateLayoutValues_();
this.lastWidth_ = this.clientWidth;
this.lastHeight_ = this.clientHeight;
this.classList.add('animating-tile-page');
this.heightChanged_();
for (var i = 0; i < this.tileElements_.length; i++) {
this.positionTile_(i);
}
},
/**
* The tile grid has an image mask which fades at the edges. We only show
* the mask when there is an active drag; it obscures doppleganger tiles
* as they enter or exit the grid.
* @private
*/
updateMask_: function() {
if (!this.isCurrentDragTarget_) {
this.tileGrid_.style.WebkitMaskBoxImage = '';
return;
}
var leftMargin = this.layoutValues_.leftMargin;
var fadeDistance = Math.min(leftMargin, 20);
var gradient =
'-webkit-linear-gradient(left,' +
'transparent, ' +
'transparent ' + (leftMargin - fadeDistance) + 'px, ' +
'black ' + leftMargin + 'px, ' +
'black ' + (this.tileGrid_.clientWidth - leftMargin) + 'px, ' +
'transparent ' + (this.tileGrid_.clientWidth - leftMargin +
fadeDistance) + 'px, ' +
'transparent)';
this.tileGrid_.style.WebkitMaskBoxImage = gradient;
},
updateTopMargin_: function() {
var layout = this.layoutValues_;
// The top margin is set so that the vertical midpoint of the grid will
// be 1/3 down the page.
var numTiles = this.tileCount +
(this.isCurrentDragTarget_ && !this.withinPageDrag_ ? 1 : 0);
var numRows = Math.ceil(numTiles / layout.numRowTiles);
var usedHeight = layout.rowHeight * numRows;
// 100 matches the top padding of tile-page.
var newMargin = document.documentElement.clientHeight / 3 -
usedHeight / 2 - 100;
newMargin = Math.max(newMargin, 0);
// |newMargin| is the final margin we actually want to show. However,
// part of that should be animated and part should not (for the same
// reason as with leftMargin). The approach is to consider differences
// when the layout changes from wide to narrow or vice versa as
// 'animatable'. These differences accumulate in animatedTopMarginPx_,
// while topMarginPx_ caches the real (total) margin. Either of these
// calculations may come out to be negative, so we use margins as the
// css property.
if (typeof this.topMarginIsForWide_ == 'undefined')
this.topMarginIsForWide_ = layout.wide;
if (this.topMarginIsForWide_ != layout.wide) {
this.animatedTopMarginPx_ += newMargin - this.topMarginPx_;
this.topMargin_.style.marginBottom =
this.animatedTopMarginPx_ + 'px';
}
this.topMarginIsForWide_ = layout.wide;
this.topMarginPx_ = newMargin;
this.topMargin_.style.marginTop =
(this.topMarginPx_ - this.animatedTopMarginPx_) + 'px';
},
/**
* Handles final setup that can only happen after |this| is inserted into
* the page.
*/
onNodeInsertedIntoDocument_: function(e) {
this.calculateLayoutValues_();
this.heightChanged_();
},
/**
* Called when the height of |this| has changed: update the size of
* tileGrid.
*/
heightChanged_: function() {
// The tile grid will expand to the bottom footer, or enough to hold all
// the tiles, whichever is greater. It would be nicer if tilePage were
// a flex box, and the tile grid could be box-flex: 1, but this exposes a
// bug where repositioning tiles will cause the scroll position to reset.
this.tileGrid_.style.minHeight = (this.clientHeight -
this.tileGrid_.offsetTop) + 'px';
},
/**
* Get the height for a tile of a certain width. Override this function to
* get non-square tiles.
* @param {number} width The pixel width of a tile.
* @return {number} The height for |width|.
*/
heightForWidth: function(width) {
return width;
},
/** Dragging **/
/**
* The number of un-paired dragenter events that have fired on |this|. This
* is incremented by |onDragEnter_| and decremented by |onDragLeave_|. This
* is necessary because dragging over child widgets will fire additional
* enter and leave events on |this|. A non-zero value does not necessarily
* indicate that |isCurrentDragTarget_| is true.
* @type {number}
* @private
*/
dragEnters_: 0,
/**
* Whether the tile page is currently being dragged over with data it can
* accept.
* @type {boolean}
* @private
*/
isCurrentDragTarget_: false,
/**
* Handler for dragenter events fired on |tileGrid_|.
* @param {Event} e A MouseEvent for the drag.
* @private
*/
onDragEnter_: function(e) {
if (++this.dragEnters_ == 1) {
this.doDragEnter_(e);
} else {
// Sometimes we'll get an enter event over a child element without an
// over event following it. In this case we have to still call the
// drag over handler so that we make the necessary updates (one visible
// symptom of not doing this is that the cursor's drag state will
// flicker during drags).
this.onDragOver_(e);
}
},
/**
* Thunk for dragover events fired on |tileGrid_|.
* @param {Event} e A MouseEvent for the drag.
* @private
*/
onDragOver_: function(e) {
if (!this.isCurrentDragTarget_)
return;
this.doDragOver_(e);
},
/**
* Thunk for drop events fired on |tileGrid_|.
* @param {Event} e A MouseEvent for the drag.
* @private
*/
onDrop_: function(e) {
this.dragEnters_ = 0;
if (!this.isCurrentDragTarget_)
return;
this.doDrop_(e);
},
/**
* Thunk for dragleave events fired on |tileGrid_|.
* @param {Event} e A MouseEvent for the drag.
* @private
*/
onDragLeave_: function(e) {
if (--this.dragEnters_ > 0)
return;
this.isCurrentDragTarget_ = false;
this.cleanUpDrag_();
},
/**
* Performs all actions necessary when a drag enters the tile page.
* @param {Event} e A mouseover event for the drag enter.
* @private
*/
doDragEnter_: function(e) {
if (!this.shouldAcceptDrag(e.dataTransfer))
return;
this.isCurrentDragTarget_ = true;
// Applies the mask so doppleganger tiles disappear into the fog.
this.updateMask_();
this.classList.add('animating-tile-page');
this.withinPageDrag_ = this.contains(currentlyDraggingTile);
this.dragItemIndex_ = this.withinPageDrag_ ?
currentlyDraggingTile.index : this.tileElements_.length;
this.currentDropIndex_ = this.dragItemIndex_;
// The new tile may change the number of rows, hence the top margin
// will change.
if (!this.withinPageDrag_)
this.updateTopMargin_();
this.doDragOver_(e);
},
/**
* Performs all actions necessary when the user moves the cursor during
* a drag over the tile page.
* @param {Event} e A mouseover event for the drag over.
* @private
*/
doDragOver_: function(e) {
e.preventDefault();
if (currentlyDraggingTile)
e.dataTransfer.dropEffect = 'move';
else
e.dataTransfer.dropEffect = 'copy';
var newDragIndex = this.getWouldBeIndexForPoint_(e.pageX, e.pageY);
if (newDragIndex < 0 || newDragIndex >= this.tileElements_.length)
newDragIndex = this.dragItemIndex_;
this.updateDropIndicator_(newDragIndex);
},
/**
* Performs all actions necessary when the user completes a drop.
* @param {Event} e A mouseover event for the drag drop.
* @private
*/
doDrop_: function(e) {
e.stopPropagation();
this.isCurrentDragTarget_ = false;
var index = this.currentDropIndex_;
// Only change data if this was not a 'null drag'.
if (!((index == this.dragItemIndex_) && this.withinPageDrag_)) {
var adjustedIndex = this.currentDropIndex_ +
(index > this.dragItemIndex_ ? 1 : 0);
if (currentlyDraggingTile) {
var originalPage = currentlyDraggingTile.tilePage;
this.tileGrid_.insertBefore(
currentlyDraggingTile,
this.tileElements_[adjustedIndex]);
if (originalPage != this)
originalPage.cleanUpDrag_();
this.tileMoved(currentlyDraggingTile);
} else {
this.addOutsideData(e.dataTransfer, adjustedIndex);
}
}
this.classList.remove('animating-tile-page');
this.cleanUpDrag_();
},
/**
* Makes sure all the tiles are in the right place after a drag is over.
* @private
*/
cleanUpDrag_: function() {
for (var i = 0; i < this.tileElements_.length; i++) {
// The current drag tile will be positioned in its dragend handler.
if (this.tileElements_[i] == currentlyDraggingTile)
continue;
this.positionTile_(i);
}
// Remove the drag mask.
this.updateMask_();
},
/**
* Updates the visual indicator for the drop location for the active drag.
* @param {Event} e A MouseEvent for the drag.
* @private
*/
updateDropIndicator_: function(newDragIndex) {
var oldDragIndex = this.currentDropIndex_;
if (newDragIndex == oldDragIndex)
return;
var repositionStart = Math.min(newDragIndex, oldDragIndex);
var repositionEnd = Math.max(newDragIndex, oldDragIndex);
for (var i = repositionStart; i <= repositionEnd; i++) {
if (i == this.dragItemIndex_)
continue;
else if (i > this.dragItemIndex_)
var adjustment = i <= newDragIndex ? -1 : 0;
else
var adjustment = i >= newDragIndex ? 1 : 0;
this.positionTile_(i, adjustment);
}
this.currentDropIndex_ = newDragIndex;
},
/**
* Checks if a page can accept a drag with the given data.
* @param {Object} dataTransfer The dataTransfer object, if the drag object
* is not a tile (e.g. it is a link).
* @return {boolean} True if this page can handle the drag.
*/
shouldAcceptDrag: function(dataTransfer) {
return false;
},
/**
* Called to accept a drag drop.
* @param {Object} dataTransfer The data transfer object that holds the drop
* data.
* @param {number} index The tile index at which the drop occurred.
*/
addOutsideData: function(dataTransfer, index) {
// This should not get called unless there is a non-default
// implementation.
assert(false);
},
/**
* Called when a tile has been moved (via dragging). Override this to make
* backend updates.
* @param {Node} draggedTile The tile that was dropped.
*/
tileMoved: function(draggedTile) {
},
};
return {
getCurrentlyDraggingTile: getCurrentlyDraggingTile,
TilePage: TilePage,
};
});
|