-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
135 lines (129 loc) · 3.87 KB
/
data.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
let tbl = null;
let activeRow = 0;
const Settings = Backbone.Model.extend({
defaults: {
activeRow: 0
},
initialize(options) {
activeRow = options.activeRow;
this.on('change', () => {
GM_setValue('settings', JSON.stringify(this.toJSON()));
});
this.on('change:activeRow', function() {
activeRow = this.get('activeRow');
});
}
});
let settings = new Settings(JSON.parse(GM_getValue('settings', '{}')));
const Row = Backbone.Model.extend({
/*
defaults: {
active: false,
},
*/
fillUp() {
jQuery('.tabulator-table .tabulator-row.active-row').not(':last').removeClass('active-row');
// jQuery(tbl.getRow(this.get('email')).getElement()).addClass('active-row');
tbl.redraw(true);
setNativeValue(document.querySelector('input[name="email"][data-bdd="email-address-field"]'), this.get('email'));
setNativeValue(document.querySelector('input[name="password"][data-bdd="password-field"]'), this.get('password'));
setNativeValue(document.querySelector('input[name="firstName"][data-bdd="first-name-field"]'), this.get('fName'));
setNativeValue(document.querySelector('input[name="lastName"][data-bdd="last-name-field"]'), this.get('lName'));
}
});
const DataRows = Backbone.Collection.extend({
modelId: attrs => attrs.email,
model: Row,
getCurrent() {
if( this.models.length )
return this.models[activeRow - 1];
else
return false;
},
fillCurrentOne() {
if( activeRow < this.models.length ) {
++activeRow;
settings.set({activeRow});
this.getCurrent().fillUp();
} else
alert('Reached the end of CSV file');
return activeRow;
}
});
let data = new DataRows(GM_getValue('data', []));
console.log(settings.toJSON());
(function() {
'use strict';
// Toolbar
jQuery('body').append(GM_getResourceText('wrapperTpl'));
$(document).arrive('[name="phoneNumber"][data-bdd="bind-phone.phone.label"]', function() {
if( data.getCurrent() ) {
setNativeValue(this, data.getCurrent().get('phone'));
}
});
// Main
jQuery(document).ready(function(event) {
jQuery('#loadFile').click(function() {
tbl.import('csv', '*.csv').then(function() {
console.log('Import Complete!');
GM_setValue('data', tbl.getData());
jQuery('.tabulator-table .tabulator-row.active-row').removeClass('active-row');
jQuery('.tabulator-table .tabulator-row:first').addClass('active-row');
settings.set('activeRow', 0);
}).catch(e => {
console.error(e);
});
});
jQuery('#fillBtn').click(function(event) {
data.fillCurrentOne();
});
tbl = new Tabulator('.tblWrapper', {
index: 'email',
layout: 'fitColumns',
autoTables: true,
columns: [
{
title: 'Email', field: 'email',
contextMenu: function(event, cell) {
return [{
label: 'Fill Form',
action: () => {
data.models[cell.getRow().getPosition(true) - 1].fillUp();
}
}]
},
},
{
title: 'Password', field: 'password'
},
{
title: 'First Name', field: 'fName'
},
{
title: 'Last Name', field: 'lName'
},
{
title: 'Phone Number', field: 'phone'
},
],
rowFormatter(row) {
let data = row.getData();
if ( activeRow == row.getPosition(true) )
jQuery(row.getElement()).addClass('active-row');
}
});
jQuery('#collapseTbl').on('show.bs.collapse', function() {
console.log('Re-Render table');
// tbl.redraw(true);
jQuery('.tabulator-table .tabulator-row.active-row').not(':last').removeClass('active-row');
});
tbl.on('renderComplete', () => {
jQuery('.tabulator-table .tabulator-row.active-row').not(':last').removeClass('active-row');
});
tbl.on('tableBuilt', () => {
if( data.length ) {
tbl.setData(data.toJSON());
}
});
});
})();