Pflichtenhefte: PDFs zu Arbeitskopie und Versionen erzeugen
[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 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 function ask_delete_text_block(key, opt) {
170   if (confirm(kivi.t8("Are you sure?")))
171     standard_text_block_ajax_call(key, opt);
172   return true;
173 }
174
175 // --------------------------------------------------------------------------------
176 // ------------------------------ sections and items ------------------------------
177 // --------------------------------------------------------------------------------
178
179 function find_item_id(clicked_elt) {
180   // console.log("clicked id: " + $(clicked_elt).attr('id'));
181   var id     = $(clicked_elt).attr('id');
182   var result = /^(function-block|function-block-content|sub-function-block|sub-function-block-content|section|section-header)-(\d+)$/.exec(id);
183   if (result) {
184     // console.log("find_item_id: case 1: " + result[2]);
185     return result[2];
186   }
187
188   id = $(clicked_elt).closest("[id*=fb-]").attr('id')
189   if (/^fb-\d+$/.test(id)) {
190     // console.log("find_item_id: case 2: " + id.substr(3));
191     return id.substr(3) * 1;
192   }
193
194   // console.log("find_item_id: case undef");
195   return undefined;
196 }
197
198 function standard_item_ajax_call(key, opt, other_data) {
199   var data = {
200     action:               "RequirementSpecItem/ajax_" + key,
201     requirement_spec_id:  $('#requirement_spec_id').val(),
202     id:                   find_item_id(opt.$trigger),
203     current_content_type: $('#current_content_type').val(),
204     current_content_id:   $('#current_content_id').val()
205   };
206
207   // console.log("I would normally POST the following now:");
208   // console.log(data);
209   $.post("controller.pl", $.extend(data, other_data || {}), eval_json_result);
210
211   return true;
212 }
213
214 function disable_edit_item_commands(key, opt) {
215   return find_item_id(opt.$trigger) == undefined;
216 }
217
218 function disable_add_function_block_command(key, opt) {
219   if (find_item_id(opt.$trigger))
220     return false;
221   return opt.$trigger.attr('id') != "section-list-empty";
222 }
223
224 function cancel_edit_item_form(form_id_base, options) {
225   $('#' + form_id_base + '_form').remove();
226   if (!options)
227     return;
228   if (options.to_show)
229     $(options.to_show).show();
230   if (options.to_hide_if_empty && (1 == $(options.to_hide_if_empty).children().size()))
231     $(options.to_hide_if_empty).hide();
232 }
233
234 function ask_delete_item(key, opt) {
235   if (confirm(kivi.t8("Are you sure?")))
236     standard_item_ajax_call(key, opt);
237   return true;
238 }
239
240 function handle_text_block_popup_menu_markings(opt, add) {
241   var id = find_text_block_id(opt.$trigger);
242   if (id)
243     $('#text-block-' + id).toggleClass('selected', add);
244   return true;
245 }
246
247 function requirement_spec_text_block_popup_menu_shown(opt) {
248   return handle_text_block_popup_menu_markings(opt, true);
249 }
250
251 function requirement_spec_text_block_popup_menu_hidden(opt) {
252   return handle_text_block_popup_menu_markings(opt, false);
253 }
254
255 function handle_item_popup_menu_markings(opt, add) {
256   var id = find_item_id(opt.$trigger);
257   if (id)
258     $('#section-' + id + ',#function-block-' + id + ',#sub-function-block-' + id).toggleClass('selected', add);
259   return true;
260 }
261
262 function requirement_spec_item_popup_menu_shown(opt) {
263   return handle_item_popup_menu_markings(opt, true);
264 }
265
266 function requirement_spec_item_popup_menu_hidden(opt) {
267   return handle_item_popup_menu_markings(opt, false);
268 }
269
270 // -------------------------------------------------------------------------
271 // -------------------------- time/cost estimate ---------------------------
272 // -------------------------------------------------------------------------
273
274 function standard_time_cost_estimate_ajax_call(key, opt) {
275   if ((key == 'cancel') && !confirm(kivi.t8('Do you really want to cancel?')))
276     return true;
277
278   var data = "action=RequirementSpec/ajax_" + key + "_time_and_cost_estimate&";
279
280   if (key == 'save')
281     data += $('#edit_time_cost_estimate_form').serialize()
282          +  '&' + $('#current_content_type').serialize()
283          +  '&' + $('#current_content_id').serialize();
284   else
285     data += 'id=' + encodeURIComponent($('#requirement_spec_id').val());
286
287   $.post("controller.pl", data, eval_json_result);
288
289   return true;
290 }
291
292 // -------------------------------------------------------------------------
293 // ---------------------------- general actions ----------------------------
294 // -------------------------------------------------------------------------
295
296 function create_reqspec_pdf(key, opt) {
297   var data = {
298     action: "RequirementSpec/create_pdf",
299     id:     $('#requirement_spec_id').val()
300   };
301   $.download("controller.pl", data);
302 }
303
304 function copy_reqspec(key, opt) {
305   window.location.href = "controller.pl?action=RequirementSpec/new&copy_source_id=" + encodeURIComponent($('#requirement_spec_id').val());
306   return true;
307 }
308
309 function delete_reqspec(key, opt) {
310   if (confirm(kivi.t8("Are you sure?")))
311     window.location.href = "controller.pl?action=RequirementSpec/destroy&id=" + encodeURIComponent($('#requirement_spec_id').val());
312   return true;
313 }
314
315 function disable_requirement_spec_commands(key, opt) {
316   if (key === "create_version")
317     return ($('#current_version_id').val() || '') == '' ? false : true;
318   return false;
319 }
320
321 // -------------------------------------------------------------------------
322 // -------------------------------- versions -------------------------------
323 // -------------------------------------------------------------------------
324
325 function find_versioned_copy_id(clicked_elt) {
326   var id = $(clicked_elt).find("[name=versioned_copy_id]");
327   return id ? id.val() : undefined;
328 }
329
330 function disable_versioned_copy_item_commands(key, opt) {
331   if (key === "revert_to_version")
332     return !find_versioned_copy_id(opt.$trigger);
333   return false;
334 }
335
336 function create_requirement_spec_version() {
337   open_jqm_window({ url:  'controller.pl',
338                     data: { action:              'RequirementSpecVersion/new',
339                             requirement_spec_id: $('#requirement_spec_id').val() },
340                     id:   'new_requirement_spec_version_window' });
341   return true;
342 }
343
344 function create_pdf_for_versioned_copy_ajax_call(key, opt) {
345   var data = {
346     action: "RequirementSpec/create_pdf",
347     id:     find_versioned_copy_id(opt.$trigger)
348   };
349   $.download("controller.pl", data);
350
351   return true;
352 }
353
354 function revert_to_versioned_copy_ajax_call(key, opt) {
355   if (!confirm(kivi.t8('Do you really want to revert to this version?')))
356     return true;
357
358   var data = {
359     action:            'RequirementSpec/revert_to',
360     versioned_copy_id: find_versioned_copy_id(opt.$trigger),
361     id:                $('#requirement_spec_id').val()
362   };
363
364   $.post("controller.pl", data, eval_json_result);
365
366   return true;
367 }
368
369 // -------------------------------------------------------------------------
370 // ----------------------------- context menus -----------------------------
371 // -------------------------------------------------------------------------
372
373 function create_requirement_spec_context_menus() {
374   var general_actions = {
375       sep98:           "---------"
376     , general_actions: { name: kivi.t8('Requirement spec actions'), className: 'context-menu-heading' }
377     // , sep99:           "---------"
378     , create_pdf:      { name: kivi.t8('Create PDF'),              icon: "pdf",    callback: create_reqspec_pdf }
379     , create_version:  { name: kivi.t8('Create new version'),      icon: "new",    callback: create_requirement_spec_version, disabled: disable_requirement_spec_commands }
380     , copy_reqspec:    { name: kivi.t8('Copy requirement spec'),   icon: "copy",   callback: copy_reqspec   }
381     , delete_reqspec:  { name: kivi.t8('Delete requirement spec'), icon: "delete", callback: delete_reqspec }
382   };
383
384   $.contextMenu({
385     selector: '#content',
386     items:    general_actions
387   });
388
389   var events = {
390     show: requirement_spec_text_block_popup_menu_shown,
391     hide: requirement_spec_text_block_popup_menu_hidden
392   };
393
394   $.contextMenu({
395     selector: '.text-block-context-menu',
396     events:   {
397         show: requirement_spec_text_block_popup_menu_shown
398       , hide: requirement_spec_text_block_popup_menu_hidden
399     },
400     items:    $.extend({
401         heading: { name: kivi.t8('Text block actions'),    className: 'context-menu-heading' }
402       , add:     { name: kivi.t8('Add text block'),        icon: "add",    callback: standard_text_block_ajax_call }
403       , edit:    { name: kivi.t8('Edit text block'),       icon: "edit",   callback: standard_text_block_ajax_call, disabled: disable_edit_text_block_commands }
404       , delete:  { name: kivi.t8('Delete text block'),     icon: "delete", callback: ask_delete_text_block,         disabled: disable_edit_text_block_commands }
405       , sep1:    "---------"
406       , flag:    { name: kivi.t8('Toggle marker'),         icon: "flag",   callback: standard_text_block_ajax_call, disabled: disable_edit_text_block_commands }
407       , sep2:    "---------"
408       , copy:    { name: kivi.t8('Copy'),                  icon: "copy",   callback: standard_text_block_ajax_call, disabled: disable_edit_text_block_commands }
409       , paste:   { name: kivi.t8('Paste'),                 icon: "paste",  callback: standard_text_block_ajax_call  }
410     }, general_actions)
411   });
412
413   events = {
414     show: requirement_spec_item_popup_menu_shown,
415     hide: requirement_spec_item_popup_menu_hidden
416   };
417
418   $.contextMenu({
419     selector: '.section-context-menu',
420     events:   events,
421     items:    $.extend({
422         heading:            { name: kivi.t8('Section/Function block actions'), className: 'context-menu-heading' }
423       , add_section:        { name: kivi.t8('Add section'),        icon: "add",    callback: standard_item_ajax_call }
424       , add_function_block: { name: kivi.t8('Add function block'), icon: "add",    callback: standard_item_ajax_call, disabled: disable_add_function_block_command }
425       , sep1:               "---------"
426       , edit:               { name: kivi.t8('Edit'),               icon: "edit",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
427       , delete:             { name: kivi.t8('Delete'),             icon: "delete", callback: ask_delete_item,         disabled: disable_edit_item_commands }
428       , sep2:               "---------"
429       , flag:               { name: kivi.t8('Toggle marker'),      icon: "flag",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
430       , sep3:               "---------"
431       , copy:               { name: kivi.t8('Copy'),               icon: "copy",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
432       , paste:              { name: kivi.t8('Paste'),              icon: "paste",  callback: standard_item_ajax_call }
433     }, general_actions)
434   });
435
436   $.contextMenu({
437     selector: '.function-block-context-menu,.sub-function-block-context-menu',
438     events:   events,
439     items:    $.extend({
440         heading:                { name: kivi.t8('Function block actions'), className: 'context-menu-heading' }
441       , add_function_block:     { name: kivi.t8('Add function block'),     icon: "add",    callback: standard_item_ajax_call }
442       , add_sub_function_block: { name: kivi.t8('Add sub function block'), icon: "add",    callback: standard_item_ajax_call }
443       , sep1:                   "---------"
444       , edit:                   { name: kivi.t8('Edit'),                   icon: "edit",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
445       , delete:                 { name: kivi.t8('Delete'),                 icon: "delete", callback: ask_delete_item,         disabled: disable_edit_item_commands }
446       , sep2:                   "---------"
447       , flag:                   { name: kivi.t8('Toggle marker'),          icon: "flag",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
448       , sep3:                   "---------"
449       , copy:                   { name: kivi.t8('Copy'),                   icon: "copy",   callback: standard_item_ajax_call, disabled: disable_edit_item_commands }
450       , paste:                  { name: kivi.t8('Paste'),                  icon: "paste",  callback: standard_item_ajax_call }
451     }, general_actions)
452   });
453
454   $.contextMenu({
455     selector: '.time-cost-estimate-context-menu',
456     items:    $.extend({
457         heading: { name: kivi.t8('Time/cost estimate actions'), className: 'context-menu-heading' }
458       , edit:    { name: kivi.t8('Edit'), icon: "edit", callback: standard_time_cost_estimate_ajax_call }
459     }, general_actions)
460   });
461
462   $.contextMenu({
463     selector: '.edit-time-cost-estimate-context-menu',
464     items:    $.extend({
465         heading: { name: kivi.t8('Time/cost estimate actions'), className: 'context-menu-heading' }
466       , save:    { name: kivi.t8('Save'),   icon: "save",  callback: standard_time_cost_estimate_ajax_call }
467       , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: standard_time_cost_estimate_ajax_call }
468     }, general_actions)
469   });
470
471   $.contextMenu({
472     selector: '.versioned-copy-context-menu',
473     items:    $.extend({
474         heading:            { name: kivi.t8('Version actions'), className: 'context-menu-heading' }
475       , create_version_pdf: { name: kivi.t8('Create PDF'),        icon: "pdf",    callback: create_pdf_for_versioned_copy_ajax_call                                                }
476       , revert_to_version:  { name: kivi.t8('Revert to version'), icon: "revert", callback: revert_to_versioned_copy_ajax_call,     disabled: disable_versioned_copy_item_commands }
477     }, general_actions)
478   });
479 }