1 Prado.WebUI = Class.create();
3 Prado.WebUI.Control = Class.create({
5 initialize : function(options)
7 this.registered = false;
9 this.element = $(this.ID);
10 this.observers = new Array();
11 this.intervals = new Array();
13 if (e = Prado.Registry.get(this.ID))
14 this.replace(e, options);
16 this.register(options);
18 if (this === Prado.Registry.get(this.ID))
20 this.registered = true;
27 * Registers the control wrapper in the Prado client side control registry
28 * @param array control wrapper options
30 register : function(options)
32 return Prado.Registry.set(options.ID, this);
36 * De-registers the control wrapper in the Prado client side control registry
38 deregister : function()
40 // extra check so we don't ever deregister another wrapper
41 if (Prado.Registry.get(this.ID)===this)
42 return Prado.Registry.unset(this.ID);
44 debugger; // invoke debugger - this should never happen
48 * Replaces and control wrapper for an already existing control in the Prado client side control registry
49 * @param object reference to the old wrapper
50 * @param array control wrapper options
52 replace : function(oldwrapper, options)
54 // if there's some advanced state management in the wrapper going on, then
55 // this method could be used either to copy the current state of the control
56 // from the old wrapper to this new one (which then could live on, while the old
57 // one could get destroyed), or to copy the new, changed options to the old wrapper,
58 // (which could then left intact to keep working, while this new wrapper could be
59 // disposed of by exiting its initialization without installing any handlers or
60 // leaving any references to it)
63 // for now this method is simply deinitializing and deregistering the old wrapper,
64 // and then registering the new wrapper for the control id
66 if (oldwrapper.deinitialize)
67 oldwrapper.deinitialize();
69 return this.register(options);
73 * Registers an event observer which will be automatically disposed of when the wrapper
75 * @param element DOM element reference or id to attach the event handler to
76 * @param string event name to observe
77 * @param handler event handler function
79 observe: function(element, eventName, handler)
81 var e = { _element: element, _eventName: eventName, _handler: handler };
82 this.observers.push(e);
83 return Event.observe(e._element,e._eventName,e._handler);
87 * Checks whether an event observer is installed and returns its index
88 * @param element DOM element reference or id the event handler was attached to
89 * @param string event name observed
90 * @param handler event handler function
91 * @result int false if the event handler is not installed, or 1-based index when installed
93 findObserver: function(element, eventName, handler)
95 var e = { _element: element, _eventName: eventName, _handler: handler };
97 for(var i=0;i<this.observers.length;i++)
99 var o = this.observers[i];
100 if ((o._element===element) && (o._eventName===eventName) && (o._handler===handler))
111 * Degisters an event observer from the list of automatically disposed handlers
112 * @param element DOM element reference or id the event handler was attached to
113 * @param string event name observed
114 * @param handler event handler function
116 stopObserving: function(element, eventName, handler)
118 var idx = this.findObserver(element,eventName,handler);
120 this.observers = this.observers.without(this.observers[idx]);
122 debugger; // shouldn't happen
124 return Event.stopObserving(element,eventName,handler);
128 * Registers a code snippet or function to be executed after a delay, if the
129 * wrapper hasn't been destroyed in the meantime
130 * @param code function or code snippet to execute
131 * @param int number of milliseconds to wait before executing
132 * @return int unique ID that can be used to cancel the scheduled execution
134 setTimeout: function(func, delay)
136 if (!Object.isFunction(func))
139 func = function() { return eval(expr); }
142 return window.setTimeout(function() {
143 if (!obj.isLingering())
150 * Cancels a previously scheduled code snippet or function
151 * @param int unique ID returned by setTimeout()
153 clearTimeout: function(timeoutid)
155 return window.clearTimeout(timeoutid);
159 * Registers a code snippet or function to be executed periodically, up until the
160 * wrapper gets destroyed or the schedule cancelled using cancelInterval()
161 * @param code function or code snippet to execute
162 * @param int number of milliseconds to wait before executing
163 * @return int unique ID that can be used to cancel the interval (see clearInterval() method)
165 setInterval: function(func, delay)
167 if (!Object.isFunction(func)) func = function() { eval(func); };
169 var h = window.setInterval(function() {
170 if (!obj.isLingering())
173 this.intervals.push(h);
178 * Deregisters a snipper or function previously registered with setInterval()
179 * @param int unique ID of interval (returned by setInterval() previously)
181 clearInterval: function(intervalid)
183 window.clearInterval(intervalid);
184 this.intervals = this.intervals.without(intervalid);
188 * Tells whether this is a wrapper that has already been deregistered and is lingering
189 * @return bool true if object
191 isLingering: function()
193 return !this.registered;
197 * Deinitializes the control wrapper by calling the onDone method and the deregistering it
198 * @param array control wrapper options
200 deinitialize : function()
207 // automatically stop all intervals
208 while (this.intervals.length>0)
209 window.clearInterval(this.intervals.pop());
211 // automatically deregister all installed observers
212 while (this.observers.length>0)
214 var e = this.observers.pop();
215 Event.stopObserving(e._element,e._eventName,e._handler);
219 debugger; // shouldn't happen
223 this.registered = false;
228 Prado.WebUI.PostBackControl = Class.create(Prado.WebUI.Control, {
230 onInit : function(options)
232 this._elementOnClick = null;
235 debugger; // element not found
238 //capture the element's onclick function
239 if(typeof(this.element.onclick)=="function")
241 this._elementOnClick = this.element.onclick.bind(this.element);
242 this.element.onclick = null;
244 this.observe(this.element, "click", this.elementClicked.bindEvent(this,options));
248 elementClicked : function(event, options)
250 var src = Event.element(event);
251 var doPostBack = true;
252 var onclicked = null;
254 if(this._elementOnClick)
256 var onclicked = this._elementOnClick(event);
257 if(typeof(onclicked) == "boolean")
258 doPostBack = onclicked;
260 if(doPostBack && !Prado.Element.isDisabled(src))
261 this.onPostBack(event,options);
262 if(typeof(onclicked) == "boolean" && !onclicked)
266 onPostBack : function(event, options)
268 Prado.PostBack(event,options);
273 Prado.WebUI.TButton = Class.create(Prado.WebUI.PostBackControl);
274 Prado.WebUI.TLinkButton = Class.create(Prado.WebUI.PostBackControl);
275 Prado.WebUI.TCheckBox = Class.create(Prado.WebUI.PostBackControl);
276 Prado.WebUI.TBulletedList = Class.create(Prado.WebUI.PostBackControl);
277 Prado.WebUI.TImageMap = Class.create(Prado.WebUI.PostBackControl);
280 * TImageButton client-side behaviour. With validation, Firefox needs
281 * to capture the x,y point of the clicked image in hidden form fields.
283 Prado.WebUI.TImageButton = Class.create(Prado.WebUI.PostBackControl,
286 * Override parent onPostBack function, tried to add hidden forms
287 * inputs to capture x,y clicked point.
289 onPostBack : function(event, options)
291 this.addXYInput(event,options);
292 Prado.PostBack(event, options);
293 this.removeXYInput(event,options);
297 * Add hidden inputs to capture the x,y point clicked on the image.
298 * @param event DOM click event.
299 * @param array image button options.
301 addXYInput : function(event,options)
303 var imagePos = this.element.cumulativeOffset();
304 var clickedPos = [event.clientX, event.clientY];
305 var x = clickedPos[0]-imagePos[0]+1;
306 var y = clickedPos[1]-imagePos[1]+1;
309 var id = options['EventTarget'];
310 var x_input = $(id+"_x");
311 var y_input = $(id+"_y");
318 x_input = INPUT({type:'hidden',name:id+'_x','id':id+'_x',value:x});
319 this.element.parentNode.appendChild(x_input);
327 y_input = INPUT({type:'hidden',name:id+'_y','id':id+'_y',value:y});
328 this.element.parentNode.appendChild(y_input);
333 * Remove hidden inputs for x,y-click capturing
334 * @param event DOM click event.
335 * @param array image button options.
337 removeXYInput : function(event,options)
339 var id = options['EventTarget'];
340 this.element.parentNode.removeChild($(id+"_x"));
341 this.element.parentNode.removeChild($(id+"_y"));
347 * Radio button, only initialize if not already checked.
349 Prado.WebUI.TRadioButton = Class.create(Prado.WebUI.PostBackControl,
351 initialize : function($super, options)
353 this.element = $(options['ID']);
356 if(!this.element.checked)
363 Prado.WebUI.TTextBox = Class.create(Prado.WebUI.PostBackControl,
365 onInit : function(options)
367 this.options=options;
368 if(this.options['TextMode'] != 'MultiLine')
369 this.observe(this.element, "keydown", this.handleReturnKey.bind(this));
370 if(this.options['AutoPostBack']==true)
371 this.observe(this.element, "change", Prado.PostBack.bindEvent(this,options));
374 handleReturnKey : function(e)
376 if(Event.keyCode(e) == Event.KEY_RETURN)
378 var target = Event.element(e);
381 if(this.options['AutoPostBack']==true)
383 Event.fireEvent(target, "change");
388 if(this.options['CausesValidation'] && typeof(Prado.Validation) != "undefined")
390 if(!Prado.Validation.validate(this.options['FormID'], this.options['ValidationGroup'], $(this.options['ID'])))
391 return Event.stop(e);
399 Prado.WebUI.TListControl = Class.create(Prado.WebUI.PostBackControl,
401 onInit : function(options)
403 this.observe(this.element, "change", Prado.PostBack.bindEvent(this,options));
407 Prado.WebUI.TListBox = Class.create(Prado.WebUI.TListControl);
408 Prado.WebUI.TDropDownList = Class.create(Prado.WebUI.TListControl);
410 Prado.WebUI.DefaultButton = Class.create(Prado.WebUI.Control,
412 onInit : function(options)
414 this.options = options;
415 this.observe(options['Panel'], 'keydown', this.triggerEvent.bindEvent(this));
418 triggerEvent : function(ev, target)
420 var enterPressed = Event.keyCode(ev) == Event.KEY_RETURN;
421 var isTextArea = Event.element(ev).tagName.toLowerCase() == "textarea";
422 var isHyperLink = Event.element(ev).tagName.toLowerCase() == "a" && Event.element(ev).hasAttribute("href");
423 var isValidButton = Event.element(ev).tagName.toLowerCase() == "input" && Event.element(ev).type.toLowerCase() == "submit";
425 if(enterPressed && !isTextArea && !isValidButton && !isHyperLink)
427 var defaultButton = $(this.options['Target']);
430 this.triggered = true;
431 Event.fireEvent(defaultButton, this.options['Event']);
438 Prado.WebUI.TTextHighlighter = Class.create();
439 Prado.WebUI.TTextHighlighter.prototype =
441 initialize:function(id)
443 if(!window.clipboardData) return;
446 href : 'javascript:;/'+'/copy code to clipboard',
447 onclick : 'Prado.WebUI.TTextHighlighter.copy(this)',
448 onmouseover : 'Prado.WebUI.TTextHighlighter.hover(this)',
449 onmouseout : 'Prado.WebUI.TTextHighlighter.out(this)'
451 var div = DIV({className:'copycode'}, A(options, 'Copy Code'));
452 document.write(DIV(null,div).innerHTML);
456 Object.extend(Prado.WebUI.TTextHighlighter,
460 var parent = obj.parentNode.parentNode.parentNode;
462 for(var i = 0; i < parent.childNodes.length; i++)
464 var node = parent.childNodes[i];
466 text += node.innerText == 'Copy Code' ? '' : node.innerText;
468 text += node.nodeValue;
471 window.clipboardData.setData("Text", text);
474 hover : function(obj)
476 obj.parentNode.className = "copycode copycode_hover";
481 obj.parentNode.className = "copycode";
486 Prado.WebUI.TCheckBoxList = Base.extend(
488 constructor : function(options)
490 Prado.Registry.set(options.ListID, this);
491 for(var i = 0; i<options.ItemCount; i++)
493 var checkBoxOptions = Object.extend(
495 ID : options.ListID+"_c"+i,
496 EventTarget : options.ListName+"$c"+i
498 new Prado.WebUI.TCheckBox(checkBoxOptions);
503 Prado.WebUI.TRadioButtonList = Base.extend(
505 constructor : function(options)
507 Prado.Registry.set(options.ListID, this);
508 for(var i = 0; i<options.ItemCount; i++)
510 var radioButtonOptions = Object.extend(
512 ID : options.ListID+"_c"+i,
513 EventTarget : options.ListName+"$c"+i
515 new Prado.WebUI.TRadioButton(radioButtonOptions);