files.mjs 5.92 KB
Newer Older
1
"use strict";
2
import { resetFilters, resetModal, handleError, userData, appData, setUserData, translations, showOnboarding } 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(index, isTabItem) {
12
  try {
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
    if(isTabItem) {
      let newItemIndex;
      userData.files[index][0] = 0;
      userData.files[index][2] = 0;
      newItemIndex = userData.files.findIndex(file => {
        return file[2] === 1;
      });
      if(newItemIndex >= 0) {
        userData.files[newItemIndex][0] = 1;
        userData.files[newItemIndex][2] = 1;
        resetFilters(true).then(function(response) {
          console.log(response);
          setUserData("files", userData.files);
          window.api.send("startFileWatcher", [userData.files[newItemIndex][1], 1]);
        }).catch(function(error) {
          handleError(error);
        });
      } else {
        userData.files[index][0] = 0;
        userData.files[index][2] = 0;
        setUserData("files", userData.files);
        if(userData.files.length>0) generateFileList();
        showOnboarding(true);
      }
    } else {
      if(userData.files[index][0]) {
        showOnboarding(true);
      }
      userData.files.splice(index, 1);
      setUserData("files", userData.files);
43
44
    }
    return Promise.resolve("Success: File removed from list");
45
46
47
48
49
  } catch (error) {
    return Promise.reject(error);
  }
}

50
function selectFileFromList(index) {
51
  try {
52
    resetFilters(false).then(function(response) {
53
54
55
56
57
58
59
60
61
      console.info(response);
    }).catch(function(error) {
      handleError(error);
    });
    resetModal().then(response => {
      console.info(response);
    }).catch(error => {
      handleError(error);
    });
62
    window.api.send("startFileWatcher", [userData.files[index][1], 1]);
63
    return Promise.resolve("Success: File selected");
64
65
66
67
68
  } catch (error) {
    return Promise.reject(error);
  }
}

69
70
function generateFileList() {
  try {
71
    fileTabBar.classList.remove("is-active");
72
    fileTabBarList.innerHTML = null;
73
    modalChangeFileTable.innerHTML = null;
74
    modalChangeFileTable.classList.add("files");
75
    let j = 0;
76
77
    for (let i = 0; i < userData.files.length; i++) {
      let isActive = userData.files[i][0];
78
79
80
81
82
83
84
85
86
87
88
89
90
      let isTabItem = userData.files[i][2];
      if(isTabItem) {
        j++;
        let fileName = userData.files[i][1].split("/").pop();
        if(j > 1 && userData.fileTabs) fileTabBar.classList.add("is-active");
        if(appData.os === "windows") fileName = userData.files[i][1].split("\\").pop();
        let listItem = document.createElement("li");
        listItem.setAttribute("title", userData.files[i][1]);
        listItem.innerHTML = fileName;
        listItem.innerHTML += "<i class=\"fas fa-times\"></i>";
        if(isActive===1) listItem.classList.add("is-highlighted");
        listItem.querySelector("i").onclick = function() {
          removeFileFromList(i, isTabItem);
91
          // trigger matomo event
92
93
94
95
96
97
98
99
100
101
102
103
104
          if(userData.matomoEvents) _paq.push(["trackEvent", "File-Tab", "Click on remove icon"]);
        }
        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"]);
          }
105
        }
106
        fileTabBarList.appendChild(listItem);
107
      }
108
      let row = modalChangeFileTable.insertRow(-1);
109
110
111
      let cell1 = row.insertCell(0);
      let cell2 = row.insertCell(1);
      let cell3 = row.insertCell(2);
112
      if(userData.files[i][0]===1) {
ransome1's avatar
ransome1 committed
113
        cell1.innerHTML = "<button disabled>" + translations.selected + "</button>";
114
      } else {
ransome1's avatar
ransome1 committed
115
        cell1.innerHTML = "<button tabindex=\"0\">" + translations.select + "</button>";
116
        cell1.onclick = function() {
117
          selectFileFromList(i).then(function(response) {
ransome1's avatar
ransome1 committed
118
119
120
121
            console.info(response);
          }).catch(function(error) {
            handleError(error);
          });
122
          // trigger matomo event
123
          if(userData.matomoEvents) _paq.push(["trackEvent", "File-Chooser", "Click on select button"]);
124
125
        }
      }
126
127
128
129
      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() {
130
        removeFileFromList(i);
131
132
133
134
135
136
137
        generateFileList().then(response => {
          modalChangeFile.classList.add("is-active");
          modalChangeFile.focus();
          console.info(response);
        }).catch(error => {
          handleError(error);
        });
138
139
        // trigger matomo event
        if(userData.matomoEvents) _paq.push(["trackEvent", "File-Chooser", "Click on remove button"]);
140
      }
141
142
143
144
145
146
147
148
149
    }
    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) {
150
    generateFileList().then(response => {
151
      console.info(response);
152
153
154
      modalChangeFile.classList.add("is-active");
      modalChangeFile.focus();
      createModalJail(modalChangeFile);
155
156
157
158
159
160
161
162
163
    }).catch(error => {
      handleError(error);
    });
  } else {
    window.api.send("openOrCreateFile", "open");
  }
  // trigger matomo event
  if(userData.matomoEvents) _paq.push(["trackEvent", "Menu", "Click on Files"]);
}
164

165
export { generateFileList, removeFileFromList };