-
Notifications
You must be signed in to change notification settings - Fork 4
/
tableGen.js
87 lines (86 loc) · 2.59 KB
/
tableGen.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
//Check if input is a number or not.
let app = new Vue({
el: "#app",
data() {
return {
columns: '',
rows: '',
tableString: '',
header: false,
tableSourceCode: '',
showTableSourceCode: false,
copyStatus: false,
copyStatusText: '',
copyStatusClass: ''
}
},
methods: {
preHTML() {
this.tableString = "";
this.tableString += "<table class='table is-bordered is-fullwidth'>";
if (this.header) {
this.tableString += "<thead>";
this.genCols(true)
this.tableString += "</thead>";
}
this.tableString += "<tbody>";
this.genRows();
this.tableString += "</tbody></table>";
},
genRows() {
for (let i = 0; i < this.rows; i++) {
this.tableString += "<tr>";
this.genCols();
this.tableString += "</tr>";
}
},
genCols(header) {
for (let i = 0; i < this.columns; i++) {
if(header) {
this.tableString += "<th>";
this.tableString += "Header Title";
this.tableString += "</th>";
} else {
this.tableString += "<td>";
this.tableString += "<input class='input' type='text' />";
this.tableString += "</td>";
}
}
},
changeValue() {
this.preHTML()
},
openModal() {
// Opens the modal and set flag
this.showTableSourceCode = true;
let string = '<code>' + this.tableString.replace(/&/g, '&').replace(/</g, '<') + '</code>';
this.tableSourceCode = string;
},
closeModal() {
// Closes the modal and reset flags as well as status content
this.showTableSourceCode = false;
this.copyStatus = false;
this.copyStatusClass = '';
this.copyStatusText = '';
},
copyCode() {
// Copies the content of 'generatedSourceCode' <div> to user's clipboard
let generatedSourceCodeStr = document.querySelector('#generatedSourceCode');
let range = document.createRange();
range.selectNode(generatedSourceCodeStr);
window.getSelection().addRange(range);
try {
// Execute copy command and set status content
this.copyStatus = document.execCommand('copy');
this.copyStatusClass = this.copyStatus ? 'has-background-success' : 'has-background-danger';
this.copyStatusText = 'Copied!'
} catch(err) {
// Copy didn't work, reset status content
this.copyStatus = false;
this.copyStatusClass = '';
this.copyStatusText = 'Something went wrong!'
}
window.getSelection().removeAllRanges();
}
}
})