-
Notifications
You must be signed in to change notification settings - Fork 101
/
beautifier.js
85 lines (71 loc) · 3.07 KB
/
beautifier.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
//Beautifier.js
//
//Copywrite 2011 Josh Benentt
//License - https://raw.github.com/joshatjben/excelFormulaUtilitiesJS/master/LICENSE.txt
//[on github](https://github.com/joshatjben/excelFormulaUtilitiesJS/tree/master/examples/basic_example1 "github")
//
(function (window, undefiend) {
"use strict";
//Check and setup name spaces.
window.excelFormulaBeautifier = window.excelFormulaBeautifier || {};
window.excelFormulaBeautifier.examples = window.excelFormulaBeautifier.examples || {};
//Configuration
//-------------------------------
var config = {
//The ID for the formula Input input/textarea
INPUT_ID: 'formula_input',
//The ID for the formula title area. in this example it spits out the function call;
FORMULA_TITLE_ID: 'fomatFormula_2',
//THE ID for the area to contain the beautified excel formula.
FORMULA_BODY_ID:'fomatFormula_2_out',
//Use this to set the inital textare/input text area.
DEFAULT_FORMULA: ''
},
//Beautifier Page functionality
//-------------------------------
beautifier = window.excelFormulaBeautifier.examples.beautifier =
(function () {
var oldFormula;
return {
formula: '=IF(SUM( If(FOO = BAR, 10, 0), 10 ) = 20 , "FOO", "BAR")',
input: null,
formulaTitle: null,
formulaBody: null,
mode: "beautify",
changeMode: function(mode){
window.excelFormulaBeautifier.examples.beautifier.mode = mode;
window.excelFormulaBeautifier.examples.beautifier.update.call(window.excelFormulaBeautifier.examples.beautifier);
},
update: function () {
this.formula = this.input.value;
//Test to see if the formula has changed, if it hasn't don't do anything
if (oldFormula === this.formula) {
return;
}
// Check to see which mode we're in, render appropriately
try{
switch( this.mode ) {
case "beautify":
this.formulaBody.innerHTML = window.excelFormulaUtilities.formatFormulaHTML(this.formula);
break;
case "js":
this.formulaBody.innerHTML = window.excelFormulaUtilities.formula2JavaScript(this.formula);
break;
}
}catch(exception){
//Do nothing, This should throw an error when the formula is improperly formed, which shouldn't blow things up.
}
}
};
}());
//On Page Load
//-------------------
window.onload = function () {
beautifier.input = document.getElementById(config.INPUT_ID);
//beautifier.formulaTitle = document.getElementById(config.FORMULA_TITLE_ID);
beautifier.formulaBody = document.getElementById(config.FORMULA_BODY_ID);
beautifier.input.value = beautifier.formula;
beautifier.update();
//add beautifier.update(); here if if you have set an inital DEFAULT_FORMULA and would like it to render on page load.
};
}(window));