/**
 * Tatoeba Project, free collaborative creation of multilingual corpuses project
 * Copyright (C) 2009  HO Ngoc Phuong Trang <tranglich@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


$(document).ready(function() {
    $(document).watch("addrule", function() {
        var sentenceId = -1;
        /*
         * Display the "check" green icon for a short time
         * after sentence has been added to list.
         */
        function feedbackValid(){
            $("#sentence"+sentenceId+"_saved_in_list").show(); // TODO Set up a better system for this thing.
            // Because you can't have the "in process" AND the "valid" at the same time.
            // It will be useful for favoriting as well.
            setTimeout(removeFeedback, 500);
        }

        /*
         * Remove the "check" green icon.
         */
        function removeFeedback(){
            $("#sentence"+sentenceId+"_saved_in_list").hide();
        }

        // Clicking on "Add to list" displays the list selection.
        // Reclicking on it hides the list selection.
        $(".addToList").off();
        $(".addToList").click(function(){
            sentenceId = $(this).attr("data-sentence-id");
            $("#addToList"+sentenceId).toggle();
        });

        // The sentence is added to the list after user has clicked
        // the button. I was hesitating between this solution and
        // having the sentence added to the list onChange, that is,
        // directly after the selection in the <select>.
        $(".validateButton").off();
        $(".validateButton").click(function(){
            sentenceId = this.dataset.sentenceId;
            var listId = $("#listSelection"+sentenceId).val();
            var rootUrl = get_tatoeba_root_url();

            // Add sentence to selected list
            if(listId > 0){
                $("#_"+sentenceId+"_in_process").show();

                $.get(
                    rootUrl
                    + "/sentences_lists/add_sentence_to_list/"
                    + sentenceId + "/" + listId
                    , {}
                    ,function(data){
                        if(!data.match('error')){
                            $('#listSelection'+sentenceId).val(-1);
                            $('#listSelection'+sentenceId+' option[value="'+data+'"]').remove();
                            feedbackValid(sentenceId);
                        }
                        $("#_"+sentenceId+"_in_process").hide();
                    },
                    'html'
                );
            }
            // Create a new list and add sentence to this new list
            else if(listId == -1){
                var txt = 'Name of the list : <br />'
                + '<input type="text" id="listName" name="listName" />';

                // callback for the popup
                function mycallbackform(value, message, form){

                    if(value != undefined){ // need to check this, otherwise it loops indefinitely when canceling...

                        $("#_"+sentenceId+"_in_process").show();

                        $.post(
                            rootUrl
                            + "/sentences_lists/add_sentence_to_new_list/"
                            + sentenceId + "/"+ form.listName
                            , {}
                            ,function(data){
                                if(data != 'error'){
                                    $('#listSelection'+sentenceId).append(
                                        '<option value="'+ data +'">'
                                        + form.listName
                                        +'</option>'
                                    );
                                    $('#listSelection'+sentenceId).val(data);
                                    feedbackValid(sentenceId);

                                }else{
                                    $.prompt("Sorry, an error occured.");
                                }
                                $("#_"+sentenceId+"_in_process").hide();
                            },
                            'html'
                        );
                    }
                }
                // popup to enter name of new list
                $.prompt(txt,{
                    callback: mycallbackform,
                    buttons: { Ok: 'OK'}
                });
            }
            // redirect to lists
            else if(listId == -2){
                $(location).attr(
                    'href',
                    rootUrl
                    + "/sentences_lists/"
                );
            }
        });
    });
});

@@ -922,708 +922,8 @@
  %7B%0D%0A
-        var sentenceId = -1;%0D%0A        /*%0D%0A         * Display the %22check%22 green icon for a short time%0D%0A         * after sentence has been added to list.%0D%0A         */%0D%0A        function feedbackValid()%7B%0D%0A            $(%22#sentence%22+sentenceId+%22_saved_in_list%22).show(); // TODO Set up a better system for this thing.%0D%0A            // Because you can't have the %22in process%22 AND the %22valid%22 at the same time.%0D%0A            // It will be useful for favoriting as well.%0D%0A            setTimeout(removeFeedback, 500);%0D%0A        %7D%0D%0A%0D%0A        /*%0D%0A         * Remove the %22check%22 green icon.%0D%0A         */%0D%0A        function removeFeedback()%7B%0D%0A            $(%22#sentence%22+sentenceId+%22_saved_in_list%22).hide();%0D%0A        %7D%0D%0A%0D%0A
     
@@ -977,32 +977,32 @@
 ist selection.%0D%0A
+
         // Recli
@@ -1119,32 +1119,36 @@
 )%7B%0D%0A            
+let 
 sentenceId = $(t
@@ -1604,16 +1604,20 @@
         
+let 
 sentence
@@ -1659,19 +1659,19 @@
         
-var
+let
  listId 
@@ -1723,19 +1723,19 @@
         
-var
+let
  rootUrl
@@ -1757,24 +1757,235 @@
 root_url();%0D
+%0A            let inProcess = $(%22#_%22 + sentenceId + %22_in_process%22);%0D%0A            let listSelection = $(%22#listSelection%22 + sentenceId);%0D%0A            let feedback = $(%22#sentence%22 + sentenceId + %22_saved_in_list%22); %0D
 %0A%0D%0A         
@@ -2070,40 +2070,17 @@
     
-$(%22#_%22+sentenceId+%22_in_p
+inP
 rocess
-%22)
 .sho
@@ -2309,32 +2309,75 @@
 unction(data)%7B%0D%0A
+                        inProcess.hide();%0D%0A
                 
@@ -2431,36 +2431,32 @@
                 
-$('#
 listSelection'+s
@@ -2456,92 +2456,15 @@
 tion
-'+sentenceId).val(-1);%0D%0A                            $('#listSelection'+sentenceId+' 
+.find('
 opti
@@ -2478,14 +2478,18 @@
 e=%22'
-+
+ + 
 data
-+
+ + 
 '%22%5D'
@@ -2537,32 +2537,42 @@
 feedback
-Valid(sentenceId
+.show(0).delay(500).hide(0
 );%0D%0A    
@@ -2598,74 +2598,8 @@
  %7D%0D%0A
-                        $(%22#_%22+sentenceId+%22_in_process%22).hide();%0D%0A
     

Fix UI feedback when adding several sentences to lists