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

89 lines
2.6 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-11-23 19:19:02 +00:00
angular.module('forms').directive('fieldDirective', ['$http', '$compile', '$rootScope', '$templateCache',
function($http, $compile, $rootScope, $templateCache) {
2015-06-29 22:51:29 +00:00
2016-04-20 05:31:36 +00:00
var getTemplateUrl = function(fieldType) {
var type = fieldType;
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',
'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-04-20 05:31:36 +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
}
2015-11-23 19:19:02 +00:00
return $templateCache.get('../public/'+templateUrl);
2015-06-29 22:51:29 +00:00
};
return {
template: '<div>{{field.title}}</div>',
restrict: 'E',
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: '=',
2015-06-29 22:51:29 +00:00
},
2015-11-06 17:25:30 +00:00
link: function(scope, element) {
scope.setActiveField = $rootScope.setActiveField;
//Set format only if field is a date
if(scope.field.fieldType === 'date'){
scope.dateOptions = {
changeYear: true,
changeMonth: true,
altFormat: 'mm/dd/yyyy',
yearRange: '1900:-0',
defaultDate: 0,
};
}
2016-04-22 19:36:34 +00:00
var fieldType = scope.field.fieldType;
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':
scope.field.input_type = 'number';
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();
$compile(element.contents())(scope);
2015-11-06 17:25:30 +00:00
},
2015-06-29 22:51:29 +00:00
};
2016-04-20 05:31:36 +00:00
}]);