function PrototypeMultiManager(parent_container, prototype_container, add_button, remove_button, caption_label, input_control)
{
this.items_list = [];
this.current_item = null;
this.add_button = add_button;
this.remove_button = remove_button;
this.input_control = input_control;
this.caption_label = caption_label;
this.parent_container = parent_container;
// html prototype
this.html_prototype = $(prototype_container).outerHTML();
}
PrototypeMultiManager.prototype.HasItems = function()
{
return (this.items_list.length > 0);
}
PrototypeMultiManager.prototype.UpdateInterface = function()
{
for (var i = 0; i < this.items_list.length; i++) {
this.items_list[i].UpdateInterface();
}
}
PrototypeMultiManager.prototype.HasElementWithIndex = function(index)
{
for (var i = 0; i < this.items_list.length; i++) {
if (this.items_list[i]._index == index) {
return true;
}
}
return false;
}
PrototypeMultiManager.prototype.OnAdd = function(on_add_callback)
{
this.on_add_callback = on_add_callback;
}
PrototypeMultiManager.prototype.OnRemove = function(on_remove_callback)
{
this.on_remove_callback = on_remove_callback;
}
PrototypeMultiManager.prototype.AddItem = function()
{
var object = this;
this.current_item = new PrototypeMultiItem(this);
this.items_list.push(this.current_item);
this.UpdateInterface();
// on remove event
this.current_item.OnRemove(function(item)
{
var array_idx = object.items_list.indexOf(item);
if (array_idx >= 0)
object.items_list.splice(array_idx, 1);
object.UpdateInterface();
if (object.on_remove_callback)
object.on_remove_callback(item);
});
if (this.on_add_callback) {
this.on_add_callback(this.current_item);
}
return this.current_item;
}
PrototypeMultiManager.prototype.SearchItemByValue = function(value)
{
if ((value) &&
(value != ""))
{
for (var i = 0; i < this.items_list.length; i++)
{
if (this.items_list[i].Value() == value) {
return this.items_list[i];
}
}
}
return null;
}
PrototypeMultiManager.prototype.SearchFirstFilledValue = function()
{
for (var i = 0; i < this.items_list.length; i++)
{
if (this.items_list[i].Value() != "")
return this.items_list[i].Value();
}
return "";
}
PrototypeMultiManager.prototype.SearchFirstEmptyItem = function()
{
for (var i = 0; i < this.items_list.length; i++)
{
if (this.items_list[i].Value() == "")
return this.items_list[i];
}
return null;
}
PrototypeMultiManager.prototype.SearchItemByPreprocessValue = function(value, preprocess_callback)
{
if ((value) &&
(value != ""))
{
value = preprocess_callback(value);
for (var i = 0; i < this.items_list.length; i++)
{
if (preprocess_callback(this.items_list[i].Value()) == value) {
return this.items_list[i];
}
}
}
return null;
}
PrototypeMultiManager.prototype.AddValuedItem = function(value)
{
if ((value) &&
(value != "") &&
(this.SearchItemByValue(value) == null))
{
for (var i = 0; i < this.items_list.length; i++)
{
if (this.items_list[i].Value() == "") {
this.items_list[i].SetValue(value);
return;
}
}
var item = this.AddItem();
item.SetValue(value);
}
}
PrototypeMultiManager.prototype.AddValuedItemWithPreprocess = function(value, preprocess_callback)
{
if ((value) &&
(value != "") &&
(preprocess_callback) &&
(this.SearchItemByPreprocessValue(value, preprocess_callback) == null))
{
this.AddValuedItem(value);
}
}