source: ether_megapoli/trunk/web/backoffice/user-script.jsp @ 281

Last change on this file since 281 was 281, checked in by vmipsl, 13 years ago

Login

File size: 13.8 KB
Line 
1<script type="text/javascript">
2var interfaceBOUser = Class.create( {
3
4    initialize: function( jsonWaitingUsers, jsonUsers, jsonUserStates, jsonUserRoles )
5    {
6        // Values
7        this.jsonUsers = jsonUsers || null;
8        this.jsonWaitingUsers = jsonWaitingUsers || null;
9        this.jsonUserStates = jsonUserStates || null;
10        this.jsonUserRoles = jsonUserRoles || null;
11        this.user = false;
12
13        // Containers
14        this.generalContainerUsers = $( "#generalContainerUsers" );
15        this.generalContainerWaitingUsers = $( "#generalContainerWaitingUsers" );
16        this.containerUsers = $( "#containerUsers" );
17        this.containerWaitingUsers = $( "#containerWaitingUsers" );
18        this.containerRoles = $( "#containerRoles" );
19        this.containerStates = $( "#containerStates" );
20        this.containerAddOrModifyTitle = $( "#addOrModifyTitle" );
21        this.containerErrors = $( "#errors" );
22
23        /** *********** LOADING *********** **/
24        // Create loading object for waiting users
25        var param = new Object();
26        param.id = "loadingForWaitingUser";
27        param.parent = this.generalContainerWaitingUsers;
28        this.loadingWaitingUser = new Loading( param );
29        this.loadingWaitingUser.display();
30
31        // Create loading object for users
32        var param = new Object();
33        param.id = "loadingForUser";
34        param.parent = this.generalContainerUsers;
35        this.loadingUser = new Loading( param );
36        this.loadingUser.display();
37
38        this.containerErrors.hide();
39        this.updateAddUserButtonAndTitle();
40        this.displayWaitingUsers();
41        this.displayUsers();
42        this.displayStates();
43        this.displayRoles();
44
45        $( "#keepSamePassword" ).attr( 'disabled', true );
46        $( "#keepSamePassword" ).bind( 'click', this.onCheckKeepSamePassword );
47    },
48
49    // REQUESTS ********************************************************
50    requestAddUser: function()
51    {
52        if( !this.verifyFields() )
53        {
54            var parametersUrl = "name=" + $( "#lastName" ).val() + "&firstName=" + $( "#firstName" ).val() + "&email=" + $( "#email" ).val() +
55                    "&pwd=" + $( "#password" ).val() + "&role=" + this.selectRoles.getValue() + "&state=" + this.selectStates.getValue() + "&hasAccess=" + $( "#boAccess" ).attr( 'checked' );
56            var request = $.ajax( {
57                url: "backoffice?methodName=addUser&" + parametersUrl,
58                success:jQuery.proxy( this.handleUser, this ),
59                error: jQuery.proxy( this.showErrors, [this] )
60            } );
61        }
62    },
63
64    requestRemoveUser: function()
65    {
66        if( this.user )
67            $.ajax( {
68                url: "backoffice?methodName=removeUser&id=" + this.user.id,
69                success:jQuery.proxy( this.handleUser, this )
70            } );
71    },
72
73    requestModifyUser: function()
74    {
75        if( this.user && !this.verifyFields() )
76        {
77            var parametersUrl = "id=" + this.user.id + "&name=" + $( "#lastName" ).val() + "&firstName=" + $( "#firstName" ).val() + "&email=" + $( "#email" ).val() +
78                    "&pwd=" + $( "#password" ).val() + "&role=" + this.selectRoles.getValue() + "&state=" + this.selectStates.getValue() +
79                    "&hasAccess=" + $( "#boAccess" ).attr( 'checked' ) + "&keepSamePassword=" + $( "#keepSamePassword" ).attr( 'checked' );
80            $.ajax( {
81                url: "backoffice?methodName=modifyUser&" + parametersUrl,
82                success:jQuery.proxy( this.handleUser, this ),
83                error: jQuery.proxy( this.showErrors, [this] )
84            } );
85        }
86    },
87
88    requestAcceptUser: function( waitingUser )
89    {
90        this.containerErrors.hide();
91
92        if( waitingUser )
93            $.ajax( {
94                url: "backoffice?methodName=acceptOrRefuseUser&id=" + waitingUser.id + "&ok=true",
95                success: jQuery.proxy( this.handleUser, this ),
96                error: jQuery.proxy( this.showErrors, [this, waitingUser.email ] )
97            } );
98    },
99
100    requestRefuseUser: function( waitingUser )
101    {
102        this.containerErrors.hide();
103
104        if( waitingUser )
105            $.ajax( {
106                url: "backoffice?methodName=acceptOrRefuseUser&id=" + waitingUser.id + "&ok=false",
107                success: jQuery.proxy( this.handleUser, this ),
108                error: jQuery.proxy( this.showErrors, [this, waitingUser.email ] )
109            } );
110    },
111
112    // HANDLES ********************************************************
113    handleUser: function( result )
114    {
115        this.jsonUsers = jQuery.parseJSON( result ).jSonUsers;
116        this.jsonWaitingUsers = jQuery.parseJSON( result ).jSonWaitingUsers;
117        this.displayWaitingUsers();
118        this.displayUsers();
119        this.clearAddOrModifyUserFields();
120        this.updateAddUserButtonAndTitle();
121    },
122
123    // DISPLAYS ********************************************************
124    displayTRForUser: function( user )
125    {
126        var tr = $( document.createElement( "tr" ) );
127        var tdId = $( document.createElement( "td" ) );
128        tdId.html( user.id );
129        tr.append( tdId );
130        var tdLastName = $( document.createElement( "td" ) );
131        tdLastName.html( user.lastName );
132        tr.append( tdLastName );
133        var tdFirstName = $( document.createElement( "td" ) );
134        tdFirstName.html( user.firstName );
135        tr.append( tdFirstName );
136        var tdEmail = $( document.createElement( "td" ) );
137        tdEmail.html( user.email );
138        tr.append( tdEmail );
139        var tdRole = $( document.createElement( "td" ) );
140        tdRole.html( user.role );
141        tr.append( tdRole );
142
143        var tdDate = $( document.createElement( "td" ) );
144        tdDate.html( formatDate( new Date( user.creationDate.time ) ) );
145        tr.append( tdDate );
146        return tr;
147    },
148
149    displayWaitingUsers: function()
150    {
151        this.containerWaitingUsers.empty();
152
153        if( this.jsonWaitingUsers && 0 < this.jsonWaitingUsers.length )
154        {
155            jQuery.each( this.jsonWaitingUsers, jQuery.proxy( function( i, waitingUser )
156            {
157                var tr = this.displayTRForUser( waitingUser );
158
159                // Buttons
160                var tdAccept = $( document.createElement( "td" ) );
161                var buttonAccept = new Button( {value:interfaceTexts["bo.accept"], parent:tdAccept, id:"button_accept", className: "small positive action_button", contextToSave: waitingUser, onClick:jQuery.proxy( this.requestAcceptUser, this )} );
162                tr.append( tdAccept );
163
164                var tdRefuse = $( document.createElement( "td" ) );
165                var buttonRefuse = new Button( {value:interfaceTexts["bo.refuse"], parent:tdRefuse, id:"button_refuse", className: "small negative action_button", contextToSave: waitingUser, onClick:jQuery.proxy( this.requestRefuseUser, this )} );
166                tr.append( tdRefuse );
167
168                this.containerWaitingUsers.append( tr );
169
170            }, this ) );
171        }
172        else
173        {
174            var tr = $( document.createElement( "tr" ) );
175            var td = $( document.createElement( "td" ) );
176            td.attr( {colspan:"11"} );
177            td.html( "<center>" + interfaceTexts["bo.noUser"] + "</center>" );
178            tr.append( td );
179            this.containerWaitingUsers.append( tr );
180        }
181
182        this.loadingWaitingUser.hide();
183    },
184
185    displayUsers: function()
186    {
187        this.containerUsers.empty();
188
189        if( this.jsonUsers && 0 < this.jsonUsers.length )
190        {
191            jQuery.each( this.jsonUsers, jQuery.proxy( function( i, user )
192            {
193                var tr = this.displayTRForUser( user );
194
195                var tdState = $( document.createElement( "td" ) );
196                tdState.html( user.state );
197                tr.append( tdState );
198                var tdBO = $( document.createElement( "td" ) );
199                tdBO.html( user.accessToBO.toString() );
200                tr.append( tdBO );
201
202                // Buttons
203                var tdModify = $( document.createElement( "td" ) );
204                var buttonModify = new Button( {value:interfaceTexts["bo.modify"], parent:tdModify, id:"button_modify", className: "small positive action_button", contextToSave: user, onClick:jQuery.proxy( this.onClickModify, this )} );
205                tr.append( tdModify );
206
207                var tdRemove = $( document.createElement( "td" ) );
208                var buttonRemove = new Button( {value:interfaceTexts["bo.remove"], parent:tdRemove, id:"button_remove", className: "small negative action_button", contextToSave: user, onClick:jQuery.proxy( this.onClickRemove, this )} );
209                tr.append( tdRemove );
210
211                this.containerUsers.append( tr );
212
213            }, this ) );
214        }
215        else
216        {
217            var tr = $( document.createElement( "tr" ) );
218            var td = $( document.createElement( "td" ) );
219            td.attr( {colspan:"11"} );
220            td.html( "<center><BR/>" + interfaceTexts["bo.noUser"] + "<BR/><BR/></center>" );
221            tr.append( td );
222            this.containerUsers.append( tr );
223        }
224
225        this.loadingUser.hide();
226    },
227
228    displayStates: function()
229    {
230        var paramSelect = new Object();
231        paramSelect.id = "select_states";
232        paramSelect.parent = this.containerStates;
233        this.selectStates = new Select( paramSelect );
234        jQuery.each( this.jsonUserStates, jQuery.proxy( function ( i, jsonUserState )
235        {
236            this.selectStates.add( jsonUserState.value, interfaceTexts[jsonUserState.text] );
237        }, this ) );
238        this.selectStates.selectFirst( false );
239    },
240
241    displayRoles: function()
242    {
243        var paramSelect = new Object();
244        paramSelect.id = "select_roles";
245        paramSelect.parent = this.containerRoles;
246        this.selectRoles = new Select( paramSelect );
247        jQuery.each( this.jsonUserRoles, jQuery.proxy( function ( i, jsonUserRole )
248        {
249            this.selectRoles.add( jsonUserRole.value, interfaceTexts[jsonUserRole.text] );
250        }, this ) );
251        this.selectRoles.selectFirst( false );
252    },
253
254    // EVENTS ********************************************************
255    onClickModify: function( user )
256    {
257        this.user = user;
258        $( "#lastName" ).val( user.lastName );
259        $( "#firstName" ).val( user.firstName );
260        $( "#email" ).val( user.email );
261        $( "#password" ).attr( 'disabled', true );
262        $( "#keepSamePassword" ).attr( 'disabled', false );
263        $( "#keepSamePassword" ).attr( 'checked', true );
264        this.selectRoles.select( user.role, false );
265        this.selectStates.select( user.state, false );
266        $( "#boAccess" ).attr( 'checked', user.accessToBO );
267
268        this.updateModifyUserButtonAndTitle( user.id );
269    },
270
271    onClickRemove: function( user )
272    {
273        this.user = user;
274        if( window.confirm( interfaceTexts["bo.user.remove.confirm"] + " " + user.firstName + " " + user.lastName + " ?" ) )
275            jQuery.proxy( this.requestRemoveUser(), this );
276    },
277
278    onCheckKeepSamePassword: function()
279    {
280        var actualState = $( "#keepSamePassword" ).attr( 'checked' );
281        $( "#password" ).attr( 'disabled', actualState );
282        if( actualState )
283            $( "#password" ).val( '' );
284    },
285
286    // OTHERS ********************************************************
287    updateAddUserButtonAndTitle: function()
288    {
289        this.containerAddOrModifyTitle.empty();
290        this.containerAddOrModifyTitle.html( interfaceTexts["bo.user.add"] + " :<BR/><BR/>" );
291        $( "#add_or_modify_user" ).unbind( 'click' );
292        $( "#add_or_modify_user" ).bind( 'click', this, jQuery.proxy( this.requestAddUser, this ) );
293        $( "#add_or_modify_user" ).html( interfaceTexts["bo.add"] );
294        $( "#password" ).attr( 'disabled', false );
295        $( "#keepSamePassword" ).attr( 'disabled', true );
296    },
297
298    updateModifyUserButtonAndTitle: function( userId )
299    {
300        this.containerAddOrModifyTitle.empty();
301        this.containerAddOrModifyTitle.html( interfaceTexts["bo.user.modify"] + " (id : " + userId + ")<BR/><BR/>" );
302        $( "#add_or_modify_user" ).unbind( 'click' );
303        $( "#add_or_modify_user" ).bind( 'click', this, jQuery.proxy( this.requestModifyUser, this ) );
304        $( "#add_or_modify_user" ).html( interfaceTexts["bo.modify"] );
305        $( "#password" ).attr( 'disabled', true );
306        $( "#keepSamePassword" ).attr( 'disabled', false );
307    },
308
309    clearAddOrModifyUserFields: function()
310    {
311        $( ':input', '#addOrModifyForm' )
312                .not( ':button, :submit, :reset, :hidden' )
313                .val( '' )
314                .removeAttr( 'checked' )
315                .removeAttr( 'selected' );
316
317        this.selectRoles.selectFirst( false );
318        this.selectStates.selectFirst( false );
319    },
320
321    verifyFields: function()
322    {
323        this.containerErrors.hide();
324
325        var errors = false;
326        if( '' == $( "#lastName" ).val() )
327            errors = interfaceTexts["bo.field.lastName"];
328        if( '' == $( "#email" ).val() )
329            errors = !errors ? interfaceTexts["bo.field.email"] : errors + "<BR/>" + interfaceTexts["bo.field.email"];
330        if( '' == $( "#password" ).val() && !$( "#keepSamePassword" ).attr( 'checked' ) )
331            errors = !errors ? interfaceTexts["bo.field.password"] : errors + "<BR/>" + interfaceTexts["bo.field.password"];
332
333        if( errors )
334        {
335            this.containerErrors.show();
336            this.containerErrors.html( errors );
337        }
338        return errors;
339    },
340
341    showErrors: function( result )
342    {
343        var context = this[0];
344        var text = this[1];
345        context.containerErrors.show();
346        if( text )
347            context.containerErrors.html( interfaceTexts[result.responseText] + " " + text );
348        else
349            context.containerErrors.html( interfaceTexts[result.responseText] );
350    }
351
352} );
353
354
355</script>
Note: See TracBrowser for help on using the repository browser.