rev |
line source |
pascal@200
|
1 plugin="Calc"
|
pascal@200
|
2 description_fr="Tableur format CSV"
|
pascal@200
|
3 description="CSV format spreadsheet"
|
pascal@200
|
4 help_fr="AideCalc"
|
pascal@200
|
5 help="HelpCalc"
|
pascal@200
|
6
|
pascal@200
|
7 case "$1" in
|
pascal@200
|
8 showhead) cat <<EOT
|
pascal@200
|
9 <!-- Based on http://jsfiddle.net/ondras/hYfN3/ by Ondřej Žára -->
|
pascal@200
|
10 <script type="text/javascript">
|
pascal@200
|
11 function csv(id,rows,cols) {
|
pascal@200
|
12 var data = "";
|
pascal@200
|
13 for (var i=1; i<=rows; i++) {
|
pascal@200
|
14 for (var j=1; j<=cols; j++) {
|
pascal@200
|
15 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
|
pascal@200
|
16 data += document.getElementById(id+letter+i).title+';';
|
pascal@200
|
17 }
|
pascal@200
|
18 data += "\\\\n";
|
pascal@200
|
19 }
|
pascal@200
|
20 alert(data);
|
pascal@200
|
21 }
|
pascal@200
|
22
|
pascal@200
|
23 var DATA={};
|
pascal@200
|
24 function buildCalc(id, rows, cols) {
|
pascal@200
|
25 DATA[id] = {};
|
pascal@200
|
26 for (var i=0; i<=rows; i++) {
|
pascal@200
|
27 var row = document.getElementById(id).insertRow(-1);
|
pascal@200
|
28 for (var j=0; j<=cols && j<=26; j++) {
|
pascal@200
|
29 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
|
pascal@200
|
30 var cell = row.insertCell(-1);
|
pascal@200
|
31 if (i&&j) {
|
pascal@200
|
32 cell.className = "cellcalc";
|
pascal@200
|
33 cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>";
|
pascal@200
|
34 }
|
pascal@200
|
35 else {
|
pascal@200
|
36 cell.className = "bordercalc";
|
pascal@200
|
37 cell.title = "Show CSV";
|
pascal@200
|
38 cell.onclick = function(){csv(id,rows,cols);};
|
pascal@200
|
39 cell.innerHTML = (i||j) ? i||letter : "√";
|
pascal@200
|
40 }
|
pascal@200
|
41 }
|
pascal@200
|
42 }
|
pascal@200
|
43 }
|
pascal@200
|
44
|
pascal@200
|
45 function getWidth(s)
|
pascal@200
|
46 {
|
pascal@200
|
47 var e = document.getElementById("widthcalc");
|
pascal@200
|
48 e.innerHTML = s+" :";
|
pascal@200
|
49 return (e.offsetWidth < 80 || s.charAt(0) == "=") ? 80 : e.offsetWidth;
|
pascal@200
|
50 }
|
pascal@200
|
51
|
pascal@200
|
52 function setCell(e, v)
|
pascal@200
|
53 {
|
pascal@200
|
54 e.style.width = getWidth(v)+"px";
|
pascal@200
|
55 e.style.textAlign =
|
pascal@200
|
56 (isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right";
|
pascal@200
|
57 e.title = v;
|
pascal@200
|
58 }
|
pascal@200
|
59 </script>
|
pascal@200
|
60 <span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span>
|
pascal@200
|
61 EOT
|
pascal@200
|
62 exit 0 ;;
|
pascal@200
|
63 showtail) cat <<EOT
|
pascal@200
|
64 <script type="text/javascript">
|
pascal@200
|
65 var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc"));
|
pascal@200
|
66 INPUTS.forEach(function(elm) {
|
pascal@200
|
67 elm.onfocus = function(e) {
|
pascal@200
|
68 e.target.value = e.target.title || "";
|
pascal@200
|
69 };
|
pascal@200
|
70 elm.onblur = function(e) {
|
pascal@200
|
71 setCell(e.target, e.target.value);
|
pascal@200
|
72 computeAll();
|
pascal@200
|
73 };
|
pascal@200
|
74 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
|
pascal@200
|
75 var getter = function() {
|
pascal@200
|
76 var value = elm.title || "";
|
pascal@200
|
77 if (value.charAt(0) == "=")
|
pascal@200
|
78 with (DATA[calcid]) return eval(value.substring(1));
|
pascal@200
|
79 else return isNaN(parseFloat(value)) ? value : parseFloat(value);
|
pascal@200
|
80 };
|
pascal@200
|
81 Object.defineProperty(DATA[calcid], cellid, {get:getter});
|
pascal@200
|
82 Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter});
|
pascal@200
|
83 });
|
pascal@200
|
84 (window.computeAll = function() {
|
pascal@200
|
85 INPUTS.forEach(function(elm) {
|
pascal@200
|
86 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
|
pascal@200
|
87 try { elm.value = DATA[calcid][cellid]; } catch(e) {} });
|
pascal@200
|
88 })();
|
pascal@200
|
89 </script>
|
pascal@200
|
90 EOT
|
pascal@200
|
91 exit 0 ;;
|
pascal@200
|
92 esac
|
pascal@200
|
93
|
pascal@200
|
94 formatEnd()
|
pascal@200
|
95 {
|
pascal@200
|
96 CONTENT=$(awk -v prg=$plugins_dir/wkp_$plugin.sh '
|
pascal@200
|
97 function showcalc()
|
pascal@200
|
98 {
|
pascal@200
|
99 if (lines > 1 && rows > 1) {
|
pascal@200
|
100 id="C" (100+cnt++)
|
pascal@200
|
101 print "<noscript><u>Enable javascript to see the spreadsheet " id "</u></noscript>"
|
pascal@200
|
102 print "<table id=\"" id "\" class=\"tablecalc\"></table>"
|
pascal@200
|
103 print "<script type=\"text/javascript\">"
|
pascal@200
|
104 print "buildCalc(\"" id "\"," lines "," rows ");"
|
pascal@200
|
105 for (i = 1; i <= lines; i++) {
|
pascal@200
|
106 gsub("<","<",line[i])
|
pascal@200
|
107 for (j = 1; j < split(line[i],tmp,";"); j++) {
|
pascal@200
|
108 if (tmp[j] == "") continue
|
pascal@200
|
109 gsub("\"","\\\\\"",tmp[j])
|
pascal@200
|
110 s = "setCell(document.getElementById(\"" id
|
pascal@200
|
111 c = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",j,1)
|
pascal@200
|
112 print s c i "\"), \"" tmp[j] "\")";
|
pascal@200
|
113 }
|
pascal@200
|
114 }
|
pascal@200
|
115 print "</script>"
|
pascal@200
|
116 }
|
pascal@200
|
117 else for (i = 1; i <= lines; i++) print line[i]
|
pascal@200
|
118 rows = lines = gotcalc = 0
|
pascal@200
|
119 }
|
pascal@200
|
120 {
|
pascal@200
|
121 if (/;<br \/>$/) {
|
pascal@200
|
122 gotcalc = 1
|
pascal@200
|
123 if (!headdone) {
|
pascal@200
|
124 headdone = 1
|
pascal@200
|
125 showtail = 1
|
pascal@200
|
126 system("/bin/sh " prg " showhead")
|
pascal@200
|
127 #print "system(" prg " showhead)"
|
pascal@200
|
128 }
|
pascal@200
|
129 line[++lines] = $0
|
pascal@200
|
130 gsub("<","<",$0)
|
pascal@200
|
131 i = split($0,tmp,";")-1
|
pascal@200
|
132 if (lines == 1) rows = i
|
pascal@200
|
133 if (i != rows) rows = -1
|
pascal@200
|
134 }
|
pascal@200
|
135 else {
|
pascal@200
|
136 if (gotcalc) showcalc()
|
pascal@200
|
137 print
|
pascal@200
|
138 }
|
pascal@200
|
139 }
|
pascal@200
|
140 END {
|
pascal@200
|
141 if (gotcalc) showcalc()
|
pascal@200
|
142 if (showtail) system("/bin/sh " prg " showtail")
|
pascal@200
|
143 }
|
pascal@200
|
144 ' <<EOT
|
pascal@200
|
145 $CONTENT
|
pascal@200
|
146 EOT
|
pascal@200
|
147 )
|
pascal@200
|
148 }
|
pascal@200
|
149
|
pascal@200
|
150 template()
|
pascal@200
|
151 {
|
pascal@200
|
152 html=$(sed 's|</head>|\t<style type="text/css"> @import "plugins/wkp_Calc.css"; </style>\n&|' <<EOT
|
pascal@200
|
153 $html
|
pascal@200
|
154 EOT
|
pascal@200
|
155 )
|
pascal@200
|
156 }
|