1 | //******************************************************** |
---|
2 | // Class Button |
---|
3 | // Display a button |
---|
4 | // |
---|
5 | // str value: Value to display in the button |
---|
6 | // dom parent: in which dom element to draw the button |
---|
7 | // int id: id of the dom element |
---|
8 | // func onclick: callback function when button clicked |
---|
9 | // str className : className of the general button |
---|
10 | // str classNameText : className for the button's text |
---|
11 | //******************************************************** |
---|
12 | |
---|
13 | var Button = Class.create({ |
---|
14 | initialize: function( param ) |
---|
15 | { |
---|
16 | // Init |
---|
17 | this.id = param.id ? param.id : ""; |
---|
18 | this.parent = param.parent ? param.parent : false; |
---|
19 | this.value = param.value ? param.value : ""; |
---|
20 | this.className = param.className ? param.className : false; |
---|
21 | this.classNameText = param.classNameText ? param.classNameText : false; |
---|
22 | this.callbackOnClick = param.onClick ? param.onClick : false; |
---|
23 | this.boolDisabled = false; |
---|
24 | this.boolSelected = false; |
---|
25 | this.boolDisplayed = false; |
---|
26 | |
---|
27 | // Create button elements |
---|
28 | this.divContainer = $(document.createElement("button")); |
---|
29 | if(this.className) |
---|
30 | this.divContainer.className = this.className; |
---|
31 | this.divContainer.atMe = this; |
---|
32 | this.divContainer.id = this.id; |
---|
33 | if( this.parent ) |
---|
34 | this.parent.appendChild(this.divContainer); |
---|
35 | |
---|
36 | this.divText = $(document.createElement("div")); |
---|
37 | if(this.classNameText) |
---|
38 | this.divText.className = "button_text"; |
---|
39 | this.divText.innerHTML = this.value; |
---|
40 | this.divContainer.appendChild(this.divText); |
---|
41 | |
---|
42 | // Define button events |
---|
43 | Event.observe(this.divContainer, 'click', this.onClick.bindAsEventListener(this)); |
---|
44 | Event.observe(this.divContainer, 'mouseover', this.onHover.bindAsEventListener(this)); |
---|
45 | }, |
---|
46 | |
---|
47 | // Getter / Setter ******************************************************** |
---|
48 | |
---|
49 | getContainer: function() |
---|
50 | { |
---|
51 | return this.divContainer; |
---|
52 | }, |
---|
53 | |
---|
54 | isDisable : function() |
---|
55 | { |
---|
56 | return this.boolDisabled; |
---|
57 | }, |
---|
58 | |
---|
59 | isSelected: function() |
---|
60 | { |
---|
61 | return this.boolSelected; |
---|
62 | }, |
---|
63 | |
---|
64 | setValue : function( value ) |
---|
65 | { |
---|
66 | this.value = value; |
---|
67 | this.divText.innerHTML = this.value; |
---|
68 | }, |
---|
69 | |
---|
70 | setCallbackOnClick : function( value ) |
---|
71 | { |
---|
72 | this.callbackOnClick = value; |
---|
73 | }, |
---|
74 | |
---|
75 | // Actions ******************************************************** |
---|
76 | |
---|
77 | disable : function() |
---|
78 | { |
---|
79 | this.boolDisabled = true; |
---|
80 | this.divContainer.addClassName("disable"); |
---|
81 | }, |
---|
82 | |
---|
83 | enable : function() |
---|
84 | { |
---|
85 | this.boolDisabled = false; |
---|
86 | this.divContainer.removeClassName("disable"); |
---|
87 | }, |
---|
88 | |
---|
89 | select : function( value ) |
---|
90 | { |
---|
91 | this.boolSelected = value; |
---|
92 | if( this.boolSelected ) |
---|
93 | this.divContainer.addClassName("selected"); |
---|
94 | else |
---|
95 | this.divContainer.removeClassName("selected"); |
---|
96 | }, |
---|
97 | |
---|
98 | show : function() |
---|
99 | { |
---|
100 | this.boolDisplayed = true; |
---|
101 | this.divContainer.style.display = ""; |
---|
102 | }, |
---|
103 | |
---|
104 | hide : function() |
---|
105 | { |
---|
106 | this.boolDisplayed = false; |
---|
107 | this.divContainer.style.display = "none"; |
---|
108 | }, |
---|
109 | |
---|
110 | // Events ******************************************************** |
---|
111 | |
---|
112 | onClick : function( event ) |
---|
113 | { |
---|
114 | if( !this.isDisable() && (undefined == event.detail || 1 == event.detail) ) |
---|
115 | { |
---|
116 | if( this.callbackOnClick ) |
---|
117 | this.callbackOnClick(); |
---|
118 | } |
---|
119 | Event.stop(event); |
---|
120 | }, |
---|
121 | |
---|
122 | onHover : function( event ) |
---|
123 | { |
---|
124 | Event.stop(event); |
---|
125 | } |
---|
126 | }); |
---|