summaryrefslogtreecommitdiff
path: root/js/main.js
blob: 41930fda259d44bb151e4ea64d4f35a5c4854374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var local = localStorage;
var session = sessionStorage;

$(document).ready(function() {
    // Get information storage
    getStorage();
    // Task Save
    $("#register").on("click", function() {

        if ($("#txtTask").val() == "") {
            alert("Please enter the task.");
        } else {
            if (confirm("Are you sure you want to store the task?")) {
                var key = getKeyToDate($("#datePiker").val());

                // Store localStorage data
                local.setItem(key, $("#txtTask").val());
                location.reload();
            }
        }
    });

    // Task Delete
    $("#delete").on("click", function() {
        $(":checkbox[name='storageCheck']:checked").each(function(i, v) {
            // The completed Task is deleted from localStorage.
            local.removeItem(v.value);
            session.removeItem(v.value);
            getStorage();
        });
    });
});

/**
 * Call Task data stored in Storage
 */
function getHtmlToStorage(index, storage) {
    return "<li id='"
            + "list_"
            + storage.key(index)
            + "'>"
            + storage.getItem(storage.key(index))
            + "<input type='checkbox' id='"
            + storage.key(index)
            + "' value='"
            + storage.key(index)
            + "' name='storageCheck' onclick='taskComplete(this.value)' /></li>";
}

/**
 * Get information storage
 */
function getStorage() {

    var $todayHtml = $("#todayList");
    var $beforeHtml = $("#beforeList");
    var $afterHtml = $("#afterList");
    var $datePiker = $("#datePiker");
    var $pDate = $("#pDate");
    var taskDate;
    var today = new Date();
    var year = today.getFullYear();
    var month = (today.getMonth() + 1) > 9 ? "" + (today.getMonth() + 1) : "0"
            + (today.getMonth() + 1);
    var date = today.getDate() > 9 ? "" + today.getDate() : "0"
            + today.getDate();
    var currentDate = year + month + date;

    $datePiker.val(year + "-" + month + "-" + date);
    $pDate[0].innerHTML = "";
    $todayHtml[0].innerHTML = "";
    $beforeHtml[0].innerHTML = "";
    $afterHtml[0].innerHTML = "";
    $pDate.append("Today (" + year + "." + month + "." + date + ")");

    // After task Complete, call the data temporarily stored in sessionStorage
    // to maintain the data until the browser (App) is re-executed
    for ( var i = 0; i < session.length; i++) {

        // Import sessionStorage key to compare with the current date
        taskDate = session.key(i).substring(0, 8);

        // Today Task
        if (currentDate == taskDate) {
            $todayHtml.append(getHtmlToStorage(i, session));
            // Previous Task
        } else if (currentDate > taskDate) {
            $beforeHtml.append(getHtmlToStorage(i, session));
            // After Task
        } else {
            $afterHtml.append(getHtmlToStorage(i, session));
        }
    }

    // Call Task data stored in localStorage
    for ( var i = 0; i < local.length; i++) {
        var isAppend = true;
        // Import localStorage key to compared with the current date
        taskDate = local.key(i).substring(0, 8);

        for ( var j = 0; j < session.length; j++) {
            if (local.key(i) == session.key(j)) {
                isAppend = false;
                break;
            }
        }

        if (isAppend) {
            // Today Task
            if (currentDate == taskDate) {
                $todayHtml.append(getHtmlToStorage(i, local));
                // Previous Task
            } else if (currentDate > taskDate) {
                $beforeHtml.append(getHtmlToStorage(i, local));
                // After Task
            } else {
                $afterHtml.append(getHtmlToStorage(i, local));
            }
        }
    }

    $todayHtml[0].innerHTML == "" ? $("#divToday").hide() : "";
    $beforeHtml[0].innerHTML == "" ? $("#divPrevious").hide() : "";
    $afterHtml[0].innerHTML == "" ? $("#divAfter").hide() : "";

    if (local.length == 0 && session.length == 0)
        $("#divList").hide();
}

/**
 * task complete stored in sessionStorage
 */
function taskComplete(key) {

    if ($("#" + key).is(":checked")) {

        // For completed Task, Strikeout is indicated
        $("#list_" + key).addClass("del");

        // task Complete stored in sessionStorage
        session.setItem(key, local.getItem(key));
    } else {
        $("#list_" + key).removeClass("del");
    }
}

/**
 * Create key by using the storage date
 */
function getKeyToDate(date) {
    if (date) {
        return (date.replace(/\-/g, "") + Math.random()).replace(/\./g, "");
    }
}