Pflichtenhefte: zusätzliche Artikel zuweisen und bearbeiten können
[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 ns.setup_tooltip_for_tree_node = function(li) {
106   $(li).find('a').prop('title', $(li).prop('title')).tooltip();
107   $(li).prop('title', '');
108 };
109
110 ns.tree_loaded = function(event) {
111   var id = $('#tree').data('initially_selected_node');
112   if (id)
113     $.jstree._reference("#tree").select_node(id, true);
114
115   $('#tree li[title!=""]').each(function(idx, elt) {
116     ns.setup_tooltip_for_tree_node(elt);
117   });
118 };
119
120 ns.tree_node_created = function(event, data) {
121   console.info("created ", data);
122   if (data && data.rslt && data.rslt.obj)
123     ns.setup_tooltip_for_tree_node(data.rslt.obj);
124 };
125
126 ns.initialize_requirement_spec = function(data) {
127   $('#tree').data('initially_selected_node', data.initially_selected_node);
128
129   $('#tree')
130     .bind('create_node.jstree', ns.tree_node_created)
131     .bind('move_node.jstree',   ns.tree_node_moved)
132     .bind('click.jstree',       ns.tree_node_clicked)
133     .bind('loaded.jstree',      ns.tree_loaded)
134     .jstree({
135       core: {
136         animation: 0,
137         initially_open: data.initially_open,
138       },
139       json_data: {
140         data: data.tree_data
141       },
142       crrm: {
143         move: {
144           check_move: ns.tree_check_move,
145           open_move:  true
146         }
147       },
148       themes: {
149         theme: "requirement-spec"
150       },
151       plugins: [ "themes", "json_data", "ui", "crrm", "dnd" ]
152     });
153
154   ns.create_context_menus(data.is_template);
155   $('#requirement_spec_tabs').on("tabsbeforeactivate", ns.tabs_before_activate);
156 };
157
158 // -------------------------------------------------------------------------
159 // ------------------------------ text blocks ------------------------------
160 // -------------------------------------------------------------------------
161
162 ns.find_text_block_id = function(clicked_elt) {
163   var id = $(clicked_elt).attr('id');
164
165   if (/^text-block-picture-\d+$/.test(id))
166     id = $(clicked_elt).closest('.text-block-context-menu').attr('id');
167
168   // console.log("id: " + id);
169   if (/^text-block-\d+$/.test(id)) {
170     // console.log("find_text_block_id: case 1: " + id.substr(11));
171     return id.substr(11) * 1;
172   }
173
174   id = $(clicked_elt).closest("[id*=text-block-]").attr('id')
175   if (/^text-block-\d+$/.test(id)) {
176     // console.log("find_text_block_id: case 2: " + id.substr(11));
177     return id.substr(11) * 1;
178   }
179
180   id = $(clicked_elt).closest("[id*=tb-]").attr('id')
181   if (/^tb-\d+$/.test(id)) {
182     // console.log("find_text_block_id: case 3: " + id.substr(3));
183     return id.substr(3) * 1;
184   }
185
186   // console.log("find_text_block_id: case undef, id: " + id);
187   return undefined;
188 };
189
190 ns.find_text_block_output_position = function(clicked_elt) {
191   var output_position = $(clicked_elt).closest('#text-block-list-container').find('#text_block_output_position').val();
192   if (output_position)
193     return output_position;
194
195   var type = $(clicked_elt).closest('#tb-back,#tb-front').data('type') || $('#current_content_type').val();
196   if (/^text-blocks-(front|back)/.test(type))
197     return type == "text-blocks-front" ? 0 : 1;
198
199   return undefined;
200 };
201
202 ns.disable_edit_text_block_commands = function(key, opt) {
203   return ns.find_text_block_id(opt.$trigger) == undefined;
204 };
205
206 ns.standard_text_block_ajax_call = function(key, opt, other_data) {
207   var data = {
208     action:               "RequirementSpecTextBlock/ajax_" + key,
209     requirement_spec_id:  $('#requirement_spec_id').val(),
210     id:                   ns.find_text_block_id(opt.$trigger),
211     output_position:      ns.find_text_block_output_position(opt.$trigger),
212     current_content_type: $('#current_content_type').val(),
213     current_content_id:   $('#current_content_id').val()
214   };
215
216   $.post("controller.pl", $.extend(data, other_data || {}), kivi.eval_json_result);
217
218   return true;
219 };
220
221 ns.cancel_edit_text_block_form = function(id_base) {
222   var id = $('#' + id_base + '_id').val();
223   $('#' + id_base + '_form').remove();
224   if (id)
225     $('#text-block-' + id).show();
226 };
227
228 ns.ask_delete_text_block = function(key, opt) {
229   if (confirm(kivi.t8("Are you sure?")))
230     ns.standard_text_block_ajax_call(key, opt);
231   return true;
232 };
233
234 ns.find_text_block_picture_id = function(clicked_elt) {
235   var id    = $(clicked_elt).attr('id');
236   var match = id.match(/^text-block-picture-(\d+)$/);
237   if (match)
238     return match[1] * 1;
239
240   return undefined;
241 };
242
243 ns.add_edit_text_block_picture_ajax_call = function(key, opt) {
244   var title = key == 'add_picture' ? kivi.t8('Add picture to text block') : kivi.t8('Edit picture');
245
246   kivi.popup_dialog({ url:    'controller.pl',
247                       data:   { action:     'RequirementSpecTextBlock/ajax_' + key,
248                                 id:         ns.find_text_block_id(opt.$trigger),
249                                 picture_id: ns.find_text_block_picture_id(opt.$trigger) },
250                       dialog: { title:      title }});
251
252   return true;
253 };
254
255 ns.standard_text_block_picture_ajax_call = function(key, opt) {
256   var data = {
257       action: "RequirementSpecTextBlock/ajax_" + key
258     , id:     ns.find_text_block_picture_id(opt.$trigger)
259   };
260
261   if (key == 'download_picture')
262     $.download("controller.pl", data);
263   else
264     $.post("controller.pl", data, kivi.eval_json_result);
265
266   return true;
267 };
268
269 ns.ask_delete_text_block_picture = function(key, opt) {
270   if (confirm(kivi.t8("Are you sure?")))
271     ns.standard_text_block_picture_ajax_call(key, opt);
272   return true;
273 };
274
275 ns.handle_text_block_picture_popup_menu_markings = function(opt, add) {
276   var id = ns.find_text_block_picture_id(opt.$trigger);
277   if (id)
278     $('#text-block-picture-' + id ).toggleClass('selected', add);
279   return true;
280 };
281
282 ns.text_block_picture_popup_menu_shown = function(opt) {
283   return ns.handle_text_block_picture_popup_menu_markings(opt, true);
284 };
285
286 ns.text_block_picture_popup_menu_hidden = function(opt) {
287   return ns.handle_text_block_picture_popup_menu_markings(opt, false);
288 };
289
290 ns.make_text_block_picture_lists_sortable = function() {
291   kivi.run_once_for(".requirement-spec-text-block-picture-list", 'make-text-block-picture-list-sortable', function($elt) {
292     $elt.sortable({
293       stop: function(event, ui) {
294         $.post('controller.pl?action=RequirementSpecTextBlock/reorder_pictures', {
295           'picture_id[]': $($elt.sortable('toArray')).map(function(idx, str) { return str.replace('text-block-picture-', ''); }).toArray()
296         });
297         return ui;
298       }
299       , distance: 5
300     });
301   });
302 };
303
304 // --------------------------------------------------------------------------------
305 // ------------------------------ sections and items ------------------------------
306 // --------------------------------------------------------------------------------
307
308 ns.find_item_id = function(clicked_elt) {
309   // console.log("clicked id: " + $(clicked_elt).attr('id'));
310   var id     = $(clicked_elt).attr('id');
311   var result = /^(function-block|function-block-content|sub-function-block|sub-function-block-content|section|section-header)-(\d+)$/.exec(id);
312   if (result) {
313     // console.log("find_item_id: case 1: " + result[2]);
314     return result[2];
315   }
316
317   id = $(clicked_elt).closest("[id*=fb-]").attr('id')
318   if (/^fb-\d+$/.test(id)) {
319     // console.log("find_item_id: case 2: " + id.substr(3));
320     return id.substr(3) * 1;
321   }
322
323   // console.log("find_item_id: case undef");
324   return undefined;
325 };
326
327 ns.standard_item_ajax_call = function(key, opt, other_data) {
328   var data = {
329     action:               "RequirementSpecItem/ajax_" + key,
330     requirement_spec_id:  $('#requirement_spec_id').val(),
331     id:                   ns.find_item_id(opt.$trigger),
332     current_content_type: $('#current_content_type').val(),
333     current_content_id:   $('#current_content_id').val()
334   };
335
336   // console.log("I would normally POST the following now:");
337   // console.log(data);
338   $.post("controller.pl", $.extend(data, other_data || {}), kivi.eval_json_result);
339
340   return true;
341 };
342
343 ns.disable_edit_item_commands = function(key, opt) {
344   return ns.find_item_id(opt.$trigger) == undefined;
345 };
346
347 ns.disable_add_function_block_command = function(key, opt) {
348   return opt.$trigger.attr('id') == "sections";
349 };
350
351 ns.cancel_edit_item_form = function(form_id_base, options) {
352   $('#' + form_id_base + '_form').remove();
353   if (!options)
354     return;
355   if (options.to_show)
356     $(options.to_show).show();
357   if (options.to_hide_if_empty && (1 == $(options.to_hide_if_empty).children().size()))
358     $(options.to_hide_if_empty).hide();
359 };
360
361 ns.ask_delete_item = function(key, opt) {
362   if (confirm(kivi.t8("Are you sure?")))
363     ns.standard_item_ajax_call(key, opt);
364   return true;
365 };
366
367 ns.handle_text_block_popup_menu_markings = function(opt, add) {
368   var id = ns.find_text_block_id(opt.$trigger);
369   if (id)
370     $('#text-block-' + id).toggleClass('selected', add);
371   return true;
372 };
373
374 ns.text_block_popup_menu_shown = function(opt) {
375   return ns.handle_text_block_popup_menu_markings(opt, true);
376 };
377
378 ns.text_block_popup_menu_hidden = function(opt) {
379   return ns.handle_text_block_popup_menu_markings(opt, false);
380 };
381
382 ns.handle_item_popup_menu_markings = function(opt, add) {
383   var id = ns.find_item_id(opt.$trigger);
384   if (id)
385     $('#section-' + id + ',#function-block-' + id + ',#sub-function-block-' + id).toggleClass('selected', add);
386   return true;
387 };
388
389 ns.item_popup_menu_shown = function(opt) {
390   return ns.handle_item_popup_menu_markings(opt, true);
391 };
392
393 ns.item_popup_menu_hidden = function(opt) {
394   return ns.handle_item_popup_menu_markings(opt, false);
395 };
396
397 ns.submit_function_block = function(event, shift_in_out) {
398   event.preventDefault();
399
400   var prefix = $(this).attr('id').match("^(?:edit|new)_function_block_[\\d_]+\\d")[0];
401   var action = $('#' + prefix + '_id').val() ? 'update' : 'create';
402   kivi.submit_ajax_form('controller.pl?action=RequirementSpecItem/ajax_' + action, '#' + prefix + '_form', { shift_in_out: !!shift_in_out });
403
404   return false;
405 };
406
407 ns.init_function_block_keypress_events = function(form_id) {
408   $("#" + form_id + " INPUT[type=text]").bind("keypress", "return", ns.submit_function_block);
409
410   $('#' + form_id + ' INPUT[type=text],#' + form_id + ' TEXTAREA,#' + form_id + ' INPUT[type=button]')
411     .bind('keypress', 'shift+return', function(event) { return ns.submit_function_block.apply(this, [ event, true ]); });
412 };
413
414 // -------------------------------------------------------------------------
415 // ------------------------------- templates -------------------------------
416 // -------------------------------------------------------------------------
417
418 ns.paste_template = function(key, opt, other_data) {
419   kivi.popup_dialog({ url: 'controller.pl?action=RequirementSpec/select_template_to_paste', dialog: { title: kivi.t8("Select template to paste") } });
420 };
421
422 ns.paste_selected_template = function(template_id) {
423   $('#jqueryui_popup_dialog').dialog("close");
424
425   var data = {
426     action:               "RequirementSpec/paste_template",
427     id:                   $('#requirement_spec_id').val(),
428     template_id:          template_id,
429     current_content_type: $('#current_content_type').val(),
430     current_content_id:   $('#current_content_id').val()
431   };
432
433   // console.log("I would normally POST the following now:");
434   // console.log(data);
435   $.post("controller.pl", data, kivi.eval_json_result);
436
437   return true;
438 };
439
440 // -------------------------------------------------------------------------
441 // ---------------------------- basic settings -----------------------------
442 // -------------------------------------------------------------------------
443 ns.standard_basic_settings_ajax_call = function(key, opt) {
444   if (key == 'cancel') {
445     if (confirm(kivi.t8('Do you really want to cancel?'))) {
446       $('#basic_settings').show();
447       $('#basic_settings_form,#project_link_form').remove();
448     }
449     return true;
450
451   } else if (key == 'save') {
452     $('#basic_settings_form_submit').click();
453     return true;
454   }
455
456   var data = 'action=RequirementSpec/ajax_' + key + '&id=' + encodeURIComponent($('#requirement_spec_id').val());
457
458   $.post("controller.pl", data, kivi.eval_json_result);
459
460   return true;
461 };
462
463 // -------------------------------------------------------------------------
464 // -------------------------- time/cost estimate ---------------------------
465 // -------------------------------------------------------------------------
466
467 ns.standard_time_cost_estimate_ajax_call = function(key, opt) {
468   if (key == 'cancel') {
469     if (confirm(kivi.t8('Do you really want to cancel?'))) {
470       $('#time_cost_estimate').show();
471       $('#time_cost_estimate_form_container').remove();
472     }
473     return true;
474   }
475
476   var add_data = '';
477   if (key == 'save_keep_open') {
478     key      = 'save';
479     add_data = 'keep_open=1&';
480   }
481
482   var data = "action=RequirementSpec/ajax_" + key + "_time_and_cost_estimate&" + add_data;
483
484   if (key == 'save')
485     data += $('#edit_time_cost_estimate_form').serialize()
486          +  '&' + $('#current_content_type').serialize()
487          +  '&' + $('#current_content_id').serialize();
488   else
489     data += 'id=' + encodeURIComponent($('#requirement_spec_id').val());
490
491   $.post("controller.pl", data, kivi.eval_json_result);
492
493   return true;
494 };
495
496 ns.time_cost_estimate_input_key_down = function(event) {
497   if(event.keyCode == 13) {
498     event.preventDefault();
499     ns.standard_time_cost_estimate_ajax_call('save');
500     return false;
501   }
502 };
503
504 // -------------------------------------------------------------------------
505 // --------------------------- quotations/orders ---------------------------
506 // -------------------------------------------------------------------------
507
508 ns.find_quotation_order_id = function(clicked_elt) {
509   return $(clicked_elt).find('>[name=order_id]').val();
510 };
511
512 ns.standard_quotation_order_ajax_call = function(key, opt) {
513   if (key == 'cancel') {
514     if (confirm(kivi.t8('Do you really want to cancel?'))) {
515       $('#quotations_and_orders').show();
516       $('#quotations_and_orders_article_assignment,#quotations_and_orders_new,#quotations_and_orders_update').remove();
517     }
518     return true;
519   }
520
521   else if ((key == 'create') && $('#quotations_and_orders_form INPUT[name="sections[].order_part_id"]').filter(function(idx, elt) { return ($(elt).val() || '') == '' }).size()) {
522     alert(kivi.t8('There is one or more sections for which no part has been assigned yet; therefore creating the new record is not possible yet.'));
523     return false;
524   }
525
526   var data = 'action=RequirementSpecOrder/' + key
527            + '&' + $('#requirement_spec_id').serialize();
528
529   if ((key == 'save_assignment') || (key == 'create') || (key == 'do_update'))
530     data += '&' + $('#quotations_and_orders_form').serialize();
531   else if ((key == 'update') || (key == 'delete'))
532     data += '&rs_order_id=' + encodeURIComponent(ns.find_quotation_order_id(opt.$trigger));
533
534   // console.log("I would normally POST the following now:");
535   // console.log(data);
536   $.post("controller.pl", data, kivi.eval_json_result);
537
538   return true;
539 };
540
541 ns.ask_delete_quotation_order = function(key, opt) {
542   if (confirm(kivi.t8("Are you sure?")))
543     ns.standard_quotation_order_ajax_call(key, opt);
544   return true;
545 };
546
547 ns.disable_edit_quotation_order_commands = function(key, opt) {
548   return ns.find_quotation_order_id(opt.$trigger) == undefined;
549 };
550
551 ns.disable_create_quotation_order_commands = function(key, opt) {
552   return !$('#quotations_and_orders_sections');
553 };
554
555 ns.assign_order_part_id_to_all = function() {
556   var order_part_id   = $('#quotations_and_orders_order_id').val();
557   var order_part_name = $('#quotations_and_orders_order_id_name').val();
558
559   $('#quotations_and_orders_form INPUT[name="sections[].order_part_id"]').each(function(idx, elt) {
560     $(elt).val(order_part_id);
561   });
562
563   $('#quotations_and_orders_form [id^=quotations_and_orders_sections_order_pard_id_]').filter(function() {
564     return $(this).attr('id') && $(this).attr('id').match("^quotations_and_orders_sections_order_pard_id_[0-9]+_name$");
565   }).each(function(idx, elt) {
566     $(elt).val(order_part_name);
567   });
568 };
569
570 // -------------------------------------------------------------------------
571 // ---------------------------- general actions ----------------------------
572 // -------------------------------------------------------------------------
573
574 ns.create_reqspec_pdf = function(key, opt) {
575   var data = {
576     action: "RequirementSpec/create_pdf",
577     id:     $('#requirement_spec_id').val()
578   };
579   $.download("controller.pl", data);
580 };
581
582 ns.copy_reqspec = function(key, opt) {
583   window.location.href = "controller.pl?action=RequirementSpec/new&copy_source_id=" + encodeURIComponent($('#requirement_spec_id').val());
584   return true;
585 };
586
587 ns.delete_reqspec = function(key, opt) {
588   if (confirm(kivi.t8("Are you sure?")))
589     window.location.href = "controller.pl?action=RequirementSpec/destroy&id=" + encodeURIComponent($('#requirement_spec_id').val());
590   return true;
591 };
592
593 ns.disable_commands = function(key, opt) {
594   if (key === "create_version")
595     return ($('#current_version_id').val() || '') == '' ? false : true;
596   return false;
597 };
598
599 // -------------------------------------------------------------------------
600 // -------------------------------- versions -------------------------------
601 // -------------------------------------------------------------------------
602
603 ns.find_versioned_copy_id = function(clicked_elt) {
604   var id = $(clicked_elt).find("[name=versioned_copy_id]");
605   return id ? id.val() : undefined;
606 };
607
608 ns.disable_versioned_copy_item_commands = function(key, opt) {
609   if (key === "revert_to_version")
610     return !ns.find_versioned_copy_id(opt.$trigger);
611   return false;
612 };
613
614 ns.create_version = function() {
615   kivi.popup_dialog({ url:    'controller.pl',
616                       data:   { action:              'RequirementSpecVersion/new',
617                                 requirement_spec_id: $('#requirement_spec_id').val() },
618                       dialog: { title: kivi.t8('Create a new version') }});
619   return true;
620 };
621
622 ns.create_pdf_for_versioned_copy_ajax_call = function(key, opt) {
623   var data = {
624     action: "RequirementSpec/create_pdf",
625     id:     ns.find_versioned_copy_id(opt.$trigger)
626   };
627   $.download("controller.pl", data);
628
629   return true;
630 };
631
632 ns.revert_to_versioned_copy_ajax_call = function(key, opt) {
633   if (!confirm(kivi.t8('Do you really want to revert to this version?')))
634     return true;
635
636   var data = {
637     action:            'RequirementSpec/revert_to',
638     versioned_copy_id: ns.find_versioned_copy_id(opt.$trigger),
639     id:                $('#requirement_spec_id').val()
640   };
641
642   $.post("controller.pl", data, kivi.eval_json_result);
643
644   return true;
645 };
646
647 // -------------------------------------------------------------------------
648 // -------------------------- time/cost estimate ---------------------------
649 // -------------------------------------------------------------------------
650
651 ns.standard_time_cost_estimate_ajax_call = function(key, opt) {
652   if (key == 'cancel') {
653     if (confirm(kivi.t8('Do you really want to cancel?'))) {
654       $('#time_cost_estimate').show();
655       $('#time_cost_estimate_form_container').remove();
656     }
657     return true;
658   }
659
660   var add_data = '';
661   if (key == 'save_keep_open') {
662     key      = 'save';
663     add_data = 'keep_open=1&';
664   }
665
666   var data = "action=RequirementSpec/ajax_" + key + "_time_and_cost_estimate&" + add_data;
667
668   if (key == 'save')
669     data += $('#edit_time_cost_estimate_form').serialize()
670          +  '&' + $('#current_content_type').serialize()
671          +  '&' + $('#current_content_id').serialize();
672   else
673     data += 'id=' + encodeURIComponent($('#requirement_spec_id').val());
674
675   $.post("controller.pl", data, kivi.eval_json_result);
676
677   return true;
678 };
679
680 ns.time_cost_estimate_input_key_down = function(event) {
681   if(event.keyCode == 13) {
682     event.preventDefault();
683     ns.standard_time_cost_estimate_ajax_call('save');
684     return false;
685   }
686 };
687
688 // -------------------------------------------------------------------------
689 // -------------------------- additional parts -----------------------------
690 // -------------------------------------------------------------------------
691
692 ns.standard_additional_parts_ajax_call = function(key, opt) {
693   var add_data = '';
694   if (key == 'save_keep_open') {
695     key      = 'save';
696     add_data = 'keep_open=1&';
697   }
698
699   var data = "action=RequirementSpecPart/ajax_" + key + "&" + add_data + 'requirement_spec_id=' + encodeURIComponent($('#requirement_spec_id').val()) + '&';
700
701   if (key == 'save')
702     data += $('#edit_additional_parts_form').serialize();
703
704   $.post("controller.pl", data, kivi.eval_json_result);
705
706   return true;
707 };
708
709 ns.prepare_edit_additional_parts_form = function() {
710   $("#edit_additional_parts_list tbody").sortable({
711     distance: 5,
712     handle:   '.dragdrop',
713     helper:   function(event, ui) {
714       ui.children().each(function() {
715         $(this).width($(this).width());
716       });
717       return ui;
718     }
719
720   });
721 };
722
723 ns.cancel_edit_additional_parts_form = function() {
724   if (confirm(kivi.t8('Do you really want to cancel?'))) {
725     $('#additional_parts_list_container').show();
726     $('#additional_parts_form_container').remove();
727   }
728   return true;
729 };
730
731 ns.additional_parts_input_key_down = function(event) {
732   if(event.keyCode == 13) {
733     event.preventDefault();
734     ns.standard_additional_parts_ajax_call('save');
735     return false;
736   }
737 };
738
739 ns.add_additional_part = function() {
740   var part_id = $('#additional_parts_add_part_id').val();
741   if (!part_id || (part_id == ''))
742     return false;
743
744   var rspec_id = $('#requirement_spec_id').val();
745   var data     = 'action=RequirementSpecPart/ajax_add&requirement_spec_id=' + encodeURIComponent(rspec_id) + '&part_id=' + encodeURIComponent(part_id);
746
747   $.post("controller.pl", data, kivi.eval_json_result);
748
749   return true;
750 };
751
752 ns.delete_additional_part = function(key, opt) {
753   opt.$trigger.remove();
754   if (!$('#edit_additional_parts_list tbody tr').size()) {
755    $('#edit_additional_parts_list_empty').show();
756    $('#edit_additional_parts_list').hide();
757   }
758
759   return true;
760 };
761
762 // -------------------------------------------------------------------------
763 // ------------------------------- tab widget ------------------------------
764 // -------------------------------------------------------------------------
765 var content_div_ids_for_tab_headers = {
766     'tab-header-function-block':     'function-blocks-tab'
767   , 'tab-header-basic-settings':     'ui-tabs-1'
768   , 'tab-header-time-cost-estimate': 'ui-tabs-2'
769   , 'tab-header-additional-parts':   'ui-tabs-3'
770   , 'tab-header-versions':           'ui-tabs-4'
771   , 'tab-header-quotations-orders':  'ui-tabs-5'
772 };
773
774 ns.tabs_before_activate = function(event, ui) {
775   if (!ui.oldTab)
776     return true;
777
778   var content_div_id = content_div_ids_for_tab_headers[ $(ui.oldTab).attr('id') ];
779   if (!content_div_id || (content_div_id == 'function-blocks-tab'))
780     return true;
781
782   var inputs = $('#' + content_div_id).find('input,select,textarea').filter('[type!=hidden]');
783   if (!inputs.size() || confirm(kivi.t8("If you switch to a different tab without saving you will lose the data you've entered in the current tab.")))
784     return true;
785
786   var new_focus = $(inputs).filter(':focusable')[0];
787   if (new_focus)
788     $(new_focus).focus();
789
790   return false;
791 };
792
793 // -------------------------------------------------------------------------
794 // ----------------------------- context menus -----------------------------
795 // -------------------------------------------------------------------------
796
797 ns.create_context_menus = function(is_template) {
798   if (is_template) {
799     var general_actions = {
800         sep98:           "---------"
801       , general_actions: { name: kivi.t8('Requirement spec template actions'), className: 'context-menu-heading' }
802       // , sep99:           "---------"
803       , copy_reqspec:    { name: kivi.t8('Copy template'),   icon: "copy",   callback: kivi.requirement_spec.copy_reqspec   }
804       , delete_reqspec:  { name: kivi.t8('Delete template'), icon: "delete", callback: kivi.requirement_spec.delete_reqspec }
805     };
806
807     $.contextMenu({
808       selector: '.basic-settings-context-menu',
809       items:    $.extend({
810           heading: { name: kivi.t8('Basic settings actions'), className: 'context-menu-heading' }
811         , edit:    { name: kivi.t8('Edit'), icon: "edit", callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
812       }, general_actions)
813     });
814
815   } else {                      // if (is_template)
816     var general_actions = {
817         sep98:              "---------"
818       , general_actions:    { name: kivi.t8('Requirement spec actions'), className: 'context-menu-heading' }
819       , create_pdf:         { name: kivi.t8('Create PDF'),              icon: "pdf",    callback: kivi.requirement_spec.create_reqspec_pdf }
820       , create_version:     { name: kivi.t8('Create new version'),      icon: "new",    callback: kivi.requirement_spec.create_version, disabled: kivi.requirement_spec.disable_commands }
821       , copy_reqspec:       { name: kivi.t8('Copy requirement spec'),   icon: "copy",   callback: kivi.requirement_spec.copy_reqspec   }
822       , delete_reqspec:     { name: kivi.t8('Delete requirement spec'), icon: "delete", callback: kivi.requirement_spec.delete_reqspec }
823       , sep_paste_template: "---------"
824       , paste_template:     { name: kivi.t8('Paste template'),     icon: "paste",  callback: kivi.requirement_spec.paste_template }
825     };
826
827     $.contextMenu({
828       selector: '.versioned-copy-context-menu',
829       items:    $.extend({
830           heading:            { name: kivi.t8('Version actions'), className: 'context-menu-heading' }
831         , create_version_pdf: { name: kivi.t8('Create PDF'),        icon: "pdf",    callback: kivi.requirement_spec.create_pdf_for_versioned_copy_ajax_call                                                                      }
832         , 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 }
833       }, general_actions)
834     });
835
836     $.contextMenu({
837       selector: '.basic-settings-context-menu',
838       items:    $.extend({
839           heading:           { name: kivi.t8('Basic settings actions'), className: 'context-menu-heading' }
840         , edit:              { name: kivi.t8('Edit'),              icon: "edit", callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
841         , edit_project_link: { name: kivi.t8('Edit project link'),               callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
842       }, general_actions)
843     });
844
845     $.contextMenu({
846       selector: '.edit-project-link-context-menu',
847       items:    $.extend({
848           heading: { name: kivi.t8('Project link actions'), className: 'context-menu-heading' }
849         , save:    { name: kivi.t8('Save'),   icon: "save",  callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
850         , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
851       }, general_actions)
852     });
853
854     var paste_template_actions = {
855     };
856   }                             // if (is_template) ... else ...
857
858   $.contextMenu({
859     selector: '.text-block-context-menu',
860     events:   {
861         show: kivi.requirement_spec.text_block_popup_menu_shown
862       , hide: kivi.requirement_spec.text_block_popup_menu_hidden
863     },
864     items:    $.extend({
865         heading: { name: kivi.t8('Text block actions'),    className: 'context-menu-heading' }
866       , add:     { name: kivi.t8('Add text block'),        icon: "add",    callback: kivi.requirement_spec.standard_text_block_ajax_call }
867       , 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 }
868       , 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 }
869       , add_picture: { name: kivi.t8('Add picture to text block'), icon: "add-picture", callback: kivi.requirement_spec.add_edit_text_block_picture_ajax_call, disabled: kivi.requirement_spec.disable_edit_text_block_commands }
870       , sep1:    "---------"
871       , 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 }
872       , sep2:    "---------"
873       , 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 }
874       , paste:   { name: kivi.t8('Paste'),                 icon: "paste",  callback: kivi.requirement_spec.standard_text_block_ajax_call  }
875     }, general_actions)
876   });
877
878   $.contextMenu({
879     selector: '.text-block-picture-context-menu',
880     events:   {
881         show: kivi.requirement_spec.text_block_picture_popup_menu_shown
882       , hide: kivi.requirement_spec.text_block_picture_popup_menu_hidden
883     },
884     items:    $.extend({
885         heading:          { name: kivi.t8('Text block picture actions'), className: 'context-menu-heading'                                                 }
886       , add_picture:      { name: kivi.t8('Add picture'),      icon: "add-picture", callback: kivi.requirement_spec.add_edit_text_block_picture_ajax_call  }
887       , edit_picture:     { name: kivi.t8('Edit picture'),     icon: "edit",        callback: kivi.requirement_spec.add_edit_text_block_picture_ajax_call  }
888       , delete_picture:   { name: kivi.t8('Delete picture'),   icon: "delete",      callback: kivi.requirement_spec.ask_delete_text_block_picture          }
889       , download_picture: { name: kivi.t8('Download picture'), icon: "download",    callback: kivi.requirement_spec.standard_text_block_picture_ajax_call  }
890       , sep2:             "---------"
891       , copy_picture:     { name: kivi.t8('Copy'),             icon: "copy",        callback: kivi.requirement_spec.standard_text_block_picture_ajax_call  }
892       , paste_picture:    { name: kivi.t8('Paste'),            icon: "paste",       callback: kivi.requirement_spec.standard_text_block_picture_ajax_call  }
893     }, general_actions)
894   });
895
896   $.contextMenu({
897     selector: '.edit-basic-settings-context-menu',
898     items:    $.extend({
899         heading: { name: kivi.t8('Basic settings actions'), className: 'context-menu-heading' }
900       , save:    { name: kivi.t8('Save'),   icon: "save",  callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
901       , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
902     }, general_actions)
903   });
904
905   $.contextMenu({
906     selector: '.time-cost-estimate-context-menu',
907     items:    $.extend({
908         heading: { name: kivi.t8('Time/cost estimate actions'), className: 'context-menu-heading' }
909       , edit:    { name: kivi.t8('Edit'), icon: "edit", callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
910     }, general_actions)
911   });
912
913   $.contextMenu({
914     selector: '.edit-time-cost-estimate-context-menu',
915     items:    $.extend({
916         heading: { name: kivi.t8('Time/cost estimate actions'), className: 'context-menu-heading' }
917       , save:    { name: kivi.t8('Save'),   icon: "save",  callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
918       , save_keep_open: { name: kivi.t8('Save and keep open'), icon: "save", callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
919       , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
920     }, general_actions)
921   });
922
923   $.contextMenu({
924     selector: '.additional-parts-context-menu',
925     items:    $.extend({
926         heading: { name: kivi.t8('Additional articles actions'), className: 'context-menu-heading' }
927       , edit:    { name: kivi.t8('Edit'), icon: "edit", callback: kivi.requirement_spec.standard_additional_parts_ajax_call }
928     }, general_actions)
929   });
930
931   var additional_parts_actions = {
932       save:           { name: kivi.t8('Save'),               icon: "save",  callback: kivi.requirement_spec.standard_additional_parts_ajax_call }
933     , save_keep_open: { name: kivi.t8('Save and keep open'), icon: "save",  callback: kivi.requirement_spec.standard_additional_parts_ajax_call }
934     , cancel:         { name: kivi.t8('Cancel'),             icon: "close",  callback: kivi.requirement_spec.cancel_edit_additional_parts_form }
935   };
936
937   $.contextMenu({
938     selector: '.edit-additional-parts-context-menu',
939     items:    $.extend({
940         heading:        { name: kivi.t8('Additional articles actions'), className: 'context-menu-heading' }
941     }, additional_parts_actions, general_actions)
942   });
943
944   $.contextMenu({
945     selector: '.edit-additional-parts-row-context-menu',
946     items:    $.extend({
947         heading:        { name: kivi.t8('Additional articles actions'), className: 'context-menu-heading' }
948       , delete:         { name: kivi.t8('Remove article'),     icon: "delete", callback: kivi.requirement_spec.delete_additional_part }
949     }, additional_parts_actions, general_actions)
950   });
951
952   $.contextMenu({
953     selector: '.quotations-and-orders-context-menu,.quotations-and-orders-order-context-menu',
954     items:    $.extend({
955         heading:            { name: kivi.t8('Quotations/Orders actions'), className: 'context-menu-heading'                                                                                            }
956       , edit_assignment:    { name: kivi.t8('Edit article/section assignments'), icon: "edit",   callback: ns.standard_quotation_order_ajax_call                                                       }
957       , sep1:               "---------"
958       , new:                { name: kivi.t8('Create new qutoation/order'),       icon: "add",    callback: ns.standard_quotation_order_ajax_call, disabled: ns.disable_create_quotation_order_commands }
959       , update:             { name: kivi.t8('Update quotation/order'),           icon: "update", callback: ns.standard_quotation_order_ajax_call, disabled: ns.disable_edit_quotation_order_commands   }
960       , sep2:               "---------"
961       , delete:             { name: kivi.t8('Delete quotation/order'),           icon: "delete", callback: ns.ask_delete_quotation_order,         disabled: ns.disable_edit_quotation_order_commands   }
962     }, general_actions)
963   });
964
965   $.contextMenu({
966     selector: '.quotations-and-orders-edit-assignment-context-menu',
967     items:    $.extend({
968         heading:         { name: kivi.t8('Edit article/section assignments'), className: 'context-menu-heading'    }
969       , save_assignment: { name: kivi.t8('Save'),   icon: "edit",  callback: ns.standard_quotation_order_ajax_call }
970       , cancel:          { name: kivi.t8('Cancel'), icon: "close", callback: ns.standard_quotation_order_ajax_call }
971     }, general_actions)
972   });
973
974   $.contextMenu({
975     selector: '.quotations-and-orders-new-context-menu',
976     items:    $.extend({
977         heading: { name: kivi.t8('Create new quotation/order'), className: 'context-menu-heading'          }
978       , create:  { name: kivi.t8('Create'), icon: "edit",  callback: ns.standard_quotation_order_ajax_call }
979       , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: ns.standard_quotation_order_ajax_call }
980     }, general_actions)
981   });
982
983   $.contextMenu({
984     selector: '.quotations-and-orders-update-context-menu',
985     items:    $.extend({
986         heading:   { name: kivi.t8('Update quotation/order'), className: 'context-menu-heading'               }
987       , do_update: { name: kivi.t8('Update'), icon: "update", callback: ns.standard_quotation_order_ajax_call }
988       , cancel:    { name: kivi.t8('Cancel'), icon: "close",  callback: ns.standard_quotation_order_ajax_call }
989     }, general_actions)
990   });
991
992   $.contextMenu({
993     selector: '#content',
994     items:    general_actions
995   });
996
997   var events = {
998       show: kivi.requirement_spec.item_popup_menu_shown
999     , hide: kivi.requirement_spec.item_popup_menu_hidden
1000   };
1001
1002   $.contextMenu({
1003     selector: '.section-context-menu',
1004     events:   events,
1005     items:    $.extend({
1006         heading:            { name: kivi.t8('Section/Function block actions'), className: 'context-menu-heading' }
1007       , add_section:        { name: kivi.t8('Add section'),        icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
1008       , 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 }
1009       , sep1:               "---------"
1010       , edit:               { name: kivi.t8('Edit'),               icon: "edit",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
1011       , delete:             { name: kivi.t8('Delete'),             icon: "delete", callback: kivi.requirement_spec.ask_delete_item,         disabled: kivi.requirement_spec.disable_edit_item_commands }
1012       , sep2:               "---------"
1013       , flag:               { name: kivi.t8('Toggle marker'),      icon: "flag",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
1014       , sep3:               "---------"
1015       , copy:               { name: kivi.t8('Copy'),               icon: "copy",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
1016       , paste:              { name: kivi.t8('Paste'),              icon: "paste",  callback: kivi.requirement_spec.standard_item_ajax_call }
1017     }, general_actions)
1018   });
1019
1020   $.contextMenu({
1021     selector: '.function-block-context-menu,.sub-function-block-context-menu',
1022     events:   events,
1023     items:    $.extend({
1024         heading:                { name: kivi.t8('Function block actions'), className: 'context-menu-heading' }
1025       , add_function_block:     { name: kivi.t8('Add function block'),     icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
1026       , add_sub_function_block: { name: kivi.t8('Add sub function block'), icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
1027       , sep1:                   "---------"
1028       , edit:                   { name: kivi.t8('Edit'),                   icon: "edit",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
1029       , delete:                 { name: kivi.t8('Delete'),                 icon: "delete", callback: kivi.requirement_spec.ask_delete_item,         disabled: kivi.requirement_spec.disable_edit_item_commands }
1030       , sep2:                   "---------"
1031       , flag:                   { name: kivi.t8('Toggle marker'),          icon: "flag",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
1032       , sep3:                   "---------"
1033       , copy:                   { name: kivi.t8('Copy'),                   icon: "copy",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
1034       , paste:                  { name: kivi.t8('Paste'),                  icon: "paste",  callback: kivi.requirement_spec.standard_item_ajax_call }
1035     }, general_actions)
1036   });
1037 };
1038
1039 });                             // end of namespace(...., function() {...