06-07serialization

by ZooeyLai

HTML

<div class="wrap">
        <div class="form-group">
            <input type="text" id="newTask" class="form-input">
            <select name="statusType" id="statusType" class="form-input" >
                <option value="" selected disabled>狀態</option>
                <option value="normal" selected>一般</option>
                <option value="important">重要</option>
                <option value="urgent">緊急</option>
            </select>
            <button class="btn btn-primary" onclick="add()">新增</button>
            
        </div>

        <div id="root"></div>
        <div class="btn-box">
            <button class="btn btn-primary" onclick="exportTask()">匯出</button>
            <button class="btn btn-outline-primary" onclick="saveTask()">儲存</button>
            <button class="btn btn-outline-danger" onclick="clearTask()">清空</button>                 
        </div>

    </div>

CSS

:root{
        --primary: #4f46e5;
        --secondary:#2563eb;
        --secondary100: #dbeafe;
        --gray: #e2e8f0;
        --gray800: #333;
        --white: #fff;
        --background:#e4e4e4;
        --warning100 :#ffedd5;
        --warning : #ea580c;
        --danger100 :#fee2e2;
        --danger : #dc2626;
        --success : #059669;
        --success100: #ecfdf5;
    }    
    *,
    *::after,
    *::before {
        padding: 0px;
        margin: 0px;
        /* font-family: inherit; */
        box-sizing: border-box;
        line-height: 1.5;
        font-size: 1rem;
    }
    html,
    body {
        height: 100vh;
        
    }

    body {
        font-family: sans-serif;
        
        display: flex;
        /* flex-direction: column;
        align-items: center;
        justify-content: center; */
        background: var(--background);
    }
    .wrap{
        width: 600px;
        margin: 100px auto;
    }
    ul,li{
        list-style: none;
    }
    .form-group{
        margin-bottom: 1rem;
        display: flex;
        flex-flow: row;
        justify-content: space-between;
        gap: 1rem;
    }
    .form-input {
        border: 1px solid #94a3b8;
        padding: 0.5rem 1rem;
        border-radius: 0.25rem;
        flex: auto;
    }

        .form-input:focus {
        border-color: var(--primary);
        box-shadow: 0px 0px 0px 3px rgba(79, 70, 229, 0.4);
        outline: none;
    }

    .task-list li{
        padding: 1.5rem 1rem;
        display: flex;
        align-items: center;
        gap: 0.75rem;
        border-left: 4px solid #c1c1c1;
        border-radius: 0.25rem;
        background-color: var(--white);
    }
    .task-label{
        flex: auto;
    }
    .task-list li + li{
        margin: 0.5rem 0;
    }
    .task-list li:last-child{
        margin-bottom: 1rem;
    }
    .task-list li span.done{
        flex: none;
        color: var(--success);
        display: none;
        font-weight: 600;
    }
    .btn {
      ...

JavaScript

'use strict'; //嚴格模式
        // let todos =[];
        let todos = [
            { 
                title: '倒垃圾' ,
                category: "normal",
                isCompleted: false
            },
            { 
                title: '繳電話費' ,
                category: "important",
                isCompleted: false
            },
            { 
                title: '採買本週食材' ,
                category: "urgent",
                isCompleted: false
            },
        ];
        let root = document.querySelector('#root');
        let tasksHolder = document.createElement('ul');
        tasksHolder.classList.add('task-list');
        root.append(tasksHolder);

        function render(){
            tasksHolder.textContent = ''; //重新渲染之前清除任務列表
            todos.forEach((task, index) => {
                let taskList = document.createElement('li'); 
                let doneEl = document.createElement('span');
                let newTaskEl = document.createElement('span'); 
                let deleteBtn = document.createElement('button');
                let toggleBtn = document.createElement('button');

                tasksHolder.append(taskList);
                taskList.append(doneEl);
                taskList.append(newTaskEl);
                taskList.append(toggleBtn);
                taskList.append(deleteBtn);

                newTaskEl.classList.add('task-label');
                toggleBtn.classList.add('btn', 'btn-outline-primary');
                deleteBtn.classList.add('btn','btn-outline-danger');

                doneEl.textContent = '(已完成)';
                newTaskEl.textContent = task.title;
                toggleBtn.textContent = task.isCompleted ? '標示為已完成' : '標示為未完成';
                deleteBtn.textContent = '刪除';
                
                // 列表狀態
                if(task.category === 'important'){
                    taskList.classList.add('status-warning');
                }else if(task.category === 'urgent'){
                   ...