Resources/public/js/wikiTag.js
changeset 40 1d4af6751f5b
parent 33 6c87166b819c
child 44 c114504de4a8
child 46 6f643fc1de26
--- a/Resources/public/js/wikiTag.js	Fri Nov 18 17:42:18 2011 +0100
+++ b/Resources/public/js/wikiTag.js	Thu Nov 24 13:04:01 2011 +0100
@@ -216,87 +216,97 @@
 
 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(){
-		for(c in reactive_selectors){
-    		$(reactive_selectors[c]).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());
-	    }
-	});
+    // 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(){
+        for(c in reactive_selectors){
+            $(reactive_selectors[c]).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: "query",
+                    limit: "20",
+                    list: "search",
+                    format: "json",
+                    srsearch: request.term
+                },
+                success: function( data ) {
+                    response( $.map( data["query"]["search"], function( item ) {
+                    	console.log(item);
+                        return {
+                            label: item["title"],
+                            snippet: item["snippet"],
+                            value: item["title"]
+                        }
+                    }));
+                }
+            });
+        },
+        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" );
+            // We force width to something not too large
+            $( this ).autocomplete("widget").addClass("wikitag_context_result");
+        },
+        close: function() {
+            $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
+        }
+    });
+    $("#wikitag_wp_search_context").data("autocomplete")._renderItem = function( ul, item ) {
+        return $( "<li></li>" )
+        .data( "item.autocomplete", item )
+        .append( '<a class="wikitag_context_result_item"><span class="wikitag_context_result_title">'+ item.label + '</span> : <span class="wikitag_context_result_snippet">' + item.snippet + '</span></a>' )
+        .appendTo( ul );
+    };
+    $('#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)