Skip to content

Instantly share code, notes, and snippets.

@fesor
Created January 29, 2015 16:51
Show Gist options
  • Select an option

  • Save fesor/727eac459ee18ebffbd5 to your computer and use it in GitHub Desktop.

Select an option

Save fesor/727eac459ee18ebffbd5 to your computer and use it in GitHub Desktop.
function generateForm (name, id) {
return {
id: id,
name: name,
updatedAt: new Date(),
sections: {
section_1: {
id: sectionID(1),
name: 'Section 1',
description: 'Some section description',
controls: [
control('Applicant'),
control('Property name/number'),
control('City/country'),
control('Village/town'),
control('Street', 'textarea'),
control('Telephone number'),
control('Mobile number'),
control('Postcode')
]
},
section_2: {
id: sectionID(2),
name: 'Section 2',
description: 'Some section description',
controls: [
control('New Connections', 'fieldset', {
controls: [
control('Facility Name'),
control('Number', 'number')
]
}),
control('Do you require a meter filling?', 'form', {
name: 'require_meter_filling',
controls: [
control('Property name/number'),
control('City/country'),
control('Village/town')
]
})
]
},
section_3: {
id: sectionID(3),
name: 'Section 3',
description: 'Some section description',
controls: [
control('Applicant'),
control('Property name/number'),
control('City/country'),
control('Village/town', 'text', {
help: 'Some help box to describe proposes of given info'
}),
control('Street', 'textarea'),
control('Some select', 'select', {
choice: [
'Some value 1',
'Some value 2',
'Some value 3',
'Some value 4'
]
}),
control('Mobile number'),
control('Postcode')
]
},
section_4: {
id: sectionID(4),
name: 'Section 4',
description: 'Some section description',
controls: [
control('Applicant'),
control('Property name/number'),
control('City/country'),
control('Village/town'),
control('Street', 'textarea'),
control('Telephone number'),
control('Mobile number'),
control('Postcode')
]
},
section_5: {
id: sectionID(5),
name: 'Section 5',
description: 'Some section description',
controls: [
control('Applicant'),
control('Property name/number'),
control('City/country'),
control('Village/town'),
control('Street', 'textarea'),
control('Telephone number'),
control('Mobile number'),
control('Postcode')
]
},
section_6: {
id: sectionID(6),
name: 'Section 6',
description: 'Some section description',
controls: [
control('Applicant'),
control('Property name/number'),
control('City/country'),
control('Village/town'),
control('Street', 'textarea'),
control('Telephone number'),
control('Mobile number'),
control('Postcode')
]
}
}
};
function sectionID(id) {
return 'section_' + id;
}
}
function control (label, type, additional) {
var result = {
name: nameFromLabel(label),
label: label,
type: type || 'text'
};
angular.forEach(additional, function (val, name) {
result[name] = val;
});
return result;
}
function inArray (needly, haystack) {
return -1 !== haystack.indexOf(needly);
}
function nameFromLabel(label) {
return label
.toLowerCase()
.replace(/[^a-z\s]/g, '_')
.replace(/\s/g, '_')
.replace(/[_]+/g, '_')
.replace(/^[_]+/, '')
.replace(/[_]+$/, '')
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment