diff --git a/distribution.amd/backbone-forms.js b/distribution.amd/backbone-forms.js index e9917af2..d8af52b1 100644 --- a/distribution.amd/backbone-forms.js +++ b/distribution.amd/backbone-forms.js @@ -588,7 +588,6 @@ Form.validators = (function() { })(); - //================================================================================================== //FIELDSET //================================================================================================== @@ -676,11 +675,24 @@ Form.Fieldset = Backbone.View.extend({ var $container = $(el), selection = $container.attr('data-fields'); - if (_.isUndefined(selection)) return; + if (_.isUndefined(selection)) { + return; + } + + //Work out which fields to include (_compact used to trim empty string selection) + var keys = _.compact((selection == '*') ? self.selectedFields || _.keys(fields) : selection.split(',')); + + //include all keys if not specified + if (!keys.length){ + keys = _.keys(fields); + } + - _.each(fields, function(field) { - $container.append(field.render().el); + //Add them + _.each(keys, function(key) { + $container.append(fields[key].render().el); }); + }); this.setElement($fieldset); diff --git a/distribution.amd/backbone-forms.min.js b/distribution.amd/backbone-forms.min.js index 8440243c..17efdbf6 100644 --- a/distribution.amd/backbone-forms.min.js +++ b/distribution.amd/backbone-forms.min.js @@ -1 +1 @@ -define(["jquery","underscore","backbone"],function(e,t,n){var r=n.View.extend({events:{submit:function(e){this.trigger("submit",e)}},initialize:function(e){var n=this;e=e||{};var r=this.schema=function(){if(e.schema)return t.result(e,"schema");var r=e.model;return r&&r.schema?t.result(r,"schema"):n.schema?t.result(n,"schema"):{}}();t.extend(this,t.pick(e,"model","data","idPrefix","templateData"));var i=this.constructor;this.template=e.template||this.template||i.template,this.Fieldset=e.Fieldset||this.Fieldset||i.Fieldset,this.Field=e.Field||this.Field||i.Field,this.NestedField=e.NestedField||this.NestedField||i.NestedField;var s=this.selectedFields=e.fields||t.keys(r),o=this.fields={};t.each(s,function(e){var t=r[e];o[e]=this.createField(e,t)},this);var u=e.fieldsets||t.result(this,"fieldsets")||[s],a=this.fieldsets=[];t.each(u,function(e){this.fieldsets.push(this.createFieldset(e))},this)},createFieldset:function(e){var t={schema:e,fields:this.fields};return new this.Fieldset(t)},createField:function(e,t){var n={form:this,key:e,schema:t,idPrefix:this.idPrefix};this.model?n.model=this.model:this.data?n.value=this.data[e]:n.value=null;var r=new this.Field(n);return this.listenTo(r.editor,"all",this.handleEditorEvent),r},handleEditorEvent:function(e,n){var r=n.key+":"+e;this.trigger.call(this,r,this,n,Array.prototype.slice.call(arguments,2));switch(e){case"change":this.trigger("change",this);break;case"focus":this.hasFocus||this.trigger("focus",this);break;case"blur":if(this.hasFocus){var i=this;setTimeout(function(){var e=t.find(i.fields,function(e){return e.editor.hasFocus});e||i.trigger("blur",i)},0)}}},render:function(){var n=this,r=this.fields,i=e(e.trim(this.template(t.result(this,"templateData"))));return i.find("[data-editors]").add(i).each(function(i,s){var o=e(s),u=o.attr("data-editors");if(t.isUndefined(u))return;var a=u=="*"?n.selectedFields||t.keys(r):u.split(",");t.each(a,function(e){var t=r[e];o.append(t.editor.render().el)})}),i.find("[data-fields]").add(i).each(function(i,s){var o=e(s),u=o.attr("data-fields");if(t.isUndefined(u))return;var a=u=="*"?n.selectedFields||t.keys(r):u.split(",");t.each(a,function(e){var t=r[e];o.append(t.render().el)})}),i.find("[data-fieldsets]").add(i).each(function(r,i){var s=e(i),o=s.attr("data-fieldsets");if(t.isUndefined(o))return;t.each(n.fieldsets,function(e){s.append(e.render().el)})}),this.setElement(i),i.addClass(this.className),this},validate:function(e){var n=this,r=this.fields,i=this.model,s={};e=e||{},t.each(r,function(e){var t=e.validate();t&&(s[e.key]=t)});if(!e.skipModelValidate&&i&&i.validate){var o=i.validate(this.getValue());if(o){var u=t.isObject(o)&&!t.isArray(o);u||(s._others=s._others||[],s._others.push(o)),u&&t.each(o,function(e,t){if(r[t]&&!s[t])r[t].setError(e),s[t]=e;else{s._others=s._others||[];var n={};n[t]=e,s._others.push(n)}})}}return t.isEmpty(s)?null:s},commit:function(e){e=e||{};var n={skipModelValidate:!e.validate},r=this.validate(n);if(r)return r;var i,s=t.extend({error:function(e,t){i=t}},e);this.model.set(this.getValue(),s);if(i)return i},getValue:function(e){if(e)return this.fields[e].getValue();var n={};return t.each(this.fields,function(e){n[e.key]=e.getValue()}),n},setValue:function(e,t){var n={};typeof e=="string"?n[e]=t:n=e;var r;for(r in this.schema)n[r]!==undefined&&this.fields[r].setValue(n[r])},getEditor:function(e){var t=this.fields[e];if(!t)throw new Error("Field not found: "+e);return t.editor},focus:function(){if(this.hasFocus)return;var e=this.fieldsets[0],t=e.getFieldAt(0);if(!t)return;t.editor.focus()},blur:function(){if(!this.hasFocus)return;var e=t.find(this.fields,function(e){return e.editor.hasFocus});e&&e.editor.blur()},trigger:function(e){return e==="focus"?this.hasFocus=!0:e==="blur"&&(this.hasFocus=!1),n.View.prototype.trigger.apply(this,arguments)},remove:function(){return t.each(this.fieldsets,function(e){e.remove()}),t.each(this.fields,function(e){e.remove()}),n.View.prototype.remove.apply(this,arguments)}},{template:t.template("
",null,this.templateSettings),templateSettings:{evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g},editors:{}});return r.validators=function(){var e={};return e.errMessages={required:"Required",regexp:"Invalid",number:"Must be a number",email:"Invalid email address",url:"Invalid URL",match:t.template('Must match field "<%= field %>"',null,r.templateSettings)},e.required=function(e){return e=t.extend({type:"required",message:this.errMessages.required},e),function(r){e.value=r;var i={type:e.type,message:t.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r===!1||r==="")return i}},e.regexp=function(e){if(!e.regexp)throw new Error('Missing required "regexp" option for "regexp" validator');return e=t.extend({type:"regexp",match:!0,message:this.errMessages.regexp},e),function(r){e.value=r;var i={type:e.type,message:t.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r==="")return;"string"==typeof e.regexp&&(e.regexp=new RegExp(e.regexp,e.flags));if(e.match?!e.regexp.test(r):e.regexp.test(r))return i}},e.number=function(n){return n=t.extend({type:"number",message:this.errMessages.number,regexp:/^[0-9]*\.?[0-9]*?$/},n),e.regexp(n)},e.email=function(n){return n=t.extend({type:"email",message:this.errMessages.email,regexp:/^[\w\-]{1,}([\w\-\+.]{1,1}[\w\-]{1,}){0,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/},n),e.regexp(n)},e.url=function(n){return n=t.extend({type:"url",message:this.errMessages.url,regexp:/^(http|https):\/\/(([A-Z0-9][A-Z0-9_\-]*)(\.[A-Z0-9][A-Z0-9_\-]*)+)(:(\d+))?\/?/i},n),e.regexp(n)},e.match=function(e){if(!e.field)throw new Error('Missing required "field" options for "match" validator');return e=t.extend({type:"match",message:this.errMessages.match},e),function(r,i){e.value=r;var s={type:e.type,message:t.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r==="")return;if(r!==i[e.field])return s}},e}(),r.Fieldset=n.View.extend({initialize:function(e){e=e||{};var n=this.schema=this.createSchema(e.schema);this.fields=t.pick(e.fields,n.fields),this.template=e.template||n.template||this.template||this.constructor.template},createSchema:function(e){return t.isArray(e)&&(e={fields:e}),e.legend=e.legend||null,e},getFieldAt:function(e){var t=this.schema.fields[e];return this.fields[t]},templateData:function(){return this.schema},render:function(){var n=this.schema,r=this.fields,i=e(e.trim(this.template(t.result(this,"templateData"))));return i.find("[data-fields]").add(i).each(function(n,i){var s=e(i),o=s.attr("data-fields");if(t.isUndefined(o))return;t.each(r,function(e){s.append(e.render().el)})}),this.setElement(i),this},remove:function(){t.each(this.fields,function(e){e.remove()}),n.View.prototype.remove.call(this)}},{template:t.template("
<% if (legend) { %> <%= legend %> <% } %>
",null,r.templateSettings)}),r.Field=n.View.extend({initialize:function(e){e=e||{},t.extend(this,t.pick(e,"form","key","model","value","idPrefix"));var n=this.schema=this.createSchema(e.schema);this.template=e.template||n.template||this.template||this.constructor.template,this.errorClassName=e.errorClassName||this.errorClassName||this.constructor.errorClassName,this.editor=this.createEditor()},createSchema:function(e){return t.isString(e)&&(e={type:e}),e=t.extend({type:"Text",title:this.createTitle()},e),e.type=t.isString(e.type)?r.editors[e.type]:e.type,e},createEditor:function(){var e=t.extend(t.pick(this,"schema","form","key","model","value"),{id:this.createEditorId()}),n=this.schema.type;return new n(e)},createEditorId:function(){var e=this.idPrefix,n=this.key;return n=n.replace(/\./g,"_"),t.isString(e)||t.isNumber(e)?e+n:t.isNull(e)?n:this.model?this.model.cid+"_"+n:n},createTitle:function(){var e=this.key;return e=e.replace(/([A-Z])/g," $1"),e=e.replace(/^./,function(e){return e.toUpperCase()}),e},templateData:function(){var e=this.schema;return{help:e.help||"",title:e.title,fieldAttrs:e.fieldAttrs,editorAttrs:e.editorAttrs,key:this.key,editorId:this.editor.id}},render:function(){var n=this.schema,i=this.editor;if(n.type==r.editors.Hidden)return this.setElement(i.render().el);var s=e(e.trim(this.template(t.result(this,"templateData"))));return n.fieldClass&&s.addClass(n.fieldClass),n.fieldAttrs&&s.attr(n.fieldAttrs),s.find("[data-editor]").add(s).each(function(n,r){var s=e(r),o=s.attr("data-editor");if(t.isUndefined(o))return;s.append(i.render().el)}),this.setElement(s),this},validate:function(){var e=this.editor.validate();return e?this.setError(e.message):this.clearError(),e},setError:function(e){if(this.editor.hasNestedForm)return;this.$el.addClass(this.errorClassName),this.$("[data-error]").html(e)},clearError:function(){this.$el.removeClass(this.errorClassName),this.$("[data-error]").empty()},commit:function(){return this.editor.commit()},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(),n.View.prototype.remove.call(this)}},{template:t.template('
<%= help %>
',null,r.templateSettings),errorClassName:"error"}),r.NestedField=r.Field.extend({template:t.template(e.trim("
<% if (help) { %>
<%= help %>
<% } %>
"),null,r.templateSettings)}),r.Editor=r.editors.Base=n.View.extend({defaultValue:null,hasFocus:!1,initialize:function(e){var e=e||{};if(e.model){if(!e.key)throw new Error("Missing option: 'key'");this.model=e.model,this.value=this.model.get(e.key)}else e.value!==undefined&&(this.value=e.value);this.value===undefined&&(this.value=this.defaultValue),t.extend(this,t.pick(e,"key","form"));var n=this.schema=e.schema||{};this.validators=e.validators||n.validators,this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),n.editorClass&&this.$el.addClass(n.editorClass),n.editorAttrs&&this.$el.attr(n.editorAttrs)},getName:function(){var e=this.key||"";return e.replace(/\./g,"_")},getValue:function(){return this.value},setValue:function(e){this.value=e},focus:function(){throw new Error("Not implemented")},blur:function(){throw new Error("Not implemented")},commit:function(e){var t=this.validate();if(t)return t;this.listenTo(this.model,"invalid",function(e,n){t=n}),this.model.set(this.key,this.getValue(),e);if(t)return t},validate:function(){var e=this.$el,n=null,r=this.getValue(),i=this.form?this.form.getValue():{},s=this.validators,o=this.getValidator;return s&&t.every(s,function(e){return n=o(e)(r,i),n?!1:!0}),n},trigger:function(e){return e==="focus"?this.hasFocus=!0:e==="blur"&&(this.hasFocus=!1),n.View.prototype.trigger.apply(this,arguments)},getValidator:function(e){var n=r.validators;if(t.isRegExp(e))return n.regexp({regexp:e});if(t.isString(e)){if(!n[e])throw new Error('Validator "'+e+'" not found');return n[e]()}if(t.isFunction(e))return e;if(t.isObject(e)&&e.type){var i=e;return n[i.type](i)}throw new Error("Invalid validator: "+e)}}),r.editors.Text=r.Editor.extend({tagName:"input",defaultValue:"",previousValue:"",events:{keyup:"determineChange",keypress:function(e){var t=this;setTimeout(function(){t.determineChange()},0)},select:function(e){this.trigger("select",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){r.editors.Base.prototype.initialize.call(this,e);var t=this.schema,n="text";t&&t.editorAttrs&&t.editorAttrs.type&&(n=t.editorAttrs.type),t&&t.dataType&&(n=t.dataType),this.$el.attr("type",n)},render:function(){return this.setValue(this.value),this},determineChange:function(e){var t=this.$el.val(),n=t!==this.previousValue;n&&(this.previousValue=t,this.trigger("change",this))},getValue:function(){return this.$el.val()},setValue:function(e){this.$el.val(e)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()},select:function(){this.$el.select()}}),r.editors.TextArea=r.editors.Text.extend({tagName:"textarea",initialize:function(e){r.editors.Base.prototype.initialize.call(this,e)}}),r.editors.Password=r.editors.Text.extend({initialize:function(e){r.editors.Text.prototype.initialize.call(this,e),this.$el.attr("type","password")}}),r.editors.Number=r.editors.Text.extend({defaultValue:0,events:t.extend({},r.editors.Text.prototype.events,{keypress:"onKeyPress",change:"onKeyPress"}),initialize:function(e){r.editors.Text.prototype.initialize.call(this,e);var t=this.schema;this.$el.attr("type","number"),(!t||!t.editorAttrs||!t.editorAttrs.step)&&this.$el.attr("step","any")},onKeyPress:function(e){var t=this,n=function(){setTimeout(function(){t.determineChange()},0)};if(e.charCode===0){n();return}var r=this.$el.val();e.charCode!=undefined&&(r+=String.fromCharCode(e.charCode));var i=/^[0-9]*\.?[0-9]*?$/.test(r);i?n():e.preventDefault()},getValue:function(){var e=this.$el.val();return e===""?null:parseFloat(e,10)},setValue:function(e){e=function(){return t.isNumber(e)?e:t.isString(e)&&e!==""?parseFloat(e,10):null}(),t.isNaN(e)&&(e=null),r.editors.Text.prototype.setValue.call(this,e)}}),r.editors.Hidden=r.editors.Text.extend({defaultValue:"",initialize:function(e){r.editors.Text.prototype.initialize.call(this,e),this.$el.attr("type","hidden")},focus:function(){},blur:function(){}}),r.editors.Checkbox=r.editors.Base.extend({defaultValue:!1,tagName:"input",events:{click:function(e){this.trigger("change",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){r.editors.Base.prototype.initialize.call(this,e),this.$el.attr("type","checkbox")},render:function(){return this.setValue(this.value),this},getValue:function(){return this.$el.prop("checked")},setValue:function(e){e?this.$el.prop("checked",!0):this.$el.prop("checked",!1)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()}}),r.editors.Select=r.editors.Base.extend({tagName:"select",events:{change:function(e){this.trigger("change",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){r.editors.Base.prototype.initialize.call(this,e);if(!this.schema||!this.schema.options)throw new Error("Missing required 'schema.options'")},render:function(){return this.setOptions(this.schema.options),this},setOptions:function(e){var r=this;if(e instanceof n.Collection){var i=e;i.length>0?this.renderOptions(e):i.fetch({success:function(t){r.renderOptions(e)}})}else t.isFunction(e)?e(function(e){r.renderOptions(e)},r):this.renderOptions(e)},renderOptions:function(e){var t=this.$el,n;n=this._getOptionsHtml(e),t.html(n),this.setValue(this.value)},_getOptionsHtml:function(e){var r;if(t.isString(e))r=e;else if(t.isArray(e))r=this._arrayToHtml(e);else if(e instanceof n.Collection)r=this._collectionToHtml(e);else if(t.isFunction(e)){var i;e(function(e){i=e},this),r=this._getOptionsHtml(i)}else r=this._objectToHtml(e);return r},getValue:function(){return this.$el.val()},setValue:function(e){this.$el.val(e)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()},_collectionToHtml:function(e){var t=[];e.each(function(e){t.push({val:e.id,label:e.toString()})});var n=this._arrayToHtml(t);return n},_objectToHtml:function(e){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push({val:n,label:e[n]});var r=this._arrayToHtml(t);return r},_arrayToHtml:function(e){var n=[];return t.each(e,function(e){if(t.isObject(e))if(e.group)n.push(''),n.push(this._getOptionsHtml(e.options)),n.push("");else{var r=e.val||e.val===0?e.val:"";n.push('")}else n.push("")},this),n.join("")}}),r.editors.Radio=r.editors.Select.extend({tagName:"ul",events:{"change input[type=radio]":function(){this.trigger("change",this)},"focus input[type=radio]":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur input[type=radio]":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("input[type=radio]:focus")[0])return;e.trigger("blur",e)},0)}},getTemplate:function(){return this.schema.template||this.constructor.template},getValue:function(){return this.$("input[type=radio]:checked").val()},setValue:function(e){this.$("input[type=radio]").val([e])},focus:function(){if(this.hasFocus)return;var e=this.$("input[type=radio]:checked");if(e[0]){e.focus();return}this.$("input[type=radio]").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("input[type=radio]:focus").blur()},_arrayToHtml:function(e){var n=this,r=this.getTemplate(),i=n.getName(),s=n.id,o=t.map(e,function(e,n){var r={name:i,id:s+"-"+n};return t.isObject(e)?(r.value=e.val||e.val===0?e.val:"",r.label=e.label):(r.value=e,r.label=e),r});return r({items:o})}},{template:t.template(' ',null,r.templateSettings)}),r.editors.Checkboxes=r.editors.Select.extend({tagName:"ul",groupNumber:0,events:{"click input[type=checkbox]":function(){this.trigger("change",this)},"focus input[type=checkbox]":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur input[type=checkbox]":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("input[type=checkbox]:focus")[0])return;e.trigger("blur",e)},0)}},getValue:function(){var t=[];return this.$("input[type=checkbox]:checked").each(function(){t.push(e(this).val())}),t},setValue:function(e){t.isArray(e)||(e=[e]),this.$("input[type=checkbox]").val(e)},focus:function(){if(this.hasFocus)return;this.$("input[type=checkbox]").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("input[type=checkbox]:focus").blur()},_arrayToHtml:function(e){var n=[],r=this;return t.each(e,function(e,i){var s="
  • ",o=!0;if(t.isObject(e))if(e.group){var u=r.id;r.id+="-"+r.groupNumber++,s='
    '+e.group+"",s+=r._arrayToHtml(e.options),s+="
    ",r.id=u,o=!1}else{var a=e.val||e.val===0?e.val:"";s+='',s+='"}else s+='',s+='";o&&(s+="
  • "),n.push(s)}),n.join("")}}),r.editors.Object=r.editors.Base.extend({hasNestedForm:!0,initialize:function(e){this.value={},r.editors.Base.prototype.initialize.call(this,e);if(!this.form)throw new Error('Missing required option "form"');if(!this.schema.subSchema)throw new Error("Missing required 'schema.subSchema' option for Object editor")},render:function(){var e=this.form.constructor;return this.nestedForm=new e({schema:this.schema.subSchema,data:this.value,idPrefix:this.id+"_",Field:e.NestedField}),this._observeFormEvents(),this.$el.html(this.nestedForm.render().el),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){return this.nestedForm?this.nestedForm.getValue():this.value},setValue:function(e){this.value=e,this.render()},focus:function(){if(this.hasFocus)return;this.nestedForm.focus()},blur:function(){if(!this.hasFocus)return;this.nestedForm.blur()},remove:function(){this.nestedForm.remove(),n.View.prototype.remove.call(this)},validate:function(){return this.nestedForm.validate()},_observeFormEvents:function(){if(!this.nestedForm)return;this.nestedForm.on("all",function(){var e=t.toArray(arguments);e[1]=this,this.trigger.apply(this,e)},this)}}),r.editors.NestedModel=r.editors.Object.extend({initialize:function(e){r.editors.Base.prototype.initialize.call(this,e);if(!this.form)throw new Error('Missing required option "form"');if(!e.schema.model)throw new Error('Missing required "schema.model" option for NestedModel editor')},render:function(){var e=this.form.constructor,t=this.value||{},n=this.key,r=this.schema.model,i=t.constructor===r?t:new r(t);return this.nestedForm=new e({model:i,idPrefix:this.id+"_",fieldTemplate:"nestedField"}),this._observeFormEvents(),this.$el.html(this.nestedForm.render().el),this.hasFocus&&this.trigger("blur",this),this},commit:function(){var e=this.nestedForm.commit();return e?(this.$el.addClass("error"),e):r.editors.Object.prototype.commit.call(this)}}),r.editors.Date=r.editors.Base.extend({events:{"change select":function(){this.updateHidden(),this.trigger("change",this)},"focus select":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur select":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("select:focus")[0])return;e.trigger("blur",e)},0)}},initialize:function(e){e=e||{},r.editors.Base.prototype.initialize.call(this,e);var n=r.editors.Date,i=new Date;this.options=t.extend({monthNames:n.monthNames,showMonthNames:n.showMonthNames},e),this.schema=t.extend({yearStart:i.getFullYear()-100,yearEnd:i.getFullYear()},e.schema||{}),this.value&&!t.isDate(this.value)&&(this.value=new Date(this.value));if(!this.value){var s=new Date;s.setSeconds(0),s.setMilliseconds(0),this.value=s}this.template=e.template||this.constructor.template},render:function(){var n=this.options,r=this.schema,i=t.map(t.range(1,32),function(e){return'"}),s=t.map(t.range(0,12),function(e){var t=n.showMonthNames?n.monthNames[e]:e+1;return'"}),o=r.yearStart'+e+""}),a=e(e.trim(this.template({dates:i.join(""),months:s.join(""),years:u.join("")})));return this.$date=a.find('[data-type="date"]'),this.$month=a.find('[data-type="month"]'),this.$year=a.find('[data-type="year"]'),this.$hidden=e(''),a.append(this.$hidden),this.setValue(this.value),this.setElement(a),this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){var e=this.$year.val(),t=this.$month.val(),n=this.$date.val();return!e||!t||!n?null:new Date(e,t,n)},setValue:function(e){this.$date.val(e.getDate()),this.$month.val(e.getMonth()),this.$year.val(e.getFullYear()),this.updateHidden()},focus:function(){if(this.hasFocus)return;this.$("select").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("select:focus").blur()},updateHidden:function(){var e=this.getValue();t.isDate(e)&&(e=e.toISOString()),this.$hidden.val(e)}},{template:t.template('
    ',null,r.templateSettings),showMonthNames:!0,monthNames:["January","February","March","April","May","June","July","August","September","October","November","December"]}),r.editors.DateTime=r.editors.Base.extend({events:{"change select":function(){this.updateHidden(),this.trigger("change",this)},"focus select":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur select":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("select:focus")[0])return;e.trigger("blur",e)},0)}},initialize:function(e){e=e||{},r.editors.Base.prototype.initialize.call(this,e),this.options=t.extend({DateEditor:r.editors.DateTime.DateEditor},e),this.schema=t.extend({minsInterval:15},e.schema||{}),this.dateEditor=new this.options.DateEditor(e),this.value=this.dateEditor.value,this.template=e.template||this.constructor.template},render:function(){function n(e){return e<10?"0"+e:e}var r=this.schema,i=t.map(t.range(0,24),function(e){return'"}),s=t.map(t.range(0,60,r.minsInterval),function(e){return'"}),o=e(e.trim(this.template({hours:i.join(),mins:s.join()})));return o.find("[data-date]").append(this.dateEditor.render().el),this.$hour=o.find('select[data-type="hour"]'),this.$min=o.find('select[data-type="min"]'),this.$hidden=o.find('input[type="hidden"]'),this.setValue(this.value),this.setElement(o),this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){var e=this.dateEditor.getValue(),t=this.$hour.val(),n=this.$min.val();return!e||!t||!n?null:(e.setHours(t),e.setMinutes(n),e)},setValue:function(e){t.isDate(e)||(e=new Date(e)),this.dateEditor.setValue(e),this.$hour.val(e.getHours()),this.$min.val(e.getMinutes()),this.updateHidden()},focus:function(){if(this.hasFocus)return;this.$("select").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("select:focus").blur()},updateHidden:function(){var e=this.getValue();t.isDate(e)&&(e=e.toISOString()),this.$hidden.val(e)},remove:function(){this.dateEditor.remove(),r.editors.Base.prototype.remove.call(this)}},{template:t.template('
    :
    ',null,r.templateSettings),DateEditor:r.editors.Date}),r.VERSION="0.14.0",n.Form=r,r}) \ No newline at end of file +define(["jquery","underscore","backbone"],function(e,t,n){var r=n.View.extend({events:{submit:function(e){this.trigger("submit",e)}},initialize:function(e){var n=this;e=e||{};var r=this.schema=function(){if(e.schema)return t.result(e,"schema");var r=e.model;return r&&r.schema?t.result(r,"schema"):n.schema?t.result(n,"schema"):{}}();t.extend(this,t.pick(e,"model","data","idPrefix","templateData"));var i=this.constructor;this.template=e.template||this.template||i.template,this.Fieldset=e.Fieldset||this.Fieldset||i.Fieldset,this.Field=e.Field||this.Field||i.Field,this.NestedField=e.NestedField||this.NestedField||i.NestedField;var s=this.selectedFields=e.fields||t.keys(r),o=this.fields={};t.each(s,function(e){var t=r[e];o[e]=this.createField(e,t)},this);var u=e.fieldsets||t.result(this,"fieldsets")||[s],a=this.fieldsets=[];t.each(u,function(e){this.fieldsets.push(this.createFieldset(e))},this)},createFieldset:function(e){var t={schema:e,fields:this.fields};return new this.Fieldset(t)},createField:function(e,t){var n={form:this,key:e,schema:t,idPrefix:this.idPrefix};this.model?n.model=this.model:this.data?n.value=this.data[e]:n.value=null;var r=new this.Field(n);return this.listenTo(r.editor,"all",this.handleEditorEvent),r},handleEditorEvent:function(e,n){var r=n.key+":"+e;this.trigger.call(this,r,this,n,Array.prototype.slice.call(arguments,2));switch(e){case"change":this.trigger("change",this);break;case"focus":this.hasFocus||this.trigger("focus",this);break;case"blur":if(this.hasFocus){var i=this;setTimeout(function(){var e=t.find(i.fields,function(e){return e.editor.hasFocus});e||i.trigger("blur",i)},0)}}},render:function(){var n=this,r=this.fields,i=e(e.trim(this.template(t.result(this,"templateData"))));return i.find("[data-editors]").add(i).each(function(i,s){var o=e(s),u=o.attr("data-editors");if(t.isUndefined(u))return;var a=u=="*"?n.selectedFields||t.keys(r):u.split(",");t.each(a,function(e){var t=r[e];o.append(t.editor.render().el)})}),i.find("[data-fields]").add(i).each(function(i,s){var o=e(s),u=o.attr("data-fields");if(t.isUndefined(u))return;var a=u=="*"?n.selectedFields||t.keys(r):u.split(",");t.each(a,function(e){var t=r[e];o.append(t.render().el)})}),i.find("[data-fieldsets]").add(i).each(function(r,i){var s=e(i),o=s.attr("data-fieldsets");if(t.isUndefined(o))return;t.each(n.fieldsets,function(e){s.append(e.render().el)})}),this.setElement(i),i.addClass(this.className),this},validate:function(e){var n=this,r=this.fields,i=this.model,s={};e=e||{},t.each(r,function(e){var t=e.validate();t&&(s[e.key]=t)});if(!e.skipModelValidate&&i&&i.validate){var o=i.validate(this.getValue());if(o){var u=t.isObject(o)&&!t.isArray(o);u||(s._others=s._others||[],s._others.push(o)),u&&t.each(o,function(e,t){if(r[t]&&!s[t])r[t].setError(e),s[t]=e;else{s._others=s._others||[];var n={};n[t]=e,s._others.push(n)}})}}return t.isEmpty(s)?null:s},commit:function(e){e=e||{};var n={skipModelValidate:!e.validate},r=this.validate(n);if(r)return r;var i,s=t.extend({error:function(e,t){i=t}},e);this.model.set(this.getValue(),s);if(i)return i},getValue:function(e){if(e)return this.fields[e].getValue();var n={};return t.each(this.fields,function(e){n[e.key]=e.getValue()}),n},setValue:function(e,t){var n={};typeof e=="string"?n[e]=t:n=e;var r;for(r in this.schema)n[r]!==undefined&&this.fields[r].setValue(n[r])},getEditor:function(e){var t=this.fields[e];if(!t)throw new Error("Field not found: "+e);return t.editor},focus:function(){if(this.hasFocus)return;var e=this.fieldsets[0],t=e.getFieldAt(0);if(!t)return;t.editor.focus()},blur:function(){if(!this.hasFocus)return;var e=t.find(this.fields,function(e){return e.editor.hasFocus});e&&e.editor.blur()},trigger:function(e){return e==="focus"?this.hasFocus=!0:e==="blur"&&(this.hasFocus=!1),n.View.prototype.trigger.apply(this,arguments)},remove:function(){return t.each(this.fieldsets,function(e){e.remove()}),t.each(this.fields,function(e){e.remove()}),n.View.prototype.remove.apply(this,arguments)}},{template:t.template("
    ",null,this.templateSettings),templateSettings:{evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g},editors:{}});return r.validators=function(){var e={};return e.errMessages={required:"Required",regexp:"Invalid",number:"Must be a number",email:"Invalid email address",url:"Invalid URL",match:t.template('Must match field "<%= field %>"',null,r.templateSettings)},e.required=function(e){return e=t.extend({type:"required",message:this.errMessages.required},e),function(r){e.value=r;var i={type:e.type,message:t.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r===!1||r==="")return i}},e.regexp=function(e){if(!e.regexp)throw new Error('Missing required "regexp" option for "regexp" validator');return e=t.extend({type:"regexp",match:!0,message:this.errMessages.regexp},e),function(r){e.value=r;var i={type:e.type,message:t.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r==="")return;"string"==typeof e.regexp&&(e.regexp=new RegExp(e.regexp,e.flags));if(e.match?!e.regexp.test(r):e.regexp.test(r))return i}},e.number=function(n){return n=t.extend({type:"number",message:this.errMessages.number,regexp:/^[0-9]*\.?[0-9]*?$/},n),e.regexp(n)},e.email=function(n){return n=t.extend({type:"email",message:this.errMessages.email,regexp:/^[\w\-]{1,}([\w\-\+.]{1,1}[\w\-]{1,}){0,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/},n),e.regexp(n)},e.url=function(n){return n=t.extend({type:"url",message:this.errMessages.url,regexp:/^(http|https):\/\/(([A-Z0-9][A-Z0-9_\-]*)(\.[A-Z0-9][A-Z0-9_\-]*)+)(:(\d+))?\/?/i},n),e.regexp(n)},e.match=function(e){if(!e.field)throw new Error('Missing required "field" options for "match" validator');return e=t.extend({type:"match",message:this.errMessages.match},e),function(r,i){e.value=r;var s={type:e.type,message:t.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r==="")return;if(r!==i[e.field])return s}},e}(),r.Fieldset=n.View.extend({initialize:function(e){e=e||{};var n=this.schema=this.createSchema(e.schema);this.fields=t.pick(e.fields,n.fields),this.template=e.template||n.template||this.template||this.constructor.template},createSchema:function(e){return t.isArray(e)&&(e={fields:e}),e.legend=e.legend||null,e},getFieldAt:function(e){var t=this.schema.fields[e];return this.fields[t]},templateData:function(){return this.schema},render:function(){var n=this.schema,r=this.fields,i=e(e.trim(this.template(t.result(this,"templateData"))));return i.find("[data-fields]").add(i).each(function(n,i){var s=e(i),o=s.attr("data-fields");if(t.isUndefined(o))return;var u=t.compact(o=="*"?self.selectedFields||t.keys(r):o.split(","));u.length||(u=t.keys(r)),t.each(u,function(e){s.append(r[e].render().el)})}),this.setElement(i),this},remove:function(){t.each(this.fields,function(e){e.remove()}),n.View.prototype.remove.call(this)}},{template:t.template("
    <% if (legend) { %> <%= legend %> <% } %>
    ",null,r.templateSettings)}),r.Field=n.View.extend({initialize:function(e){e=e||{},t.extend(this,t.pick(e,"form","key","model","value","idPrefix"));var n=this.schema=this.createSchema(e.schema);this.template=e.template||n.template||this.template||this.constructor.template,this.errorClassName=e.errorClassName||this.errorClassName||this.constructor.errorClassName,this.editor=this.createEditor()},createSchema:function(e){return t.isString(e)&&(e={type:e}),e=t.extend({type:"Text",title:this.createTitle()},e),e.type=t.isString(e.type)?r.editors[e.type]:e.type,e},createEditor:function(){var e=t.extend(t.pick(this,"schema","form","key","model","value"),{id:this.createEditorId()}),n=this.schema.type;return new n(e)},createEditorId:function(){var e=this.idPrefix,n=this.key;return n=n.replace(/\./g,"_"),t.isString(e)||t.isNumber(e)?e+n:t.isNull(e)?n:this.model?this.model.cid+"_"+n:n},createTitle:function(){var e=this.key;return e=e.replace(/([A-Z])/g," $1"),e=e.replace(/^./,function(e){return e.toUpperCase()}),e},templateData:function(){var e=this.schema;return{help:e.help||"",title:e.title,fieldAttrs:e.fieldAttrs,editorAttrs:e.editorAttrs,key:this.key,editorId:this.editor.id}},render:function(){var n=this.schema,i=this.editor;if(n.type==r.editors.Hidden)return this.setElement(i.render().el);var s=e(e.trim(this.template(t.result(this,"templateData"))));return n.fieldClass&&s.addClass(n.fieldClass),n.fieldAttrs&&s.attr(n.fieldAttrs),s.find("[data-editor]").add(s).each(function(n,r){var s=e(r),o=s.attr("data-editor");if(t.isUndefined(o))return;s.append(i.render().el)}),this.setElement(s),this},validate:function(){var e=this.editor.validate();return e?this.setError(e.message):this.clearError(),e},setError:function(e){if(this.editor.hasNestedForm)return;this.$el.addClass(this.errorClassName),this.$("[data-error]").html(e)},clearError:function(){this.$el.removeClass(this.errorClassName),this.$("[data-error]").empty()},commit:function(){return this.editor.commit()},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(),n.View.prototype.remove.call(this)}},{template:t.template('
    <%= help %>
    ',null,r.templateSettings),errorClassName:"error"}),r.NestedField=r.Field.extend({template:t.template(e.trim("
    <% if (help) { %>
    <%= help %>
    <% } %>
    "),null,r.templateSettings)}),r.Editor=r.editors.Base=n.View.extend({defaultValue:null,hasFocus:!1,initialize:function(e){var e=e||{};if(e.model){if(!e.key)throw new Error("Missing option: 'key'");this.model=e.model,this.value=this.model.get(e.key)}else e.value!==undefined&&(this.value=e.value);this.value===undefined&&(this.value=this.defaultValue),t.extend(this,t.pick(e,"key","form"));var n=this.schema=e.schema||{};this.validators=e.validators||n.validators,this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),n.editorClass&&this.$el.addClass(n.editorClass),n.editorAttrs&&this.$el.attr(n.editorAttrs)},getName:function(){var e=this.key||"";return e.replace(/\./g,"_")},getValue:function(){return this.value},setValue:function(e){this.value=e},focus:function(){throw new Error("Not implemented")},blur:function(){throw new Error("Not implemented")},commit:function(e){var t=this.validate();if(t)return t;this.listenTo(this.model,"invalid",function(e,n){t=n}),this.model.set(this.key,this.getValue(),e);if(t)return t},validate:function(){var e=this.$el,n=null,r=this.getValue(),i=this.form?this.form.getValue():{},s=this.validators,o=this.getValidator;return s&&t.every(s,function(e){return n=o(e)(r,i),n?!1:!0}),n},trigger:function(e){return e==="focus"?this.hasFocus=!0:e==="blur"&&(this.hasFocus=!1),n.View.prototype.trigger.apply(this,arguments)},getValidator:function(e){var n=r.validators;if(t.isRegExp(e))return n.regexp({regexp:e});if(t.isString(e)){if(!n[e])throw new Error('Validator "'+e+'" not found');return n[e]()}if(t.isFunction(e))return e;if(t.isObject(e)&&e.type){var i=e;return n[i.type](i)}throw new Error("Invalid validator: "+e)}}),r.editors.Text=r.Editor.extend({tagName:"input",defaultValue:"",previousValue:"",events:{keyup:"determineChange",keypress:function(e){var t=this;setTimeout(function(){t.determineChange()},0)},select:function(e){this.trigger("select",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){r.editors.Base.prototype.initialize.call(this,e);var t=this.schema,n="text";t&&t.editorAttrs&&t.editorAttrs.type&&(n=t.editorAttrs.type),t&&t.dataType&&(n=t.dataType),this.$el.attr("type",n)},render:function(){return this.setValue(this.value),this},determineChange:function(e){var t=this.$el.val(),n=t!==this.previousValue;n&&(this.previousValue=t,this.trigger("change",this))},getValue:function(){return this.$el.val()},setValue:function(e){this.$el.val(e)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()},select:function(){this.$el.select()}}),r.editors.TextArea=r.editors.Text.extend({tagName:"textarea",initialize:function(e){r.editors.Base.prototype.initialize.call(this,e)}}),r.editors.Password=r.editors.Text.extend({initialize:function(e){r.editors.Text.prototype.initialize.call(this,e),this.$el.attr("type","password")}}),r.editors.Number=r.editors.Text.extend({defaultValue:0,events:t.extend({},r.editors.Text.prototype.events,{keypress:"onKeyPress",change:"onKeyPress"}),initialize:function(e){r.editors.Text.prototype.initialize.call(this,e);var t=this.schema;this.$el.attr("type","number"),(!t||!t.editorAttrs||!t.editorAttrs.step)&&this.$el.attr("step","any")},onKeyPress:function(e){var t=this,n=function(){setTimeout(function(){t.determineChange()},0)};if(e.charCode===0){n();return}var r=this.$el.val();e.charCode!=undefined&&(r+=String.fromCharCode(e.charCode));var i=/^[0-9]*\.?[0-9]*?$/.test(r);i?n():e.preventDefault()},getValue:function(){var e=this.$el.val();return e===""?null:parseFloat(e,10)},setValue:function(e){e=function(){return t.isNumber(e)?e:t.isString(e)&&e!==""?parseFloat(e,10):null}(),t.isNaN(e)&&(e=null),r.editors.Text.prototype.setValue.call(this,e)}}),r.editors.Hidden=r.editors.Text.extend({defaultValue:"",initialize:function(e){r.editors.Text.prototype.initialize.call(this,e),this.$el.attr("type","hidden")},focus:function(){},blur:function(){}}),r.editors.Checkbox=r.editors.Base.extend({defaultValue:!1,tagName:"input",events:{click:function(e){this.trigger("change",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){r.editors.Base.prototype.initialize.call(this,e),this.$el.attr("type","checkbox")},render:function(){return this.setValue(this.value),this},getValue:function(){return this.$el.prop("checked")},setValue:function(e){e?this.$el.prop("checked",!0):this.$el.prop("checked",!1)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()}}),r.editors.Select=r.editors.Base.extend({tagName:"select",events:{change:function(e){this.trigger("change",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){r.editors.Base.prototype.initialize.call(this,e);if(!this.schema||!this.schema.options)throw new Error("Missing required 'schema.options'")},render:function(){return this.setOptions(this.schema.options),this},setOptions:function(e){var r=this;if(e instanceof n.Collection){var i=e;i.length>0?this.renderOptions(e):i.fetch({success:function(t){r.renderOptions(e)}})}else t.isFunction(e)?e(function(e){r.renderOptions(e)},r):this.renderOptions(e)},renderOptions:function(e){var t=this.$el,n;n=this._getOptionsHtml(e),t.html(n),this.setValue(this.value)},_getOptionsHtml:function(e){var r;if(t.isString(e))r=e;else if(t.isArray(e))r=this._arrayToHtml(e);else if(e instanceof n.Collection)r=this._collectionToHtml(e);else if(t.isFunction(e)){var i;e(function(e){i=e},this),r=this._getOptionsHtml(i)}else r=this._objectToHtml(e);return r},getValue:function(){return this.$el.val()},setValue:function(e){this.$el.val(e)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()},_collectionToHtml:function(e){var t=[];e.each(function(e){t.push({val:e.id,label:e.toString()})});var n=this._arrayToHtml(t);return n},_objectToHtml:function(e){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push({val:n,label:e[n]});var r=this._arrayToHtml(t);return r},_arrayToHtml:function(e){var n=[];return t.each(e,function(e){if(t.isObject(e))if(e.group)n.push(''),n.push(this._getOptionsHtml(e.options)),n.push("");else{var r=e.val||e.val===0?e.val:"";n.push('")}else n.push("")},this),n.join("")}}),r.editors.Radio=r.editors.Select.extend({tagName:"ul",events:{"change input[type=radio]":function(){this.trigger("change",this)},"focus input[type=radio]":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur input[type=radio]":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("input[type=radio]:focus")[0])return;e.trigger("blur",e)},0)}},getTemplate:function(){return this.schema.template||this.constructor.template},getValue:function(){return this.$("input[type=radio]:checked").val()},setValue:function(e){this.$("input[type=radio]").val([e])},focus:function(){if(this.hasFocus)return;var e=this.$("input[type=radio]:checked");if(e[0]){e.focus();return}this.$("input[type=radio]").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("input[type=radio]:focus").blur()},_arrayToHtml:function(e){var n=this,r=this.getTemplate(),i=n.getName(),s=n.id,o=t.map(e,function(e,n){var r={name:i,id:s+"-"+n};return t.isObject(e)?(r.value=e.val||e.val===0?e.val:"",r.label=e.label):(r.value=e,r.label=e),r});return r({items:o})}},{template:t.template(' ',null,r.templateSettings)}),r.editors.Checkboxes=r.editors.Select.extend({tagName:"ul",groupNumber:0,events:{"click input[type=checkbox]":function(){this.trigger("change",this)},"focus input[type=checkbox]":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur input[type=checkbox]":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("input[type=checkbox]:focus")[0])return;e.trigger("blur",e)},0)}},getValue:function(){var t=[];return this.$("input[type=checkbox]:checked").each(function(){t.push(e(this).val())}),t},setValue:function(e){t.isArray(e)||(e=[e]),this.$("input[type=checkbox]").val(e)},focus:function(){if(this.hasFocus)return;this.$("input[type=checkbox]").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("input[type=checkbox]:focus").blur()},_arrayToHtml:function(e){var n=[],r=this;return t.each(e,function(e,i){var s="
  • ",o=!0;if(t.isObject(e))if(e.group){var u=r.id;r.id+="-"+r.groupNumber++,s='
    '+e.group+"",s+=r._arrayToHtml(e.options),s+="
    ",r.id=u,o=!1}else{var a=e.val||e.val===0?e.val:"";s+='',s+='"}else s+='',s+='";o&&(s+="
  • "),n.push(s)}),n.join("")}}),r.editors.Object=r.editors.Base.extend({hasNestedForm:!0,initialize:function(e){this.value={},r.editors.Base.prototype.initialize.call(this,e);if(!this.form)throw new Error('Missing required option "form"');if(!this.schema.subSchema)throw new Error("Missing required 'schema.subSchema' option for Object editor")},render:function(){var e=this.form.constructor;return this.nestedForm=new e({schema:this.schema.subSchema,data:this.value,idPrefix:this.id+"_",Field:e.NestedField}),this._observeFormEvents(),this.$el.html(this.nestedForm.render().el),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){return this.nestedForm?this.nestedForm.getValue():this.value},setValue:function(e){this.value=e,this.render()},focus:function(){if(this.hasFocus)return;this.nestedForm.focus()},blur:function(){if(!this.hasFocus)return;this.nestedForm.blur()},remove:function(){this.nestedForm.remove(),n.View.prototype.remove.call(this)},validate:function(){return this.nestedForm.validate()},_observeFormEvents:function(){if(!this.nestedForm)return;this.nestedForm.on("all",function(){var e=t.toArray(arguments);e[1]=this,this.trigger.apply(this,e)},this)}}),r.editors.NestedModel=r.editors.Object.extend({initialize:function(e){r.editors.Base.prototype.initialize.call(this,e);if(!this.form)throw new Error('Missing required option "form"');if(!e.schema.model)throw new Error('Missing required "schema.model" option for NestedModel editor')},render:function(){var e=this.form.constructor,t=this.value||{},n=this.key,r=this.schema.model,i=t.constructor===r?t:new r(t);return this.nestedForm=new e({model:i,idPrefix:this.id+"_",fieldTemplate:"nestedField"}),this._observeFormEvents(),this.$el.html(this.nestedForm.render().el),this.hasFocus&&this.trigger("blur",this),this},commit:function(){var e=this.nestedForm.commit();return e?(this.$el.addClass("error"),e):r.editors.Object.prototype.commit.call(this)}}),r.editors.Date=r.editors.Base.extend({events:{"change select":function(){this.updateHidden(),this.trigger("change",this)},"focus select":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur select":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("select:focus")[0])return;e.trigger("blur",e)},0)}},initialize:function(e){e=e||{},r.editors.Base.prototype.initialize.call(this,e);var n=r.editors.Date,i=new Date;this.options=t.extend({monthNames:n.monthNames,showMonthNames:n.showMonthNames},e),this.schema=t.extend({yearStart:i.getFullYear()-100,yearEnd:i.getFullYear()},e.schema||{}),this.value&&!t.isDate(this.value)&&(this.value=new Date(this.value));if(!this.value){var s=new Date;s.setSeconds(0),s.setMilliseconds(0),this.value=s}this.template=e.template||this.constructor.template},render:function(){var n=this.options,r=this.schema,i=t.map(t.range(1,32),function(e){return'"}),s=t.map(t.range(0,12),function(e){var t=n.showMonthNames?n.monthNames[e]:e+1;return'"}),o=r.yearStart'+e+""}),a=e(e.trim(this.template({dates:i.join(""),months:s.join(""),years:u.join("")})));return this.$date=a.find('[data-type="date"]'),this.$month=a.find('[data-type="month"]'),this.$year=a.find('[data-type="year"]'),this.$hidden=e(''),a.append(this.$hidden),this.setValue(this.value),this.setElement(a),this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){var e=this.$year.val(),t=this.$month.val(),n=this.$date.val();return!e||!t||!n?null:new Date(e,t,n)},setValue:function(e){this.$date.val(e.getDate()),this.$month.val(e.getMonth()),this.$year.val(e.getFullYear()),this.updateHidden()},focus:function(){if(this.hasFocus)return;this.$("select").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("select:focus").blur()},updateHidden:function(){var e=this.getValue();t.isDate(e)&&(e=e.toISOString()),this.$hidden.val(e)}},{template:t.template('
    ',null,r.templateSettings),showMonthNames:!0,monthNames:["January","February","March","April","May","June","July","August","September","October","November","December"]}),r.editors.DateTime=r.editors.Base.extend({events:{"change select":function(){this.updateHidden(),this.trigger("change",this)},"focus select":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur select":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("select:focus")[0])return;e.trigger("blur",e)},0)}},initialize:function(e){e=e||{},r.editors.Base.prototype.initialize.call(this,e),this.options=t.extend({DateEditor:r.editors.DateTime.DateEditor},e),this.schema=t.extend({minsInterval:15},e.schema||{}),this.dateEditor=new this.options.DateEditor(e),this.value=this.dateEditor.value,this.template=e.template||this.constructor.template},render:function(){function n(e){return e<10?"0"+e:e}var r=this.schema,i=t.map(t.range(0,24),function(e){return'"}),s=t.map(t.range(0,60,r.minsInterval),function(e){return'"}),o=e(e.trim(this.template({hours:i.join(),mins:s.join()})));return o.find("[data-date]").append(this.dateEditor.render().el),this.$hour=o.find('select[data-type="hour"]'),this.$min=o.find('select[data-type="min"]'),this.$hidden=o.find('input[type="hidden"]'),this.setValue(this.value),this.setElement(o),this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){var e=this.dateEditor.getValue(),t=this.$hour.val(),n=this.$min.val();return!e||!t||!n?null:(e.setHours(t),e.setMinutes(n),e)},setValue:function(e){t.isDate(e)||(e=new Date(e)),this.dateEditor.setValue(e),this.$hour.val(e.getHours()),this.$min.val(e.getMinutes()),this.updateHidden()},focus:function(){if(this.hasFocus)return;this.$("select").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("select:focus").blur()},updateHidden:function(){var e=this.getValue();t.isDate(e)&&(e=e.toISOString()),this.$hidden.val(e)},remove:function(){this.dateEditor.remove(),r.editors.Base.prototype.remove.call(this)}},{template:t.template('
    :
    ',null,r.templateSettings),DateEditor:r.editors.Date}),r.VERSION="0.14.0",n.Form=r,r}) \ No newline at end of file diff --git a/distribution/backbone-forms.js b/distribution/backbone-forms.js index 12617b6e..3ef16476 100644 --- a/distribution/backbone-forms.js +++ b/distribution/backbone-forms.js @@ -601,7 +601,6 @@ Form.validators = (function() { })(); - //================================================================================================== //FIELDSET //================================================================================================== @@ -690,10 +689,19 @@ Form.Fieldset = Backbone.View.extend({ selection = $container.attr('data-fields'); if (_.isUndefined(selection)) return; + + //Work out which fields to include (_compact used to trim empty string selection) + var keys = _.compact((selection == '*') ? self.selectedFields || _.keys(fields) : selection.split(',')); + + //include all keys if not specified + if (!keys.length) + keys = _.keys(fields); - _.each(fields, function(field) { - $container.append(field.render().el); + //Add them + _.each(keys, function(key) { + $container.append(fields[key].render().el); }); + }); this.setElement($fieldset); diff --git a/distribution/backbone-forms.min.js b/distribution/backbone-forms.min.js index 7f6565d0..170170fd 100644 --- a/distribution/backbone-forms.min.js +++ b/distribution/backbone-forms.min.js @@ -1 +1 @@ -(function(e){if(typeof exports!="undefined"&&typeof require!="undefined")var t=e.jQuery||e.Zepto||e.ender||require("jquery"),n=e._||require("underscore"),r=e.Backbone||require("backbone");else var t=e.jQuery,n=e._,r=e.Backbone;var i=r.View.extend({events:{submit:function(e){this.trigger("submit",e)}},initialize:function(e){var t=this;e=e||{};var r=this.schema=function(){if(e.schema)return n.result(e,"schema");var r=e.model;return r&&r.schema?n.result(r,"schema"):t.schema?n.result(t,"schema"):{}}();n.extend(this,n.pick(e,"model","data","idPrefix","templateData"));var i=this.constructor;this.template=e.template||this.template||i.template,this.Fieldset=e.Fieldset||this.Fieldset||i.Fieldset,this.Field=e.Field||this.Field||i.Field,this.NestedField=e.NestedField||this.NestedField||i.NestedField;var s=this.selectedFields=e.fields||n.keys(r),o=this.fields={};n.each(s,function(e){var t=r[e];o[e]=this.createField(e,t)},this);var u=e.fieldsets||n.result(this,"fieldsets")||[s],a=this.fieldsets=[];n.each(u,function(e){this.fieldsets.push(this.createFieldset(e))},this)},createFieldset:function(e){var t={schema:e,fields:this.fields};return new this.Fieldset(t)},createField:function(e,t){var n={form:this,key:e,schema:t,idPrefix:this.idPrefix};this.model?n.model=this.model:this.data?n.value=this.data[e]:n.value=null;var r=new this.Field(n);return this.listenTo(r.editor,"all",this.handleEditorEvent),r},handleEditorEvent:function(e,t){var r=t.key+":"+e;this.trigger.call(this,r,this,t,Array.prototype.slice.call(arguments,2));switch(e){case"change":this.trigger("change",this);break;case"focus":this.hasFocus||this.trigger("focus",this);break;case"blur":if(this.hasFocus){var i=this;setTimeout(function(){var e=n.find(i.fields,function(e){return e.editor.hasFocus});e||i.trigger("blur",i)},0)}}},render:function(){var e=this,r=this.fields,i=t(t.trim(this.template(n.result(this,"templateData"))));return i.find("[data-editors]").add(i).each(function(i,s){var o=t(s),u=o.attr("data-editors");if(n.isUndefined(u))return;var a=u=="*"?e.selectedFields||n.keys(r):u.split(",");n.each(a,function(e){var t=r[e];o.append(t.editor.render().el)})}),i.find("[data-fields]").add(i).each(function(i,s){var o=t(s),u=o.attr("data-fields");if(n.isUndefined(u))return;var a=u=="*"?e.selectedFields||n.keys(r):u.split(",");n.each(a,function(e){var t=r[e];o.append(t.render().el)})}),i.find("[data-fieldsets]").add(i).each(function(r,i){var s=t(i),o=s.attr("data-fieldsets");if(n.isUndefined(o))return;n.each(e.fieldsets,function(e){s.append(e.render().el)})}),this.setElement(i),i.addClass(this.className),this},validate:function(e){var t=this,r=this.fields,i=this.model,s={};e=e||{},n.each(r,function(e){var t=e.validate();t&&(s[e.key]=t)});if(!e.skipModelValidate&&i&&i.validate){var o=i.validate(this.getValue());if(o){var u=n.isObject(o)&&!n.isArray(o);u||(s._others=s._others||[],s._others.push(o)),u&&n.each(o,function(e,t){if(r[t]&&!s[t])r[t].setError(e),s[t]=e;else{s._others=s._others||[];var n={};n[t]=e,s._others.push(n)}})}}return n.isEmpty(s)?null:s},commit:function(e){e=e||{};var t={skipModelValidate:!e.validate},r=this.validate(t);if(r)return r;var i,s=n.extend({error:function(e,t){i=t}},e);this.model.set(this.getValue(),s);if(i)return i},getValue:function(e){if(e)return this.fields[e].getValue();var t={};return n.each(this.fields,function(e){t[e.key]=e.getValue()}),t},setValue:function(e,t){var n={};typeof e=="string"?n[e]=t:n=e;var r;for(r in this.schema)n[r]!==undefined&&this.fields[r].setValue(n[r])},getEditor:function(e){var t=this.fields[e];if(!t)throw new Error("Field not found: "+e);return t.editor},focus:function(){if(this.hasFocus)return;var e=this.fieldsets[0],t=e.getFieldAt(0);if(!t)return;t.editor.focus()},blur:function(){if(!this.hasFocus)return;var e=n.find(this.fields,function(e){return e.editor.hasFocus});e&&e.editor.blur()},trigger:function(e){return e==="focus"?this.hasFocus=!0:e==="blur"&&(this.hasFocus=!1),r.View.prototype.trigger.apply(this,arguments)},remove:function(){return n.each(this.fieldsets,function(e){e.remove()}),n.each(this.fields,function(e){e.remove()}),r.View.prototype.remove.apply(this,arguments)}},{template:n.template("
    ",null,this.templateSettings),templateSettings:{evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g},editors:{}});i.validators=function(){var e={};return e.errMessages={required:"Required",regexp:"Invalid",number:"Must be a number",email:"Invalid email address",url:"Invalid URL",match:n.template('Must match field "<%= field %>"',null,i.templateSettings)},e.required=function(e){return e=n.extend({type:"required",message:this.errMessages.required},e),function(r){e.value=r;var i={type:e.type,message:n.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r===!1||r==="")return i}},e.regexp=function(e){if(!e.regexp)throw new Error('Missing required "regexp" option for "regexp" validator');return e=n.extend({type:"regexp",match:!0,message:this.errMessages.regexp},e),function(r){e.value=r;var i={type:e.type,message:n.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r==="")return;"string"==typeof e.regexp&&(e.regexp=new RegExp(e.regexp,e.flags));if(e.match?!e.regexp.test(r):e.regexp.test(r))return i}},e.number=function(t){return t=n.extend({type:"number",message:this.errMessages.number,regexp:/^[0-9]*\.?[0-9]*?$/},t),e.regexp(t)},e.email=function(t){return t=n.extend({type:"email",message:this.errMessages.email,regexp:/^[\w\-]{1,}([\w\-\+.]{1,1}[\w\-]{1,}){0,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/},t),e.regexp(t)},e.url=function(t){return t=n.extend({type:"url",message:this.errMessages.url,regexp:/^(http|https):\/\/(([A-Z0-9][A-Z0-9_\-]*)(\.[A-Z0-9][A-Z0-9_\-]*)+)(:(\d+))?\/?/i},t),e.regexp(t)},e.match=function(e){if(!e.field)throw new Error('Missing required "field" options for "match" validator');return e=n.extend({type:"match",message:this.errMessages.match},e),function(r,i){e.value=r;var s={type:e.type,message:n.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r==="")return;if(r!==i[e.field])return s}},e}(),i.Fieldset=r.View.extend({initialize:function(e){e=e||{};var t=this.schema=this.createSchema(e.schema);this.fields=n.pick(e.fields,t.fields),this.template=e.template||t.template||this.template||this.constructor.template},createSchema:function(e){return n.isArray(e)&&(e={fields:e}),e.legend=e.legend||null,e},getFieldAt:function(e){var t=this.schema.fields[e];return this.fields[t]},templateData:function(){return this.schema},render:function(){var e=this.schema,r=this.fields,i=t(t.trim(this.template(n.result(this,"templateData"))));return i.find("[data-fields]").add(i).each(function(e,i){var s=t(i),o=s.attr("data-fields");if(n.isUndefined(o))return;n.each(r,function(e){s.append(e.render().el)})}),this.setElement(i),this},remove:function(){n.each(this.fields,function(e){e.remove()}),r.View.prototype.remove.call(this)}},{template:n.template("
    <% if (legend) { %> <%= legend %> <% } %>
    ",null,i.templateSettings)}),i.Field=r.View.extend({initialize:function(e){e=e||{},n.extend(this,n.pick(e,"form","key","model","value","idPrefix"));var t=this.schema=this.createSchema(e.schema);this.template=e.template||t.template||this.template||this.constructor.template,this.errorClassName=e.errorClassName||this.errorClassName||this.constructor.errorClassName,this.editor=this.createEditor()},createSchema:function(e){return n.isString(e)&&(e={type:e}),e=n.extend({type:"Text",title:this.createTitle()},e),e.type=n.isString(e.type)?i.editors[e.type]:e.type,e},createEditor:function(){var e=n.extend(n.pick(this,"schema","form","key","model","value"),{id:this.createEditorId()}),t=this.schema.type;return new t(e)},createEditorId:function(){var e=this.idPrefix,t=this.key;return t=t.replace(/\./g,"_"),n.isString(e)||n.isNumber(e)?e+t:n.isNull(e)?t:this.model?this.model.cid+"_"+t:t},createTitle:function(){var e=this.key;return e=e.replace(/([A-Z])/g," $1"),e=e.replace(/^./,function(e){return e.toUpperCase()}),e},templateData:function(){var e=this.schema;return{help:e.help||"",title:e.title,fieldAttrs:e.fieldAttrs,editorAttrs:e.editorAttrs,key:this.key,editorId:this.editor.id}},render:function(){var e=this.schema,r=this.editor;if(e.type==i.editors.Hidden)return this.setElement(r.render().el);var s=t(t.trim(this.template(n.result(this,"templateData"))));return e.fieldClass&&s.addClass(e.fieldClass),e.fieldAttrs&&s.attr(e.fieldAttrs),s.find("[data-editor]").add(s).each(function(e,i){var s=t(i),o=s.attr("data-editor");if(n.isUndefined(o))return;s.append(r.render().el)}),this.setElement(s),this},validate:function(){var e=this.editor.validate();return e?this.setError(e.message):this.clearError(),e},setError:function(e){if(this.editor.hasNestedForm)return;this.$el.addClass(this.errorClassName),this.$("[data-error]").html(e)},clearError:function(){this.$el.removeClass(this.errorClassName),this.$("[data-error]").empty()},commit:function(){return this.editor.commit()},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(),r.View.prototype.remove.call(this)}},{template:n.template('
    <%= help %>
    ',null,i.templateSettings),errorClassName:"error"}),i.NestedField=i.Field.extend({template:n.template(t.trim("
    <% if (help) { %>
    <%= help %>
    <% } %>
    "),null,i.templateSettings)}),i.Editor=i.editors.Base=r.View.extend({defaultValue:null,hasFocus:!1,initialize:function(e){var e=e||{};if(e.model){if(!e.key)throw new Error("Missing option: 'key'");this.model=e.model,this.value=this.model.get(e.key)}else e.value!==undefined&&(this.value=e.value);this.value===undefined&&(this.value=this.defaultValue),n.extend(this,n.pick(e,"key","form"));var t=this.schema=e.schema||{};this.validators=e.validators||t.validators,this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),t.editorClass&&this.$el.addClass(t.editorClass),t.editorAttrs&&this.$el.attr(t.editorAttrs)},getName:function(){var e=this.key||"";return e.replace(/\./g,"_")},getValue:function(){return this.value},setValue:function(e){this.value=e},focus:function(){throw new Error("Not implemented")},blur:function(){throw new Error("Not implemented")},commit:function(e){var t=this.validate();if(t)return t;this.listenTo(this.model,"invalid",function(e,n){t=n}),this.model.set(this.key,this.getValue(),e);if(t)return t},validate:function(){var e=this.$el,t=null,r=this.getValue(),i=this.form?this.form.getValue():{},s=this.validators,o=this.getValidator;return s&&n.every(s,function(e){return t=o(e)(r,i),t?!1:!0}),t},trigger:function(e){return e==="focus"?this.hasFocus=!0:e==="blur"&&(this.hasFocus=!1),r.View.prototype.trigger.apply(this,arguments)},getValidator:function(e){var t=i.validators;if(n.isRegExp(e))return t.regexp({regexp:e});if(n.isString(e)){if(!t[e])throw new Error('Validator "'+e+'" not found');return t[e]()}if(n.isFunction(e))return e;if(n.isObject(e)&&e.type){var r=e;return t[r.type](r)}throw new Error("Invalid validator: "+e)}}),i.editors.Text=i.Editor.extend({tagName:"input",defaultValue:"",previousValue:"",events:{keyup:"determineChange",keypress:function(e){var t=this;setTimeout(function(){t.determineChange()},0)},select:function(e){this.trigger("select",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){i.editors.Base.prototype.initialize.call(this,e);var t=this.schema,n="text";t&&t.editorAttrs&&t.editorAttrs.type&&(n=t.editorAttrs.type),t&&t.dataType&&(n=t.dataType),this.$el.attr("type",n)},render:function(){return this.setValue(this.value),this},determineChange:function(e){var t=this.$el.val(),n=t!==this.previousValue;n&&(this.previousValue=t,this.trigger("change",this))},getValue:function(){return this.$el.val()},setValue:function(e){this.$el.val(e)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()},select:function(){this.$el.select()}}),i.editors.TextArea=i.editors.Text.extend({tagName:"textarea",initialize:function(e){i.editors.Base.prototype.initialize.call(this,e)}}),i.editors.Password=i.editors.Text.extend({initialize:function(e){i.editors.Text.prototype.initialize.call(this,e),this.$el.attr("type","password")}}),i.editors.Number=i.editors.Text.extend({defaultValue:0,events:n.extend({},i.editors.Text.prototype.events,{keypress:"onKeyPress",change:"onKeyPress"}),initialize:function(e){i.editors.Text.prototype.initialize.call(this,e);var t=this.schema;this.$el.attr("type","number"),(!t||!t.editorAttrs||!t.editorAttrs.step)&&this.$el.attr("step","any")},onKeyPress:function(e){var t=this,n=function(){setTimeout(function(){t.determineChange()},0)};if(e.charCode===0){n();return}var r=this.$el.val();e.charCode!=undefined&&(r+=String.fromCharCode(e.charCode));var i=/^[0-9]*\.?[0-9]*?$/.test(r);i?n():e.preventDefault()},getValue:function(){var e=this.$el.val();return e===""?null:parseFloat(e,10)},setValue:function(e){e=function(){return n.isNumber(e)?e:n.isString(e)&&e!==""?parseFloat(e,10):null}(),n.isNaN(e)&&(e=null),i.editors.Text.prototype.setValue.call(this,e)}}),i.editors.Hidden=i.editors.Text.extend({defaultValue:"",initialize:function(e){i.editors.Text.prototype.initialize.call(this,e),this.$el.attr("type","hidden")},focus:function(){},blur:function(){}}),i.editors.Checkbox=i.editors.Base.extend({defaultValue:!1,tagName:"input",events:{click:function(e){this.trigger("change",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){i.editors.Base.prototype.initialize.call(this,e),this.$el.attr("type","checkbox")},render:function(){return this.setValue(this.value),this},getValue:function(){return this.$el.prop("checked")},setValue:function(e){e?this.$el.prop("checked",!0):this.$el.prop("checked",!1)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()}}),i.editors.Select=i.editors.Base.extend({tagName:"select",events:{change:function(e){this.trigger("change",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){i.editors.Base.prototype.initialize.call(this,e);if(!this.schema||!this.schema.options)throw new Error("Missing required 'schema.options'")},render:function(){return this.setOptions(this.schema.options),this},setOptions:function(e){var t=this;if(e instanceof r.Collection){var i=e;i.length>0?this.renderOptions(e):i.fetch({success:function(n){t.renderOptions(e)}})}else n.isFunction(e)?e(function(e){t.renderOptions(e)},t):this.renderOptions(e)},renderOptions:function(e){var t=this.$el,n;n=this._getOptionsHtml(e),t.html(n),this.setValue(this.value)},_getOptionsHtml:function(e){var t;if(n.isString(e))t=e;else if(n.isArray(e))t=this._arrayToHtml(e);else if(e instanceof r.Collection)t=this._collectionToHtml(e);else if(n.isFunction(e)){var i;e(function(e){i=e},this),t=this._getOptionsHtml(i)}else t=this._objectToHtml(e);return t},getValue:function(){return this.$el.val()},setValue:function(e){this.$el.val(e)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()},_collectionToHtml:function(e){var t=[];e.each(function(e){t.push({val:e.id,label:e.toString()})});var n=this._arrayToHtml(t);return n},_objectToHtml:function(e){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push({val:n,label:e[n]});var r=this._arrayToHtml(t);return r},_arrayToHtml:function(e){var t=[];return n.each(e,function(e){if(n.isObject(e))if(e.group)t.push(''),t.push(this._getOptionsHtml(e.options)),t.push("");else{var r=e.val||e.val===0?e.val:"";t.push('")}else t.push("")},this),t.join("")}}),i.editors.Radio=i.editors.Select.extend({tagName:"ul",events:{"change input[type=radio]":function(){this.trigger("change",this)},"focus input[type=radio]":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur input[type=radio]":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("input[type=radio]:focus")[0])return;e.trigger("blur",e)},0)}},getTemplate:function(){return this.schema.template||this.constructor.template},getValue:function(){return this.$("input[type=radio]:checked").val()},setValue:function(e){this.$("input[type=radio]").val([e])},focus:function(){if(this.hasFocus)return;var e=this.$("input[type=radio]:checked");if(e[0]){e.focus();return}this.$("input[type=radio]").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("input[type=radio]:focus").blur()},_arrayToHtml:function(e){var t=this,r=this.getTemplate(),i=t.getName(),s=t.id,o=n.map(e,function(e,t){var r={name:i,id:s+"-"+t};return n.isObject(e)?(r.value=e.val||e.val===0?e.val:"",r.label=e.label):(r.value=e,r.label=e),r});return r({items:o})}},{template:n.template(' ',null,i.templateSettings)}),i.editors.Checkboxes=i.editors.Select.extend({tagName:"ul",groupNumber:0,events:{"click input[type=checkbox]":function(){this.trigger("change",this)},"focus input[type=checkbox]":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur input[type=checkbox]":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("input[type=checkbox]:focus")[0])return;e.trigger("blur",e)},0)}},getValue:function(){var e=[];return this.$("input[type=checkbox]:checked").each(function(){e.push(t(this).val())}),e},setValue:function(e){n.isArray(e)||(e=[e]),this.$("input[type=checkbox]").val(e)},focus:function(){if(this.hasFocus)return;this.$("input[type=checkbox]").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("input[type=checkbox]:focus").blur()},_arrayToHtml:function(e){var t=[],r=this;return n.each(e,function(e,i){var s="
  • ",o=!0;if(n.isObject(e))if(e.group){var u=r.id;r.id+="-"+r.groupNumber++,s='
    '+e.group+"",s+=r._arrayToHtml(e.options),s+="
    ",r.id=u,o=!1}else{var a=e.val||e.val===0?e.val:"";s+='',s+='"}else s+='',s+='";o&&(s+="
  • "),t.push(s)}),t.join("")}}),i.editors.Object=i.editors.Base.extend({hasNestedForm:!0,initialize:function(e){this.value={},i.editors.Base.prototype.initialize.call(this,e);if(!this.form)throw new Error('Missing required option "form"');if(!this.schema.subSchema)throw new Error("Missing required 'schema.subSchema' option for Object editor")},render:function(){var e=this.form.constructor;return this.nestedForm=new e({schema:this.schema.subSchema,data:this.value,idPrefix:this.id+"_",Field:e.NestedField}),this._observeFormEvents(),this.$el.html(this.nestedForm.render().el),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){return this.nestedForm?this.nestedForm.getValue():this.value},setValue:function(e){this.value=e,this.render()},focus:function(){if(this.hasFocus)return;this.nestedForm.focus()},blur:function(){if(!this.hasFocus)return;this.nestedForm.blur()},remove:function(){this.nestedForm.remove(),r.View.prototype.remove.call(this)},validate:function(){return this.nestedForm.validate()},_observeFormEvents:function(){if(!this.nestedForm)return;this.nestedForm.on("all",function(){var e=n.toArray(arguments);e[1]=this,this.trigger.apply(this,e)},this)}}),i.editors.NestedModel=i.editors.Object.extend({initialize:function(e){i.editors.Base.prototype.initialize.call(this,e);if(!this.form)throw new Error('Missing required option "form"');if(!e.schema.model)throw new Error('Missing required "schema.model" option for NestedModel editor')},render:function(){var e=this.form.constructor,t=this.value||{},n=this.key,r=this.schema.model,i=t.constructor===r?t:new r(t);return this.nestedForm=new e({model:i,idPrefix:this.id+"_",fieldTemplate:"nestedField"}),this._observeFormEvents(),this.$el.html(this.nestedForm.render().el),this.hasFocus&&this.trigger("blur",this),this},commit:function(){var e=this.nestedForm.commit();return e?(this.$el.addClass("error"),e):i.editors.Object.prototype.commit.call(this)}}),i.editors.Date=i.editors.Base.extend({events:{"change select":function(){this.updateHidden(),this.trigger("change",this)},"focus select":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur select":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("select:focus")[0])return;e.trigger("blur",e)},0)}},initialize:function(e){e=e||{},i.editors.Base.prototype.initialize.call(this,e);var t=i.editors.Date,r=new Date;this.options=n.extend({monthNames:t.monthNames,showMonthNames:t.showMonthNames},e),this.schema=n.extend({yearStart:r.getFullYear()-100,yearEnd:r.getFullYear()},e.schema||{}),this.value&&!n.isDate(this.value)&&(this.value=new Date(this.value));if(!this.value){var s=new Date;s.setSeconds(0),s.setMilliseconds(0),this.value=s}this.template=e.template||this.constructor.template},render:function(){var e=this.options,r=this.schema,i=n.map(n.range(1,32),function(e){return'"}),s=n.map(n.range(0,12),function(t){var n=e.showMonthNames?e.monthNames[t]:t+1;return'"}),o=r.yearStart'+e+""}),a=t(t.trim(this.template({dates:i.join(""),months:s.join(""),years:u.join("")})));return this.$date=a.find('[data-type="date"]'),this.$month=a.find('[data-type="month"]'),this.$year=a.find('[data-type="year"]'),this.$hidden=t(''),a.append(this.$hidden),this.setValue(this.value),this.setElement(a),this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){var e=this.$year.val(),t=this.$month.val(),n=this.$date.val();return!e||!t||!n?null:new Date(e,t,n)},setValue:function(e){this.$date.val(e.getDate()),this.$month.val(e.getMonth()),this.$year.val(e.getFullYear()),this.updateHidden()},focus:function(){if(this.hasFocus)return;this.$("select").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("select:focus").blur()},updateHidden:function(){var e=this.getValue();n.isDate(e)&&(e=e.toISOString()),this.$hidden.val(e)}},{template:n.template('
    ',null,i.templateSettings),showMonthNames:!0,monthNames:["January","February","March","April","May","June","July","August","September","October","November","December"]}),i.editors.DateTime=i.editors.Base.extend({events:{"change select":function(){this.updateHidden(),this.trigger("change",this)},"focus select":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur select":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("select:focus")[0])return;e.trigger("blur",e)},0)}},initialize:function(e){e=e||{},i.editors.Base.prototype.initialize.call(this,e),this.options=n.extend({DateEditor:i.editors.DateTime.DateEditor},e),this.schema=n.extend({minsInterval:15},e.schema||{}),this.dateEditor=new this.options.DateEditor(e),this.value=this.dateEditor.value,this.template=e.template||this.constructor.template},render:function(){function e(e){return e<10?"0"+e:e}var r=this.schema,i=n.map(n.range(0,24),function(t){return'"}),s=n.map(n.range(0,60,r.minsInterval),function(t){return'"}),o=t(t.trim(this.template({hours:i.join(),mins:s.join()})));return o.find("[data-date]").append(this.dateEditor.render().el),this.$hour=o.find('select[data-type="hour"]'),this.$min=o.find('select[data-type="min"]'),this.$hidden=o.find('input[type="hidden"]'),this.setValue(this.value),this.setElement(o),this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){var e=this.dateEditor.getValue(),t=this.$hour.val(),n=this.$min.val();return!e||!t||!n?null:(e.setHours(t),e.setMinutes(n),e)},setValue:function(e){n.isDate(e)||(e=new Date(e)),this.dateEditor.setValue(e),this.$hour.val(e.getHours()),this.$min.val(e.getMinutes()),this.updateHidden()},focus:function(){if(this.hasFocus)return;this.$("select").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("select:focus").blur()},updateHidden:function(){var e=this.getValue();n.isDate(e)&&(e=e.toISOString()),this.$hidden.val(e)},remove:function(){this.dateEditor.remove(),i.editors.Base.prototype.remove.call(this)}},{template:n.template('
    :
    ',null,i.templateSettings),DateEditor:i.editors.Date}),i.VERSION="0.14.0",r.Form=i,typeof exports!="undefined"&&(exports=i)})(window||global||this) \ No newline at end of file +(function(e){if(typeof exports!="undefined"&&typeof require!="undefined")var t=e.jQuery||e.Zepto||e.ender||require("jquery"),n=e._||require("underscore"),r=e.Backbone||require("backbone");else var t=e.jQuery,n=e._,r=e.Backbone;var i=r.View.extend({events:{submit:function(e){this.trigger("submit",e)}},initialize:function(e){var t=this;e=e||{};var r=this.schema=function(){if(e.schema)return n.result(e,"schema");var r=e.model;return r&&r.schema?n.result(r,"schema"):t.schema?n.result(t,"schema"):{}}();n.extend(this,n.pick(e,"model","data","idPrefix","templateData"));var i=this.constructor;this.template=e.template||this.template||i.template,this.Fieldset=e.Fieldset||this.Fieldset||i.Fieldset,this.Field=e.Field||this.Field||i.Field,this.NestedField=e.NestedField||this.NestedField||i.NestedField;var s=this.selectedFields=e.fields||n.keys(r),o=this.fields={};n.each(s,function(e){var t=r[e];o[e]=this.createField(e,t)},this);var u=e.fieldsets||n.result(this,"fieldsets")||[s],a=this.fieldsets=[];n.each(u,function(e){this.fieldsets.push(this.createFieldset(e))},this)},createFieldset:function(e){var t={schema:e,fields:this.fields};return new this.Fieldset(t)},createField:function(e,t){var n={form:this,key:e,schema:t,idPrefix:this.idPrefix};this.model?n.model=this.model:this.data?n.value=this.data[e]:n.value=null;var r=new this.Field(n);return this.listenTo(r.editor,"all",this.handleEditorEvent),r},handleEditorEvent:function(e,t){var r=t.key+":"+e;this.trigger.call(this,r,this,t,Array.prototype.slice.call(arguments,2));switch(e){case"change":this.trigger("change",this);break;case"focus":this.hasFocus||this.trigger("focus",this);break;case"blur":if(this.hasFocus){var i=this;setTimeout(function(){var e=n.find(i.fields,function(e){return e.editor.hasFocus});e||i.trigger("blur",i)},0)}}},render:function(){var e=this,r=this.fields,i=t(t.trim(this.template(n.result(this,"templateData"))));return i.find("[data-editors]").add(i).each(function(i,s){var o=t(s),u=o.attr("data-editors");if(n.isUndefined(u))return;var a=u=="*"?e.selectedFields||n.keys(r):u.split(",");n.each(a,function(e){var t=r[e];o.append(t.editor.render().el)})}),i.find("[data-fields]").add(i).each(function(i,s){var o=t(s),u=o.attr("data-fields");if(n.isUndefined(u))return;var a=u=="*"?e.selectedFields||n.keys(r):u.split(",");n.each(a,function(e){var t=r[e];o.append(t.render().el)})}),i.find("[data-fieldsets]").add(i).each(function(r,i){var s=t(i),o=s.attr("data-fieldsets");if(n.isUndefined(o))return;n.each(e.fieldsets,function(e){s.append(e.render().el)})}),this.setElement(i),i.addClass(this.className),this},validate:function(e){var t=this,r=this.fields,i=this.model,s={};e=e||{},n.each(r,function(e){var t=e.validate();t&&(s[e.key]=t)});if(!e.skipModelValidate&&i&&i.validate){var o=i.validate(this.getValue());if(o){var u=n.isObject(o)&&!n.isArray(o);u||(s._others=s._others||[],s._others.push(o)),u&&n.each(o,function(e,t){if(r[t]&&!s[t])r[t].setError(e),s[t]=e;else{s._others=s._others||[];var n={};n[t]=e,s._others.push(n)}})}}return n.isEmpty(s)?null:s},commit:function(e){e=e||{};var t={skipModelValidate:!e.validate},r=this.validate(t);if(r)return r;var i,s=n.extend({error:function(e,t){i=t}},e);this.model.set(this.getValue(),s);if(i)return i},getValue:function(e){if(e)return this.fields[e].getValue();var t={};return n.each(this.fields,function(e){t[e.key]=e.getValue()}),t},setValue:function(e,t){var n={};typeof e=="string"?n[e]=t:n=e;var r;for(r in this.schema)n[r]!==undefined&&this.fields[r].setValue(n[r])},getEditor:function(e){var t=this.fields[e];if(!t)throw new Error("Field not found: "+e);return t.editor},focus:function(){if(this.hasFocus)return;var e=this.fieldsets[0],t=e.getFieldAt(0);if(!t)return;t.editor.focus()},blur:function(){if(!this.hasFocus)return;var e=n.find(this.fields,function(e){return e.editor.hasFocus});e&&e.editor.blur()},trigger:function(e){return e==="focus"?this.hasFocus=!0:e==="blur"&&(this.hasFocus=!1),r.View.prototype.trigger.apply(this,arguments)},remove:function(){return n.each(this.fieldsets,function(e){e.remove()}),n.each(this.fields,function(e){e.remove()}),r.View.prototype.remove.apply(this,arguments)}},{template:n.template("
    ",null,this.templateSettings),templateSettings:{evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g},editors:{}});i.validators=function(){var e={};return e.errMessages={required:"Required",regexp:"Invalid",number:"Must be a number",email:"Invalid email address",url:"Invalid URL",match:n.template('Must match field "<%= field %>"',null,i.templateSettings)},e.required=function(e){return e=n.extend({type:"required",message:this.errMessages.required},e),function(r){e.value=r;var i={type:e.type,message:n.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r===!1||r==="")return i}},e.regexp=function(e){if(!e.regexp)throw new Error('Missing required "regexp" option for "regexp" validator');return e=n.extend({type:"regexp",match:!0,message:this.errMessages.regexp},e),function(r){e.value=r;var i={type:e.type,message:n.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r==="")return;"string"==typeof e.regexp&&(e.regexp=new RegExp(e.regexp,e.flags));if(e.match?!e.regexp.test(r):e.regexp.test(r))return i}},e.number=function(t){return t=n.extend({type:"number",message:this.errMessages.number,regexp:/^[0-9]*\.?[0-9]*?$/},t),e.regexp(t)},e.email=function(t){return t=n.extend({type:"email",message:this.errMessages.email,regexp:/^[\w\-]{1,}([\w\-\+.]{1,1}[\w\-]{1,}){0,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/},t),e.regexp(t)},e.url=function(t){return t=n.extend({type:"url",message:this.errMessages.url,regexp:/^(http|https):\/\/(([A-Z0-9][A-Z0-9_\-]*)(\.[A-Z0-9][A-Z0-9_\-]*)+)(:(\d+))?\/?/i},t),e.regexp(t)},e.match=function(e){if(!e.field)throw new Error('Missing required "field" options for "match" validator');return e=n.extend({type:"match",message:this.errMessages.match},e),function(r,i){e.value=r;var s={type:e.type,message:n.isFunction(e.message)?e.message(e):e.message};if(r===null||r===undefined||r==="")return;if(r!==i[e.field])return s}},e}(),i.Fieldset=r.View.extend({initialize:function(e){e=e||{};var t=this.schema=this.createSchema(e.schema);this.fields=n.pick(e.fields,t.fields),this.template=e.template||t.template||this.template||this.constructor.template},createSchema:function(e){return n.isArray(e)&&(e={fields:e}),e.legend=e.legend||null,e},getFieldAt:function(e){var t=this.schema.fields[e];return this.fields[t]},templateData:function(){return this.schema},render:function(){var e=this.schema,r=this.fields,i=t(t.trim(this.template(n.result(this,"templateData"))));return i.find("[data-fields]").add(i).each(function(e,i){var s=t(i),o=s.attr("data-fields");if(n.isUndefined(o))return;var u=n.compact(o=="*"?self.selectedFields||n.keys(r):o.split(","));u.length||(u=n.keys(r)),n.each(u,function(e){s.append(r[e].render().el)})}),this.setElement(i),this},remove:function(){n.each(this.fields,function(e){e.remove()}),r.View.prototype.remove.call(this)}},{template:n.template("
    <% if (legend) { %> <%= legend %> <% } %>
    ",null,i.templateSettings)}),i.Field=r.View.extend({initialize:function(e){e=e||{},n.extend(this,n.pick(e,"form","key","model","value","idPrefix"));var t=this.schema=this.createSchema(e.schema);this.template=e.template||t.template||this.template||this.constructor.template,this.errorClassName=e.errorClassName||this.errorClassName||this.constructor.errorClassName,this.editor=this.createEditor()},createSchema:function(e){return n.isString(e)&&(e={type:e}),e=n.extend({type:"Text",title:this.createTitle()},e),e.type=n.isString(e.type)?i.editors[e.type]:e.type,e},createEditor:function(){var e=n.extend(n.pick(this,"schema","form","key","model","value"),{id:this.createEditorId()}),t=this.schema.type;return new t(e)},createEditorId:function(){var e=this.idPrefix,t=this.key;return t=t.replace(/\./g,"_"),n.isString(e)||n.isNumber(e)?e+t:n.isNull(e)?t:this.model?this.model.cid+"_"+t:t},createTitle:function(){var e=this.key;return e=e.replace(/([A-Z])/g," $1"),e=e.replace(/^./,function(e){return e.toUpperCase()}),e},templateData:function(){var e=this.schema;return{help:e.help||"",title:e.title,fieldAttrs:e.fieldAttrs,editorAttrs:e.editorAttrs,key:this.key,editorId:this.editor.id}},render:function(){var e=this.schema,r=this.editor;if(e.type==i.editors.Hidden)return this.setElement(r.render().el);var s=t(t.trim(this.template(n.result(this,"templateData"))));return e.fieldClass&&s.addClass(e.fieldClass),e.fieldAttrs&&s.attr(e.fieldAttrs),s.find("[data-editor]").add(s).each(function(e,i){var s=t(i),o=s.attr("data-editor");if(n.isUndefined(o))return;s.append(r.render().el)}),this.setElement(s),this},validate:function(){var e=this.editor.validate();return e?this.setError(e.message):this.clearError(),e},setError:function(e){if(this.editor.hasNestedForm)return;this.$el.addClass(this.errorClassName),this.$("[data-error]").html(e)},clearError:function(){this.$el.removeClass(this.errorClassName),this.$("[data-error]").empty()},commit:function(){return this.editor.commit()},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(),r.View.prototype.remove.call(this)}},{template:n.template('
    <%= help %>
    ',null,i.templateSettings),errorClassName:"error"}),i.NestedField=i.Field.extend({template:n.template(t.trim("
    <% if (help) { %>
    <%= help %>
    <% } %>
    "),null,i.templateSettings)}),i.Editor=i.editors.Base=r.View.extend({defaultValue:null,hasFocus:!1,initialize:function(e){var e=e||{};if(e.model){if(!e.key)throw new Error("Missing option: 'key'");this.model=e.model,this.value=this.model.get(e.key)}else e.value!==undefined&&(this.value=e.value);this.value===undefined&&(this.value=this.defaultValue),n.extend(this,n.pick(e,"key","form"));var t=this.schema=e.schema||{};this.validators=e.validators||t.validators,this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),t.editorClass&&this.$el.addClass(t.editorClass),t.editorAttrs&&this.$el.attr(t.editorAttrs)},getName:function(){var e=this.key||"";return e.replace(/\./g,"_")},getValue:function(){return this.value},setValue:function(e){this.value=e},focus:function(){throw new Error("Not implemented")},blur:function(){throw new Error("Not implemented")},commit:function(e){var t=this.validate();if(t)return t;this.listenTo(this.model,"invalid",function(e,n){t=n}),this.model.set(this.key,this.getValue(),e);if(t)return t},validate:function(){var e=this.$el,t=null,r=this.getValue(),i=this.form?this.form.getValue():{},s=this.validators,o=this.getValidator;return s&&n.every(s,function(e){return t=o(e)(r,i),t?!1:!0}),t},trigger:function(e){return e==="focus"?this.hasFocus=!0:e==="blur"&&(this.hasFocus=!1),r.View.prototype.trigger.apply(this,arguments)},getValidator:function(e){var t=i.validators;if(n.isRegExp(e))return t.regexp({regexp:e});if(n.isString(e)){if(!t[e])throw new Error('Validator "'+e+'" not found');return t[e]()}if(n.isFunction(e))return e;if(n.isObject(e)&&e.type){var r=e;return t[r.type](r)}throw new Error("Invalid validator: "+e)}}),i.editors.Text=i.Editor.extend({tagName:"input",defaultValue:"",previousValue:"",events:{keyup:"determineChange",keypress:function(e){var t=this;setTimeout(function(){t.determineChange()},0)},select:function(e){this.trigger("select",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){i.editors.Base.prototype.initialize.call(this,e);var t=this.schema,n="text";t&&t.editorAttrs&&t.editorAttrs.type&&(n=t.editorAttrs.type),t&&t.dataType&&(n=t.dataType),this.$el.attr("type",n)},render:function(){return this.setValue(this.value),this},determineChange:function(e){var t=this.$el.val(),n=t!==this.previousValue;n&&(this.previousValue=t,this.trigger("change",this))},getValue:function(){return this.$el.val()},setValue:function(e){this.$el.val(e)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()},select:function(){this.$el.select()}}),i.editors.TextArea=i.editors.Text.extend({tagName:"textarea",initialize:function(e){i.editors.Base.prototype.initialize.call(this,e)}}),i.editors.Password=i.editors.Text.extend({initialize:function(e){i.editors.Text.prototype.initialize.call(this,e),this.$el.attr("type","password")}}),i.editors.Number=i.editors.Text.extend({defaultValue:0,events:n.extend({},i.editors.Text.prototype.events,{keypress:"onKeyPress",change:"onKeyPress"}),initialize:function(e){i.editors.Text.prototype.initialize.call(this,e);var t=this.schema;this.$el.attr("type","number"),(!t||!t.editorAttrs||!t.editorAttrs.step)&&this.$el.attr("step","any")},onKeyPress:function(e){var t=this,n=function(){setTimeout(function(){t.determineChange()},0)};if(e.charCode===0){n();return}var r=this.$el.val();e.charCode!=undefined&&(r+=String.fromCharCode(e.charCode));var i=/^[0-9]*\.?[0-9]*?$/.test(r);i?n():e.preventDefault()},getValue:function(){var e=this.$el.val();return e===""?null:parseFloat(e,10)},setValue:function(e){e=function(){return n.isNumber(e)?e:n.isString(e)&&e!==""?parseFloat(e,10):null}(),n.isNaN(e)&&(e=null),i.editors.Text.prototype.setValue.call(this,e)}}),i.editors.Hidden=i.editors.Text.extend({defaultValue:"",initialize:function(e){i.editors.Text.prototype.initialize.call(this,e),this.$el.attr("type","hidden")},focus:function(){},blur:function(){}}),i.editors.Checkbox=i.editors.Base.extend({defaultValue:!1,tagName:"input",events:{click:function(e){this.trigger("change",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){i.editors.Base.prototype.initialize.call(this,e),this.$el.attr("type","checkbox")},render:function(){return this.setValue(this.value),this},getValue:function(){return this.$el.prop("checked")},setValue:function(e){e?this.$el.prop("checked",!0):this.$el.prop("checked",!1)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()}}),i.editors.Select=i.editors.Base.extend({tagName:"select",events:{change:function(e){this.trigger("change",this)},focus:function(e){this.trigger("focus",this)},blur:function(e){this.trigger("blur",this)}},initialize:function(e){i.editors.Base.prototype.initialize.call(this,e);if(!this.schema||!this.schema.options)throw new Error("Missing required 'schema.options'")},render:function(){return this.setOptions(this.schema.options),this},setOptions:function(e){var t=this;if(e instanceof r.Collection){var i=e;i.length>0?this.renderOptions(e):i.fetch({success:function(n){t.renderOptions(e)}})}else n.isFunction(e)?e(function(e){t.renderOptions(e)},t):this.renderOptions(e)},renderOptions:function(e){var t=this.$el,n;n=this._getOptionsHtml(e),t.html(n),this.setValue(this.value)},_getOptionsHtml:function(e){var t;if(n.isString(e))t=e;else if(n.isArray(e))t=this._arrayToHtml(e);else if(e instanceof r.Collection)t=this._collectionToHtml(e);else if(n.isFunction(e)){var i;e(function(e){i=e},this),t=this._getOptionsHtml(i)}else t=this._objectToHtml(e);return t},getValue:function(){return this.$el.val()},setValue:function(e){this.$el.val(e)},focus:function(){if(this.hasFocus)return;this.$el.focus()},blur:function(){if(!this.hasFocus)return;this.$el.blur()},_collectionToHtml:function(e){var t=[];e.each(function(e){t.push({val:e.id,label:e.toString()})});var n=this._arrayToHtml(t);return n},_objectToHtml:function(e){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push({val:n,label:e[n]});var r=this._arrayToHtml(t);return r},_arrayToHtml:function(e){var t=[];return n.each(e,function(e){if(n.isObject(e))if(e.group)t.push(''),t.push(this._getOptionsHtml(e.options)),t.push("");else{var r=e.val||e.val===0?e.val:"";t.push('")}else t.push("")},this),t.join("")}}),i.editors.Radio=i.editors.Select.extend({tagName:"ul",events:{"change input[type=radio]":function(){this.trigger("change",this)},"focus input[type=radio]":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur input[type=radio]":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("input[type=radio]:focus")[0])return;e.trigger("blur",e)},0)}},getTemplate:function(){return this.schema.template||this.constructor.template},getValue:function(){return this.$("input[type=radio]:checked").val()},setValue:function(e){this.$("input[type=radio]").val([e])},focus:function(){if(this.hasFocus)return;var e=this.$("input[type=radio]:checked");if(e[0]){e.focus();return}this.$("input[type=radio]").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("input[type=radio]:focus").blur()},_arrayToHtml:function(e){var t=this,r=this.getTemplate(),i=t.getName(),s=t.id,o=n.map(e,function(e,t){var r={name:i,id:s+"-"+t};return n.isObject(e)?(r.value=e.val||e.val===0?e.val:"",r.label=e.label):(r.value=e,r.label=e),r});return r({items:o})}},{template:n.template('
      <% _.each(items, function(item) { %>
    • <% }); %>
    ',null,i.templateSettings)}),i.editors.Checkboxes=i.editors.Select.extend({tagName:"ul",groupNumber:0,events:{"click input[type=checkbox]":function(){this.trigger("change",this)},"focus input[type=checkbox]":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur input[type=checkbox]":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("input[type=checkbox]:focus")[0])return;e.trigger("blur",e)},0)}},getValue:function(){var e=[];return this.$("input[type=checkbox]:checked").each(function(){e.push(t(this).val())}),e},setValue:function(e){n.isArray(e)||(e=[e]),this.$("input[type=checkbox]").val(e)},focus:function(){if(this.hasFocus)return;this.$("input[type=checkbox]").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("input[type=checkbox]:focus").blur()},_arrayToHtml:function(e){var t=[],r=this;return n.each(e,function(e,i){var s="
  • ",o=!0;if(n.isObject(e))if(e.group){var u=r.id;r.id+="-"+r.groupNumber++,s='
    '+e.group+"",s+=r._arrayToHtml(e.options),s+="
    ",r.id=u,o=!1}else{var a=e.val||e.val===0?e.val:"";s+='',s+='"}else s+='',s+='";o&&(s+="
  • "),t.push(s)}),t.join("")}}),i.editors.Object=i.editors.Base.extend({hasNestedForm:!0,initialize:function(e){this.value={},i.editors.Base.prototype.initialize.call(this,e);if(!this.form)throw new Error('Missing required option "form"');if(!this.schema.subSchema)throw new Error("Missing required 'schema.subSchema' option for Object editor")},render:function(){var e=this.form.constructor;return this.nestedForm=new e({schema:this.schema.subSchema,data:this.value,idPrefix:this.id+"_",Field:e.NestedField}),this._observeFormEvents(),this.$el.html(this.nestedForm.render().el),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){return this.nestedForm?this.nestedForm.getValue():this.value},setValue:function(e){this.value=e,this.render()},focus:function(){if(this.hasFocus)return;this.nestedForm.focus()},blur:function(){if(!this.hasFocus)return;this.nestedForm.blur()},remove:function(){this.nestedForm.remove(),r.View.prototype.remove.call(this)},validate:function(){return this.nestedForm.validate()},_observeFormEvents:function(){if(!this.nestedForm)return;this.nestedForm.on("all",function(){var e=n.toArray(arguments);e[1]=this,this.trigger.apply(this,e)},this)}}),i.editors.NestedModel=i.editors.Object.extend({initialize:function(e){i.editors.Base.prototype.initialize.call(this,e);if(!this.form)throw new Error('Missing required option "form"');if(!e.schema.model)throw new Error('Missing required "schema.model" option for NestedModel editor')},render:function(){var e=this.form.constructor,t=this.value||{},n=this.key,r=this.schema.model,i=t.constructor===r?t:new r(t);return this.nestedForm=new e({model:i,idPrefix:this.id+"_",fieldTemplate:"nestedField"}),this._observeFormEvents(),this.$el.html(this.nestedForm.render().el),this.hasFocus&&this.trigger("blur",this),this},commit:function(){var e=this.nestedForm.commit();return e?(this.$el.addClass("error"),e):i.editors.Object.prototype.commit.call(this)}}),i.editors.Date=i.editors.Base.extend({events:{"change select":function(){this.updateHidden(),this.trigger("change",this)},"focus select":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur select":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("select:focus")[0])return;e.trigger("blur",e)},0)}},initialize:function(e){e=e||{},i.editors.Base.prototype.initialize.call(this,e);var t=i.editors.Date,r=new Date;this.options=n.extend({monthNames:t.monthNames,showMonthNames:t.showMonthNames},e),this.schema=n.extend({yearStart:r.getFullYear()-100,yearEnd:r.getFullYear()},e.schema||{}),this.value&&!n.isDate(this.value)&&(this.value=new Date(this.value));if(!this.value){var s=new Date;s.setSeconds(0),s.setMilliseconds(0),this.value=s}this.template=e.template||this.constructor.template},render:function(){var e=this.options,r=this.schema,i=n.map(n.range(1,32),function(e){return'"}),s=n.map(n.range(0,12),function(t){var n=e.showMonthNames?e.monthNames[t]:t+1;return'"}),o=r.yearStart'+e+""}),a=t(t.trim(this.template({dates:i.join(""),months:s.join(""),years:u.join("")})));return this.$date=a.find('[data-type="date"]'),this.$month=a.find('[data-type="month"]'),this.$year=a.find('[data-type="year"]'),this.$hidden=t(''),a.append(this.$hidden),this.setValue(this.value),this.setElement(a),this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){var e=this.$year.val(),t=this.$month.val(),n=this.$date.val();return!e||!t||!n?null:new Date(e,t,n)},setValue:function(e){this.$date.val(e.getDate()),this.$month.val(e.getMonth()),this.$year.val(e.getFullYear()),this.updateHidden()},focus:function(){if(this.hasFocus)return;this.$("select").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("select:focus").blur()},updateHidden:function(){var e=this.getValue();n.isDate(e)&&(e=e.toISOString()),this.$hidden.val(e)}},{template:n.template('
    ',null,i.templateSettings),showMonthNames:!0,monthNames:["January","February","March","April","May","June","July","August","September","October","November","December"]}),i.editors.DateTime=i.editors.Base.extend({events:{"change select":function(){this.updateHidden(),this.trigger("change",this)},"focus select":function(){if(this.hasFocus)return;this.trigger("focus",this)},"blur select":function(){if(!this.hasFocus)return;var e=this;setTimeout(function(){if(e.$("select:focus")[0])return;e.trigger("blur",e)},0)}},initialize:function(e){e=e||{},i.editors.Base.prototype.initialize.call(this,e),this.options=n.extend({DateEditor:i.editors.DateTime.DateEditor},e),this.schema=n.extend({minsInterval:15},e.schema||{}),this.dateEditor=new this.options.DateEditor(e),this.value=this.dateEditor.value,this.template=e.template||this.constructor.template},render:function(){function e(e){return e<10?"0"+e:e}var r=this.schema,i=n.map(n.range(0,24),function(t){return'"}),s=n.map(n.range(0,60,r.minsInterval),function(t){return'"}),o=t(t.trim(this.template({hours:i.join(),mins:s.join()})));return o.find("[data-date]").append(this.dateEditor.render().el),this.$hour=o.find('select[data-type="hour"]'),this.$min=o.find('select[data-type="min"]'),this.$hidden=o.find('input[type="hidden"]'),this.setValue(this.value),this.setElement(o),this.$el.attr("id",this.id),this.$el.attr("name",this.getName()),this.hasFocus&&this.trigger("blur",this),this},getValue:function(){var e=this.dateEditor.getValue(),t=this.$hour.val(),n=this.$min.val();return!e||!t||!n?null:(e.setHours(t),e.setMinutes(n),e)},setValue:function(e){n.isDate(e)||(e=new Date(e)),this.dateEditor.setValue(e),this.$hour.val(e.getHours()),this.$min.val(e.getMinutes()),this.updateHidden()},focus:function(){if(this.hasFocus)return;this.$("select").first().focus()},blur:function(){if(!this.hasFocus)return;this.$("select:focus").blur()},updateHidden:function(){var e=this.getValue();n.isDate(e)&&(e=e.toISOString()),this.$hidden.val(e)},remove:function(){this.dateEditor.remove(),i.editors.Base.prototype.remove.call(this)}},{template:n.template('
    :
    ',null,i.templateSettings),DateEditor:i.editors.Date}),i.VERSION="0.14.0",r.Form=i,typeof exports!="undefined"&&(exports=i)})(window||global||this) \ No newline at end of file diff --git a/src/fieldset.js b/src/fieldset.js index bc2bcb1a..52b63236 100644 --- a/src/fieldset.js +++ b/src/fieldset.js @@ -1,4 +1,3 @@ - //================================================================================================== //FIELDSET //================================================================================================== @@ -87,10 +86,19 @@ Form.Fieldset = Backbone.View.extend({ selection = $container.attr('data-fields'); if (_.isUndefined(selection)) return; - - _.each(fields, function(field) { - $container.append(field.render().el); + + //Work out which fields to include (_compact used to trim empty string selection) + var keys = _.compact((selection == '*') ? self.selectedFields || _.keys(fields) : selection.split(',')); + + //include all keys if not specified + if (!keys.length) + keys = _.keys(fields); + + //Add them + _.each(keys, function(key) { + $container.append(fields[key].render().el); }); + }); this.setElement($fieldset);