/*
 * todolist.js
 * part of elabftw.net project
 * adapted from http://web.koesbong.com/2011/01/24/sortable-and-editable-to-do-list-using-html5s-localstorage/
 */

$(function() {

    /*
     * FUNCTIONS
     */

    function remove($this) {
        var parentId = $this.parent().attr('id');
        
        // Remove todo list from localStorage based on the id of the clicked parent element
        localStorage.removeItem(
            "'" + parentId + "'"
        );
        
        // Fade out the list item then remove from DOM
        $this.parent().fadeOut(function() { 
            $this.parent().remove();
            
            regenerate();
        });
    }

    function regenerate() {
        var $todoItemLi = $('#show-items li');
        // Empty the order array
        order.length = 0;
        
        // Go through the list item, grab the ID then push into the array
        $todoItemLi.each(function() {
            var id = $(this).attr('id');
            order.push(id);
        });
        
        // Convert the array into string and save to localStorage
        localStorage.setItem(
            'todo-orders', order.join(',')
        );
    }
    
    function clearAll() {
        var $todoListLi = $('#show-items li');
        
        order.length = 0;
        localStorage.clear();
        $todoListLi.remove();
    }

    /*
     * VARS
     */

    var i = Number(localStorage.getItem('todo-counter')) + 1,
        j = 0,
        k,
        $form = $('#todo-form'),
        $removeLink = $('#show-items li a'),
        $itemList = $('#show-items'),
        $clearAll = $('#clear-all'),
        $newTodo = $('#todo'),
        order = [],
        orderList;

    /*
     * START
     */

    // Initial loading of todo items
    orderList = localStorage.getItem('todo-orders');
    
    orderList = orderList ? orderList.split(',') : [];
    
    // display items
    for( j = 0, k = orderList.length; j < k; j++) {
        $itemList.append(
            "<li id='" + orderList[j] + "'>"
            + localStorage.getItem(orderList[j]) 
            + " <a href='#'>X</a></li>"
        );
    }


    // Add a todo item
    $form.submit(function(e) {
        e.preventDefault();
        if ($newTodo.val() !== "") {
            // Take the value of the input field and save it to localStorage
            localStorage.setItem( 
                "todo-" + i, $newTodo.val() 
            );
            
            // Set the todo max counter so on page refresh it keeps going up instead of reset
            localStorage.setItem('todo-counter', i);
            
            // Append a new list item with the value of the new todo list
            $itemList.append(
                "<li id='todo-" + i + "'>"
                + "<span class='editable'>"
                + localStorage.getItem("todo-" + i) 
                + " </span><a href='#'>x</a></li>"
            );

            regenerate();

            // Hide the new list, then fade it in for effects
            $("#todo-" + i)
                .css('display', 'none')
                .fadeIn();
            
            // Empty the input field
            $newTodo.val("");
            
            i++;
        }
    });

    // Remove todo
    $itemList.delegate('a', 'click', function(e) {
        var $this = $(this);
        
        e.preventDefault();
        remove($this);
    });
    
    
    // Clear all
    $clearAll.click(function(e) {
        e.preventDefault();
        clearAll();
    });

    // Fade In and Fade Out the Remove link on hover
    $itemList.delegate('li', 'mouseover mouseout', function(event) {
        var $this = $(this).find('a');
        
        if(event.type === 'mouseover') {
            $this.stop(true, true).fadeIn();
        } else {
            $this.stop(true, true).fadeOut();
        }
    });
});

@@ -11,16 +11,156 @@
 list.js%0A
+ * this file is minified in todolist.min.js, and this is what is used in the code%0A * so if you change stuff here, don't forget to minify ;)%0A
  * part 
@@ -182,16 +182,16 @@
 project%0A
-
  * adapt
@@ -422,33 +422,24 @@
 attr('id');%0A
-        %0A
         // R
@@ -595,25 +595,16 @@
      );%0A
-        %0A
         
@@ -693,17 +693,16 @@
 tion() %7B
- 
 %0A       
@@ -727,37 +727,24 @@
 ).remove();%0A
-            %0A
             
@@ -910,25 +910,17 @@
 th = 0;%0A
-        
 %0A
+
         
@@ -1104,25 +1104,17 @@
     %7D);%0A
-        
 %0A
+
         
@@ -1261,21 +1261,16 @@
 ;%0A    %7D%0A
-    %0A
     func
@@ -1330,33 +1330,25 @@
 items li');%0A
-        
 %0A
+
         orde
@@ -1887,21 +1887,17 @@
 ders');%0A
-    
 %0A
+
     orde
@@ -1943,21 +1943,17 @@
 ) : %5B%5D;%0A
-    
 %0A
+
     // d
@@ -2136,17 +2136,16 @@
 List%5Bj%5D)
- 
 %0A       
@@ -2425,17 +2425,16 @@
 setItem(
- 
 %0A       
@@ -2469,17 +2469,16 @@
 do.val()
- 
 %0A       
@@ -2485,29 +2485,17 @@
      );%0A
-            
 %0A
+
         
@@ -2633,29 +2633,17 @@
 r', i);%0A
-            
 %0A
+
         
@@ -2876,17 +2876,16 @@
 o-%22 + i)
- 
 %0A       
@@ -3128,29 +3128,17 @@
 deIn();%0A
-            
 %0A
+
         
@@ -3200,21 +3200,9 @@
 %22);%0A
-            
 %0A
+
     
@@ -3332,25 +3332,17 @@
 (this);%0A
-        
 %0A
+
         
@@ -3396,18 +3396,9 @@
 %7D);%0A
-    %0A    
 %0A
+
     
@@ -3662,24 +3662,16 @@
 d('a');%0A
-        
 %0A       
@@ -3673,16 +3673,17 @@
       if
+ 
 (event.t

remove whitespaces from todolist.js