1 namespace('kivi', function(k){
 
   2   k.PartPicker = function($real, options) {
 
   3     // short circuit in case someone double inits us
 
   4     if ($real.data("part_picker"))
 
   5       return $real.data("part_picker");
 
  19       fat_set_item: $real.hasClass('partpicker_fat_set_item'),
 
  25     var real_id = $real.attr('id');
 
  26     var $dummy  = $('#' + real_id + '_name');
 
  27     var $type   = $('#' + real_id + '_type');
 
  28     var $unit   = $('#' + real_id + '_unit');
 
  29     var $convertible_unit = $('#' + real_id + '_convertible_unit');
 
  30     var $column = $('#' + real_id + '_column');
 
  31     var state   = STATES.PICKED;
 
  32     var last_real = $real.val();
 
  33     var last_dummy = $dummy.val();
 
  36     function open_dialog () {
 
  38         url: 'controller.pl?action=Part/part_picker_search',
 
  41         }, ajax_data($dummy.val())),
 
  44           title: k.t8('Part picker'),
 
  49       window.clearTimeout(timer);
 
  53     function ajax_data(term) {
 
  55         'filter.all:substr:multi::ilike': term,
 
  57         'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '',
 
  58         no_paginate:  $('#no_paginate').prop('checked') ? 1 : 0,
 
  59         column:   $column && $column.val() ? $column.val() : '',
 
  63       if ($type && $type.val())
 
  64         data['filter.type'] = $type.val().split(',');
 
  66       if ($unit && $unit.val())
 
  67         data['filter.unit'] = $unit.val().split(',');
 
  72     function set_item (item) {
 
  75         // autocomplete ui has name, ajax items have description
 
  76         $dummy.val(item.name ? item.name : item.description);
 
  81       state = STATES.PICKED;
 
  82       last_real = $real.val();
 
  83       last_dummy = $dummy.val();
 
  84       $real.trigger('change');
 
  86       if (o.fat_set_item && item.id) {
 
  88           url: 'controller.pl?action=Part/show.json',
 
  89           data: { id: item.id },
 
  90           success: function(rsp) {
 
  91             $real.trigger('set_item:PartPicker', rsp);
 
  95         $real.trigger('set_item:PartPicker', item);
 
  99     function make_defined_state () {
 
 100       if (state == STATES.PICKED)
 
 102       else if (state == STATES.UNDEFINED && $dummy.val() == '')
 
 105         set_item({ id: last_real, name: last_dummy })
 
 108     function update_results () {
 
 110         url: 'controller.pl?action=Part/part_picker_result',
 
 112             'real_id': $real.val(),
 
 113         }, ajax_data(function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val })),
 
 114         success: function(data){ $('#part_picker_result').html(data) }
 
 118     function result_timer (event) {
 
 119       if (!$('no_paginate').prop('checked')) {
 
 120         if (event.keyCode == KEY.PAGE_UP) {
 
 121           $('#part_picker_result a.paginate-prev').click();
 
 124         if (event.keyCode == KEY.PAGE_DOWN) {
 
 125           $('#part_picker_result a.paginate-next').click();
 
 129       window.clearTimeout(timer);
 
 130       timer = window.setTimeout(update_results, 100);
 
 133     function close_popup() {
 
 134       $('#part_selection').dialog('close');
 
 137     $dummy.autocomplete({
 
 138       source: function(req, rsp) {
 
 140           url:      'controller.pl?action=Part/ajax_autocomplete',
 
 142           data:     ajax_data(req.term),
 
 143           success:  function (data){ rsp(data) }
 
 146       select: function(event, ui) {
 
 150     /*  In case users are impatient and want to skip ahead:
 
 151      *  Capture <enter> key events and check if it's a unique hit.
 
 152      *  If it is, go ahead and assume it was selected. If it wasn't don't do
 
 153      *  anything so that autocompletion kicks in.  For <tab> don't prevent
 
 154      *  propagation. It would be nice to catch it, but javascript is too stupid
 
 155      *  to fire a tab event later on, so we'd have to reimplement the "find
 
 156      *  next active element in tabindex order and focus it".
 
 159      *  event.which does not contain tab events in keypressed in firefox but will report 0
 
 160      *  chrome does not fire keypressed at all on tab or escape
 
 161      *  TODO: users expect tab to work on keydown but enter to trigger on keyup,
 
 162      *        should be handled seperately
 
 164     $dummy.keydown(function(event){
 
 165       if (event.which == KEY.ENTER || event.which == KEY.TAB) { // enter or tab or tab
 
 166         // if string is empty assume they want to delete
 
 167         if ($dummy.val() == '') {
 
 170         } else if (state == STATES.PICKED) {
 
 174           url: 'controller.pl?action=Part/ajax_autocomplete',
 
 176           data: $.extend( ajax_data($dummy.val()), { prefer_exact: 1 } ),
 
 177           success: function (data){
 
 178             if (data.length == 1) {
 
 180               if (event.which == KEY.ENTER)
 
 181                 $('#update_button').click();
 
 182             } else if (data.length > 1) {
 
 183              if (event.which == KEY.ENTER)
 
 186                 make_defined_state();
 
 188               if (event.which == KEY.TAB)
 
 189                 make_defined_state();
 
 193         if (event.which == KEY.ENTER)
 
 196         state = STATES.UNDEFINED;
 
 200     $dummy.blur(function(){
 
 201       window.clearTimeout(timer);
 
 202       timer = window.setTimeout(make_defined_state, 100);
 
 205     // now add a picker div after the original input
 
 206     var pcont  = $('<span>').addClass('position-absolute');
 
 207     var picker = $('<div>');
 
 209     pcont.append(picker);
 
 210     picker.addClass('icon16 crm--search').click(open_dialog);
 
 213       real:           function() { return $real },
 
 214       dummy:          function() { return $dummy },
 
 215       type:           function() { return $type },
 
 216       unit:           function() { return $unit },
 
 217       convertible_unit: function() { return $convertible_unit },
 
 218       column:         function() { return $column },
 
 219       update_results: update_results,
 
 220       result_timer:   result_timer,
 
 222       reset:          make_defined_state,
 
 223       init_results:    function () {
 
 224         $('div.part_picker_part').each(function(){
 
 225           $(this).click(function(){
 
 227               id:   $(this).children('input.part_picker_id').val(),
 
 228               name: $(this).children('input.part_picker_description').val(),
 
 229               unit: $(this).children('input.part_picker_unit').val(),
 
 236         $('#part_selection').keydown(function(e){
 
 237            if (e.which == KEY.ESCAPE) {
 
 244     $real.data('part_picker', pp);
 
 250   $('input.part_autocomplete').each(function(i,real){
 
 251     kivi.PartPicker($(real));