-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
135 lines (118 loc) · 4.2 KB
/
main.js
File metadata and controls
135 lines (118 loc) · 4.2 KB
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
console.log("Injected");
var columns = [ //no espacios
"ID"
, "producto"
, "precio"
, "rfc"
, "procesado"
, "facturado"
]
var databaseName = "didec";
var tableName = "facturas";
$(document).ready(function(){
var mainDiv = $("."+databaseName+tableName);
db = new localStorageDB(databaseName, localStorage);
if (mainDiv.length){
$.each(columns, function(i, item){
var type = 'text'
if (['ID', 'procesado', 'facturado'].includes(item)) type = 'hidden';
if (!(type == 'hidden')){
mainDiv.append($('<p>').text(item));
}
mainDiv.append($('<input>').attr('id',item).attr('name', item).attr('type', type));
});
mainDiv.append('<br> <br>')
mainDiv.append($('<button>').attr('id', 'insert').text("Insertar"))
mainDiv.append($('<button>').text("Exportar").attr('id', 'export'));
mainDiv.append($('<button>').text("Importar").attr('id', 'import'));
var clipboard = new ClipboardJS('#export');
}
var table = $('<table>').attr("id", tableName).html($('<thead><tbody>')); //, <tbody>
$("body").append(table);
$('#'+tableName + ' thead').append($('<tr>').append(generateTableHeaders(columns)));
if (!db.tableExists(tableName))
{
db.createTable(tableName, columns);
} else {
var actualColumns = db.tableFields(tableName);
var actualColumns = columns.filter(function(value, index, arr){
return !columns.includes(value);
});
db.alterTable(tableName, actualColumns);
var rows = db.queryAll(tableName);
insertHTMLrow(rows);
}
//Facturador
window.getInvoice = function getInvoice(){
return db.queryAll(tableName, {
query: {procesado: "", facturado: ""}
, limit: 1
});
}
window.setInvoice = function setInvoice(id){
db.update(tableName, {ID: id}, function(row) {
row.procesado = "1";
row.facturado = (row.procesado == '1') ? "1" : '';
return row;
});
db.commit();
var rows = db.queryAll(tableName);
insertHTMLrow(rows, true);
}
//CRUD
$('#insert').click(function(e){
e.preventDefault();
var row = Object();
row[0] = mainDiv.serializeArray().reduce(function(a, x) { a[x.name] = String(x.value); return a; }, {});
var id = db.insert(tableName, row[0]);
db.commit();
row[0]["ID"] = id;
insertHTMLrow(row, true);
});
$('#export').click(function(e){
e.preventDefault();
$(this).attr("data-clipboard-text", JSON.stringify(db.queryAll(tableName)));
alert("Copiado jefa");
});
$('#import').click(function(e){
e.preventDefault();
var json = prompt("Pega el texto", "{}");
var rows = JSON.parse(json);
if ($.isEmptyObject({rows})){
insertHTMLrow(rows);
$.each(rows, function (i, item){
db.insertOrUpdate(tableName, null, item);
});
db.commit();
}
});
//Helpers functions
function insertHTMLrow(data, deleteActualRows){
if(deleteActualRows){
$("#"+tableName+ " tbody tr").remove();
}
$.each(data, function( index, value ) {
var rowContent = $("<tr>");
$.each(columns, function(i, item){
$("<td>").text(data[index][item]).appendTo(rowContent);
});
$("<a>").attr('href', '#').attr('onClick','deleteRow(this); return false;').text("Eliminar").appendTo(rowContent);
$("#" + tableName).append(rowContent);
});
mainDiv.trigger("reset");
}
window.deleteRow = function deleteRow(elem) {
var textContainer = elem.parentNode.firstChild;
var textValue = $(textContainer).text();
db.deleteRows(tableName, {ID: String(textValue)});
db.commit();
elem.parentNode.remove();
}
function generateTableHeaders(myColumns){
var headers = '';
$.each(myColumns, function(i, item){
headers += $("<th>").text(item).prop('outerHTML');
});
return headers;
}
});