Afform Gui - Support cloning existing forms
[civicrm-core.git] / ext / afform / core / ang / af / afJoin.directive.js
1 (function(angular, $, _) {
2 // Example usage: <div af-join="Email" min="1" max="3" add-label="Add email" ><div join-email-default /></div>
3 angular.module('af')
4 .directive('afJoin', function() {
5 return {
6 restrict: 'A',
7 require: ['afJoin', '^^afFieldset', '?^^afRepeatItem'],
8 bindToController: {
9 entity: '@afJoin',
10 },
11 link: function($scope, $el, $attr, ctrls) {
12 var self = ctrls[0];
13 self.afFieldset = ctrls[1];
14 self.repeatItem = ctrls[2];
15 },
16 controller: function($scope) {
17 var self = this;
18 this.getEntityType = function() {
19 return this.entity;
20 };
21 this.getData = function() {
22 var data, fieldsetData;
23 if (self.repeatItem) {
24 data = self.repeatItem.item;
25 } else {
26 fieldsetData = self.afFieldset.getData();
27 if (!fieldsetData.length) {
28 fieldsetData.push({fields: {}, joins: {}});
29 }
30 data = fieldsetData[0];
31 }
32 if (!data.joins) {
33 data.joins = {};
34 }
35 if (!data.joins[self.entity]) {
36 data.joins[self.entity] = [];
37 }
38 return data.joins[self.entity];
39 };
40 this.getFieldData = function() {
41 var data = this.getData();
42 if (!data.length) {
43 data.push({});
44 }
45 return data[0];
46 };
47 }
48 };
49 });
50 })(angular, CRM.$, CRM._);