-
Notifications
You must be signed in to change notification settings - Fork 1
/
ie.js
235 lines (187 loc) · 6.1 KB
/
ie.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
var ui_export = require('./ui_export');
var ui_import = require('./ui_import');
var Bind = require('github/jxmono/bind');
var Events = require('github/jxmono/events');
module.exports = function (config) {
var self = this;
self['import'] = self['import'] || {};
self.config = config;
self.inbox = [];
self.$ = {};
self.config.binds = self.config.binds || [];
Events.call(self, config);
// configure the external events
self.on('reset', reset);
self.on('setTemplate', setTemplate);
self.on('setQuery', setQuery);
self.on('export', startExport);
// process the export UI only if the configuration is present
if (self.config['export'] && self.config['export'].ui) {
ui_export.call(self);
// run the binds
for (var i = 0; i < self.config.binds.length; ++i) {
Bind.call(self, self.config.binds[i]);
}
}
// process the import UI only if the configuration is present
if (self.config['import'] && self.config['import'].ui) {
ui_import.call(self);
// run the binds
for (var i = 0; i < self.config.binds.length; ++i) {
Bind.call(self, self.config.binds[i]);
}
}
// start by getting all the templates
getTemplates.call(self);
}
function reset () {
var self = this;
// common properties
delete self['import'].template;
// import properties
delete self.mappings;
delete self.columns;
// export properties
delete self.query;
}
function setTemplate (templateId) {
var self = this;
if (!templateId) {
self.emit('reset');
return;
}
self['import'].template = JSON.parse(JSON.stringify(self.templates[templateId]));
}
function setQuery (query, options) {
var self = this;
self.query = query;
self.queryOptions = options;
}
function startExport() {
var self = this;
self['export'] = self['export'] || {};
if (!self.query) {
alert('No data query set for export');
return;
}
if (!self['export'].template) {
alert('No data template set for export');
return;
}
var columns = [];
if (self['export'].columns) {
columns = self['export'].columns;
} else {
for (var field in self['export'].template.schema) {
if (!self['export'].template.schema.hasOwnProperty(field)) continue;
// Skip keys that begin with '_'
if (field.toString().charAt(0) === '_') continue;
if (!('hidden' in self['export'].template.schema[field])) {
columns.push(field);
}
}
}
// get the labels for the columns
var labels = {};
for (var i = 0, l = columns.length; i < l; ++ i) {
if ('label' in self['export'].template.schema[columns[i]]) {
if (typeof self['export'].template.schema[columns[i]].label === 'object') {
labels[columns[i]] = self['export'].template.schema[columns[i]].label[M.getLocale()];
} else {
labels[columns[i]] = self['export'].template.schema[columns[i]].label;
}
} else {
labels[columns[i]] = columns[i];
}
}
var templateName = '';
if (typeof self['export'].template.options.label === 'object') {
templateName += self['export'].template.options.label[M.getLocale()];
} else {
templateName += self['export'].template.options.label;
}
if (!templateName) {
templateName = self['export'].template.name;
}
var date = new Date().toISOString().replace(/[^\d]/g, '');
var timestamp = date.substr(0, 8) + '_' + date.substr(8, 4);
var separators = {
'COMMA': ',',
'SEMICOLON': ';',
'TAB': '\t',
'SPACE': ' '
};
var options = {
data: {
template: self['export'].template._id,
query: self.query,
email: self['export'].email,
options: self.queryOptions,
hasHeaders: self['export'].headers || false,
columns: columns,
labels: labels,
separator: separators[self['export'].separator] || ';',
filename: self['export'].filename || templateName.toLowerCase().replace(' ', '_')
}
}
self.link('export', options, function(err) {
// give it a name
var templateName;
if (self['export'].template.options && self['export'].template.options.label) {
templateName = self['export'].template.options.label;
if (typeof templateName === 'object') {
templateName = templateName[M.getLocale()];
}
}
if (!templateName) {
templateName = self['export'].template.name;
}
var notificationMessage = {
de: 'Export ist gestartet und wird bald in der Inbox zur Verfügung stehen.',
fr: 'L\'exportation est lancé et sera bientôt disponible in Inbox.',
it: 'L\'esportazione è lanciato e sarà presto disponibile in Inbox.'
};
self.emit('notifications.show', {
type: err ? 'error' : 'info',
message: err ? err.error || err : notificationMessage[M.getLocale()]
});
});
// hiding the UI
self.emit('hideUi');
}
function getTemplates () {
var self = this;
var query = {};
var options = {
fields: {
_id: 1,
'options.label': 1
}
};
var crudObj = {
t: '000000000000000000000000',
q: query,
o: options,
noMerge: true
};
// get all template ids
self.emit('find', crudObj, function (err, data) {
// handle error
if (err) {
console.error(err);
return;
}
// an array with template ids
self.templates = {};
for (var i = 0; i < data.length; ++i) {
if (data[i]._id === '000000000000000000000004') {
continue;
}
self.templates[data[i]._id] = data[i];
}
// and emit some events
self.emit('_setTemplates');
self.emit('ready');
return;
});
}