OSDN Git Service

清書
[nucleus-jp/nucleus-plugins.git] / trunk / NP_Dtree / dtree / dtree.php
1 /*--------------------------------------------------|
2 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
3 |---------------------------------------------------|
4 | Copyright (c) 2002-2003 Geir Landr\e$B!&\e(B              |
5 |                                                   |
6 | This script can be used freely as long as all     |
7 | copyright messages are intact.                    |
8 |                                                   |
9 | Updated: 17.04.2003                               |
10 |--------------------------------------------------*/
11
12
13 <?php
14         $strRel = '../../../';
15         include($strRel . 'config.php');
16         include($DIR_LIBS . 'PLUGINADMIN.php');
17                 $oPluginAdmin =  new PluginAdmin('Dtree');
18                 $plug         =& $oPluginAdmin->plugin;
19                 $imgpath      =  $plug->getAdminURL();
20 //              $imgpath = 'nucleus/plugins/dtree/';
21
22                 if ($plug->getOption('folderLinks') == 'yes') {
23                         $folderLinks = 'true,';
24                 } else {
25                         $folderLinks = 'false,';
26                 }
27                 if ($plug->getOption('useSelection') == 'yes') {
28                         $useSelection = 'true,';
29                 } else {
30                         $useSelection = 'false,';
31                 }
32                 if ($plug->getOption('useCookies') == 'yes') {
33                         $useCookies = 'true,';
34                 } else {
35                         $useCookies = 'false,';
36                 }
37                 if ($plug->getOption('useLines') == 'yes') {
38                         $useLines = 'true,';
39                 } else {
40                         $useLines = 'false,';
41                 }
42                 if ($plug->getOption('useIcons') == 'yes') {
43                         $useIcons = 'true,';
44                 } else {
45                         $useIcons = 'false,';
46                 }
47                 if ($plug->getOption('useStatusText') == 'yes') {
48                         $useStatusText = 'true,';
49                 } else {
50                         $useStatusText = 'false,';
51                 }
52                 if ($plug->getOption('closeSameLevel') == 'yes') {
53                         $closeSameLevel = 'true,';
54                 } else {
55                         $closeSameLevel = 'false,';
56                 }
57                 if ($plug->getOption('inOrder') == 'yes') {
58                         $inOrder = 'true';
59                 } else {
60                         $inOrder = 'false';
61                 }
62
63 ?>
64 // Node object
65
66 var imgpath = "<?php echo $imgpath;?>";
67
68 function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
69
70         this.id = id;
71
72         this.pid = pid;
73
74         this.name = name;
75
76         this.url = url;
77
78         this.title = title;
79
80         this.target = target;
81
82         this.icon = icon;
83
84         this.iconOpen = iconOpen;
85
86         this._io = open || false;
87
88         this._is = false;
89
90         this._ls = false;
91
92         this._hc = false;
93
94         this._ai = 0;
95
96         this._p;
97
98 };
99
100 // Tree object
101
102 function dTree(objName) {
103
104         this.config = {
105                 target : null,
106
107                 folderLinks             : <?php echo $folderLinks ?>
108                 useSelection    : <?php echo $useSelection ?>
109                 useCookies              : <?php echo $useCookies ?>
110                 useLines                : <?php echo $useLines ?>
111                 useIcons                : <?php echo $useIcons ?>
112                 useStatusText   : <?php echo $useStatusText ?>
113                 closeSameLevel  : <?php echo $closeSameLevel ?>
114                 inOrder                 : <?php echo $inOrder ?>
115
116 //              folderLinks                     : true,
117 //              useSelection            : false,
118 //              useCookies                      : false,
119 //              useLines                                : false,
120 //              useIcons                                : false,
121 //              useStatusText           : true,
122 //              closeSameLevel  : true,
123 //              inOrder                                 : false
124         }
125
126         this.icon = {
127
128                 root                    : imgpath + 'img/base.gif',
129                 folder                  : imgpath + 'img/folder.gif',
130                 folderOpen              : imgpath + 'img/folderopen.gif',
131 //              node                    : imgpath + 'img/page.gif',
132                 node                    : imgpath + 'img/folder.gif',
133                 empty                   : imgpath + 'img/empty.gif',
134                 line                    : imgpath + 'img/line.gif',
135                 join                    : imgpath + 'img/join.gif',
136                 joinBottom              : imgpath + 'img/joinbottom.gif',
137                 plus                    : imgpath + 'img/plus.gif',
138                 plusBottom              : imgpath + 'img/plusbottom.gif',
139                 minus                   : imgpath + 'img/minus.gif',
140                 minusBottom             : imgpath + 'img/minusbottom.gif',
141                 nlPlus                  : imgpath + 'img/nolines_plus.gif',
142                 nlMinus                 : imgpath + 'img/nolines_minus.gif'
143
144         };
145
146         this.obj = objName;
147
148         this.aNodes = [];
149
150         this.aIndent = [];
151
152         this.root = new Node(-1);
153
154         this.selectedNode = null;
155
156         this.selectedFound = false;
157
158         this.completed = false;
159
160 };
161
162
163
164 // Adds a new node to the node array
165
166 dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
167
168         this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
169
170 };
171
172
173
174 // Open/close all nodes
175
176 dTree.prototype.openAll = function() {
177
178         this.oAll(true);
179
180 };
181
182 dTree.prototype.closeAll = function() {
183
184         this.oAll(false);
185
186 };
187
188
189
190 // Outputs the tree to the page
191
192 dTree.prototype.toString = function() {
193
194         var str = '<div class="dtree">\n';
195
196         if (document.getElementById) {
197
198                 if (this.config.useCookies) this.selectedNode = this.getSelected();
199
200                 str += this.addNode(this.root);
201
202         } else str += 'Browser not supported.';
203
204         str += '</div>';
205
206         if (!this.selectedFound) this.selectedNode = null;
207
208         this.completed = true;
209
210         return str;
211
212 };
213
214
215
216 // Creates the tree structure
217
218 dTree.prototype.addNode = function(pNode) {
219
220         var str = '';
221
222         var n=0;
223
224         if (this.config.inOrder) n = pNode._ai;
225
226         for (n; n<this.aNodes.length; n++) {
227
228                 if (this.aNodes[n].pid == pNode.id) {
229
230                         var cn = this.aNodes[n];
231
232                         cn._p = pNode;
233
234                         cn._ai = n;
235
236                         this.setCS(cn);
237
238                         if (!cn.target && this.config.target) cn.target = this.config.target;
239
240                         if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
241
242                         if (!this.config.folderLinks && cn._hc) cn.url = null;
243
244                         if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
245
246                                         cn._is = true;
247
248                                         this.selectedNode = n;
249
250                                         this.selectedFound = true;
251
252                         }
253
254                         str += this.node(cn, n);
255
256                         if (cn._ls) break;
257
258                 }
259
260         }
261
262         return str;
263
264 };
265
266
267
268 // Creates the node icon, url and text
269
270 dTree.prototype.node = function(node, nodeId) {
271
272         var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
273
274         if (this.config.useIcons) {
275
276                 if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
277
278                 if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
279
280                 if (this.root.id == node.pid) {
281
282                         node.icon = this.icon.root;
283
284                         node.iconOpen = this.icon.root;
285
286                 }
287
288                 str += '\n<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />\n';
289
290         }
291
292         if (node.url) {
293
294                 str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
295
296                 if (node.title) str += ' title="' + node.title + '"';
297
298                 if (node.target) str += ' target="' + node.target + '"';
299
300                 if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
301
302                 if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
303
304                         str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
305
306                 str += '>\n';
307
308         }
309
310         else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
311
312                 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
313
314         str += node.name;
315
316         if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>\n';
317
318         str += '</div>\n';
319
320         if (node._hc) {
321
322                 str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">\n';
323
324                 str += this.addNode(node);
325
326                 str += '</div>\n';
327
328         }
329
330         this.aIndent.pop();
331
332         return str;
333
334 };
335
336
337
338 // Adds the empty and line icons
339
340 dTree.prototype.indent = function(node, nodeId) {
341
342         var str = '';
343
344         if (this.root.id != node.pid) {
345
346                 for (var n=0; n<this.aIndent.length; n++)
347
348                         str += '\n<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
349
350                 (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
351
352                 if (node._hc) {
353
354                         str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
355
356                         if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
357
358                         else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );
359
360                         str += '" alt="" /></a>\n';
361
362                 } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />\n';
363
364         }
365
366         return str;
367
368 };
369
370
371
372 // Checks if a node has any children and if it is the last sibling
373
374 dTree.prototype.setCS = function(node) {
375
376         var lastId;
377
378         for (var n=0; n<this.aNodes.length; n++) {
379
380                 if (this.aNodes[n].pid == node.id) node._hc = true;
381
382                 if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
383
384         }
385
386         if (lastId==node.id) node._ls = true;
387
388 };
389
390
391
392 // Returns the selected node
393
394 dTree.prototype.getSelected = function() {
395
396         var sn = this.getCookie('cs' + this.obj);
397
398         return (sn) ? sn : null;
399
400 };
401
402
403
404 // Highlights the selected node
405
406 dTree.prototype.s = function(id) {
407
408         if (!this.config.useSelection) return;
409
410         var cn = this.aNodes[id];
411
412         if (cn._hc && !this.config.folderLinks) return;
413
414         if (this.selectedNode != id) {
415
416                 if (this.selectedNode || this.selectedNode==0) {
417
418                         eOld = document.getElementById("s" + this.obj + this.selectedNode);
419
420                         eOld.className = "node";
421
422                 }
423
424                 eNew = document.getElementById("s" + this.obj + id);
425
426                 eNew.className = "nodeSel";
427
428                 this.selectedNode = id;
429
430                 if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
431
432         }
433
434 };
435
436
437
438 // Toggle Open or close
439
440 dTree.prototype.o = function(id) {
441
442         var cn = this.aNodes[id];
443
444         this.nodeStatus(!cn._io, id, cn._ls);
445
446         cn._io = !cn._io;
447
448         if (this.config.closeSameLevel) this.closeLevel(cn);
449
450         if (this.config.useCookies) this.updateCookie();
451
452 };
453
454
455
456 // Open or close all nodes
457
458 dTree.prototype.oAll = function(status) {
459
460         for (var n=0; n<this.aNodes.length; n++) {
461
462                 if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
463
464                         this.nodeStatus(status, n, this.aNodes[n]._ls)
465
466                         this.aNodes[n]._io = status;
467
468                 }
469
470         }
471
472         if (this.config.useCookies) this.updateCookie();
473
474 };
475
476
477
478 // Opens the tree to a specific node
479
480 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
481
482         if (!bFirst) {
483
484                 for (var n=0; n<this.aNodes.length; n++) {
485
486                         if (this.aNodes[n].id == nId) {
487
488                                 nId=n;
489
490                                 break;
491
492                         }
493
494                 }
495
496         }
497
498         var cn=this.aNodes[nId];
499
500         if (cn.pid==this.root.id || !cn._p) return;
501
502         cn._io = true;
503
504         cn._is = bSelect;
505
506         if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
507
508         if (this.completed && bSelect) this.s(cn._ai);
509
510         else if (bSelect) this._sn=cn._ai;
511
512         this.openTo(cn._p._ai, false, true);
513
514 };
515
516
517
518 // Closes all nodes on the same level as certain node
519
520 dTree.prototype.closeLevel = function(node) {
521
522         for (var n=0; n<this.aNodes.length; n++) {
523
524                 if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
525
526                         this.nodeStatus(false, n, this.aNodes[n]._ls);
527
528                         this.aNodes[n]._io = false;
529
530                         this.closeAllChildren(this.aNodes[n]);
531
532                 }
533
534         }
535
536 }
537
538
539
540 // Closes all children of a node
541
542 dTree.prototype.closeAllChildren = function(node) {
543
544         for (var n=0; n<this.aNodes.length; n++) {
545
546                 if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
547
548                         if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
549
550                         this.aNodes[n]._io = false;
551
552                         this.closeAllChildren(this.aNodes[n]);          
553
554                 }
555
556         }
557
558 }
559
560
561
562 // Change the status of a node(open or closed)
563
564 dTree.prototype.nodeStatus = function(status, id, bottom) {
565
566         eDiv    = document.getElementById('d' + this.obj + id);
567
568         eJoin   = document.getElementById('j' + this.obj + id);
569
570         if (this.config.useIcons) {
571
572                 eIcon   = document.getElementById('i' + this.obj + id);
573
574                 eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
575
576         }
577
578         eJoin.src = (this.config.useLines)?
579
580         ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
581
582         ((status)?this.icon.nlMinus:this.icon.nlPlus);
583
584         eDiv.style.display = (status) ? 'block': 'none';
585
586 };
587
588
589
590
591
592 // [Cookie] Clears a cookie
593
594 dTree.prototype.clearCookie = function() {
595
596         var now = new Date();
597
598         var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
599
600         this.setCookie('co'+this.obj, 'cookieValue', yesterday);
601
602         this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
603
604 };
605
606
607
608 // [Cookie] Sets value in a cookie
609
610 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
611
612         document.cookie =
613
614                 escape(cookieName) + '=' + escape(cookieValue)
615
616                 + (expires ? '; expires=' + expires.toGMTString() : '')
617
618                 + (path ? '; path=' + path : '')
619
620                 + (domain ? '; domain=' + domain : '')
621
622                 + (secure ? '; secure' : '');
623
624 };
625
626
627
628 // [Cookie] Gets a value from a cookie
629
630 dTree.prototype.getCookie = function(cookieName) {
631
632         var cookieValue = '';
633
634         var posName = document.cookie.indexOf(escape(cookieName) + '=');
635
636         if (posName != -1) {
637
638                 var posValue = posName + (escape(cookieName) + '=').length;
639
640                 var endPos = document.cookie.indexOf(';', posValue);
641
642                 if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
643
644                 else cookieValue = unescape(document.cookie.substring(posValue));
645
646         }
647
648         return (cookieValue);
649
650 };
651
652
653
654 // [Cookie] Returns ids of open nodes as a string
655
656 dTree.prototype.updateCookie = function() {
657
658         var str = '';
659
660         for (var n=0; n<this.aNodes.length; n++) {
661
662                 if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
663
664                         if (str) str += '.';
665
666                         str += this.aNodes[n].id;
667
668                 }
669
670         }
671
672         this.setCookie('co' + this.obj, str);
673
674 };
675
676
677
678 // [Cookie] Checks if a node id is in a cookie
679
680 dTree.prototype.isOpen = function(id) {
681
682         var aOpen = this.getCookie('co' + this.obj).split('.');
683
684         for (var n=0; n<aOpen.length; n++)
685
686                 if (aOpen[n] == id) return true;
687
688         return false;
689
690 };
691
692
693
694 // If Push and pop is not implemented by the browser
695
696 if (!Array.prototype.push) {
697
698         Array.prototype.push = function array_push() {
699
700                 for(var i=0;i<arguments.length;i++)
701
702                         this[this.length]=arguments[i];
703
704                 return this.length;
705
706         }
707
708 };
709
710 if (!Array.prototype.pop) {
711
712         Array.prototype.pop = function array_pop() {
713
714                 lastElement = this[this.length-1];
715
716                 this.length = Math.max(this.length-1,0);
717
718                 return lastElement;
719
720         }
721
722 };