Pflichtenhefte: Statischen JavaScript-Code nach js/requirement_spec.js verschieben
[kivitendo-erp.git] / js / requirement_spec.js
1 /* Functions used for the requirement specs */
2
3 // -----------------------------------------------------------------------------
4 // ------------------------------ basic settings -------------------------------
5 // -----------------------------------------------------------------------------
6
7 function basic_settings_customer_changed(customer_ctrl, value_ctrl) {
8   $.get(
9     'controller.pl?action=Customer/get_hourly_rate',
10     { id: $(customer_ctrl).val() },
11     function(data) { if (data.hourly_rate_formatted) $(value_ctrl).val(data.hourly_rate_formatted); }
12   );
13 }
14
15 // -----------------------------------------------------------------------------
16 // ------------------------------ the tree itself ------------------------------
17 // -----------------------------------------------------------------------------
18
19 function requirement_spec_tree_check_move(data) {
20   var dragged_type = data.o.data('type');
21   var dropped_type = data.r.data('type');
22
23   // console.debug("dragged " + dragged_type + " dropped " + dropped_type + " dir " + data.p);
24
25   if ((dragged_type == "sections") || (dragged_type == "text-blocks-front") || (dragged_type == "text-blocks-back"))
26     return false;
27
28   if (dragged_type == "text-block") {
29     if ((dropped_type == "text-blocks-front") || (dropped_type == "text-blocks-back"))
30       return (data.p == "inside") || (data.p == "last");
31     if (dropped_type == "text-block")
32       return (data.p == "before") || (data.p == "after");
33
34     return false;
35   }
36
37   if (dragged_type == "section") {
38     if (dropped_type == "sections")
39       return (data.p == "inside") || (data.p == "last");
40     if (dropped_type == "section")
41       return (data.p == "before") || (data.p == "after");
42
43     return false;
44   }
45
46   // dragged_type == (sub) function blocks
47   if ((dropped_type == "text-block") || (dropped_type == "text-blocks-front") || (dropped_type == "text-blocks-back"))
48     return false;
49
50   var dropped_depth = dropped_type == "sections" ? 0 : dropped_type == "section" ? 1 : data.r.parent().parent().data('type') != "function-block" ? 2 : 3;
51   if ((data.p == "inside") || (data.p == "last"))
52     dropped_depth++;
53
54   var dragged_depth = 1 + data.o.children('ul').size();
55
56   // console.debug("dropped_depth " + dropped_depth + " dragged_depth " + dragged_depth);
57
58   return (2 <= dropped_depth) && ((dragged_depth + dropped_depth) <= 4);
59 }
60
61 function requirement_spec_tree_node_moved(event) {
62   // console.debug("node moved");
63   var move_obj   = $.jstree._reference('#tree')._get_move();
64   var dragged    = move_obj.o;
65   var dropped    = move_obj.r;
66   var controller = dragged.data("type") == "text-block" ? "RequirementSpecTextBlock" : "RequirementSpecItem";
67   var data       = {
68     action:               controller + "/dragged_and_dropped",
69     requirement_spec_id:  $('#requirement_spec_id').val(),
70     id:                   dragged.data("id"),
71     dropped_id:           dropped.data("id"),
72     dropped_type:         dropped.data("type"),
73     position:             move_obj.p,
74     current_content_type: $('#current_content_type').val(),
75     current_content_id:   $('#current_content_id').val()
76   };
77   // console.debug("controller: " + controller);
78   // console.debug(data);
79
80   $.post("controller.pl", data, eval_json_result);
81
82   return true;
83 }
84
85 function requirement_spec_tree_node_clicked(event) {
86   var node = $.jstree._reference('#tree')._get_node(event.target);
87   var type = node ? node.data('type') : undefined;
88
89   if (!type)
90     return;
91
92   var url = 'controller.pl?action='
93   $.get('controller.pl', {
94     action:               (/^text-block/.test(type) ? 'RequirementSpecTextBlock' : 'RequirementSpecItem') + '/ajax_list.js',
95     requirement_spec_id:  $('#requirement_spec_id').val(),
96     current_content_type: $('#current_content_type').val(),
97     current_content_id:   $('#current_content_id').val(),
98     clicked_type:         type,
99     clicked_id:           node.data('id')
100   }, eval_json_result);
101 }
102
103 // -------------------------------------------------------------------------
104 // ------------------------------ text blocks ------------------------------
105 // -------------------------------------------------------------------------
106
107 function find_text_block_id(clicked_elt) {
108   // console.log("id: " + $(clicked_elt).attr('id'));
109   var id = $(clicked_elt).attr('id');
110   if (/^text-block-\d+$/.test(id)) {
111     // console.log("find_text_block_id: case 1: " + id.substr(11));
112     return id.substr(11) * 1;
113   }
114
115   id = $(clicked_elt).closest("[id*=text-block-]").attr('id')
116   if (/^text-block-\d+$/.test(id)) {
117     // console.log("find_text_block_id: case 2: " + id.substr(11));
118     return id.substr(11) * 1;
119   }
120
121   id = $(clicked_elt).closest("[id*=tb-]").attr('id')
122   if (/^tb-\d+$/.test(id)) {
123     // console.log("find_text_block_id: case 3: " + id.substr(3));
124     return id.substr(3) * 1;
125   }
126
127   // console.log("find_text_block_id: case undef");
128   return undefined;
129 }
130
131 function find_text_block_output_position(clicked_elt) {
132   var output_position = $(clicked_elt).closest('#text-block-list-container').find('#text_block_output_position').val();
133   if (output_position)
134     return output_position;
135
136   var type = $(clicked_elt).closest('#tb-back,#tb-front').data('type');
137   if (/^text-blocks-(front|back)/.test(type))
138     return type == "text-blocks-front" ? 0 : 1;
139
140   return undefined;
141 }
142
143 function disable_edit_text_block_commands(key, opt) {
144   return find_text_block_id(opt.$trigger) == undefined;
145 }
146
147 function standard_text_block_ajax_call(key, opt, other_data) {
148   var data = {
149     action:               "RequirementSpecTextBlock/ajax_" + key,
150     requirement_spec_id:  $('#requirement_spec_id').val(),
151     id:                   find_text_block_id(opt.$trigger),
152     output_position:      find_text_block_output_position(opt.$trigger),
153     current_content_type: $('#current_content_type').val(),
154     current_content_id:   $('#current_content_id').val()
155   };
156
157   $.post("controller.pl", $.extend(data, other_data || {}), eval_json_result);
158
159   return true;
160 }
161
162 function submit_edit_text_block_form(id_base) {
163   var id   = $('#' + id_base + '_id').val();
164   var url  = "controller.pl?" + $('#' + id_base + '_form').serialize();
165   var data = {
166     action:      'RequirementSpecTextBlock/ajax_' + (id ? 'update' : 'create'),
167     id:          id,
168     form_prefix: id_base
169   };
170   $.post(url, data, eval_json_result);
171   return true;
172 }
173
174 function cancel_edit_text_block_form(id_base) {
175   var id = $('#' + id_base + '_id').val();
176   $('#' + id_base + '_form').remove();
177   if (id)
178     $('#text-block-' + id).show();
179 }
180
181 function ask_delete_text_block(key, opt) {
182   if (confirm(kivi.t8("Are you sure?")))
183     standard_text_block_ajax_call(key, opt);
184   return true;
185 }
186
187 // --------------------------------------------------------------------------------
188 // ------------------------------ sections and items ------------------------------
189 // --------------------------------------------------------------------------------
190
191 function find_item_id(clicked_elt) {
192   // console.log("clicked id: " + $(clicked_elt).attr('id'));
193   var id     = $(clicked_elt).attr('id');
194   var result = /^(function-block|function-block-content|sub-function-block|sub-function-block-content|section|section-header)-(\d+)$/.exec(id);
195   if (result) {
196     // console.log("find_item_id: case 1: " + result[2]);
197     return result[2];
198   }
199
200   id = $(clicked_elt).closest("[id*=fb-]").attr('id')
201   if (/^fb-\d+$/.test(id)) {
202     // console.log("find_item_id: case 2: " + id.substr(3));
203     return id.substr(3) * 1;
204   }
205
206   // console.log("find_item_id: case undef");
207   return undefined;
208 }
209
210 function standard_item_ajax_call(key, opt, other_data) {
211   var data = {
212     action:               "RequirementSpecItem/ajax_" + key,
213     requirement_spec_id:  $('#requirement_spec_id').val(),
214     id:                   find_item_id(opt.$trigger),
215     current_content_type: $('#current_content_type').val(),
216     current_content_id:   $('#current_content_id').val()
217   };
218
219   // console.log("I would normally POST the following now:");
220   // console.log(data);
221   $.post("controller.pl", $.extend(data, other_data || {}), eval_json_result);
222
223   return true;
224 }
225
226 function disable_edit_item_commands(key, opt) {
227   return find_item_id(opt.$trigger) == undefined;
228 }
229
230 function disable_add_function_block_command(key, opt) {
231   if (find_item_id(opt.$trigger))
232     return false;
233   return opt.$trigger.attr('id') != "section-list-empty";
234 }
235
236 function submit_edit_item_form(id_base) {
237   var id   = $('#' + id_base + '_id').val();
238   var url  = "controller.pl?" + $('#' + id_base + '_form').serialize();
239   var data = {
240     action:      'RequirementSpecItem/ajax_' + (id ? 'update' : 'create'),
241     id:          id,
242     form_prefix: id_base
243   };
244   $.post(url, data, eval_json_result);
245   return true;
246 }
247
248 function cancel_edit_item_form(form_id_base, options) {
249   $('#' + form_id_base + '_form').remove();
250   if (!options)
251     return;
252   if (options.to_show)
253     $(options.to_show).show();
254   if (options.to_hide_if_empty && (1 == $(options.to_hide_if_empty).children().size()))
255     $(options.to_hide_if_empty).hide();
256 }
257
258 function ask_delete_item(key, opt) {
259   if (confirm(kivi.t8("Are you sure?")))
260     standard_item_ajax_call(key, opt);
261   return true;
262 }
263
264 function create_requirement_spec_context_menus() {
265   $.contextMenu({
266     selector: '.text-block-context-menu',
267     items: {
268         add:    { name: kivi.t8('Add text block'),    icon: "add",    callback: standard_text_block_ajax_call }
269       , edit:   { name: kivi.t8('Edit text block'),   icon: "edit",   callback: standard_text_block_ajax_call, disabled: disable_edit_text_block_commands }
270       , delete: { name: kivi.t8('Delete text block'), icon: "delete", callback: ask_delete_text_block,         disabled: disable_edit_text_block_commands }
271       , sep1:   "---------"
272       , flag:   { name: kivi.t8('Toggle marker'),     icon: "flag",   callback: standard_text_block_ajax_call, disabled: disable_edit_text_block_commands }
273       , sep2:   "---------"
274       , copy:   { name: kivi.t8('Copy'),              icon: "copy",                                            disabled: function() { return true; } }
275       , paste:  { name: kivi.t8('Paste'),             icon: "paste",                                           disabled: function() { return true; } }
276       // , copy:   { name: kivi.t8('Copy'),              icon: "copy",                                            disabled: disable_edit_text_block_commands }
277       // , paste:  { name: kivi.t8('Paste'),             icon: "paste",                                           disabled: disable_edit_text_block_commands }
278     }
279   });
280
281   $.contextMenu({
282     selector: '.section-context-menu',
283     items: {
284         add_section:        { name: kivi.t8('Add section'),        icon: "add",    callback: standard_item_ajax_call }
285       , add_function_block: { name: kivi.t8('Add function block'), icon: "add",    callback: standard_item_ajax_call, disabled: disable_add_function_block_command }
286       , sep1:               "---------"
287       , edit:               { name: kivi.t8('Edit'),               icon: "edit",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
288       , delete:             { name: kivi.t8('Delete'),             icon: "delete", callback: ask_delete_item,         disabled: disable_edit_item_commands }
289       , sep2:               "---------"
290       , flag:               { name: kivi.t8('Toggle marker'),      icon: "flag",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
291       , sep3:               "---------"
292       , copy:               { name: kivi.t8('Copy'),               icon: "copy",                                      disabled: function() { return true; } }
293       , paste:              { name: kivi.t8('Paste'),              icon: "paste",                                     disabled: function() { return true; } }
294       // , copy:               { name: kivi.t8('Copy'),               icon: "copy",                                      disabled: disable_edit_item_commands }
295       // , paste:              { name: kivi.t8('Paste'),              icon: "paste",                                     disabled: disable_edit_item_commands }
296     }
297   });
298
299   $.contextMenu({
300     selector: '.function-block-context-menu,.sub-function-block-context-menu',
301     items: {
302         add_function_block:     { name: kivi.t8('Add function block'),     icon: "add",    callback: standard_item_ajax_call }
303       , add_sub_function_block: { name: kivi.t8('Add sub function block'), icon: "add",    callback: standard_item_ajax_call }
304       , sep1:                   "---------"
305       , edit:                   { name: kivi.t8('Edit'),                   icon: "edit",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
306       , delete:                 { name: kivi.t8('Delete'),                 icon: "delete", callback: ask_delete_item,         disabled: disable_edit_item_commands }
307       , sep2:                   "---------"
308       , flag:                   { name: kivi.t8('Toggle marker'),          icon: "flag",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
309       , sep3:                   "---------"
310       , copy:                   { name: kivi.t8('Copy'),                   icon: "copy",                                      disabled: function() { return true; } }
311       , paste:                  { name: kivi.t8('Paste'),                  icon: "paste",                                     disabled: function() { return true; } }
312       // , copy:                   { name: kivi.t8('Copy'),                   icon: "copy",                                      disabled: disable_edit_item_commands }
313       // , paste:                  { name: kivi.t8('Paste'),                  icon: "paste",                                     disabled: disable_edit_item_commands }
314     }
315   });
316 }