You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1010 lines
32 KiB

  1. /*--------------------------------------------------------|
  2. | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
  3. |--------------------------------------------------------|
  4. | Copyright (c) 2002-2003 Geir Landro |
  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. | Modified for Dokuwiki by |
  12. | Samuele Tognini <samuele@samuele.netsons.org> |
  13. | under GPL 2 license |
  14. | (http://www.gnu.org/licenses/gpl.html) |
  15. | Updated: 29.08.2009 |
  16. |--------------------------------------------------------|
  17. | Modified for Dokuwiki by |
  18. | Rene Hadler <rene.hadler@iteas.at> |
  19. | under GPL 2 license |
  20. | (http://www.gnu.org/licenses/gpl.html) |
  21. | Updated: 07.08.2012 |
  22. |--------------------------------------------------------|
  23. | jQuery update - 27 02 2012 |
  24. | Gerrit Uitslag <klapinklapin@gmail.com |
  25. |--------------------------------------------------------|
  26. | indexmenu | https://www.dokuwiki.org/plugin:indexmenu |
  27. |-------------------------------------------------------*/
  28. /* global DOKU_COOKIE_PARAM */
  29. /* global DOKU_BASE */
  30. /*
  31. * ids used in the dTree:
  32. * - div#cdtree_<id indexmenu> div top level
  33. * - div#dtree_<id indexmenu> div contains all nodes
  34. * - div#toc_<id indexmenu> ??
  35. * - div.dtreeNode
  36. * - img#i<id indexmenu><nodenr?> icon
  37. * - a#s<id indexmenu><nodenr?> url to page/namespace with title
  38. * - div#t<id indexmenu><nodenr?> button for opening ToC, included if hovered
  39. * - div.d<id indexmenu><nodenr?>
  40. * repeats: - div.dtreeNode (with img#i, a#s and div#t)
  41. * repeats: - div.d<id indexmenu><nodenr?>
  42. * - z<id indexmenu> scroll rightward arrows
  43. * - left_<id indexmenu> scroll leftward arrows
  44. *
  45. * at the end of body:
  46. * - picker_<id indexmenu> popup with ToC
  47. * - r<id indexmenu> rightmouse button menu
  48. */
  49. /**
  50. * dTreeNode object
  51. *
  52. * @param {string} dokuid page id of node
  53. * @param {number} id node id
  54. * @param {number} pid parent node id
  55. * @param {string} name Page Title
  56. * @param {number|string} hns page id of headpage of namespace
  57. * @param {number} isdir is directory?
  58. * @param {number} ajax load subnodes by ajax
  59. * @constructor
  60. */
  61. function dTreeNode(dokuid, id, pid, name, hns, isdir, ajax) {
  62. /** @type {string} */
  63. this.dokuid = dokuid; // page id of node
  64. /** @type {number} */
  65. this.id = id; // id number of node
  66. /** @type {number} */
  67. this.pid = pid; // id number of parent node
  68. /** @type {string} */
  69. this.name = name; // ns/page title
  70. /** @type {number|string} */
  71. this.hns = hns; // headpage of namespace or zero
  72. /** @type {boolean} */
  73. this.isdir = Boolean(isdir); // is directory
  74. /** @type {boolean} */
  75. this.ajax = Boolean(ajax); // load its nodes by ajax
  76. /** @type {boolean} */
  77. this._io = false; // is node open
  78. /** @type {boolean} */
  79. this._is = false; // is selected
  80. /** @type {boolean} */
  81. this._ls = false; // is last sibling
  82. /** @type {boolean} */
  83. this._hc = Boolean(ajax); // has children
  84. /** @type {number} */
  85. this._ai = 0; // id number of first child....
  86. /** @type {dTreeNode} */
  87. this._p = undefined; // parent dTreeNode
  88. /** @type {number} */
  89. this._lvl = 0; // level
  90. /** @type {boolean} */
  91. this._ok = false; // all children are loaded
  92. /** @type {boolean} */
  93. this._cp = false; // current page
  94. /** @type {string} */
  95. this.icon = ''; // icon of closed node
  96. /** @type {string} */
  97. this.iconOpen = ''; // icon of opened node
  98. }
  99. /**
  100. * Tree object
  101. *
  102. * @param {string} treeName id of the indexmenu, has form 'indexmenu_<identifier>'
  103. * @param {string} theme name of theme dir
  104. * @constructor
  105. */
  106. function dTree(treeName, theme) {
  107. let imgExt = IndexmenuUtils.determineExtension(theme);
  108. this.config = {
  109. urlbase: DOKU_BASE + 'doku.php?id=', // base of dokuwiki (set in page)
  110. plugbase: DOKU_BASE + 'lib/plugins/indexmenu', // base of plugin folder
  111. useCookies: true, // use cookies (set in page) e.g. disabled for context option
  112. scroll: true, // enable scrolling of tree in too small columns (set in page)
  113. toc: true, // enable ToC popups in tree (set in page)
  114. maxjs: 1, // number set by maxjs option (set in page)
  115. jsajax: '', // &max=#&sort=(t|d)&msort=(indexmenu_n|<metakey>)&rsort=1&nsort=1&hsort=1&nopg=1&skipns=+=/.../&skipfile=+=/.../(set in page)
  116. sepchar: ':', // value ':', ';' or '/' (set in page)
  117. theme: theme // dir name of theme folder
  118. };
  119. let imagePath = this.config.plugbase + '/images/' + theme + '/';
  120. this.icon = {
  121. root: imagePath + 'base.' + imgExt,
  122. folder: imagePath + 'folder.' + imgExt,
  123. folderH: imagePath + 'folderh.' + imgExt,
  124. folderOpen: imagePath + 'folderopen.' + imgExt,
  125. folderHOpen: imagePath + 'folderhopen.' + imgExt,
  126. node: imagePath + 'page.' + imgExt,
  127. empty: imagePath + 'empty.' + imgExt,
  128. line: imagePath + 'line.' + imgExt,
  129. join: imagePath + 'join.' + imgExt,
  130. joinBottom: imagePath + 'joinbottom.' + imgExt,
  131. plus: imagePath + 'plus.' + imgExt,
  132. plusBottom: imagePath + 'plusbottom.' + imgExt,
  133. minus: imagePath + 'minus.' + imgExt,
  134. minusBottom: imagePath + 'minusbottom.' + imgExt,
  135. nlPlus: imagePath + 'nolines_plus.' + imgExt,
  136. nlMinus: imagePath + 'nolines_minus.' + imgExt
  137. };
  138. /** @type {string} */
  139. this.treeName = treeName; // (unique) name of this indexmenu
  140. /** @type {dTreeNode[]} */
  141. this.aNodes = []; // array of nodes
  142. /** @type {number[]} */
  143. this.aIndent = []; // array stores the indents of the tree (contains values 0 or 1)
  144. /** @type {dTreeNode} */
  145. this.root = new dTreeNode(false, -1);
  146. /** @type {number} */
  147. this.selectedNode = undefined; // node id
  148. /** @type {boolean} */
  149. this.selectedFound = false; // set to true when found
  150. /** @type {boolean} */
  151. this.completed = false; // succesfull written js tree to the page
  152. /** @type {number} */
  153. this.scrllTmr = 0; // store timer id for horizontal scrolling the page
  154. /** @type {string} */
  155. this.pageid = JSINFO.id || ''; // current page
  156. this.fajax = false; // if retrieve next level of opened nodes
  157. }
  158. /**
  159. * CSS classes:
  160. *
  161. * a.nodeFdUrl Namespace with url link (headpage)
  162. * a.node Namespace without url link
  163. * a.nodeUrl Page
  164. * a.nodeSel Last visited page
  165. * a.navSel Current page
  166. */
  167. /**
  168. * Adds a new node to the node array
  169. *
  170. * @param {string} dokuid page id of node
  171. * @param {number} id node id
  172. * @param {number} pid parent node id
  173. * @param {string} name Page Title
  174. * @param {number|string} hns page id of headpage of namespace
  175. * @param {number} isdir is directory?
  176. * @param {number} ajax load subnodes by ajax
  177. */
  178. dTree.prototype.add = function (dokuid, id, pid, name, hns, isdir, ajax) {
  179. this.aNodes[this.aNodes.length] = new dTreeNode(dokuid, id, pid, name, hns, isdir, ajax);
  180. };
  181. /**
  182. * Open all nodes, if no node status was stored in cookie
  183. */
  184. dTree.prototype.openAll = function () {
  185. if (!this.getCookie('co' + this.treeName)) {
  186. this.oAll(true);
  187. }
  188. };
  189. /**
  190. * Outputs the tree to the page. Called by document.write after adding the nodes to the tree.
  191. *
  192. * @returns {string} html of whole tree
  193. */
  194. dTree.prototype.toString = function () {
  195. let str = '';
  196. this.pageid = this.pageid.replace(/:/g,this.config.sepchar);
  197. if (this.config.scroll) {
  198. str += '<div id="cdtree_' + this.treeName + '" class="dtree" style="position:relative;overflow:hidden;width:100%;">';
  199. }
  200. str += '<div id="dtree_' + this.treeName + '" class="dtree ' + this.config.theme + '" style="overflow:';
  201. if (this.config.scroll) {
  202. str += 'visible;position:relative;width:100%"';
  203. } else {
  204. str += 'hidden;"';
  205. }
  206. str += '>';
  207. if (jQuery('#dtree_' + this.treeName)[0]) {
  208. str += '<div class="error">Indexmenu id conflict</div>';
  209. }
  210. if (this.config.toc) {
  211. str += '<div id="t' + this.treeName + '" class="indexmenu_tocbullet ' + this.config.theme + '" style="display:none;" title="Table of contents"></div>';
  212. str += '<div id="toc_' + this.treeName + '" style="display:none;"></div>';
  213. }
  214. if (this.config.useCookies) {
  215. this.selectedNode = this.getSelected();
  216. }
  217. str += this.addNode(this.root) + '</div>';
  218. if (this.config.scroll) {
  219. str += '<div id="z' + this.treeName + '" class="indexmenu_rarrow"></div>';
  220. str += '<div id="left_' + this.treeName + '" class="indexmenu_larrow" style="display:none;" title="Click to scroll back" onmousedown="' + this.treeName + '.scroll(\'r\',1)" onmouseup="' + this.treeName + '.stopscroll()"></div>';
  221. str += '</div>';
  222. }
  223. this.completed = true;
  224. //hide the fallback nojs indexmenu
  225. jQuery('#nojs_' + this.treeName).css("display", "none"); //using .hide(); let's crash opera
  226. return str;
  227. };
  228. /**
  229. * Creates the tree structure
  230. *
  231. * @param {dTreeNode} pNode
  232. * @returns {string} html of node (inclusive children)
  233. */
  234. dTree.prototype.addNode = function (pNode) {
  235. let str = '', cn, n = pNode._ai, l = pNode._lvl + 1;
  236. for (n; n < this.aNodes.length; n++) {
  237. if (this.aNodes[n].pid === pNode.id) {
  238. cn = this.aNodes[n];
  239. cn._p = pNode;
  240. cn._ai = n;
  241. cn._lvl = l;
  242. this.setCS(cn);
  243. if (cn._hc && !cn._io && this.config.useCookies) {
  244. cn._io = this.isOpen(cn.id);
  245. }
  246. if (this.pageid === (!cn.hns && cn.dokuid || cn.hns)) {
  247. cn._cp = true;
  248. } else if (cn.id === this.selectedNode && !this.selectedFound) {
  249. cn._is = true;
  250. this.selectedNode = n;
  251. this.selectedFound = true;
  252. }
  253. if (!cn._hc && cn.isdir && !cn.ajax && !cn.hns) {
  254. if (cn._ls) {
  255. str += this.noderr(cn, n);
  256. }
  257. } else {
  258. str += this.node(cn, n);
  259. }
  260. if (cn._ls) {
  261. break;
  262. }
  263. }
  264. }
  265. return str;
  266. };
  267. /**
  268. * Create empty node
  269. *
  270. * @param {dTreeNode} node
  271. * @param {int} nodeId
  272. * @returns {string} html of empty node
  273. */
  274. dTree.prototype.noderr = function (node, nodeId) {
  275. let str = '<div class="dTreeNode">' + this.indent(node, nodeId);
  276. str += '<div class="emptynode" title="Empty"></div></div>';
  277. return str;
  278. };
  279. /**
  280. * Creates the node icon, url and text
  281. *
  282. * @param {dTreeNode} node
  283. * @param {int} nodeId
  284. * @returns {string} html of node (inclusive children)
  285. */
  286. dTree.prototype.node = function (node, nodeId) {
  287. let h = 1, jsfnc, str;
  288. jsfnc = 'onmouseover="' + this.treeName + '.show_feat(\'' + nodeId + '\');" onmousedown="return IndexmenuContextmenu.checkcontextm(\'' + nodeId + '\',' + this.treeName + ',event);" oncontextmenu="return IndexmenuContextmenu.stopevt(event)"';
  289. if (node._lvl > this.config.maxjs) {
  290. h = 0;
  291. } else {
  292. node._ok = true;
  293. }
  294. str = '<div class="dTreeNode">' + this.indent(node, nodeId);
  295. node.icon = (this.root.id === node.pid) ? this.icon.root : ((node.hns) ? this.icon.folderH : ((node._hc) ? this.icon.folder : this.icon.node));
  296. node.iconOpen = (node._hc) ? ((node.hns) ? this.icon.folderHOpen : this.icon.folderOpen) : this.icon.node;
  297. if (this.root.id === node.pid) {
  298. node.icon = this.icon.root;
  299. node.iconOpen = this.icon.root;
  300. }
  301. str += '<img id="i' + this.treeName + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
  302. if (!node._hc || node.hns) {
  303. str += '<a id="s' + this.treeName + nodeId + '" class="' + ((node._cp) ? 'navSel' : ((node._is) ? 'nodeSel' : (node._hc) ? 'nodeFdUrl' : 'nodeUrl'));
  304. str += '" href="' + this.config.urlbase;
  305. (node.hns) ? str += node.hns : str += node.dokuid;
  306. str += '"' + ' title="' + node.name + '"' + jsfnc;
  307. str += ' onclick="javascript: ' + this.treeName + '.s(' + nodeId + ');"';
  308. str += ' data-wiki-id="' + node.dokuid + '"';
  309. str += '>' + node.name + '</a>';
  310. }
  311. else if (node.pid !== this.root.id) {
  312. str += '<a id="s' + this.treeName + nodeId + '" href="javascript: ' + this.treeName + '.o(' + nodeId + '); " data-wiki-id="' + node.dokuid + '" class="node"' + jsfnc + '>' + node.name + '</a>';
  313. } else {
  314. str += node.name;
  315. }
  316. str += '</div>';
  317. if (node._hc) {
  318. str += '<div id="d' + this.treeName + nodeId + '" class="clip" style="display:' + ((this.root.id === node.pid || node._io) ? 'block' : 'none') + ';">';
  319. if (h) {
  320. str += this.addNode(node);
  321. }
  322. str += '</div>';
  323. }
  324. this.aIndent.pop();
  325. return str;
  326. };
  327. /**
  328. * Adds the empty and line icons which indent the node
  329. *
  330. * @param {dTreeNode} node
  331. * @param {int} nodeId
  332. * @returns {string} html of indent icons
  333. */
  334. dTree.prototype.indent = function (node, nodeId) {
  335. let n, str = '';
  336. if (this.root.id !== node.pid) {
  337. for (n = 0; n < this.aIndent.length; n++) {
  338. str += '<img src="' + ( (this.aIndent[n] === 1) ? this.icon.line : this.icon.empty ) + '" alt="" />';
  339. }
  340. if (node._ls) {
  341. this.aIndent.push(0);
  342. } else {
  343. this.aIndent.push(1);
  344. }
  345. if (node._hc) {
  346. str += '<a href="javascript: ' + this.treeName + '.o(' + nodeId + ');">' +
  347. '<img id="j' + this.treeName + nodeId + '" src="' +
  348. ( (node._io) ? ((node._ls) ? this.icon.minusBottom : this.icon.minus) : ((node._ls) ? this.icon.plusBottom : this.icon.plus ) ) +
  349. '" alt="" /></a>';
  350. } else {
  351. str += '<img src="' + ((node._ls) ? this.icon.joinBottom : this.icon.join) + '" alt="" />';
  352. }
  353. }
  354. return str;
  355. };
  356. /**
  357. * Checks if a node has any children and if it is the last sibling
  358. *
  359. * @param {dTreeNode} node
  360. */
  361. dTree.prototype.setCS = function (node) {
  362. let lastId, n;
  363. for (n = 0; n < this.aNodes.length; n++) {
  364. if (this.aNodes[n].pid === node.id) {
  365. node._hc = true;
  366. }
  367. if (this.aNodes[n].pid === node.pid) {
  368. lastId = this.aNodes[n].id;
  369. }
  370. }
  371. if (lastId === node.id) {
  372. node._ls = true;
  373. }
  374. };
  375. /**
  376. * Returns the selected node as stored in cookie
  377. *
  378. * @returns {int} node id
  379. */
  380. dTree.prototype.getSelected = function () {
  381. let sn = this.getCookie('cs' + this.treeName);
  382. return (sn) ? parseInt(sn, 10) : null;
  383. };
  384. /**
  385. * Highlights the selected node
  386. *
  387. * @param {int} id node id
  388. */
  389. dTree.prototype.s = function (id) {
  390. let eOld, eNew, cn = this.aNodes[id];
  391. if (this.selectedNode !== id) {
  392. eNew = jQuery("#s" + this.treeName + id)[0];
  393. if (!eNew) {
  394. return;
  395. }
  396. if (this.selectedNode || this.selectedNode === 0) {
  397. eOld = jQuery("#s" + this.treeName + this.selectedNode)[0];
  398. eOld.className = "node";
  399. }
  400. eNew.className = "nodeSel";
  401. this.selectedNode = id;
  402. if (this.config.useCookies) {
  403. this.setCookie('cs' + this.treeName, cn.id);
  404. }
  405. }
  406. };
  407. /**
  408. * Toggle Open or close
  409. *
  410. * @param {int} id node id
  411. */
  412. dTree.prototype.o = function (id) {
  413. let cn = this.aNodes[id];
  414. this.nodeStatus(!cn._io, id, cn._ls);
  415. cn._io = !cn._io;
  416. if (this.config.useCookies) {
  417. this.updateCookie();
  418. }
  419. // scroll
  420. this.divdisplay('z', false);
  421. this.resizescroll("block");
  422. };
  423. /**
  424. * Open or close all nodes
  425. *
  426. * @param {boolean} status if true open
  427. */
  428. dTree.prototype.oAll = function (status) {
  429. for (let n = 0; n < this.aNodes.length; n++) {
  430. if (this.aNodes[n]._hc && this.aNodes[n].pid !== this.root.id) {
  431. this.nodeStatus(status, n, this.aNodes[n]._ls);
  432. this.aNodes[n]._io = status;
  433. }
  434. }
  435. if (this.config.useCookies) {
  436. this.updateCookie();
  437. }
  438. };
  439. /**
  440. * Opens the tree to a specific node
  441. *
  442. * @param {number} nId node id
  443. * @param {boolean} bSelect
  444. * @param {boolean} bFirst
  445. */
  446. dTree.prototype.openTo = function (nId, bSelect, bFirst) {
  447. let n, cn;
  448. if (!bFirst) {
  449. for (n = 0; n < this.aNodes.length; n++) {
  450. if (this.aNodes[n].id === nId) {
  451. nId = n;
  452. break;
  453. }
  454. }
  455. }
  456. this.fill(this.aNodes[nId].pid);
  457. cn = this.aNodes[nId];
  458. if (cn.pid === this.root.id || !cn._p) {
  459. return;
  460. }
  461. cn._io = 1;
  462. if (this.completed && cn._hc) {
  463. this.nodeStatus(true, cn._ai, cn._ls);
  464. }
  465. if (cn._is) {
  466. (this.completed) ? this.s(cn._ai) : this._sn = cn._ai;
  467. }
  468. this.openTo(cn._p._ai, false, true);
  469. };
  470. /**
  471. * Open the given nodes, if no node status is already stored
  472. *
  473. * @param {Array|string} nodes array of nodes to open or empty string to open all nodes
  474. */
  475. dTree.prototype.getOpenTo = function (nodes) {
  476. if (nodes === '') {
  477. this.openAll();
  478. } else if (!this.config.useCookies || !this.getCookie('co' + this.treeName)) {
  479. for (let n = 0; n < nodes.length; n++) {
  480. this.openTo(nodes[n], false, true);
  481. }
  482. }
  483. };
  484. /**
  485. * Change the status of a node(open or closed)
  486. *
  487. * @param {boolean} status true if open
  488. * @param {int} id node id
  489. * @param {boolean} bottom true if bottom node
  490. */
  491. dTree.prototype.nodeStatus = function (status, id, bottom) {
  492. if (status && !this.fill(id)) {
  493. return;
  494. }
  495. let eJoin, eIcon;
  496. eJoin = jQuery('#j' + this.treeName + id)[0];
  497. eIcon = jQuery('#i' + this.treeName + id)[0];
  498. eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
  499. eJoin.src = ((status) ? ((bottom) ? this.icon.minusBottom : this.icon.minus) : ((bottom) ? this.icon.plusBottom : this.icon.plus));
  500. jQuery('#d' + this.treeName + id)[0].style.display = (status) ? 'block' : 'none';
  501. };
  502. /**
  503. * [Cookie] Clears a cookie
  504. */
  505. dTree.prototype.clearCookie = function () {
  506. let now, yday;
  507. now = new Date();
  508. yday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
  509. this.setCookie('co' + this.treeName, 'cookieValue', yday);
  510. this.setCookie('cs' + this.treeName, 'cookieValue', yday);
  511. };
  512. /**
  513. * [Cookie] Sets value in a cookie
  514. *
  515. * @param {string} cookieName
  516. * @param {string} cookieValue
  517. * @param {boolean|Date} expires
  518. */
  519. dTree.prototype.setCookie = function (cookieName, cookieValue, expires = false) {
  520. document.cookie =
  521. encodeURIComponent(cookieName) + '=' + encodeURIComponent(cookieValue) +
  522. (expires ? '; expires=' + expires.toUTCString() : '') +
  523. '; path=' + DOKU_COOKIE_PARAM.path +
  524. '; secure=' + DOKU_COOKIE_PARAM.secure;
  525. };
  526. /**
  527. * [Cookie] Gets a value from a cookie
  528. *
  529. * @param cookieName
  530. * @returns {string}
  531. */
  532. dTree.prototype.getCookie = function (cookieName) {
  533. let cookieValue = '', pN, posValue, endPos;
  534. pN = document.cookie.indexOf(encodeURIComponent(cookieName) + '=');
  535. if (pN !== -1) {
  536. posValue = pN + (encodeURIComponent(cookieName) + '=').length;
  537. endPos = document.cookie.indexOf(';', posValue);
  538. if (endPos !== -1) {
  539. cookieValue = decodeURIComponent(document.cookie.substring(posValue, endPos));
  540. }
  541. else {
  542. cookieValue = decodeURIComponent(document.cookie.substring(posValue));
  543. }
  544. }
  545. return (cookieValue);
  546. };
  547. /**
  548. * [Cookie] Stores ids of open nodes as a string in cookie
  549. */
  550. dTree.prototype.updateCookie = function () {
  551. let str = '', n;
  552. for (n = 0; n < this.aNodes.length; n++) {
  553. if (this.aNodes[n]._io && this.aNodes[n].pid !== this.root.id) {
  554. if (str) {
  555. str += '.';
  556. }
  557. str += this.aNodes[n].id;
  558. }
  559. }
  560. this.setCookie('co' + this.treeName, str);
  561. };
  562. /**
  563. * [Cookie] Checks if a node id is in the cookie
  564. *
  565. * @param {int} id node id
  566. * @return {Boolean} if open true
  567. */
  568. dTree.prototype.isOpen = function (id) {
  569. let n, aOpen = this.getCookie('co' + this.treeName).split('.');
  570. for (n = 0; n < aOpen.length; n++) {
  571. if (parseInt(aOpen[n],10) === id) {
  572. return true;
  573. }
  574. }
  575. return false;
  576. };
  577. /**
  578. * Open the node of the current namespace
  579. *
  580. * @param {int} max
  581. */
  582. dTree.prototype.openCurNS = function (max) {
  583. let r, cn, match, t, i, n, cnsa, cna;
  584. let cns = this.pageid;
  585. r = new RegExp("\\b" + this.config.sepchar + "\\b", "g");
  586. match = cns.match(r) || -1;
  587. if (max > 0 && match.length >= max) {
  588. t = cns.split(this.config.sepchar);
  589. n = (this.aNodes[0].dokuid === '') ? 0 : this.aNodes[0].dokuid.split(this.config.sepchar).length;
  590. t.splice(max + n, t.length);
  591. cnsa = t.join(this.config.sepchar);
  592. }
  593. for (i = 0; i < this.aNodes.length; i++) {
  594. cn = this.aNodes[i];
  595. if (cns === cn.dokuid || cns === cn.hns) {
  596. this.openTo(cn.id, false, true);
  597. this.fajax = false;
  598. if (cn.pid >= 0) {
  599. jQuery(this.scroll("l", 4, cn.pid, 1));
  600. }
  601. break;
  602. }
  603. if (cnsa === cn.dokuid || cnsa === cn.hns) {
  604. cna = cn;
  605. this.fajax = true;
  606. }
  607. }
  608. if (cna) {
  609. this.openTo(cna.id, false, true);
  610. }
  611. };
  612. /**
  613. * Load children when not available
  614. *
  615. * @param {int} id node id
  616. * @returns {boolean}
  617. */
  618. dTree.prototype.fill = function (id) {
  619. if (id === -1 || this.aNodes[id]._ok) {
  620. return true;
  621. }
  622. let n = id, $eLoad, a, rd, ln, eDiv;
  623. if (this.aNodes[n].ajax) {
  624. //temporary load indicator
  625. $eLoad = jQuery('#l' + this.treeName);
  626. if (!$eLoad.length) {
  627. $eLoad = IndexmenuUtils.createPicker('l' + this.treeName, 'picker');
  628. }
  629. jQuery('#s' + this.treeName + n).parent().append($eLoad);
  630. $eLoad
  631. .html('Loading ...')
  632. .css({width: 'auto'})
  633. .show();
  634. //retrieves children
  635. this.getAjax(n);
  636. return true;
  637. }
  638. rd = [];
  639. while (!this.aNodes[n]._ok) {
  640. rd[rd.length] = n;
  641. n = this.aNodes[n].pid;
  642. }
  643. for (ln = rd.length - 1; ln >= 0; ln--) {
  644. id = rd[ln];
  645. a = this.aNodes[id];
  646. eDiv = jQuery('#d' + this.treeName + id)[0];
  647. if (!eDiv) {
  648. return false;
  649. }
  650. this.aIndent = [];
  651. n = a;
  652. while (n.pid >= 0) {
  653. if (n._ls) {
  654. this.aIndent.unshift(0);
  655. } else {
  656. this.aIndent.unshift(1);
  657. }
  658. n = n._p;
  659. }
  660. eDiv.innerHTML = this.addNode(a);
  661. a._ok = true;
  662. }
  663. return true;
  664. };
  665. /**
  666. * Open the nodes stored in cookie
  667. */
  668. dTree.prototype.openCookies = function () {
  669. let n, cn, aOpen = this.getCookie('co' + this.treeName).split('.');
  670. for (n = 0; n < aOpen.length; n++) {
  671. if (aOpen[n] === "") {
  672. break;
  673. }
  674. cn = this.aNodes[aOpen[n]];
  675. if (!cn._ok) {
  676. this.nodeStatus(true, aOpen[n], cn._ls);
  677. cn._io = true;
  678. }
  679. }
  680. };
  681. /**
  682. * Scrolls the index
  683. *
  684. * @param {string} where to move to
  685. * @param {int} s start
  686. * @param {int} n parent node id
  687. * @param {int} i
  688. */
  689. dTree.prototype.scroll = function (where, s, n, i) {
  690. if (!this.config.scroll) {
  691. return false;
  692. }
  693. let w, dtree, dtreel, nodeId;
  694. dtree = jQuery('#dtree_' + this.treeName)[0];
  695. dtreel = parseInt(dtree.offsetLeft);
  696. if (where === "r") {
  697. jQuery('#left_' + this.treeName)[0].style.border = "thin inset";
  698. this.scrollRight(dtreel, s);
  699. } else {
  700. nodeId = jQuery('#s' + this.treeName + n)[0];
  701. if (nodeId == null) {
  702. return false;
  703. }
  704. w = parseInt(dtree.parentNode.offsetWidth - nodeId.offsetWidth - nodeId.offsetLeft);
  705. if (this.config.toc) {
  706. w = w - 11;
  707. }
  708. if (dtreel <= w) {
  709. return;
  710. }
  711. this.resizescroll("none");
  712. this.stopscroll();
  713. this.scrollLeft(dtreel, s, w - 3, i);
  714. }
  715. };
  716. /**
  717. * Scroll index to the left
  718. *
  719. * @param {int} lft current position
  720. * @param {int} s start
  721. * @param {int} w width
  722. * @param {int} i
  723. */
  724. dTree.prototype.scrollLeft = function (lft, s, w, i) {
  725. if (lft < w - i - 10) {
  726. this.divdisplay('z', false);
  727. this.scrllTmr = 0;
  728. return;
  729. }
  730. var self = this;
  731. jQuery('#dtree_' + self.treeName)[0].style.left = lft + "px";
  732. this.scrllTmr = setTimeout(function () {
  733. self.scrollLeft(lft - s, s + i, w, i);
  734. }, 20);
  735. };
  736. /**
  737. * Scroll Index back to the right
  738. *
  739. * @param {int} lft current position
  740. * @param {int} s start
  741. */
  742. dTree.prototype.scrollRight = function (lft, s) {
  743. if (lft >= s) {
  744. this.divdisplay('left_', false);
  745. this.stopscroll();
  746. return;
  747. }
  748. var self = this;
  749. jQuery('#dtree_' + self.treeName)[0].style.left = lft + "px";
  750. if (lft > -15) {
  751. s = 1;
  752. }
  753. this.scrllTmr = setTimeout(function () {
  754. self.scrollRight(lft + s, s + 1);
  755. }, 20);
  756. };
  757. /**
  758. * Stop scroll movement
  759. */
  760. dTree.prototype.stopscroll = function () {
  761. jQuery('#left_' + this.treeName)[0].style.border = "none";
  762. clearTimeout(this.scrllTmr);
  763. this.scrllTmr = 0;
  764. };
  765. /**
  766. * Show features and add event handlers for ToC and scroll
  767. *
  768. * @param {int} n node id
  769. */
  770. dTree.prototype.show_feat = function (n) {
  771. var w, div, id, dtree, dtreel, self, node = jQuery('#s' + this.treeName + n)[0];
  772. self = this;
  773. if (this.config.toc && node.className !== "node") {
  774. div = jQuery('#t' + this.treeName)[0];
  775. id = (this.aNodes[n].hns) ? this.aNodes[n].hns : this.aNodes[n].dokuid;
  776. div.onmousedown = function () {
  777. IndexmenuContextmenu.createTocMenu('call=indexmenu&req=toc&id=' + decodeURIComponent(id), 'picker_' + self.treeName, 't' + self.treeName);
  778. };
  779. node.parentNode.appendChild(div);
  780. if (div.style.display === "none") {
  781. div.style.display = "inline";
  782. }
  783. }
  784. if (this.config.scroll) {
  785. div = jQuery('#z' + this.treeName)[0];
  786. div.onmouseover = function () {
  787. div.style.border = "none";
  788. self.scroll("l", 1, n, 0);
  789. };
  790. div.onmousedown = function () {
  791. div.style.border = "thin inset";
  792. self.scroll("l", 4, n, 1);
  793. };
  794. div.onmouseout = function () {
  795. div.style.border = "none";
  796. self.stopscroll();
  797. };
  798. div.onmouseup = div.onmouseover;
  799. dtree = jQuery('#dtree_' + this.treeName)[0];
  800. dtreel = parseInt(dtree.offsetLeft);
  801. w = parseInt(dtree.parentNode.offsetWidth - node.offsetWidth - node.offsetLeft + 1);
  802. if (dtreel > w) {
  803. div.style.display = "none";
  804. div.style.top = node.offsetTop + "px";
  805. div.style.left = parseInt(node.offsetLeft + node.offsetWidth + w - 12) + "px";
  806. div.style.display = "block";
  807. }
  808. }
  809. };
  810. /**
  811. * Show and resize the scroll-back button relatively to size of tree
  812. *
  813. * @param {string} status 'block' or 'none'
  814. */
  815. dTree.prototype.resizescroll = function (status) {
  816. let dtree, w, h, left = jQuery('#left_' + this.treeName)[0];
  817. if (!left) {
  818. return;
  819. }
  820. if (left.style.display === status) {
  821. dtree = jQuery('#dtree_' + this.treeName)[0];
  822. w = Math.trunc(dtree.offsetHeight / 3);
  823. h = Math.trunc(w / 50) * 50;
  824. if (h < 50) {
  825. h = 50;
  826. }
  827. left.style.height = h + "px";
  828. left.style.top = w + "px";
  829. if (status === "none") {
  830. left.style.display = "block";
  831. }
  832. }
  833. };
  834. /**
  835. * Toggle Open or close
  836. *
  837. * @param {int} n node id
  838. */
  839. dTree.prototype.getAjax = function (n) {
  840. var node, selft = this;
  841. let req, curns;
  842. node = selft.aNodes[n];
  843. req = 'req=index&idx=' + node.dokuid + decodeURIComponent(this.config.jsajax);
  844. curns = this.pageid.substring(0, this.pageid.lastIndexOf(this.config.sepchar));
  845. if (this.fajax) {
  846. req += '&nss=' + curns + '&max=1';
  847. }
  848. var onCompletion = function (data) {
  849. var i, ajxnodes, ajxnode, plus;
  850. plus = selft.aNodes.length - 1;
  851. eval(data);
  852. if (!ajxnodes instanceof Array || ajxnodes.length < 1) {
  853. ajxnodes = [
  854. ['', 1, 0, '', 0, 1, 0]
  855. ];
  856. }
  857. node.ajax = false;
  858. for (i = 0; i < ajxnodes.length; i++) {
  859. ajxnode = ajxnodes[i];
  860. ajxnode[2] = (ajxnode[2] == 0) ? node.id : ajxnode[2] + plus;
  861. ajxnode[1] += plus;
  862. selft.add(ajxnode[0], ajxnode[1], ajxnode[2], ajxnode[3], ajxnode[4], ajxnode[5], ajxnode[6]);
  863. }
  864. if (selft.fajax) {
  865. selft.fajax = false;
  866. selft.openCurNS(0);
  867. } else {
  868. selft.openTo(node.id, false, true);
  869. }
  870. jQuery('#l' + selft.treeName).hide();
  871. };
  872. jQuery.post(
  873. DOKU_BASE + 'lib/exe/ajax.php',
  874. 'call=indexmenu&'+req,
  875. onCompletion,
  876. 'html'
  877. );
  878. };
  879. /**
  880. * Load custom css for theme
  881. */
  882. dTree.prototype.loadCss = function () {
  883. let oLink = document.createElement("link");
  884. oLink.href = this.config.plugbase + '/images/' + this.config.theme + '/style.css';
  885. oLink.rel = "stylesheet";
  886. oLink.type = "text/css";
  887. document.getElementsByTagName('head')[0].appendChild(oLink);
  888. };
  889. /**
  890. * Show the contextmenu
  891. *
  892. * @param {int} n node id
  893. * @param {Event} e event
  894. * @returns {boolean}
  895. */
  896. dTree.prototype.contextmenu = function (n, e) {
  897. let type, node, cdtree, $rmenu;
  898. cdtree = jQuery("#cdtree_" + this.treeName)[0];
  899. $rmenu = jQuery('#r' + this.treeName)[0];
  900. if (!$rmenu) {
  901. return true;
  902. }
  903. IndexmenuContextmenu.mouseposition($rmenu, e);
  904. let cmenu = window.indexmenu_contextmenu;
  905. node = this.aNodes[n];
  906. $rmenu.innerHTML = '<div class="indexmenu_rmenuhead" title="' + node.name + '">' + node.name + "</div>";
  907. $rmenu.appendChild(document.createElement('ul'));
  908. type = (node.isdir || node._hc) ? 'ns' : 'pg';
  909. IndexmenuContextmenu.arrconcat(cmenu['all'][type], this, n);
  910. if (node.hns) {
  911. IndexmenuContextmenu.arrconcat(cmenu[type], this, n);
  912. type = 'pg';
  913. IndexmenuContextmenu.arrconcat(cmenu['all'][type], this, n);
  914. }
  915. IndexmenuContextmenu.arrconcat(cmenu[type], this, n);
  916. $rmenu.style.display = 'inline';
  917. return false;
  918. };
  919. /**
  920. * Show/hide object with given id of current indexmenu
  921. *
  922. * @param {string} objName name of object, which is combined with the unique id of the indexmenu
  923. * @param {boolean} visible true: visible, false: hide.
  924. */
  925. dTree.prototype.divdisplay = function (objName, visible) {
  926. let o = jQuery('#' + objName + this.treeName)[0];
  927. if (!o) {
  928. return;
  929. }
  930. (visible) ? o.style.display = 'inline' : o.style.display = 'none';
  931. };
  932. /**
  933. * Initialise the dTree index
  934. *
  935. * @param {int} hasstyle has an additional css style sheet
  936. * @param {int} nocookies use no cookies
  937. * @param {string} opennodes string of initial opened nodes
  938. * @param {int} nav is navbar option set
  939. * @param {int} max max level of available nodes (deeper levels are included with js)
  940. * @param {int} nomenu show no menu
  941. */
  942. dTree.prototype.init = function (hasstyle, nocookies, opennodes, nav, max, nomenu) {
  943. if (hasstyle) {
  944. this.loadCss();
  945. }
  946. if (!nocookies) {
  947. this.openCookies();
  948. }
  949. //open given nodes
  950. if (opennodes) {
  951. this.getOpenTo(opennodes.split(" "));
  952. }
  953. if (nav) {
  954. this.openCurNS(max);
  955. }
  956. //create contextmenu
  957. if (!nomenu) {
  958. var self = this;
  959. IndexmenuUtils.createPicker('r' + this.treeName, 'indexmenu_rmenu ' + this.config.theme);
  960. jQuery('#r' + this.treeName)[0].oncontextmenu = IndexmenuContextmenu.stopevt;
  961. jQuery(document).on("click",function() {
  962. self.divdisplay('r', false);
  963. });
  964. }
  965. };