/* <copyright>
Copyright (c) 2012, Motorola Mobility, Inc
All Rights Reserved.
BSD License.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Motorola Mobility nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */
/**
@module "montage/ui/repetition.reel"
@requires montage/core/core
@requires montage/ui/component
@requires montage/ui/template
@requires montage/core/logger
@requires montage/core/gate
*/
var Montage = require("montage").Montage,
Component = require("ui/component").Component,
Template = require("ui/template").Template,
logger = require("core/logger").logger("repetition"),
Gate = require("core/gate").Gate,
ChangeNotification = require("core/change-notification").ChangeNotification,
PropertyChangeNotification = require("core/change-notification").PropertyChangeNotification;
var FakeObjects = Montage.create(Object.prototype, {
_repetition: {value: null},
_fakeIndex: {value: null},
_unusedIndexes: {value: null},
initWithRepetition: {
value: function(repetition) {
this._repetition = repetition;
this._fakeIndex = [];
this._unusedIndexes = [];
return this;
}
},
automaticallyDispatchPropertyChangeListener: {
value: function() {
return false;
}
},
undefinedGet: {
value: function(propertyName) {
if (this._repetition.objects) {
return this._repetition.objects[this._fakeIndex.indexOf(propertyName)];
}
}
},
// This is to catch a two-way binding on
"0": {
// This is to catch two way bindings
set: function() {
throw("You cannot use a two-way binding on the \"objectAtCurrentIteration\" or \"current\" property.");
},
get: function() {
if (this._repetition.objects) {
return this._repetition.objects[this._fakeIndex.indexOf("0")];
}
}
},
addFakeObjectAtPosition: {
value: function(position) {
var index;
if (this._unusedIndexes.length > 0) {
index = this._unusedIndexes.pop();
} else {
index = String(this._fakeIndex.length);
}
this._fakeIndex.splice(position, 0, index);
return index;
}
},
resetFakeObjects: {
value: function() {
var objects = this._repetition.objects;
this._fakeIndex.length = 0;
if (objects) {
for (var i = 0, l = objects.length; i < l; i++) {
this._fakeIndex[i] = String(i);
}
}
}
},
removeFakeObjectAtPosition: {
value: function(position) {
var index;
this._unusedIndexes.unshift(this._fakeIndex.splice(position, 1)[0]);
return this._unusedIndexes[0];
}
},
_dispatchFakePropertyChange: {
value: function(propertyName, minus) {
var descriptor,
notification;
descriptor = ChangeNotification.getPropertyChangeDescriptor(this, propertyName);
if (descriptor) {
notification = Object.create(PropertyChangeNotification);
notification.target = this;
notification.propertyPath = propertyName;
notification.minus = minus;
notification.plus = this.undefinedGet(propertyName);
if (minus !== notification.plus) {
descriptor.handleChange(notification);
}
}
}
}
});
/**
@class module:"montage/ui/repetition.reel".Repetition
@extends module:montage/ui/component.Component
*/
var Repetition = exports.Repetition = Montage.create(Component, /** @lends module:"montage/ui/repetition.reel".Repetition# */{
/**
Description TODO
*/
hasTemplate: {value: false},
didCreate: {
value: function() {
this.addPropertyChangeListener("objects", this);
this._fakeObjects = Object.create(FakeObjects).initWithRepetition(this);
}
},
_emptyFunction: {value: function(){}},
_updateItems: {
value: function(minus, plus, index) {
var fakeObjects = this._fakeObjects,
fakeIndex,
minusCount = minus ? minus.length : 0,
plusCount = plus ? plus.length : 0,
max, min, delta;
max = Math.max(minusCount, plusCount);
min = Math.min(minusCount, plusCount);
delta = plusCount - minusCount;
//console.log("Going to change " + min + " iterations", fakeObjects._fakeIndex);
// send updates for the elements that were just replaced by new ones
for (var i = 0; i < min; i++) {
//console.log("Going to change " + (index+i), minus[index+i]);
fakeObjects._dispatchFakePropertyChange(fakeObjects._fakeIndex[index+i], minus[index+i]);
}
// add new objects, no need to send updates on this one, they're new!
if (delta > 0) {
//console.log("Going to add " + (max-i) + " iterations");
this._expectedChildComponentsCount += (this._iterationChildComponentsCount||1) * delta;
this.canDrawGate.setField("iterationLoaded", false);
for (; i < max; i++) {
//console.log("New item " + (index+i) + " " + plus[index+i].uuid);
fakeObjects.addFakeObjectAtPosition(index + i);
this._addItem({index: index + i, insertionIndex: index + i});
}
} else i
|