Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/I 30.31 KB | None | 0 0
  1. create or replace package body select2 is
  2.  
  3.   --subtype gt_string is varchar2(32767);
  4.  
  5.   gco_min_lov_cols constant number(1) := 2;
  6.   gco_max_lov_cols constant number(1) := 3;
  7.   gco_lov_display_col constant number(1) := 1;
  8.   gco_lov_return_col constant number(1) := 2;
  9.   gco_lov_group_col constant number(1) := 3;
  10.   gco_contains_ignore_case constant char(3) := 'CIC';
  11.   gco_contains_ignore_case_diac constant char(4) := 'CICD';
  12.   gco_contains_case_sensitive constant char(3) := 'CCS';
  13.   gco_exact_ignore_case constant char(3) := 'EIC';
  14.   gco_exact_case_sensitive constant char(3) := 'ECS';
  15.   gco_starts_with_ignore_case constant char(3) := 'SIC';
  16.   gco_starts_with_case_sensitive constant char(3) := 'SCS';
  17.   gco_multi_word constant char(2) := 'MW';
  18.  
  19.  
  20.   procedure print_lov_options(
  21.               p_item in apex_plugin.t_page_item,
  22.               p_plugin in apex_plugin.t_plugin,
  23.               p_value in gt_string default null
  24.             ) is
  25.     l_null_optgroup_label_app gt_string := p_plugin.attribute_05;
  26.     l_select_list_type gt_string := p_item.attribute_01;
  27.     l_null_optgroup_label_cmp gt_string := p_item.attribute_09;
  28.     l_drag_and_drop_sorting gt_string := p_item.attribute_11;
  29.     l_lazy_loading gt_string := p_item.attribute_14;
  30.  
  31.     lco_null_optgroup_label constant gt_string := 'Ungrouped';
  32.  
  33.     l_lov apex_plugin_util.t_column_value_list;
  34.     l_null_optgroup gt_string;
  35.     l_tmp_optgroup gt_string;
  36.     l_selected_values apex_application_global.vc_arr2;
  37.     l_display_value gt_string;
  38.  
  39.     type gt_optgroups
  40.       is table of gt_string
  41.       index by pls_integer;
  42.     laa_optgroups gt_optgroups;
  43.  
  44.     -- local subprograms
  45.     function optgroup_exists(
  46.                p_optgroups in gt_optgroups,
  47.                p_optgroup in gt_string
  48.              ) return boolean is
  49.       l_index pls_integer := p_optgroups.first;
  50.     begin
  51.       while (l_index is not null) loop
  52.         if p_optgroups(l_index) = p_optgroup then
  53.           return true;
  54.         end if;
  55.  
  56.         l_index := p_optgroups.next(l_index);
  57.       end loop;
  58.  
  59.       return false;
  60.     end optgroup_exists;
  61.  
  62.  
  63.     function is_selected_value(
  64.                p_value in gt_string,
  65.                p_selected_values in gt_string
  66.              ) return boolean is
  67.       l_selected_values apex_application_global.vc_arr2;
  68.     begin
  69.       l_selected_values := apex_util.string_to_table(p_selected_values);
  70.  
  71.       for i in 1 .. l_selected_values.count loop
  72.         if apex_plugin_util.is_equal(p_value, l_selected_values(i)) then
  73.           return true;
  74.         end if;
  75.       end loop;
  76.  
  77.       return false;
  78.     end is_selected_value;
  79.   begin
  80.     l_lov := apex_plugin_util.get_data(
  81.                p_sql_statement  => p_item.lov_definition,
  82.                p_min_columns => gco_min_lov_cols,
  83.                p_max_columns => gco_max_lov_cols,
  84.                p_component_name => p_item.name
  85.              );
  86.  
  87.     -- print the selected LOV options in case of lazy loading or when drag and drop sorting is enabled
  88.     if (l_lazy_loading is not null or l_drag_and_drop_sorting is not null) then
  89.       if p_value is not null then
  90.         l_selected_values := apex_util.string_to_table(p_value);
  91.  
  92.         for i in 1 .. l_selected_values.count loop
  93.           begin
  94.             l_display_value := apex_plugin_util.get_display_data(
  95.                                  p_sql_statement => p_item.lov_definition,
  96.                                  p_min_columns => gco_min_lov_cols,
  97.                                  p_max_columns => gco_max_lov_cols,
  98.                                  p_component_name => p_item.name,
  99.                                  p_display_column_no => gco_lov_display_col,
  100.                                  p_search_column_no => gco_lov_return_col,
  101.                                  p_search_string => l_selected_values(i),
  102.                                  p_display_extra => false
  103.                                );
  104.           exception
  105.             when no_data_found then
  106.               l_display_value := null;
  107.           end;
  108.  
  109.           if not (l_display_value is null and not p_item.lov_display_extra) then
  110.             -- print the display value, or return value if no display value was found
  111.             apex_plugin_util.print_option(
  112.               p_display_value => nvl(l_display_value, l_selected_values(i)),
  113.               p_return_value => l_selected_values(i),
  114.               p_is_selected => true,
  115.               p_attributes => p_item.element_option_attributes,
  116.               p_escape => p_item.escape_output
  117.             );
  118.           end if;
  119.         end loop;
  120.       end if;
  121.     end if;
  122.  
  123.     if l_lazy_loading is null then
  124.       if l_lov.exists(gco_lov_group_col) then
  125.         if l_null_optgroup_label_cmp is not null then
  126.           l_null_optgroup := l_null_optgroup_label_cmp;
  127.         else
  128.           l_null_optgroup := nvl(l_null_optgroup_label_app, lco_null_optgroup_label);
  129.         end if;
  130.  
  131.         for i in 1 .. l_lov(gco_lov_display_col).count loop
  132.           l_tmp_optgroup := nvl(l_lov(gco_lov_group_col)(i), l_null_optgroup);
  133.  
  134.           if not optgroup_exists(laa_optgroups, l_tmp_optgroup) then
  135.             htp.p('<optgroup label="' || l_tmp_optgroup || '">');
  136.             for j in 1 .. l_lov(gco_lov_display_col).count loop
  137.               if nvl(l_lov(gco_lov_group_col)(j), l_null_optgroup) = l_tmp_optgroup then
  138.                 apex_plugin_util.print_option(
  139.                   p_display_value => l_lov(gco_lov_display_col)(j),
  140.                   p_return_value => l_lov(gco_lov_return_col)(j),
  141.                   p_is_selected => is_selected_value(l_lov(gco_lov_return_col)(j), p_value),
  142.                   p_attributes => p_item.element_option_attributes,
  143.                   p_escape => p_item.escape_output
  144.                 );
  145.               end if;
  146.             end loop;
  147.             htp.p('</optgroup>');
  148.  
  149.             laa_optgroups(i) := l_tmp_optgroup;
  150.           end if;
  151.         end loop;
  152.       else
  153.         if (l_drag_and_drop_sorting is not null and p_value is not null) then
  154.           for i in 1 .. l_lov(gco_lov_display_col).count loop
  155.             if not is_selected_value(l_lov(gco_lov_return_col)(i), p_value) then
  156.               apex_plugin_util.print_option(
  157.                 p_display_value => l_lov(gco_lov_display_col)(i),
  158.                 p_return_value => l_lov(gco_lov_return_col)(i),
  159.                 p_is_selected => false,
  160.                 p_attributes => p_item.element_option_attributes,
  161.                 p_escape => p_item.escape_output
  162.               );
  163.             end if;
  164.           end loop;
  165.         else
  166.           for i in 1 .. l_lov(gco_lov_display_col).count loop
  167.             apex_plugin_util.print_option(
  168.               p_display_value => l_lov(gco_lov_display_col)(i),
  169.               p_return_value => l_lov(gco_lov_return_col)(i),
  170.               p_is_selected => is_selected_value(l_lov(gco_lov_return_col)(i), p_value),
  171.               p_attributes => p_item.element_option_attributes,
  172.               p_escape => p_item.escape_output
  173.             );
  174.           end loop;
  175.         end if;
  176.       end if;
  177.     end if;
  178.  
  179.     if (p_value is not null and (l_select_list_type = 'TAG' or p_item.lov_display_extra)) then
  180.       if not (l_lazy_loading is not null or l_drag_and_drop_sorting is not null) then
  181.         l_selected_values := apex_util.string_to_table(p_value);
  182.  
  183.         for i in 1 .. l_selected_values.count loop
  184.           begin
  185.             l_display_value := apex_plugin_util.get_display_data(
  186.                                  p_sql_statement => p_item.lov_definition,
  187.                                  p_min_columns => gco_min_lov_cols,
  188.                                  p_max_columns => gco_max_lov_cols,
  189.                                  p_component_name => p_item.name,
  190.                                  p_display_column_no => gco_lov_display_col,
  191.                                  p_search_column_no => gco_lov_return_col,
  192.                                  p_search_string => l_selected_values(i),
  193.                                  p_display_extra => false
  194.                                );
  195.           exception
  196.             when no_data_found then
  197.               l_display_value := null;
  198.           end;
  199.  
  200.           if l_display_value is null then
  201.             apex_plugin_util.print_option(
  202.               p_display_value => l_selected_values(i),
  203.               p_return_value => l_selected_values(i),
  204.               p_is_selected => true,
  205.               p_attributes => p_item.element_option_attributes,
  206.               p_escape => p_item.escape_output
  207.             );
  208.           end if;
  209.         end loop;
  210.       end if;
  211.     end if;
  212.   end print_lov_options;
  213.  
  214.  
  215.   function render(
  216.              p_item in apex_plugin.t_page_item,
  217.              p_plugin in apex_plugin.t_plugin,
  218.              p_value in gt_string,
  219.              p_is_readonly in boolean,
  220.              p_is_printer_friendly in boolean
  221.            ) return apex_plugin.t_page_item_render_result is
  222.     l_no_matches_msg gt_string := p_plugin.attribute_01;
  223.     l_input_too_short_msg gt_string := p_plugin.attribute_02;
  224.     l_selection_too_big_msg gt_string := p_plugin.attribute_03;
  225.     l_searching_msg gt_string := p_plugin.attribute_04;
  226.     l_null_optgroup_label_app gt_string := p_plugin.attribute_05;
  227.     l_loading_more_results_msg gt_string := p_plugin.attribute_06;
  228.     l_look_and_feel gt_string := p_plugin.attribute_07;
  229.     l_error_loading_msg gt_string := p_plugin.attribute_08;
  230.     l_input_too_long_msg gt_string := p_plugin.attribute_09;
  231.     l_custom_css_path gt_string := p_plugin.attribute_10;
  232.     l_custom_css_filename gt_string := p_plugin.attribute_11;
  233.  
  234.     l_select_list_type gt_string := p_item.attribute_01;
  235.     l_min_results_for_search gt_string := p_item.attribute_02;
  236.     l_min_input_length gt_string := p_item.attribute_03;
  237.     l_max_input_length gt_string := p_item.attribute_04;
  238.     l_max_selection_size gt_string := p_item.attribute_05;
  239.     l_rapid_selection gt_string := p_item.attribute_06;
  240.     l_select_on_blur gt_string := p_item.attribute_07;
  241.     l_search_logic gt_string := p_item.attribute_08;
  242.     l_null_optgroup_label_cmp gt_string := p_item.attribute_09;
  243.     l_width gt_string := p_item.attribute_10;
  244.     l_drag_and_drop_sorting gt_string := p_item.attribute_11;
  245.     l_token_separators gt_string := p_item.attribute_12;
  246.     l_lazy_loading gt_string := p_item.attribute_14;
  247.     l_lazy_append_row_count gt_string := p_item.attribute_15;
  248.  
  249.     l_display_values apex_application_global.vc_arr2;
  250.     l_multiselect gt_string;
  251.  
  252.     l_item_jq gt_string := apex_plugin_util.page_item_names_to_jquery(p_item.name);
  253.     l_cascade_parent_items_jq gt_string := apex_plugin_util.page_item_names_to_jquery(p_item.lov_cascade_parent_items);
  254.     l_cascade_items_to_submit_jq gt_string := apex_plugin_util.page_item_names_to_jquery(p_item.ajax_items_to_submit);
  255.     l_items_for_session_state_jq gt_string;
  256.     l_cascade_parent_items apex_application_global.vc_arr2;
  257.     l_optimize_refresh_condition gt_string;
  258.  
  259.     l_apex_version gt_string;
  260.     l_onload_code gt_string;
  261.     l_render_result apex_plugin.t_page_item_render_result;
  262.  
  263.     -- local subprograms
  264.     function get_select2_constructor
  265.     return gt_string is
  266.       l_selected_values apex_application_global.vc_arr2;
  267.       l_display_values apex_application_global.vc_arr2;
  268.       l_json gt_string;
  269.       l_code gt_string;
  270.  
  271.       l_allow_clear_bool boolean;
  272.       l_rapid_selection_bool boolean;
  273.       l_select_on_blur_bool boolean;
  274.     begin
  275.       if p_item.lov_display_null then
  276.         l_allow_clear_bool := true;
  277.       else
  278.         l_allow_clear_bool := false;
  279.       end if;
  280.  
  281.       if l_rapid_selection is null then
  282.         l_rapid_selection_bool := true;
  283.       else
  284.         l_rapid_selection_bool := false;
  285.       end if;
  286.  
  287.       if l_select_on_blur is null then
  288.         l_select_on_blur_bool := false;
  289.       else
  290.         l_select_on_blur_bool := true;
  291.       end if;
  292.  
  293.       l_code := '
  294.        $("' || l_item_jq || '").select2({' ||
  295.           apex_javascript.add_attribute('placeholder', p_item.lov_null_text, false) ||
  296.           apex_javascript.add_attribute('allowClear', l_allow_clear_bool) ||
  297.           apex_javascript.add_attribute('minimumInputLength', to_number(l_min_input_length)) ||
  298.           apex_javascript.add_attribute('maximumInputLength', to_number(l_max_input_length)) ||
  299.           apex_javascript.add_attribute('minimumResultsForSearch', to_number(l_min_results_for_search)) ||
  300.           apex_javascript.add_attribute('maximumSelectionLength', to_number(l_max_selection_size)) ||
  301.           apex_javascript.add_attribute('closeOnSelect', l_rapid_selection_bool) ||
  302.           apex_javascript.add_attribute('selectOnClose', l_select_on_blur_bool) ||
  303.           apex_javascript.add_attribute('tokenSeparators', l_token_separators);
  304.  
  305.       if l_look_and_feel = 'SELECT2_CLASSIC' then
  306.         l_code := l_code || apex_javascript.add_attribute('theme', 'classic');
  307.       end if;
  308.  
  309.       l_code := l_code || '"language": {';
  310.  
  311.       if l_error_loading_msg is not null then
  312.         l_code := l_code || '
  313.          "errorLoading": function() {
  314.                            return "' || l_error_loading_msg || '";
  315.                          },';
  316.       end if;
  317.       if l_input_too_long_msg is not null then
  318.         l_code := l_code || '
  319.          "inputTooLong": function(args) {
  320.                            var msg = "' || l_input_too_long_msg || '";
  321.                            msg = msg.replace("#TERM#", args.input);
  322.                            msg = msg.replace("#MAXLENGTH#", args.maximum);
  323.                            msg = msg.replace("#OVERCHARS#", args.input.length - args.maximum);
  324.                            return msg;
  325.                          },';
  326.       end if;
  327.       if l_input_too_short_msg is not null then
  328.         l_code := l_code || '
  329.          "inputTooShort": function(args) {
  330.                             var msg = "' || l_input_too_short_msg || '";
  331.                             msg = msg.replace("#TERM#", args.input);
  332.                             msg = msg.replace("#MINLENGTH#", args.minimum);
  333.                             msg = msg.replace("#REMAININGCHARS#", args.minimum - args.input.length);
  334.                             return msg;
  335.                           },';
  336.       end if;
  337.       if l_loading_more_results_msg is not null then
  338.         l_code := l_code || '
  339.          "loadingMore": function() {
  340.                           return "' || l_loading_more_results_msg || '";
  341.                         },';
  342.       end if;
  343.       if l_selection_too_big_msg is not null then
  344.         l_code := l_code || '
  345.          "maximumSelected": function(args) {
  346.                               var msg = "' || l_selection_too_big_msg || '";
  347.                               msg = msg.replace("#MAXSIZE#", args.maximum);
  348.                               return msg;
  349.                             },';
  350.       end if;
  351.       if l_no_matches_msg is not null then
  352.         l_code := l_code || '
  353.          "noResults": function() {
  354.                         return "' || l_no_matches_msg || '";
  355.                       },';
  356.       end if;
  357.       if l_searching_msg is not null then
  358.         l_code := l_code || '
  359.          "searching": function() {
  360.                         return "' || l_searching_msg || '";
  361.                       },';
  362.       end if;
  363.  
  364.       l_code := rtrim(l_code, ',') || '},';
  365.  
  366.       if l_search_logic != gco_contains_ignore_case then
  367.         case l_search_logic
  368.           when gco_contains_ignore_case_diac then l_search_logic := 'return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;';
  369.           when gco_contains_case_sensitive then l_search_logic := 'return text.indexOf(term) >= 0;';
  370.           when gco_exact_ignore_case then l_search_logic := 'return text.toUpperCase() === term.toUpperCase() || term.length === 0;';
  371.           when gco_exact_case_sensitive then l_search_logic := 'return text === term || term.length === 0;';
  372.           when gco_starts_with_ignore_case then l_search_logic := 'return text.toUpperCase().indexOf(term.toUpperCase()) === 0;';
  373.           when gco_starts_with_case_sensitive then l_search_logic := 'return text.indexOf(term) === 0;';
  374.           when gco_multi_word then l_search_logic := '
  375.            var escpTerm = term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  376.            return new RegExp(escpTerm.replace(/ /g, ".*"), "i").test(text);';
  377.           else l_search_logic := 'return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;';
  378.         end case;
  379.  
  380.         l_code := '$.fn.select2.amd.require([''select2/compat/matcher''], function(oldMatcher) {' ||
  381.           l_code || '
  382.          matcher: oldMatcher(
  383.                     function(term, text) {
  384.                       ' || l_search_logic || '
  385.                     }
  386.                   ),';
  387.       end if;
  388.  
  389.       if l_lazy_loading is not null then
  390.         l_code := l_code || '
  391.          ajax: {
  392.            url: "wwv_flow.show",
  393.            type: "POST",
  394.            dataType: "json",
  395.            delay: 400,
  396.            data: function(params) {
  397.                    return {
  398.                      p_flow_id: $("#pFlowId").val(),
  399.                      p_flow_step_id: $("#pFlowStepId").val(),
  400.                      p_instance: $("#pInstance").val(),
  401.                      x01: params.term,
  402.                      x02: params.page,
  403.                      x03: "LAZY_LOAD",
  404.                      p_request: "PLUGIN=' || apex_plugin.get_ajax_identifier || '"
  405.                    };
  406.                  },
  407.            processResults: function(data, params) {
  408.                              var select2Data = $.map(data.row, function(obj) {
  409.                                obj.id = obj.R;
  410.                                obj.text = obj.D;
  411.                                return obj;
  412.                              });
  413.  
  414.                              return {
  415.                                results: select2Data,
  416.                                pagination: { more: data.more }
  417.                              };
  418.                            },
  419.            cache: true
  420.          },
  421.          escapeMarkup: function(markup) { return markup; },';
  422.       end if;
  423.  
  424.       if l_select_list_type = 'TAG' then
  425.         l_code := l_code || apex_javascript.add_attribute('tags', true);
  426.       end if;
  427.  
  428.       l_code := l_code || apex_javascript.add_attribute('width', nvl(l_width, 'element'), true, false);
  429.       l_code := l_code || '})';
  430.  
  431.       if l_search_logic != gco_contains_ignore_case then
  432.         l_code := l_code || '});';
  433.       else
  434.         l_code := l_code || ';';
  435.       end if;
  436.  
  437.       return l_code;
  438.     end get_select2_constructor;
  439.  
  440.  
  441.     function get_sortable_constructor
  442.     return gt_string is
  443.       l_code gt_string;
  444.     begin
  445.       l_code := '
  446.        var s2item = $("' || l_item_jq || '");
  447.        var s2ul = s2item.next(".select2-container").find("ul.select2-selection__rendered");
  448.        s2ul.sortable({
  449.          containment: "parent",
  450.          items: "li:not(.select2-search)",
  451.          tolerance: "pointer",
  452.          stop: function() {
  453.            $(s2ul.find(".select2-selection__choice").get().reverse()).each(function() {
  454.              s2item.prepend(s2item.find(''option[value="'' + $(this).data("data").id + ''"]'')[0]);
  455.            });
  456.          }
  457.        });';
  458.  
  459.         /* prevent automatic tags sorting
  460.            http://stackoverflow.com/questions/31431197/select2-how-to-prevent-tags-sorting
  461.         s2item.on("select2:select", function(e) {
  462.           var $element = $(e.params.data.element);
  463.  
  464.           $element.detach();
  465.           $(this).append($element);
  466.           $(this).trigger("change");
  467.         });';
  468.         */
  469.  
  470.       return l_code;
  471.     end get_sortable_constructor;
  472.   begin
  473.     if apex_application.g_debug then
  474.       apex_plugin_util.debug_page_item(p_plugin, p_item, p_value, p_is_readonly, p_is_printer_friendly);
  475.     end if;
  476.  
  477.     if (p_is_readonly or p_is_printer_friendly) then
  478.       apex_plugin_util.print_hidden_if_readonly(p_item.name, p_value, p_is_readonly, p_is_printer_friendly);
  479.  
  480.       begin
  481.         l_display_values := apex_plugin_util.get_display_data(
  482.                               p_sql_statement => p_item.lov_definition,
  483.                               p_min_columns => gco_min_lov_cols,
  484.                               p_max_columns => gco_max_lov_cols,
  485.                               p_component_name => p_item.name,
  486.                               p_search_value_list => apex_util.string_to_table(p_value),
  487.                               p_display_extra => p_item.lov_display_extra
  488.                             );
  489.       exception
  490.         when no_data_found then
  491.           null; -- https://github.com/nbuytaert1/apex-select2/issues/51
  492.       end;
  493.  
  494.       if l_display_values.count = 1 then
  495.         apex_plugin_util.print_display_only(
  496.           p_item_name => p_item.name,
  497.           p_display_value => l_display_values(1),
  498.           p_show_line_breaks => false,
  499.           p_escape => p_item.escape_output,
  500.           p_attributes => p_item.element_attributes
  501.         );
  502.       elsif l_display_values.count > 1 then
  503.         htp.p('
  504.          <ul id="' || p_item.name || '_DISPLAY"
  505.            class="display_only ' || p_item.element_css_classes || '"' ||
  506.             p_item.element_attributes || '>');
  507.  
  508.         for i in 1 .. l_display_values.count loop
  509.           if p_item.escape_output then
  510.             htp.p('<li>' || htf.escape_sc(l_display_values(i)) || '</li>');
  511.           else
  512.             htp.p('<li>' || l_display_values(i) || '</li>');
  513.           end if;
  514.         end loop;
  515.  
  516.         htp.p('</ul>');
  517.       end if;
  518.  
  519.       return l_render_result;
  520.     end if;
  521.  
  522.     apex_javascript.add_library(
  523.       p_name => 'select2.full.min',
  524.       p_directory => p_plugin.file_prefix,
  525.       p_version => null
  526.     );
  527.     apex_javascript.add_library(
  528.       p_name => 'select2-apex',
  529.       p_directory => p_plugin.file_prefix,
  530.       p_version => null
  531.     );
  532.     apex_css.add_file(
  533.       p_name => 'select2.min',
  534.       p_directory => p_plugin.file_prefix,
  535.       p_version => null
  536.     );
  537.     if l_look_and_feel = 'SELECT2_CLASSIC' then
  538.       apex_css.add_file(
  539.         p_name => 'select2-classic',
  540.         p_directory => p_plugin.file_prefix,
  541.         p_version => null
  542.       );
  543.     elsif l_look_and_feel = 'CUSTOM' then
  544.       apex_css.add_file(
  545.         p_name => apex_plugin_util.replace_substitutions(l_custom_css_filename),
  546.         p_directory => apex_plugin_util.replace_substitutions(l_custom_css_path),
  547.         p_version => null
  548.       );
  549.     end if;
  550.  
  551.     if l_select_list_type in ('MULTI', 'TAG') then
  552.       l_multiselect := 'multiple="multiple"';
  553.     end if;
  554.  
  555.     htp.p('
  556.      <select ' || l_multiselect || '
  557.        id="' || p_item.name || '"
  558.        name="' || apex_plugin.get_input_name_for_page_item(true) || '"
  559.        class="selectlist ' || p_item.element_css_classes || '"' ||
  560.         p_item.element_attributes || '>');
  561.  
  562.     if (l_select_list_type = 'SINGLE' and p_item.lov_display_null) then
  563.       apex_plugin_util.print_option(
  564.         p_display_value => p_item.lov_null_text,
  565.         p_return_value => p_item.lov_null_value,
  566.         p_is_selected => false,
  567.         p_attributes => p_item.element_option_attributes,
  568.         p_escape => p_item.escape_output
  569.       );
  570.     end if;
  571.  
  572.     print_lov_options(p_item, p_plugin, p_value);
  573.  
  574.     htp.p('</select>');
  575.  
  576.     l_onload_code := get_select2_constructor;
  577.  
  578.     if l_drag_and_drop_sorting is not null then
  579.       select substr(version_no, 1, 3)
  580.       into l_apex_version
  581.       from apex_release;
  582.  
  583.       if l_apex_version = '4.2' then
  584.         apex_javascript.add_library(
  585.           p_name => 'jquery.ui.sortable.min',
  586.           p_directory => '#IMAGE_PREFIX#libraries/jquery-ui/1.8.22/ui/minified/',
  587.           p_version => null
  588.         );
  589.       else
  590.         apex_javascript.add_library(
  591.           p_name => 'jquery.ui.sortable.min',
  592.           p_directory => '#IMAGE_PREFIX#libraries/jquery-ui/1.10.4/ui/minified/',
  593.           p_version => null
  594.         );
  595.       end if;
  596.  
  597.       l_onload_code := l_onload_code || get_sortable_constructor();
  598.     end if;
  599.  
  600.     if p_item.lov_cascade_parent_items is not null then
  601.       l_items_for_session_state_jq := l_cascade_parent_items_jq;
  602.  
  603.       if l_cascade_items_to_submit_jq is not null then
  604.         l_items_for_session_state_jq := l_items_for_session_state_jq || ',' || l_cascade_items_to_submit_jq;
  605.       end if;
  606.  
  607.       l_onload_code := l_onload_code || '
  608.        $("' || l_cascade_parent_items_jq || '").on("change", function(e) {';
  609.  
  610.       if p_item.ajax_optimize_refresh then
  611.         l_cascade_parent_items := apex_util.string_to_table(l_cascade_parent_items_jq, ',');
  612.  
  613.         l_optimize_refresh_condition := '$("' || l_cascade_parent_items(1) || '").val() === ""';
  614.  
  615.         for i in 2 .. l_cascade_parent_items.count loop
  616.           l_optimize_refresh_condition := l_optimize_refresh_condition || ' || $("' || l_cascade_parent_items(i) || '").val() === ""';
  617.         end loop;
  618.  
  619.         l_onload_code := l_onload_code || '
  620.          var item = $("' || l_item_jq || '");
  621.          if (' || l_optimize_refresh_condition || ') {
  622.            item.val("").trigger("change");
  623.          } else {';
  624.       end if;
  625.  
  626.       l_onload_code := l_onload_code || '
  627.            apex.server.plugin(
  628.              "' || apex_plugin.get_ajax_identifier || '",
  629.              { pageItems: "' || l_items_for_session_state_jq || '" },
  630.              { refreshObject: "' || l_item_jq || '",
  631.                loadingIndicator: "' || l_item_jq || '",
  632.                loadingIndicatorPosition: "after",
  633.                dataType: "text",
  634.                success: function(pData) {
  635.                           var item = $("' || l_item_jq || '");
  636.                           item.html(pData);
  637.                           item.val("").trigger("change");
  638.                         }
  639.              });';
  640.  
  641.       if p_item.ajax_optimize_refresh then
  642.         l_onload_code := l_onload_code || '}';
  643.       end if;
  644.  
  645.       l_onload_code := l_onload_code || '});';
  646.     end if;
  647.  
  648.     l_onload_code := l_onload_code || '
  649.        beCtbSelect2.events.bind("' || l_item_jq || '");';
  650.  
  651.     apex_javascript.add_onload_code(l_onload_code);
  652.     l_render_result.is_navigable := true;
  653.     return l_render_result;
  654.   end render;
  655.  
  656.  
  657.   function ajax(
  658.              p_item in apex_plugin.t_page_item,
  659.              p_plugin in apex_plugin.t_plugin
  660.            ) return apex_plugin.t_page_item_ajax_result is
  661.     l_select_list_type gt_string := p_item.attribute_01;
  662.     l_search_logic gt_string := p_item.attribute_08;
  663.     l_lazy_append_row_count gt_string := p_item.attribute_15;
  664.  
  665.     l_lov apex_plugin_util.t_column_value_list;
  666.     l_json gt_string;
  667.     l_apex_plugin_search_logic gt_string;
  668.     l_search_string gt_string;
  669.     l_search_page number;
  670.     l_first_row number;
  671.     l_loop_count number;
  672.     l_more_rows_boolean boolean;
  673.  
  674.     l_result apex_plugin.t_page_item_ajax_result;
  675.   begin
  676.     if apex_application.g_x03 = 'LAZY_LOAD' then
  677.       l_search_string := nvl(apex_application.g_x01, '%');
  678.       l_search_page := nvl(apex_application.g_x02, 1);
  679.       l_first_row := ((l_search_page - 1) * nvl(l_lazy_append_row_count, 0)) + 1;
  680.  
  681.       -- translate Select2 search logic into APEX_PLUGIN_UTIL search logic
  682.       -- the percentage wildcard returns all rows whenever the search string is null
  683.       case l_search_logic
  684.         when gco_contains_case_sensitive then
  685.           l_apex_plugin_search_logic := apex_plugin_util.c_search_like_case; -- uses LIKE %value%
  686.         when gco_exact_ignore_case then
  687.           l_apex_plugin_search_logic := apex_plugin_util.c_search_exact_ignore; -- uses LIKE VALUE% with UPPER (not completely correct)
  688.         when gco_exact_case_sensitive then
  689.           l_apex_plugin_search_logic := apex_plugin_util.c_search_lookup; -- uses = value
  690.         when gco_starts_with_ignore_case then
  691.           l_apex_plugin_search_logic := apex_plugin_util.c_search_exact_ignore; -- uses LIKE VALUE% with UPPER
  692.         when gco_starts_with_case_sensitive then
  693.           l_apex_plugin_search_logic := apex_plugin_util.c_search_exact_case; -- uses LIKE value%
  694.         else
  695.           l_apex_plugin_search_logic := apex_plugin_util.c_search_like_ignore; -- uses LIKE %VALUE% with UPPER
  696.       end case;
  697.  
  698.       if l_search_logic = gco_multi_word then
  699.         l_search_string := replace(l_search_string, ' ', '%');
  700.       end if;
  701.  
  702.       l_lov := apex_plugin_util.get_data(
  703.                  p_sql_statement => p_item.lov_definition,
  704.                  p_min_columns => gco_min_lov_cols,
  705.                  p_max_columns => gco_max_lov_cols,
  706.                  p_component_name => p_item.name,
  707.                  p_search_type => l_apex_plugin_search_logic,
  708.                  p_search_column_no => gco_lov_display_col,
  709.                  p_search_string => apex_plugin_util.get_search_string(
  710.                                       p_search_type => l_apex_plugin_search_logic,
  711.                                       p_search_string => l_search_string
  712.                                     ),
  713.                  p_first_row => l_first_row,
  714.                  p_max_rows => l_lazy_append_row_count + 1
  715.                );
  716.  
  717.       if l_lov(gco_lov_return_col).count = l_lazy_append_row_count + 1 then
  718.         l_loop_count := l_lov(gco_lov_return_col).count - 1;
  719.       else
  720.         l_loop_count := l_lov(gco_lov_return_col).count;
  721.       end if;
  722.  
  723.       l_json := '{"row":[';
  724.  
  725.       if p_item.escape_output then
  726.         for i in 1 .. l_loop_count loop
  727.           l_json := l_json || '{' ||
  728.             apex_javascript.add_attribute('R', htf.escape_sc(l_lov(gco_lov_return_col)(i)), false, true) ||
  729.             apex_javascript.add_attribute('D', htf.escape_sc(l_lov(gco_lov_display_col)(i)), false, false) ||
  730.           '},';
  731.         end loop;
  732.       else
  733.         for i in 1 .. l_loop_count loop
  734.           l_json := l_json || '{' ||
  735.             apex_javascript.add_attribute('R', l_lov(gco_lov_return_col)(i), false, true) ||
  736.             apex_javascript.add_attribute('D', l_lov(gco_lov_display_col)(i), false, false) ||
  737.           '},';
  738.         end loop;
  739.       end if;
  740.  
  741.       l_json := rtrim(l_json, ',');
  742.  
  743.       if l_lov(gco_lov_return_col).exists(l_lazy_append_row_count + 1) then
  744.         l_more_rows_boolean := true;
  745.       else
  746.         l_more_rows_boolean := false;
  747.       end if;
  748.  
  749.       l_json := l_json || '],' || apex_javascript.add_attribute('more', l_more_rows_boolean, true, false) || '}';
  750.  
  751.       htp.p(l_json);
  752.     else
  753.       print_lov_options(p_item, p_plugin);
  754.     end if;
  755.  
  756.     return l_result;
  757.   end ajax;
  758.  
  759. end select2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement