- LSinfosBox :
[ldapsaisie.git] / trunk / includes / js / LSinfosBox.js
1 var LSinfosBox = new Class({
2     initialize: function(options){
3       // Default options
4       this._options = {
5         closeBtn:       0,
6         name:           '',
7         fxDuration:     500,
8         opacity:        0.8,
9         autoClose:      3000
10       };
11       
12       // Load options from argument
13       if ($type(options)=='object') {
14         $each(options,function(val,name) {
15           if ($type(this._options[name])) {
16             this._options[name]=val;
17           }
18         },this);
19       }
20       
21       this.build();
22       
23       this.opened=0;
24     },
25     
26     build: function() {
27       var classes;
28       if (this._options.name!='') {
29         classes='LSinfosBox '+this._options.name;
30       }
31       else {
32         classes='LSinfosBox'
33       }
34       
35       this.core = new Element('div');
36       this.core.addClass(classes);
37       this.core.addEvent('dblclick',this.close.bind(this));
38       
39       if(this._options.closeBtn) {
40         this.closeBtn = new Element('span');
41         this.closeBtn.addClass(classes);
42         this.closeBtn.set('html','X');
43         this.closeBtn.addEvent('click',this.close.bind(this));
44         this.closeBtn.injectInside(this.core);
45       }
46       
47       this.content = new Element('p');
48       this.content.addEvent(classes);
49       this.content.injectInside(this.core);
50       
51       this.fx = new Fx.Tween(
52         this.core,
53         {
54             property: 'opacity',
55             duration: this._options.fxDuration,
56             fps:      30
57         }
58       );
59       
60       this.core.inject(document.body,'top');
61     },
62     
63     isOpened: function() {
64       return this.opened;
65     },
66     
67     open: function() {
68       this.core.setStyle('top',getScrollTop()+10);
69       
70       if (this._options.autoClose>0) {
71         this.closeTime = (new Date()).getTime();
72         this.autoClose.delay((this._options.autoClose+this._options.fxDuration),this,this.closeTime);
73       }
74       
75       if (this.opened) {
76         return true;
77       }
78       
79       this.fx.start(0,this._options.opacity);
80       this.opened = 1;
81       
82     },
83     
84     close: function(withoutEffect) {
85       if (this.opened) {
86         this.opened = 0;
87         if (withoutEffect==1) {
88           this.fx.set(0);
89         }
90         else {
91           this.fx.start(this._options.opacity,0);
92         }
93       }
94     },
95     
96     autoClose: function(time) {
97       if (time==this.closeTime) {
98         this.close();
99         this.closeTime=0;
100       }
101     },
102     
103     addInfo: function(html) {
104       var ul = this.content.getLast("ul");
105       var add = 0;
106       if (!$type(ul)) {
107         ul = new Element('ul');
108         if (this.content.innerHTML!="") {
109           var c_li = new Element('li');
110           c_li.set('html',this.content.innerHTML);
111           c_li.injectInside(ul);
112           add=1;
113         }
114         this.content.empty();
115         ul.injectInside(this.content);
116       }
117       if (add) {
118         var b_li = new Element('li');
119         b_li.set('html','<hr/>');
120         b_li.injectInside(ul);
121       }
122       var li = new Element('li');
123       li.set('html',html);
124       li.injectInside(ul);
125       this.open();
126     },
127     
128     display: function(html) {
129       if ($type(html)) {
130         this.content.empty();
131         this.content.set('html',html);
132       }
133       this.open();
134     }
135 });