3d744c20993dcebc8f36933044b55aaefe5fca97
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / extjs / components / pagetree / javascript / Ext.ux.state.TreePanel.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13 Ext.ns('Ext.ux.state');
14
15 // dummy constructor
16 Ext.ux.state.TreePanel = function() {};
17
18 /**
19 * State Provider for a tree panel
20 */
21 Ext.override(Ext.ux.state.TreePanel, {
22 /**
23 * Initializes the plugin
24 * @param {Ext.tree.TreePanel} tree
25 * @private
26 */
27 init:function(tree) {
28 tree.lastSelectedNode = null;
29 tree.isRestoringState = false;
30 tree.stateHash = {};
31
32 // install event handlers on TreePanel
33 tree.on({
34 // add path of expanded node to stateHash
35 beforeexpandnode:function(node) {
36 if (this.isRestoringState) {
37 return;
38 }
39
40 var saveID = (node.id === 'root' ? node.id : node.id.substr(1));
41 this.stateHash[saveID] = 1;
42 },
43
44 // delete path and all subpaths of collapsed node from stateHash
45 beforecollapsenode:function(node) {
46 if (this.isRestoringState) {
47 return;
48 }
49
50 var deleteID = (node.id === 'root' ? node.id : node.id.substr(1));
51 delete this.stateHash[deleteID];
52 },
53
54 beforeclick: function(node) {
55 if (this.isRestoringState) {
56 return;
57 }
58 this.stateHash['lastSelectedNode'] = node.id;
59 }
60 });
61
62 // update state on node expand or collapse
63 tree.stateEvents = tree.stateEvents || [];
64 tree.stateEvents.push('expandnode', 'collapsenode', 'click');
65
66 // add state related props to the tree
67 Ext.apply(tree, {
68 // keeps expanded nodes paths keyed by node.ids
69 stateHash:{},
70
71 restoreState: function() {
72 this.isRestoringState = true;
73 // get last selected node
74 for (var pageID in this.stateHash) {
75 var pageNode = this.getNodeById((pageID !== 'root' ? 'p' : '') + pageID);
76 if (pageNode) {
77 pageNode.on({
78 expand: {
79 single:true,
80 scope:this,
81 fn: this.restoreState
82 }
83 });
84 if (pageNode.expanded === false && pageNode.rendered == true) {
85 pageNode.expand();
86 }
87 }
88 }
89
90 if (this.stateHash['lastSelectedNode']) {
91 var node = this.getNodeById(this.stateHash['lastSelectedNode']);
92 if (node) {
93 var contentId = TYPO3.Backend.ContentContainer.getIdFromUrl() ||
94 String(fsMod.recentIds['web']) || '-1';
95
96 var hasContentFrameValidPageId = (contentId !== '-1' && contentId !== '0');
97 var isCurrentSelectedNode = (
98 String(node.attributes.nodeData.id) === contentId ||
99 contentId.indexOf('pages' + String(node.attributes.nodeData.id)) !== -1
100 );
101
102 if (isCurrentSelectedNode) {
103 this.selectPath(node.getPath());
104 }
105
106 var isSingleClickPossible = (this.app.isVisible() && this.commandProvider && this.commandProvider.singleClick);
107 if (!hasContentFrameValidPageId && !isCurrentSelectedNode && isSingleClickPossible) {
108 this.selectPath(node.getPath());
109 this.commandProvider.singleClick(node, this);
110 }
111 }
112 }
113
114 this.isRestoringState = false;
115 },
116
117 // apply state on tree initialization
118 applyState:function(state) {
119 if(state) {
120 Ext.apply(this, state);
121
122 // it is too early to expand paths here
123 // so do it once on root load
124 this.root.on({
125 load: {
126 single:true,
127 scope:this,
128 fn: this.restoreState
129 }
130 });
131 }
132 },
133
134 // returns stateHash for save by state manager
135 getState:function() {
136 return {stateHash:this.stateHash};
137 }
138 });
139 }
140 });