slitaz-dev-tools view tazwikiss/rootfs/var/www/wiki/plugins/wkp_Calc.sh @ rev 238

tazwikiss: add min, max, cnt
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Feb 22 11:10:22 2015 +0100 (2015-02-22)
parents 674bad1f758b
children f38bbdf61c95
line source
1 plugin="Calc"
2 description_fr="Feuille de calcul au format CSV"
3 description="CSV format spreadsheet"
4 help_fr="AideCalc"
5 help="HelpCalc"
7 case "$1" in
8 showhead) cat <<EOT
9 <!-- Based on http://jsfiddle.net/ondras/hYfN3/ by Ondřej Žára -->
10 <script type="text/javascript">
11 <!--
12 function csv(id,rows,cols) {
13 var data = "";
14 for (var i=1; i<=rows; i++) {
15 for (var j=1; j<=cols; j++) {
16 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
17 data += document.getElementById(id+letter+i).title+';';
18 }
19 data += "\\\\n";
20 }
21 alert(data);
22 }
24 function cnt(from,to) {
25 return (to.charCodeAt(0) - from.charCodeAt(0) + 1) *
26 (parseInt(to.substring(1)) - parseInt(from.substring(1)) + 1)
27 }
29 function zone(id,from,to,init,func) {
30 var result=init
31 for (var l=from.charCodeAt(0);;l++) {
32 for (var n=parseInt(from.substring(1));
33 n <= parseInt(to.substring(1));n++) {
34 var e=document.getElementById(id+String.fromCharCode(l)+n)
35 result=func(result,parseFloat(e.value))
36 }
37 if (l == to.charCodeAt(0)) break
38 }
39 return result;
40 }
42 var DATA={};
43 function buildCalc(id, rows, cols) {
44 DATA[id] = {};
45 var maths = [ "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp",
46 "floor", "log", "max", "min", "pow", "random", "round", "sin",
47 "tan", "sqrt", "PI", "E" ];
48 for (var i=0; v = maths[i]; i++)
49 eval("DATA[id]."+v+" = DATA[id]."+v.toUpperCase()+" = Math."+v);
50 DATA[id].rand = DATA[id].RAND = Math.random;
51 DATA[id].ln = DATA[id].LN = Math.log;
52 DATA[id].log10= DATA[id].LOG10= function(n){return Math.log(n)/Math.LN10;};
53 DATA[id].log2 = DATA[id].LOG2 = function(n){return Math.log(n)/Math.LN2;};
54 DATA[id].fact = DATA[id].FACT =
55 function(n){var x=1;while(n>1)x*=n--;return x;};
56 DATA[id].fib = DATA[id].FIB =
57 function(n){var c=0,p=1;while(n-->0){var x=c;c+=p;p=x};return c;};
58 DATA[id].sum = DATA[id].SUM =
59 function(a,b){return zone(id,a,b,0,function(a,b){return a+b});};
60 DATA[id].min = DATA[id].MIN =
61 function(a,b){return zone(id,a,b,Number.MAX_VALUE,Math.min);};
62 DATA[id].max = DATA[id].MAX =
63 function(a,b){return zone(id,a,b,Number.MIN_VALUE,Math.max);};
64 DATA[id].cnt = DATA[id].CNT = cnt
65 for (var i=0; i<=rows; i++) {
66 var row = document.getElementById(id).insertRow(-1);
67 for (var j=0; j<=cols && j<=26; j++) {
68 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
69 var cell = row.insertCell(-1);
70 if (i&&j) {
71 cell.className = "cellcalc";
72 cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>";
73 }
74 else {
75 cell.className = "bordercalc";
76 cell.title = "Show CSV";
77 cell.onclick = function(){csv(id,rows,cols);};
78 cell.innerHTML = (i||j) ? i||letter : "&radic;";
79 }
80 }
81 }
82 }
84 function getWidth(s)
85 {
86 var e = document.getElementById("widthcalc");
87 e.innerHTML = s;
88 return (e.offsetWidth < 80 || s.charAt(0) == "=") ? 80 : e.offsetWidth;
89 }
91 function setCell(e, v)
92 {
93 e.style.width = getWidth(v)+"px";
94 e.style.textAlign =
95 (isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right";
96 e.title = v;
97 }
98 //-->
99 </script>
100 <span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span>
101 EOT
102 exit 0 ;;
103 showtail) cat <<EOT
104 <script type="text/javascript">
105 <!--
106 var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc"));
107 INPUTS.forEach(function(elm) {
108 elm.onfocus = function(e) {
109 e.target.value = e.target.title || "";
110 };
111 elm.onblur = function(e) {
112 setCell(e.target, e.target.value);
113 computeAll();
114 };
115 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
116 var getter = function() {
117 var value = elm.title || "";
118 if (value.charAt(0) == "=")
119 with (DATA[calcid]) return eval(value.substring(1));
120 else return (value == "" || isNaN(value)) ? value : parseFloat(value);
121 };
122 Object.defineProperty(DATA[calcid], cellid, {get:getter});
123 Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter});
124 });
125 (window.computeAll = function() {
126 INPUTS.forEach(function(elm) {
127 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
128 try { elm.value = DATA[calcid][cellid]; } catch(e) {} });
129 })();
130 //-->
131 </script>
132 EOT
133 exit 0 ;;
134 esac
136 formatEnd()
137 {
138 CONTENT=$(awk -v prg=$plugins_dir/wkp_$plugin.sh '
139 function showcalc()
140 {
141 if (lines > 1 && rows > 1) {
142 id="C" (100+cnt++)
143 print "<noscript><a href=\"http://www.enable-javascript.com/\" target=\"_blank\">Enable javascript to see the spreadsheet " id "</a></noscript>"
144 print "<table id=\"" id "\" class=\"tablecalc\"></table>"
145 print "<script type=\"text/javascript\">"
146 print "<!--"
147 print "buildCalc(\"" id "\"," lines "," rows ");"
148 for (i = 1; i <= lines; i++) {
149 gsub("&lt;","<",line[i])
150 for (j = 1; j < split(line[i],tmp,";"); j++) {
151 if (tmp[j] == "") continue
152 gsub("\"","\\\\\"",tmp[j])
153 s = "setCell(document.getElementById(\"" id
154 c = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",j,1)
155 print s c i "\"), \"" tmp[j] "\")";
156 }
157 }
158 print "//-->"
159 print "</script>"
160 }
161 else for (i = 1; i <= lines; i++) print line[i]
162 rows = lines = gotcalc = 0
163 }
164 {
165 if (/;<br \/>$/) {
166 gotcalc = 1
167 if (!headdone) {
168 headdone = 1
169 showtail = 1
170 system("/bin/sh " prg " showhead")
171 }
172 line[++lines] = $0
173 gsub("&lt;","<",$0)
174 i = split($0,tmp,";")-1
175 if (lines == 1) rows = i
176 if (i != rows) rows = -1
177 }
178 else {
179 if (gotcalc) showcalc()
180 print
181 }
182 }
183 END {
184 if (gotcalc) showcalc()
185 if (showtail) system("/bin/sh " prg " showtail")
186 }
187 ' <<EOT
188 $CONTENT
189 EOT
190 )
191 }
193 template()
194 {
195 html=$(sed 's|</head>|\t<style type="text/css"> @import "plugins/wkp_Calc.css"; </style>\n&|' <<EOT
196 $html
197 EOT
198 )
199 }