function PrototypeManager(parent_container, prototype_container, remove_button, add_button) { this.items_list = []; this.current_item = null; this.add_button = add_button; this.remove_button = remove_button; this.parent_container = parent_container; // html prototype this.html_prototype = $(prototype_container).outerHTML(); } PrototypeManager.prototype.HasItems = function() { return (this.items_list.length > 0); } PrototypeManager.prototype.OnAdd = function(on_add_callback) { this.on_add_callback = on_add_callback; } PrototypeManager.prototype.OnRemove = function(on_remove_callback) { this.on_remove_callback = on_remove_callback; } PrototypeManager.prototype.OnGenerateIndexedElementID = function(event) { this.on_generate_indexed_element_id = event; } PrototypeManager.prototype.RemoveAll = function() { while (this.items_list.length > 0) this.items_list[0].Remove(); } PrototypeManager.prototype.Clear = function() { this.RemoveAll(); } PrototypeManager.prototype.IsValidIndex = function(index) { var result = (index >= 0); result = result && (index < this.items_list.length); return result; } PrototypeManager.prototype.Swap = function(index1, index2) { if ((this.IsValidIndex(index1)) && (this.IsValidIndex(index2)) && (index1 != index2)) { var temp = this.items_list[index1]; this.items_list[index1] = this.items_list[index2]; this.items_list[index2]= temp; console.log(`Swap element. From: ${index2} To ${index1}`); } } PrototypeManager.prototype.ChangeIndex = function(indexFrom, indexTo) { if ((this.IsValidIndex(indexFrom)) && (this.IsValidIndex(indexTo)) && (indexTo != indexFrom)) { var item = this.items_list.splice(indexFrom, 1); this.items_list.splice(indexTo, 0, item[0]); } } PrototypeManager.prototype.AddItem = function(index) { var object = this; this.current_item = new PrototypeItem(this); if (typeof(index) !== "undefined") this.items_list.splice(index, 0, this.current_item); else this.items_list.push(this.current_item); // 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); 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; } PrototypeManager.prototype.GetItemByIndex = function(index) { for (var i = 0; i < this.items_list.length; i++) { if (this.items_list[i]._index == index) return this.items_list[i]; } return null; }