-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
89 lines (65 loc) · 1.81 KB
/
script.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
function convertToArrayOfStrings(string) {
return JSON.stringify(string.split('\n'));
}
function stripQuotes(string) {
return string.replace(/\'|\"/g, '');
}
var modes = {
number: function(string) {
string = string.replace(/\./g, '');
string = string.replace(/\,/g, '.');
string = convertToArrayOfStrings(string);
return stripQuotes(string);
},
text: convertToArrayOfStrings
};
var ExcelTransformForm = {
create: function() {
var instance = $.extend({}, this.prototype);
this.init.apply(instance, arguments);
return instance;
},
init: function() {
this.$input = $('#input');
this.$output = $('#output');
this.$relaunch = $('#relaunch');
this.$relaunch.on('click', this.transformInput.bind(this));
this.$empty = $('#empty-input');
this.$empty.on('click', this.emptyInput.bind(this));
this.$modeControl = $('#mode');
this.$modeControl.on('click', 'button', function(e) {
var clicked = e.currentTarget;
var mode = clicked.dataset.mode;
this.clearMode();
this.setMode(mode);
this.transformInput();
}.bind(this));
this.$input.on('input', this.transformInput.bind(this));
this.setMode('text');
},
prototype: {
emptyInput: function() {
this.$input.val('');
},
setMode: function(mode) {
this.mode = modes[mode];
this.$modeControl.find('[data-mode="'+ mode + '"]').addClass('active');
},
clearMode: function() {
this.mode = $.noop();
this.$modeControl.find('button').removeClass('active');
},
transformInput: function(string) {
try {
var value = this.$input.val();
this.$output.val(this.mode(value));
} catch(err) {
console.error(err);
}
}
}
};
$(function() {
'use strict';
ExcelTransformForm.create();
});