PHP Classes

File: twzToDoList.js

Recommend this page to a friend!
  Classes of Tony   twz PHP To Do List Project   twzToDoList.js   Download  
File: twzToDoList.js
Role: Auxiliary script
Content type: text/plain
Description: js
Class: twz PHP To Do List Project
Manage to do tasks of project on a Web page
Author: By
Last change: new version
Date: 5 years ago
Size: 8,207 bytes
 

Contents

Class file image Download
/*
    twzToDoList.js Javascript for twzToDoList.class.php



    Copyright (C) 2019 Tony Phelps
    ---------------------------------------------------------------------
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
    ---------------------------------------------------------------------
    email: dev@tweezy.net.au
    post: P O Box 200 Kingston Tas 7051 AUSTRALIA
    ---------------------------------------------------------------------
*/

var isDirty = false;

document.addEventListener("DOMContentLoaded", function() {

    var clearChkExpand = function () {
        var elem = document.getElementById("chkExpand");
        if(elem) { elem.checked = false; }
        };

    var expandTextarea = function (elem) {
        if(elem && elem.value != '' && elem.scrollHeight > 55)
            {
            elem.style.height = "1px";
            elem.style.height = (5 + elem.scrollHeight)+"px";
            }
        };

    var showTaskInfo = function (taskInfo) {
        taskInfo.style.display = "block";
        // expand textarea to fit initial content..
        // ..we can't do this on page load because if textarea is hidden its scrollHeight is zero!
        expandTextarea(taskInfo.getElementsByTagName("textarea")[0]);
        };

    // ______________________________
    // expand ProjectDescn textarea..
    expandTextarea(document.getElementById("ProjectDescn"));

    // __________________________
    // toggle extra task fields..
    var task = document.getElementsByClassName("TaskState");
    for(var i=0; i < task.length; i++)
        {
        task[i].style.cursor = "pointer";
        task[i].addEventListener("click", function(){
            var info = this.parentNode.getElementsByClassName("taskInfo")[0];

            if(info.style.display == "block")
                { info.style.display = "none"; }
            else
                { showTaskInfo(info); }
            //clearChkExpand(); // something odd here - see TODO list [9/6/19]
            });
        }
    // ____________________________________________________
    // show extra task fields if TaskSeq|TaskName changes..
    var task = document.getElementsByClassName("tasktrigger");
    for(var i=0; i < task.length; i++)
        {
        task[i].addEventListener("input", function(){
            var info = this.parentNode.getElementsByClassName("taskInfo")[0];
            showTaskInfo(info);
            clearChkExpand();
            });
        }
    // _________________________________________
    // set taskDirty[] when any task input changes..
    // we don't worry if any field is changed back to its defaultValue; once changed we'll assume it's dirty
    var tasklist = document.getElementById("tasklist");
    if(tasklist)
        {
        var input = tasklist.querySelectorAll("input[type=text],textarea");
        for(var i=0; i < input.length; i++)
            {
            input[i].oninput = function(){ // setting .oninput (not addEventListener) so it can be called by setnow
                var index = this.name.replace(/[^0-9]/g,''); // extracts 34 from fldName[34] .. BUT we must ensure no digits except inside [] !!
                document.getElementsByName("taskDirty["+index+"]")[0].value=1;
                isDirty = true;
                };
            }
        }

    // _________________________________________
    // set prDirty when project|preset data changes..
    var input = document.getElementsByClassName("prData");
    for(var i=0; i < input.length; i++)
        {
        input[i].oninput = function(){ // setting .oninput (not addEventListener) so it can be called by setnow
            document.getElementsByName("prDirty")[0].value=1;
            isDirty = true;
            };
        }
    // chkArchive should also trigger this
    var elem = document.getElementById("chkArchive");
    if(elem)
        {
        elem.addEventListener("change", function(){
            document.getElementsByName("prDirty")[0].value=1;
            isDirty = true;
            });
        }

    // _________________________________________
    // tasks expander..
    var elem = document.getElementById("chkExpand");
    if(elem)
        {
        elem.addEventListener("change", function(){
            var info = document.getElementsByClassName("taskInfo");
            for(var i=0; i < info.length; i++)
                {
                if(this.checked)
                    { showTaskInfo(info[i]); }
                else
                    { info[i].style.display = "none"; }
                }
            });
        }


    // _________________________________________
    // hide/show tasklist depending on lstPreset combo..
    var elem = document.getElementById("lstPreset");
    if(elem)
        {
        elem.addEventListener("change", function(){
            isDirty = true;
            document.getElementById("tasklist").style.display = (this.value == 0) ? 'block' : 'none';
            });
        }


    // _________________________________________
    // search box..
    var elem = document.getElementById("findText");
    if(elem)
        {
        elem.addEventListener("focus", function(){
            if(this.value!="") { document.getElementById("SearchExtra").style.display = "block"; }
            });
        elem.addEventListener("input", function(){
            document.getElementById("SearchExtra").style.display = (this.value=="") ? "none" : "block";
            });
        }

    // _________________________________________
    // set datetime field to 'now'..
    var elem = document.getElementsByClassName("setnow");
    for(var i=0; i < elem.length; i++)
        {
        elem[i].title="set date to now";
        elem[i].addEventListener("click", function(e){
            var inp = this.parentNode.querySelector("input[type=text]");
            if(inp)
                {
                inp.value = "now";
                inp.oninput.apply(inp); // call oninput to trigger taskDirty/isDirty
                }
            e.preventDefault();
            });
        }

    // _________________________________________
    // clear isDirty on form submit..
    var elem = document.getElementsByTagName("form");
    for(var i=0; i < elem.length; i++)
        { elem[i].addEventListener("submit", function() { isDirty = false; }); }


    // _________________________________________
    // change favicon if any due/overdue..
    var icon = "favicon.ico";
    var elem = document.getElementsByClassName("status-overdue");
    if(elem.length > 0) { icon = "favicon_late.ico"; }
    else
        {
        elem = document.getElementsByClassName("status-due");
        if(elem.length > 0) { icon = "favicon_due.ico"; }
        }

    var link = document.querySelector("link[rel*=icon]") || document.createElement("link");
    link.type = "image/x-icon";
    link.rel = "shortcut icon";
    link.href = icon;
    document.getElementsByTagName("head")[0].appendChild(link);

    }); // end DOMContentLoaded


window.addEventListener("beforeunload", function(e) {
    if(isDirty)
        {
        var msg = "Changes have not been saved. Are you sure you want to leave this page?";
        e.returnValue = msg; // Gecko and Trident
        return msg; // Gecko and WebKit
        // 1/6/19: none of Firefox|Edge|Chrome actually show this msg; just a generic one - ok
        }
    });