]> git.proxmox.com Git - pmg-gui.git/blame - js/MainView.js
MainView: make UserName a button/menu
[pmg-gui.git] / js / MainView.js
CommitLineData
2c1d504e 1/*global Proxmox*/
9d91eeb0
DC
2Ext.define('PMG.MainView', {
3 extend: 'Ext.container.Container',
4 xtype: 'mainview',
5
6 title: 'Proxmox Mail Gateway',
7
8 controller: {
9 xclass: 'Ext.app.ViewController',
10 routes: {
11 ':path:subpath': {
12 action: 'changePath',
13 before: 'beforeChangePath',
14 conditions : {
6355fc2c
DC
15 ':path' : '(?:([%a-zA-Z0-9\\-\\_\\s,]+))',
16 ':subpath' : '(?:(?::)([%a-zA-Z0-9\\-\\_\\s,]+))?'
9d91eeb0
DC
17 }
18 }
19 },
20
21 beforeChangePath: function(path, subpath, action) {
22 var me = this;
23
24 if (!Ext.ClassManager.getByAlias('widget.'+ path)) {
25 console.warn('xtype "'+path+'" not found');
26 action.stop();
27 return;
28 }
29
30 var lastpanel = me.lookupReference('contentpanel').getLayout().getActiveItem();
31 if (lastpanel && lastpanel.xtype === path) {
32 // we have the right component already,
33 // we just need to select the correct tab
34 // default to the first
35 subpath = subpath || 0;
36 if (lastpanel.getActiveTab) {
37 // we assume lastpanel is a tabpanel
2468e1a1 38 if (lastpanel.getActiveTab().getItemId() !== subpath) {
9d91eeb0
DC
39 // set the active tab
40 lastpanel.setActiveTab(subpath);
41 }
2468e1a1 42 // else we are already there
9d91eeb0
DC
43 }
44 action.stop();
45 return;
46 }
47
48 action.resume();
49 },
50
51 changePath: function(path,subpath) {
52 var me = this;
53 var contentpanel = me.lookupReference('contentpanel');
54 var lastpanel = contentpanel.getLayout().getActiveItem();
55
56 var obj = contentpanel.add({ xtype: path });
57 var treelist = me.lookupReference('navtree');
58
59 treelist.suspendEvents();
60 treelist.select(path);
61 treelist.resumeEvents();
62
63 if (Ext.isFunction(obj.setActiveTab)) {
64 obj.setActiveTab(subpath || 0);
65 obj.addListener('tabchange', function(tabpanel, newc, oldc) {
66 var newpath = path;
67
68 // only add the subpath part for the
69 // non-default tabs
70 if (tabpanel.items.findIndex('id', newc.id) !== 0) {
71 newpath += ":" + newc.getItemId();
72 }
73
74 me.redirectTo(newpath);
75 });
76 }
77
78 contentpanel.setActiveItem(obj);
79
80 if (lastpanel) {
81 contentpanel.remove(lastpanel, { destroy: true });
82 }
83
84 },
85
86 logout: function() {
99bba12c 87 PMG.app.logout();
9d91eeb0
DC
88 },
89
90 navigate: function(treelist, item) {
91 this.redirectTo(item.get('path'));
92 },
93
94 control: {
6886c8b4 95 '[reference=logoutButton]': {
9d91eeb0
DC
96 click: 'logout'
97 }
98 },
99
100 init: function(view) {
101 var me = this;
102
103 // load username
6886c8b4 104 me.lookupReference('usernameinfo').setText(Proxmox.UserName);
9d91eeb0
DC
105
106 // show login on requestexception
107 // fixme: what about other errors
108 Ext.Ajax.on('requestexception', function(conn, response, options) {
109 if (response.status == 401) { // auth failure
110 me.logout();
111 }
112 });
113
114 // get ticket periodically
115 Ext.TaskManager.start({
116 run: function() {
117 var ticket = Proxmox.Utils.authOK();
118 if (!ticket || !Proxmox.UserName) {
119 return;
120 }
121
122 Ext.Ajax.request({
123 params: {
124 username: Proxmox.UserName,
125 password: ticket
126 },
127 url: '/api2/json/access/ticket',
128 method: 'POST',
129 failure: function() {
130 me.logout();
131 },
132 success: function(response, opts) {
133 var obj = Ext.decode(response.responseText);
134 PMG.Utils.updateLoginData(obj.data);
135 }
136 });
137 },
138 interval: 15*60*1000
139 });
140
141 // select treeitem and load page from url fragment
033228c0 142 var token = Ext.util.History.getToken() || 'pmgDashboard';
9d91eeb0
DC
143 this.redirectTo(token, true);
144
145 }
146 },
147
148 plugins: 'viewport',
149
ea07c9aa 150 layout: { type: 'border' },
9d91eeb0
DC
151
152 items: [
153 {
154 region: 'north',
155 xtype: 'container',
156 layout: {
157 type: 'hbox',
158 align: 'middle'
159 },
c45e23e4
DC
160 margin: '2 5 2 5',
161 height: 38,
9d91eeb0
DC
162 items: [
163 {
164 xtype: 'proxmoxlogo'
165 },
166 {
167 xtype: 'versioninfo'
168 },
169 {
170 flex: 1
171 },
6a3d1c51
DM
172 {
173 xtype: 'proxmoxHelpButton',
0c503218 174 text: gettext('Documentation'),
6a3d1c51
DM
175 hidden: false,
176 baseCls: 'x-btn',
177 iconCls: 'fa fa-info-circle x-btn-icon-el-default-toolbar-small ',
c45e23e4 178 margin: '0 5 0 0',
6a3d1c51
DM
179 listenToGlobalEvent: false,
180 onlineHelp: 'pmg_documentation_index'
181 },
9d91eeb0 182 {
9d91eeb0 183 xtype: 'button',
6886c8b4
DC
184 reference: 'usernameinfo',
185 style: {
186 // proxmox dark grey p light grey as border
187 backgroundColor: '#464d4d',
188 borderColor: '#ABBABA'
189 },
190 margin: '0 5 0 0',
191 iconCls: 'fa fa-user',
192 menu: [
193 {
194 reference: 'logoutButton',
195 iconCls: 'fa fa-sign-out',
196 text: gettext('Logout')
197 },
198 ],
199 },
9d91eeb0
DC
200 ]
201 },
202 {
2c866ec9
DC
203 xtype: 'panel',
204 scrollable: 'y',
9d91eeb0
DC
205 border: false,
206 region: 'west',
2c866ec9
DC
207 layout: {
208 type: 'vbox',
209 align: 'stretch'
210 },
211 items: [{
212 xtype: 'navigationtree',
213 minWidth: 180,
214 reference: 'navtree',
215 // we have to define it here until extjs 6.2
216 // because of a bug where a viewcontroller does not detect
217 // the selectionchange event of a treelist
218 listeners: {
219 selectionchange: 'navigate'
220 }
221 }, {
222 xtype: 'box',
223 cls: 'x-treelist-nav',
224 flex: 1
225 }]
9d91eeb0
DC
226 },
227 {
228 xtype: 'panel',
ea07c9aa 229 layout: { type: 'card' },
9d91eeb0
DC
230 region: 'center',
231 border: false,
749af060 232 reference: 'contentpanel'
9d91eeb0
DC
233 }
234 ]
235});