Skip to content

Commit

Permalink
fixed less.
Browse files Browse the repository at this point in the history
  • Loading branch information
jspears committed Apr 2, 2012
1 parent ce2407c commit 233e446
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 624 deletions.
123 changes: 64 additions & 59 deletions lib/display-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,56 @@ function find(field, args) {

}
var global_options = {
read_only:['_id','id', 'id_', 'modified_by','created_by','created_at','modified_at'],
builtin_editors: ['Checkbox',
'Checkboxes',
'Date',
'DateTime',
'Hidden',
'List',
'NestedModel',
'Number',
'Object',
'Password',
'Radio',
'Select',
'Text',
'TextArea'],
read_only:['_id', 'id', 'id_', 'modified_by', 'created_by', 'created_at', 'modified_at'],
builtin_editors:['Checkbox',
'Checkboxes',
'Date',
'DateTime',
'Hidden',
'List',
'NestedModel',
'Number',
'Object',
'Password',
'Radio',
'Select',
'Text',
'TextArea'],
plugin_editors:['MultiEditor']
}
function App(options) {
this.options = _u.extend({}, global_options, options);
var args = Array.prototype.slice.call(arguments, 1);
var args = this._args = Array.prototype.slice.call(arguments, 1);
['title', 'version', 'description'].forEach(easyget(args), this);
this._modelPaths = {};
args.forEach(function (k, v) {

_u.each(k.modelPaths, function onAppArgs(vv, kk) {
var key = vv.modelName || kk;
(this._modelPaths[key] || (this._modelPaths[key] = [])).push(vv);
}, this);

}, this);
args.forEach(this.add, this);
this.__defineGetter__('models', function () {
return _u.map(this.modelPaths, function (k, v) {
return _u.map(this.modelPaths, function (k, v) {
return k
});
});

this.__defineGetter__('modelPaths', function () {
var ret = {};
_u.each(this._modelPaths, function onModelPaths(v, k) {
var model = ret[k] = new Model(k, v);
var model = ret[k] = new Model(k, v);
}, this);
return ret;
});
this.__defineGetter__('editors', function(){
this.__defineGetter__('editors', function () {
return this.options.builtin_editors.concat(this.options.plugin_editors);
});
}
App.prototype.add = function (App) {
this._args.shift(App);
this._args.forEach(function (k, v) {
_u.each(k.modelPaths, function onAppArgs(vv, kk) {
var key = vv.modelName || kk;
(this._modelPaths[key] || (this._modelPaths[key] = [])).push(vv);
}, this);

}, this);
}
App.prototype.modelFor = function (model) {
var Model = model;
if (typeof model == 'string')
Expand Down Expand Up @@ -93,55 +96,57 @@ function Model(modelName, args) {
return ret;
});
this.__defineGetter__('title', function onTitle() {
var title = find('title',args) || inflection.titleize(inflection.humanize(modelName));
var title = find('title', args) || inflection.titleize(inflection.humanize(modelName));
return title;
});
this.__defineGetter__('plural', function onTitle() {
var plural = find('plural',args) || inflection.titleize(inflection.pluralize(inflection.humanize(modelName)));
var plural = find('plural', args) || inflection.titleize(inflection.pluralize(inflection.humanize(modelName)));
return plural;
});
this.__defineGetter__('labelAttr', function onLabelAttrGet() {
var labelAttr = find('labelAttr',args);
var labelAttr = find('labelAttr', args);
if (labelAttr) return labelAttr;
var labels = ['label','name','title'];
for(var i=0,l=labels.length;i<l;i++){
if (this.fields.indexOf(labels[i]) > -1)
return labels[i];
var labels = ['label', 'name', 'title'];
for (var i = 0, l = labels.length; i < l; i++) {
if (this.fields.indexOf(labels[i]) > -1)
return labels[i];
}
});
this.__defineGetter__('fields', function onFieldsGet() {
var fields = find('fields',args);
var fields = find('fields', args);
if (!fields)
fields = Object.keys(this.paths);
return fields;
});
this.__defineGetter__('edit_fields', function onEditFieldsGet() {
var fields = find( 'edit_fields', args);
if (!fields){
var fields = find('edit_fields', args);
if (!fields) {
fields = this.fields.filter(filterRO, this.app);
}
return fields;
});
this.__defineGetter__('list_fields', function onListFieldsGet() {
var fields = find('list_fields',args);
var fields = find('list_fields', args);
if (!fields)
return this.fields.filter(function(v,k){
return this.fields.filter(function (v, k) {
return !(v == 'id' || v == '_id' || v == 'id_')
});
return fields;
});
this.__defineGetter__('fieldsets', function onFieldSetsGet(){
var fieldsets = find('fieldsets', args);
this.__defineGetter__('fieldsets', function onFieldSetsGet() {
var fieldsets = find('fieldsets', args);
if (fieldsets)
return fieldsets;
return [{legend:this.title,fields:this.edit_fields}]
return fieldsets;
return [
{legend:this.title, fields:this.edit_fields}
]
});
}
function contains(arr, value){
function contains(arr, value) {
return arr && arr.indexOf(value) > -1;
}
function filterRO(v,k){
return global_options.read_only.indexOf(v) < 0;
function filterRO(v, k) {
return global_options.read_only.indexOf(v) < 0;
}
Model.prototype.fieldsFor = function (list_type) {
if (!list_type)
Expand All @@ -158,24 +163,24 @@ Model.prototype.fieldsFor = function (list_type) {
Model.prototype.editorsFor = function onEditorsFor(fields, schema) {
schema = schema || this.schemaFor(fields);
var editors = {};
_u(fields).each(function(v,k){
_u(fields).each(function (v, k) {
var type;
if (k.indexOf('.') < 0)
type = schema[k].type;
type = schema[k].type;
else {
var split = k.split('.');
var obj = schema.subSchema;

while (split.length ) {
while (split.length) {
obj = obj[split.shift()];
if (obj.subSchema)
obj = obj.subSchema;
}
if (obj){
if (obj) {
type = obj.type;
}
}
if (type &! (global_options.builtin_editors.indexOf(type) > -1))
if (type & !(global_options.builtin_editors.indexOf(type) > -1))
editors[util.dasherize(type)] = true;


Expand All @@ -186,14 +191,14 @@ Model.prototype.schemaFor = function onCreateSchema(fields) {
var schema = {};
var f = [];
fields = this.fieldsets || this.edit_fields;
_u(fields).each(function(v,k){
if (v.fields){
f = f.concat(v.fields);
}else{
f.push(v);
}
})
_u(this.fieldsFor(f)).each(function (v, k) {
_u(fields).each(function (v, k) {
if (v.fields) {
f = f.concat(v.fields);
} else {
f.push(v);
}
})
_u(this.fieldsFor(f)).each(function (v, k) {
if (k.indexOf('.') < 0)
schema[k] = v;
else {
Expand Down
Loading

0 comments on commit 233e446

Please sign in to comment.