]> git.proxmox.com Git - pmg-gui.git/blame - js/MainView.js
improve gettext usage
[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: {
95 'button[reference=logoutButton]': {
96 click: 'logout'
97 }
98 },
99
100 init: function(view) {
101 var me = this;
102
103 // load username
104 me.lookupReference('usernameinfo').update({username:Proxmox.UserName});
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 },
172 {
173 baseCls: 'x-plain',
174 reference: 'usernameinfo',
175 padding: '0 5',
64fb657f 176 tpl: Ext.String.format(gettext("You are logged in as {0}"), "'{username}'")
9d91eeb0 177 },
6a3d1c51
DM
178 {
179 xtype: 'proxmoxHelpButton',
180 hidden: false,
181 baseCls: 'x-btn',
182 iconCls: 'fa fa-info-circle x-btn-icon-el-default-toolbar-small ',
c45e23e4 183 margin: '0 5 0 0',
6a3d1c51
DM
184 listenToGlobalEvent: false,
185 onlineHelp: 'pmg_documentation_index'
186 },
9d91eeb0
DC
187 {
188 reference: 'logoutButton',
189 xtype: 'button',
190 iconCls: 'fa fa-sign-out',
749af060 191 text: gettext('Logout')
9d91eeb0
DC
192 }
193 ]
194 },
195 {
2c866ec9
DC
196 xtype: 'panel',
197 scrollable: 'y',
9d91eeb0
DC
198 border: false,
199 region: 'west',
2c866ec9
DC
200 layout: {
201 type: 'vbox',
202 align: 'stretch'
203 },
204 items: [{
205 xtype: 'navigationtree',
206 minWidth: 180,
207 reference: 'navtree',
208 // we have to define it here until extjs 6.2
209 // because of a bug where a viewcontroller does not detect
210 // the selectionchange event of a treelist
211 listeners: {
212 selectionchange: 'navigate'
213 }
214 }, {
215 xtype: 'box',
216 cls: 'x-treelist-nav',
217 flex: 1
218 }]
9d91eeb0
DC
219 },
220 {
221 xtype: 'panel',
ea07c9aa 222 layout: { type: 'card' },
9d91eeb0
DC
223 region: 'center',
224 border: false,
749af060 225 reference: 'contentpanel'
9d91eeb0
DC
226 }
227 ]
228});