Skip to content

Commit

Permalink
Fix TTLApp#889: Adapting calendar window to electron isolated environ…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
araujoarthur0 committed Oct 19, 2023
1 parent 0e26631 commit 647c9c0
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 140 deletions.
2 changes: 1 addition & 1 deletion __tests__/__renderer__/notification-channel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const notificationChannel = require('../../js/notification-channel.js');
const notificationChannel = require('../../renderer/notification-channel.js');

describe('Notifications channel', () =>
{
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__renderer__/window-aux.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('window-aux.js Testing', function()
// });
// });

describe('showDialogSync(options, successCallback)', function()
describe('showDialogSync(options)', function()
{
test('Does not crash', async() =>
{
Expand All @@ -104,7 +104,7 @@ describe('window-aux.js Testing', function()
const options = {
title: 'Time to Leave',
};
windowAux.showDialogSync(options, () =>
windowAux.showDialogSync(options).then(() =>
{
return;
});
Expand Down
2 changes: 2 additions & 0 deletions esm-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { handleSquirrelEvent } = require('./js/squirrel.js');
const { showAlert, showDialogSync } = require('./js/window-aux.js');

import { appConfig } from './js/app-config.js';
import { setupCalendarStore } from './main/calendar-aux.js';

if (appConfig.win32)
{
Expand Down Expand Up @@ -134,6 +135,7 @@ app.on('ready', () =>
}
createWindow();
createMenu();
setupCalendarStore();
setLanguageChangedCallback(createMenu);
triggerStartupDialogs();
setInterval(refreshOnDayChange, 60 * 60 * 1000);
Expand Down
17 changes: 10 additions & 7 deletions js/main-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
const { getCurrentTranslation } = require('../src/configs/i18next.config');
let { contextMenu, tray } = require('./windows.js');

import { getDefaultWidthHeight, getUserPreferences } from './user-preferences.js';
import { getDefaultWidthHeight, getUserPreferences, switchCalendarView } from './user-preferences.js';
import { appConfig, getDetails } from './app-config.js';
import { createLeaveNotification } from './notification.js';

Expand Down Expand Up @@ -80,7 +80,8 @@ function createWindow()
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
preload: path.join(__dirname, '../renderer/preload-scripts/calendar-bridge.js'),
contextIsolation: true
}
});

Expand All @@ -100,14 +101,16 @@ function createWindow()
tray.setContextMenu(contextMenu);
});

ipcMain.on('RESIZE_MAIN_WINDOW', (event, width, height) =>
ipcMain.on('RESIZE_MAIN_WINDOW', () =>
{
mainWindow.setSize(width, height);
const widthHeight = getDefaultWidthHeight();
mainWindow.setSize(widthHeight.width, widthHeight.height);
});

ipcMain.on('VIEW_CHANGED', (event, savedPreferences) =>
ipcMain.on('SWITCH_VIEW', () =>
{
mainWindow.webContents.send('PREFERENCE_SAVED', savedPreferences);
const preferences = switchCalendarView();
mainWindow.webContents.send('PREFERENCES_SAVED', preferences);
});

ipcMain.on('RECEIVE_LEAVE_BY', (event, element) =>
Expand Down Expand Up @@ -186,7 +189,7 @@ function proposeFlexibleDbMigration()
if (response === 0 /*migrate*/)
{
const migrateResult = migrateFixedDbToFlexible();
getMainWindow().webContents.send('RELOAD_CALENDAR');
mainWindow.webContents.send('RELOAD_CALENDAR');
if (migrateResult['result'] === true)
{
dialog.showMessageBox(BrowserWindow.getFocusedWindow(),
Expand Down
4 changes: 2 additions & 2 deletions js/menus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { app, BrowserWindow, clipboard, dialog, shell } = require('electron');
const { app, BrowserWindow, clipboard, dialog, shell, ipcMain } = require('electron');
const path = require('path');
const Store = require('electron-store');

Expand Down Expand Up @@ -145,7 +145,7 @@ function getEditMenuTemplate(mainWindow)
if (savedPreferences !== null)
{
savePreferences(savedPreferences);
mainWindow.webContents.send('PREFERENCE_SAVED', savedPreferences);
mainWindow.webContents.send('PREFERENCES_SAVED', savedPreferences);
}
});
prefWindow.webContents.on('before-input-event', (event, input) =>
Expand Down
14 changes: 0 additions & 14 deletions js/notification-channel.js

This file was deleted.

1 change: 1 addition & 0 deletions js/time-balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ async function computeAllTimeBalanceUntilAsync(limitDate)
}, 1);
});
}

export {
computeAllTimeBalanceUntilAsync,
computeAllTimeBalanceUntil,
Expand Down
43 changes: 43 additions & 0 deletions main/calendar-aux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const { ipcMain } = require('electron');

import { computeAllTimeBalanceUntilAsync } from '../js/time-balance.js';

const Store = require('electron-store');

const flexibleStore = new Store({name: 'flexible-store'});

function getFlexibleStore()
{
return flexibleStore.store;
}

function setupCalendarStore()
{
ipcMain.handle('GET_FLEXIBLE_STORE_CONTENTS', () =>
{
return getFlexibleStore();
});

ipcMain.handle('SET_FLEXIBLE_STORE_DATA', (event, key, contents) =>
{
flexibleStore.set(key, contents);
return true;
});

ipcMain.handle('DELETE_FLEXIBLE_STORE_DATA', (event, key) =>
{
flexibleStore.delete(key);
return true;
});

ipcMain.handle('COMPUTE_ALL_TIME_BALANCE_UNTIL', (event, targetDate) =>
{
return computeAllTimeBalanceUntilAsync(targetDate);
});
}

export {
setupCalendarStore
};
54 changes: 16 additions & 38 deletions renderer/classes/BaseCalendar.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
'use strict';

const Store = require('electron-store');
const { ipcRenderer } = require('electron');

import {
hourMinToHourFormatted,
isNegative,
subtractTime,
sumTime,
validateTime
} from '../../js/time-math.js';
import {
formatDayId,
displayWaiverWindow
} from '../workday-waiver-aux.js';
import { showDay, switchCalendarView } from '../../js/user-preferences.js';
import { getDateStr, getMonthLength } from '../../js/date-aux.js';
import { computeAllTimeBalanceUntilAsync } from '../../js/time-balance.js';
import { generateKey } from '../../js/date-db-formatter.js';
import { getTranslationInLanguageData } from '../i18n-translator.js';

// Global values for calendar
const flexibleStore = new Store({name: 'flexible-store'});
const waivedWorkdays = new Store({name: 'waived-workdays'});

// Holds the calendar information and manipulation functions
class BaseCalendar
{
Expand All @@ -34,8 +21,6 @@ class BaseCalendar
{
this._calendarDate = new Date();
this.updateLanguageData(languageData);
this.loadInternalStore();
this.loadInternalWaiveStore();
this.updatePreferences(preferences);
this._initCalendar();
}
Expand Down Expand Up @@ -74,7 +59,7 @@ class BaseCalendar
_updateAllTimeBalance()
{
const targetDate = this._getTargetDayForAllTimeBalance();
computeAllTimeBalanceUntilAsync(targetDate)
window.mainApi.computeAllTimeBalanceUntilPromise(targetDate)
.then(balance =>
{
const balanceElement = $('#overall-balance');
Expand Down Expand Up @@ -158,10 +143,10 @@ class BaseCalendar
/**
* Reloads internal DBs based on external DBs and then redraws the calendar.
*/
reload()
async reload()
{
this.loadInternalStore();
this.loadInternalWaiveStore();
await this.loadInternalStore();
await this.loadInternalWaiveStore();
this.redraw();
}

Expand Down Expand Up @@ -192,14 +177,6 @@ class BaseCalendar
calendar._updateTimeDayCallback($(this).attr('data-date'));
});

$('.waiver-trigger').off('click').on('click', function()
{
// deepcode ignore no-invalid-this: jQuery use
const dayId = $(this).closest('tr').attr('id').substr(3);
const waiverDay = formatDayId(dayId);
displayWaiverWindow(waiverDay);
});

this._updateAllTimeBalance();
}

Expand Down Expand Up @@ -408,11 +385,12 @@ class BaseCalendar
/**
* Stores year data in memory to make operations faster
*/
loadInternalStore()
async loadInternalStore()
{
this._internalStore = {};

for (const entry of flexibleStore)
const flexibleStore = await window.mainApi.getFlexibleStoreContents();
for (const entry of Object.entries(flexibleStore))
{
const key = entry[0];
const value = entry[1];
Expand All @@ -424,11 +402,12 @@ class BaseCalendar
/**
* Stores waiver data in memory to make operations faster
*/
loadInternalWaiveStore()
async loadInternalWaiveStore()
{
this._internalWaiverStore = {};

for (const entry of waivedWorkdays)
const waivedWorkdays = await window.mainApi.getWaiverStoreContents();
for (const entry of Object.entries(waivedWorkdays))
{
const date = entry[0];
const reason = entry[1]['reason'];
Expand All @@ -455,7 +434,7 @@ class BaseCalendar
_setStore(key, newValues)
{
this._internalStore[key] = { values: newValues };
flexibleStore.set(key, this._internalStore[key]);
window.mainApi.setFlexibleStoreData(key, this._internalStore[key]);
}

/*
Expand All @@ -464,19 +443,19 @@ class BaseCalendar
_removeStore(key)
{
this._internalStore[key] = undefined;
flexibleStore.delete(key);
window.mainApi.deleteFlexibleStoreData(key);
}

/**
* Calls showDay from user-preferences.js passing the last preferences set.
* Checks based on last set preferences if the day can be shown.
* @param {number} year
* @param {number} month
* @param {number} day
* @return {Boolean}
*/
_showDay(year, month, day)
{
return showDay(year, month, day, this._preferences);
return window.mainApi.showDay(year, month, day, this._preferences);
}

/**
Expand Down Expand Up @@ -722,16 +701,15 @@ class BaseCalendar
_togglePunchButton(enable)
{
$('#punch-button').prop('disabled', !enable);
ipcRenderer.send('TOGGLE_TRAY_PUNCH_TIME', enable);
window.mainApi.toggleTrayPunchTime(enable);
}

/**
* Switches the calendar from Month to Day view.
*/
_switchView()
{
const preferences = switchCalendarView();
ipcRenderer.send('VIEW_CHANGED', preferences);
window.mainApi.switchView();
}
}

Expand Down
12 changes: 5 additions & 7 deletions renderer/classes/CalendarFactory.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
'use strict';

const { ipcRenderer } = require('electron');

import { getDefaultWidthHeight} from '../../js/user-preferences.js';
import { FlexibleMonthCalendar } from './FlexibleMonthCalendar.js';
import { FlexibleDayCalendar } from './FlexibleDayCalendar.js';

class CalendarFactory
{
static getInstance(preferences, languageData, calendar = undefined)
static async getInstance(preferences, languageData, calendar = undefined)
{
const view = preferences['view'];
const widthHeight = getDefaultWidthHeight();
if (view !== 'day' && view !== 'month')
{
throw new Error(`Could not instantiate ${view}`);
Expand All @@ -23,9 +19,11 @@ class CalendarFactory
{
if (calendar !== undefined && calendar.constructor.name !== constructorName)
{
ipcRenderer.send('RESIZE_MAIN_WINDOW', widthHeight.width, widthHeight.height);
window.mainApi.resizeMainWindow();
}
return new CalendarClass(preferences, languageData);
calendar = new CalendarClass(preferences, languageData);
await calendar.reload();
return calendar;
}
else
{
Expand Down
Loading

0 comments on commit 647c9c0

Please sign in to comment.