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 #501

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules/
/bower_components/
*.iml
*.ipr
*.iws
*.iws
20 changes: 17 additions & 3 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,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
13 changes: 7 additions & 6 deletions src/editors/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,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 @@ -59,6 +58,8 @@ Form.editors.Checkbox = Form.editors.Base.extend({
if (!this.hasFocus) return;

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

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

});
11 changes: 9 additions & 2 deletions src/editors/checkboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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 @@ -67,6 +73,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 @@ -80,7 +87,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 @@ -90,7 +97,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
16 changes: 14 additions & 2 deletions src/editors/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,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 @@ -169,7 +173,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
17 changes: 15 additions & 2 deletions src/editors/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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 @@ -155,7 +159,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
32 changes: 28 additions & 4 deletions src/editors/extra/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
addLabel: 'Add'
}, 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 @@ -251,7 +256,14 @@
};

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 @@ -295,9 +307,14 @@
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 @@ -391,7 +408,14 @@
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
8 changes: 4 additions & 4 deletions src/editors/hidden.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,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
10 changes: 4 additions & 6 deletions src/editors/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ Form.editors.Number = Form.editors.Text.extend({
'change': 'onKeyPress'
}),

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

var schema = this.schema;
this.$el.prop('type', 'number');

this.$el.attr('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
6 changes: 3 additions & 3 deletions src/editors/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*/
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');
}

});
20 changes: 19 additions & 1 deletion src/editors/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,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 @@ -97,7 +106,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
7 changes: 7 additions & 0 deletions src/editors/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 @@ -166,6 +170,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
18 changes: 9 additions & 9 deletions src/editors/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,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 @@ -95,6 +93,8 @@ Form.editors.Text = Form.Editor.extend({

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

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

});
Loading