Viewport.js
2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Controller: Viewport
*
*/
Ext.define('MyMA.controller.Viewport', {
extend: 'Ext.app.Controller',
views: ['Viewport', 'Authorize'],
refs: [{
selector: 'viewport',
ref: 'appView'
}, {
selector: 'authorize',
ref: 'authPanel'
}, {
selector: 'authorize > form',
ref: 'authForm'
}],
/**
*
*/
isGuest: Ext.undefined,
/**
* A template method that is called when your application boots.
* It is called before the Application's launch function is executed so gives a hook
* point to run any code before your Viewport is created.
*/
init: function(app) {
// Bind Guest to the Ext.data.Connection
Ext.override(Ext.data.Connection, {
/**
* Set guest state to the authorization controller
*/
setGuest: Ext.Function.bind(this.setGuest, this),
/**
* Return isGuest current value
* @return {Boolean}
*/
getGuest: Ext.Function.bind(this.getGuest, this)
});
this.control({
'authorize > form > toolbar > button': {
click: this.Authorize
}
});
this.Authorize();
},
/**
* Authorize
* if passed main application view, the first step need to query
* @param object, main application view
*/
Authorize: function() {
if (this.getAuthPanel()) {
var form = this.getAuthPanel().down('form').getForm();
if(form.isValid()) {
form.submit({
url: Ext.Ajax.getRestUrl('api','login', 'authorize', 0),
method: 'PUT',
clientValidation: true,
scope: this,
success: this.successLogin
})
}
}
else {
Ext.Ajax.request({
url: Ext.Ajax.getRestUrl('api', 'login'),
scope: this,
callback: function(){
if (!this.isGuest && !this.getAppView()) {
this.setGuest(false);
this.getView('Viewport').create();
}
}
});
}
},
/**
* Return isGuest value
* @param state
*/
getGuest: function() {
return this.isGuest;
},
/**
* Set isGuest flag value
* @param object, this controller
* @param boolean, state to set
*/
setGuest: function(state) {
this.isGuest = Ext.isBoolean(state) ? state : true;
if(this.isGuest && !this.getAuthPanel()) {
this.getView('Authorize').create();
}
},
/**
* private
*/
successLogin: function(form, action) {
this.getAuthPanel().close();
if(!this.getAppView()) {
this.getView('Viewport').create();
}
},
/**
* Logout
* public
*/
Logout: function() {
Ext.Ajax.request({
url: Ext.Ajax.getRestUrl('api','login', 'logout', 0),
method: 'PUT',
scope: this,
callback: function() {
if(this.getAppView()) {
this.getController('Taskpanel').closeAll();
this.getAppView().destroy();
}
this.setGuest(this, true);
}
});
}
});