440e7cabffd09c610fe562762abd7da69396841c
[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       $$('a.LSrelation_modify').each(function(el) {
8         this.edit=1;
9         el.addEvent('click',this.onLSrelationModifyBtnClick.bindWithEvent(this,el));
10       }, this);
11       if (this.edit) {
12         this.initializeBtn();
13       }
14     },
15     
16     initializeBtn: function() {
17       $$('img.LSrelation-btn').each(function(el) {
18         el.remove();
19       }, this);
20       this.deleteBtnId = 0;
21       $$('li.LSrelation').each(function(li) {
22         this.deleteBtn[this.deleteBtnId] = new Element('img');
23         this.deleteBtn[this.deleteBtnId].src = 'templates/images/delete.png';
24         this.deleteBtn[this.deleteBtnId].setStyle('cursor','pointer');
25         this.deleteBtn[this.deleteBtnId].addClass('LSrelation-btn');
26         this.deleteBtn[this.deleteBtnId].addEvent('click',this.onDeleteBtnClick.bind(this,this.deleteBtn[this.deleteBtnId]));
27         this.deleteBtn[this.deleteBtnId].injectInside(li);
28         li.id=this.deleteBtnId;
29         this.deleteBtnId++;
30       }, this);
31     },
32     
33     onDeleteBtnClick: function(img) {
34       li = img.getParent();
35       ul = li.getParent();
36       img.remove();
37       LSdebug(ul.id);
38       var getId = /LSrelation_ul_([0-9]*)/
39       var id = getId.exec(ul.id)[1];
40       
41       var data = {
42         template:   'LSrelation',
43         action:     'deleteByDisplayValue',
44         id:         id,
45         value:      li.innerHTML
46       };
47       this.deleteLi = li;
48       data.imgload=varLSdefault.loadingImgDisplay(li.id,'inside');
49       LSdebug(data);
50       new Ajax('index_ajax.php',  {data: data, onComplete: this.onDeleteBtnClickComplete.bind(this)}).request();
51     },
52     
53     onDeleteBtnClickComplete: function(responseText, responseXML) {
54       var data = Json.evaluate(responseText);
55       LSdebug(data);
56       if ( data ) {
57         if ( typeof(data.LSerror) != "undefined" ) {
58             if (data.imgload!='') {
59               varLSdefault.loadingImgHide(data.imgload);
60             }
61             else {
62               varLSdefault.loadingImgHide();
63             }
64             varLSdefault.displayError(data.LSerror);
65             return;
66           } 
67           else {
68             varLSdefault.loadingImgHide(data.imgload);
69             this.deleteLi.remove();
70           }
71       }
72     },
73     
74     onLSrelationModifyBtnClick: function(event,a) {
75       new Event(event).stop();
76       
77       var data = {
78         template:   'LSrelation',
79         action:     'refreshSession',
80         id:         a.id,
81         href:       a.href
82       };
83       
84       LSdebug(data);
85       this.refreshRelation=a.id;
86       data.imgload=varLSdefault.loadingImgDisplay('LSrelation_title_'+a.id,'inside');
87       new Ajax('index_ajax.php',  {data: data, onComplete: this.onLSrelationModifyBtnClickComplete.bind(this)}).request();
88     },
89     
90     onLSrelationModifyBtnClickComplete: function(responseText, responseXML) {
91       var data = Json.evaluate(responseText);
92       LSdebug(data);
93       if ( data ) {
94         if ( typeof(data.LSerror) != "undefined" ) {
95             if (data.imgload!='') {
96               varLSdefault.loadingImgHide(data.imgload);
97             }
98             else {
99               varLSdefault.loadingImgHide();
100             }
101             varLSdefault.displayError(data.LSerror);
102             return;
103           } 
104           else {
105             varLSdefault.loadingImgHide(data.imgload);
106             varLSsmoothbox.openURL(data.href,this);
107           }
108       }
109     },
110     
111     refresh: function() {
112       var data = {
113         template:   'LSrelation',
114         action:     'refreshList',
115         id:         this.refreshRelation
116       };
117       
118       LSdebug(data);
119       data.imgload=varLSdefault.loadingImgDisplay('LSrelation_title_'+this.refreshRelation,'inside');
120       new Ajax('index_ajax.php',  {data: data, onComplete: this.onRrefreshComplete.bind(this)}).request();
121     },
122     
123     onRrefreshComplete: function(responseText, responseXML) {
124       var data = Json.evaluate(responseText);
125       LSdebug(data);
126       if ( data ) {
127         if ( typeof(data.LSerror) != "undefined" ) {
128             if (data.imgload!='') {
129               varLSdefault.loadingImgHide(data.imgload);
130             }
131             else {
132               varLSdefault.loadingImgHide();
133             }
134             varLSdefault.displayError(data.LSerror);
135             return;
136           } 
137           else {
138             varLSdefault.loadingImgHide(data.imgload);
139             $('LSrelation_ul_'+this.refreshRelation).setHTML(data.html);
140             this.initializeBtn();
141           }
142       }
143     }
144
145 });
146
147 window.addEvent(window.ie ? 'load' : 'domready', function() {
148   varLSrelation = new LSrelation();
149 });
150
151 LSdebug('titi');