Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All editors have readonly mode #452

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 115 additions & 39 deletions distribution.amd/backbone-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,14 +1115,28 @@ Form.Editor = Form.editors.Base = Backbone.View.extend({
_.extend(this, _.pick(options, 'key', 'form'));

var schema = this.schema = options.schema || {};

this.setElAttributes();

this.validators = options.validators || schema.validators;
},

//Main attributes
setElAttributes: function() {
this.$el.attr('id', this.id);
this.$el.attr('name', this.getName());
if (schema.editorClass) this.$el.addClass(schema.editorClass);
if (schema.editorAttrs) this.$el.attr(schema.editorAttrs);

//Main attributes
if (this.schema.editorClass) this.$el.addClass(this.schema.editorClass);
if (this.schema.editorAttrs) this.$el.attr(this.schema.editorAttrs);
},

render: function() {
if (this.schema.readonly && this.readonlyTemplate) {
var $el = $($.trim(this.readonlyTemplate()));
this.setElement($el);
this.setElAttributes();
}
return this;
},

/**
Expand Down Expand Up @@ -1304,26 +1318,24 @@ Form.editors.Text = Form.Editor.extend({
}
},

initialize: function(options) {
Form.editors.Base.prototype.initialize.call(this, options);

var schema = this.schema;
setElAttributes: function() {
Form.editors.Base.prototype.setElAttributes.call(this);

//Allow customising text type (email, phone etc.) for HTML5 browsers
var type = 'text';

if (schema && schema.editorAttrs && schema.editorAttrs.type) type = schema.editorAttrs.type;
if (schema && schema.dataType) type = schema.dataType;
if (this.schema && this.schema.editorAttrs && this.schema.editorAttrs.type) type = this.schema.editorAttrs.type;
if (this.schema && this.schema.dataType) type = this.schema.dataType;

this.$el.attr('type', type);
this.$el.prop('type', type);
},

/**
* Adds the editor to the DOM
*/
render: function() {
Form.editors.Base.prototype.render.call(this);
this.setValue(this.value);

return this;
},

Expand Down Expand Up @@ -1368,7 +1380,9 @@ Form.editors.Text = Form.Editor.extend({

select: function() {
this.$el.select();
}
},

readonlyTemplate: _.template('<input readonly></input>', null, Form.templateSettings)

});

Expand All @@ -1384,7 +1398,13 @@ Form.editors.TextArea = Form.editors.Text.extend({
*/
initialize: function(options) {
Form.editors.Base.prototype.initialize.call(this, options);
}
},

setElAttributes: function() {
Form.editors.Base.prototype.setElAttributes.call(this);
},

readonlyTemplate: _.template('<textarea readonly></textarea>', null, Form.templateSettings)

});

