blob: e18ab10eaae602a7c6b3836c76666982412b8016 [file] [log] [blame]
Matteo Scandolo6bc31bf2016-08-29 10:17:31 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 9/29/16.
7 */
8
9(function () {
10 'use strict';
11 angular.module('xos.mcord-slicing')
12 .service('FormHandler', function(LabelFormatter, XosFormHelpers){
13 const self = this;
14 const t = d3.transition()
15 .duration(500);
16
17 // draw a form nearby the node
18 this.drawForm = (node, linkGroup, formGroup) => {
19
20 // create an svg to draw a line from the node to the form
21 const line = linkGroup
22 .append('line')
23 .attr({
24 class: 'form-line',
25 id: `form-line-${node.type}-${node.id}`,
26 x1: node.x + 10,
27 y1: node.y,
28 x2: node.x + 10,
29 y2: node.y + 40,
30 opacity: 0,
31 });
32
33 line.transition(t)
34 .attr({
35 opacity: 1
36 });
37
38 // form container
39 const form = formGroup
40 .append('div')
41 .attr({
42 class: 'element-form',
43 id: `form-${node.type}-${node.id}`,
44 })
45 .style({
46 opacity: 0
47 });
48
49 const formEl = form
50 .append('form');
51
52 // cicle trough props (to be defined from rest)
53 this.addFormfields(node, formEl);
54
55 const buttonRow = formEl
56 .append('div')
57 .attr({
58 class: 'row'
59 });
60
61 buttonRow
62 .append('div')
63 .attr({
64 class: 'col-xs-6'
65 })
66 .append('a')
67 .attr({
68 class: 'btn btn-danger',
69 'data-parent-node-type': node.type,
70 'data-parent-node-id': node.id
71 })
72 .text('Close')
73 .on('click', function(){
74 self.removeForm(
75 d3.select(this).attr('data-parent-node-type'),
76 d3.select(this).attr('data-parent-node-id'),
77 linkGroup,
78 formGroup
79 );
80 });
81
82 buttonRow
83 .append('div')
84 .attr({
85 class: 'col-xs-6'
86 })
87 .append('button')
88 .attr({
89 type: 'button',
90 class: 'btn btn-success'
91 })
92 .text('Save')
93 .on('click', function(){
94 $(`#form-${node.type}-${node.id} input`).each(function(){
95 let input = $(this); // This is the jquery object of the input, do what you will
96 let val = input.val();
97 let name = input.attr('name');
98 console.log(name, val);
99 });
100 });
101
102 form.transition(t)
103 .style({
104 opacity: 1,
105 top: `${node.y + 95}px`,
106 left: `${node.x}px`
107 });
108 };
109
110 this.removeForm = (nodeType, nodeId, linkGroup, formGroup) => {
111 this.removeFormByParentNode({type: nodeType, id: nodeId}, linkGroup, formGroup);
112 };
113
114 // remove a form starting form his parent node
115 this.removeFormByParentNode = (node, linkGroup, formGroup) => {
116 // remove form
117 formGroup.selectAll(`#form-${node.type}-${node.id}`)
118 .transition(t)
119 .style({
120 opacity: 0
121 })
122 .remove();
123 // remove link
124 linkGroup.selectAll(`#form-line-${node.type}-${node.id}`)
125 .transition(t)
126 .attr({
127 opacity: 0
128 })
129 .remove();
130 };
131
132 this.getFieldValue = (val, fieldType) => {
133 if(fieldType === 'date'){
134 val = new Date(val);
135 val = `${val.getFullYear()}-${('0' + val.getMonth() + 1).slice(-2)}-${('0' + val.getDate()).slice(-2)}`
136 }
137 return val || '';
138 };
139
140 this.addFormField = (fieldName, fieldValue, formEl) => {
141 let fieldType = XosFormHelpers._getFieldFormat(fieldValue);
142 formEl
143 .append('div')
144 .attr({
145 class: 'row'
146 })
147 .append('div')
148 .attr({
149 class: 'col-xs-12'
150 })
151 .append('label')
152 .text(fieldName ? LabelFormatter.format(fieldName ) : 'test')
153 .append('input')
154 .attr({
155 type: fieldType,
156 name: fieldName,
157 value: this.getFieldValue(fieldValue, fieldType),
158 class: 'form-control'
159 });
160 };
161
162 this.addFormfields = (node, formEl) => {
163
164 this.addFormField('name', node.name, formEl);
165 // tmp check
166 if(!node.model){
167 return this.addFormField(null, null, formEl);
168 }
169
170 // create a list of fields to be printed
171 const fields = Object.keys(node.model);
172 _.forEach(fields, f => {
173 this.addFormField(f, node.model[f], formEl);
174 });
175 };
176 });
177})();
178