wok-current view wikiss/stuff/plugins/wkp_Calc.php @ rev 19275

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