PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/albumtree/views/albumtree_block_dtree.html.php

https://github.com/wrlee/gallery3-contrib
PHP | 414 lines | 348 code | 26 blank | 40 comment | 122 complexity | ceb77b783c7f84ad5464da94e1c7f941 MD5 | raw file
  1. <?php defined("SYSPATH") or die("No direct script access.") ?>
  2. <script type="text/javascript">
  3. /*--------------------------------------------------|
  4. | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
  5. |---------------------------------------------------|
  6. | Copyright (c) 2002-2003 Geir Landrö |
  7. | |
  8. | This script can be used freely as long as all |
  9. | copyright messages are intact. |
  10. | |
  11. | Updated: 17.04.2003 |
  12. |--------------------------------------------------*/
  13. // Node object
  14. function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
  15. this.id = id;
  16. this.pid = pid;
  17. this.name = name;
  18. this.url = url;
  19. this.title = title;
  20. this.target = target;
  21. this.icon = icon;
  22. this.iconOpen = iconOpen;
  23. this._io = open || false;
  24. this._is = false;
  25. this._ls = false;
  26. this._hc = false;
  27. this._ai = 0;
  28. this._p;
  29. };
  30. // Tree object
  31. function dTree(objName) {
  32. this.config = {
  33. target : null,
  34. folderLinks : true,
  35. useSelection : true,
  36. useCookies : true,
  37. useLines : true,
  38. useIcons : false,
  39. useStatusText : false,
  40. closeSameLevel : false,
  41. inOrder : false,
  42. cookiePath : null,
  43. cookieDomain : null
  44. }
  45. this.obj = objName;
  46. this.aNodes = [];
  47. this.aIndent = [];
  48. this.root = new Node(-1);
  49. this.selectedNode = null;
  50. this.selectedFound = false;
  51. this.completed = false;
  52. };
  53. // Adds a new node to the node array
  54. dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
  55. this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
  56. };
  57. // Open/close all nodes
  58. dTree.prototype.openAll = function() {
  59. this.oAll(true);
  60. };
  61. dTree.prototype.closeAll = function() {
  62. this.oAll(false);
  63. };
  64. // Outputs the tree to the page
  65. dTree.prototype.toString = function() {
  66. var str = '<div class="dtree">\n';
  67. if (document.getElementById) {
  68. if (this.config.useCookies) this.selectedNode = this.getSelected();
  69. str += this.addNode(this.root);
  70. } else str += 'Browser not supported.';
  71. str += '</div>';
  72. if (!this.selectedFound) this.selectedNode = null;
  73. this.completed = true;
  74. return str;
  75. };
  76. // Creates the tree structure
  77. dTree.prototype.addNode = function(pNode) {
  78. var str = '';
  79. var n=0;
  80. if (this.config.inOrder) n = pNode._ai;
  81. for (n; n<this.aNodes.length; n++) {
  82. if (this.aNodes[n].pid == pNode.id) {
  83. var cn = this.aNodes[n];
  84. cn._p = pNode;
  85. cn._ai = n;
  86. this.setCS(cn);
  87. if (!cn.target && this.config.target) cn.target = this.config.target;
  88. if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
  89. if (!this.config.folderLinks && cn._hc) cn.url = null;
  90. if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
  91. cn._is = true;
  92. this.selectedNode = n;
  93. this.selectedFound = true;
  94. }
  95. str += this.node(cn, n);
  96. if (cn._ls) break;
  97. }
  98. }
  99. return str;
  100. };
  101. // Creates the node icon, url and text
  102. dTree.prototype.node = function(node, nodeId) {
  103. var str = '<div class="dTreeNode" title="' + node.name + '">' + this.indent(node, nodeId);
  104. if (this.config.useIcons) {
  105. if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
  106. if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
  107. if (this.root.id == node.pid) {
  108. node.icon = this.icon.root;
  109. node.iconOpen = this.icon.root;
  110. }
  111. str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
  112. }
  113. if (node.url) {
  114. str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
  115. if (node.title) str += ' title="' + node.title + '"';
  116. if (node.target) str += ' target="' + node.target + '"';
  117. if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
  118. if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
  119. str += ' onclick="' + this.obj + '.s(' + nodeId + ');"';
  120. str += '>';
  121. }
  122. else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
  123. str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
  124. str += node.name;
  125. if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
  126. str += '</div>';
  127. if (node._hc) {
  128. str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
  129. str += this.addNode(node);
  130. str += '</div>';
  131. }
  132. this.aIndent.pop();
  133. return str;
  134. };
  135. // Adds the empty and line icons
  136. dTree.prototype.indent = function(node, nodeId) {
  137. var str = '';
  138. if (this.root.id != node.pid) {
  139. for (var n=0; n<this.aIndent.length; n++)
  140. str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
  141. (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
  142. if (node._hc) {
  143. str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
  144. if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
  145. 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 ) );
  146. str += '" alt="" /></a>';
  147. } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
  148. }
  149. return str;
  150. };
  151. // Checks if a node has any children and if it is the last sibling
  152. dTree.prototype.setCS = function(node) {
  153. var lastId;
  154. for (var n=0; n<this.aNodes.length; n++) {
  155. if (this.aNodes[n].pid == node.id) node._hc = true;
  156. if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
  157. }
  158. if (lastId==node.id) node._ls = true;
  159. };
  160. // Returns the selected node
  161. dTree.prototype.getSelected = function() {
  162. var sn = this.getCookie('cs' + this.obj);
  163. return (sn) ? sn : null;
  164. };
  165. // Highlights the selected node
  166. dTree.prototype.s = function(id) {
  167. if (!this.config.useSelection) return;
  168. var cn = this.aNodes[id];
  169. if (cn._hc && !this.config.folderLinks) return;
  170. if (this.selectedNode != id) {
  171. if (this.selectedNode || this.selectedNode==0) {
  172. eOld = document.getElementById("s" + this.obj + this.selectedNode);
  173. eOld.className = "node";
  174. }
  175. eNew = document.getElementById("s" + this.obj + id);
  176. eNew.className = "nodeSel";
  177. this.selectedNode = id;
  178. if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
  179. }
  180. };
  181. // Toggle Open or close
  182. dTree.prototype.o = function(id) {
  183. var cn = this.aNodes[id];
  184. this.nodeStatus(!cn._io, id, cn._ls);
  185. cn._io = !cn._io;
  186. if (this.config.closeSameLevel) this.closeLevel(cn);
  187. if (this.config.useCookies) this.updateCookie();
  188. };
  189. // Open or close all nodes
  190. dTree.prototype.oAll = function(status) {
  191. for (var n=0; n<this.aNodes.length; n++) {
  192. if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
  193. this.nodeStatus(status, n, this.aNodes[n]._ls)
  194. this.aNodes[n]._io = status;
  195. }
  196. }
  197. if (this.config.useCookies) this.updateCookie();
  198. };
  199. // Opens the tree to a specific node
  200. dTree.prototype.openTo = function(nId, bSelect, bFirst) {
  201. if (!bFirst) {
  202. for (var n=0; n<this.aNodes.length; n++) {
  203. if (this.aNodes[n].id == nId) {
  204. nId=n;
  205. break;
  206. }
  207. }
  208. }
  209. var cn=this.aNodes[nId];
  210. if (cn.pid==this.root.id || !cn._p) return;
  211. cn._io = true;
  212. cn._is = bSelect;
  213. if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
  214. if (this.completed && bSelect) this.s(cn._ai);
  215. else if (bSelect) this._sn=cn._ai;
  216. this.openTo(cn._p._ai, false, true);
  217. };
  218. // Closes all nodes on the same level as certain node
  219. dTree.prototype.closeLevel = function(node) {
  220. for (var n=0; n<this.aNodes.length; n++) {
  221. if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
  222. this.nodeStatus(false, n, this.aNodes[n]._ls);
  223. this.aNodes[n]._io = false;
  224. this.closeAllChildren(this.aNodes[n]);
  225. }
  226. }
  227. }
  228. // Closes all children of a node
  229. dTree.prototype.closeAllChildren = function(node) {
  230. for (var n=0; n<this.aNodes.length; n++) {
  231. if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
  232. if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
  233. this.aNodes[n]._io = false;
  234. this.closeAllChildren(this.aNodes[n]);
  235. }
  236. }
  237. }
  238. // Change the status of a node(open or closed)
  239. dTree.prototype.nodeStatus = function(status, id, bottom) {
  240. eDiv = document.getElementById('d' + this.obj + id);
  241. eJoin = document.getElementById('j' + this.obj + id);
  242. if (this.config.useIcons) {
  243. eIcon = document.getElementById('i' + this.obj + id);
  244. eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
  245. }
  246. eJoin.src = (this.config.useLines)?
  247. ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
  248. ((status)?this.icon.nlMinus:this.icon.nlPlus);
  249. eDiv.style.display = (status) ? 'block': 'none';
  250. };
  251. // [Cookie] Clears a cookie
  252. dTree.prototype.clearCookie = function() {
  253. var now = new Date();
  254. var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
  255. this.setCookie('co'+this.obj, 'cookieValue', yesterday);
  256. this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
  257. };
  258. // [Cookie] Sets value in a cookie
  259. dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
  260. path = path || this.config.cookiePath;
  261. domain = domain || this.config.cookieDomain;
  262. document.cookie =
  263. escape(cookieName) + '=' + escape(cookieValue)
  264. + (expires ? '; expires=' + expires.toGMTString() : '')
  265. + (path ? '; path=' + path : '')
  266. + (domain ? '; domain=' + domain : '')
  267. + (secure ? '; secure' : '');
  268. };
  269. // [Cookie] Gets a value from a cookie
  270. dTree.prototype.getCookie = function(cookieName) {
  271. var cookieValue = '';
  272. var posName = document.cookie.indexOf(escape(cookieName) + '=');
  273. if (posName != -1) {
  274. var posValue = posName + (escape(cookieName) + '=').length;
  275. var endPos = document.cookie.indexOf(';', posValue);
  276. if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
  277. else cookieValue = unescape(document.cookie.substring(posValue));
  278. }
  279. return (cookieValue);
  280. };
  281. // [Cookie] Returns ids of open nodes as a string
  282. dTree.prototype.updateCookie = function() {
  283. var str = '';
  284. for (var n=0; n<this.aNodes.length; n++) {
  285. if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
  286. if (str) str += '.';
  287. str += this.aNodes[n].id;
  288. }
  289. }
  290. this.setCookie('co' + this.obj, str);
  291. };
  292. // [Cookie] Checks if a node id is in a cookie
  293. dTree.prototype.isOpen = function(id) {
  294. var aOpen = this.getCookie('co' + this.obj).split('.');
  295. for (var n=0; n<aOpen.length; n++)
  296. if (aOpen[n] == id) return true;
  297. return false;
  298. };
  299. // If Push and pop is not implemented by the browser
  300. if (!Array.prototype.push) {
  301. Array.prototype.push = function array_push() {
  302. for(var i=0;i<arguments.length;i++)
  303. this[this.length]=arguments[i];
  304. return this.length;
  305. }
  306. };
  307. if (!Array.prototype.pop) {
  308. Array.prototype.pop = function array_pop() {
  309. lastElement = this[this.length-1];
  310. this.length = Math.max(this.length-1,0);
  311. return lastElement;
  312. }
  313. };
  314. </script>
  315. <style type="text/css">
  316. /*--------------------------------------------------|
  317. | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
  318. |---------------------------------------------------|
  319. | Copyright (c) 2002-2003 Geir Landrö |
  320. |--------------------------------------------------*/
  321. .dtree {
  322. font-size: 1.05em;
  323. white-space: nowrap;
  324. }
  325. .dtree img {
  326. border: 0px;
  327. vertical-align: middle;
  328. }
  329. .dtree a.node, .dtree a.nodeSel {
  330. white-space: nowrap;
  331. padding: 1px 2px 1px 2px;
  332. }
  333. .dtree a.nodeSel {
  334. font-style: italic;
  335. }
  336. .dtree .clip {
  337. overflow: hidden;
  338. }
  339. </style>
  340. <div class="block-albumselect-AlbumTree gbBlock">
  341. <div class="dtree">
  342. <script type="text/javascript">
  343. // <![CDATA[
  344. function albumSelect_goToNode(nodeId) {
  345. document.location = new String('main.php?g2_itemId=__ID__').replace('__ID__', nodeId);
  346. }
  347. var albumTree = new dTree('albumTree');
  348. var albumTree_images = '<?= item::root()->url() ?>modules/albumtree/images/'
  349. albumTree.icon = {
  350. root : albumTree_images + 'base.gif',
  351. folder : albumTree_images + 'folder.gif',
  352. folderOpen : albumTree_images + 'imgfolder.gif',
  353. node : albumTree_images + 'imgfolder.gif',
  354. empty : albumTree_images + 'empty.gif',
  355. line : albumTree_images + 'line.gif',
  356. join : albumTree_images + 'join.gif',
  357. joinBottom : albumTree_images + 'joinbottom.gif',
  358. plus : albumTree_images + 'plus.gif',
  359. plusBottom : albumTree_images + 'plusbottom.gif',
  360. minus : albumTree_images + 'minus.gif',
  361. minusBottom : albumTree_images + 'minusbottom.gif',
  362. nlPlus : albumTree_images + 'nolines_plus.gif',
  363. nlMinus : albumTree_images + 'nolines_minus.gif'
  364. };
  365. albumTree.config.useLines = true;
  366. albumTree.config.useIcons = false;
  367. albumTree.config.useCookies = false;
  368. albumTree.config.closeSameLevel = false;
  369. albumTree.config.cookiePath = '<?= item::root()->url() ?>';
  370. albumTree.config.cookieDomain = '';
  371. { var pf = '<?= item::root()->url() ?>';
  372. <?
  373. function addtree($album){
  374. ?>
  375. albumTree.add(<?= $album->id -1 ?>, <?= $album->parent_id -1 ?>, "<?= $album->title ?>", pf+'<?= $album->relative_url_cache ?>');
  376. <?
  377. foreach ($album->viewable()->children(null, null, array(array("type", "=", "album"))) as $child){
  378. addtree($child);
  379. }
  380. }
  381. addtree($root);
  382. ?>
  383. }
  384. document.write(albumTree);
  385. // ]]>
  386. </script>
  387. </div>
  388. </div>