files.mjs 4.87 KB
Newer Older
1
"use strict";
2
import { resetFilters, resetModal, handleError, userData, setUserData, translations } from "../render.js";
3
4
5
6
7
8
import { _paq } from "./matomo.mjs";
import { createModalJail } from "../configs/modal.config.mjs";

const btnOpenTodoFile = document.getElementById("btnOpenTodoFile");
const modalChangeFile = document.getElementById("modalChangeFile");
const modalChangeFileTable = document.getElementById("modalChangeFileTable");
9
10
const fileTabBarList = document.querySelector("#fileTabBar ul");

11
function removeFileFromList(isActive, index) {
12
  try {
13
14
15
16
17
18
19
    if(isActive && index-1 === -1) {
      userData.files[index+1][0] = 1;
    } else if(isActive && index-1 >= 0) {
      userData.files[index-1][0] = 1;
    }
    userData.files.splice(index, 1);
    setUserData("files", userData.files);
20
21
    resetFilters().then(function(response) {
      console.info(response);
22
23
      index = userData.files.findIndex(file => file[0] === 1);
      window.api.send("startFileWatcher", userData.files[index][1]);
24
25
26
    }).catch(function(error) {
      handleError(error);
    });
27
    return Promise.resolve("Success: File removed from list");
28
29
30
31
32
  } catch (error) {
    return Promise.reject(error);
  }
}

33
function selectFileFromList(index) {
34
35
36
37
38
39
40
  try {
    resetFilters().then(function(response) {
      console.info(response);
    }).catch(function(error) {
      handleError(error);
    });
    resetModal().then(response => {
41
      window.api.send("startFileWatcher", userData.files[index][1]);
42
43
44
45
      console.info(response);
    }).catch(error => {
      handleError(error);
    });
46
    return Promise.resolve("Success: File selected");
47
48
49
50
51
  } catch (error) {
    return Promise.reject(error);
  }
}

52
53
54
55
function generateFileList() {
  try {
    if(userData.files.length>1 && userData.fileTabs) {
      fileTabBar.classList.add("is-active");
56
    } else {
57
      fileTabBar.classList.remove("is-active");
58
    }
59
    fileTabBarList.innerHTML = null;
60
    modalChangeFileTable.innerHTML = null;
61
    modalChangeFileTable.classList.add("files");
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    for (let i = 0; i < userData.files.length; i++) {
      let isActive = userData.files[i][0];
      let fileName = userData.files[i][1].split("/").pop();
      let listItem = document.createElement("li");
      listItem.innerHTML = fileName;
      listItem.innerHTML += "<i class=\"fas fa-minus-circle\"></i>";
      if(isActive===1) listItem.classList.add("is-highlighted");
      listItem.querySelector("i").onclick = function() {
        removeFileFromList(isActive, i);
      }
      if(!isActive) {
        listItem.onclick = function(event) {
          if(event.target.classList.contains("fas")) return false;
          selectFileFromList(i).then(function(response) {
            console.info(response);
          }).catch(function(error) {
            handleError(error);
          });
          // trigger matomo event
          if(userData.matomoEvents) _paq.push(["trackEvent", "File-Tab", "Click on tab"]);
        }
      }
      fileTabBarList.appendChild(listItem);
85
      let row = modalChangeFileTable.insertRow(-1);
86
87
88
      let cell1 = row.insertCell(0);
      let cell2 = row.insertCell(1);
      let cell3 = row.insertCell(2);
89
      if(userData.files[i][0]===1) {
ransome1's avatar
ransome1 committed
90
        cell1.innerHTML = "<button disabled>" + translations.selected + "</button>";
91
      } else {
ransome1's avatar
ransome1 committed
92
        cell1.innerHTML = "<button tabindex=\"0\">" + translations.select + "</button>";
93
        cell1.onclick = function() {
94
          selectFileFromList(i).then(function(response) {
ransome1's avatar
ransome1 committed
95
96
97
98
            console.info(response);
          }).catch(function(error) {
            handleError(error);
          });
99
100
101
102
          // trigger matomo event
          if(userData.matomoEvents) _paq.push(["trackEvent", "File", "Click on select button"]);
        }
      }
103
104
105
106
107
108
109
110
111
112
113
114
115
      cell2.innerHTML = userData.files[i][1];
      cell3.innerHTML = "<a href=\"#\" tabindex=\"0\"><i class=\"fas fa-minus-circle\"></i></a>";
      cell3.title = translations.delete;
      cell3.onclick = function() {
        removeFileFromList(isActive, i);
        generateFileList().then(response => {
          modalChangeFile.classList.add("is-active");
          modalChangeFile.focus();
          console.info(response);
        }).catch(error => {
          handleError(error);
        });
      }
116
117
118
119
120
121
122
123
124
    }
    return Promise.resolve("Success: File changer modal built and opened");
  } catch (error) {
    return Promise.reject(error);
  }
}

btnOpenTodoFile.onclick = function() {
  if(typeof userData.files === "object" && userData.files.length>0) {
125
    generateFileList().then(response => {
126
      console.info(response);
127
128
129
      modalChangeFile.classList.add("is-active");
      modalChangeFile.focus();
      createModalJail(modalChangeFile);
130
131
132
133
134
135
136
137
138
    }).catch(error => {
      handleError(error);
    });
  } else {
    window.api.send("openOrCreateFile", "open");
  }
  // trigger matomo event
  if(userData.matomoEvents) _paq.push(["trackEvent", "Menu", "Click on Files"]);
}
139

140
export { generateFileList, removeFileFromList };