Resources/public/js/wikiTag.js
changeset 2 13f43f53d0ba
child 11 5f038a505cd7
equal deleted inserted replaced
1:06a22ff5d58d 2:13f43f53d0ba
       
     1 // -*- coding: utf-8 -*-
       
     2 function wikitag_init_tags_events()
       
     3 {
       
     4     // Tag simple operations : activate/unactivate wp link, reset wp info, remove wp link, remove tag from list
       
     5     $(".wikitag_reset_wp_info").click(function(e){
       
     6         if(confirm("Confirmez-vous le rétablissement du label original de ce tag ?")){
       
     7             wikitag_update_tag(this);
       
     8         }
       
     9     });
       
    10     $(".wikitag_remove_wp_link").click(function(e){
       
    11         if(confirm("Confirmez-vous le suppression du lien Wikipédia pour le tag \"" + $(this).attr('alt') + "\" ?")){
       
    12             wikitag_update_tag(this);
       
    13         }
       
    14     });
       
    15     $(".wikitag_remove_tag_from_list").click(function(){
       
    16         if(confirm("Confirmez-vous la suppression du tag \"" + $(this).attr('alt') + "\" de la liste courante ?")){
       
    17             wikitag_update_tag(this);
       
    18         }
       
    19     });
       
    20     
       
    21     // Wikipedia search management (autocompletion and save changes)
       
    22     $.editable.addInputType('autocomplete', {
       
    23     	element : $.editable.types.text.element,
       
    24     	plugin : function(settings, original) {
       
    25     		$('input', this).autocomplete(settings.autocomplete);
       
    26     	}
       
    27     });
       
    28     $(".wikipediatag").editable(modify_tag_url, { 
       
    29     	indicator : "<img src='"+static_url+"images/indicator.gif'>",
       
    30     	type      : "autocomplete",
       
    31     	tooltip   : "Cliquer pour éditer...",
       
    32     	onblur    : "submit",
       
    33     	submitdata: {
       
    34             csrfmiddlewaretoken:global_csrf_token, 
       
    35             wikitag_document_id:$('#wikitag_document_id').val(),
       
    36             num_page:$('#num_page').val(),
       
    37             nb_by_page:$('#nb_by_page').val(),
       
    38             sort:$('#sort').val(),
       
    39             searched:$('#searched_str').val()
       
    40         },
       
    41     	callback  : function(value, settings) {
       
    42             $('#wikitag_table_container').html(value);
       
    43             wikitag_init_tags_events();
       
    44     	},
       
    45 		onerror: function(settings, original, jqXHR) {
       
    46 			resp = $.parseJSON(jqXHR.responseText);
       
    47 			alert(resp.message);
       
    48 			original.reset();
       
    49 		},
       
    50     	autocomplete : {
       
    51 			source: function( request, response ) {
       
    52 				$.ajax({
       
    53 					url: "http://fr.wikipedia.org/w/api.php",
       
    54 					dataType: "jsonp",
       
    55 					data: {
       
    56 						action: "opensearch",
       
    57 						limit: "20",
       
    58 						namespace: "0",
       
    59 						format: "json",
       
    60 						search: request.term
       
    61 					},
       
    62 					success: function( data ) {
       
    63 						response( $.map( data[1], function( item ) {
       
    64 							return {
       
    65 								label: item,
       
    66 								value: item
       
    67 							}
       
    68 						}));
       
    69 					}
       
    70 				});
       
    71 			},
       
    72 			minLength: 2,
       
    73 			open: function() {
       
    74 				$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
       
    75 			},
       
    76 			close: function() {
       
    77 				$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
       
    78 			}
       
    79     	}
       
    80     });
       
    81     
       
    82     // Update alias management
       
    83     $(".wikitag_alias").editable(update_tag_alias_url, {
       
    84     	indicator : "<img src='"+static_url+"images/indicator.gif'>",
       
    85     	type      : "text",
       
    86     	placeholder:"",
       
    87     	tooltip   : "Cliquer pour éditer...",
       
    88     	onblur    : "submit",
       
    89     	submitdata: {
       
    90             csrfmiddlewaretoken:global_csrf_token, 
       
    91             wikitag_document_id:$('#wikitag_document_id').val(),
       
    92             num_page:$('#num_page').val(),
       
    93             nb_by_page:$('#nb_by_page').val(),
       
    94             sort:$('#sort').val(),
       
    95             searched:$('#searched_str').val()
       
    96         },
       
    97     	callback  : function(value, settings) {
       
    98             $('#wikitag_table_container').html(value);
       
    99             wikitag_init_tags_events();
       
   100     	}
       
   101     });
       
   102     
       
   103     // Tag categories management
       
   104     $(".wikitag_category").editable(update_tag_category_url, {
       
   105     	indicator : "<img src='"+static_url+"/images/indicator.gif'>",
       
   106     	type      : "select",
       
   107         data      : categories_list,
       
   108     	placeholder:"",
       
   109     	tooltip   : "Cliquer pour éditer...",
       
   110     	onblur    : "submit",
       
   111     	submitdata: {
       
   112             csrfmiddlewaretoken:global_csrf_token,
       
   113             wikitag_document_id:$('#wikitag_document_id').val(),
       
   114             num_page:$('#num_page').val(),
       
   115             nb_by_page:$('#nb_by_page').val(),
       
   116             sort:$('#sort').val(),
       
   117             searched:$('#searched_str').val()
       
   118         },
       
   119     	callback  : function(value, settings) {
       
   120             $('#wikitag_table_container').html(value);
       
   121             wikitag_init_tags_events();
       
   122     	}
       
   123     });
       
   124     
       
   125     // Tag table drag and drop
       
   126     $("#wikitag_table").tableDnD({
       
   127         onDragClass: "wikitag_dragged_row",
       
   128         onDrop: function(table, row){
       
   129             old_order = row.id;
       
   130             $($(row).children()[1]).html("<img src='"+static_url+"/images/indicator.gif'/>");
       
   131             rows = table.tBodies[0].rows;
       
   132             nb_rows = rows.length;
       
   133             for(var i=1; i<nb_rows; i++){
       
   134                 if(rows[i].id==old_order){
       
   135                     new_order = i; // No need to +1 because rows[0] are headers
       
   136                     $.ajax({
       
   137                         url: tag_up_down_url,
       
   138                         type: 'POST',
       
   139                         data: {csrfmiddlewaretoken:global_csrf_token, 
       
   140                                wikitag_document_id:$('#wikitag_document_id').val(),
       
   141                                new_order:new_order,
       
   142                                old_order:old_order
       
   143                                },
       
   144                         // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType
       
   145                         //dataType: 'json',
       
   146                         success: function(msg, textStatus, XMLHttpRequest) {
       
   147                             $('#wikitag_table_container').html(msg);
       
   148                             wikitag_init_tags_events();
       
   149                         }
       
   150                     });
       
   151                 }
       
   152             }
       
   153         },
       
   154         dragHandle: "wikitag_updown_td"
       
   155     });
       
   156 }
       
   157 
       
   158 function wikitag_init_datasheet_events()
       
   159 {
       
   160     var select_done = false;
       
   161     // Wikipedia search management (new tag)
       
   162     $("#wikitag_wp_search").autocomplete({
       
   163         source: function( request, response ) {
       
   164             $.ajax({
       
   165                 url: "http://fr.wikipedia.org/w/api.php",
       
   166                 dataType: "jsonp",
       
   167                 data: {
       
   168                     action: "opensearch",
       
   169                     limit: "20",
       
   170                     namespace: "0",
       
   171                     format: "json",
       
   172                     search: request.term
       
   173                 },
       
   174                 success: function( data ) {
       
   175                     response( $.map( data[1], function( item ) {
       
   176                         return {
       
   177                             label: item,
       
   178                             value: item
       
   179                         }
       
   180                     }));
       
   181                 }
       
   182             });
       
   183         },
       
   184         select: function(event, ui) { 
       
   185             // Since the event still did not update wp_search's val, we force it.
       
   186             $("#wikitag_wp_search").val(ui.item.label);
       
   187             select_done = true;
       
   188             $("#wikitag_ok_search").click();
       
   189         },
       
   190         minLength: 2,
       
   191         open: function() {
       
   192             $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
       
   193         },
       
   194         close: function() {
       
   195             $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
       
   196         }
       
   197     });
       
   198     $('#wikitag_wp_search').keyup(function(e){
       
   199         if((e.keyCode==13) && ($("#wikitag_wp_search").val()!="") && (select_done==false)){
       
   200             add_tag($("#wikitag_wp_search").val());
       
   201         }
       
   202         select_done = false;
       
   203     });
       
   204     $("#wikitag_ok_search").click(function(){
       
   205         if($("#wikitag_wp_search").val()!=""){
       
   206             add_tag($("#wikitag_wp_search").val());
       
   207         }
       
   208     });
       
   209     $("#wikitag_tags_sort").click(function(e){
       
   210     	 e.preventDefault();
       
   211     	if(confirm("Confirmez-vous le tri des tags ?")) {
       
   212     		reorder_tags();
       
   213     	}
       
   214     });
       
   215     /*
       
   216     // Validate sheet management : the radiobutton name has is "'gr_validated' + datasheet.hda_id"
       
   217     $("#validated").click(function(e){
       
   218         e.preventDefault();
       
   219         // We count the number of tags. It has to be between 5 and 25
       
   220         var nb_tags = $('#tag_table tr').length - 1;
       
   221         if(nb_tags<5 || nb_tags>25){
       
   222             alert("Cette fiche n'est pas validable : elle doit contenir entre 5 et 25 tags.");
       
   223         }
       
   224         else{
       
   225             if(confirm("Confirmez-vous la validation de cette fiche ? Elle contient " + nb_tags + " tags.")){
       
   226                 window.location = validate_datasheet_url + "/" + $('#wikitag_document_id').val() + "/true";
       
   227             }
       
   228         }
       
   229     });
       
   230     $("#not_validated").click(function(e){
       
   231         e.preventDefault();
       
   232         if(confirm("Confirmez-vous l'invalidation de cette fiche ?")){
       
   233             window.location = validate_datasheet_url + "/" + $('#wikitag_document_id').val() + "/false";
       
   234         }
       
   235     });
       
   236     */
       
   237 }
       
   238 
       
   239 function wikitag_update_tag(btn)
       
   240 {
       
   241     new_checked = false;
       
   242     if ($(btn).is(".wikitag_remove_tag_from_list")) {
       
   243         var url = remove_tag_from_list_url;
       
   244         var id_tag = $(btn).attr('id');
       
   245     }
       
   246     else if ($(btn).is(".wikitag_reset_wp_info")) {
       
   247         var url = reset_wp_info_url;
       
   248         var id_tag = $(btn).html();
       
   249         $(btn).html("<img src='"+static_url+"/images/indicator.gif'>");
       
   250     }
       
   251     else if ($(btn).is(".wikitag_remove_wp_link")) {
       
   252         var url = remove_wp_link_url;
       
   253         var id_tag = $(btn).attr('id');
       
   254     }
       
   255     
       
   256     // 2 cases : 
       
   257     // - ordered tag for one datasheet : $('#wikitag_document_id') is not null
       
   258     // - all tags list : $('#wikitag_document_id') is null and $('#num_page') and $('#nb_by_page') are not null
       
   259     $.ajax({
       
   260         url: url,
       
   261         type: 'POST',
       
   262         data: {csrfmiddlewaretoken:global_csrf_token, 
       
   263                wikitag_document_id:$('#wikitag_document_id').val(),
       
   264                num_page:$('#num_page').val(),
       
   265                nb_by_page:$('#nb_by_page').val(),
       
   266                sort:$('#sort').val(),
       
   267                searched:$('#searched_str').val(),
       
   268                tag_id:id_tag,
       
   269                activated:new_checked
       
   270                },
       
   271         // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType
       
   272         //dataType: 'json',
       
   273         success: function(msg, textStatus, XMLHttpRequest) {
       
   274             $('#wikitag_table_container').html(msg);
       
   275             wikitag_init_tags_events();
       
   276         },
       
   277 		error: function(jqXHR, textStatus, errorThrown) {
       
   278 			resp = $.parseJSON(jqXHR.responseText);
       
   279 			alert(resp.message);
       
   280 			$(btn).html(id_tag);
       
   281 		}
       
   282     });
       
   283 }
       
   284 
       
   285 function wikitag_up_down(arrow)
       
   286 {
       
   287     if ($(arrow).is(".up")) {
       
   288         mv = "u";
       
   289     } else {
       
   290         mv = "d";
       
   291     }
       
   292     var url = tag_up_down_url;
       
   293     var id_tag = $(arrow).attr('id');
       
   294     // This indicates the position (from 0) of the tag in the list. NB : it is different from the TagSheet.order in the database.
       
   295     var pos_tag = $(arrow).attr('pos');
       
   296     $.ajax({
       
   297         url: url,
       
   298         type: 'POST',
       
   299         data: {csrfmiddlewaretoken:global_csrf_token, 
       
   300                wikitag_document_id:$('#wikitag_document_id').val(),
       
   301                tag_id:id_tag,
       
   302                move:mv,
       
   303                tag_pos:pos_tag
       
   304                },
       
   305         // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType
       
   306         //dataType: 'json',
       
   307         success: function(msg, textStatus, XMLHttpRequest) {
       
   308             $('#wikitag_table_container').html(msg);
       
   309             wikitag_init_tags_events();
       
   310         }
       
   311     });
       
   312 }
       
   313 
       
   314 function add_tag(tag_label)
       
   315 {
       
   316     $("#wikitag_ok_search").html("<img src='"+static_url+"/images/indicator.gif'>");
       
   317     var url = add_tag_url;
       
   318     $.ajax({
       
   319         url: url,
       
   320         type: 'POST',
       
   321         data: {csrfmiddlewaretoken:global_csrf_token,
       
   322                wikitag_document_id:$('#wikitag_document_id').val(),
       
   323                value:tag_label
       
   324                },
       
   325         // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType
       
   326         //dataType: 'json',
       
   327         success: function(msg, textStatus, XMLHttpRequest) {
       
   328             $('#wikitag_table_container').html(msg);
       
   329             wikitag_init_tags_events();
       
   330             // And scroll to the bottom
       
   331             $("html").animate({ scrollTop: $(document).height() }, 500);
       
   332         },
       
   333         error: function(jqXHR, textStatus, errorThrown) {
       
   334 			resp = $.parseJSON(jqXHR.responseText);
       
   335 			alert(resp.message);
       
   336         },
       
   337         complete: function(){
       
   338             // We empty the input and hide the ok button
       
   339             $("#wikitag_wp_search").val("");
       
   340             $("#wikitag_ok_search").html("<b>OK</b>");
       
   341         }
       
   342     });
       
   343 }
       
   344 
       
   345 function reorder_tags() {
       
   346 	$('#wikitag_tags_sort').attr("disabled", "disabled");
       
   347 	var tag_sort_old_src = $("#wikitag_tags_sort").attr("src");
       
   348 	$("#wikitag_tags_sort").attr("src",static_url+"images/indicator.gif");
       
   349 	$.ajax({
       
   350 		url: reorder_tag_datasheet_url,
       
   351 		type: 'POST',
       
   352 		data: {
       
   353 			csrfmiddlewaretoken:global_csrf_token,
       
   354             wikitag_document_id:$('#wikitag_document_id').val()
       
   355 		},
       
   356         success: function(msg, textStatus, XMLHttpRequest) {
       
   357             $('#wikittag_table_container').html(msg);
       
   358             wikitag_init_tags_events();
       
   359             // And scroll to the bottom
       
   360             $("html").animate({ scrollTop: $(document).height() }, 500);
       
   361         },
       
   362         complete: function(){
       
   363         	$("#wikitag_tags_sort").attr("src",tag_sort_old_src);
       
   364         	$('#wikitag_tags_sort').removeAttr("disabled");
       
   365         }
       
   366 	});
       
   367 }
       
   368