Debugcode
[kivitendo-erp.git] / js / requirement_spec.js
1 /* Functions used for the requirement specs tree view */
2
3 // -----------------------------------------------------------------------------
4 // ------------------------------ the tree itself ------------------------------
5 // -----------------------------------------------------------------------------
6
7 function requirement_spec_tree_check_move(data) {
8   var dragged_type = data.o.data('type');
9   var dropped_type = data.r.data('type');
10
11   // console.debug("dragged " + dragged_type + " dropped " + dropped_type + " dir " + data.p);
12
13   if ((dragged_type == "sections") || (dragged_type == "text-blocks-front") || (dragged_type == "text-blocks-back"))
14     return false;
15
16   if (dragged_type == "text-block") {
17     if ((dropped_type == "text-blocks-front") || (dropped_type == "text-blocks-back"))
18       return (data.p == "inside") || (data.p == "last");
19     if (dropped_type == "text-block")
20       return (data.p == "before") || (data.p == "after");
21
22     return false;
23   }
24
25   if (dragged_type == "section") {
26     if (dropped_type == "sections")
27       return (data.p == "inside") || (data.p == "last");
28     if (dropped_type == "section")
29       return (data.p == "before") || (data.p == "after");
30
31     return false;
32   }
33
34   // dragged_type == (sub) function blocks
35   if ((dropped_type == "text-block") || (dropped_type == "text-blocks-front") || (dropped_type == "text-blocks-back"))
36     return false;
37
38   var dropped_depth = dropped_type == "sections" ? 0 : dropped_type == "section" ? 1 : data.r.parent().parent().data('type') != "function-block" ? 2 : 3;
39   if ((data.p == "inside") || (data.p == "last"))
40     dropped_depth++;
41
42   var dragged_depth = 1 + data.o.children('ul').size();
43
44   // console.debug("dropped_depth " + dropped_depth + " dragged_depth " + dragged_depth);
45
46   return (2 <= dropped_depth) && ((dragged_depth + dropped_depth) <= 4);
47 }
48
49 function requirement_spec_tree_node_moved(event) {
50   // console.debug("node moved");
51   var move_obj   = $.jstree._reference('#tree')._get_move();
52   var dragged    = move_obj.o;
53   var dropped    = move_obj.r;
54   var controller = dragged.data("type") == "text-block" ? "RequirementSpecTextBlock" : "RequirementSpecItem";
55   var data       = {
56     action:               controller + "/dragged_and_dropped",
57     requirement_spec_id:  $('#requirement_spec_id').val(),
58     id:                   dragged.data("id"),
59     dropped_id:           dropped.data("id"),
60     dropped_type:         dropped.data("type"),
61     position:             move_obj.p,
62     current_content_type: $('#current_content_type').val(),
63     current_content_id:   $('#current_content_id').val()
64   };
65   // console.debug("controller: " + controller);
66   // console.debug(data);
67
68   $.post("controller.pl", data, eval_json_result);
69
70   return true;
71 }
72
73 function requirement_spec_tree_node_clicked(event) {
74   var node = $.jstree._reference('#tree')._get_node(event.target);
75   var type = node ? node.data('type') : undefined;
76
77   if (!type)
78     return;
79
80   var url = 'controller.pl?action='
81   $.get('controller.pl', {
82     action:               (/^text-block/.test(type) ? 'RequirementSpecTextBlock' : 'RequirementSpecItem') + '/ajax_list.js',
83     requirement_spec_id:  $('#requirement_spec_id').val(),
84     current_content_type: $('#current_content_type').val(),
85     current_content_id:   $('#current_content_id').val(),
86     clicked_type:         type,
87     clicked_id:           node.data('id')
88   }, eval_json_result);
89 }
90
91 // -------------------------------------------------------------------------
92 // ------------------------------ text blocks ------------------------------
93 // -------------------------------------------------------------------------
94
95 function find_text_block_id(clicked_elt) {
96   // console.log("id: " + $(clicked_elt).attr('id'));
97   var id = $(clicked_elt).attr('id');
98   if (/^text-block-\d+$/.test(id)) {
99     // console.log("find_text_block_id: case 1: " + id.substr(11));
100     return id.substr(11) * 1;
101   }
102
103   id = $(clicked_elt).closest("[id*=text-block-]").attr('id')
104   if (/^text-block-\d+$/.test(id)) {
105     // console.log("find_text_block_id: case 2: " + id.substr(11));
106     return id.substr(11) * 1;
107   }
108
109   id = $(clicked_elt).closest("[id*=tb-]").attr('id')
110   if (/^tb-\d+$/.test(id)) {
111     // console.log("find_text_block_id: case 3: " + id.substr(3));
112     return id.substr(3) * 1;
113   }
114
115   // console.log("find_text_block_id: case undef");
116   return undefined;
117 }
118
119 function find_text_block_output_position(clicked_elt) {
120   var output_position = $(clicked_elt).closest('#text-block-list-container').find('#text_block_output_position').val();
121   if (output_position)
122     return output_position;
123
124   var type = $(clicked_elt).closest('#tb-back,#tb-front').data('type');
125   if (/^text-blocks-(front|back)/.test(type))
126     return type == "text-blocks-front" ? 0 : 1;
127
128   return undefined;
129 }
130
131 function disable_edit_text_block_commands(key, opt) {
132   return find_text_block_id(opt.$trigger) == undefined;
133 }
134
135 function standard_text_block_ajax_call(key, opt, other_data) {
136   var data = {
137     action:               "RequirementSpecTextBlock/ajax_" + key,
138     requirement_spec_id:  $('#requirement_spec_id').val(),
139     id:                   find_text_block_id(opt.$trigger),
140     output_position:      find_text_block_output_position(opt.$trigger),
141     current_content_type: $('#current_content_type').val(),
142     current_content_id:   $('#current_content_id').val()
143   };
144
145   $.post("controller.pl", $.extend(data, other_data || {}), eval_json_result);
146
147   return true;
148 }
149
150 function submit_edit_text_block_form(id_base) {
151   var id   = $('#' + id_base + '_id').val();
152   var url  = "controller.pl?" + $('#' + id_base + '_form').serialize();
153   var data = {
154     action:      'RequirementSpecTextBlock/ajax_' + (id ? 'update' : 'create'),
155     id:          id,
156     form_prefix: id_base
157   };
158   $.post(url, data, eval_json_result);
159   return true;
160 }
161
162 function cancel_edit_text_block_form(id_base) {
163   var id = $('#' + id_base + '_id').val();
164   $('#' + id_base + '_form').remove();
165   if (id)
166     $('#text-block-' + id).show();
167 }
168
169 // --------------------------------------------------------------------------------
170 // ------------------------------ sections and items ------------------------------
171 // --------------------------------------------------------------------------------
172
173 function find_item_id(clicked_elt) {
174   // console.log("clicked id: " + $(clicked_elt).attr('id'));
175   var id     = $(clicked_elt).attr('id');
176   var result = /^(function-block|function-block-content|sub-function-block|sub-function-block-content|section|section-header)-(\d+)$/.exec(id);
177   if (result) {
178     // console.log("find_item_id: case 1: " + result[2]);
179     return result[2];
180   }
181
182   id = $(clicked_elt).closest("[id*=fb-]").attr('id')
183   if (/^fb-\d+$/.test(id)) {
184     // console.log("find_item_id: case 2: " + id.substr(3));
185     return id.substr(3) * 1;
186   }
187
188   // console.log("find_item_id: case undef");
189   return undefined;
190 }
191
192 function standard_item_ajax_call(key, opt, other_data) {
193   var data = {
194     action:               "RequirementSpecItem/ajax_" + key,
195     requirement_spec_id:  $('#requirement_spec_id').val(),
196     id:                   find_item_id(opt.$trigger),
197     current_content_type: $('#current_content_type').val(),
198     current_content_id:   $('#current_content_id').val()
199   };
200
201   // console.log("I would normally POST the following now:");
202   // console.log(data);
203   $.post("controller.pl", $.extend(data, other_data || {}), eval_json_result);
204
205   return true;
206 }
207
208 function disable_edit_item_commands(key, opt) {
209   return find_item_id(opt.$trigger) == undefined;
210 }
211
212 function submit_edit_item_form(id_base) {
213   var id   = $('#' + id_base + '_id').val();
214   var url  = "controller.pl?" + $('#' + id_base + '_form').serialize();
215   var data = {
216     action:      'RequirementSpecItem/ajax_' + (id ? 'update' : 'create'),
217     id:          id,
218     form_prefix: id_base
219   };
220   $.post(url, data, eval_json_result);
221   return true;
222 }
223
224 function cancel_edit_item_form(form_id_base, hidden_id_base) {
225   var id = $('#' + form_id_base + '_id').val();
226   $('#' + form_id_base + '_form').remove();
227   if (id)
228     $('#' + hidden_id_base + '-' + id).show();
229 }