2013-12-04 13:23:00 -05:00
|
|
|
(function () {
|
|
|
|
|
|
|
|
/**
|
|
|
|
Display a list of cloaked items
|
|
|
|
|
2014-01-17 16:40:43 -05:00
|
|
|
@class CloakedCollectionView
|
|
|
|
@extends Ember.CollectionView
|
2013-12-04 13:23:00 -05:00
|
|
|
@namespace Ember
|
|
|
|
**/
|
|
|
|
Ember.CloakedCollectionView = Ember.CollectionView.extend({
|
|
|
|
topVisible: null,
|
|
|
|
bottomVisible: null,
|
2014-06-06 12:59:07 -04:00
|
|
|
offsetFixedTopElement: null,
|
|
|
|
offsetFixedBottomElement: null,
|
2013-12-04 13:23:00 -05:00
|
|
|
|
|
|
|
init: function() {
|
|
|
|
var cloakView = this.get('cloakView'),
|
2014-03-31 14:32:32 -04:00
|
|
|
idProperty = this.get('idProperty'),
|
|
|
|
uncloakDefault = !!this.get('uncloakDefault');
|
2013-12-04 13:23:00 -05:00
|
|
|
|
|
|
|
// Set the slack ratio differently to allow for more or less slack in preloading
|
|
|
|
var slackRatio = parseFloat(this.get('slackRatio'));
|
|
|
|
if (!slackRatio) { this.set('slackRatio', 1.0); }
|
|
|
|
|
|
|
|
this.set('itemViewClass', Ember.CloakedView.extend({
|
|
|
|
classNames: [cloakView + '-cloak'],
|
|
|
|
cloaks: cloakView,
|
2014-02-13 14:05:12 -05:00
|
|
|
preservesContext: this.get('preservesContext') === "true",
|
|
|
|
cloaksController: this.get('itemController'),
|
2014-03-17 12:00:59 -04:00
|
|
|
defaultHeight: this.get('defaultHeight'),
|
2013-12-04 13:23:00 -05:00
|
|
|
|
|
|
|
init: function() {
|
|
|
|
this._super();
|
2014-02-13 14:05:12 -05:00
|
|
|
|
|
|
|
if (idProperty) {
|
|
|
|
this.set('elementId', cloakView + '-cloak-' + this.get('content.' + idProperty));
|
|
|
|
}
|
2014-03-31 14:32:32 -04:00
|
|
|
if (uncloakDefault) {
|
|
|
|
this.uncloak();
|
|
|
|
} else {
|
|
|
|
this.cloak();
|
|
|
|
}
|
2013-12-04 13:23:00 -05:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
this._super();
|
|
|
|
Ember.run.next(this, 'scrolled');
|
|
|
|
},
|
|
|
|
|
2014-02-13 14:05:12 -05:00
|
|
|
|
2013-12-04 13:23:00 -05:00
|
|
|
/**
|
|
|
|
If the topmost visible view changed, we will notify the controller if it has an appropriate hook.
|
|
|
|
|
|
|
|
@method _topVisibleChanged
|
|
|
|
@observes topVisible
|
|
|
|
**/
|
|
|
|
_topVisibleChanged: function() {
|
|
|
|
var controller = this.get('controller');
|
|
|
|
if (controller.topVisibleChanged) { controller.topVisibleChanged(this.get('topVisible')); }
|
|
|
|
}.observes('topVisible'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
If the bottommost visible view changed, we will notify the controller if it has an appropriate hook.
|
|
|
|
|
|
|
|
@method _bottomVisible
|
|
|
|
@observes bottomVisible
|
|
|
|
**/
|
|
|
|
_bottomVisible: function() {
|
|
|
|
var controller = this.get('controller');
|
|
|
|
if (controller.bottomVisibleChanged) { controller.bottomVisibleChanged(this.get('bottomVisible')); }
|
|
|
|
}.observes('bottomVisible'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
Binary search for finding the topmost view on screen.
|
|
|
|
|
|
|
|
@method findTopView
|
|
|
|
@param {Array} childViews the childViews to search through
|
|
|
|
@param {Number} windowTop The top of the viewport to search against
|
|
|
|
@param {Number} min The minimum index to search through of the child views
|
|
|
|
@param {Number} max The max index to search through of the child views
|
|
|
|
@returns {Number} the index into childViews of the topmost view
|
|
|
|
**/
|
|
|
|
findTopView: function(childViews, viewportTop, min, max) {
|
|
|
|
if (max < min) { return min; }
|
|
|
|
|
2014-06-10 01:04:34 -04:00
|
|
|
var wrapperTop = this.get('wrapperTop')>>0;
|
|
|
|
|
|
|
|
while(max>min){
|
|
|
|
var mid = Math.floor((min + max) / 2),
|
|
|
|
// in case of not full-window scrolling
|
|
|
|
$view = childViews[mid].$(),
|
|
|
|
viewBottom = $view.position().top + wrapperTop + $view.height();
|
|
|
|
|
|
|
|
if (viewBottom > viewportTop) {
|
|
|
|
max = mid-1;
|
|
|
|
} else {
|
|
|
|
min = mid+1;
|
|
|
|
}
|
2013-12-04 13:23:00 -05:00
|
|
|
}
|
2014-06-10 01:04:34 -04:00
|
|
|
|
|
|
|
return min;
|
2013-12-04 13:23:00 -05:00
|
|
|
},
|
|
|
|
|
2014-06-10 01:04:34 -04:00
|
|
|
|
2013-12-04 13:23:00 -05:00
|
|
|
/**
|
|
|
|
Determine what views are onscreen and cloak/uncloak them as necessary.
|
|
|
|
|
|
|
|
@method scrolled
|
|
|
|
**/
|
|
|
|
scrolled: function() {
|
2014-02-28 13:03:05 -05:00
|
|
|
if (!this.get('scrollingEnabled')) { return; }
|
|
|
|
|
2013-12-04 13:23:00 -05:00
|
|
|
var childViews = this.get('childViews');
|
2013-12-04 13:28:56 -05:00
|
|
|
if ((!childViews) || (childViews.length === 0)) { return; }
|
2013-12-04 13:23:00 -05:00
|
|
|
|
2014-06-10 01:04:34 -04:00
|
|
|
var self = this,
|
|
|
|
toUncloak = [],
|
2014-02-13 14:05:12 -05:00
|
|
|
onscreen = [],
|
2014-06-10 01:04:34 -04:00
|
|
|
onscreenCloaks = [],
|
2014-02-13 14:05:12 -05:00
|
|
|
// calculating viewport edges
|
2013-12-04 13:23:00 -05:00
|
|
|
$w = $(window),
|
2014-02-13 14:05:12 -05:00
|
|
|
windowHeight = this.get('wrapperHeight') || ( window.innerHeight ? window.innerHeight : $w.height() ),
|
|
|
|
windowTop = this.get('wrapperTop') || $w.scrollTop(),
|
2013-12-04 13:23:00 -05:00
|
|
|
slack = Math.round(windowHeight * this.get('slackRatio')),
|
|
|
|
viewportTop = windowTop - slack,
|
|
|
|
windowBottom = windowTop + windowHeight,
|
|
|
|
viewportBottom = windowBottom + slack,
|
|
|
|
topView = this.findTopView(childViews, viewportTop, 0, childViews.length-1),
|
2014-02-13 14:05:12 -05:00
|
|
|
bodyHeight = this.get('wrapperHeight') ? this.$().height() : $('body').height(),
|
2013-12-04 13:23:00 -05:00
|
|
|
bottomView = topView,
|
2014-06-06 12:59:07 -04:00
|
|
|
offsetFixedTopElement = this.get('offsetFixedTopElement'),
|
|
|
|
offsetFixedBottomElement = this.get('offsetFixedBottomElement');
|
2013-12-04 13:23:00 -05:00
|
|
|
|
|
|
|
if (windowBottom > bodyHeight) { windowBottom = bodyHeight; }
|
|
|
|
if (viewportBottom > bodyHeight) { viewportBottom = bodyHeight; }
|
|
|
|
|
2014-06-06 12:59:07 -04:00
|
|
|
if (offsetFixedTopElement) {
|
|
|
|
windowTop += (offsetFixedTopElement.outerHeight(true) || 0);
|
2014-02-13 14:05:12 -05:00
|
|
|
}
|
2014-06-06 12:59:07 -04:00
|
|
|
|
|
|
|
if (offsetFixedBottomElement) {
|
|
|
|
windowBottom -= (offsetFixedBottomElement.outerHeight(true) || 0);
|
|
|
|
}
|
|
|
|
|
2013-12-04 13:23:00 -05:00
|
|
|
// Find the bottom view and what's onscreen
|
|
|
|
while (bottomView < childViews.length) {
|
|
|
|
var view = childViews[bottomView],
|
|
|
|
$view = view.$(),
|
2014-02-13 14:05:12 -05:00
|
|
|
// in case of not full-window scrolling
|
2014-06-06 12:59:07 -04:00
|
|
|
scrollOffset = this.get('wrapperTop') || 0,
|
|
|
|
viewTop = $view.offset().top + scrollOffset,
|
2013-12-04 13:23:00 -05:00
|
|
|
viewBottom = viewTop + $view.height();
|
|
|
|
|
|
|
|
if (viewTop > viewportBottom) { break; }
|
|
|
|
toUncloak.push(view);
|
|
|
|
|
|
|
|
if (viewBottom > windowTop && viewTop <= windowBottom) {
|
|
|
|
onscreen.push(view.get('content'));
|
2014-06-10 01:04:34 -04:00
|
|
|
onscreenCloaks.push(view);
|
2013-12-04 13:23:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bottomView++;
|
|
|
|
}
|
|
|
|
if (bottomView >= childViews.length) { bottomView = childViews.length - 1; }
|
|
|
|
|
|
|
|
// If our controller has a `sawObjects` method, pass the on screen objects to it.
|
|
|
|
var controller = this.get('controller');
|
|
|
|
if (onscreen.length) {
|
|
|
|
this.setProperties({topVisible: onscreen[0], bottomVisible: onscreen[onscreen.length-1]});
|
|
|
|
if (controller && controller.sawObjects) {
|
|
|
|
Em.run.schedule('afterRender', function() {
|
|
|
|
controller.sawObjects(onscreen);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.setProperties({topVisible: null, bottomVisible: null});
|
|
|
|
}
|
|
|
|
|
2014-01-17 16:40:43 -05:00
|
|
|
var toCloak = childViews.slice(0, topView).concat(childViews.slice(bottomView+1));
|
2014-06-10 01:04:34 -04:00
|
|
|
|
|
|
|
this._uncloak = toUncloak;
|
|
|
|
if(this._nextUncloak){
|
|
|
|
Em.run.cancel(this._nextUncloak);
|
|
|
|
this._nextUncloak = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Em.run.schedule('afterRender', this, function() {
|
|
|
|
onscreenCloaks.forEach(function (v) {
|
|
|
|
if(v && v.uncloak) {
|
|
|
|
v.uncloak();
|
|
|
|
}
|
|
|
|
});
|
2013-12-04 13:23:00 -05:00
|
|
|
toCloak.forEach(function (v) { v.cloak(); });
|
2014-06-10 01:04:34 -04:00
|
|
|
if(self._nextUncloak){Em.run.cancel(self._nextUncloak)}
|
|
|
|
self._nextUncloak = Em.run.later(self, self.uncloakQueue,50);
|
2013-12-04 13:23:00 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
for (var j=bottomView; j<childViews.length; j++) {
|
|
|
|
var checkView = childViews[j];
|
2014-06-01 19:56:06 -04:00
|
|
|
if (!checkView._containedView) {
|
2013-12-04 13:23:00 -05:00
|
|
|
if (!checkView.get('loading')) {
|
2014-01-17 16:40:43 -05:00
|
|
|
checkView.$().html(this.get('loadingHTML') || "Loading...");
|
2013-12-04 13:23:00 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-06-10 01:04:34 -04:00
|
|
|
uncloakQueue: function(){
|
|
|
|
var maxPerRun = 3, delay = 50, processed = 0, self = this;
|
|
|
|
|
|
|
|
if(this._uncloak){
|
|
|
|
while(processed < maxPerRun && this._uncloak.length>0){
|
|
|
|
var view = this._uncloak.shift();
|
|
|
|
if(view && view.uncloak && !view._containedView){
|
|
|
|
Em.run.schedule('afterRender', view, view.uncloak);
|
|
|
|
processed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(this._uncloak.length === 0){
|
|
|
|
this._uncloak = null;
|
|
|
|
} else {
|
|
|
|
Em.run.schedule('afterRender', self, function(){
|
|
|
|
if(self._nextUncloak){
|
|
|
|
Em.run.cancel(self._nextUncloak);
|
|
|
|
}
|
|
|
|
self._nextUncloak = Em.run.next(self, function(){
|
|
|
|
if(self._nextUncloak){
|
|
|
|
Em.run.cancel(self._nextUncloak);
|
|
|
|
}
|
|
|
|
self._nextUncloak = Em.run.later(self,self.uncloakQueue,delay);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-12-04 13:23:00 -05:00
|
|
|
scrollTriggered: function() {
|
|
|
|
Em.run.scheduleOnce('afterRender', this, 'scrolled');
|
|
|
|
},
|
|
|
|
|
2014-02-13 14:05:12 -05:00
|
|
|
_startEvents: function() {
|
2014-06-06 12:59:07 -04:00
|
|
|
if (this.get('offsetFixed')) {
|
|
|
|
Em.warn("Cloaked-collection's `offsetFixed` is deprecated. Use `offsetFixedTop` instead.");
|
|
|
|
}
|
|
|
|
|
2013-12-04 13:23:00 -05:00
|
|
|
var self = this,
|
2014-06-06 12:59:07 -04:00
|
|
|
offsetFixedTop = this.get('offsetFixedTop') || this.get('offsetFixed'),
|
|
|
|
offsetFixedBottom = this.get('offsetFixedBottom'),
|
2013-12-04 13:23:00 -05:00
|
|
|
onScrollMethod = function() {
|
|
|
|
Ember.run.debounce(self, 'scrollTriggered', 10);
|
|
|
|
};
|
|
|
|
|
2014-06-06 12:59:07 -04:00
|
|
|
if (offsetFixedTop) {
|
|
|
|
this.set('offsetFixedTopElement', $(offsetFixedTop));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offsetFixedBottom) {
|
|
|
|
this.set('offsetFixedBottomElement', $(offsetFixedBottom));
|
2014-02-13 14:05:12 -05:00
|
|
|
}
|
|
|
|
|
2013-12-04 13:23:00 -05:00
|
|
|
$(document).bind('touchmove.ember-cloak', onScrollMethod);
|
|
|
|
$(window).bind('scroll.ember-cloak', onScrollMethod);
|
2014-02-13 14:05:12 -05:00
|
|
|
this.addObserver('wrapperTop', self, onScrollMethod);
|
|
|
|
this.addObserver('wrapperHeight', self, onScrollMethod);
|
2014-03-21 14:30:03 -04:00
|
|
|
this.addObserver('content.@each', self, onScrollMethod);
|
2014-03-17 12:00:59 -04:00
|
|
|
this.scrollTriggered();
|
2014-02-28 13:03:05 -05:00
|
|
|
|
|
|
|
this.set('scrollingEnabled', true);
|
2014-02-13 14:05:12 -05:00
|
|
|
}.on('didInsertElement'),
|
2013-12-04 13:23:00 -05:00
|
|
|
|
2014-02-28 13:03:05 -05:00
|
|
|
cleanUp: function() {
|
2014-01-17 16:40:43 -05:00
|
|
|
$(document).unbind('touchmove.ember-cloak');
|
|
|
|
$(window).unbind('scroll.ember-cloak');
|
2014-02-28 13:03:05 -05:00
|
|
|
this.set('scrollingEnabled', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
_endEvents: function() {
|
|
|
|
this.cleanUp();
|
2014-02-13 14:05:12 -05:00
|
|
|
}.on('willDestroyElement')
|
2013-12-04 13:23:00 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
A cloaked view is one that removes its content when scrolled off the screen
|
|
|
|
|
|
|
|
@class CloakedView
|
|
|
|
@extends Ember.View
|
|
|
|
@namespace Ember
|
|
|
|
**/
|
|
|
|
Ember.CloakedView = Ember.View.extend({
|
|
|
|
attributeBindings: ['style'],
|
|
|
|
|
|
|
|
/**
|
|
|
|
Triggers the set up for rendering a view that is cloaked.
|
|
|
|
|
|
|
|
@method uncloak
|
|
|
|
*/
|
|
|
|
uncloak: function() {
|
2014-06-01 19:56:06 -04:00
|
|
|
if (!this._containedView) {
|
2014-02-13 14:05:12 -05:00
|
|
|
var model = this.get('content'),
|
|
|
|
controller = null,
|
|
|
|
container = this.get('container');
|
|
|
|
|
|
|
|
// Wire up the itemController if necessary
|
|
|
|
var controllerName = this.get('cloaksController');
|
|
|
|
if (controllerName) {
|
|
|
|
var controllerFullName = 'controller:' + controllerName,
|
|
|
|
factory = container.lookupFactory(controllerFullName),
|
|
|
|
parentController = this.get('controller');
|
|
|
|
|
|
|
|
// let ember generate controller if needed
|
|
|
|
if (factory === undefined) {
|
|
|
|
factory = Ember.generateControllerFactory(container, controllerName, model);
|
|
|
|
|
|
|
|
// inform developer about typo
|
2014-03-17 12:00:59 -04:00
|
|
|
Ember.Logger.warn('ember-cloaking: can\'t lookup controller by name "' + controllerFullName + '".');
|
|
|
|
Ember.Logger.warn('ember-cloaking: using ' + factory.toString() + '.');
|
2014-02-13 14:05:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
controller = factory.create({
|
|
|
|
model: model,
|
|
|
|
parentController: parentController,
|
|
|
|
target: parentController
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var createArgs = {},
|
|
|
|
target = controller || model;
|
2013-12-04 13:23:00 -05:00
|
|
|
|
2014-02-13 14:05:12 -05:00
|
|
|
if (this.get('preservesContext')) {
|
|
|
|
createArgs.content = target;
|
|
|
|
} else {
|
|
|
|
createArgs.context = target;
|
|
|
|
}
|
|
|
|
if (controller) { createArgs.controller = controller; }
|
2013-12-04 13:23:00 -05:00
|
|
|
this.setProperties({
|
|
|
|
style: null,
|
2014-06-01 19:56:06 -04:00
|
|
|
loading: false
|
2013-12-04 13:23:00 -05:00
|
|
|
});
|
|
|
|
|
2014-06-01 19:56:06 -04:00
|
|
|
this._containedView = this.createChildView(this.get('cloaks'), createArgs);
|
2013-12-04 13:23:00 -05:00
|
|
|
this.rerender();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Removes the view from the DOM and tears down all observers.
|
|
|
|
|
|
|
|
@method cloak
|
|
|
|
*/
|
|
|
|
cloak: function() {
|
2014-06-01 19:56:06 -04:00
|
|
|
var self = this;
|
2013-12-04 13:23:00 -05:00
|
|
|
|
2014-06-01 20:55:54 -04:00
|
|
|
if (this._containedView && (this._state || this.state) === 'inDOM') {
|
2013-12-04 13:23:00 -05:00
|
|
|
var style = 'height: ' + this.$().height() + 'px;';
|
|
|
|
this.set('style', style);
|
|
|
|
this.$().prop('style', style);
|
|
|
|
|
|
|
|
// We need to remove the container after the height of the element has taken
|
|
|
|
// effect.
|
|
|
|
Ember.run.schedule('afterRender', function() {
|
2014-06-01 19:56:06 -04:00
|
|
|
if(self._containedView){
|
|
|
|
self._containedView.remove();
|
|
|
|
self._containedView = null;
|
|
|
|
}
|
2013-12-04 13:23:00 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-01 19:56:06 -04:00
|
|
|
willDestroyElement: function(){
|
|
|
|
if(this._containedView){
|
|
|
|
this._containedView.remove();
|
|
|
|
this._containedView = null;
|
|
|
|
}
|
|
|
|
this._super();
|
|
|
|
},
|
2013-12-04 13:23:00 -05:00
|
|
|
|
2014-03-17 12:00:59 -04:00
|
|
|
didInsertElement: function(){
|
2014-06-01 19:56:06 -04:00
|
|
|
if (!this._containedView) {
|
2014-03-17 12:00:59 -04:00
|
|
|
// setting default height
|
|
|
|
// but do not touch if height already defined
|
|
|
|
if(!this.$().height()){
|
|
|
|
var defaultHeight = 100;
|
|
|
|
if(this.get('defaultHeight')) {
|
|
|
|
defaultHeight = this.get('defaultHeight');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$().css('height', defaultHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-12-04 13:23:00 -05:00
|
|
|
/**
|
|
|
|
Render the cloaked view if applicable.
|
|
|
|
|
|
|
|
@method render
|
|
|
|
*/
|
|
|
|
render: function(buffer) {
|
2014-06-01 20:30:16 -04:00
|
|
|
var containedView = this._containedView, self = this;
|
2014-06-01 19:56:06 -04:00
|
|
|
|
2014-06-01 20:58:46 -04:00
|
|
|
if (containedView && (containedView._state || containedView.state) !== 'inDOM') {
|
2014-06-01 19:56:06 -04:00
|
|
|
containedView.triggerRecursively('willInsertElement');
|
2013-12-04 13:23:00 -05:00
|
|
|
containedView.renderToBuffer(buffer);
|
|
|
|
containedView.transitionTo('inDOM');
|
|
|
|
Em.run.schedule('afterRender', function() {
|
2014-06-01 20:30:16 -04:00
|
|
|
if(self._containedView) {
|
|
|
|
self._containedView.triggerRecursively('didInsertElement');
|
2014-06-01 19:56:06 -04:00
|
|
|
}
|
2013-12-04 13:23:00 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ember.Handlebars.registerHelper('cloaked-collection', function(options) {
|
|
|
|
var hash = options.hash,
|
|
|
|
types = options.hashTypes;
|
|
|
|
|
|
|
|
for (var prop in hash) {
|
|
|
|
if (types[prop] === 'ID') {
|
|
|
|
hash[prop + 'Binding'] = hash[prop];
|
|
|
|
delete hash[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ember.Handlebars.helpers.view.call(this, Ember.CloakedCollectionView, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|