tellform/public/modules/forms/directives/field.client.directive.js

81 lines
2.2 KiB
JavaScript
Raw Normal View History

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-08-06 05:52:59 +00:00
angular.module('forms').directive('fieldDirective', ['$http', '$compile', '$rootScope',
function($http, $compile, $rootScope) {
2015-06-29 22:51:29 +00:00
var getTemplateUrl = function(field) {
var type = field.fieldType;
var templateUrl = './modules/forms/views/directiveViews/field/';
var supported_fields = [
'textfield',
'email',
'textarea',
'checkbox',
'date',
'dropdown',
'hidden',
'password',
2015-07-27 18:11:43 +00:00
'radio',
2015-07-29 17:45:24 +00:00
'legal',
'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
];
if (__indexOf.call(supported_fields, type) >= 0) {
return templateUrl += type + '.html';
}
};
var linker = function(scope, element) {
2015-08-06 05:52:59 +00:00
scope.setActiveField = $rootScope.setActiveField;
//Set format only if field is a date
if(scope.field.fieldType === 'date'){
scope.dateOptions = {
changeYear: true,
changeMonth: true,
2015-08-07 21:02:44 +00:00
altFormat: 'mm/dd/yyyy',
yearRange: '1900:-0',
defaultDate: 0,
};
}
2015-07-30 01:27:46 +00:00
//Set only if we have a natural lang processing field
else if(scope.field.fieldType === 'natural'){
2015-08-04 21:06:16 +00:00
scope.field.fieldMatchValue = '';
2015-07-30 01:27:46 +00:00
//Fires when field is changed
2015-08-04 21:06:16 +00:00
scope.$watch('scope.field', function(newField, oldField) {
2015-07-30 01:27:46 +00:00
});
}
2015-07-06 04:29:05 +00:00
2015-06-29 22:51:29 +00:00
// GET template content from path
var templateUrl = getTemplateUrl(scope.field);
$http.get(templateUrl).success(function(data) {
element.html(data);
$compile(element.contents())(scope);
});
};
return {
template: '<div>{{field.title}}</div>',
restrict: 'E',
scope: {
2015-07-06 04:29:05 +00:00
field: '=',
required: '&'
2015-06-29 22:51:29 +00:00
},
link: linker
};
2015-08-06 05:52:59 +00:00
}]);