- Passage à Mootools v1.2templates/LSrelations.tpl
[ldapsaisie.git] / trunk / includes / js / LSrelation.js
1 var LSrelation = new Class({
2     initialize: function(){
3       this.edit = 0;
4       this.deleteBtn = [];
5       this.deleteBtnId = 0;
6       this.refreshRelation=0;
7       this._confirmDelete=1;
8       $$('a.LSrelation_modify').each(function(el) {
9         this.edit=1;
10         el.addEvent('click',this.onLSrelationModifyBtnClick.bindWithEvent(this,el));
11       }, this);
12       if (this.edit) {
13         this.initializeBtn();
14       }
15     },
16     
17     initializeBtn: function() {
18       $$('img.LSrelation-btn').each(function(el) {
19         el.destroy();
20       }, this);
21       this.deleteBtnId = 0;
22       $$('li.LSrelation').each(function(li) {
23         this.deleteBtn[this.deleteBtnId] = new Element('img');
24         this.deleteBtn[this.deleteBtnId].src = 'templates/images/delete.png';
25         this.deleteBtn[this.deleteBtnId].setStyle('cursor','pointer');
26         this.deleteBtn[this.deleteBtnId].addClass('LSrelation-btn');
27         this.deleteBtn[this.deleteBtnId].addEvent('click',this.onDeleteBtnClick.bind(this,this.deleteBtn[this.deleteBtnId]));
28         this.deleteBtn[this.deleteBtnId].injectInside(li);
29         li.id=this.deleteBtnId;
30         this.deleteBtnId++;
31       }, this);
32     },
33     
34     onDeleteBtnClick: function(img) {
35       if (this._confirmDelete) {
36         var li = img.getParent();
37         var span = li.getFirst('span');
38         this.confirmBox = new LSconfirmBox({
39           text:         'Etês-vous sur de vouloir supprimer "'+span.innerHTML+'" ?', 
40           startElement: img,
41           onConfirm:    this.deleteFromImg.bind(this,img)
42         });
43       }
44       else {
45         this.deleteFromImg(img);
46       }
47     },
48     
49     deleteFromImg: function(img) {
50       var li = img.getParent();
51       var span = li.getFirst('span');
52       var ul = li.getParent();
53       img.destroy();
54       LSdebug(ul.id);
55       var getId = /LSrelation_ul_([0-9]*)/
56       var id = getId.exec(ul.id)[1];
57       
58       var data = {
59         template:   'LSrelation',
60         action:     'deleteByDn',
61         id:         id,
62         dn:         span.id
63       };
64       data.imgload=varLSdefault.loadingImgDisplay(li.id,'inside');
65       new Request({url: 'index_ajax.php', data: data, onSuccess: this.deleteFromImgComplete.bind(this)}).send();
66     },
67     
68     deleteFromImgComplete: function(responseText, responseXML) {
69       var data = JSON.decode(responseText);
70       if ( varLSdefault.checkAjaxReturn(data) ) {
71         try  {
72           $(data.dn).getParent().destroy();
73         }
74         catch(e) {
75           LSdebug('Erreur durant la suppression du li du DN : '+data.dn);
76         }
77       }
78     },
79     
80     onLSrelationModifyBtnClick: function(event,a) {
81       new Event(event).stop();
82       
83       var data = {
84         template:   'LSrelation',
85         action:     'refreshSession',
86         id:         a.id,
87         href:       a.href
88       };
89       
90       LSdebug(data);
91       this.refreshRelation=a.id;
92       data.imgload=varLSdefault.loadingImgDisplay('LSrelation_title_'+a.id,'inside');
93       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onLSrelationModifyBtnClickComplete.bind(this)}).send();
94     },
95     
96     onLSrelationModifyBtnClickComplete: function(responseText, responseXML) {
97       var data = JSON.decode(responseText);
98       if ( varLSdefault.checkAjaxReturn(data) ) {
99         varLSsmoothbox.setRefreshElement(this);
100         varLSsmoothbox.openURL(data.href,{startElement: $(data.id), width: 615});
101       }
102     },
103     
104     refresh: function() {
105       var data = {
106         template:   'LSrelation',
107         action:     'refreshList',
108         id:         this.refreshRelation
109       };
110       
111       LSdebug(data);
112       data.imgload=varLSdefault.loadingImgDisplay('LSrelation_title_'+this.refreshRelation,'inside');
113       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onRrefreshComplete.bind(this)}).send();
114     },
115     
116     onRrefreshComplete: function(responseText, responseXML) {
117       var data = JSON.decode(responseText);
118       if ( varLSdefault.checkAjaxReturn(data) ) {
119         $('LSrelation_ul_'+this.refreshRelation).set('html',data.html);
120         this.initializeBtn();
121       }
122     }
123
124 });
125
126 window.addEvent(window.ie ? 'load' : 'domready', function() {
127   varLSrelation = new LSrelation();
128 });