-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
138 lines (127 loc) · 4.97 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* global Elm Pikaday */
function init(options, dev) {
var elmProgrammeBuilder;
if (dev) {
elmProgrammeBuilder = Elm;
} else {
elmProgrammeBuilder = require('./elm');
}
var app = elmProgrammeBuilder.Main.embed(options.node, {
eventId: options.eventId,
host: options.host,
showPreviewUi: options.showPreviewUi,
showPublishPage: options.showPublishPage,
showBasicPage: options.showBasicPage
});
app.ports.openDatepicker.subscribe(function(id) {
// adds pikaday to input for newly added date
var pikadayInputs = document.getElementsByClassName('pikaday-input');
if (id) {
return setTimeout(function() {
var pickNew = document.getElementById('pikaday-instance-' + id);
new Pikaday({
field: pickNew,
format: 'D MMM YYYY',
defaultDate: new Date(Date.now()),
minDate: new Date(),
onSelect: function() {
sendDates('changePickedDates');
}
});
}, 100);
}[].forEach.call(pikadayInputs, function(pikadayInput) {
new Pikaday({
field: pikadayInput,
format: 'D MMM YY',
minDate: new Date(),
defaultDate: new Date(pikadayInput.value),
onSelect: function() {
sendDates('changePickedDates');
}
});
});
document.getElementById('save-dates-btn').addEventListener('click', function() {
sendDates('changeDates');
});
function sendDates(port) {
var pikadayDateArray = [].map.call(pikadayInputs, function(pikaDayInput) {
return pikaDayInput.value;
});
app.ports[port].send(pikadayDateArray);
}
});
app.ports.showDeleteConfirmation.subscribe(function(sessionId) {
var confirmed = confirm('Are you sure? This will permanently delete the session');
if(confirmed){
app.ports['deleteSession'].send(sessionId);
}
});
app.ports.showDeleteInformationConfirmation.subscribe(function(savedInfoId) {
var confirmed = confirm('Are you sure? This will permanently the information');
if(confirmed){
app.ports['deleteInformation'].send(savedInfoId);
}
});
// thanks to https://github.com/phylor/elm-image-upload/blob/master/index.html
app.ports.fileSelected.subscribe(function (id) {
var node = document.getElementById('file-to-save-' + id);
if (node === null || !node.files[0]) {
return app.ports.fileContentRead.send(null);
}
// If your file upload field allows multiple files, you might
// want to consider turning this into a `for` loop.
var file = node.files[0];
var reader = new FileReader();
// FileReader API is event based. Once a file is selected
// it fires events. We hook into the `onload` event for our reader.
reader.onload = (function(event) {
// The event carries the `target`. The `target` is the file
// that was selected. The result is base64 encoded contents of the file.
var base64encoded = event.target.result;
// We build up the `FilePortData` object here that will be passed to our Elm
// runtime through the `fileContentRead` subscription.
var portData = {
id: id,
contents: base64encoded,
filename: file.name,
infoTitle: document.getElementById('info-title-' + id).value,
infoDescription: document.getElementById('info-description-' + id).value
};
// We call the `fileContentRead` port with the file data
// which will be sent to our Elm runtime via Subscriptions.
app.ports.fileContentRead.send(portData);
});
// Connect our FileReader with the file that was selected in our `input` node.
reader.readAsDataURL(file);
});
app.ports.fileChanged.subscribe(function (savedInfoId) {
var node = document.getElementById('file-to-change-' + savedInfoId);
if (node === null || !node.files[0]) {
return app.ports.changedFileContentRead.send(null);
}
// If your file upload field allows multiple files, you might
// want to consider turning this into a `for` loop.
var file = node.files[0];
var reader = new FileReader();
// FileReader API is event based. Once a file is selected
// it fires events. We hook into the `onload` event for our reader.
reader.onload = (function(event) {
// The event carries the `target`. The `target` is the file
// that was selected. The result is base64 encoded contents of the file.
var base64encoded = event.target.result;
// We build up the `FilePortData` object here that will be passed to our Elm
// runtime through the `fileContentRead` subscription.
var portData = {
id: savedInfoId,
contents: base64encoded,
filename: file.name
};
// We call the `fileContentRead` port with the file data
// which will be sent to our Elm runtime via Subscriptions.
app.ports.changedFileContentRead.send(portData);
});
// Connect our FileReader with the file that was selected in our `input` node.
reader.readAsDataURL(file);
});
}
module.exports.init = init;