includes/clientside/static/autofill.js
changeset 420 301f546688d1
parent 407 35d94240a197
child 550 685e839d934e
equal deleted inserted replaced
419:b8b4e38825db 420:301f546688d1
    56       if ( autofill.event.keyCode )
    56       if ( autofill.event.keyCode )
    57       {
    57       {
    58         if ( autofill.event.keyCode == autofill.KEY_ENTER && autofill.boxes.length < 1 && !autofill.box_id )
    58         if ( autofill.event.keyCode == autofill.KEY_ENTER && autofill.boxes.length < 1 && !autofill.box_id )
    59         {
    59         {
    60           // user hit enter after accepting a suggestion - submit the form
    60           // user hit enter after accepting a suggestion - submit the form
    61           var frm = findParentForm($(autofill.field_id).object);
    61           var frm = findParentForm($dynano(autofill.field_id).object);
    62           frm._af_acting = false;
    62           frm._af_acting = false;
    63           frm.submit();
    63           frm.submit();
    64           // window.console.info('Submitting form');
    64           // window.console.info('Submitting form');
    65           return false;
    65           return false;
    66         }
    66         }
    84     for ( var i = 0; i < resp_json.users_real.length; i++ )
    84     for ( var i = 0; i < resp_json.users_real.length; i++ )
    85     {
    85     {
    86       try
    86       try
    87       {
    87       {
    88         var user = resp_json.users_real[i].toLowerCase();
    88         var user = resp_json.users_real[i].toLowerCase();
    89         var inp  = $(autofill.field_id).object.value;
    89         var inp  = $dynano(autofill.field_id).object.value;
    90         inp = inp.toLowerCase();
    90         inp = inp.toLowerCase();
    91         if ( user.indexOf(inp) > -1 )
    91         if ( user.indexOf(inp) > -1 )
    92         {
    92         {
    93           users.push(resp_json.users_real[i]);
    93           users.push(resp_json.users_real[i]);
    94         }
    94         }
   148         tr.appendChild(td);
   148         tr.appendChild(td);
   149         table.appendChild(tr);
   149         table.appendChild(tr);
   150       }
   150       }
   151       
   151       
   152     // Finalize div
   152     // Finalize div
   153     var tb_top    = $(autofill.field_id).Top();
   153     var tb_top    = $dynano(autofill.field_id).Top();
   154     var tb_height = $(autofill.field_id).Height();
   154     var tb_height = $dynano(autofill.field_id).Height();
   155     var af_top    = tb_top + tb_height - 9;
   155     var af_top    = tb_top + tb_height - 9;
   156     var tb_left   = $(autofill.field_id).Left();
   156     var tb_left   = $dynano(autofill.field_id).Left();
   157     var af_left   = tb_left;
   157     var af_left   = tb_left;
   158     
   158     
   159     div.style.position = 'absolute';
   159     div.style.position = 'absolute';
   160     div.style.left = af_left + 'px';
   160     div.style.left = af_left + 'px';
   161     div.style.top  = af_top  + 'px';
   161     div.style.top  = af_top  + 'px';
   191         if ( response.substr(0,1) != '{' )
   191         if ( response.substr(0,1) != '{' )
   192         {
   192         {
   193           new messagebox(MB_OK|MB_ICONSTOP, 'Invalid response', 'Invalid or unexpected JSON response from server:<pre>' + ajax.responseText + '</pre>');
   193           new messagebox(MB_OK|MB_ICONSTOP, 'Invalid response', 'Invalid or unexpected JSON response from server:<pre>' + ajax.responseText + '</pre>');
   194           return false;
   194           return false;
   195         }
   195         }
   196         if ( $(afobj.field_id).object.value.length < 3 )
   196         if ( $dynano(afobj.field_id).object.value.length < 3 )
   197           return false;
   197           return false;
   198         var resp_json = parseJSON(response);
   198         var resp_json = parseJSON(response);
   199         var resp_code = $(afobj.field_id).object.value.toLowerCase().substr(0, 3);
   199         var resp_code = $dynano(afobj.field_id).object.value.toLowerCase().substr(0, 3);
   200         afobj.responses[resp_code] = resp_json;
   200         afobj.responses[resp_code] = resp_json;
   201         afobj.process_dataset(resp_json);
   201         afobj.process_dataset(resp_json);
   202       }
   202       }
   203     }
   203     }
   204     var usernamefragment = ajaxEscape($(this.field_id).object.value);
   204     var usernamefragment = ajaxEscape($dynano(this.field_id).object.value);
   205     ajaxGet(stdAjaxPrefix + '&_mode=fillusername&name=' + usernamefragment + '&allowanon=' + ( this.allowanon ? '1' : '0' ), processResponse);
   205     ajaxGet(stdAjaxPrefix + '&_mode=fillusername&name=' + usernamefragment + '&allowanon=' + ( this.allowanon ? '1' : '0' ), processResponse);
   206   }
   206   }
   207   
   207   
   208   this.go = function()
   208   this.go = function()
   209   {
   209   {
   214     }
   214     }
   215     
   215     
   216     if ( af_current )
   216     if ( af_current )
   217       return false;
   217       return false;
   218     
   218     
   219     var resp_code = $(this.field_id).object.value.toLowerCase().substr(0, 3);
   219     var resp_code = $dynano(this.field_id).object.value.toLowerCase().substr(0, 3);
   220     if ( this.responses.length < 1 || ! this.responses[ resp_code ] )
   220     if ( this.responses.length < 1 || ! this.responses[ resp_code ] )
   221     {
   221     {
   222       // window.console.info('Cannot find dataset ' + resp_code + ' in cache, sending AJAX request');
   222       // window.console.info('Cannot find dataset ' + resp_code + ' in cache, sending AJAX request');
   223       this.fetch_and_process();
   223       this.fetch_and_process();
   224     }
   224     }
   259   {
   259   {
   260     var key = this.event.keyCode;
   260     var key = this.event.keyCode;
   261     if ( key == this.KEY_ENTER && !this.repeat )
   261     if ( key == this.KEY_ENTER && !this.repeat )
   262     {
   262     {
   263       submitAuthorized = true;
   263       submitAuthorized = true;
   264       var form = findParentForm($(this.field_id).object);
   264       var form = findParentForm($dynano(this.field_id).object);
   265       form._af_acting = false;
   265       form._af_acting = false;
   266       return true;
   266       return true;
   267     }
   267     }
   268     switch(key)
   268     switch(key)
   269     {
   269     {
   282       case this.KEY_ENTER:
   282       case this.KEY_ENTER:
   283         this.set();
   283         this.set();
   284         break;
   284         break;
   285     }
   285     }
   286     
   286     
   287     var form = findParentForm($(this.field_id).object);
   287     var form = findParentForm($dynano(this.field_id).object);
   288       form._af_acting = false;
   288       form._af_acting = false;
   289   }
   289   }
   290   
   290   
   291   this.get_state_td = function()
   291   this.get_state_td = function()
   292   {
   292   {
   325       this.state = newstate;
   325       this.state = newstate;
   326       state_td.className = 'row1';
   326       state_td.className = 'row1';
   327       state_td.parentNode.nextSibling.firstChild.className = 'row2';
   327       state_td.parentNode.nextSibling.firstChild.className = 'row2';
   328       
   328       
   329       // Exception - automatically scroll around if the item is off-screen
   329       // Exception - automatically scroll around if the item is off-screen
   330       var height = $(this.box_id).Height();
   330       var height = $dynano(this.box_id).Height();
   331       var top = $(this.box_id).object.scrollTop;
   331       var top = $dynano(this.box_id).object.scrollTop;
   332       var scroll_bottom = height + top;
   332       var scroll_bottom = height + top;
   333       
   333       
   334       var td_top = $(state_td.parentNode.nextSibling.firstChild).Top() - $(this.box_id).Top();
   334       var td_top = $dynano(state_td.parentNode.nextSibling.firstChild).Top() - $dynano(this.box_id).Top();
   335       var td_height = $(state_td.parentNode.nextSibling.firstChild).Height();
   335       var td_height = $dynano(state_td.parentNode.nextSibling.firstChild).Height();
   336       var td_bottom = td_top + td_height;
   336       var td_bottom = td_top + td_height;
   337       
   337       
   338       if ( td_bottom > scroll_bottom )
   338       if ( td_bottom > scroll_bottom )
   339       {
   339       {
   340         var scrollY = td_top - height + 2*td_height - 7;
   340         var scrollY = td_top - height + 2*td_height - 7;
   341         // window.console.debug(scrollY);
   341         // window.console.debug(scrollY);
   342         $(this.box_id).object.scrollTop = scrollY;
   342         $dynano(this.box_id).object.scrollTop = scrollY;
   343         /*
   343         /*
   344         var newtd = state_td.parentNode.nextSibling.firstChild;
   344         var newtd = state_td.parentNode.nextSibling.firstChild;
   345         var a = document.createElement('a');
   345         var a = document.createElement('a');
   346         var id = 'autofill' + Math.floor(Math.random() * 100000);
   346         var id = 'autofill' + Math.floor(Math.random() * 100000);
   347         a.name = id;
   347         a.name = id;
   349         newtd.appendChild(a);
   349         newtd.appendChild(a);
   350         window.location.hash = '#' + id;
   350         window.location.hash = '#' + id;
   351         */
   351         */
   352         
   352         
   353         // In firefox, scrolling like that makes the field get unfocused
   353         // In firefox, scrolling like that makes the field get unfocused
   354         $(this.field_id).object.focus();
   354         $dynano(this.field_id).object.focus();
   355       }
   355       }
   356     }
   356     }
   357     else
   357     else
   358     {
   358     {
   359       return false;
   359       return false;
   380       this.state = newstate;
   380       this.state = newstate;
   381       state_td.className = 'row1';
   381       state_td.className = 'row1';
   382       state_td.parentNode.previousSibling.firstChild.className = 'row2';
   382       state_td.parentNode.previousSibling.firstChild.className = 'row2';
   383       
   383       
   384       // Exception - automatically scroll around if the item is off-screen
   384       // Exception - automatically scroll around if the item is off-screen
   385       var top = $(this.box_id).object.scrollTop;
   385       var top = $dynano(this.box_id).object.scrollTop;
   386       
   386       
   387       var td_top = $(state_td.parentNode.previousSibling.firstChild).Top() - $(this.box_id).Top();
   387       var td_top = $dynano(state_td.parentNode.previousSibling.firstChild).Top() - $dynano(this.box_id).Top();
   388       
   388       
   389       if ( td_top < top )
   389       if ( td_top < top )
   390       {
   390       {
   391         $(this.box_id).object.scrollTop = td_top - 10;
   391         $dynano(this.box_id).object.scrollTop = td_top - 10;
   392         /*
   392         /*
   393         var newtd = state_td.parentNode.previousSibling.firstChild;
   393         var newtd = state_td.parentNode.previousSibling.firstChild;
   394         var a = document.createElement('a');
   394         var a = document.createElement('a');
   395         var id = 'autofill' + Math.floor(Math.random() * 100000);
   395         var id = 'autofill' + Math.floor(Math.random() * 100000);
   396         a.name = id;
   396         a.name = id;
   398         newtd.appendChild(a);
   398         newtd.appendChild(a);
   399         window.location.hash = '#' + id;
   399         window.location.hash = '#' + id;
   400         */
   400         */
   401         
   401         
   402         // In firefox, scrolling like that makes the field get unfocused
   402         // In firefox, scrolling like that makes the field get unfocused
   403         $(this.field_id).object.focus();
   403         $dynano(this.field_id).object.focus();
   404       }
   404       }
   405     }
   405     }
   406     else
   406     else
   407     {
   407     {
   408       $(this.box_id).object.scrollTop = 0;
   408       $dynano(this.box_id).object.scrollTop = 0;
   409       return false;
   409       return false;
   410     }
   410     }
   411   }
   411   }
   412   
   412   
   413   this.destroy = function()
   413   this.destroy = function()
   437     if ( val )
   437     if ( val )
   438       ta.value = val;
   438       ta.value = val;
   439     else if ( this.state )
   439     else if ( this.state )
   440       ta.value = this.state;
   440       ta.value = this.state;
   441     this.destroy();
   441     this.destroy();
   442     findParentForm($(this.field_id.object))._af_acting = false;
   442     findParentForm($dynano(this.field_id.object))._af_acting = false;
   443   }
   443   }
   444   
   444   
   445   this.sleep = function()
   445   this.sleep = function()
   446   {
   446   {
   447     if ( this.box_id )
   447     if ( this.box_id )
   448     {
   448     {
   449       var div = document.getElementById(this.box_id);
   449       var div = document.getElementById(this.box_id);
   450       div.style.display = 'none';
   450       div.style.display = 'none';
   451     }
   451     }
   452     var el = $(this.field_id).object;
   452     var el = $dynano(this.field_id).object;
   453     var fr = findParentForm(el);
   453     var fr = findParentForm(el);
   454     el._af_acting = false;
   454     el._af_acting = false;
   455   }
   455   }
   456   
   456   
   457   this.wake = function()
   457   this.wake = function()