source: tapas/web/resources/js/DomHelper.js @ 381

Last change on this file since 381 was 381, checked in by rboipsl, 12 years ago

clean

File size: 7.0 KB
Line 
1///
2// Requires prototype
3
4var Dom = Dom || {};
5
6Dom.clearContainer = function( containerId )
7{
8    this.getContainer(containerId).innerHTML = '';
9};
10
11Dom.getContainer = function( containerId )
12{
13    var container = $(containerId);
14    if( !container )
15    {
16        throw('Error: container with id="' + containerId + '" not found.');
17    }
18    return container;
19};
20
21Dom.createContainer = function(containerId, className)
22{       
23        var container = $(document.createElement("div"));
24        container.id = containerId;
25        if(className)
26                container.className = className;
27       
28        return container;
29};
30
31Dom.isEnter = function( event )
32{
33    var keyCode = event.keyCode || event.which;
34    return 13 == keyCode;
35};
36
37Dom.isDisable = function(containerId)
38{
39        var container = Dom.getContainer(containerId);
40        if(container.className == "disable")
41                return true;
42        else
43                return false;
44}
45
46var Row = Class.create(
47{
48    initialize: function( id, classType )
49    {
50        this.cellType = 'td';
51        if( null != id && "undefined" != id && null != classType)
52            this.domRow = new Element('tr', {id:id, class:classType});
53        else if ( null != id && "undefined" != id )
54            this.domRow = new Element('tr', {id:id});
55        else
56            this.domRow = new Element('tr');
57    },
58
59    addCell: function( contents )
60    {
61        var td = new Element(this.cellType);
62        if( 'string' == typeof contents )
63        {
64            td.update(contents);
65        }
66        else
67        {
68            td.insert(contents);
69        }
70        this.domRow.insert(td);
71    },
72
73    addCellWithSpaceIfEmpty: function( contents )
74    {
75        if( null != contents && "" != contents )
76            this.addCell(contents);
77        else
78        {
79            var td = new Element(this.cellType);
80            td.insert(" ");
81            this.domRow.insert(td);
82        }
83    },
84
85    addRealCell: function( cell )
86    {
87        this.domRow.insert(cell);
88    },
89
90    findInContainer: function( containerId )
91    {
92        var container = Dom.getContainer(containerId);
93        var row;
94        $A(container.childElements()).each(function( each )
95        {
96            if( this.id == each.readAttribute('id') )
97            {
98                row = each;
99                throw $break;
100            }
101        }.bind(this));
102        return row;
103    },
104
105    addToContainer: function( containerId )
106    {
107        var domRow = this.findInContainer(containerId);
108        if( undefined == domRow )
109        {
110            Dom.getContainer(containerId).insert(this.domRow);
111        }
112    },
113
114    removeFromContainer: function( containerId )
115    {
116        var row = this.findInContainer(containerId);
117        if( undefined != row )
118        {
119            row.remove();
120        }
121    }
122});
123
124
125var HeaderRow = Class.create(Row, {
126    initialize: function( $super, name, id )
127    {
128        $super(name, id);
129        this.cellType = 'th';
130    }
131});
132
133
134var Cell = Class.create({
135       
136    initialize: function( id )
137    {
138        if( null != id && "undefined" != id )
139                this.domCell = new Element('td', {id:id});
140        else
141            this.domCell = new Element('td');
142    },
143
144    addContents: function( contents )
145    {
146        if( 'string' == typeof contents )
147            this.domCell.update(contents);
148        else
149            this.domCell.insert(contents);
150
151        return this.domCell;
152    },
153   
154    addColspan: function(colspan)
155    {
156        Element.writeAttribute(this.domCell, {colspan:colspan});
157    },
158   
159    addClassName: function(className)
160    {
161        Element.addClassName(this.domCell, className);
162    },
163});
164
165
166var Select = Class.create({
167    initialize: function( id, className, name, disabled )
168    {
169        this.id = id;
170        this.name = name;
171        this.valueKey = 'value';
172        this.textKey = 'text';
173        if( disabled )
174            this.select = new Element('select', {id:id, disabled:disabled});
175        else
176            this.select = new Element('select', {id:id});
177        this.select.addClassName(className);
178    },
179
180    setValueKey: function( key )
181    {
182        this.valueKey = key;
183    },
184
185    setTextKey: function( key )
186    {
187        this.textKey = key;
188    },
189
190    addOptions: function( options, selectedOption, onClickAction )
191    {
192        options.each(function( option )
193        {
194            this.addOption(option, selectedOption, onClickAction);
195        }.bind(this));
196    },
197
198    addOption: function( option, selectedOption, onClickAction )
199    {
200        var value = this.valueKey ? option[this.valueKey] : option;
201        var text = this.textKey ? option[this.textKey] : option;
202        if( null != selectedOption && value == selectedOption )
203            var option = new Element('option', {value:value, selected:"selected"}).update(text);
204        else
205            var option = new Element('option', {value:value}).update(text);
206
207        if( null != onClickAction )
208            Event.observe(option, 'click', onClickAction.bind(this));
209        this.select.insert(option);
210    },
211
212    addToElement: function( elementId )
213    {
214        $(elementId).insert(this.select);
215    }
216});
217
218
219var DomIdentifiedObject = Class.create(
220{
221    initialize: function( identifiedObject )
222    {
223        this.id = identifiedObject.id;
224        this.code = identifiedObject.code;
225    },
226
227    findInContainer: function( containerId )
228    {
229        var container = Dom.getContainer(containerId);
230        var row;
231        $A(container.childElements()).each(function( each )
232        {
233            if( this.id == each.readAttribute('id') )
234            {
235                row = each;
236                throw $break;
237            }
238        }.bind(this));
239        return row;
240    },
241
242    removeFromContainer: function( containerId )
243    {
244        var row = this.findInContainer(containerId);
245        if( undefined != row )
246        {
247            row.remove();
248        }
249    },
250
251    getBindedButton: function( text, callback, id )
252    {
253        if( id )
254            var button = new Element('input', {type: 'button', value: text, id: id});
255        else
256            var button = new Element('input', {type: 'button', value: text});
257        button.observe('click', callback.bindAsEventListener(this));
258        return button;
259    },
260
261    getInputText: function( idInput, nameInput, valueInput, classInput, actionForOnBlur )
262    {
263        var input = new Element('input', {type: 'text', name:nameInput, id:idInput, value:valueInput, onBlur:actionForOnBlur});
264        input.addClassName(classInput);
265        return input;
266    },
267
268    getCheckBox: function( idInput, nameInput, valueInput, actionForOnBlur )
269    {
270        var input = new Element('input', {type: 'checkbox', name:nameInput, checked:( "true" == valueInput ), id:idInput, onBlur:actionForOnBlur});
271        return input;
272    },
273   
274    getBindedText: function(text, callback)
275    {
276        var text = new Element('a', {class: 'bindedText'}).update(text);
277        text.observe('click', callback.bindAsEventListener(this));
278        return text;
279    },
280   
281    getLink: function( text, url)
282    {
283        var link = new Element('a', {href: url}).update(text);
284        return link;
285    },
286
287});
Note: See TracBrowser for help on using the repository browser.