blob: 5c5b48a6d7d7486a397917953e83e794d2314684 [file] [log] [blame]
Matteo Scandolo80056232017-07-10 16:24:41 -07001import './field.scss';
Matteo Scandoloee655a12016-12-19 15:38:43 -08002import {IXosConfigHelpersService} from '../services/helpers/config.helpers';
3import {IXosFormHelpersService} from '../form/form-helpers';
4import * as _ from 'lodash';
5
6class FieldCtrl {
7 static $inject = ['$attrs', '$scope', 'ConfigHelpers', 'XosFormHelpers'];
8 // $inject = ['$onInit'];
9
10 public field: any;
11 public name: string;
12 public ngModel: any;
13 public getType = this.XosFormHelpers._getFieldFormat;
14 public formatLabel = this.ConfigHelpers.toLabel;
15
16 constructor(
17 private $attrs: ng.IAttributes,
18 private $scope: ng.IScope,
19 private ConfigHelpers: IXosConfigHelpersService,
20 private XosFormHelpers: IXosFormHelpersService
21 ) {
22
23 }
24
25 public isEmptyObject = (o: any) => o ? Object.keys(o).length === 0 : true;
26
27 $onInit() {
28 if (!this.name) {
29 throw new Error('[xosField] Please provide a field name');
30 }
31 if (!this.field) {
32 throw new Error('[xosField] Please provide a field definition');
33 }
34 if (!this.field.type) {
35 throw new Error('[xosField] Please provide a type in the field definition');
36 }
37 if (!this.$attrs['ngModel']) {
38 throw new Error('[xosField] Please provide an ng-model');
39 }
40
41
42 if (this.field.type === 'array') {
43 this.$scope.$watch(() => this.ngModel.length, () => {
44 this.field.availableOptions = _.difference(this.field.options, this.ngModel);
45 });
46 }
47
48 }
49}
50
51export const xosField: angular.IComponentOptions = {
52 template: require('./field.html'),
53 controllerAs: 'vm',
54 controller: FieldCtrl,
55 bindings: {
56 ngModel: '=',
57 name: '=',
58 field: '='
59 }
60};