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

tazwikiss: fix date display
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Feb 21 11:04:03 2015 +0100 (2015-02-21)
parents e3b40fe05c1d
children 673b33f90a09
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 var DATA={};
25 function buildCalc(id, rows, cols) {
26 DATA[id] = {};
27 var maths = [ "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp",
28 "floor", "log", "max", "min", "pow", "random", "round", "sin",
29 "tan", "sqrt", "PI", "E" ];
30 for (var i=0; v = maths[i]; i++)
31 eval("DATA[id]."+v+" = DATA[id]."+v.toUpperCase()+" = Math."+v);
32 DATA[id].rand = DATA[id].RAND = Math.random;
33 DATA[id].ln = DATA[id].LN = Math.log;
34 DATA[id].log10= DATA[id].LOG10= function(n){return Math.log(n)/Math.LN10;};
35 DATA[id].log2 = DATA[id].LOG2 = function(n){return Math.log(n)/Math.LN2;};
36 DATA[id].fact = DATA[id].FACT =
37 function(n){var x=1;while(n>1)x*=n--;return x;};
38 DATA[id].fib = DATA[id].FIB =
39 function(n){var c=0,p=1;while(n-->0){var x=c;c+=p;p=x};return c;};
40 for (var i=0; i<=rows; i++) {
41 var row = document.getElementById(id).insertRow(-1);
42 for (var j=0; j<=cols && j<=26; j++) {
43 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
44 var cell = row.insertCell(-1);
45 if (i&&j) {
46 cell.className = "cellcalc";
47 cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>";
48 }
49 else {
50 cell.className = "bordercalc";
51 cell.title = "Show CSV";
52 cell.onclick = function(){csv(id,rows,cols);};
53 cell.innerHTML = (i||j) ? i||letter : "&radic;";
54 }
55 }
56 }
57 }
59 function getWidth(s)
60 {
61 var e = document.getElementById("widthcalc");
62 e.innerHTML = s+" :";
63 return (e.offsetWidth < 80 || s.charAt(0) == "=") ? 80 : e.offsetWidth;
64 }
66 function setCell(e, v)
67 {
68 e.style.width = getWidth(v)+"px";
69 e.style.textAlign =
70 (isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right";
71 e.title = v;
72 }
73 //-->
74 </script>
75 <span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span>
76 EOT
77 exit 0 ;;
78 showtail) cat <<EOT
79 <script type="text/javascript">
80 <!--
81 var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc"));
82 INPUTS.forEach(function(elm) {
83 elm.onfocus = function(e) {
84 e.target.value = e.target.title || "";
85 };
86 elm.onblur = function(e) {
87 setCell(e.target, e.target.value);
88 computeAll();
89 };
90 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
91 var getter = function() {
92 var value = elm.title || "";
93 if (value.charAt(0) == "=")
94 with (DATA[calcid]) return eval(value.substring(1));
95 else return isNaN(value) ? value : parseFloat(value);
96 };
97 Object.defineProperty(DATA[calcid], cellid, {get:getter});
98 Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter});
99 });
100 (window.computeAll = function() {
101 INPUTS.forEach(function(elm) {
102 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
103 try { elm.value = DATA[calcid][cellid]; } catch(e) {} });
104 })();
105 //-->
106 </script>
107 EOT
108 exit 0 ;;
109 esac
111 formatEnd()
112 {
113 CONTENT=$(awk -v prg=$plugins_dir/wkp_$plugin.sh '
114 function showcalc()
115 {
116 if (lines > 1 && rows > 1) {
117 id="C" (100+cnt++)
118 print "<noscript><a href=\"http://www.enable-javascript.com/\" target=\"_blank\">Enable javascript to see the spreadsheet " id "</a></noscript>"
119 print "<table id=\"" id "\" class=\"tablecalc\"></table>"
120 print "<script type=\"text/javascript\">"
121 print "<!--"
122 print "buildCalc(\"" id "\"," lines "," rows ");"
123 for (i = 1; i <= lines; i++) {
124 gsub("&lt;","<",line[i])
125 for (j = 1; j < split(line[i],tmp,";"); j++) {
126 if (tmp[j] == "") continue
127 gsub("\"","\\\\\"",tmp[j])
128 s = "setCell(document.getElementById(\"" id
129 c = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",j,1)
130 print s c i "\"), \"" tmp[j] "\")";
131 }
132 }
133 print "//-->"
134 print "</script>"
135 }
136 else for (i = 1; i <= lines; i++) print line[i]
137 rows = lines = gotcalc = 0
138 }
139 {
140 if (/;<br \/>$/) {
141 gotcalc = 1
142 if (!headdone) {
143 headdone = 1
144 showtail = 1
145 system("/bin/sh " prg " showhead")
146 }
147 line[++lines] = $0
148 gsub("&lt;","<",$0)
149 i = split($0,tmp,";")-1
150 if (lines == 1) rows = i
151 if (i != rows) rows = -1
152 }
153 else {
154 if (gotcalc) showcalc()
155 print
156 }
157 }
158 END {
159 if (gotcalc) showcalc()
160 if (showtail) system("/bin/sh " prg " showtail")
161 }
162 ' <<EOT
163 $CONTENT
164 EOT
165 )
166 }
168 template()
169 {
170 html=$(sed 's|</head>|\t<style type="text/css"> @import "plugins/wkp_Calc.css"; </style>\n&|' <<EOT
171 $html
172 EOT
173 )
174 }