/** * Output Javascript for tags autocompletion. * @todo dh> a more facebook like widget would be: http://plugins.jquery.com/project/facelist * "ListBuilder" is being planned for jQuery UI: http://wiki.jqueryui.com/ListBuilder */ function echo_autocomplete_tags() { ?> <script type="text/javascript"> function init_autocomplete_tags( selector ) { var tags = jQuery( selector ).val(); var tags_json = new Array(); if( tags.length > 0 ) { // Get tags from <input> tags = tags.split( ',' ); for( var t in tags ) { tags_json.push( { id: tags[t], title: tags[t] } ); } } jQuery( selector ).tokenInput( '<?php echo get_samedomain_htsrv_url() . 'anon_async.php?action=get_tags'; ?> ', { theme: 'facebook', queryParam: 'term', propertyToSearch: 'title', tokenValue: 'title', preventDuplicates: true, prePopulate: tags_json, hintText: '<?php echo TS_('Type in a tag'); ?> ', noResultsText: '<?php echo TS_('No results'); ?> ', searchingText: '<?php echo TS_('Searching...'); ?> ' } ); } jQuery( document ).ready( function() { if( jQuery( '#suggest_item_tags' ).is( ':checked' ) ) { init_autocomplete_tags( '#item_tags' ); } jQuery( '#suggest_item_tags' ).click( function() { if( jQuery( this ).is( ':checked' ) ) { // Use plugin to suggest tags jQuery( '#item_tags' ).hide(); init_autocomplete_tags( '#item_tags' ); } else { // Remove autocomplete tags plugin jQuery( '#item_tags' ).show(); jQuery( '#item_tags' ).parent().find( 'ul.token-input-list-facebook' ).remove(); } } ); <?php // Don't submit a form by Enter when user is editing the tags echo get_prevent_key_enter_js('#token-input-item_tags'); ?> } ); </script> <?php }
/** * Registers headlines required to autocomplete the user logins * * @param string alias, url or filename (relative to rsc/css, rsc/js) for JS/CSS files * @param string Library: 'hintbox', 'typeahead' */ function init_autocomplete_login_js($relative_to = 'rsc_url', $library = 'hintbox') { global $blog; require_js('#jquery#', $relative_to); // dependency switch ($library) { case 'typeahead': // Use typeahead library of bootstrap add_js_headline('jQuery( document ).ready( function() { jQuery( "input.autocomplete_login" ).typeahead( null, { displayKey: "login", source: function ( query, cb ) { jQuery.ajax( { url: "' . get_secure_htsrv_url() . 'async.php?action=get_login_list", type: "post", data: { q: query, data_type: "json" }, dataType: "JSON", success: function( logins ) { var json = new Array(); for( var l in logins ) { json.push( { login: logins[ l ] } ); } cb( json ); } } ); } } ); ' . get_prevent_key_enter_js('input.autocomplete_login') . ' } );'); break; case 'hintbox': default: // Use hintbox plugin of jQuery // Add jQuery hintbox (autocompletion). // Form 'username' field requires the following JS and CSS. // fp> TODO: think about a way to bundle this with other JS on the page -- maybe always load hintbox in the backoffice // dh> Handle it via http://www.appelsiini.net/projects/lazyload ? // dh> TODO: should probably also get ported to use jquery.ui.autocomplete (or its successor) require_css('jquery/jquery.hintbox.css', $relative_to); require_js('jquery/jquery.hintbox.min.js', $relative_to); add_js_headline('jQuery( document ).on( "focus", "input.autocomplete_login", function() { var ajax_params = ""; if( jQuery( this ).hasClass( "only_assignees" ) ) { ajax_params = "&user_type=assignees&blog=' . $blog . '"; } jQuery( this ).hintbox( { url: "' . get_secure_htsrv_url() . 'async.php?action=get_login_list" + ajax_params, matchHint: true, autoDimentions: true } ); ' . get_prevent_key_enter_js('input.autocomplete_login') . ' } );'); break; } }