$(document).ready(function(){
// ... DOM
});
$('#some_id')
$(some_DOM_element)
$(li).hover( function() { this.style.fontWeight = 'bold'; }, function() { this.style.fontWeight = 'normal'; } )
.show()
show.hide()
hide.val()
get control value.val(new_value)
set the value of the control$(form.some_element_name).dropdown( some_directory, options ) ;
jQuery.fn.dropdown = function (data, options)
{
options = options || {};
options.data = (( typeof data == "object" ) && (data.constructor == Array)) ? data : null ;
options.key = options.key || "key" ;
options.value = options.value || "value" ;
options.matchCase = options.matchCase || 0;
options.list_size = options.list_size || 11;
options.containerClass = options.containerClass || "__dropdown_container_class" ;
options.selectedItemClass = options.selectedItemClass || "__dropdown_selected_item_class" ;
options.items = options.data ? clone_quicksort(options.data, options.value, options.matchCase ? less_than_compare : less_than_ignore_case_compare) : null ;
options.items_hash = options.items ? hash_array(options.items, options.key) : null ;
this .each( function () { var input = this ; new jQuery.dropdownConstructor(input, options); } );
return this ;
}
* This source code was highlighted with Source Code Highlighter .
this.each( function() { var input = this; new jQuery.dropdownConstructor(input, options); } );
jQuery.dropdownConstructor = function (input, options)
{
var input = input; // DOM
this .control = input;
var $input = $(input); // jQuery , DOM ( jQuery )
var container = document .createElement( "div" ); // DIV list
var $container = $(container);
var list = document .createElement( "ul" );
var $list = $(list);
var active = false ;
var last_key_pressed = null ;
var timeout = null ;
var value_of_key = ""
var prev_truevalue, next_truevalue;
var that = this ;
input.dropdown = that;
input.truevalue = input.truevalue || null ;
container.appendChild(list); //
$container.hide().addClass(options.containerClass).css( "position" , "absolute" ); //
if ( options.width > 0 ) $container.css( "width" , options.width);
$( "body" ).append(container); set_truevalue(input.truevalue);
function postpone(func)
{
if (timeout) clearTimeout(timeout);
timeout = setTimeout( function (){ func(); }, 25);
}
function set_truevalue(new_truevalue)
{
if (!options.items_hash[new_truevalue])
{
input.truevalue = null ;
value_of_key = "" ;
$input.val( "" );
return ;
}
input.truevalue = new_truevalue;
if (input.truevalue) value_of_key = options.items_hash[input.truevalue][options.value]; else value_of_key = "" ;
$input.val( value_of_key );
}
input.update_control = function () { set_truevalue(that.control.truevalue); }
function activate()
{
if (active) return ;
var pos = element_position(input);
var W = (options.width > 0) ? options.width : $input.width();
$container.css( { width: parseInt(W) + "px" , left: pos.x + "px" , top: (pos.y + input.offsetHeight) + "px" } ).show();
var index = binary_search(options.items, value_of_key, options.value, options.matchCase ? less_than_compare : less_than_ignore_case_compare);
build_list(index - (options.list_size >> 1), index);
active = true ;
}
function deactivate()
{
if (!active) return ;
$container.hide();
active = false ;
}
function move_up()
{
...
}
function move_down()
{
...
}
function select_text_part(start, end)
{
...
}
* This source code was highlighted with Source Code Highlighter .
$input.keydown( function (e)
{
last_key_pressed = e.keyCode;
switch (e.keyCode)
{
case 38: e.preventDefault(); move_up(); break ;
case 40: e.preventDefault(); move_down(); break ;
case 13: case 9: select_text_part(); deactivate(); break ;
default : postpone(on_change); break ;
}
}).focus( function (){
activate(); select_text_part();
}).blur( function (){
deactivate();
}).mousedown( function (e) { if (!active && !input.disabled) { e.preventDefault(); $input.focus(); activate(); } } );
* This source code was highlighted with Source Code Highlighter .
[ { key:'m', value:'male' }, { key: 'f', value: 'female' } ]
". By default, the key field contains the key, and the value field contains the value. However, this can always be changed.function less_than_compare(val1, val2)
{
return val1 < val2;
}
function binary_search(items,value,key,compare)
{
key = key || "key" ;
compare = compare || less_than_compare;
var l = -1;
var r = items.length;
while ( true )
{
var m = (r - l) >> 1;
if (m == 0) return r;
m += l;
if ( compare(items[m][key],value) )
l = m;
else if ( compare(value, items[m][key]) )
r = m;
else
return m;
}
}
* This source code was highlighted with Source Code Highlighter .
function receive_request()
{
if (request._command == "jason" ) return "/joo.js,/boo.js" ;
if (request._command == "html" ) return "\
<tr><th> <td><INPUT name=user_name>\
<tr><th> <td><INPUT name=sex truevalue='u'>\
<tr><th> <td><INPUT name=birth_country>\
<tr><th> <td><INPUT name=country>\
" ;
if (request._command == "code" ) return "\
enterAsTab(form, 1);\
$(form.country).dropdown( jason('/boo.js') );\
$(form.birth_country).dropdown( jason('/boo.js') );\
$(form.sex).dropdown( jason('/joo.js') );\
" ;
if (request._command == "post" )
{
var error = "" ;
if (request.user_name.length == 0) error += "user_name: \r" ;
if (request.user_name.length < 5) error += "user_name: 5 \r" ;
if (request.birth_country.length == 0) error += "country: \r" ;
if (request.country.length == 0) error += "country: \r" ;
if (error) return error;
// .. ..
return "OKAY" ;
}
return "" ;
}
* This source code was highlighted with Source Code Highlighter .
activate: function (instance_name, form_name, container)
{
if (!form_loader.ready(form_name)) return false ;
var form = document .createElement( "form" ); \\ , DOM- form
var jason = function (url) { return jason_loader.data(url); } \\ , jason()
container.innerHTML = '' ;
container.appendChild(form);
form.innerHTML = "<table class='form' >" + form_loader.html(form_name) + "</table>" ;
try
{
eval(form_loader.code(form_name));
}
catch (e)
{
form_loader.forms[form_name].error = ' <i>' + form_name + '</i><BR>' + e.message;
return false ;
}
this .actor[instance_name] = { type: 'form' , name: form_name, form_elem: form, container_elem: container, disabled: [], post: null };
return true ;
}
* This source code was highlighted with Source Code Highlighter .
$( document ).ready( function (){
register_form( "foo" , "/foo.js" ); // foo /foo.js
register_form( "voo" , "/voo.js" ); //
await(load_callback); //
});
function load_callback(form_name)
{
if (form_name) //
$( '#report' ).html( $( '#report' ).html() + '<BR>' + form_loader.error(form_name) );
else
{
// !
}
}
* This source code was highlighted with Source Code Highlighter .
director.activate( 'foo_instance' , "foo" , $( '#foo_form' )[0]);
director.fill( "foo_instance" , {user_name: 'John Smith' , country: 'RO' } );
director.submit( "foo_instance" , foo_submit);
function foo_submit(instance_name, result, error)
{
if (result)
$( '#report' ).html( ' ' + instance_name + ' ' );
else if (error)
$( '#report' ).html( ' ' + instance_name + ' <BR>' + error);
else
$( '#report' ).html( ' ' + instance_name);
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/59413/
All Articles