1 /* Functions used for the requirement specs */
 
   3 namespace("kivi.requirement_spec", function(ns) {
 
   5 // -----------------------------------------------------------------------------
 
   6 // ------------------------------ basic settings -------------------------------
 
   7 // -----------------------------------------------------------------------------
 
   9 ns.basic_settings_customer_changed = function(customer_ctrl, value_ctrl) {
 
  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); }
 
  17 // -----------------------------------------------------------------------------
 
  18 // ------------------------------ the tree itself ------------------------------
 
  19 // -----------------------------------------------------------------------------
 
  21 ns.tree_check_move = function(data) {
 
  22   var dragged_type = data.o.data('type');
 
  23   var dropped_type = data.r.data('type');
 
  25   // console.debug("dragged " + dragged_type + " dropped " + dropped_type + " dir " + data.p);
 
  27   if ((dragged_type == "sections") || (dragged_type == "text-blocks-front") || (dragged_type == "text-blocks-back"))
 
  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");
 
  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");
 
  48   // dragged_type == (sub) function blocks
 
  49   if ((dropped_type == "text-block") || (dropped_type == "text-blocks-front") || (dropped_type == "text-blocks-back"))
 
  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"))
 
  56   var dragged_depth = 1 + data.o.children('ul').size();
 
  58   // console.debug("dropped_depth " + dropped_depth + " dragged_depth " + dragged_depth);
 
  60   return (2 <= dropped_depth) && ((dragged_depth + dropped_depth) <= 4);
 
  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";
 
  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"),
 
  76     current_content_type: $('#current_content_type').val(),
 
  77     current_content_id:   $('#current_content_id').val()
 
  79   // console.debug("controller: " + controller);
 
  80   // console.debug(data);
 
  82   $.post("controller.pl", data, kivi.eval_json_result);
 
  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;
 
  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(),
 
 101     clicked_id:           node.data('id')
 
 102   }, kivi.eval_json_result);
 
 105 ns.setup_tooltip_for_tree_node = function(li) {
 
 106   $(li).find('a').prop('title', $(li).prop('title')).tooltip();
 
 107   $(li).prop('title', '');
 
 110 ns.tree_loaded = function(event) {
 
 111   var id = $('#tree').data('initially_selected_node');
 
 113     $.jstree._reference("#tree").select_node(id, true);
 
 115   $('#tree li[title!=""]').each(function(idx, elt) {
 
 116     ns.setup_tooltip_for_tree_node(elt);
 
 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);
 
 126 ns.initialize_requirement_spec = function(data) {
 
 127   $('#tree').data('initially_selected_node', data.initially_selected_node);
 
 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)
 
 137         initially_open: data.initially_open,
 
 144           check_move: ns.tree_check_move,
 
 149         theme: "requirement-spec"
 
 151       plugins: [ "themes", "json_data", "ui", "crrm", "dnd" ]
 
 154   ns.create_context_menus(data.is_template);
 
 155   $('#requirement_spec_tabs').on("tabsbeforeactivate", ns.tabs_before_activate);
 
 157   ns.time_based_units = data.time_based_units;
 
 160 // -------------------------------------------------------------------------
 
 161 // ------------------------------ text blocks ------------------------------
 
 162 // -------------------------------------------------------------------------
 
 164 ns.find_text_block_id = function(clicked_elt) {
 
 165   var id = $(clicked_elt).attr('id');
 
 167   if (/^text-block-picture-\d+$/.test(id))
 
 168     id = $(clicked_elt).closest('.text-block-context-menu').attr('id');
 
 170   // console.log("id: " + id);
 
 171   if (/^text-block-\d+$/.test(id)) {
 
 172     // console.log("find_text_block_id: case 1: " + id.substr(11));
 
 173     return id.substr(11) * 1;
 
 176   id = $(clicked_elt).closest("[id*=text-block-]").attr('id')
 
 177   if (/^text-block-\d+$/.test(id)) {
 
 178     // console.log("find_text_block_id: case 2: " + id.substr(11));
 
 179     return id.substr(11) * 1;
 
 182   id = $(clicked_elt).closest("[id*=tb-]").attr('id')
 
 183   if (/^tb-\d+$/.test(id)) {
 
 184     // console.log("find_text_block_id: case 3: " + id.substr(3));
 
 185     return id.substr(3) * 1;
 
 188   // console.log("find_text_block_id: case undef, id: " + id);
 
 192 ns.find_text_block_output_position = function(clicked_elt) {
 
 193   var output_position = $(clicked_elt).closest('#text-block-list-container').find('#text_block_output_position').val();
 
 195     return output_position;
 
 197   var type = $(clicked_elt).closest('#tb-back,#tb-front').data('type') || $('#current_content_type').val();
 
 198   if (/^text-blocks-(front|back)/.test(type))
 
 199     return type == "text-blocks-front" ? 0 : 1;
 
 204 ns.disable_edit_text_block_commands = function(key, opt) {
 
 205   return ns.find_text_block_id(opt.$trigger) == undefined;
 
 208 ns.standard_text_block_ajax_call = function(key, opt, other_data) {
 
 210     action:               "RequirementSpecTextBlock/ajax_" + key,
 
 211     requirement_spec_id:  $('#requirement_spec_id').val(),
 
 212     id:                   ns.find_text_block_id(opt.$trigger),
 
 213     output_position:      ns.find_text_block_output_position(opt.$trigger),
 
 214     current_content_type: $('#current_content_type').val(),
 
 215     current_content_id:   $('#current_content_id').val()
 
 218   $.post("controller.pl", $.extend(data, other_data || {}), kivi.eval_json_result);
 
 223 ns.cancel_edit_text_block_form = function(id_base) {
 
 224   var id = $('#' + id_base + '_id').val();
 
 225   $('#' + id_base + '_form').remove();
 
 227     $('#text-block-' + id).show();
 
 230 ns.ask_delete_text_block = function(key, opt) {
 
 231   if (confirm(kivi.t8("Are you sure?")))
 
 232     ns.standard_text_block_ajax_call(key, opt);
 
 236 ns.find_text_block_picture_id = function(clicked_elt) {
 
 237   var id    = $(clicked_elt).attr('id');
 
 238   var match = id.match(/^text-block-picture-(\d+)$/);
 
 245 ns.add_edit_text_block_picture_ajax_call = function(key, opt) {
 
 246   var title = key == 'add_picture' ? kivi.t8('Add picture to text block') : kivi.t8('Edit picture');
 
 248   kivi.popup_dialog({ url:    'controller.pl',
 
 249                       data:   { action:     'RequirementSpecTextBlock/ajax_' + key,
 
 250                                 id:         ns.find_text_block_id(opt.$trigger),
 
 251                                 picture_id: ns.find_text_block_picture_id(opt.$trigger) },
 
 252                       dialog: { title:      title }});
 
 257 ns.standard_text_block_picture_ajax_call = function(key, opt) {
 
 259       action: "RequirementSpecTextBlock/ajax_" + key
 
 260     , id:     ns.find_text_block_picture_id(opt.$trigger)
 
 263   if (key == 'download_picture')
 
 264     $.download("controller.pl", data);
 
 266     $.post("controller.pl", data, kivi.eval_json_result);
 
 271 ns.ask_delete_text_block_picture = function(key, opt) {
 
 272   if (confirm(kivi.t8("Are you sure?")))
 
 273     ns.standard_text_block_picture_ajax_call(key, opt);
 
 277 ns.handle_text_block_picture_popup_menu_markings = function(opt, add) {
 
 278   var id = ns.find_text_block_picture_id(opt.$trigger);
 
 280     $('#text-block-picture-' + id ).toggleClass('selected', add);
 
 284 ns.text_block_picture_popup_menu_shown = function(opt) {
 
 285   return ns.handle_text_block_picture_popup_menu_markings(opt, true);
 
 288 ns.text_block_picture_popup_menu_hidden = function(opt) {
 
 289   return ns.handle_text_block_picture_popup_menu_markings(opt, false);
 
 292 ns.make_text_block_picture_lists_sortable = function() {
 
 293   kivi.run_once_for(".requirement-spec-text-block-picture-list", 'make-text-block-picture-list-sortable', function($elt) {
 
 295       stop: function(event, ui) {
 
 296         $.post('controller.pl?action=RequirementSpecTextBlock/reorder_pictures', {
 
 297           'picture_id[]': $($elt.sortable('toArray')).map(function(idx, str) { return str.replace('text-block-picture-', ''); }).toArray()
 
 306 // --------------------------------------------------------------------------------
 
 307 // ------------------------------ sections and items ------------------------------
 
 308 // --------------------------------------------------------------------------------
 
 310 ns.find_item_id = function(clicked_elt) {
 
 311   // console.log("clicked id: " + $(clicked_elt).attr('id'));
 
 312   var id     = $(clicked_elt).attr('id');
 
 313   var result = /^(function-block|function-block-content|sub-function-block|sub-function-block-content|section|section-header)-(\d+)$/.exec(id);
 
 315     // console.log("find_item_id: case 1: " + result[2]);
 
 319   id = $(clicked_elt).closest("[id*=fb-]").attr('id')
 
 320   if (/^fb-\d+$/.test(id)) {
 
 321     // console.log("find_item_id: case 2: " + id.substr(3));
 
 322     return id.substr(3) * 1;
 
 325   // console.log("find_item_id: case undef");
 
 329 ns.standard_item_ajax_call = function(key, opt, other_data) {
 
 331     action:               "RequirementSpecItem/ajax_" + key,
 
 332     requirement_spec_id:  $('#requirement_spec_id').val(),
 
 333     id:                   ns.find_item_id(opt.$trigger),
 
 334     current_content_type: $('#current_content_type').val(),
 
 335     current_content_id:   $('#current_content_id').val()
 
 338   // console.log("I would normally POST the following now:");
 
 339   // console.log(data);
 
 340   $.post("controller.pl", $.extend(data, other_data || {}), kivi.eval_json_result);
 
 345 ns.disable_edit_item_commands = function(key, opt) {
 
 346   return ns.find_item_id(opt.$trigger) == undefined;
 
 349 ns.disable_add_function_block_command = function(key, opt) {
 
 350   return opt.$trigger.attr('id') == "sections";
 
 353 ns.cancel_edit_item_form = function(form_id_base, options) {
 
 354   $('#' + form_id_base + '_form').remove();
 
 358     $(options.to_show).show();
 
 359   if (options.to_hide_if_empty && (1 == $(options.to_hide_if_empty).children().size()))
 
 360     $(options.to_hide_if_empty).hide();
 
 363 ns.ask_delete_item = function(key, opt) {
 
 364   if (confirm(kivi.t8("Are you sure?")))
 
 365     ns.standard_item_ajax_call(key, opt);
 
 369 ns.handle_text_block_popup_menu_markings = function(opt, add) {
 
 370   var id = ns.find_text_block_id(opt.$trigger);
 
 372     $('#text-block-' + id).toggleClass('selected', add);
 
 376 ns.text_block_popup_menu_shown = function(opt) {
 
 377   return ns.handle_text_block_popup_menu_markings(opt, true);
 
 380 ns.text_block_popup_menu_hidden = function(opt) {
 
 381   return ns.handle_text_block_popup_menu_markings(opt, false);
 
 384 ns.handle_item_popup_menu_markings = function(opt, add) {
 
 385   var id = ns.find_item_id(opt.$trigger);
 
 387     $('#section-' + id + ',#function-block-' + id + ',#sub-function-block-' + id).toggleClass('selected', add);
 
 391 ns.item_popup_menu_shown = function(opt) {
 
 392   return ns.handle_item_popup_menu_markings(opt, true);
 
 395 ns.item_popup_menu_hidden = function(opt) {
 
 396   return ns.handle_item_popup_menu_markings(opt, false);
 
 399 ns.submit_function_block = function(event, shift_in_out) {
 
 400   event.preventDefault();
 
 402   var prefix = $(this).attr('id').match("^(?:edit|new)_function_block_[\\d_]+\\d")[0];
 
 403   var action = $('#' + prefix + '_id').val() ? 'update' : 'create';
 
 404   kivi.submit_ajax_form('controller.pl?action=RequirementSpecItem/ajax_' + action, '#' + prefix + '_form', { shift_in_out: !!shift_in_out });
 
 409 ns.init_function_block_keypress_events = function(form_id) {
 
 410   $("#" + form_id + " INPUT[type=text]").bind("keypress", "return", ns.submit_function_block);
 
 412   $('#' + form_id + ' INPUT[type=text],#' + form_id + ' TEXTAREA,#' + form_id + ' INPUT[type=button]')
 
 413     .bind('keypress', 'shift+return', function(event) { return ns.submit_function_block.apply(this, [ event, true ]); });
 
 416 // -------------------------------------------------------------------------
 
 417 // ------------------------------- templates -------------------------------
 
 418 // -------------------------------------------------------------------------
 
 420 ns.paste_template = function(key, opt, other_data) {
 
 421   kivi.popup_dialog({ url: 'controller.pl?action=RequirementSpec/select_template_to_paste', dialog: { title: kivi.t8("Select template to paste") } });
 
 424 ns.paste_selected_template = function(template_id) {
 
 425   $('#jqueryui_popup_dialog').dialog("close");
 
 428     action:               "RequirementSpec/paste_template",
 
 429     id:                   $('#requirement_spec_id').val(),
 
 430     template_id:          template_id,
 
 431     current_content_type: $('#current_content_type').val(),
 
 432     current_content_id:   $('#current_content_id').val()
 
 435   // console.log("I would normally POST the following now:");
 
 436   // console.log(data);
 
 437   $.post("controller.pl", data, kivi.eval_json_result);
 
 442 // -------------------------------------------------------------------------
 
 443 // ---------------------------- basic settings -----------------------------
 
 444 // -------------------------------------------------------------------------
 
 445 ns.standard_basic_settings_ajax_call = function(key, opt) {
 
 446   if (key == 'cancel') {
 
 447     if (confirm(kivi.t8('Do you really want to cancel?'))) {
 
 448       $('#basic_settings').show();
 
 449       $('#basic_settings_form,#project_link_form').remove();
 
 453   } else if (key == 'save') {
 
 454     $('#basic_settings_form_submit').click();
 
 458   var data = 'action=RequirementSpec/ajax_' + key + '&id=' + encodeURIComponent($('#requirement_spec_id').val());
 
 460   $.post("controller.pl", data, kivi.eval_json_result);
 
 465 // -------------------------------------------------------------------------
 
 466 // -------------------------- time/cost estimate ---------------------------
 
 467 // -------------------------------------------------------------------------
 
 469 ns.standard_time_cost_estimate_ajax_call = function(key, opt) {
 
 470   if (key == 'cancel') {
 
 471     if (confirm(kivi.t8('Do you really want to cancel?'))) {
 
 472       $('#time_cost_estimate').show();
 
 473       $('#time_cost_estimate_form_container').remove();
 
 479   if (key == 'save_keep_open') {
 
 481     add_data = 'keep_open=1&';
 
 484   var data = "action=RequirementSpec/ajax_" + key + "_time_and_cost_estimate&" + add_data;
 
 487     data += $('#edit_time_cost_estimate_form').serialize()
 
 488          +  '&' + $('#current_content_type').serialize()
 
 489          +  '&' + $('#current_content_id').serialize();
 
 491     data += 'id=' + encodeURIComponent($('#requirement_spec_id').val());
 
 493   $.post("controller.pl", data, kivi.eval_json_result);
 
 498 ns.time_cost_estimate_input_key_down = function(event) {
 
 499   if(event.keyCode == 13) {
 
 500     event.preventDefault();
 
 501     ns.standard_time_cost_estimate_ajax_call('save');
 
 506 // -------------------------------------------------------------------------
 
 507 // --------------------------- quotations/orders ---------------------------
 
 508 // -------------------------------------------------------------------------
 
 510 ns.find_quotation_order_id = function(clicked_elt) {
 
 511   return $(clicked_elt).find('>[name=order_id]').val();
 
 514 ns.standard_quotation_order_ajax_call = function(key, opt) {
 
 515   if (key == 'cancel') {
 
 516     if (confirm(kivi.t8('Do you really want to cancel?'))) {
 
 517       $('#quotations_and_orders').show();
 
 518       $('#quotations_and_orders_article_assignment,#quotations_and_orders_new,#quotations_and_orders_update').remove();
 
 523   else if ((key == 'create') && $('#quotations_and_orders_form INPUT[name="sections[].order_part_id"]').filter(function(idx, elt) { return ($(elt).val() || '') == '' }).size()) {
 
 524     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.'));
 
 528   var data = 'action=RequirementSpecOrder/' + key
 
 529            + '&' + $('#requirement_spec_id').serialize();
 
 531   if ((key == 'save_assignment') || (key == 'create') || (key == 'do_update'))
 
 532     data += '&' + $('#quotations_and_orders_form').serialize();
 
 533   else if ((key == 'update') || (key == 'delete'))
 
 534     data += '&rs_order_id=' + encodeURIComponent(ns.find_quotation_order_id(opt.$trigger));
 
 536   // console.log("I would normally POST the following now:");
 
 537   // console.log(data);
 
 538   $.post("controller.pl", data, kivi.eval_json_result);
 
 543 ns.ask_delete_quotation_order = function(key, opt) {
 
 544   if (confirm(kivi.t8("Are you sure?")))
 
 545     ns.standard_quotation_order_ajax_call(key, opt);
 
 549 ns.disable_edit_quotation_order_commands = function(key, opt) {
 
 550   return ns.find_quotation_order_id(opt.$trigger) == undefined;
 
 553 ns.disable_create_quotation_order_commands = function(key, opt) {
 
 554   return !$('#quotations_and_orders_sections');
 
 557 ns.assign_order_part_id_to_all = function() {
 
 558   var order_part_id   = $('#quotations_and_orders_order_id').val();
 
 559   var order_part_name = $('#quotations_and_orders_order_id_name').val();
 
 561   $('#quotations_and_orders_form INPUT[name="sections[].order_part_id"]').each(function(idx, elt) {
 
 562     $(elt).val(order_part_id);
 
 565   $('#quotations_and_orders_form [id^=quotations_and_orders_sections_order_pard_id_]').filter(function() {
 
 566     return $(this).attr('id') && $(this).attr('id').match("^quotations_and_orders_sections_order_pard_id_[0-9]+_name$");
 
 567   }).each(function(idx, elt) {
 
 568     $(elt).val(order_part_name);
 
 571   var unit = $('#quotations_and_orders_order_id').closest('td').data('unit');
 
 572   var text = ns.time_based_units[unit] ? kivi.t8("time and effort based position") : kivi.t8("flat-rate position");
 
 574   $('#quotations_and_orders_form [data-unit-column=1]').html(unit);
 
 575   $('#quotations_and_orders_form [data-position-type-column=1]').html(text);
 
 578 ns.assign_order_part_on_part_picked = function(event, item) {
 
 579   if (!item || !item.unit)
 
 583       id   = $elt.prop('id');
 
 585   if (id == 'quotations_and_orders_order_id')
 
 586     $elt.closest('td').data('unit', item.unit);
 
 589     var $tr  = $elt.closest('tr');
 
 590     var text = ns.time_based_units[item.unit] ? kivi.t8("time and effort based position") : kivi.t8("flat-rate position");
 
 592     $tr.find('[data-unit-column=1]').html(item.unit);
 
 593     $tr.find('[data-position-type-column=1]').html(text);
 
 597 // -------------------------------------------------------------------------
 
 598 // ---------------------------- general actions ----------------------------
 
 599 // -------------------------------------------------------------------------
 
 601 ns.create_reqspec_pdf = function(key, opt) {
 
 603     action: "RequirementSpec/create_pdf",
 
 604     id:     $('#requirement_spec_id').val()
 
 606   $.download("controller.pl", data);
 
 609 ns.copy_reqspec = function(key, opt) {
 
 610   window.location.href = "controller.pl?action=RequirementSpec/new©_source_id=" + encodeURIComponent($('#requirement_spec_id').val());
 
 614 ns.delete_reqspec = function(key, opt) {
 
 615   if (confirm(kivi.t8("Are you sure?")))
 
 616     window.location.href = "controller.pl?action=RequirementSpec/destroy&id=" + encodeURIComponent($('#requirement_spec_id').val());
 
 620 ns.disable_commands = function(key, opt) {
 
 621   if (key === "create_version")
 
 622     return ($('#current_version_id').val() || '') == '' ? false : true;
 
 626 // -------------------------------------------------------------------------
 
 627 // -------------------------------- versions -------------------------------
 
 628 // -------------------------------------------------------------------------
 
 630 ns.find_versioned_copy_id = function(clicked_elt) {
 
 631   var id = $(clicked_elt).find("[name=versioned_copy_id]");
 
 632   return id ? id.val() : undefined;
 
 635 ns.disable_versioned_copy_item_commands = function(key, opt) {
 
 636   if (key === "revert_to_version")
 
 637     return !ns.find_versioned_copy_id(opt.$trigger);
 
 641 ns.create_version = function() {
 
 642   kivi.popup_dialog({ url:    'controller.pl',
 
 643                       data:   { action:              'RequirementSpecVersion/new',
 
 644                                 requirement_spec_id: $('#requirement_spec_id').val() },
 
 645                       dialog: { title: kivi.t8('Create a new version') }});
 
 649 ns.create_pdf_for_versioned_copy_ajax_call = function(key, opt) {
 
 651     action: "RequirementSpec/create_pdf",
 
 652     id:     ns.find_versioned_copy_id(opt.$trigger)
 
 654   $.download("controller.pl", data);
 
 659 ns.revert_to_versioned_copy_ajax_call = function(key, opt) {
 
 660   if (!confirm(kivi.t8('Do you really want to revert to this version?')))
 
 664     action:            'RequirementSpec/revert_to',
 
 665     versioned_copy_id: ns.find_versioned_copy_id(opt.$trigger),
 
 666     id:                $('#requirement_spec_id').val()
 
 669   $.post("controller.pl", data, kivi.eval_json_result);
 
 674 // -------------------------------------------------------------------------
 
 675 // -------------------------- time/cost estimate ---------------------------
 
 676 // -------------------------------------------------------------------------
 
 678 ns.standard_time_cost_estimate_ajax_call = function(key, opt) {
 
 679   if (key == 'cancel') {
 
 680     if (confirm(kivi.t8('Do you really want to cancel?'))) {
 
 681       $('#time_cost_estimate').show();
 
 682       $('#time_cost_estimate_form_container').remove();
 
 688   if (key == 'save_keep_open') {
 
 690     add_data = 'keep_open=1&';
 
 693   var data = "action=RequirementSpec/ajax_" + key + "_time_and_cost_estimate&" + add_data;
 
 696     data += $('#edit_time_cost_estimate_form').serialize()
 
 697          +  '&' + $('#current_content_type').serialize()
 
 698          +  '&' + $('#current_content_id').serialize();
 
 700     data += 'id=' + encodeURIComponent($('#requirement_spec_id').val());
 
 702   $.post("controller.pl", data, kivi.eval_json_result);
 
 707 ns.time_cost_estimate_input_key_down = function(event) {
 
 708   if(event.keyCode == 13) {
 
 709     event.preventDefault();
 
 710     ns.standard_time_cost_estimate_ajax_call('save');
 
 715 // -------------------------------------------------------------------------
 
 716 // -------------------------- additional parts -----------------------------
 
 717 // -------------------------------------------------------------------------
 
 719 ns.standard_additional_parts_ajax_call = function(key, opt) {
 
 721   if (key == 'save_keep_open') {
 
 723     add_data = 'keep_open=1&';
 
 726   var data = "action=RequirementSpecPart/ajax_" + key + "&" + add_data + 'requirement_spec_id=' + encodeURIComponent($('#requirement_spec_id').val()) + '&';
 
 729     data += $('#edit_additional_parts_form').serialize();
 
 731   $.post("controller.pl", data, kivi.eval_json_result);
 
 736 ns.prepare_edit_additional_parts_form = function() {
 
 737   $("#edit_additional_parts_list tbody").sortable({
 
 740     helper:   function(event, ui) {
 
 741       ui.children().each(function() {
 
 742         $(this).width($(this).width());
 
 750 ns.cancel_edit_additional_parts_form = function() {
 
 751   if (confirm(kivi.t8('Do you really want to cancel?'))) {
 
 752     $('#additional_parts_list_container').show();
 
 753     $('#additional_parts_form_container').remove();
 
 758 ns.additional_parts_input_key_down = function(event) {
 
 759   if(event.keyCode == 13) {
 
 760     event.preventDefault();
 
 761     ns.standard_additional_parts_ajax_call('save');
 
 766 ns.add_additional_part = function() {
 
 767   var part_id = $('#additional_parts_add_part_id').val();
 
 768   if (!part_id || (part_id == ''))
 
 771   var rspec_id = $('#requirement_spec_id').val();
 
 772   var data     = 'action=RequirementSpecPart/ajax_add&requirement_spec_id=' + encodeURIComponent(rspec_id) + '&part_id=' + encodeURIComponent(part_id);
 
 774   $.post("controller.pl", data, kivi.eval_json_result);
 
 779 ns.delete_additional_part = function(key, opt) {
 
 780   opt.$trigger.remove();
 
 781   if (!$('#edit_additional_parts_list tbody tr').size()) {
 
 782    $('#edit_additional_parts_list_empty').show();
 
 783    $('#edit_additional_parts_list').hide();
 
 789 // -------------------------------------------------------------------------
 
 790 // ------------------------------- tab widget ------------------------------
 
 791 // -------------------------------------------------------------------------
 
 792 var content_div_ids_for_tab_headers = {
 
 793     'tab-header-function-block':     'function-blocks-tab'
 
 794   , 'tab-header-basic-settings':     'ui-tabs-1'
 
 795   , 'tab-header-time-cost-estimate': 'ui-tabs-2'
 
 796   , 'tab-header-additional-parts':   'ui-tabs-3'
 
 797   , 'tab-header-versions':           'ui-tabs-4'
 
 798   , 'tab-header-quotations-orders':  'ui-tabs-5'
 
 801 ns.tabs_before_activate = function(event, ui) {
 
 805   var content_div_id = content_div_ids_for_tab_headers[ $(ui.oldTab).attr('id') ];
 
 806   if (!content_div_id || (content_div_id == 'function-blocks-tab'))
 
 809   var inputs = $('#' + content_div_id).find('input,select,textarea').filter('[type!=hidden]');
 
 810   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.")))
 
 813   var new_focus = $(inputs).filter(':focusable')[0];
 
 815     $(new_focus).focus();
 
 820 // -------------------------------------------------------------------------
 
 821 // ----------------------------- context menus -----------------------------
 
 822 // -------------------------------------------------------------------------
 
 824 ns.create_context_menus = function(is_template) {
 
 826     var general_actions = {
 
 828       , general_actions: { name: kivi.t8('Requirement spec template actions'), className: 'context-menu-heading' }
 
 829       // , sep99:           "---------"
 
 830       , copy_reqspec:    { name: kivi.t8('Copy template'),   icon: "copy",   callback: kivi.requirement_spec.copy_reqspec   }
 
 831       , delete_reqspec:  { name: kivi.t8('Delete template'), icon: "delete", callback: kivi.requirement_spec.delete_reqspec }
 
 835       selector: '.basic-settings-context-menu',
 
 837           heading: { name: kivi.t8('Basic settings actions'), className: 'context-menu-heading' }
 
 838         , edit:    { name: kivi.t8('Edit'), icon: "edit", callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
 
 842   } else {                      // if (is_template)
 
 843     var general_actions = {
 
 845       , general_actions:    { name: kivi.t8('Requirement spec actions'), className: 'context-menu-heading' }
 
 846       , create_pdf:         { name: kivi.t8('Create PDF'),              icon: "pdf",    callback: kivi.requirement_spec.create_reqspec_pdf }
 
 847       , create_version:     { name: kivi.t8('Create new version'),      icon: "new",    callback: kivi.requirement_spec.create_version, disabled: kivi.requirement_spec.disable_commands }
 
 848       , copy_reqspec:       { name: kivi.t8('Copy requirement spec'),   icon: "copy",   callback: kivi.requirement_spec.copy_reqspec   }
 
 849       , delete_reqspec:     { name: kivi.t8('Delete requirement spec'), icon: "delete", callback: kivi.requirement_spec.delete_reqspec }
 
 850       , sep_paste_template: "---------"
 
 851       , paste_template:     { name: kivi.t8('Paste template'),     icon: "paste",  callback: kivi.requirement_spec.paste_template }
 
 855       selector: '.versioned-copy-context-menu',
 
 857           heading:            { name: kivi.t8('Version actions'), className: 'context-menu-heading' }
 
 858         , create_version_pdf: { name: kivi.t8('Create PDF'),        icon: "pdf",    callback: kivi.requirement_spec.create_pdf_for_versioned_copy_ajax_call                                                                      }
 
 859         , 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 }
 
 864       selector: '.basic-settings-context-menu',
 
 866           heading:           { name: kivi.t8('Basic settings actions'), className: 'context-menu-heading' }
 
 867         , edit:              { name: kivi.t8('Edit'),              icon: "edit", callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
 
 868         , edit_project_link: { name: kivi.t8('Edit project link'),               callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
 
 873       selector: '.edit-project-link-context-menu',
 
 875           heading: { name: kivi.t8('Project link actions'), className: 'context-menu-heading' }
 
 876         , save:    { name: kivi.t8('Save'),   icon: "save",  callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
 
 877         , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
 
 881     var paste_template_actions = {
 
 883   }                             // if (is_template) ... else ...
 
 886     selector: '.text-block-context-menu',
 
 888         show: kivi.requirement_spec.text_block_popup_menu_shown
 
 889       , hide: kivi.requirement_spec.text_block_popup_menu_hidden
 
 892         heading: { name: kivi.t8('Text block actions'),    className: 'context-menu-heading' }
 
 893       , add:     { name: kivi.t8('Add text block'),        icon: "add",    callback: kivi.requirement_spec.standard_text_block_ajax_call }
 
 894       , 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 }
 
 895       , 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 }
 
 896       , 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 }
 
 898       , 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 }
 
 900       , 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 }
 
 901       , paste:   { name: kivi.t8('Paste'),                 icon: "paste",  callback: kivi.requirement_spec.standard_text_block_ajax_call  }
 
 906     selector: '.text-block-picture-context-menu',
 
 908         show: kivi.requirement_spec.text_block_picture_popup_menu_shown
 
 909       , hide: kivi.requirement_spec.text_block_picture_popup_menu_hidden
 
 912         heading:          { name: kivi.t8('Text block picture actions'), className: 'context-menu-heading'                                                 }
 
 913       , add_picture:      { name: kivi.t8('Add picture'),      icon: "add-picture", callback: kivi.requirement_spec.add_edit_text_block_picture_ajax_call  }
 
 914       , edit_picture:     { name: kivi.t8('Edit picture'),     icon: "edit",        callback: kivi.requirement_spec.add_edit_text_block_picture_ajax_call  }
 
 915       , delete_picture:   { name: kivi.t8('Delete picture'),   icon: "delete",      callback: kivi.requirement_spec.ask_delete_text_block_picture          }
 
 916       , download_picture: { name: kivi.t8('Download picture'), icon: "download",    callback: kivi.requirement_spec.standard_text_block_picture_ajax_call  }
 
 918       , copy_picture:     { name: kivi.t8('Copy'),             icon: "copy",        callback: kivi.requirement_spec.standard_text_block_picture_ajax_call  }
 
 919       , paste_picture:    { name: kivi.t8('Paste'),            icon: "paste",       callback: kivi.requirement_spec.standard_text_block_picture_ajax_call  }
 
 924     selector: '.edit-basic-settings-context-menu',
 
 926         heading: { name: kivi.t8('Basic settings actions'), className: 'context-menu-heading' }
 
 927       , save:    { name: kivi.t8('Save'),   icon: "save",  callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
 
 928       , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: kivi.requirement_spec.standard_basic_settings_ajax_call }
 
 933     selector: '.time-cost-estimate-context-menu',
 
 935         heading: { name: kivi.t8('Time/cost estimate actions'), className: 'context-menu-heading' }
 
 936       , edit:    { name: kivi.t8('Edit'), icon: "edit", callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
 
 941     selector: '.edit-time-cost-estimate-context-menu',
 
 943         heading: { name: kivi.t8('Time/cost estimate actions'), className: 'context-menu-heading' }
 
 944       , save:    { name: kivi.t8('Save'),   icon: "save",  callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
 
 945       , save_keep_open: { name: kivi.t8('Save and keep open'), icon: "save", callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
 
 946       , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: kivi.requirement_spec.standard_time_cost_estimate_ajax_call }
 
 951     selector: '.additional-parts-context-menu',
 
 953         heading: { name: kivi.t8('Additional articles actions'), className: 'context-menu-heading' }
 
 954       , edit:    { name: kivi.t8('Edit'), icon: "edit", callback: kivi.requirement_spec.standard_additional_parts_ajax_call }
 
 958   var additional_parts_actions = {
 
 959       save:           { name: kivi.t8('Save'),               icon: "save",  callback: kivi.requirement_spec.standard_additional_parts_ajax_call }
 
 960     , save_keep_open: { name: kivi.t8('Save and keep open'), icon: "save",  callback: kivi.requirement_spec.standard_additional_parts_ajax_call }
 
 961     , cancel:         { name: kivi.t8('Cancel'),             icon: "close",  callback: kivi.requirement_spec.cancel_edit_additional_parts_form }
 
 965     selector: '.edit-additional-parts-context-menu',
 
 967         heading:        { name: kivi.t8('Additional articles actions'), className: 'context-menu-heading' }
 
 968     }, additional_parts_actions, general_actions)
 
 972     selector: '.edit-additional-parts-row-context-menu',
 
 974         heading:        { name: kivi.t8('Additional articles actions'), className: 'context-menu-heading' }
 
 975       , delete:         { name: kivi.t8('Remove article'),     icon: "delete", callback: kivi.requirement_spec.delete_additional_part }
 
 976     }, additional_parts_actions, general_actions)
 
 980     selector: '.quotations-and-orders-context-menu,.quotations-and-orders-order-context-menu',
 
 982         heading:            { name: kivi.t8('Quotations/Orders actions'), className: 'context-menu-heading'                                                                                            }
 
 983       , edit_assignment:    { name: kivi.t8('Edit article/section assignments'), icon: "edit",   callback: ns.standard_quotation_order_ajax_call                                                       }
 
 985       , new:                { name: kivi.t8('Create new qutoation/order'),       icon: "add",    callback: ns.standard_quotation_order_ajax_call, disabled: ns.disable_create_quotation_order_commands }
 
 986       , update:             { name: kivi.t8('Update quotation/order'),           icon: "update", callback: ns.standard_quotation_order_ajax_call, disabled: ns.disable_edit_quotation_order_commands   }
 
 988       , delete:             { name: kivi.t8('Delete quotation/order'),           icon: "delete", callback: ns.ask_delete_quotation_order,         disabled: ns.disable_edit_quotation_order_commands   }
 
 993     selector: '.quotations-and-orders-edit-assignment-context-menu',
 
 995         heading:         { name: kivi.t8('Edit article/section assignments'), className: 'context-menu-heading'    }
 
 996       , save_assignment: { name: kivi.t8('Save'),   icon: "edit",  callback: ns.standard_quotation_order_ajax_call }
 
 997       , cancel:          { name: kivi.t8('Cancel'), icon: "close", callback: ns.standard_quotation_order_ajax_call }
 
1002     selector: '.quotations-and-orders-new-context-menu',
 
1004         heading: { name: kivi.t8('Create new quotation/order'), className: 'context-menu-heading'          }
 
1005       , create:  { name: kivi.t8('Create'), icon: "edit",  callback: ns.standard_quotation_order_ajax_call }
 
1006       , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: ns.standard_quotation_order_ajax_call }
 
1011     selector: '.quotations-and-orders-update-context-menu',
 
1013         heading:   { name: kivi.t8('Update quotation/order'), className: 'context-menu-heading'               }
 
1014       , do_update: { name: kivi.t8('Update'), icon: "update", callback: ns.standard_quotation_order_ajax_call }
 
1015       , cancel:    { name: kivi.t8('Cancel'), icon: "close",  callback: ns.standard_quotation_order_ajax_call }
 
1020     selector: '#content',
 
1021     items:    general_actions
 
1025       show: kivi.requirement_spec.item_popup_menu_shown
 
1026     , hide: kivi.requirement_spec.item_popup_menu_hidden
 
1030     selector: '.section-context-menu',
 
1033         heading:            { name: kivi.t8('Section/Function block actions'), className: 'context-menu-heading' }
 
1034       , add_section:        { name: kivi.t8('Add section'),        icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
 
1035       , 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 }
 
1037       , edit:               { name: kivi.t8('Edit'),               icon: "edit",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
 
1038       , delete:             { name: kivi.t8('Delete'),             icon: "delete", callback: kivi.requirement_spec.ask_delete_item,         disabled: kivi.requirement_spec.disable_edit_item_commands }
 
1040       , flag:               { name: kivi.t8('Toggle marker'),      icon: "flag",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
 
1042       , copy:               { name: kivi.t8('Copy'),               icon: "copy",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
 
1043       , paste:              { name: kivi.t8('Paste'),              icon: "paste",  callback: kivi.requirement_spec.standard_item_ajax_call }
 
1048     selector: '.function-block-context-menu,.sub-function-block-context-menu',
 
1051         heading:                { name: kivi.t8('Function block actions'), className: 'context-menu-heading' }
 
1052       , add_function_block:     { name: kivi.t8('Add function block'),     icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
 
1053       , add_sub_function_block: { name: kivi.t8('Add sub function block'), icon: "add",    callback: kivi.requirement_spec.standard_item_ajax_call }
 
1055       , edit:                   { name: kivi.t8('Edit'),                   icon: "edit",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
 
1056       , delete:                 { name: kivi.t8('Delete'),                 icon: "delete", callback: kivi.requirement_spec.ask_delete_item,         disabled: kivi.requirement_spec.disable_edit_item_commands }
 
1058       , flag:                   { name: kivi.t8('Toggle marker'),          icon: "flag",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
 
1060       , copy:                   { name: kivi.t8('Copy'),                   icon: "copy",   callback: kivi.requirement_spec.standard_item_ajax_call, disabled: kivi.requirement_spec.disable_edit_item_commands }
 
1061       , paste:                  { name: kivi.t8('Paste'),                  icon: "paste",  callback: kivi.requirement_spec.standard_item_ajax_call }
 
1066 });                             // end of namespace(...., function() {...
 
1068 function local_reinit_widgets() {
 
1069   kivi.run_once_for('#quotations_and_orders_order_id,[name="sections[].order_part_id"]', "assign_order_part_on_part_picked", function(elt) {
 
1070     $(elt).on('set_item:PartPicker', kivi.requirement_spec.assign_order_part_on_part_picked);