Added image.php to permit fully dynamic image path
[ldapsaisie.git] / public_html / includes / js / LSrelation.js
1 var LSrelation = new Class({
2     initialize: function(){
3       this.labels = varLSdefault.LSjsConfig['LSrelation_labels'];
4       if (!$type(this.labels)) {
5         this.labels = {
6           close_confirm_text:     'Do you really want to delete', 
7           close_confirm_title:    'Warning', 
8           close_confirm_validate: 'Delete' 
9         };
10       }
11           
12       this.edit = 0;
13       this.deleteBtn = [];
14       this.deleteBtnId = 0;
15       this.refreshRelation=0;
16       this._confirmDelete=1;
17       $$('a.LSrelation_modify').each(function(el) {
18         this.edit=1;
19         el.addEvent('click',this.onLSrelationModifyBtnClick.bindWithEvent(this,el));
20       }, this);
21       if (this.edit) {
22         this.initializeBtn();
23       }
24     },
25     
26     initializeBtn: function() {
27       $$('img.LSrelation-btn').each(function(el) {
28         el.destroy();
29       }, this);
30       this.deleteBtnId = 0;
31       $$('a.LSrelation_editable').each(function(a) {
32         this.deleteBtn[this.deleteBtnId] = new Element('img');
33         this.deleteBtn[this.deleteBtnId].src = varLSdefault.imagePath('delete');
34         this.deleteBtn[this.deleteBtnId].setStyle('cursor','pointer');
35         this.deleteBtn[this.deleteBtnId].addClass('LSrelation-btn');
36         this.deleteBtn[this.deleteBtnId].addEvent('click',this.onDeleteBtnClick.bind(this,this.deleteBtn[this.deleteBtnId]));
37         this.deleteBtn[this.deleteBtnId].injectAfter(a);
38         a.getParent().id=this.deleteBtnId;
39         this.deleteBtnId++;
40       }, this);
41     },
42     
43     onDeleteBtnClick: function(img) {
44       if (this._confirmDelete) {
45         var a = img.getPrevious('a');
46         this.confirmBox = new LSconfirmBox({
47           text:           this.labels.close_confirm_text + ' "'+a.innerHTML+'" ?', 
48           title:          this.labels.close_confirm_title, 
49           validate_label: this.labels.close_confirm_validate, 
50           startElement:   img,
51           onConfirm:      this.deleteFromImg.bind(this,img)
52         });
53       }
54       else {
55         this.deleteFromImg(img);
56       }
57     },
58     
59     deleteFromImg: function(img) {
60       var li = img.getParent('li');
61       var a = img.getPrevious('a');
62       var ul = li.getParent('ul');
63       img.destroy();
64       LSdebug(ul.id);
65       var getId = /LSrelation_ul_([0-9]*)/
66       var id = getId.exec(ul.id)[1];
67       
68       var data = {
69         template:   'LSrelation',
70         action:     'deleteByDn',
71         id:         id,
72         dn:         a.id
73       };
74       data.imgload=varLSdefault.loadingImgDisplay(li,'inside');
75       new Request({url: 'index_ajax.php', data: data, onSuccess: this.deleteFromImgComplete.bind(this)}).send();
76     },
77     
78     deleteFromImgComplete: function(responseText, responseXML) {
79       var data = JSON.decode(responseText);
80       if ( varLSdefault.checkAjaxReturn(data) ) {
81         try  {
82           var li = $(data.dn).getParent();
83           var ul=$(data.dn).getParent().getParent();
84           li.destroy();
85           if (!$type(ul.getFirst())) {
86             var getId = /LSrelation_ul_([0-9]*)/
87             var id = getId.exec(ul.id)[1];
88             
89             var newli = new Element('li');
90             newli.addClass('LSrelation');
91             newli.set('html',varLSdefault.LSjsConfig['LSrelations'][id]['emptyText']);
92             newli.injectInside(ul);
93           }
94         }
95         catch(e) {
96           LSdebug('Error while deleting the li of DN : '+data.dn);
97         }
98       }
99     },
100     
101     onLSrelationModifyBtnClick: function(event,a) {
102       new Event(event).stop();
103       
104       var data = {
105         template:   'LSrelation',
106         action:     'refreshSession',
107         id:         a.id,
108         href:       a.href
109       };
110       
111       LSdebug(data);
112       this.refreshRelation=a.id;
113       data.imgload=varLSdefault.loadingImgDisplay('LSrelation_title_'+a.id,'inside');
114       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onLSrelationModifyBtnClickComplete.bind(this)}).send();
115     },
116     
117     onLSrelationModifyBtnClickComplete: function(responseText, responseXML) {
118       var data = JSON.decode(responseText);
119       if ( varLSdefault.checkAjaxReturn(data) ) {
120         varLSsmoothbox.asNew();
121         varLSsmoothbox.addEvent('valid',this.onLSsmoothboxValid.bind(this));
122         varLSsmoothbox.openURL(data.href,{startElement: $(data.id), width: 635});
123       }
124     },
125     
126     onLSsmoothboxValid: function() {
127       var data = {
128         template:   'LSrelation',
129         action:     'refreshList',
130         id:         this.refreshRelation
131       };
132       
133       LSdebug(data);
134       data.imgload=varLSdefault.loadingImgDisplay('LSrelation_title_'+this.refreshRelation,'inside');
135       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onLSsmoothboxValidComplete.bind(this)}).send();
136     },
137     
138     onLSsmoothboxValidComplete: function(responseText, responseXML) {
139       var data = JSON.decode(responseText);
140       if ( varLSdefault.checkAjaxReturn(data) ) {
141         $('LSrelation_ul_'+this.refreshRelation).set('html',data.html);
142         this.initializeBtn();
143       }
144     }
145
146 });
147
148 window.addEvent(window.ie ? 'load' : 'domready', function() {
149   varLSrelation = new LSrelation();
150 });