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' ) ,
2016-08-23 21:45:59 +00:00
Schema = mongoose . Schema ,
LogicJumpSchema = require ( './logic_jump.server.model' ) ;
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-10-06 20:24:02 +00:00
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 ( {
2016-06-05 03:13:42 +00:00
isSubmission : {
type : Boolean ,
default : false
} ,
submissionId : {
type : Schema . Types . ObjectId
} ,
2016-06-01 01:06:45 +00:00
title : {
type : String ,
trim : true ,
required : 'Field Title cannot be blank'
} ,
description : {
type : String ,
default : ''
} ,
2015-09-03 18:21:56 +00:00
2016-08-23 21:45:59 +00:00
logicJump : LogicJumpSchema ,
2015-07-27 18:11:43 +00:00
2017-03-07 00:38:37 +00:00
ratingOptions : RatingFieldSchema ,
2016-06-01 01:06:45 +00:00
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' ,
2016-08-17 19:04:42 +00:00
'stripe' ,
2016-06-01 01:06:45 +00:00
'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 ;
}
2016-06-05 03:13:42 +00:00
2016-06-01 01:06:45 +00:00
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 } ) ;
2016-08-23 21:45:59 +00:00
console . error ( error ) ;
2016-06-01 01:06:45 +00:00
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 ;
}
}
2016-06-04 07:00:31 +00:00
//If field is multiple choice check that it has field
if ( this . fieldType !== 'dropdown' && this . fieldType !== 'radio' && this . fieldType !== 'checkbox' ) {
2016-08-23 21:45:59 +00:00
if ( this . fieldOptions && this . fieldOptions . length > 0 ) {
2016-06-01 01:06:45 +00:00
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 } ) ;
2016-08-23 21:45:59 +00:00
console . error ( error ) ;
2016-06-01 01:06:45 +00:00
return ( next ( error ) ) ;
}
}
2016-06-05 03:13:42 +00:00
return next ( ) ;
} ) ;
2016-08-23 21:45:59 +00:00
//LogicJump Save
2016-06-05 03:13:42 +00:00
FormFieldSchema . pre ( 'save' , function ( next ) {
2016-11-14 20:07:36 +00:00
if ( this . logicJump && this . logicJump . fieldA ) {
if ( this . logicJump . jumpTo === '' ) delete this . logicJump . jumpTo ;
2016-08-23 21:45:59 +00:00
}
next ( ) ;
} ) ;
2016-06-05 03:13:42 +00:00
2016-08-23 21:45:59 +00:00
//Submission fieldValue correction
FormFieldSchema . pre ( 'save' , function ( next ) {
2016-06-07 00:37:09 +00:00
if ( this . fieldType === 'dropdown' && this . isSubmission ) {
2016-06-05 03:13:42 +00:00
this . fieldValue = this . fieldValue . option _value ;
}
2016-06-01 01:06:45 +00:00
return next ( ) ;
2015-10-06 20:14:38 +00:00
} ) ;
2015-08-18 21:44:36 +00:00
2016-06-05 03:13:42 +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