Permalink
Browse files

timescale helper for time_parent & max children limits (we destroy ex…

…tra children after X)
  • Loading branch information...
1 parent d5a1c5c commit 7b966f800fe4c5937ae7ec2309f85974de3e7680 @lightsofapollo lightsofapollo committed Sep 5, 2012
Showing with 95 additions and 0 deletions.
  1. +40 −0 apps/calendar/js/views/time_parent.js
  2. +55 −0 apps/calendar/test/unit/views/time_parent_test.js
@@ -49,6 +49,9 @@ Calendar.ns('Views').TimeParent = (function() {
panThreshold: 10,
+ /* should always be higher then padding */
+ maxChildren: 6,
+
/* panning variables */
_childWidth: null,
@@ -251,6 +254,7 @@ Calendar.ns('Views').TimeParent = (function() {
var len = this._activeChildren.length;
var id;
var child;
+ var curLen = len;
for (; i < len; i++) {
child = this._activeChildren.items[i];
@@ -260,10 +264,36 @@ Calendar.ns('Views').TimeParent = (function() {
if (child.element) {
child.element.style.transform = '';
}
+
child.deactivate();
}
}
+ // cleanup extra children after we go
+ // over the limit
+ if (this.children.length > this.maxChildren) {
+ var current = this.children.indexOf(
+ this.currentChild.id
+ );
+
+ // we will remove down to just the minimum so
+ // we don't need to keep calling this after for
+ // each move...
+ var keep = this.children.items.splice(
+ current, this.visibleChildren
+ );
+
+ var remove = this.children.items;
+ var len = remove.length;
+ var i = 0;
+
+ for (; i < len; i++) {
+ remove[i][1].destroy();
+ }
+
+ this.children.items = keep;
+ }
+
this._activeChildren.items = children;
},
@@ -427,6 +457,16 @@ Calendar.ns('Views').TimeParent = (function() {
offset += 1;
}
}
+ },
+
+ onactive: function() {
+ Calendar.View.prototype.onactive.apply(
+ this, arguments
+ );
+
+ if (this.app && this.scale) {
+ this.app.timeController.scale = this.scale;
+ }
}
};
@@ -12,6 +12,7 @@ suite('views/time_parent', function() {
var app;
var subject;
var id;
+ var controller;
var TimeParent;
@@ -64,6 +65,10 @@ suite('views/time_parent', function() {
this.active = false;
},
+ destroy: function() {
+ this.destroyed = true;
+ },
+
create: function() {
var el = document.createElement('div');
el.innerHTML = this.id;
@@ -80,6 +85,7 @@ suite('views/time_parent', function() {
setup(function() {
id = 0;
app = testSupport.calendar.app();
+ controller = app.timeController;
testEl = document.createElement('div');
testEl.id = 'test';
@@ -92,6 +98,10 @@ suite('views/time_parent', function() {
Subclass.prototype = {
__proto__: TimeParent.prototype,
+ // easier for testing we have
+ // a separate test which does purge...
+ maxChildren: 20,
+
get element() {
return testEl;
},
@@ -386,6 +396,25 @@ suite('views/time_parent', function() {
subject.activateChild(future);
});
+ test('when number of items is over max', function() {
+ subject.maxChildren = 1;
+ subject.deactivateChildren();
+
+ assert.length(subject.children, 1);
+ assert.length(subject._activeChildren, 1);
+
+ assert.deepEqual(
+ mapKeys(subject._activeChildren),
+ [current.id]
+ );
+
+ assert.deepEqual(
+ mapKeys(subject.children),
+ [current.id]
+ );
+
+ });
+
test('hidding', function() {
subject.deactivateChildren();
@@ -894,4 +923,30 @@ suite('views/time_parent', function() {
);
});
});
+
+ suite('#onactive', function() {
+
+ test('without scale', function() {
+ subject.scale = null;
+ assert.ok(!controller.scale);
+ subject.onactive();
+ assert.ok(!controller.scale, 'should not set scale');
+ });
+
+ test('with scale', function() {
+ subject.scale = 'random';
+
+ assert.ok(!controller.scale);
+
+ subject.onactive();
+
+ assert.equal(
+ controller.scale, 'random',
+ 'when'
+ );
+ });
+
+ });
+
+
});

0 comments on commit 7b966f8

Please sign in to comment.