tellform/app/models/form_field.server.model.js

208 lines
4 KiB
JavaScript
Raw Normal View History

2015-06-29 22:51:29 +00:00
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
2016-06-01 01:06:45 +00:00
util = require('util'),
2015-10-06 20:14:38 +00:00
mUtilities = require('mongoose-utilities'),
2015-09-18 16:32:17 +00:00
_ = require('lodash'),
2015-09-10 22:06:28 +00:00
Schema = mongoose.Schema;
2015-06-29 22:51:29 +00:00
2015-10-06 20:14:38 +00:00
var FieldOptionSchema = new Schema({
option_id: {
2016-04-29 06:16:17 +00:00
type: Number
2015-10-06 20:14:38 +00:00
},
option_title: {
2016-04-29 06:16:17 +00:00
type: String
2015-10-06 20:14:38 +00:00
},
option_value: {
type: String,
2016-04-29 06:16:17 +00:00
trim: true
}
2015-10-06 20:14:38 +00:00
});
2016-06-01 01:06:45 +00:00
var RatingFieldSchema = new Schema({
steps: {
2016-06-01 01:16:24 +00:00
type: Number,
min: 1,
max: 10
2016-06-01 01:06:45 +00:00
},
shape: {
type: String,
enum: [
'Heart',
'Star',
'thumbs-up',
'thumbs-down',
'Circle',
'Square',
'Check Circle',
'Smile Outlined',
'Hourglass',
'bell',
'Paper Plane',
'Comment',
'Trash'
]
},
validShapes: {
type: [String]
}
});
2015-06-29 22:51:29 +00:00
/**
2015-10-06 20:14:38 +00:00
* FormField Schema
2015-06-29 22:51:29 +00:00
*/
2016-06-01 01:06:45 +00:00
function BaseFieldSchema(){
Schema.apply(this, arguments);
2015-09-03 18:21:56 +00:00
2016-06-01 01:06:45 +00:00
this.add({
title: {
type: String,
trim: true,
required: 'Field Title cannot be blank'
},
description: {
type: String,
default: ''
},
2015-09-03 18:21:56 +00:00
2016-06-01 01:06:45 +00:00
logicJump: {
type: Schema.Types.ObjectId,
ref: 'LogicJump'
},
2015-07-27 18:11:43 +00:00
2016-06-01 01:06:45 +00:00
ratingOptions: {
type: RatingFieldSchema,
required: false,
default: {}
},
fieldOptions: [FieldOptionSchema],
required: {
type: Boolean,
default: true
},
disabled: {
type: Boolean,
default: false
},
deletePreserved: {
type: Boolean,
default: false
},
validFieldTypes: {
type: [String]
},
fieldType: {
type: String,
required: true,
enum: [
'textfield',
'date',
'email',
'link',
'legal',
'url',
'textarea',
'statement',
'welcome',
'thankyou',
'file',
'dropdown',
'scale',
'rating',
'radio',
'checkbox',
'hidden',
'yes_no',
'natural',
'number'
]
},
fieldValue: Schema.Types.Mixed
});
this.plugin(mUtilities.timestamp, {
createdPath: 'created',
modifiedPath: 'lastModified',
useVirtual: false
});
this.pre('save', function (next) {
this.validFieldTypes = mongoose.model('Field').schema.path('fieldType').enumValues;
if(this.fieldType === 'rating' && this.ratingOptions.validShapes.length === 0){
this.ratingOptions.validShapes = mongoose.model('RatingOptions').schema.path('shape').enumValues;
}
next();
});
}
util.inherits(BaseFieldSchema, Schema);
var FormFieldSchema = new BaseFieldSchema();
FormFieldSchema.pre('validate', function(next) {
var error = new mongoose.Error.ValidationError(this);
//If field is rating check that it has ratingOptions
if(this.fieldType !== 'rating'){
if(this.ratingOptions && this.ratingOptions.steps && this.ratingOptions.shape){
error.errors.ratingOptions = new mongoose.Error.ValidatorError({path: 'ratingOptions', message: 'ratingOptions is only allowed for type \'rating\' fields.', type: 'notvalid', value: this.ratingOptions});
return(next(error));
}
}else{
//Setting default values for ratingOptions
if(!this.ratingOptions.steps){
this.ratingOptions.steps = 10;
}
if(!this.ratingOptions.shape){
this.ratingOptions.shape = 'Star';
}
2015-06-29 22:51:29 +00:00
2016-06-01 01:06:45 +00:00
//Checking that the fieldValue is between 0 and ratingOptions.steps
if(this.fieldValue+0 > this.ratingOptions.steps || this.fieldValue+0 < 0){
this.fieldValue = 1;
}
}
//If field is multiple choice check that it has ratingOptions
if(this.fieldType !== 'dropdown' && this.fieldType !== 'radio' && this.fieldType === 'checkbox'){
if(!this.fieldOptions || this.fieldOptions.length !== 0){
error.errors.ratingOptions = new mongoose.Error.ValidatorError({path:'fieldOptions', message: 'fieldOptions are only allowed for type dropdown, checkbox or radio fields.', type: 'notvalid', value: this.ratingOptions});
return(next(error));
}
}
if(error)
return next();
2015-10-06 20:14:38 +00:00
});
2015-08-18 21:44:36 +00:00
2016-06-01 01:06:45 +00:00
//Validate and convert dropdown value to fieldValue
FormFieldSchema.pre('validate', function (next) {
return next();
/*
if(this.fieldType === 'dropdown' && this.fieldOptions.length > 0){
for(var i = 0; i<this.fieldOptions.length; i++){
}
}*/
2015-10-30 20:14:48 +00:00
});
2015-10-30 18:40:02 +00:00
2015-08-18 21:44:36 +00:00
2016-06-01 01:06:45 +00:00
var Field = mongoose.model('Field', FormFieldSchema);
var RatingOptions = mongoose.model('RatingOptions', RatingFieldSchema);
2015-09-18 16:32:17 +00:00
2015-10-06 20:14:38 +00:00
module.exports = FormFieldSchema;
2015-09-18 16:32:17 +00:00