Resources/public/js/wikiTag.js
changeset 31 b910b4f7485f
parent 30 d2fba1e3b94b
child 33 6c87166b819c
--- a/Resources/public/js/wikiTag.js	Wed Nov 09 16:25:13 2011 +0100
+++ b/Resources/public/js/wikiTag.js	Wed Nov 09 17:33:26 2011 +0100
@@ -214,6 +214,89 @@
     });
 }
 
+function wikitag_init_search_context_events()
+{
+	// We implement the behaviour on text select. Kolich is just an object name, it could be anything
+	if(!window.Kolich){
+	    Kolich = {};
+	}
+	Kolich.Selector = {};
+	Kolich.Selector.getSelected = function(){
+	    var t = '';
+	    if(window.getSelection){
+	        t = window.getSelection();
+	    }else if(document.getSelection){
+	        t = document.getSelection();
+	    }else if(document.selection){
+	        t = document.selection.createRange().text;
+	    }
+	    return t;
+	}
+	Kolich.Selector.mouseup = function(e){
+	  var st = Kolich.Selector.getSelected();
+	  if(st!=''){
+	    // Behaviour after the text was selected
+	    $("#wikitag_context_div").offset({left:e.pageX+10,top:e.pageY});
+	    $("#wikitag_context_div").show();
+	    $("#wikitag_context_div #wikitag_wp_search_context").val(st);
+	    $("#wikitag_context_div #wikitag_wp_search_context").autocomplete("search");
+	  }
+	}
+	$(document).ready(function(){
+	    $(document).bind("mouseup", Kolich.Selector.mouseup);
+	});
+	
+	// Function to close the context window
+	$("#wikitag_context_close").click(function(e){
+	    $("#wikitag_context_div #wikitag_wp_search_context").autocomplete("close");
+	    $("#wikitag_context_div").offset({left:0,top:0});
+	    $("#wikitag_context_div").hide();
+	});
+	
+	// Wikipedia search management (new tag)
+	$("#wikitag_wp_search_context").autocomplete({
+	    source: function( request, response ) {
+	        $.ajax({
+	            url: "http://fr.wikipedia.org/w/api.php",
+	            dataType: "jsonp",
+	            data: {
+	                action: "opensearch",
+	                limit: "20",
+	                namespace: "0",
+	                format: "json",
+	                search: request.term
+	            },
+	            success: function( data ) {
+	                response( $.map( data[1], function( item ) {
+	                    return {
+	                        label: item,
+	                        value: item
+	                    }
+	                }));
+	            }
+	        });
+	    },
+	    select: function(event, ui) { 
+	        // Since the event still did not update wp_search's val, we force it.
+	        $("#wikitag_wp_search_context").val(ui.item.label);
+	        add_tag($("#wikitag_wp_search_context").val());
+	        $("#wikitag_context_close").click();
+	    },
+	    minLength: 2,
+	    open: function() {
+	        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
+	    },
+	    close: function() {
+	        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
+	    }
+	});
+	$('#wikitag_wp_search_context').keyup(function(e){
+	    if((e.keyCode==13) && ($("#wikitag_wp_search_context").val()!="")){
+	        add_tag($("#wikitag_wp_search_context").val());
+	    }
+	});
+}
+
 function wikitag_update_tag(btn)
 {
     new_checked = false;