Expand All @@ -1393,10 +1413,10 @@ Form.editors.TextArea = Form.editors.Text.extend({
*/
Form.editors.Password = Form.editors.Text.extend({

initialize: function(options) {
Form.editors.Text.prototype.initialize.call(this, options);
setElAttributes: function() {
Form.editors.Text.prototype.setElAttributes.call(this);

this.$el.attr('type', 'password');
this.$el.prop('type', 'password');
}

});
Expand All @@ -1415,14 +1435,12 @@ Form.editors.Number = Form.editors.Text.extend({
'change': 'onKeyPress'
}),

initialize: function(options) {
Form.editors.Text.prototype.initialize.call(this, options);

var schema = this.schema;
setElAttributes: function() {
Form.editors.Text.prototype.setElAttributes.call(this);

this.$el.attr('type', 'number');
this.$el.prop('type', 'number');

if (!schema || !schema.editorAttrs || !schema.editorAttrs.step) {
if (!this.schema || !this.schema.editorAttrs || !this.schema.editorAttrs.step) {
// provide a default for `step` attr,
// but don't overwrite if already specified
this.$el.attr('step', 'any');
Expand Down Expand Up @@ -1493,10 +1511,10 @@ Form.editors.Hidden = Form.editors.Text.extend({

noField: true,

initialize: function(options) {
Form.editors.Text.prototype.initialize.call(this, options);

this.$el.attr('type', 'hidden');
setElAttributes: function() {
Form.editors.Text.prototype.setElAttributes.call(this);
this.$el.prop('type', 'hidden');
},

focus: function() {
Expand Down Expand Up @@ -1532,18 +1550,17 @@ Form.editors.Checkbox = Form.editors.Base.extend({
}
},

initialize: function(options) {
Form.editors.Base.prototype.initialize.call(this, options);

this.$el.attr('type', 'checkbox');
setElAttributes: function() {
Form.editors.Base.prototype.setElAttributes.call(this);
this.$el.prop('type', 'checkbox');
},

/**
* Adds the editor to the DOM
*/
render: function() {
Form.editors.Base.prototype.render.call(this);
this.setValue(this.value);

return this;
},

Expand All @@ -1569,7 +1586,9 @@ Form.editors.Checkbox = Form.editors.Base.extend({
if (!this.hasFocus) return;

this.$el.blur();
}
},

readonlyTemplate: _.template('<input disabled></input>', null, Form.templateSettings)

});

Expand Down Expand Up @@ -1614,6 +1633,10 @@ Form.editors.Select = Form.editors.Base.extend({
},

render: function() {
if (this.schema.readonly && this.readonlyTemplate) {
var $el = $($.trim(this.readonlyTemplate()));
this.setElement($el);
}
this.setOptions(this.schema.options);

return this;
Expand Down Expand Up @@ -1740,6 +1763,9 @@ Form.editors.Select = Form.editors.Base.extend({
this.$el.blur();
},

readonlyTemplate: _.template('<select disabled></select>', null, Form.templateSettings),


/**
* Transforms a collection into HTML ready to use in the renderOptions method
* @param {Backbone.Collection}
Expand Down Expand Up @@ -1842,12 +1868,21 @@ Form.editors.Radio = Form.editors.Select.extend({
}
},

render: function() {
this.setOptions(this.schema.options);

return this;
},

/**
* Returns the template. Override for custom templates
*
* @return {Function} Compiled template
*/
getTemplate: function() {
if(this.schema.readonly && this.readonlyTemplate)
return this.readonlyTemplate;

return this.schema.template || this.constructor.template;
},

Expand Down Expand Up @@ -1909,7 +1944,16 @@ Form.editors.Radio = Form.editors.Select.extend({
});

return template({ items: items });
}
},

readonlyTemplate: _.template('\
<% _.each(items, function(item) { %>\
<li>\
<input type="radio" disabled name="<%= item.name %>" value="<%- item.value %>" id="<%= item.id %>" />\
<label for="<%= item.id %>"><% if (item.labelHTML){ %><%= item.labelHTML %><% }else{ %><%- item.label %><% } %></label>\
</li>\
<% }); %>\
', null, Form.templateSettings)

}, {

Expand Down Expand Up @@ -1983,6 +2027,12 @@ Form.editors.Checkboxes = Form.editors.Select.extend({
this.$('input[type=checkbox]:focus').blur();
},

render: function() {
this.setOptions(this.schema.options);

return this;
},

/**
* Create the checkbox list HTML
* @param {Array} Options as a simple array e.g. ['option1', 'option2']
Expand All @@ -1992,6 +2042,7 @@ Form.editors.Checkboxes = Form.editors.Select.extend({
_arrayToHtml: function (array) {
var html = $();
var self = this;
var readonlyAttr = this.schema.readonly ? 'disabled' : '';

_.each(array, function(option, index) {
var itemHtml = $('<li>');
Expand All @@ -2005,7 +2056,7 @@ Form.editors.Checkboxes = Form.editors.Select.extend({
close = false;
}else{
var val = (option.val || option.val === 0) ? option.val : '';
itemHtml.append( $('<input type="checkbox" name="'+self.getName()+'" id="'+self.id+'-'+index+'" />').val(val) );
itemHtml.append( $('<input type="checkbox" name="'+self.getName()+'" id="'+self.id+'-'+index+'" ' + readonlyAttr + ' />').val(val) );
if (option.labelHTML){
itemHtml.append( $('<label for="'+self.id+'-'+index+'">').html(option.labelHTML) );
}
Expand All @@ -2015,7 +2066,7 @@ Form.editors.Checkboxes = Form.editors.Select.extend({
}
}
else {
itemHtml.append( $('<input type="checkbox" name="'+self.getName()+'" id="'+self.id+'-'+index+'" />').val(option) );
itemHtml.append( $('<input type="checkbox" name="'+self.getName()+'" id="'+self.id+'-'+index+'" ' + readonlyAttr + ' />').val(option) );
itemHtml.append( $('<label for="'+self.id+'-'+index+'">').text(option) );
}
html = html.add(itemHtml);
Expand Down Expand Up @@ -2254,7 +2305,11 @@ Form.editors.Date = Form.editors.Base.extend({
}

//Template
this.template = options.template || this.constructor.template;
if (this.schema.readonly && this.readonlyTemplate)
this.template = this.readonlyTemplate;

else
this.template = options.template || this.constructor.template;
},

render: function() {
Expand Down Expand Up @@ -2357,7 +2412,15 @@ Form.editors.Date = Form.editors.Base.extend({
if (_.isDate(val)) val = val.toISOString();

this.$hidden.val(val);
}
},

readonlyTemplate: _.template('\
<div>\
<select disabled data-type="date"><%= dates %></select>\
<select disabled data-type="month"><%= months %></select>\
<select disabled data-type="year"><%= years %></select>\
</div>\
', null, Form.templateSettings)

}, {
//STATICS
Expand Down Expand Up @@ -2425,7 +2488,11 @@ Form.editors.DateTime = Form.editors.Base.extend({
this.value = this.dateEditor.value;

//Template
this.template = options.template || this.constructor.template;
if (this.schema.readonly && this.readonlyTemplate)
this.template = this.readonlyTemplate;

else
this.template = options.template || this.constructor.template;
},

render: function() {
Expand Down Expand Up @@ -2534,7 +2601,16 @@ Form.editors.DateTime = Form.editors.Base.extend({
this.dateEditor.remove();

Form.editors.Base.prototype.remove.call(this);
}
},

readonlyTemplate: _.template('\
<div class="bbf-datetime">\
<div class="bbf-date-container" data-date></div>\
<select disabled data-type="hour"><%= hours %></select>\
:\
<select disabled data-type="min"><%= mins %></select>\
</div>\
', null, Form.templateSettings)

}, {
//STATICS
Expand Down
2 changes: 1 addition & 1 deletion distribution.amd/backbone-forms.min.js

Large diffs are not rendered by default.

31 changes: 27 additions & 4 deletions distribution.amd/editors/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ define(['jquery', 'underscore', 'backbone', 'backbone-forms'], function($, _, Ba
var schema = this.schema;
if (!schema) throw new Error("Missing required option 'schema'");

this.template = options.template || this.constructor.template;
if (schema.readonly && this.readonlyTemplate)
this.template = this.readonlyTemplate;

else
this.template = options.template || this.constructor.template;

//Determine the editor to use
this.Editor = (function() {
Expand Down Expand Up @@ -246,7 +250,14 @@ define(['jquery', 'underscore', 'backbone', 'backbone-forms'], function($, _, Ba
};

return fieldError;
}
},

readonlyTemplate: _.template('\
<div>\
<div data-items></div>\
<button type="button" disabled data-action="add">Add</button>\
</div>\
', null, Form.templateSettings)
}, {

//STATICS
Expand Down Expand Up @@ -290,9 +301,14 @@ define(['jquery', 'underscore', 'backbone', 'backbone-forms'], function($, _, Ba
this.value = options.value;
this.Editor = options.Editor || Form.editors.Text;
this.key = options.key;
this.template = options.template || this.schema.itemTemplate || this.constructor.template;
this.errorClassName = options.errorClassName || this.constructor.errorClassName;
this.form = options.form;

if (this.schema.readonly && this.readonlyTemplate)
this.template = this.readonlyTemplate;

else
this.template = options.template || this.schema.itemTemplate || this.constructor.template;
},

render: function() {
Expand Down Expand Up @@ -382,7 +398,14 @@ define(['jquery', 'underscore', 'backbone', 'backbone-forms'], function($, _, Ba
clearError: function() {
this.$el.removeClass(this.errorClassName);
this.$el.attr('title', null);
}
},

readonlyTemplate: _.template('\
<div>\
<span data-editor></span>\
<button type="button" disabled data-action="remove">&times;</button>\
</div>\
', null, Form.templateSettings)
}, {

//STATICS
Expand Down
Loading