Pflichtenheftabschnittsvorlagen erzeugen, bearbeiten, löschen
[kivitendo-erp.git] / js / requirement_spec.js
1 /* Functions used for the requirement specs */
2
3 namespace("kivi.requirement_spec", function(ns) {
4
5 // -----------------------------------------------------------------------------
6 // ------------------------------ basic settings -------------------------------
7 // -----------------------------------------------------------------------------
8
9 ns.basic_settings_customer_changed = function(customer_ctrl, value_ctrl) {
10   $.get(
11     'controller.pl?action=Customer/get_hourly_rate',
12     { id: $(customer_ctrl).val() },
13     function(data) { if (data.hourly_rate_formatted) $(value_ctrl).val(data.hourly_rate_formatted); }
14   );
15 };
16
17 // -----------------------------------------------------------------------------
18 // ------------------------------ the tree itself ------------------------------
19 // -----------------------------------------------------------------------------
20
21 ns.tree_check_move = function(data) {
22   var dragged_type = data.o.data('type');
23   var dropped_type = data.r.data('type');
24
25   // console.debug("dragged " + dragged_type + " dropped " + dropped_type + " dir " + data.p);
26
27   if ((dragged_type == "sections") || (dragged_type == "text-blocks-front") || (dragged_type == "text-blocks-back"))
28     return false;
29
30   if (dragged_type == "text-block") {
31     if ((dropped_type == "text-blocks-front") || (dropped_type == "text-blocks-back"))
32       return (data.p == "inside") || (data.p == "last");
33     if (dropped_type == "text-block")
34       return (data.p == "before") || (data.p == "after");
35
36     return false;
37   }
38
39   if (dragged_type == "section") {
40     if (dropped_type == "sections")
41       return (data.p == "inside") || (data.p == "last");
42     if (dropped_type == "section")
43       return (data.p == "before") || (data.p == "after");
44
45     return false;
46   }
47
48   // dragged_type == (sub) function blocks
49   if ((dropped_type == "text-block") || (dropped_type == "text-blocks-front") || (dropped_type == "text-blocks-back"))
50     return false;
51
52   var dropped_depth = dropped_type == "sections" ? 0 : dropped_type == "section" ? 1 : data.r.parent().parent().data('type') != "function-block" ? 2 : 3;
53   if ((data.p == "inside") || (data.p == "last"))
54     dropped_depth++;
55
56   var dragged_depth = 1 + data.o.children('ul').size();
57
58   // console.debug("dropped_depth " + dropped_depth + " dragged_depth " + dragged_depth);
59
60   return (2 <= dropped_depth) && ((dragged_depth + dropped_depth) <= 4);
61 };
62
63 ns.tree_node_moved = function(event) {
64   // console.debug("node moved");
65   var move_obj   = $.jstree._reference('#tree')._get_move();
66   var dragged    = move_obj.o;
67   var dropped    = move_obj.r;
68   var controller = dragged.data("type") == "text-block" ? "RequirementSpecTextBlock" : "RequirementSpecItem";
69   var data       = {
70     action:               controller + "/dragged_and_dropped",
71     requirement_spec_id:  $('#requirement_spec_id').val(),
72     id:                   dragged.data("id"),
73     dropped_id:           dropped.data("id"),
74     dropped_type:         dropped.data("type"),
75     position:             move_obj.p,
76     current_content_type: $('#current_content_type').val(),
77     current_content_id:   $('#current_content_id').val()
78   };
79   // console.debug("controller: " + controller);
80   // console.debug(data);
81
82   $.post("controller.pl", data, kivi.eval_json_result);
83
84   return true;
85 };
86
87 ns.tree_node_clicked = function(event) {
88   var node = $.jstree._reference('#tree')._get_node(event.target);
89   var type = node ? node.data('type') : undefined;
90
91   if (!type)
92     return;
93
94   var url = 'controller.pl?action='
95   $.get('controller.pl', {
96     action:               (/^text-block/.test(type) ? 'RequirementSpecTextBlock' : 'RequirementSpecItem') + '/ajax_list.js',
97     requirement_spec_id:  $('#requirement_spec_id').val(),
98     current_content_type: $('#current_content_type').val(),
99     current_content_id:   $('#current_content_id').val(),
100     clicked_type:         type,
101     clicked_id:           node.data('id')
102   }, kivi.eval_json_result);
103 };
104
105 // -------------------------------------------------------------------------
106 // ------------------------------ text blocks ------------------------------
107 // -------------------------------------------------------------------------
108
109 ns.find_text_block_id = function(clicked_elt) {
110   // console.log("id: " + $(clicked_elt).attr('id'));
111   var id = $(clicked_elt).attr('id');
112   if (/^text-block-\d+$/.test(id)) {
113     // console.log("find_text_block_id: case 1: " + id.substr(11));
114     return id.substr(11) * 1;
115   }
116
117   id = $(clicked_elt).closest("[id*=text-block-]").attr('id')
118   if (/^text-block-\d+$/.test(id)) {
119     // console.log("find_text_block_id: case 2: " + id.substr(11));
120     return id.substr(11) * 1;
121   }
122
123   id = $(clicked_elt).closest("[id*=tb-]").attr('id')
124   if (/^tb-\d+$/.test(id)) {
125     // console.log("find_text_block_id: case 3: " + id.substr(3));
126     return id.substr(3) * 1;
127   }
128
129   // console.log("find_text_block_id: case undef");
130   return undefined;
131 };
132
133 ns.find_text_block_output_position = function(clicked_elt) {
134   var output_position = $(clicked_elt).closest('#text-block-list-container').find('#text_block_output_position').val();
135   if (output_position)
136     return output_position;
137
138   var type = $(clicked_elt).closest('#tb-back,#tb-front').data('type');
139   if (/^text-blocks-(front|back)/.test(type))
140     return type == "text-blocks-front" ? 0 : 1;
141
142   return undefined;
143 };
144
145 ns.disable_edit_text_block_commands = function(key, opt) {
146   return ns.find_text_block_id(opt.$trigger) == undefined;
147 };
148
149 ns.standard_text_block_ajax_call = function(key, opt, other_data) {
150   var data = {
151     action:               "RequirementSpecTextBlock/ajax_" + key,
152     requirement_spec_id:  $('#requirement_spec_id').val(),
153     id:                   ns.find_text_block_id(opt.$trigger),
154     output_position:      ns.find_text_block_output_position(opt.$trigger),
155     current_content_type: $('#current_content_type').val(),
156     current_content_id:   $('#current_content_id').val()
157   };
158
159   $.post("controller.pl", $.extend(data, other_data || {}), kivi.eval_json_result);
160
161   return true;
162 };
163
164 ns.cancel_edit_text_block_form = function(id_base) {
165   var id = $('#' + id_base + '_id').val();
166   $('#' + id_base + '_form').remove();
167   if (id)
168     $('#text-block-' + id).show();
169 };
170
171 ns.ask_delete_text_block = function(key, opt) {
172   if (confirm(kivi.t8("Are you sure?")))
173     ns.standard_text_block_ajax_call(key, opt);
174   return true;
175 };
176
177 // --------------------------------------------------------------------------------
178 // ------------------------------ sections and items ------------------------------
179 // --------------------------------------------------------------------------------
180
181 ns.find_item_id = function(clicked_elt) {
182   // console.log("clicked id: " + $(clicked_elt).attr('id'));
183   var id     = $(clicked_elt).attr('id');
184   var result = /^(function-block|function-block-content|sub-function-block|sub-function-block-content|section|section-header)-(\d+)$/.exec(id);
185   if (result) {
186     // console.log("find_item_id: case 1: " + result[2]);
187     return result[2];
188   }
189
190   id = $(clicked_elt).closest("[id*=fb-]").attr('id')
191   if (/^fb-\d+$/.test(id)) {
192     // console.log("find_item_id: case 2: " + id.substr(3));
193     return id.substr(3) * 1;
194   }
195
196   // console.log("find_item_id: case undef");
197   return undefined;
198 };
199
200 ns.standard_item_ajax_call = function(key, opt, other_data) {
201   var data = {
202     action:               "RequirementSpecItem/ajax_" + key,
203     requirement_spec_id:  $('#requirement_spec_id').val(),
204     id:                   ns.find_item_id(opt.$trigger),
205     current_content_type: $('#current_content_type').val(),
206     current_content_id:   $('#current_content_id').val()
207   };
208
209   // console.log("I would normally POST the following now:");
210   // console.log(data);
211   $.post("controller.pl", $.extend(data, other_data || {}), kivi.eval_json_result);
212
213   return true;
214 };
215
216 ns.disable_edit_item_commands = function(key, opt) {
217   return ns.find_item_id(opt.$trigger) == undefined;
218 };
219
220 ns.disable_add_function_block_command = function(key, opt) {
221   if (ns.find_item_id(opt.$trigger))
222     return false;
223   return opt.$trigger.attr('id') != "section-list-empty";
224 };
225
226 ns.cancel_edit_item_form = function(form_id_base, options) {
227   $('#' + form_id_base + '_form').remove();
228   if (!options)
229     return;
230   if (options.to_show)
231     $(options.to_show).show();
232   if (options.to_hide_if_empty && (1 == $(options.to_hide_if_empty).children().size()))
233     $(options.to_hide_if_empty).hide();
234 };
235
236 ns.ask_delete_item = function(key, opt) {
237   if (confirm(kivi.t8("Are you sure?")))
238     ns.standard_item_ajax_call(key, opt);
239   return true;
240 };
241
242 ns.handle_text_block_popup_menu_markings = function(opt, add) {
243   var id = ns.find_text_block_id(opt.$trigger);
244   if (id)
245     $('#text-block-' + id).toggleClass('selected', add);
246   return true;
247 };
248
249 ns.text_block_popup_menu_shown = function(opt) {
250   return ns.handle_text_block_popup_menu_markings(opt, true);
251 };
252
253 ns.text_block_popup_menu_hidden = function(opt) {
254   return ns.handle_text_block_popup_menu_markings(opt, false);
255 };
256
257 ns.handle_item_popup_menu_markings = function(opt, add) {
258   var id = ns.find_item_id(opt.$trigger);
259   if (id)
260     $('#section-' + id + ',#function-block-' + id + ',#sub-function-block-' + id).toggleClass('selected', add);
261   return true;
262 };
263
264 ns.item_popup_menu_shown = function(opt) {
265   return ns.handle_item_popup_menu_markings(opt, true);
266 };
267
268 ns.item_popup_menu_hidden = function(opt) {
269   return ns.handle_item_popup_menu_markings(opt, false);
270 };
271
272 // -------------------------------------------------------------------------
273 // -------------------------- time/cost estimate ---------------------------
274 // -------------------------------------------------------------------------
275
276 ns.standard_time_cost_estimate_ajax_call = function(key, opt) {
277   if ((key == 'cancel') && !confirm(kivi.t8('Do you really want to cancel?')))
278     return true;
279
280   var data = "action=RequirementSpec/ajax_" + key + "_time_and_cost_estimate&";
281
282   if (key == 'save')
283     data += $('#edit_time_cost_estimate_form').serialize()
284          +  '&' + $('#current_content_type').serialize()
285          +  '&' + $('#current_content_id').serialize();
286   else
287     data += 'id=' + encodeURIComponent($('#requirement_spec_id').val());
288
289   $.post("controller.pl", data, kivi.eval_json_result);
290
291   return true;
292 };
293
294 // -------------------------------------------------------------------------
295 // ---------------------------- general actions ----------------------------
296 // -------------------------------------------------------------------------
297
298 ns.create_reqspec_pdf = function(key, opt) {
299   var data = {
300     action: "RequirementSpec/create_pdf",
301     id:     $('#requirement_spec_id').val()
302   };
303   $.download("controller.pl", data);
304 };
305
306 ns.copy_reqspec = function(key, opt) {
307   window.location.href = "controller.pl?action=RequirementSpec/new&copy_source_id=" + encodeURIComponent($('#requirement_spec_id').val());
308   return true;
309 };
310
311 ns.delete_reqspec = function(key, opt) {
312   if (confirm(kivi.t8("Are you sure?")))
313     window.location.href = "controller.pl?action=RequirementSpec/destroy&id=" + encodeURIComponent($('#requirement_spec_id').val());
314   return true;
315 };
316
317 ns.disable_commands = function(key, opt) {
318   if (key === "create_version")
319     return ($('#current_version_id').val() || '') == '' ? false : true;
320   return false;
321 };
322
323 // -------------------------------------------------------------------------
324 // -------------------------------- versions -------------------------------
325 // -------------------------------------------------------------------------
326
327 ns.find_versioned_copy_id = function(clicked_elt) {
328   var id = $(clicked_elt).find("[name=versioned_copy_id]");
329   return id ? id.val() : undefined;
330 };
331
332 ns.disable_versioned_copy_item_commands = function(key, opt) {
333   if (key === "revert_to_version")
334     return !ns.find_versioned_copy_id(opt.$trigger);
335   return false;
336 };
337
338 ns.create_version = function() {
339   open_jqm_window({ url:  'controller.pl',
340                     data: { action:              'RequirementSpecVersion/new',
341                             requirement_spec_id: $('#requirement_spec_id').val() },
342                     id:   'new_requirement_spec_version_window' });
343   return true;
344 };
345
346 ns.create_pdf_for_versioned_copy_ajax_call = function(key, opt) {
347   var data = {
348     action: "RequirementSpec/create_pdf",
349     id:     ns.find_versioned_copy_id(opt.$trigger)
350   };
351   $.download("controller.pl", data);
352
353   return true;
354 };
355
356 ns.revert_to_versioned_copy_ajax_call = function(key, opt) {
357   if (!confirm(kivi.t8('Do you really want to revert to this version?')))
358     return true;
359
360   var data = {
361     action:            'RequirementSpec/revert_to',
362     versioned_copy_id: ns.find_versioned_copy_id(opt.$trigger),
363     id:                $('#requirement_spec_id').val()
364   };
365
366   $.post("controller.pl", data, kivi.eval_json_result);
367
368   return true;
369 };
370
371 // -------------------------------------------------------------------------
372 // ----------------------------- context menus -----------------------------
373 // -------------------------------------------------------------------------
374
375 ns.create_context_menus = function(is_template) {
376   if (is_template) {
377     var general_actions = {
378         sep98:           "---------"
379       , general_actions: { name: kivi.t8('Section template actions'), className: 'context-menu-heading' }
380       // , sep99:           "---------"
381       , copy_reqspec:    { name: kivi.t8('Copy section template'),   icon: "copy",   callback: kivi.requirement_spec.copy_reqspec   }
382       , delete_reqspec:  { name: kivi.t8('Delete section template'), icon: "delete", callback: kivi.requirement_spec.delete_reqspec }
383     };
384     var events = {};
385
386   } else {                      // if (is_template)
387     var general_actions = {
388         sep98:           "---------"
389       , general_actions: { name: kivi.t8('Requirement spec actions'), className: 'context-menu-heading' }
390       // , sep99:           "---------"
391       , create_pdf:      { name: kivi.t8('Create PDF'),              icon: "pdf",    callback: kivi.requirement_spec.create_reqspec_pdf }
392       , create_version:  { name: kivi.t8('Create new version'),      icon: "new",    callback: kivi.requirement_spec.create_version, disabled: kivi.requirement_spec.disable_commands }
393       , copy_reqspec:    { name: kivi.t8('Copy requirement spec'),   icon: "copy",   callback: kivi.requirement_spec.copy_reqspec   }
394       , delete_reqspec:  { name: kivi.t8('Delete requirement spec'), icon: "delete", callback: kivi.requirement_spec.delete_reqspec }
395     };
396
397     var events = {
398         show: kivi.requirement_spec.text_block_popup_menu_shown
399       , hide: kivi.requirement_spec.text_block_popup_menu_hidden
400     };
401
402     $.contextMenu({
403       selector: '.text-block-context-menu',
404       events:   {
405           show: kivi.requirement_spec.text_block_popup_menu_shown
406         , hide: kivi.requirement_spec.text_block_popup_menu_hidden
407       },
408       items:    $.extend({
409           heading: { name: kivi.t8('Text block actions'),    className: 'context-menu-heading' }
410         , add:     { name: kivi.t8('Add text block'),        icon: "add",    callback: kivi.requirement_spec.standard_text_block_ajax_call }
411         , edit:    { name: kivi.t8('Edit text block'),       icon: "edit",   callback: kivi.requirement_spec.standard_text_block_ajax_call, disabled: kivi.requirement_spec.disable_edit_text_block_commands }
412         , delete:  { name: kivi.t8('Delete text block'),     icon: "delete", callback: kivi.requirement_spec.ask_delete_text_block,         disabled: kivi.requirement_spec.disable_edit_text_block_commands }
413         , sep1:    "---------"
414         , flag:    { name: kivi.t8('Toggle marker'),         icon: "flag",   callback: kivi.requirement_spec.standard_text_block_ajax_call, disabled: kivi.requirement_spec.disable_edit_text_block_commands }
415         , sep2:    "---------"
416         , copy:    { name: kivi.t8('Copy'),                  icon: "copy",   callback: kivi.requirement_spec.standard_text_block_ajax_call, disabled: kivi.requirement_spec.disable_edit_text_block_commands }
417         , paste:   { name: kivi.t8('Paste'),                 icon: "paste",  callback: kivi.requirement_spec.standard_text_block_ajax_call  }
418       }, general_actions)
419     });
420
421     $.contextMenu({
422       selector: '.time-cost-estimate-context-menu',
423       items:    $.extend({
424           heading: { name: kivi.t8('Time/cost estimate actions'), className: 'context-menu-heading' }
425         , edit:    { name: kivi.t8('Edit'), icon: "edit", callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
426       }, general_actions)
427     });
428
429     $.contextMenu({
430       selector: '.edit-time-cost-estimate-context-menu',
431       items:    $.extend({
432           heading: { name: kivi.t8('Time/cost estimate actions'), className: 'context-menu-heading' }
433         , save:    { name: kivi.t8('Save'),   icon: "save",  callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
434         , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
435       }, general_actions)
436     });
437
438     $.contextMenu({
439       selector: '.versioned-copy-context-menu',
440       items:    $.extend({
441           heading:            { name: kivi.t8('Version actions'), className: 'context-menu-heading' }
442         , create_version_pdf: { name: kivi.t8('Create PDF'),        icon: "pdf",    callback: kivi.requirement_spec.create_pdf_for_versioned_copy_ajax_call                                                                      }
443         , revert_to_version:  { name: kivi.t8('Revert to version'), icon: "revert", callback: kivi.requirement_spec.revert_to_versioned_copy_ajax_call,     disabled: kivi.requirement_spec.disable_versioned_copy_item_commands }
444       }, general_actions)
445     });
446   }                             // if (is_template) ... else ...
447
448   $.contextMenu({
449     selector: '#content',
450     items:    general_actions
451   });
452
453   events = {
454       show: kivi.requirement_spec.item_popup_menu_shown
455     , hide: kivi.requirement_spec.item_popup_menu_hidden
456   };
457
458   $.contextMenu({
459     selector: '.section-context-menu',
460     events:   events,
461     items:    $.extend({
462         heading:            { name: kivi.t8('Section/Function block actions'), className: 'context-menu-heading' }
463       , add_section:        { name: kivi.t8('Add section'),        icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
464       , add_function_block: { name: kivi.t8('Add function block'), icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_add_function_block_command }
465       , sep1:               "---------"
466       , edit:               { name: kivi.t8('Edit'),               icon: "edit",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
467       , delete:             { name: kivi.t8('Delete'),             icon: "delete", callback: kivi.requirement_spec.ask_delete_item,         disabled: kivi.requirement_spec.disable_edit_item_commands }
468       , sep2:               "---------"
469       , flag:               { name: kivi.t8('Toggle marker'),      icon: "flag",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
470       , sep3:               "---------"
471       , copy:               { name: kivi.t8('Copy'),               icon: "copy",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
472       , paste:              { name: kivi.t8('Paste'),              icon: "paste",  callback: kivi.requirement_spec.standard_item_ajax_call }
473     }, general_actions)
474   });
475
476   $.contextMenu({
477     selector: '.function-block-context-menu,.sub-function-block-context-menu',
478     events:   events,
479     items:    $.extend({
480         heading:                { name: kivi.t8('Function block actions'), className: 'context-menu-heading' }
481       , add_function_block:     { name: kivi.t8('Add function block'),     icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
482       , add_sub_function_block: { name: kivi.t8('Add sub function block'), icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
483       , sep1:                   "---------"
484       , edit:                   { name: kivi.t8('Edit'),                   icon: "edit",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
485       , delete:                 { name: kivi.t8('Delete'),                 icon: "delete", callback: kivi.requirement_spec.ask_delete_item,         disabled: kivi.requirement_spec.disable_edit_item_commands }
486       , sep2:                   "---------"
487       , flag:                   { name: kivi.t8('Toggle marker'),          icon: "flag",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
488       , sep3:                   "---------"
489       , copy:                   { name: kivi.t8('Copy'),                   icon: "copy",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
490       , paste:                  { name: kivi.t8('Paste'),                  icon: "paste",  callback: kivi.requirement_spec.standard_item_ajax_call }
491     }, general_actions)
492   });
493 };
494
495 });                             // end of namespace(...., function() {...