2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// coffeescript's for in loop
|
|
|
|
var __indexOf = [].indexOf || function(item) {
|
|
|
|
for (var i = 0, l = this.length; i < l; i++) {
|
|
|
|
if (i in this && this[i] === item) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
angular.module('forms').directive('fieldDirective', ['$http', '$compile', '$rootScope', '$templateCache',
|
|
|
|
function($http, $compile, $rootScope, $templateCache) {
|
2016-04-29 03:45:00 +00:00
|
|
|
|
2016-04-20 05:31:36 +00:00
|
|
|
var getTemplateUrl = function(fieldType) {
|
|
|
|
var type = fieldType;
|
2015-08-25 21:33:52 +00:00
|
|
|
var templateUrl = 'modules/forms/views/directiveViews/field/';
|
2015-06-29 22:51:29 +00:00
|
|
|
var supported_fields = [
|
|
|
|
'textfield',
|
|
|
|
'textarea',
|
|
|
|
'date',
|
|
|
|
'dropdown',
|
|
|
|
'hidden',
|
|
|
|
'password',
|
2015-07-27 18:11:43 +00:00
|
|
|
'radio',
|
2015-07-29 17:45:24 +00:00
|
|
|
'legal',
|
2015-07-30 00:22:53 +00:00
|
|
|
'statement',
|
|
|
|
'rating',
|
2015-07-30 01:27:46 +00:00
|
|
|
'yes_no',
|
2015-08-06 05:52:59 +00:00
|
|
|
'number',
|
2015-07-30 01:27:46 +00:00
|
|
|
'natural'
|
2015-06-29 22:51:29 +00:00
|
|
|
];
|
2016-06-01 01:06:45 +00:00
|
|
|
if (__indexOf.call(supported_fields, type) >= 0) {
|
2015-10-30 18:40:02 +00:00
|
|
|
templateUrl = templateUrl+type+'.html';
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
2016-04-28 23:47:50 +00:00
|
|
|
return $templateCache.get('../public/'+templateUrl);
|
2015-06-29 22:51:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
template: '<div>{{field.title}}</div>',
|
|
|
|
restrict: 'E',
|
2016-06-01 01:06:45 +00:00
|
|
|
scope: {
|
2015-07-06 04:29:05 +00:00
|
|
|
field: '=',
|
2015-10-30 21:40:22 +00:00
|
|
|
required: '&',
|
2015-11-06 19:00:45 +00:00
|
|
|
design: '=',
|
|
|
|
index: '=',
|
2016-04-29 03:45:00 +00:00
|
|
|
forms: '='
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
2015-11-06 17:25:30 +00:00
|
|
|
link: function(scope, element) {
|
2016-04-29 04:57:04 +00:00
|
|
|
|
|
|
|
$rootScope.chooseDefaultOption = scope.chooseDefaultOption = function(type) {
|
|
|
|
if(type === 'yes_no'){
|
|
|
|
scope.field.fieldValue = 'true';
|
|
|
|
}else if(type === 'rating'){
|
|
|
|
scope.field.fieldValue = 0;
|
|
|
|
}else if(scope.field.fieldType === 'radio'){
|
|
|
|
console.log(scope.field);
|
|
|
|
scope.field.fieldValue = scope.field.fieldOptions[0].option_value;
|
|
|
|
console.log(scope.field.fieldValue);
|
|
|
|
}else if(type === 'legal'){
|
|
|
|
scope.field.fieldValue = 'true';
|
|
|
|
$rootScope.nextField();
|
|
|
|
}
|
|
|
|
};
|
2016-04-29 03:45:00 +00:00
|
|
|
|
2015-11-06 17:25:30 +00:00
|
|
|
scope.setActiveField = $rootScope.setActiveField;
|
2016-04-29 03:45:00 +00:00
|
|
|
|
2015-11-06 17:25:30 +00:00
|
|
|
//Set format only if field is a date
|
|
|
|
if(scope.field.fieldType === 'date'){
|
|
|
|
scope.dateOptions = {
|
|
|
|
changeYear: true,
|
|
|
|
changeMonth: true,
|
|
|
|
altFormat: 'mm/dd/yyyy',
|
2016-04-29 03:45:00 +00:00
|
|
|
yearRange: '1900:-0',
|
|
|
|
defaultDate: 0
|
2015-11-06 17:25:30 +00:00
|
|
|
};
|
|
|
|
}
|
2016-04-29 03:45:00 +00:00
|
|
|
|
2016-04-22 19:36:34 +00:00
|
|
|
var fieldType = scope.field.fieldType;
|
2016-04-29 03:45:00 +00:00
|
|
|
|
2016-04-22 01:22:52 +00:00
|
|
|
if(scope.field.fieldType === 'number' || scope.field.fieldType === 'textfield' || scope.field.fieldType === 'email' || scope.field.fieldType === 'link'){
|
2016-04-20 05:31:36 +00:00
|
|
|
switch(scope.field.fieldType){
|
|
|
|
case 'textfield':
|
|
|
|
scope.field.input_type = 'text';
|
|
|
|
break;
|
|
|
|
case 'email':
|
|
|
|
scope.field.input_type = 'email';
|
|
|
|
scope.field.placeholder = 'joesmith@example.com';
|
|
|
|
break;
|
2016-04-22 01:22:52 +00:00
|
|
|
case 'number':
|
2016-04-29 04:57:04 +00:00
|
|
|
scope.field.input_type = 'text';
|
2016-05-28 14:49:37 +00:00
|
|
|
scope.field.validateRegex = /^-?\d+$/;
|
2016-04-22 01:22:52 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-20 05:31:36 +00:00
|
|
|
scope.field.input_type = 'url';
|
|
|
|
scope.field.placeholder = 'http://example.com';
|
|
|
|
break;
|
|
|
|
}
|
2016-04-22 19:36:34 +00:00
|
|
|
fieldType = 'textfield';
|
2016-04-20 05:31:36 +00:00
|
|
|
}
|
2016-04-22 19:36:34 +00:00
|
|
|
var template = getTemplateUrl(fieldType);
|
2016-04-20 05:31:36 +00:00
|
|
|
element.html(template).show();
|
2016-06-01 01:06:45 +00:00
|
|
|
var output = $compile(element.contents())(scope);
|
2016-04-29 03:45:00 +00:00
|
|
|
}
|
2015-06-29 22:51:29 +00:00
|
|
|
};
|
2016-04-20 05:31:36 +00:00
|
|
|
}]);
|