-
Notifications
You must be signed in to change notification settings - Fork 1
/
preferencesRenderer.js
105 lines (91 loc) · 2.75 KB
/
preferencesRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*jshint esversion: 6 */
/* This is the javascript rederer code for the preferences popup
* The html file is preference.html
* The look and feel is slightly different on first load that
* when selected from the menu as on first load preferences must be supplied
* */
const electron = require('electron');
const {ipcRenderer} = electron;
const dialog = require('electron').remote.dialog;
var PRF = (function() {
"use strict";
//listeners
ipcRenderer.on('showPreferences', function(event, data) {
populatePreferences(data);
});
//private functions
var getPreferences, validateData, savePreferences, populatePreferences, selectPath;
$(document).ready(function() {
//add event handlers
$('#save-btn').on('click', function() {
savePreferences();
});
$('#cancel-btn').on('click', function() {
ipcRenderer.send('close:preferences');
});
$('.directory_select').on('click', function(event) {
selectPath(event);
});
});
getPreferences = function() {
var data, labels, value;
labels = ['initialisation', 'transcriber', 'default_directory', 'default_basetext_directory'];
data = {};
for (let i = 0; i < labels.length; i += 1) {
value = null;
value = document.getElementById(labels[i]).value;
if (value !== null && value.trim() !== '' && value !== undefined) {
data[labels[i]] = value;
}
}
return data;
};
validateData = function(data) {
var labels;
labels = ['initialisation', 'transcriber', 'default_directory', 'default_basetext_directory'];
for (let i = 0; i < labels.length; i += 1) {
if (!data.hasOwnProperty(labels[i])) {
return false;
}
}
return true;
};
//public functions
savePreferences = function() {
var data = getPreferences();
if (validateData(data)) {
ipcRenderer.send('save:preferences', data);
return;
}
alert('You must provide all of the information.');
return;
};
populatePreferences = function(data) {
var key;
for (key in data.preferences) {
if (data.preferences.hasOwnProperty(key)) {
document.getElementById(key).value = data.preferences[key];
}
}
if (!data.preferences.hasOwnProperty('initialisation')) {
$('#first_time_splash').hide();
$('#cancel-btn').prop('disabled', false);
}
};
selectPath = function(event) {
var optns, target_id;
optns = {
properties: ['openDirectory']
};
target_id = event.target.id;
if (document.getElementById(target_id).value !== '') {
optns.defaultPath = document.getElementById(target_id).value;
}
dialog.showOpenDialog(
optns,
function(fileNames) {
document.getElementById(target_id).value = fileNames[0];
}
);
};
}());