1 (function(){var e
=Backbone
.Form
,t
=e
.editors
;t
.List
=t
.Base
.extend({events
:{'click [data-action="add"]':function(e
){e
.preventDefault(),this.addItem(null,!0)}},initialize:function(e
){t
.Base
.prototype.initialize
.call(this,e
);var n
=this.schema
;if(!n
)throw"Missing required option 'schema'";this.schema
=_
.extend({listTemplate
:"list",listItemTemplate
:"listItem"},n
),this.Editor=function(){var e
=n
.itemType
;return e
?t
.List
[e
]?t
.List
[e
]:t
[e
]:t
.Text
}(),this.items
=[]},render:function(){var t
=this,n
=this.value
||[],r
=$(e
.templates
[this.schema
.listTemplate
]({items
:'<b class="bbf-tmp"></b>'}));return this.$list
=r
.find(".bbf-tmp").parent().empty(),n
.length
?_
.each(n
,function(e
){t
.addItem(e
)}):this.Editor
.isAsync
||this.addItem(),this.setElement(r
),this.$el
.attr("id",this.id
),this.$el
.attr("name",this.key
),this.hasFocus
&&this.trigger("blur",this),this},addItem:function(e
,n
){var r
=this,i
=(new t
.List
.Item({list
:this,schema
:this.schema
,value
:e
,Editor
:this.Editor
,key
:this.key
})).render(),s=function(){r
.items
.push(i
),r
.$list
.append(i
.el
),i
.editor
.on("all",function(e
){if(e
==="change")return;var n
=_
.toArray(arguments
);n
[0]="item:"+e
,n
.splice(1,0,r
),t
.List
.prototype.trigger
.apply(this,n
)},r
),i
.editor
.on("change",function(){i
.addEventTriggered
||(i
.addEventTriggered
=!0,this.trigger("add",this,i
.editor
)),this.trigger("item:change",this,i
.editor
),this.trigger("change",this)},r
),i
.editor
.on("focus",function(){if(this.hasFocus
)return;this.trigger("focus",this)},r
),i
.editor
.on("blur",function(){if(!this.hasFocus
)return;var e
=this;setTimeout(function(){if(_
.find(e
.items
,function(e
){return e
.editor
.hasFocus
}))return;e
.trigger("blur",e
)},0)},r
);if(n
||e
)i
.addEventTriggered
=!0;n
&&(r
.trigger("add",r
,i
.editor
),r
.trigger("change",r
))};return this.Editor
.isAsync
?i
.editor
.on("readyToAdd",s
,this):s(),i
},removeItem:function(e
){var t
=this.schema
.confirmDelete
;if(t
&&!confirm(t
))return;var n
=_
.indexOf(this.items
,e
);this.items
[n
].remove(),this.items
.splice(n
,1),e
.addEventTriggered
&&(this.trigger("remove",this,e
.editor
),this.trigger("change",this)),!this.items
.length
&&!this.Editor
.isAsync
&&this.addItem()},getValue:function(){var e
=_
.map(this.items
,function(e
){return e
.getValue()});return _
.without(e
,undefined,"")},setValue:function(e
){this.value
=e
,this.render()},focus:function(){if(this.hasFocus
)return;this.items
[0]&&this.items
[0].editor
.focus()},blur:function(){if(!this.hasFocus
)return;var e
=_
.find(this.items
,function(e
){return e
.editor
.hasFocus
});e
&&e
.editor
.blur()},remove:function(){_
.invoke(this.items
,"remove"),t
.Base
.prototype.remove
.call(this)},validate:function(){if(!this.validators
)return null;var e
=_
.map(this.items
,function(e
){return e
.validate()}),t
=_
.compact(e
).length
?!0:!1;if(!t
)return null;var n
={type
:"list",message
:"Some of the items in the list failed validation",errors
:e
};return n
}}),t
.List
.Item
=Backbone
.View
.extend({events
:{'click [data-action="remove"]':function(e
){e
.preventDefault(),this.list
.removeItem(this)},"keydown input[type=text]":function(e
){if(e
.keyCode
!==13)return;e
.preventDefault(),this.list
.addItem(),this.list
.$list
.find("> li:last input").focus()}},initialize:function(e
){this.list
=e
.list
,this.schema
=e
.schema
||this.list
.schema
,this.value
=e
.value
,this.Editor
=e
.Editor
||t
.Text
,this.key
=e
.key
},render:function(){this.editor
=(new this.Editor({key
:this.key
,schema
:this.schema
,value
:this.value
,list
:this.list
,item
:this})).render();var t
=$(e
.templates
[this.schema
.listItemTemplate
]({editor
:'<b class="bbf-tmp"></b>'}));return t
.find(".bbf-tmp").replaceWith(this.editor
.el
),this.setElement(t
),this},getValue:function(){return this.editor
.getValue()},setValue:function(e
){this.editor
.setValue(e
)},focus:function(){this.editor
.focus()},blur:function(){this.editor
.blur()},remove:function(){this.editor
.remove(),Backbone
.View
.prototype.remove
.call(this)},validate:function(){var t
=this.getValue(),n
=this.list
.form
?this.list
.form
.getValue():{},r
=this.schema
.validators
,i
=e
.helpers
.getValidator
;if(!r
)return null;var s
=null;return _
.every(r
,function(e
){return s
=i(e
)(t
,n
),s
?!1:!0}),s
?this.setError(s
):this.clearError(),s
?s
:null},setError:function(t
){this.$el
.addClass(e
.classNames
.error
),this.$el
.attr("title",t
.message
)},clearError:function(){this.$el
.removeClass(e
.classNames
.error
),this.$el
.attr("title",null)}}),t
.List
.Modal
=t
.List
.Object
=t
.List
.NestedModel
=t
.Base
.extend({events
:{click
:"openEditor"},initialize:function(e
){t
.Base
.prototype.initialize
.call(this,e
);var n
=this.schema
;if(!t
.List
.Modal
.ModalAdapter
)throw"A ModalAdapter is required";if(n
.itemType
==="Object"){if(!n
.subSchema
)throw'Missing required option "schema.subSchema"';this.nestedSchema
=n
.subSchema
}if(n
.itemType
==="NestedModel"){if(!n
.model
)throw'Missing required option "schema.model"';this.nestedSchema
=n
.model
.prototype.schema
,_
.isFunction(this.nestedSchema
)&&(this.nestedSchema
=this.nestedSchema())}},render:function(){var e
=this;return _
.isEmpty(this.value
)?this.openEditor():(this.renderSummary(),setTimeout(function(){e
.trigger("readyToAdd")},0)),this.hasFocus
&&this.trigger("blur",this),this},renderSummary:function(){var t
=e
.templates
["list.Modal"];this.$el
.html(t({summary
:this.getStringValue()}))},itemToString:function(t
){t
=t
||{};var n
=[];return _
.each(this.nestedSchema
,function(r
,i
){var s
=r
.title
?r
.title
:e
.helpers
.keyToTitle(i
),o
=t
[i
];if(_
.isUndefined(o
)||_
.isNull(o
))o
="";n
.push(s
+": "+o
)}),n
.join("<br />")},getStringValue:function(){var e
=this.schema
,t
=this.getValue();return _
.isEmpty(t
)?"[Empty]":e
.itemToString
?e
.itemToString(t
):e
.itemType
==="NestedModel"?(new e
.model(t
)).toString():this.itemToString(t
)},openEditor:function(){var t
=this,n
=new e({schema
:this.nestedSchema
,data
:this.value
}),r
=this.modal
=(new Backbone
.BootstrapModal({content
:n
,animate
:!0})).open();this.trigger("open",this),this.trigger("focus",this),r
.on("cancel",function(){this.modal
=null,this.trigger("close",this),this.trigger("blur",this)},this),r
.on("ok",_
.bind(this.onModalSubmitted
,this,n
,r
))},onModalSubmitted:function(e
,t
){var n
=!this.value
,r
=e
.validate();if(r
)return t
.preventClose();this.modal
=null,this.value
=e
.getValue(),this.renderSummary(),n
&&this.trigger("readyToAdd"),this.trigger("change",this),this.trigger("close",this),this.trigger("blur",this)},getValue:function(){return this.value
},setValue:function(e
){this.value
=e
},focus:function(){if(this.hasFocus
)return;this.openEditor()},blur:function(){if(!this.hasFocus
)return;this.modal
&&(this.modal
.trigger("cancel"),this.modal
.close())}},{ModalAdapter
:Backbone
.BootstrapModal
,isAsync
:!0})})()