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

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

BO pagination

File size: 18.8 KB
Line 
1<script type="text/javascript">
2var interfaceBOUser = Class.create( {
3
4    initialize: function( nbUsers, 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        this.nbUsers = nbUsers || false;
13
14        // Containers
15        this.generalContainerUsers = $( "#generalContainerUsers" );
16        this.generalContainerWaitingUsers = $( "#generalContainerWaitingUsers" );
17        this.containerUsers = $( "#containerUsers" );
18        this.containerWaitingUsers = $( "#containerWaitingUsers" );
19        this.containerRoles = $( "#containerRoles" );
20        this.containerStates = $( "#containerStates" );
21        this.containerAddOrModifyTitle = $( "#addOrModifyTitle" );
22        this.containerErrors = $( "#errors" );
23        this.containerUsersNumber = $( "#nbResults" );
24
25        this.containerSorts = $( "#containerSorts" );
26        this.containerSortsRoles = $( "#containerSorts_roles" );
27        this.containerSortsStates = $( "#containerSorts_states" );
28        this.containerSortsDisplay = $( "#containerSorts_display" );
29        this.containerPage = $( "#page" );
30        this.containerMaxPage = $( "#maxPages" );
31
32        /** *********** LOADING *********** **/
33        // Create loading object for waiting users
34        var param = new Object();
35        param.id = "loadingForWaitingUser";
36        param.parent = this.generalContainerWaitingUsers;
37        this.loadingWaitingUser = new Loading( param );
38        this.loadingWaitingUser.display();
39
40        // Create loading object for users
41        var param = new Object();
42        param.id = "loadingForUser";
43        param.parent = this.generalContainerUsers;
44        this.loadingUser = new Loading( param );
45        this.loadingUser.display();
46
47        this.containerErrors.hide();
48        this.updateAddUserButtonAndTitle();
49        this.displayWaitingUsers();
50        this.displaySorts();
51        this.managePartAddOrModify();
52        this.managePagination();
53        this.displayUsers();
54    },
55
56    // REQUESTS ********************************************************
57    requestAddUser: function()
58    {
59        if( !this.verifyFields() )
60        {
61            var parametersUrl = "name=" + $( "#lastName" ).val() + "&firstName=" + $( "#firstName" ).val() + "&email=" + $( "#email" ).val() +
62                    "&pwd=" + $( "#password" ).val() + "&role=" + this.selectRoles.getValue() + "&state=" + this.selectStates.getValue() + "&hasAccess=" + $( "#boAccess" ).attr( 'checked' );
63            var request = $.ajax( {
64                url: "backoffice?methodName=addUser&" + parametersUrl,
65                success:jQuery.proxy( this.handleUser, this ),
66                error: jQuery.proxy( this.showErrors, [this] )
67            } );
68        }
69    },
70
71    requestRemoveUser: function()
72    {
73        if( this.user )
74            $.ajax( {
75                url: "backoffice?methodName=removeUser&id=" + this.user.id,
76                success:jQuery.proxy( this.handleUser, this )
77            } );
78    },
79
80    requestModifyUser: function()
81    {
82        if( this.user && !this.verifyFields() )
83        {
84            var parametersUrl = "id=" + this.user.id + "&name=" + $( "#lastName" ).val() + "&firstName=" + $( "#firstName" ).val() + "&email=" + $( "#email" ).val() +
85                    "&pwd=" + $( "#password" ).val() + "&role=" + this.selectRoles.getValue() + "&state=" + this.selectStates.getValue() +
86                    "&hasAccess=" + $( "#boAccess" ).attr( 'checked' ) + "&keepSamePassword=" + $( "#keepSamePassword" ).attr( 'checked' );
87            $.ajax( {
88                url: "backoffice?methodName=modifyUser&" + parametersUrl,
89                success:jQuery.proxy( this.handleUser, this ),
90                error: jQuery.proxy( this.showErrors, [this] )
91            } );
92        }
93    },
94
95    requestAcceptUser: function( waitingUser )
96    {
97        this.containerErrors.hide();
98
99        if( waitingUser )
100            $.ajax( {
101                url: "backoffice?methodName=acceptOrRefuseUser&id=" + waitingUser.id + "&ok=true",
102                success: jQuery.proxy( this.handleUser, this ),
103                error: jQuery.proxy( this.showErrors, [this, waitingUser.email ] )
104            } );
105    },
106
107    requestRefuseUser: function( waitingUser )
108    {
109        this.containerErrors.hide();
110
111        if( waitingUser )
112            $.ajax( {
113                url: "backoffice?methodName=acceptOrRefuseUser&id=" + waitingUser.id + "&ok=false",
114                success: jQuery.proxy( this.handleUser, this ),
115                error: jQuery.proxy( this.showErrors, [this, waitingUser.email ] )
116            } );
117    },
118
119    requestSortUser: function()
120    {
121        var parametersUrl = "sort=" + this.selectSorts.getValue() + "&sortRole=" + this.selectSortsRoles.getValue() + "&sortState=" + this.selectSortsStates.getValue() +
122                "&sortAccessBoTrue=" + $( "#sort_accessBO_true" ).attr( 'checked' ) + "&sortAccessBoFalse=" + $( "#sort_accessBO_false" ).attr( 'checked' ) +
123                "&maxResults=" + this.selectSortsDisplay.getValue() + "&page=" + this.containerPage.html();
124        var request = $.ajax( {
125            url: "backoffice?methodName=sortUser&" + parametersUrl,
126            success:jQuery.proxy( this.handleSortUser, this ),
127            error: jQuery.proxy( this.showErrors, [this] )
128        } );
129    },
130
131    // HANDLES ********************************************************
132    handleUser: function()
133    {
134        this.requestSortUser();
135    },
136
137    handleSortUser: function( result )
138    {
139        this.jsonUsers = jQuery.parseJSON( result ).jSonUsers;
140        this.jsonWaitingUsers = jQuery.parseJSON( result ).jSonWaitingUsers;
141        this.nbUsers = jQuery.parseJSON( result ).usersNumber;
142        this.displayWaitingUsers();
143        this.displayUsers();
144        this.clearAddOrModifyUserFields();
145        this.updateAddUserButtonAndTitle();
146    },
147
148    // DISPLAYS ********************************************************
149    displayTRForUser: function( user )
150    {
151        var tr = $( document.createElement( "tr" ) );
152        var tdId = $( document.createElement( "td" ) );
153        tdId.html( user.id );
154        tr.append( tdId );
155        var tdLastName = $( document.createElement( "td" ) );
156        tdLastName.html( user.lastName );
157        tr.append( tdLastName );
158        var tdFirstName = $( document.createElement( "td" ) );
159        tdFirstName.html( user.firstName );
160        tr.append( tdFirstName );
161        var tdEmail = $( document.createElement( "td" ) );
162        tdEmail.html( user.email );
163        tr.append( tdEmail );
164        var tdRole = $( document.createElement( "td" ) );
165        tdRole.html( user.role );
166        tr.append( tdRole );
167
168        var tdDate = $( document.createElement( "td" ) );
169        tdDate.html( formatDate( new Date( user.creationDate.time ) ) );
170        tr.append( tdDate );
171        return tr;
172    },
173
174    displayWaitingUsers: function()
175    {
176        this.containerWaitingUsers.empty();
177
178        if( this.jsonWaitingUsers && 0 < this.jsonWaitingUsers.length )
179        {
180            jQuery.each( this.jsonWaitingUsers, jQuery.proxy( function( i, waitingUser )
181            {
182                var tr = this.displayTRForUser( waitingUser );
183
184                // Buttons
185                var tdAccept = $( document.createElement( "td" ) );
186                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 )} );
187                tr.append( tdAccept );
188
189                var tdRefuse = $( document.createElement( "td" ) );
190                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 )} );
191                tr.append( tdRefuse );
192
193                this.containerWaitingUsers.append( tr );
194
195            }, this ) );
196        }
197        else
198        {
199            var tr = $( document.createElement( "tr" ) );
200            var td = $( document.createElement( "td" ) );
201            td.attr( {colspan:"11"} );
202            td.html( "<center>" + interfaceTexts["bo.noUser"] + "</center>" );
203            tr.append( td );
204            this.containerWaitingUsers.append( tr );
205        }
206
207        this.loadingWaitingUser.hide();
208    },
209
210    displayUsers: function()
211    {
212        this.containerUsers.empty();
213
214        if( this.jsonUsers && 0 < this.jsonUsers.length )
215        {
216            jQuery.each( this.jsonUsers, jQuery.proxy( function( i, user )
217            {
218                var tr = this.displayTRForUser( user );
219
220                var tdState = $( document.createElement( "td" ) );
221                tdState.html( user.state );
222                tr.append( tdState );
223                var tdBO = $( document.createElement( "td" ) );
224                var imgBO = $( document.createElement( "img" ) );
225                if( user.accessToBO )
226                    imgBO.attr( {width:"10", height:"10", src:"resources/images/utils/ok.png"} );
227                else
228                    imgBO.attr( {width:"10", height:"10", src:"resources/images/utils/not.png"} );
229                tdBO.append( "&nbsp;" ).append( imgBO );
230                tr.append( tdBO );
231
232                // Buttons
233                var tdModify = $( document.createElement( "td" ) );
234                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 )} );
235                tr.append( tdModify );
236
237                var tdRemove = $( document.createElement( "td" ) );
238                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 )} );
239                tr.append( tdRemove );
240
241                this.containerUsers.append( tr );
242
243            }, this ) );
244        }
245        else
246        {
247            var tr = $( document.createElement( "tr" ) );
248            var td = $( document.createElement( "td" ) );
249            td.attr( {colspan:"11"} );
250            td.html( "<center><BR/>" + interfaceTexts["bo.noUser"] + "<BR/><BR/></center>" );
251            tr.append( td );
252            this.containerUsers.append( tr );
253        }
254
255        // Update number of users and max pages
256        this.containerUsersNumber.html( this.nbUsers );
257        this.containerMaxPage.html( Math.ceil( this.nbUsers / this.selectSortsDisplay.getValue() ) );
258        this.loadingUser.hide();
259    },
260
261    displayStates: function()
262    {
263        var paramSelect = new Object();
264        paramSelect.id = "select_states";
265        paramSelect.parent = this.containerStates;
266        this.selectStates = new Select( paramSelect );
267        jQuery.each( this.jsonUserStates, jQuery.proxy( function ( i, jsonUserState )
268        {
269            this.selectStates.add( jsonUserState.value, interfaceTexts[jsonUserState.text] );
270        }, this ) );
271        this.selectStates.selectFirst( false );
272    },
273
274    displayRoles: function()
275    {
276        var paramSelect = new Object();
277        paramSelect.id = "select_roles";
278        paramSelect.parent = this.containerRoles;
279        this.selectRoles = new Select( paramSelect );
280        jQuery.each( this.jsonUserRoles, jQuery.proxy( function ( i, jsonUserRole )
281        {
282            this.selectRoles.add( jsonUserRole.value, interfaceTexts[jsonUserRole.text] );
283        }, this ) );
284        this.selectRoles.selectFirst( false );
285    },
286
287    displaySorts: function()
288    {
289        // Sort by name, email, ...
290        var paramSelect = new Object();
291        paramSelect.id = "select_sorts";
292        paramSelect.parent = this.containerSorts;
293        this.selectSorts = new Select( paramSelect );
294        this.selectSorts.add( "lastName", interfaceTexts["bo.user.lastName"] );
295        this.selectSorts.add( "id", interfaceTexts["bo.id"] );
296        this.selectSorts.add( "email", interfaceTexts["bo.user.email"] );
297        this.selectSorts.add( "creationDate", interfaceTexts["bo.user.creationDate"] );
298        this.selectSorts.selectFirst( false );
299
300        // Sort by state
301        var paramSelect = new Object();
302        paramSelect.id = "select_sorts_states";
303        paramSelect.parent = this.containerSortsStates;
304        this.selectSortsStates = new Select( paramSelect );
305        this.selectSortsStates.add( "ALL", interfaceTexts["bo.all"] );
306        jQuery.each( this.jsonUserStates, jQuery.proxy( function ( i, jsonUserState )
307        {
308            this.selectSortsStates.add( jsonUserState.value, interfaceTexts[jsonUserState.text] );
309        }, this ) );
310        this.selectSortsStates.selectFirst( false );
311
312        // Sort by role
313        var paramSelect = new Object();
314        paramSelect.id = "select_sorts_roles";
315        paramSelect.parent = this.containerSortsRoles;
316        this.selectSortsRoles = new Select( paramSelect );
317        this.selectSortsRoles.add( "ALL", interfaceTexts["bo.all"] );
318        jQuery.each( this.jsonUserRoles, jQuery.proxy( function ( i, jsonUserRole )
319        {
320            this.selectSortsRoles.add( jsonUserRole.value, interfaceTexts[jsonUserRole.text] );
321        }, this ) );
322        this.selectSortsRoles.selectFirst( false );
323
324        // Number of displays
325        var paramSelect = new Object();
326        paramSelect.id = "select_sorts_display";
327        paramSelect.parent = this.containerSortsDisplay;
328        this.selectSortsDisplay = new Select( paramSelect );
329        this.selectSortsDisplay.add( "5", 5 );
330        this.selectSortsDisplay.add( "10", 10 );
331        this.selectSortsDisplay.add( "15", 15 );
332        this.selectSortsDisplay.add( "20", 20 );
333        this.selectSortsDisplay.selectFirst( false );
334
335        // Button sort
336        $( "#sortButton" ).bind( 'click', this, jQuery.proxy( this.onClickSort, this ) );
337    },
338
339    // EVENTS ********************************************************
340    onClickModify: function( user )
341    {
342        this.user = user;
343        $( "#lastName" ).val( user.lastName );
344        $( "#firstName" ).val( user.firstName );
345        $( "#email" ).val( user.email );
346        $( "#password" ).attr( 'disabled', true );
347        $( "#keepSamePassword" ).attr( 'disabled', false );
348        $( "#keepSamePassword" ).attr( 'checked', true );
349        this.selectRoles.select( user.role, false );
350        this.selectStates.select( user.state, false );
351        $( "#boAccess" ).attr( 'checked', user.accessToBO );
352
353        this.updateModifyUserButtonAndTitle( user.id );
354    },
355
356    onClickRemove: function( user )
357    {
358        this.user = user;
359        if( window.confirm( interfaceTexts["bo.user.remove.confirm"] + " " + user.firstName + " " + user.lastName + " ?" ) )
360            jQuery.proxy( this.requestRemoveUser(), this );
361    },
362
363    onCheckKeepSamePassword: function()
364    {
365        var actualState = $( "#keepSamePassword" ).attr( 'checked' );
366        $( "#password" ).attr( 'disabled', actualState );
367        if( actualState )
368            $( "#password" ).val( '' );
369    },
370
371    onClickInit: function()
372    {
373        this.updateAddUserButtonAndTitle();
374        this.clearAddOrModifyUserFields();
375    },
376
377    onClickSort: function()
378    {
379        this.containerPage.html( 1 );
380        this.requestSortUser();
381    },
382
383    onClickPrevious: function()
384    {
385        decrementPage( this.containerPage, this.containerMaxPage );
386        this.requestSortUser();
387    },
388
389    onClickNext: function()
390    {
391        incrementPage( this.containerPage, this.containerMaxPage );
392        this.requestSortUser();
393    },
394
395    // OTHERS ********************************************************
396    updateAddUserButtonAndTitle: function()
397    {
398        this.containerAddOrModifyTitle.empty();
399        this.containerAddOrModifyTitle.html( interfaceTexts["bo.user.add"] + " :<BR/><BR/>" );
400        $( "#add_or_modify_user" ).unbind( 'click' );
401        $( "#add_or_modify_user" ).bind( 'click', this, jQuery.proxy( this.requestAddUser, this ) );
402        $( "#add_or_modify_user" ).html( interfaceTexts["bo.add"] );
403        $( "#password" ).attr( 'disabled', false );
404        $( "#keepSamePassword" ).attr( 'disabled', true );
405    },
406
407    updateModifyUserButtonAndTitle: function( userId )
408    {
409        this.containerAddOrModifyTitle.empty();
410        this.containerAddOrModifyTitle.html( interfaceTexts["bo.user.modify"] + " (id : " + userId + ")<BR/><BR/>" );
411        $( "#add_or_modify_user" ).unbind( 'click' );
412        $( "#add_or_modify_user" ).bind( 'click', this, jQuery.proxy( this.requestModifyUser, this ) );
413        $( "#add_or_modify_user" ).html( interfaceTexts["bo.modify"] );
414        $( "#password" ).attr( 'disabled', true );
415        $( "#keepSamePassword" ).attr( 'disabled', false );
416    },
417
418    clearAddOrModifyUserFields: function()
419    {
420        $( ':input', '#addOrModifyForm' )
421                .not( ':button, :submit, :reset, :hidden' )
422                .val( '' )
423                .removeAttr( 'checked' )
424                .removeAttr( 'selected' );
425
426        this.selectRoles.selectFirst( false );
427        this.selectStates.selectFirst( false );
428    },
429
430    verifyFields: function()
431    {
432        this.containerErrors.hide();
433
434        var errors = false;
435        if( '' == $( "#lastName" ).val() )
436            errors = interfaceTexts["bo.field.lastName"];
437        if( '' == $( "#email" ).val() )
438            errors = !errors ? interfaceTexts["bo.field.email"] : errors + "<BR/>" + interfaceTexts["bo.field.email"];
439        if( '' == $( "#password" ).val() && !$( "#keepSamePassword" ).attr( 'checked' ) )
440            errors = !errors ? interfaceTexts["bo.field.password"] : errors + "<BR/>" + interfaceTexts["bo.field.password"];
441
442        if( errors )
443        {
444            this.containerErrors.show();
445            this.containerErrors.html( errors );
446        }
447        return errors;
448    },
449
450    showErrors: function( result )
451    {
452        var context = this[0];
453        var text = this[1];
454        context.containerErrors.show();
455        if( text )
456            context.containerErrors.html( interfaceTexts[result.responseText] + " " + text );
457        else
458            context.containerErrors.html( interfaceTexts[result.responseText] );
459    },
460
461    managePartAddOrModify: function()
462    {
463        this.displayStates();
464        this.displayRoles();
465
466        $( "#keepSamePassword" ).attr( 'disabled', true );
467        $( "#keepSamePassword" ).bind( 'click', this.onCheckKeepSamePassword );
468
469        $( "#init" ).bind( 'click', this, jQuery.proxy( this.onClickInit, this ) );
470    },
471
472    managePagination: function()
473    {
474        $( "#previous" ).bind( 'click', this, jQuery.proxy( this.onClickPrevious, this ) );
475        $( "#next" ).bind( 'click', this, jQuery.proxy( this.onClickNext, this ) );
476    }
477
478} );
479
480
481</script>
Note: See TracBrowser for help on using the repository browser.