| 1 | package Koha::Contrib::ValueBuilder::RepeatableAutocomplete; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use JSON qw(to_json); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | # ABSTRACT: Repeatable autcomplete value-builder for Koha | ||||||||||||
| 7 | |||||||||||||
| 8 | 2 | our $VERSION = '1.006'; # VERSION | |||||||||||
| 9 | |||||||||||||
| 10 | 0 | ▶ | sub build_builder_inline { | ||||||||||
|
Subroutine
build_builder_inline: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 11 | 0 | my $class = shift; | |||||||||||
| 12 | 0 | my $args = shift; | |||||||||||
| 13 | |||||||||||||
| 14 | 0 | ▶ | my $builder = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 15 | 0 | my ($params) = @_; | |||||||||||
| 16 | |||||||||||||
| 17 | my $val = { | ||||||||||||
| 18 | function_name => $params->{id}, | ||||||||||||
| 19 | data => to_json( $args->{data} ), | ||||||||||||
| 20 | target => $args->{target}, | ||||||||||||
| 21 | 0 | ▶ | minlength => $args->{minlength} // 3, | ||||||||||
|
Condition0%0/2
$args->{"minlength"} // 3
MC/DC0%0/1
$args->{"minlength"} // 3
$args->{"minlength"}
Truth table0%0/2
A
A= $args->{"minlength"}
| |||||||||||||
| 22 | }; | ||||||||||||
| 23 | |||||||||||||
| 24 | 0 | my $res = <<'EOJS'; | |||||||||||
| 25 | <script> | ||||||||||||
| 26 | function Focus[% function_name %](event) { | ||||||||||||
| 27 | var dropdown = [% data %]; | ||||||||||||
| 28 | |||||||||||||
| 29 | var inputField = $(event.target); | ||||||||||||
| 30 | var currentVal = inputField.val(); | ||||||||||||
| 31 | var field = inputField.attr('id').replace(/_subfield_.*$/,''); | ||||||||||||
| 32 | |||||||||||||
| 33 | inputField.autocomplete({ | ||||||||||||
| 34 | source: dropdown, | ||||||||||||
| 35 | minLength: [% minlength %], | ||||||||||||
| 36 | select: function( event, ui ) { | ||||||||||||
| 37 | event.preventDefault(); | ||||||||||||
| 38 | |||||||||||||
| 39 | const source_id_prefix = inputField.attr('id').match(/tag_\d+_subfield_[^_]+/); | ||||||||||||
| 40 | const sources = inputField.closest('ul').find(`input[id^=${source_id_prefix}]`); | ||||||||||||
| 41 | const sourceIdx = sources.index(inputField); | ||||||||||||
| 42 | |||||||||||||
| 43 | const targets = inputField.closest('ul').find('input[id^="' + field + '_subfield_4"]'); | ||||||||||||
| 44 | let target = targets[sourceIdx]; | ||||||||||||
| 45 | |||||||||||||
| 46 | if (target == undefined) { | ||||||||||||
| 47 | // must be a new source, create a new target | ||||||||||||
| 48 | targets.last().parent().parent().find('a.buttonPlus').click(); | ||||||||||||
| 49 | target = inputField.closest('ul').find('input[id^="' + field + '_subfield_4"]').last(); | ||||||||||||
| 50 | } | ||||||||||||
| 51 | inputField.val( ui.item.value_for_input || ui.item.label ); | ||||||||||||
| 52 | $(target).val( ui.item.value_for_target ); | ||||||||||||
| 53 | inputField.autocomplete('destroy'); | ||||||||||||
| 54 | inputField.blur(); | ||||||||||||
| 55 | }, | ||||||||||||
| 56 | change: function (event, ui) { | ||||||||||||
| 57 | if(currentVal != inputField.val() && !ui.item){ | ||||||||||||
| 58 | target.val(''); | ||||||||||||
| 59 | inputField.val(''); | ||||||||||||
| 60 | } | ||||||||||||
| 61 | inputField.autocomplete('destroy'); | ||||||||||||
| 62 | inputField.blur(); | ||||||||||||
| 63 | }, | ||||||||||||
| 64 | }); | ||||||||||||
| 65 | |||||||||||||
| 66 | return 1; | ||||||||||||
| 67 | } | ||||||||||||
| 68 | </script> | ||||||||||||
| 69 | EOJS | ||||||||||||
| 70 | 0 | ▶ | $res =~ s{\[%\s?(.*?)\s?%\]}{$val->{$1} // ''}eg; | ||||||||||
|
Condition0%0/2
$val->{$1} // ""
MC/DC0%0/1
$val->{$1} // ""
$val->{$1}
Truth table0%0/2
A
A= $val->{$1}
| |||||||||||||
| 71 | 0 | return $res; | |||||||||||
| 72 | 0 | }; | |||||||||||
| 73 | 0 | return { builder => $builder }; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | ▶ | sub build_builder_inline_multiple { | ||||||||||
|
Subroutine
build_builder_inline_multiple: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 77 | 0 | my $class = shift; | |||||||||||
| 78 | 0 | my $args = shift; | |||||||||||
| 79 | |||||||||||||
| 80 | 0 | ▶ | my $builder = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 81 | 0 | my ($params) = @_; | |||||||||||
| 82 | |||||||||||||
| 83 | my $val = { | ||||||||||||
| 84 | function_name => $params->{id}, | ||||||||||||
| 85 | data => to_json( $args->{data} ), | ||||||||||||
| 86 | target_map => to_json( $args->{target_map}), | ||||||||||||
| 87 | 0 | ▶ | minlength => $args->{minlength} // 3, | ||||||||||
|
Condition0%0/2
$args->{"minlength"} // 3
MC/DC0%0/1
$args->{"minlength"} // 3
$args->{"minlength"}
Truth table0%0/2
A
A= $args->{"minlength"}
| |||||||||||||
| 88 | }; | ||||||||||||
| 89 | |||||||||||||
| 90 | 0 | my $res = <<'EOJS'; | |||||||||||
| 91 | <script> | ||||||||||||
| 92 | function Focus[% function_name %](event) { | ||||||||||||
| 93 | var dropdown = [% data %]; | ||||||||||||
| 94 | var targetMap = [% target_map %]; | ||||||||||||
| 95 | |||||||||||||
| 96 | var inputField = $(event.target); | ||||||||||||
| 97 | var currentVal = inputField.val(); | ||||||||||||
| 98 | var field = inputField.attr('id').replace(/_subfield_.*$/,''); | ||||||||||||
| 99 | |||||||||||||
| 100 | inputField.autocomplete({ | ||||||||||||
| 101 | source: dropdown, | ||||||||||||
| 102 | minLength: [% minlength %], | ||||||||||||
| 103 | select: function( event, ui ) { | ||||||||||||
| 104 | event.preventDefault(); | ||||||||||||
| 105 | inputField.val( ui.item.label ); | ||||||||||||
| 106 | |||||||||||||
| 107 | targetMap.forEach( function(element) { | ||||||||||||
| 108 | var target = $(inputField.closest('ul').find('input[id^="' + field + '_subfield_' + element.subfield + '"]')[0]); | ||||||||||||
| 109 | |||||||||||||
| 110 | switch (element.type) { | ||||||||||||
| 111 | case 'selected': | ||||||||||||
| 112 | target.val( ui.item[element.key] ); | ||||||||||||
| 113 | break; | ||||||||||||
| 114 | case 'literal': | ||||||||||||
| 115 | target.val(element.literal); | ||||||||||||
| 116 | break; | ||||||||||||
| 117 | } | ||||||||||||
| 118 | }); | ||||||||||||
| 119 | |||||||||||||
| 120 | inputField.autocomplete('destroy'); | ||||||||||||
| 121 | inputField.blur(); | ||||||||||||
| 122 | }, | ||||||||||||
| 123 | change: function (event, ui) { | ||||||||||||
| 124 | if(currentVal != inputField.val() && !ui.item){ | ||||||||||||
| 125 | inputField.val(''); | ||||||||||||
| 126 | targetMap.forEach( function(element) { | ||||||||||||
| 127 | var target = $(inputField.closest('ul').find('input[id^="' + field + '_subfield_' + element.subfield + '"]')[0]); | ||||||||||||
| 128 | target.val(''); | ||||||||||||
| 129 | }); | ||||||||||||
| 130 | } | ||||||||||||
| 131 | inputField.autocomplete('destroy'); | ||||||||||||
| 132 | inputField.blur(); | ||||||||||||
| 133 | }, | ||||||||||||
| 134 | }); | ||||||||||||
| 135 | |||||||||||||
| 136 | return 1; | ||||||||||||
| 137 | } | ||||||||||||
| 138 | </script> | ||||||||||||
| 139 | EOJS | ||||||||||||
| 140 | 0 | ▶ | $res =~ s{\[%\s?(.*?)\s?%\]}{$val->{$1} // ''}eg; | ||||||||||
|
Condition0%0/2
$val->{$1} // ""
MC/DC0%0/1
$val->{$1} // ""
$val->{$1}
Truth table0%0/2
A
A= $val->{$1}
| |||||||||||||
| 141 | 0 | return $res; | |||||||||||
| 142 | 0 | }; | |||||||||||
| 143 | 0 | return { builder => $builder }; | |||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | |||||||||||||
| 147 | |||||||||||||
| 148 | 2 | q{ listening to: Fatima Spar & JOV: The Voice Within }; | |||||||||||
| 149 | |||||||||||||
| 150 | __END__ | ||||||||||||