Lang file : Update lang french file
[ldapsaisie.git] / trunk / includes / js / LSconfirmBox.js
1 var LSconfirmBox = new Class({
2     initialize: function(options) {
3       this._options = options;
4       this.labels = varLSdefault.LSjsConfig['LSconfirmBox_labels'];
5       if (!$type(this.labels)) {
6         this.labels = {
7           title: 'Confirmation',
8           text: 'You confirm your choice ?',
9           validate: 'Validate',
10           cancel: 'Cancel'
11         };
12       }
13       this.create();
14       this.display();
15     },
16     
17     create: function() {
18       this.box = new Element('div');
19       this.box.setProperty('id','box-LSconfirmBox');
20       this.box.injectInside(document.body);
21
22       this.title = new Element('p');
23       this.title.setProperty('id','title-LSconfirmBox');
24       if (this._options.title) {
25         this.title.set('html',this._options.title);
26       }
27       else {
28         this.title.set('html',this.labels.title);
29       };
30       this.title.injectInside(this.box)
31       
32       this.closeBtn = new Element('span');
33       this.closeBtn.setProperty('id','closeBtn-LSconfirmBox');
34       this.closeBtn.injectInside(this.box);
35       this.closeBtn.addEvent('click',this.cancel.bind(this));
36       
37       this.text = new Element('p');
38       this.text.setProperty('id','text-LSconfirmBox');
39       if (this._options.text) {
40         this.text.set('html',this._options.text);
41       }
42       else {
43         this.text.set('html',this.labels.text);
44       }
45       this.text.injectInside(this.box);
46       
47       this.btnsBox = new Element('p');
48       this.btnsBox.setProperty('id','btnsBox-LSconfirmBox');
49       this.btnsBox.injectInside(this.box);
50       
51       this.confirmBtn = new Element('span');
52       this.confirmBtn.addClass('btn-LSconfirmBox');
53       if (this._options.validate_label) {
54         this.confirmBtn.set('html',this._options.validate_label);
55       }
56       else {
57         this.confirmBtn.set('html',this.labels.validate);
58       }
59       this.confirmBtn.injectInside(this.btnsBox);
60       this.confirmBtn.addEvent('click',this.confirm.bind(this));
61       
62       this.cancelBtn = new Element('span');
63       this.cancelBtn.addClass('btn-LSconfirmBox');
64       if (this._options.cancel_label) {
65         this.cancelBtn.set('html',this._options.cancel_label);
66       }
67       else {
68         this.cancelBtn.set('html',this.labels.cancel);
69       }
70       this.cancelBtn.injectInside(this.btnsBox);
71       this.cancelBtn.addEvent('click',this.cancel.bind(this));
72       
73       this._purge=0;
74       
75       this.fx = {
76         open:   new Fx.Morph(this.box, {duration: 500, fps: 30, transition: Fx.Transitions.Sine.easeOut, onComplete: this.displayContent.bind(this)}),
77         close:  new Fx.Morph(this.box, {duration: 500, fps: 30, transition: Fx.Transitions.Sine.easeOut, onComplete: this.onClose.bind(this)})
78       };
79       this._scrolling=0;
80     },
81     
82     display: function() {
83       this.box.setStyle('display','block');
84       this.position(true);
85       window.addEvent('resize', this.position.bind(this));
86       window.addEvent('scroll', this.positionWhenScrolling.bind(this));
87     },
88     
89     positionWhenScrolling: function(oldValue) {
90       if (this._scrolling==0||$type(oldValue)) {
91         this._scrolling = 1;
92         var current = window.getScrollTop().toInt();
93         if (oldValue == current) {
94           this.position();
95           this._scrolling=0;
96         }
97         else {
98           this.positionWhenScrolling.delay(200,this,current);
99         }
100       }
101     },
102     
103     displayContent: function() {
104       [this.title, this.closeBtn, this.text, this.btnsBox].each(function(el) {
105         var fx = new Fx.Tween(el,{duration: 200, fps: 30});
106         fx.start('opacity',1);
107       },this);
108     },
109     
110     hide: function() {
111       this.box.empty();
112       this.fx.close.start(this.getStartStyles());
113       window.removeEvent('resize', this.position.bind(this));
114     },
115     
116     onClose: function() {
117       this.box.setStyle('display','none');
118       this.purge();
119       if (this._options.onClose) {
120         $try(this._options.onClose);
121       }
122     },
123     
124     purge: function() {
125       this._purge=1;
126       this.box.empty();
127       this.box.destroy();
128       delete this.fx;
129     },
130     
131     getStartStyles: function() {
132       if ($type(this._options.startElement)) {
133         var startStyles = {
134           top:      this._options.startElement.getCoordinates().top,
135           left:     this._options.startElement.getCoordinates().left,
136           width:    this._options.startElement.getWidth().toInt(),
137           opacity:  0
138         };
139       }
140       else {
141         var startStyles = {
142           top:      '0px',
143           left:     '0px',
144           width:    '0px',
145           opacity:  0
146         };
147       }
148       return startStyles;
149     },
150     
151     getEndStyles: function() {
152       if (this._options.width) {
153         w = this._options.width;
154       }
155       else {
156         w = 300;
157       }
158       
159       var endStyles = {
160         width:    w.toInt()+'px',
161         top:      ((window.getHeight()/2)-(this.box.getStyle('height').toInt()/2)-this.box.getStyle('border').toInt()+window.getScrollTop()).toInt(),
162         left:     ((window.getWidth()/2)-(w/2)-this.box.getStyle('border').toInt()).toInt(),
163         opacity:  1
164       };
165       return endStyles;
166     },
167     
168     position: function(start) {
169       if (this._purge==0) {
170         var endStyles = this.getEndStyles();
171         if (start) {
172           this.box.setStyles(this.getStartStyles());
173         }
174         this.fx.open.start(endStyles);
175       }
176     },
177     
178     confirm: function() {
179       this.hide();
180       if (this._options.onConfirm) {
181         $try(this._options.onConfirm);
182       }
183     },
184     
185     cancel: function() {
186       this.hide();
187       if (this._options.onCancel) {
188         $try(this._options.onCancel);
189       }
190     }
191 });