forked from NicholasMohr/ember-list-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-view.amd.js
1754 lines (1422 loc) · 51.5 KB
/
list-view.amd.js
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==========================================================================
// Project: Ember ListView
// Copyright: ©2012-2013 Erik Bryn, Yapp Inc., and contributors.
// License: Licensed under MIT license
// Version: 0.0.5
// ==========================================================================
define("list-view/helper",
["./list_view","./virtual_list_view","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var EmberListView = __dependency1__["default"];
var EmberVirtualListView = __dependency2__["default"];
var EmberVirtualList = createHelper(EmberVirtualListView);
var EmberList = createHelper(EmberListView);
function createHelper(view) {
if (Ember.HTMLBars) {
return function htmlBarsHelper(params, hash, options, env) {
hash.content = hash.items;
delete hash.items;
for (var prop in hash) {
if (/-/.test(prop)) {
var camelized = Ember.String.camelize(prop);
hash[camelized] = hash[prop];
delete hash[prop];
}
}
/*jshint validthis:true */
return Ember.HTMLBars.helpers.collection.helperFunction.call(this, [view], hash, options, env);
};
}
return function handelbarsHelperFactory(options) {
return createHandlebarsHelper.call(this, view, options);
};
}
function createHandlebarsHelper(view, options) {
var hash = options.hash;
var types = options.hashTypes;
hash.content = hash.items;
delete hash.items;
types.content = types.items;
delete types.items;
if (!hash.content) {
hash.content = 'this';
types.content = 'ID';
}
for (var prop in hash) {
if (/-/.test(prop)) {
var camelized = Ember.String.camelize(prop);
hash[camelized] = hash[prop];
types[camelized] = types[prop];
delete hash[prop];
delete types[prop];
}
}
/*jshint validthis:true */
return Ember.Handlebars.helpers.collection.call(this, view, options);
}
__exports__.EmberList = EmberList;
__exports__.EmberVirtualList = EmberVirtualList;
});
define("list-view/list_item_view",
["list-view/list_item_view_mixin","exports"],
function(__dependency1__, __exports__) {
"use strict";
/*jshint validthis:true */
var ListItemViewMixin = __dependency1__["default"];
var get = Ember.get, set = Ember.set;
/**
The `Ember.ListItemView` view class renders a
[div](https://developer.mozilla.org/en/HTML/Element/div) HTML element
with `ember-list-item-view` class. It allows you to specify a custom item
handlebars template for `Ember.ListView`.
Example:
```handlebars
<script type="text/x-handlebars" data-template-name="row_item">
{{name}}
</script>
```
```javascript
App.ListView = Ember.ListView.extend({
height: 500,
rowHeight: 20,
itemViewClass: Ember.ListItemView.extend({templateName: "row_item"})
});
```
@extends Ember.View
@class ListItemView
@namespace Ember
*/
__exports__["default"] = Ember.View.extend(ListItemViewMixin, {
updateContext: function(newContext) {
var context = get(this, 'context');
Ember.instrument('view.updateContext.render', this, function() {
if (context !== newContext) {
set(this, 'context', newContext);
if (newContext && newContext.isController) {
set(this, 'controller', newContext);
}
}
}, this);
},
rerender: function () {
if (this.isDestroying || this.isDestroyed) {
return;
}
return this._super.apply(this, arguments);
},
_contextDidChange: Ember.observer(function () {
Ember.run.once(this, this.rerender);
}, 'context', 'controller')
});
});
define("list-view/list_item_view_mixin",
["exports"],
function(__exports__) {
"use strict";
/*jshint validthis:true */
function samePosition(a, b) {
return a && b && a.x === b.x && a.y === b.y;
}
function positionElement() {
var element, position, _position;
Ember.instrument('view.updateContext.positionElement', this, function() {
element = this.element;
position = this.position;
_position = this._position;
if (!position || !element) {
return;
}
// // TODO: avoid needing this by avoiding unnecessary
// // calls to this method in the first place
if (samePosition(position, _position)) {
return;
}
Ember.run.schedule('render', this, this._parentView.applyTransform, this, position.x, position.y);
this._position = position;
}, this);
}
__exports__["default"] = Ember.Mixin.create({
classNames: ['ember-list-item-view'],
style: '',
attributeBindings: ['style'],
_position: null,
_positionElement: positionElement,
positionElementWhenInserted: Ember.on('init', function(){
this.one('didInsertElement', positionElement);
}),
updatePosition: function(position) {
this.position = position;
this._positionElement();
}
});
});
define("list-view/list_view",
["list-view/list_view_helper","list-view/list_view_mixin","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var ListViewHelper = __dependency1__["default"];
var ListViewMixin = __dependency2__["default"];
var get = Ember.get;
/**
The `Ember.ListView` view class renders a
[div](https://developer.mozilla.org/en/HTML/Element/div) HTML element,
with `ember-list-view` class.
The context of each item element within the `Ember.ListView` are populated
from the objects in the `Element.ListView`'s `content` property.
### `content` as an Array of Objects
The simplest version of an `Ember.ListView` takes an array of object as its
`content` property. The object will be used as the `context` each item element
inside the rendered `div`.
Example:
```javascript
App.ContributorsRoute = Ember.Route.extend({
model: function() {
return [{ name: 'Stefan Penner' }, { name: 'Alex Navasardyan' }, { name: 'Ray Cohen'}];
}
});
```
```handlebars
{{#ember-list items=contributors height=500 rowHeight=50}}
{{name}}
{{/ember-list}}
```
Would result in the following HTML:
```html
<div id="ember181" class="ember-view ember-list-view" style="height:500px;width:500px;position:relative;overflow:scroll;-webkit-overflow-scrolling:touch;overflow-scrolling:touch;">
<div class="ember-list-container">
<div id="ember186" class="ember-view ember-list-item-view" style="-webkit-transform: translate3d(0px, 0px, 0);">
<script id="metamorph-0-start" type="text/x-placeholder"></script>Stefan Penner<script id="metamorph-0-end" type="text/x-placeholder"></script>
</div>
<div id="ember187" class="ember-view ember-list-item-view" style="-webkit-transform: translate3d(0px, 50px, 0);">
<script id="metamorph-1-start" type="text/x-placeholder"></script>Alex Navasardyan<script id="metamorph-1-end" type="text/x-placeholder"></script>
</div>
<div id="ember188" class="ember-view ember-list-item-view" style="-webkit-transform: translate3d(0px, 100px, 0);">
<script id="metamorph-2-start" type="text/x-placeholder"></script>Rey Cohen<script id="metamorph-2-end" type="text/x-placeholder"></script>
</div>
<div id="ember189" class="ember-view ember-list-scrolling-view" style="height: 150px"></div>
</div>
</div>
```
By default `Ember.ListView` provides support for `height`,
`rowHeight`, `width`, `elementWidth`, `scrollTop` parameters.
Note, that `height` and `rowHeight` are required parameters.
```handlebars
{{#ember-list items=this height=500 rowHeight=50}}
{{name}}
{{/ember-list}}
```
If you would like to have multiple columns in your view layout, you can
set `width` and `elementWidth` parameters respectively.
```handlebars
{{#ember-list items=this height=500 rowHeight=50 width=500 elementWidth=80}}
{{name}}
{{/ember-list}}
```
### extending `Ember.ListView`
Example:
```handlebars
{{view App.ListView contentBinding="content"}}
<script type="text/x-handlebars" data-template-name="row_item">
{{name}}
</script>
```
```javascript
App.ListView = Ember.ListView.extend({
height: 500,
width: 500,
elementWidth: 80,
rowHeight: 20,
itemViewClass: Ember.ListItemView.extend({templateName: "row_item"})
});
```
@extends Ember.ContainerView
@class ListView
@namespace Ember
*/
__exports__["default"] = Ember.ContainerView.extend(ListViewMixin, {
css: {
position: 'relative',
overflow: 'auto',
'-webkit-overflow-scrolling': 'touch',
'overflow-scrolling': 'touch'
},
applyTransform: ListViewHelper.applyTransform,
_scrollTo: function(scrollTop) {
var element = this.element;
if (element) { element.scrollTop = scrollTop; }
},
didInsertElement: function() {
var that = this;
this._updateScrollableHeight();
this._scroll = function(e) { that.scroll(e); };
Ember.$(this.element).on('scroll', this._scroll);
},
willDestroyElement: function() {
Ember.$(this.element).off('scroll', this._scroll);
},
scroll: function(e) {
this.scrollTo(e.target.scrollTop);
},
scrollTo: function(y) {
this._scrollTo(y);
this._scrollContentTo(y);
},
totalHeightDidChange: Ember.observer(function () {
Ember.run.scheduleOnce('afterRender', this, this._updateScrollableHeight);
}, 'totalHeight'),
_updateScrollableHeight: function () {
var height, state;
// Support old and new Ember versions
state = this._state || this.state;
if (state === 'inDOM') {
// if the list is currently displaying the emptyView, remove the height
if (this._isChildEmptyView()) {
height = '';
} else {
height = get(this, 'totalHeight');
}
this.$('.ember-list-container').css({
height: height
});
}
}
});
});
define("list-view/list_view_helper",
["exports"],
function(__exports__) {
"use strict";
// TODO - remove this!
var el = document.body || document.createElement('div');
var style = el.style;
var set = Ember.set;
function getElementStyle (prop) {
var uppercaseProp = prop.charAt(0).toUpperCase() + prop.slice(1);
var props = [
prop,
'webkit' + prop,
'webkit' + uppercaseProp,
'Moz' + uppercaseProp,
'moz' + uppercaseProp,
'ms' + uppercaseProp,
'ms' + prop
];
for (var i=0; i < props.length; i++) {
var property = props[i];
if (property in style) {
return property;
}
}
return null;
}
function getCSSStyle (attr) {
var styleName = getElementStyle(attr);
var prefix = styleName.toLowerCase().replace(attr, '');
var dic = {
webkit: '-webkit-' + attr,
moz: '-moz-' + attr,
ms: '-ms-' + attr
};
if (prefix && dic[prefix]) {
return dic[prefix];
}
return styleName;
}
var styleAttributeName = getElementStyle('transform');
var transformProp = getCSSStyle('transform');
var perspectiveProp = getElementStyle('perspective');
var supports2D = !!transformProp;
var supports3D = !!perspectiveProp;
function setStyle (optionalStyleString) {
return function (obj, x, y) {
var isElement = obj instanceof Element;
if (optionalStyleString && (supports2D || supports3D)) {
var style = Ember.String.fmt(optionalStyleString, x, y);
if (isElement) {
obj.style[styleAttributeName] = style;
} else {
set(obj, 'style', transformProp + ': ' + style);
}
} else {
if (isElement) {
obj.style.top = y;
obj.style.left = x;
}
}
};
}
__exports__["default"] = {
transformProp: transformProp,
applyTransform: (function () {
if (supports2D) {
return setStyle('translate(%@px, %@px)');
}
return setStyle();
})(),
apply3DTransform: (function () {
if (supports3D) {
return setStyle('translate3d(%@px, %@px, 0)');
} else if (supports2D) {
return setStyle('translate(%@px, %@px)');
}
return setStyle();
})()
};
});
define("list-view/list_view_mixin",
["list-view/reusable_list_item_view","exports"],
function(__dependency1__, __exports__) {
"use strict";
/*jshint validthis:true */
var ReusableListItemView = __dependency1__["default"];
var get = Ember.get;
var set = Ember.set;
var min = Math.min;
var max = Math.max;
var floor = Math.floor;
var ceil = Math.ceil;
var forEach = Ember.ArrayPolyfills.forEach;
function addContentArrayObserver() {
var content = get(this, 'content');
if (content) {
content.addArrayObserver(this);
}
}
function removeAndDestroy(object) {
this.removeObject(object);
object.destroy();
}
function syncChildViews() {
Ember.run.once(this, '_syncChildViews');
}
function sortByContentIndex (viewOne, viewTwo) {
return get(viewOne, 'contentIndex') - get(viewTwo, 'contentIndex');
}
function removeEmptyView() {
var emptyView = get(this, 'emptyView');
if (emptyView && emptyView instanceof Ember.View) {
emptyView.removeFromParent();
if (this.totalHeightDidChange !== undefined) {
this.totalHeightDidChange();
}
}
}
function addEmptyView() {
var emptyView = get(this, 'emptyView');
if (!emptyView) {
return;
}
if ('string' === typeof emptyView) {
emptyView = get(emptyView) || emptyView;
}
emptyView = this.createChildView(emptyView);
set(this, 'emptyView', emptyView);
if (Ember.CoreView.detect(emptyView)) {
this._createdEmptyView = emptyView;
}
this.unshiftObject(emptyView);
}
function enableProfilingOutput() {
function before(name, time/*, payload*/) {
console.time(name);
}
function after (name, time/*, payload*/) {
console.timeEnd(name);
}
if (Ember.ENABLE_PROFILING) {
Ember.subscribe('view._scrollContentTo', {
before: before,
after: after
});
Ember.subscribe('view.updateContext', {
before: before,
after: after
});
}
}
/**
@class Ember.ListViewMixin
@namespace Ember
*/
__exports__["default"] = Ember.Mixin.create({
itemViewClass: ReusableListItemView,
emptyViewClass: Ember.View,
classNames: ['ember-list-view'],
attributeBindings: ['style'],
classNameBindings: ['_isGrid:ember-list-view-grid:ember-list-view-list'],
scrollTop: 0,
bottomPadding: 0, // TODO: maybe this can go away
_lastEndingIndex: 0,
paddingCount: 1,
_cachedPos: 0,
_isGrid: Ember.computed('columnCount', function() {
return this.get('columnCount') > 1;
}).readOnly(),
/**
@private
Setup a mixin.
- adding observer to content array
- creating child views based on height and length of the content array
@method init
*/
init: function() {
this._super();
this._cachedHeights = [0];
this.on('didInsertElement', this._syncListContainerWidth);
this.columnCountDidChange();
this._syncChildViews();
this._addContentArrayObserver();
},
_addContentArrayObserver: Ember.beforeObserver(function() {
addContentArrayObserver.call(this);
}, 'content'),
/**
Called on your view when it should push strings of HTML into a
`Ember.RenderBuffer`.
Adds a [div](https://developer.mozilla.org/en-US/docs/HTML/Element/div)
with a required `ember-list-container` class.
@method render
@param {Ember.RenderBuffer} buffer The render buffer
*/
render: function (buffer) {
var element = buffer.element();
var dom = buffer.dom;
var container = dom.createElement('div');
container.className = 'ember-list-container';
element.appendChild(container);
this._childViewsMorph = dom.createMorph(container, container, null);
return container;
},
createChildViewsMorph: function (element) {
this._childViewsMorph = this._renderer._dom.createMorph(element.lastChild, element.lastChild, null);
return element;
},
willInsertElement: function() {
if (!this.get('height') || !this.get('rowHeight')) {
throw new Error('A ListView must be created with a height and a rowHeight.');
}
this._super();
},
/**
@private
Sets inline styles of the view:
- height
- width
- position
- overflow
- -webkit-overflow
- overflow-scrolling
Called while attributes binding.
@property {Ember.ComputedProperty} style
*/
style: Ember.computed('height', 'width', function() {
var height, width, style, css;
height = get(this, 'height');
width = get(this, 'width');
css = get(this, 'css');
style = '';
if (height) {
style += 'height:' + height + 'px;';
}
if (width) {
style += 'width:' + width + 'px;';
}
for ( var rule in css ) {
if (css.hasOwnProperty(rule)) {
style += rule + ':' + css[rule] + ';';
}
}
return style;
}),
/**
@private
Performs visual scrolling. Is overridden in Ember.ListView.
@method scrollTo
*/
scrollTo: function(y) {
throw new Error('must override to perform the visual scroll and effectively delegate to _scrollContentTo');
},
/**
@private
Internal method used to force scroll position
@method scrollTo
*/
_scrollTo: Ember.K,
/**
@private
@method _scrollContentTo
*/
_scrollContentTo: function(y) {
var startingIndex, endingIndex,
contentIndex, visibleEndingIndex, maxContentIndex,
contentIndexEnd, contentLength, scrollTop, content;
scrollTop = max(0, y);
if (this.scrollTop === scrollTop) {
return;
}
// allow a visual overscroll, but don't scroll the content. As we are doing needless
// recycyling, and adding unexpected nodes to the DOM.
var maxScrollTop = max(0, get(this, 'totalHeight') - get(this, 'height'));
scrollTop = min(scrollTop, maxScrollTop);
content = get(this, 'content');
contentLength = get(content, 'length');
startingIndex = this._startingIndex(contentLength);
Ember.instrument('view._scrollContentTo', {
scrollTop: scrollTop,
content: content,
startingIndex: startingIndex,
endingIndex: min(max(contentLength - 1, 0), startingIndex + this._numChildViewsForViewport())
}, function () {
this.scrollTop = scrollTop;
maxContentIndex = max(contentLength - 1, 0);
startingIndex = this._startingIndex();
visibleEndingIndex = startingIndex + this._numChildViewsForViewport();
endingIndex = min(maxContentIndex, visibleEndingIndex);
if (startingIndex === this._lastStartingIndex &&
endingIndex === this._lastEndingIndex) {
this.trigger('scrollYChanged', y);
return;
} else {
Ember.run(this, function() {
this._reuseChildren();
this._lastStartingIndex = startingIndex;
this._lastEndingIndex = endingIndex;
this.trigger('scrollYChanged', y);
});
}
}, this);
},
/**
@private
Computes the height for a `Ember.ListView` scrollable container div.
You must specify `rowHeight` parameter for the height to be computed properly.
@property {Ember.ComputedProperty} totalHeight
*/
totalHeight: Ember.computed('content.length',
'rowHeight',
'columnCount',
'bottomPadding', function() {
if (typeof this.heightForIndex === 'function') {
return this._totalHeightWithHeightForIndex();
} else {
return this._totalHeightWithStaticRowHeight();
}
}),
_doRowHeightDidChange: function() {
this._cachedHeights = [0];
this._cachedPos = 0;
this._syncChildViews();
},
_rowHeightDidChange: Ember.observer('rowHeight', function() {
Ember.run.once(this, this._doRowHeightDidChange);
}),
_totalHeightWithHeightForIndex: function() {
var length = this.get('content.length');
return this._cachedHeightLookup(length);
},
_totalHeightWithStaticRowHeight: function() {
var contentLength, rowHeight, columnCount, bottomPadding;
contentLength = get(this, 'content.length');
rowHeight = get(this, 'rowHeight');
columnCount = get(this, 'columnCount');
bottomPadding = get(this, 'bottomPadding');
return ((ceil(contentLength / columnCount)) * rowHeight) + bottomPadding;
},
/**
@private
@method _prepareChildForReuse
*/
_prepareChildForReuse: function(childView) {
childView.prepareForReuse();
},
createChildView: function (_view) {
return this._super(_view, this._itemViewProps || {});
},
/**
@private
@method _reuseChildForContentIndex
*/
_reuseChildForContentIndex: function(childView, contentIndex) {
var content, context, newContext, childsCurrentContentIndex, position, enableProfiling, oldChildView;
var contentViewClass = this.itemViewForIndex(contentIndex);
if (childView.constructor !== contentViewClass) {
// rather then associative arrays, lets move childView + contentEntry maping to a Map
var i = this._childViews.indexOf(childView);
childView.destroy();
childView = this.createChildView(contentViewClass);
this.insertAt(i, childView);
}
content = get(this, 'content');
enableProfiling = get(this, 'enableProfiling');
position = this.positionForIndex(contentIndex);
childView.updatePosition(position);
set(childView, 'contentIndex', contentIndex);
if (enableProfiling) {
Ember.instrument('view._reuseChildForContentIndex', position, function() {
}, this);
}
newContext = content.objectAt(contentIndex);
childView.updateContext(newContext);
},
/**
@private
@method positionForIndex
*/
positionForIndex: function(index) {
if (typeof this.heightForIndex !== 'function') {
return this._singleHeightPosForIndex(index);
}
else {
return this._multiHeightPosForIndex(index);
}
},
_singleHeightPosForIndex: function(index) {
var elementWidth, width, columnCount, rowHeight, y, x;
elementWidth = get(this, 'elementWidth') || 1;
width = get(this, 'width') || 1;
columnCount = get(this, 'columnCount');
rowHeight = get(this, 'rowHeight');
y = (rowHeight * floor(index/columnCount));
x = (index % columnCount) * elementWidth;
return {
y: y,
x: x
};
},
// 0 maps to 0, 1 maps to heightForIndex(i)
_multiHeightPosForIndex: function(index) {
var elementWidth, width, columnCount, rowHeight, y, x;
elementWidth = get(this, 'elementWidth') || 1;
width = get(this, 'width') || 1;
columnCount = get(this, 'columnCount');
x = (index % columnCount) * elementWidth;
y = this._cachedHeightLookup(index);
return {
x: x,
y: y
};
},
_cachedHeightLookup: function(index) {
for (var i = this._cachedPos; i < index; i++) {
this._cachedHeights[i + 1] = this._cachedHeights[i] + this.heightForIndex(i);
}
this._cachedPos = i;
return this._cachedHeights[index];
},
/**
@private
@method _childViewCount
*/
_childViewCount: function() {
var contentLength, childViewCountForHeight;
contentLength = get(this, 'content.length');
childViewCountForHeight = this._numChildViewsForViewport();
return min(contentLength, childViewCountForHeight);
},
/**
@private
Returns a number of columns in the Ember.ListView (for grid layout).
If you want to have a multi column layout, you need to specify both
`width` and `elementWidth`.
If no `elementWidth` is specified, it returns `1`. Otherwise, it will
try to fit as many columns as possible for a given `width`.
@property {Ember.ComputedProperty} columnCount
*/
columnCount: Ember.computed('width', 'elementWidth', function() {
var elementWidth, width, count;
elementWidth = get(this, 'elementWidth');
width = get(this, 'width');
if (elementWidth && width > elementWidth) {
count = floor(width / elementWidth);
} else {
count = 1;
}
return count;
}),
/**
@private
Fires every time column count is changed.
@event columnCountDidChange
*/
columnCountDidChange: Ember.observer(function() {
var ratio, currentScrollTop, proposedScrollTop, maxScrollTop,
scrollTop, lastColumnCount, newColumnCount, element;
lastColumnCount = this._lastColumnCount;
currentScrollTop = this.scrollTop;
newColumnCount = get(this, 'columnCount');
maxScrollTop = get(this, 'maxScrollTop');
element = this.element;
this._lastColumnCount = newColumnCount;
if (lastColumnCount) {
ratio = (lastColumnCount / newColumnCount);
proposedScrollTop = currentScrollTop * ratio;
scrollTop = min(maxScrollTop, proposedScrollTop);
this._scrollTo(scrollTop);
this.scrollTop = scrollTop;
}
if (arguments.length > 0) {
// invoked by observer
Ember.run.schedule('afterRender', this, this._syncListContainerWidth);
}
}, 'columnCount'),
/**
@private
Computes max possible scrollTop value given the visible viewport
and scrollable container div height.
@property {Ember.ComputedProperty} maxScrollTop
*/
maxScrollTop: Ember.computed('height', 'totalHeight', function(){
var totalHeight, viewportHeight;
totalHeight = get(this, 'totalHeight');
viewportHeight = get(this, 'height');
return max(0, totalHeight - viewportHeight);
}),
/**
@private
Determines whether the emptyView is the current childView.
@method _isChildEmptyView
*/
_isChildEmptyView: function() {
var emptyView = get(this, 'emptyView');
return emptyView && emptyView instanceof Ember.View &&
this._childViews.length === 1 && this._childViews.indexOf(emptyView) === 0;
},
/**
@private
Computes the number of views that would fit in the viewport area.
You must specify `height` and `rowHeight` parameters for the number of
views to be computed properly.
@method _numChildViewsForViewport
*/
_numChildViewsForViewport: function() {
if (this.heightForIndex) {
return this._numChildViewsForViewportWithMultiHeight();
} else {