blib/lib/HTML/EditableTable/Javascript.pm

filter: Statementstmt 17.5% 16 / 91 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 23.0% 3 / 13 Podpod 0.0% 0 / 10 totaltotal 14.5% 19 / 131 CC 13 SCAR 43.3
CC
13
Cov
15%
htmlDisplay
156.0
new
6.0
setUid
2.0
CRAP 76.1
« blib/lib/HTML/EditableTable/Vertical.pm ↑ Summary example/example_1.cgi »
1 package HTML::EditableTable::Javascript;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 HTML::EditableTable::Javascript
11  
12 =head1 VERSION
13  
14 Version 0.21
15  
16 =cut
17  
18 1 our $Version = '0.21';
19  
20 1 my $javascriptDisplayCount = 0;
21  
22 1 my %fieldParamJavascriptMap = (
23 'tooltip' => \&jsToolTip,
24 'drillDownTruncate' => \&jsExpandText,
25 'jsClearColumnOnEdit' => \&jsClearColumnOnEdit,
26 );
27  
28 1 my %tableParamJavascriptMap = (
29 'jsSortHeader' => \&jsSortHeader,
30 );
31  
32 1 my %formElementTypeJavascriptMap = (
33 'calendar' => \&jsCalendar10Setup,
34 );
35  
36 =head1 SYNOPSIS
37  
38 This class provides all of the javascript functionality supported in EditableTable. It is designed so that the javascript code used can be easily overridden with local .js files See the documetation in L<HTML::EditableTable> for guidance on extending this class.
39  
40 =cut
41  
42 1 $|=1;
43  
44 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
45
46 0 my $class = shift;
47 0 my $parent = shift; # reference to parent EditableTable
48
49 0 ▶ if (!$parent->isa('HTML::EditableTable')) { confess "parent is not an HTML::EditableTable"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->isa("HTML::EditableTable")) 0 0 0
50
51 0 my $self= {};
52 0 $self->{parent} = $parent;
53 0 $self->{methods} = [];
54
55 0 bless $self, $class;
56
57 0 return $self;
58 }
59  
60 0 ▶sub setUid {
Subroutine setUid: not called CC=1 SCAR=6.9
Pod undocumented
61 0 my $uid = shift;
62 }
63
64 0 ▶sub htmlDisplay {
Subroutine htmlDisplay: not called CC=12 SCAR=50.5
Pod undocumented
65  
66 0 my $self = shift;
67  
68 # for multiple table case, only print these once
69  
70 0 print "debug displayCount = $javascriptDisplayCount<br>";
71  
72 0 ▶ if (!$javascriptDisplayCount) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($javascriptDisplayCount) 0 0 0
73  
74 # table field-level javascript support
75  
76 0 my @potentialJSSupportParams = keys %fieldParamJavascriptMap;
77 0 my @potentialJSSupportFormElementTypes = keys %formElementTypeJavascriptMap;
78 0 my @potentialJSSupportTableParams = keys %tableParamJavascriptMap;
79  
80 # table-param-level javascript
81  
82 0 foreach my $param (keys %{$self->{parent}}) {
83 0 foreach my $potentialJSSupportTableParam (@potentialJSSupportTableParams) {
84
85 0 ▶ if (my $mp = $tableParamJavascriptMap{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mp = $tableParamJavascriptMap{$param}) 0 0 0
86 0 print $self->$mp();
87 }
88 }
89 }
90
91 0 foreach my $tableField (@{$self->{parent}->{tableFields}}) {
92
93 # form-element-based javascript
94 0 my @remainingJSSupportFormElementTypes = ();
95  
96 0 foreach my $potentialFieldFormElementType (@potentialJSSupportFormElementTypes) {
97
98 0 ▶ if ($tableField->{formElement} && $formElementTypeJavascriptMap{$tableField->{formElement}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tableField->{'formElement'} and $formElementTypeJavascriptMap{$tableField->{'formElement'}}) { } 0 0 0
Condition0%0/3
$tableField->{"formElement"} and $formElementTypeJavascriptMap{$tableField->{"formElement"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$tableField->{"formElement"} and $formElementTypeJavascriptMap{$tableField->{"formElement"}}
$tableField->{"formElement"} $formElementTypeJavascriptMap{$tableField->{"formElement"}}
Truth table0%0/3
A and B
A= $tableField->{"formElement"}B= $formElementTypeJavascriptMap{$tableField->{"formElement"}}
ABresult
0X0
100
111
99
100 0 my $mp = $formElementTypeJavascriptMap{$tableField->{formElement}};
101 0 print $self->$mp();
102 }
103 else {
104 0 push @remainingJSSupportFormElementTypes, $potentialFieldFormElementType;
105 }
106 }
107 0 @potentialJSSupportFormElementTypes = @remainingJSSupportFormElementTypes;
108  
109  
110 # param-based javascript
111
112 0 my @remainingJSSupportParams = ();
113  
114 0 foreach my $potentialFieldParam (@potentialJSSupportParams) {
115
116 0 ▶ if ($tableField->{$potentialFieldParam}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tableField->{$potentialFieldParam}) { } 0 0 0
117 0 my $mp = $fieldParamJavascriptMap{$potentialFieldParam};
118 0 print $self->$mp();
119 }
120 else {
121 0 push @remainingJSSupportParams, $potentialFieldParam;
122 }
123 }
124 0 @potentialJSSupportParams = @remainingJSSupportParams;
125 }
126 }
127  
128 0 $javascriptDisplayCount = 1;
129  
130 # this method is unique to each table
131  
132 0 ▶ if ($self->{parent}->isParamSet('jsAddData')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parent"}->isParamSet("jsAddData")) 0 0 0
133 0 print $self->jsAddData();
134 }
135 }
136  
137 0 ▶sub resetJavascriptDisplayCount {
Subroutine resetJavascriptDisplayCount: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my $self = shift;
139
140 0 print "reseting display count<br>";
141 0 $javascriptDisplayCount = 0;
142 }
143  
144 0 ▶sub jsExpandText {
Subroutine jsExpandText: not called CC=1 SCAR=6.9
Pod undocumented
145  
146 0 my $self = shift;
147  
148 0 my $javascript = "<script language='javascript' type='text/javascript'>\n";
149 0 $javascript .= <<'END_JS';
150  
151 function expandText(target_elm_id, full_text_div_id, short_text_div_id) {
152
153 var target_elm = document.getElementById(target_elm_id);
154 var full_text_div = document.getElementById(full_text_div_id);
155 var short_text_div = document.getElementById(short_text_div_id);
156
157 if (target_elm.innerHTML == full_text_div.innerHTML) {
158 target_elm.innerHTML = short_text_div.innerHTML;
159 }
160 else {
161 target_elm.innerHTML = full_text_div.innerHTML;
162 }
163 }
164 END_JS
165  
166 0 $javascript .= "</script>";
167 0 return $javascript;
168 }
169  
170 0 ▶sub jsClearColumnOnEdit {
Subroutine jsClearColumnOnEdit: not called CC=1 SCAR=6.9
Pod undocumented
171  
172 0 my $self = shift;
173
174 0 my $javascript = "<script language='javascript' type='text/javascript'>\n";
175 0 $javascript .= <<'END_JS';
176  
177 function clearColumnOnEdit(prefix) {
178
179 // iterate through fields and clear those with the regex ${prefix}_\d+
180 // not real clean but should be reasonably safe
181
182 var field_name_regex = new RegExp(prefix + "_" + "\\d+");
183 var fields = document.getElementsByTagName("textarea");
184
185 for (i=0; i<fields.length; i++) {
186
187 if (fields[i].name.search(field_name_regex) != -1) {
188 fields[i].value = '';
189 }
190 }
191 }
192 END_JS
193  
194 0 $javascript .= "</script>";
195  
196 0 return $javascript;
197 }
198  
199 0 ▶sub jsSortHeader {
Subroutine jsSortHeader: not called CC=1 SCAR=6.9
Pod undocumented
200
201 0 my $javascript = "<script language='javascript' type='text/javascript'>\n";
202 0 $javascript .= << 'END_JS';
203  
204  
205 // SortTable
206 // version 2
207 // 7th April 2007
208 // Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
209 //
210 // Instructions:
211 // Download this file
212  
213 // Add class="sortable" to any table you'd like to make sortable
214 // Click on the headers to sort
215 //
216 // Thanks to many, many people for contributions and suggestions.
217 // Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
218 // This basically means: do what you want with it.
219 //
220  
221
222 var stIsIE = /*@cc_on!@*/false;
223  
224 sorttable = {
225 init: function() {
226 // quit if this function has already been called
227 if (arguments.callee.done) return;
228 // flag this function so we don't do the same thing twice
229 arguments.callee.done = true;
230 // kill the timer
231 if (_timer) clearInterval(_timer);
232
233 if (!document.createElement || !document.getElementsByTagName) return;
234
235 sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
236
237 forEach(document.getElementsByTagName('table'), function(table) {
238 if (table.className.search(/\bsortable\b/) != -1) {
239 sorttable.makeSortable(table);
240 }
241 });
242
243 },
244
245 makeSortable: function(table) {
246 if (table.getElementsByTagName('thead').length == 0) {
247 // table doesn't have a tHead. Since it should have, create one and
248 // put the first table row in it.
249 the = document.createElement('thead');
250 the.appendChild(table.rows[0]);
251 table.insertBefore(the,table.firstChild);
252 }
253 // Safari doesn't support table.tHead, sigh
254 if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
255
256 if (table.tHead.rows.length != 1) return; // can't cope with two header rows
257
258 // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
259 // "total" rows, for example). This is B&R, since what you're supposed
260 // to do is put them in a tfoot. So, if there are sortbottom rows,
261 // for backwards compatibility, move them to tfoot (creating it if needed).
262 sortbottomrows = [];
263 for (var i=0; i<table.rows.length; i++) {
264 if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
265 sortbottomrows[sortbottomrows.length] = table.rows[i];
266 }
267 }
268 if (sortbottomrows) {
269 if (table.tFoot == null) {
270 // table doesn't have a tfoot. Create one.
271 tfo = document.createElement('tfoot');
272 table.appendChild(tfo);
273 }
274 for (var i=0; i<sortbottomrows.length; i++) {
275 tfo.appendChild(sortbottomrows[i]);
276 }
277 delete sortbottomrows;
278 }
279
280 // work through each column and calculate its type
281 headrow = table.tHead.rows[0].cells;
282 for (var i=0; i<headrow.length; i++) {
283 // manually override the type with a sorttable_type attribute
284 if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
285 mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
286 if (mtch) { override = mtch[1]; }
287 if (mtch && typeof sorttable["sort_"+override] == 'function') {
288 headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
289 } else {
290 headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
291 }
292 // make it clickable to sort
293 headrow[i].sorttable_columnindex = i;
294 headrow[i].sorttable_tbody = table.tBodies[0];
295 dean_addEvent(headrow[i],"click", function(e) {
296  
297 if (this.className.search(/\bsorttable_sorted\b/) != -1) {
298 // if we're already sorted by this column, just
299 // reverse the table, which is quicker
300 sorttable.reverse(this.sorttable_tbody);
301 this.className = this.className.replace('sorttable_sorted',
302 'sorttable_sorted_reverse');
303 this.removeChild(document.getElementById('sorttable_sortfwdind'));
304 sortrevind = document.createElement('span');
305 sortrevind.id = "sorttable_sortrevind";
306 sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
307 this.appendChild(sortrevind);
308 return;
309 }
310 if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
311 // if we're already sorted by this column in reverse, just
312 // re-reverse the table, which is quicker
313 sorttable.reverse(this.sorttable_tbody);
314 this.className = this.className.replace('sorttable_sorted_reverse',
315 'sorttable_sorted');
316 this.removeChild(document.getElementById('sorttable_sortrevind'));
317 sortfwdind = document.createElement('span');
318 sortfwdind.id = "sorttable_sortfwdind";
319 sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
320 this.appendChild(sortfwdind);
321 return;
322 }
323
324 // remove sorttable_sorted classes
325 theadrow = this.parentNode;
326 forEach(theadrow.childNodes, function(cell) {
327 if (cell.nodeType == 1) { // an element
328 cell.className = cell.className.replace('sorttable_sorted_reverse','');
329 cell.className = cell.className.replace('sorttable_sorted','');
330 }
331 });
332 sortfwdind = document.getElementById('sorttable_sortfwdind');
333 if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
334 sortrevind = document.getElementById('sorttable_sortrevind');
335 if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
336
337 this.className += ' sorttable_sorted';
338 sortfwdind = document.createElement('span');
339 sortfwdind.id = "sorttable_sortfwdind";
340 sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
341 this.appendChild(sortfwdind);
342  
343 // build an array to sort. This is a Schwartzian transform thing,
344 // i.e., we "decorate" each row with the actual sort key,
345 // sort based on the sort keys, and then put the rows back in order
346 // which is a lot faster because you only do getInnerText once per row
347 row_array = [];
348 col = this.sorttable_columnindex;
349 rows = this.sorttable_tbody.rows;
350 for (var j=0; j<rows.length; j++) {
351 row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
352 }
353 /* If you want a stable sort, uncomment the following line */
354 sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
355 /* and comment out this one */
356 // row_array.sort(this.sorttable_sortfunction);
357
358 tb = this.sorttable_tbody;
359 for (var j=0; j<row_array.length; j++) {
360 tb.appendChild(row_array[j][1]);
361 }
362
363 delete row_array;
364 });
365 }
366 }
367 },
368
369 guessType: function(table, column) {
370 // guess the type of a column based on its first non-blank row
371 sortfn = sorttable.sort_alpha;
372 for (var i=0; i<table.tBodies[0].rows.length; i++) {
373 text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
374 if (text != '') {
375 if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
376 return sorttable.sort_numeric;
377 }
378 // check for a date: dd/mm/yyyy or dd/mm/yy
379 // can have / or . or - as separator
380 // can be mm/dd as well
381 possdate = text.match(sorttable.DATE_RE)
382 if (possdate) {
383 // looks like a date
384 first = parseInt(possdate[1]);
385 second = parseInt(possdate[2]);
386 if (first > 12) {
387 // definitely dd/mm
388 return sorttable.sort_ddmm;
389 } else if (second > 12) {
390 return sorttable.sort_mmdd;
391 } else {
392 // looks like a date, but we can't tell which, so assume
393 // that it's dd/mm (English imperialism!) and keep looking
394 sortfn = sorttable.sort_ddmm;
395 }
396 }
397 }
398 }
399 return sortfn;
400 },
401
402 getInnerText: function(node) {
403 // gets the text we want to use for sorting for a cell.
404 // strips leading and trailing whitespace.
405 // this is *not* a generic getInnerText function; it's special to sorttable.
406 // for example, you can override the cell text with a customkey attribute.
407 // it also gets .value for <input> fields.
408
409 hasInputs = (typeof node.getElementsByTagName == 'function') &&
410 node.getElementsByTagName('input').length;
411
412 // getAttribute fails on FF for node type 3
413  
414 if (node.nodeType != 3 && node.getAttribute("sorttable_customkey") != null) {
415 return node.getAttribute("sorttable_customkey");
416 }
417
418 else if (typeof node.textContent != 'undefined' && !hasInputs) {
419 return node.textContent.replace(/^\s+|\s+$/g, '');
420 }
421 else if (typeof node.innerText != 'undefined' && !hasInputs) {
422 return node.innerText.replace(/^\s+|\s+$/g, '');
423 }
424 else if (typeof node.text != 'undefined' && !hasInputs) {
425 return node.text.replace(/^\s+|\s+$/g, '');
426 }
427 else {
428 switch (node.nodeType) {
429 case 3:
430 if (node.nodeName.toLowerCase() == 'input') {
431 return node.value.replace(/^\s+|\s+$/g, '');
432 }
433 case 4:
434 return node.nodeValue.replace(/^\s+|\s+$/g, '');
435 break;
436 case 1:
437 case 11:
438 var innerText = '';
439 for (var i = 0; i < node.childNodes.length; i++) {
440 innerText += sorttable.getInnerText(node.childNodes[i]);
441 }
442 return innerText.replace(/^\s+|\s+$/g, '');
443 break;
444 default:
445 return '';
446 }
447 }
448 },
449
450 reverse: function(tbody) {
451 // reverse the rows in a tbody
452 newrows = [];
453 for (var i=0; i<tbody.rows.length; i++) {
454 newrows[newrows.length] = tbody.rows[i];
455 }
456 for (var i=newrows.length-1; i>=0; i--) {
457 tbody.appendChild(newrows[i]);
458 }
459 delete newrows;
460 },
461
462 /* sort functions
463 each sort function takes two parameters, a and b
464 you are comparing a[0] and b[0] */
465 sort_numeric: function(a,b) {
466 aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
467 if (isNaN(aa)) aa = 0;
468 bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
469 if (isNaN(bb)) bb = 0;
470 return aa-bb;
471 },
472 sort_alpha: function(a,b) {
473 if (a[0]==b[0]) return 0;
474 if (a[0]<b[0]) return -1;
475 return 1;
476 },
477 sort_ddmm: function(a,b) {
478 mtch = a[0].match(sorttable.DATE_RE);
479 y = mtch[3]; m = mtch[2]; d = mtch[1];
480 if (m.length == 1) m = '0'+m;
481 if (d.length == 1) d = '0'+d;
482 dt1 = y+m+d;
483 mtch = b[0].match(sorttable.DATE_RE);
484 y = mtch[3]; m = mtch[2]; d = mtch[1];
485 if (m.length == 1) m = '0'+m;
486 if (d.length == 1) d = '0'+d;
487 dt2 = y+m+d;
488 if (dt1==dt2) return 0;
489 if (dt1<dt2) return -1;
490 return 1;
491 },
492 sort_mmdd: function(a,b) {
493 mtch = a[0].match(sorttable.DATE_RE);
494 y = mtch[3]; d = mtch[2]; m = mtch[1];
495 if (m.length == 1) m = '0'+m;
496 if (d.length == 1) d = '0'+d;
497 dt1 = y+m+d;
498 mtch = b[0].match(sorttable.DATE_RE);
499 y = mtch[3]; d = mtch[2]; m = mtch[1];
500 if (m.length == 1) m = '0'+m;
501 if (d.length == 1) d = '0'+d;
502 dt2 = y+m+d;
503 if (dt1==dt2) return 0;
504 if (dt1<dt2) return -1;
505 return 1;
506 },
507
508 shaker_sort: function(list, comp_func) {
509 // A stable sort function to allow multi-level sorting of data
510 // see: http://en.wikipedia.org/wiki/Cocktail_sort
511 // thanks to Joseph Nahmias
512 var b = 0;
513 var t = list.length - 1;
514 var swap = true;
515  
516 while(swap) {
517 swap = false;
518 for(var i = b; i < t; ++i) {
519 if ( comp_func(list[i], list[i+1]) > 0 ) {
520 var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
521 swap = true;
522 }
523 } // for
524 t--;
525  
526 if (!swap) break;
527  
528 for(var i = t; i > b; --i) {
529 if ( comp_func(list[i], list[i-1]) < 0 ) {
530 var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
531 swap = true;
532 }
533 } // for
534 b++;
535  
536 } // while(swap)
537 }
538 }
539  
540 /* ******************************************************************
541 Supporting functions: bundled here to avoid depending on a library
542 ****************************************************************** */
543  
544 // Dean Edwards/Matthias Miller/John Resig
545  
546 /* for Mozilla/Opera9 */
547 if (document.addEventListener) {
548 document.addEventListener("DOMContentLoaded", sorttable.init, false);
549 }
550  
551 /* for Internet Explorer */
552 /*@cc_on @*/
553 /*@if (@_win32)
554 document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
555 var script = document.getElementById("__ie_onload");
556 script.onreadystatechange = function() {
557 if (this.readyState == "complete") {
558 sorttable.init(); // call the onload handler
559 }
560 };
561 /*@end @*/
562  
563 /* for Safari */
564 if (/WebKit/i.test(navigator.userAgent)) { // sniff
565 var _timer = setInterval(function() {
566 if (/loaded|complete/.test(document.readyState)) {
567 sorttable.init(); // call the onload handler
568 }
569 }, 10);
570 }
571  
572 /* for other browsers */
573 window.onload = sorttable.init;
574  
575 // written by Dean Edwards, 2005
576 // with input from Tino Zijdel, Matthias Miller, Diego Perini
577  
578 // http://dean.edwards.name/weblog/2005/10/add-event/
579  
580 function dean_addEvent(element, type, handler) {
581 if (element.addEventListener) {
582 element.addEventListener(type, handler, false);
583 } else {
584 // assign each event handler a unique ID
585 if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
586 // create a hash table of event types for the element
587 if (!element.events) element.events = {};
588 // create a hash table of event handlers for each element/event pair
589 var handlers = element.events[type];
590 if (!handlers) {
591 handlers = element.events[type] = {};
592 // store the existing event handler (if there is one)
593 if (element["on" + type]) {
594 handlers[0] = element["on" + type];
595 }
596 }
597 // store the event handler in the hash table
598 handlers[handler.$$guid] = handler;
599 // assign a global event handler to do all the work
600 element["on" + type] = handleEvent;
601 }
602 };
603 // a counter used to create unique IDs
604 dean_addEvent.guid = 1;
605  
606 function removeEvent(element, type, handler) {
607 if (element.removeEventListener) {
608 element.removeEventListener(type, handler, false);
609 } else {
610 // delete the event handler from the hash table
611 if (element.events && element.events[type]) {
612 delete element.events[type][handler.$$guid];
613 }
614 }
615 };
616  
617 function handleEvent(event) {
618 var returnValue = true;
619 // grab the event object (IE uses a global event object)
620 event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
621 // get a reference to the hash table of event handlers
622 var handlers = this.events[event.type];
623 // execute each event handler
624 for (var i in handlers) {
625 this.$$handleEvent = handlers[i];
626 if (this.$$handleEvent(event) === false) {
627 returnValue = false;
628 }
629 }
630 return returnValue;
631 };
632  
633 function fixEvent(event) {
634 // add W3C standard event methods
635 event.preventDefault = fixEvent.preventDefault;
636 event.stopPropagation = fixEvent.stopPropagation;
637 return event;
638 };
639 fixEvent.preventDefault = function() {
640 this.returnValue = false;
641 };
642 fixEvent.stopPropagation = function() {
643 this.cancelBubble = true;
644 }
645  
646 // Dean's forEach: http://dean.edwards.name/base/forEach.js
647 /*
648 forEach, version 1.0
649 Copyright 2006, Dean Edwards
650 License: http://www.opensource.org/licenses/mit-license.php
651 */
652  
653 // array-like enumeration
654 if (!Array.forEach) { // mozilla already supports this
655 Array.forEach = function(array, block, context) {
656 for (var i = 0; i < array.length; i++) {
657 block.call(context, array[i], i, array);
658 }
659 };
660 }
661  
662 // generic enumeration
663 Function.prototype.forEach = function(object, block, context) {
664 for (var key in object) {
665 if (typeof this.prototype[key] == "undefined") {
666 block.call(context, object[key], key, object);
667 }
668 }
669 };
670  
671 // character enumeration
672 String.forEach = function(string, block, context) {
673 Array.forEach(string.split(""), function(chr, index) {
674 block.call(context, chr, index, string);
675 });
676 };
677  
678 // globally resolve forEach enumeration
679 var forEach = function(object, block, context) {
680 if (object) {
681 var resolve = Object; // default
682 if (object instanceof Function) {
683 // functions have a "length" property
684 resolve = Function;
685 } else if (object.forEach instanceof Function) {
686 // the object implements a custom forEach method so use that
687 object.forEach(block, context);
688 return;
689 } else if (typeof object == "string") {
690 // the object is a string
691 resolve = String;
692 } else if (typeof object.length == "number") {
693 // the object is array-like
694 resolve = Array;
695 }
696 resolve.forEach(object, block, context);
697 }
698 };
699  
700 END_JS
701  
702 0 $javascript .= "</script>";
703  
704 0 return $javascript;
705 }
706  
707 0 ▶sub jsAddData {
Subroutine jsAddData: not called CC=1 SCAR=6.9
Pod undocumented
708  
709 0 my $self = shift;
710  
711 0 my $tableId = $self->{parent}->getTableId();
712
713 0 my $javascript = "<script language='javascript' type='text/javascript'>\n";
714 0 $javascript .= <<END_JS;
715  
716 var add_data_counter = 0;
717 var row_counter = 1;
718  
719 function addData() {
720  
721 add_data_counter++;
722
723 var newFieldsNode = document.getElementById('readroot_$tableId').cloneNode(true);
724 newFieldsNode.id = '';
725 var rowFields = newFieldsNode.childNodes;
726  
727 for (var i=0;i<rowFields.length;i++) {
728 var theName = rowFields[i].name;
729 var theId = rowFields[i].id;
730  
731 // special handling for the jscalendar integration
732 // using id here as div tag name attribute is not supported by FF
733  
734 if (theId == "jscalsetup_$tableId") {
735 // change the name of the input and span
736
737 var calFields = rowFields[i].childNodes;
738
739 // hidden input field
740  
741 calFields[0].name = calFields[0].name + '_-' + add_data_counter + '_-' + add_data_counter + '_-' + add_data_counter;
742 calFields[0].id = calFields[0].name;
743  
744 // text display and trigger
745  
746 calFields[2].id = calFields[2].id + '_-' + add_data_counter + '_-' + add_data_counter + '_-' + add_data_counter;
747
748 // call the calendar setup
749  
750 var calSetup = { inputField : calFields[0],
751 ifFormat : "%Y-%m-%d",
752 displayArea : calFields[2],
753 daFormat : "%Y-%m-%d",
754 cache : true
755 };
756
757 Calendar.setup(calSetup);
758 }
759 else if (theName) {
760 rowFields[i].name = theName + '_-' + add_data_counter + '_-' + add_data_counter + '_-' + add_data_counter;
761 rowFields[i].id = rowFields[i].name;
762 }
763 }
764
765 // add table cells and move the nodes into them
766  
767 var newrow = document.getElementById('addData').insertRow(row_counter);
768 var fieldCount = 0;
769  
770 while(rowFields.length > 0) {
771 var cell = newrow.insertCell(fieldCount++);
772 // note that the following method modifies the collection
773 cell.appendChild(rowFields[0]);
774 }
775 }
776 END_JS
777  
778 0 $javascript .= "</script>";
779 0 return $javascript;
780 }
781  
782 0 ▶sub jsCalendar10Setup {
Subroutine jsCalendar10Setup: not called CC=1 SCAR=6.9
Pod undocumented
783  
784 0 my $self = shift;
785 0 my $calendarDir = $self->{parent}->getCalendarDir();
786  
787 0 my $javascript = "<style type='text/css'>\@import url($calendarDir/calendar-win2k-1.css);</style>\n";
788 0 $javascript .= "<script type='text/javascript' src='$calendarDir/calendar.js'></script>\n";
789 0 $javascript .= "<script type='text/javascript' src='$calendarDir/lang/calendar-en.js'></script>\n";
790 0 $javascript .= "<script type='text/javascript' src='$calendarDir/calendar-setup.js'></script>\n";
791
792 0 return $javascript;
793 }
794  
795 0 ▶sub jsToolTip {
Subroutine jsToolTip: not called CC=1 SCAR=6.9
Pod undocumented
796  
797 0 my $self = shift;
798  
799 0 my $javascript = "<script language='javascript' type='text/javascript'>\n";
800 0 $javascript .= <<'END_JS';
801  
802 // from OReilly Javascript - The Definitive Guide
803  
804 // must put in body
805  
806 var Geometry = {};
807
808 // IE
809
810 if (window.screenLeft) {
811 Geometry.getWindowX = function() { return window.screenLeft; };
812 Geometry.getWindowY = function() { return window.screenTop; };
813 }
814
815 // Firefox
816
817 else if (window.screenX) {
818 Geometry.getWindowX = function() { return window.screenX; };
819 Geometry.getWindowY = function() { return window.screenY; };
820 }
821
822
823 // all but IE
824
825 if (window.innerWidth) {
826
827 Geometry.getViewportWidth = function() { return window.innerWidth; };
828 Geometry.getViewportHeight = function() { return window.innerHeight; };
829 Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
830 Geometry.getVerticalScroll = function() { return window.pageYOffset; };
831 }
832
833 // IE 6 w DOCTYPE
834
835 else if (document.documentElement && document.documentElement.clientWidth) {
836
837 Geometry.getViewportWidth = function() { return document.documentElement.clientWidth; };
838 Geometry.getViewportHeight = function() { return document.documentElement.clientHeight; };
839 Geometry.getHorizontalScroll = function() { return document.documentElement.scrollLeft; };
840 Geometry.getVerticalScroll = function() { return document.documentElement.scrollTop; };
841 }
842
843 // all other IE (not sure about 7)
844
845 else {
846
847 Geometry.getViewportWidth = function() { return document.body.clientWidth; };
848 Geometry.getViewportHeight = function() { return document.body.clientHeight; };
849 Geometry.getHorizontalScroll = function() { return document.body.scrollLeft; };
850 Geometry.getVerticalScroll = function() { return document.body.scrollTop; };
851 }
852
853 // document size
854
855 if (document.documentElement && document.documentElement.scrollWidth) {
856 Geometry.getDocumentWidth = function() { return document.documentElement.scrollWidth; };
857 Geometry.getDocumentHeight = function() { return document.documentElement.scrollHeight; };
858 }
859
860 else if (document.body.scrollWidth) {
861 Geometry.getDocumentWidth = function() { return document.body.scrollWidth; };
862 Geometry.getDocumentHeight = function() { return document.body.scrollHeight; };
863 }
864
865 // tooltips from Javascript, the Definitive Guide
866
867 function Tooltip() {
868
869 this.tooltip = document.createElement('div');
870 this.tooltip.style.position = 'absolute';
871 this.tooltip.style.visibility = 'hidden';
872 // this.tooltip.className = 'tooltipShadow';
873
874 this.content = document.createElement('div');
875 this.content.style.position = 'relative';
876 // this.content.className = 'tooltipContent';
877
878 // default properties
879  
880 this.content.style.backgroundColor = '#ff0';
881 this.content.style.padding = '5px';
882 this.content.style.border = 'solid black 1px';
883
884 this.tooltip.appendChild(this.content);
885 }
886
887 Tooltip.prototype.show = function(text, x, y) {
888 this.content.innerHTML = text;
889 this.tooltip.style.left = x + 'px';
890 this.tooltip.style.top = y + 'px';
891 this.tooltip.style.visibility = 'visible';
892  
893 if (this.tooltip.parentNode != document.body) document.body.appendChild(this.tooltip);
894 };
895  
896 // hide it
897  
898 Tooltip.prototype.hide = function() {
899 this.tooltip.style.visibility = 'hidden';
900 }
901  
902 Tooltip.X_OFFSET = 25; // pixels right
903 Tooltip.Y_OFFSET = 15; // pixels down
904 Tooltip.DELAY = 500; // msec
905  
906 Tooltip.prototype.schedule = function(target, e) {
907
908 var text = target.getAttribute('tooltip');
909 if(!text) return;
910
911 var x = e.clientX + Geometry.getHorizontalScroll();
912 var y = e.clientY + Geometry.getVerticalScroll();
913  
914 x += Tooltip.X_OFFSET;
915 y += Tooltip.Y_OFFSET;
916  
917 var self = this;
918
919 var timer = window.setTimeout(function() { self.show(text, x, y); }, Tooltip.DELAY);
920
921 if(target.addEventListener) target.addEventListener('mouseout', mouseout, false);
922 else if (target.attachEvent) target.attachEvent('onmouseout', mouseout);
923 else target.onmouseout = mouseout;
924  
925 function mouseout() {
926
927 self.hide();
928 window.clearTimeout(timer);
929 if (target.removeEventListener) target.removeEventListener('mouseout', mouseout, false);
930 else if (target.detachEvent) target.detachEvent('onmouseout', mouseout);
931 else target.onmouseout = null;
932 }
933 }
934  
935 // global tooltip
936  
937 Tooltip.tooltip = new Tooltip();
938 Tooltip.schedule = function(target, e) { Tooltip.tooltip.schedule(target, e); }
939 END_JS
940
941 0 $javascript .= "</script>";
942  
943 0 return $javascript;
944 }
945  
946 =head1 COPYRIGHT & LICENSE
947  
948 Copyright 2010 Freescale Semiconductor, all rights reserved.
949  
950 This program is free software; you can redistribute it and/or modify it
951 under the same terms as Perl itself.
952  
953  
954 =cut
955  
956 1 1; # End of HTML::EditableTable::JavaScript
« blib/lib/HTML/EditableTable/Vertical.pm ↑ Summary example/example_1.cgi »