1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
Prado.WebUI = jQuery.klass();
Prado.WebUI.Control = jQuery.klass({
initialize : function(options)
{
this.registered = false;
this.ID = options.ID;
this.element = jQuery("#" + this.ID).get(0);
this.observers = new Array();
this.intervals = new Array();
var e;
if (jQuery.inArray(this.ID, Prado.Registry) == -1)
this.register(options);
else
this.replace(Prado.Registry[this.ID], options);
if (this === Prado.Registry[this.ID])
{
this.registered = true;
if(this.onInit)
this.onInit(options);
}
},
/**
* Registers the control wrapper in the Prado client side control registry
* @param array control wrapper options
*/
register : function(options)
{
return Prado.Registry[options.ID] = this;
},
/**
* De-registers the control wrapper in the Prado client side control registry
*/
deregister : function()
{
// extra check so we don't ever deregister another wrapper
var value = Prado.Registry[this.ID];
if (value===this)
{
delete Prado.Registry[this.ID];
return value;
}
else
debugger; // invoke debugger - this should never happen
},
/**
* Replaces and control wrapper for an already existing control in the Prado client side control registry
* @param object reference to the old wrapper
* @param array control wrapper options
*/
replace : function(oldwrapper, options)
{
// if there's some advanced state management in the wrapper going on, then
// this method could be used either to copy the current state of the control
// from the old wrapper to this new one (which then could live on, while the old
// one could get destroyed), or to copy the new, changed options to the old wrapper,
// (which could then left intact to keep working, while this new wrapper could be
// disposed of by exiting its initialization without installing any handlers or
// leaving any references to it)
//
// for now this method is simply deinitializing and deregistering the old wrapper,
// and then registering the new wrapper for the control id
if (oldwrapper.deinitialize)
oldwrapper.deinitialize();
return this.register(options);
},
/**
* Registers an event observer which will be automatically disposed of when the wrapper
* is deregistered
* @param element DOM element reference or id to attach the event handler to
* @param string event name to observe
* @param handler event handler function
*/
observe: function(element, eventName, handler, options)
{
var e = { _element: element, _eventName: eventName, _handler: handler };
this.observers.push(e);
return jQuery(e._element).bind(e._eventName, options, e._handler);
},
/**
* Checks whether an event observer is installed and returns its index
* @param element DOM element reference or id the event handler was attached to
* @param string event name observed
* @param handler event handler function
* @result int false if the event handler is not installed, or 1-based index when installed
*/
findObserver: function(element, eventName, handler)
{
var e = { _element: element, _eventName: eventName, _handler: handler };
var idx = -1;
for(var i=0;i<this.observers.length;i++)
{
var o = this.observers[i];
if ((o._element===element) && (o._eventName===eventName) && (o._handler===handler))
{
idx = i;
break;
}
}
return idx;
},
/**
* Degisters an event observer from the list of automatically disposed handlers
* @param element DOM element reference or id the event handler was attached to
* @param string event name observed
* @param handler event handler function
*/
stopObserving: function(element, eventName, handler)
{
var idx = this.findObserver(element,eventName,handler);
if (idx!=-1)
this.observers = this.observers.without(this.observers[idx]);
else
debugger; // shouldn't happen
return jQuery(element).unbind(eventName, handler);
},
/**
* Registers a code snippet or function to be executed after a delay, if the
* wrapper hasn't been destroyed in the meantime
* @param code function or code snippet to execute
* @param int number of milliseconds to wait before executing
* @return int unique ID that can be used to cancel the scheduled execution
*/
setTimeout: function(func, delay)
{
if (!jQuery.isFunction(func))
{
var expr = func;
func = function() { return eval(expr); }
};
var obj = this;
return window.setTimeout(function() {
if (!obj.isLingering())
func();
obj = null;
},delay);
},
/**
* Cancels a previously scheduled code snippet or function
* @param int unique ID returned by setTimeout()
*/
clearTimeout: function(timeoutid)
{
return window.clearTimeout(timeoutid);
},
/**
* Registers a code snippet or function to be executed periodically, up until the
* wrapper gets destroyed or the schedule cancelled using cancelInterval()
* @param code function or code snippet to execute
* @param int number of milliseconds to wait before executing
* @return int unique ID that can be used to cancel the interval (see clearInterval() method)
*/
setInterval: function(func, delay)
{
if (!jQuery.isFunction(func)) func = function() { eval(func); };
var obj = this;
var h = window.setInterval(function() {
if (!obj.isLingering())
func();
},delay);
this.intervals.push(h);
return h;
},
/**
* Deregisters a snipper or function previously registered with setInterval()
* @param int unique ID of interval (returned by setInterval() previously)
*/
clearInterval: function(intervalid)
{
window.clearInterval(intervalid);
this.intervals.splice( jQuery.inArray(intervalid, this.intervals), 1 );
},
/**
* Tells whether this is a wrapper that has already been deregistered and is lingering
* @return bool true if object
*/
isLingering: function()
{
return !this.registered;
},
/**
* Deinitializes the control wrapper by calling the onDone method and the deregistering it
* @param array control wrapper options
*/
deinitialize : function()
{
if (this.registered)
{
if(this.onDone)
this.onDone();
// automatically stop all intervals
while (this.intervals.length>0)
window.clearInterval(this.intervals.pop());
// automatically deregister all installed observers
while (this.observers.length>0)
{
var e = this.observers.pop();
jQuery(e._element).unbind(e._eventName, e._handler);
}
}
else
debugger; // shouldn't happen
this.deregister();
this.registered = false;
}
});
Prado.WebUI.PostBackControl = jQuery.klass(Prado.WebUI.Control, {
onInit : function(options)
{
this._elementOnClick = null;
if (!this.element)
debugger; // element not found
else
{
//capture the element's onclick function
if(typeof(this.element.onclick)=="function")
{
this._elementOnClick = this.element.onclick.bind(this.element);
this.element.onclick = null;
}
this.observe(this.element, "click", jQuery.proxy(this.elementClicked,this,options));
}
},
elementClicked : function(options, event)
{
var src = event.target;
var doPostBack = true;
var onclicked = null;
if(this._elementOnClick)
{
var onclicked = this._elementOnClick(event);
if(typeof(onclicked) == "boolean")
doPostBack = onclicked;
}
if(doPostBack && !jQuery(src).is(':disabled'))
this.onPostBack(options,event);
if(typeof(onclicked) == "boolean" && !onclicked)
event.stopPropagation();
},
onPostBack : function(options, event)
{
new Prado.PostBack(options, event);
}
});
Prado.WebUI.TButton = jQuery.klass(Prado.WebUI.PostBackControl);
Prado.WebUI.TLinkButton = jQuery.klass(Prado.WebUI.PostBackControl);
Prado.WebUI.TCheckBox = jQuery.klass(Prado.WebUI.PostBackControl);
Prado.WebUI.TBulletedList = jQuery.klass(Prado.WebUI.PostBackControl);
Prado.WebUI.TImageMap = jQuery.klass(Prado.WebUI.PostBackControl);
/**
* TImageButton client-side behaviour. With validation, Firefox needs
* to capture the x,y point of the clicked image in hidden form fields.
*/
Prado.WebUI.TImageButton = jQuery.klass(Prado.WebUI.PostBackControl,
{
/**
* Override parent onPostBack function, tried to add hidden forms
* inputs to capture x,y clicked point.
*/
onPostBack : function(event, options)
{
this.addXYInput(event,options);
Prado.PostBack(event, options);
this.removeXYInput(event,options);
},
/**
* Add hidden inputs to capture the x,y point clicked on the image.
* @param event DOM click event.
* @param array image button options.
*/
addXYInput : function(event,options)
{
var imagePos = jQuery(this.element).offset();
var clickedPos = [event.clientX, event.clientY];
var x = clickedPos[0]-imagePos['left']+1;
var y = clickedPos[1]-imagePos['top']+1;
x = x < 0 ? 0 : x;
y = y < 0 ? 0 : y;
var id = this.element.id;
var name = options['EventTarget'];
var form = this.element.form;
var input=null;
input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("id", id+"_x");
input.setAttribute("name", name+"_x");
input.setAttribute("value", x);
form.appendChild(input);
input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("id", id+"_y");
input.setAttribute("name", name+"_y");
input.setAttribute("value", y);
form.appendChild(input);
},
/**
* Remove hidden inputs for x,y-click capturing
* @param event DOM click event.
* @param array image button options.
*/
removeXYInput : function(event,options)
{
var id = this.element.id;
jQuery('#'+id+'_x').remove();
jQuery('#'+id+'_y').remove();
}
});
/**
* Radio button, only initialize if not already checked.
*/
Prado.WebUI.TRadioButton = jQuery.klass(Prado.WebUI.PostBackControl,
{
initialize : function($super, options)
{
this.element = $("#" + options['ID']).get(0);
if(this.element)
{
if(!this.element.checked)
$super(options);
}
}
});
Prado.WebUI.TTextBox = jQuery.klass(Prado.WebUI.PostBackControl,
{
onInit : function(options)
{
this.options=options;
if(this.options['TextMode'] != 'MultiLine')
this.observe(this.element, "keydown", this.handleReturnKey.bind(this));
if(this.options['AutoPostBack']==true)
this.observe(this.element, "change", jQuery.proxy(Prado.PostBack,this,options));
},
handleReturnKey : function(e)
{
if(e.keyCode == 13) // KEY_RETURN
{
var target = e.target;
if(target)
{
if(this.options['AutoPostBack']==true)
{
$(target).trigger( "change" );
e.stopPropagation();
}
else
{
if(this.options['CausesValidation'] && typeof(Prado.Validation) != "undefined")
{
if(!Prado.Validation.validate(this.options['FormID'], this.options['ValidationGroup'], $(this.options['ID'])))
return e.stopPropagation();
}
}
}
}
}
});
Prado.WebUI.TListControl = jQuery.klass(Prado.WebUI.PostBackControl,
{
onInit : function(options)
{
this.observe(this.element, "change", jQuery.proxy(Prado.PostBack,this,options));
}
});
Prado.WebUI.TListBox = jQuery.klass(Prado.WebUI.TListControl);
Prado.WebUI.TDropDownList = jQuery.klass(Prado.WebUI.TListControl);
Prado.WebUI.DefaultButton = jQuery.klass(Prado.WebUI.Control,
{
onInit : function(options)
{
this.options = options;
this.observe(jQuery('#'+options['Panel']), "keydown", jQuery.proxy(this.triggerEvent,this));
},
triggerEvent : function(ev)
{
var enterPressed = ev.keyCode == 13;
var isTextArea = ev.target.tagName.toLowerCase() == "textarea";
var isHyperLink = ev.target.tagName.toLowerCase() == "a" && ev.target.hasAttribute("href");
var isValidButton = ev.target.tagName.toLowerCase() == "input" && ev.target.type.toLowerCase() == "submit";
if(enterPressed && !isTextArea && !isValidButton && !isHyperLink)
{
var defaultButton = $('#'+this.options['Target']);
if(defaultButton)
{
this.triggered = true;
defaultButton.trigger(this.options['Event']);
ev.preventDefault();
}
}
}
});
Prado.WebUI.TTextHighlighter = jQuery.klass();
Prado.WebUI.TTextHighlighter.prototype =
{
initialize:function(id)
{
if(!window.clipboardData) return;
var options =
{
href : 'javascript:;/'+'/copy code to clipboard',
onclick : 'Prado.WebUI.TTextHighlighter.copy(this)',
onmouseover : 'Prado.WebUI.TTextHighlighter.hover(this)',
onmouseout : 'Prado.WebUI.TTextHighlighter.out(this)'
}
var div = DIV({className:'copycode'}, A(options, 'Copy Code'));
document.write(DIV(null,div).innerHTML);
}
};
jQuery.klass(Prado.WebUI.TTextHighlighter,
{
copy : function(obj)
{
var parent = obj.parentNode.parentNode.parentNode;
var text = '';
for(var i = 0; i < parent.childNodes.length; i++)
{
var node = parent.childNodes[i];
if(node.innerText)
text += node.innerText == 'Copy Code' ? '' : node.innerText;
else
text += node.nodeValue;
}
if(text.length > 0)
window.clipboardData.setData("Text", text);
},
hover : function(obj)
{
obj.parentNode.className = "copycode copycode_hover";
},
out : function(obj)
{
obj.parentNode.className = "copycode";
}
});
Prado.WebUI.TCheckBoxList = jQuery.klass(Prado.WebUI.Control,
{
constructor : function(options)
{
Prado.Registry.set(options.ListID, this);
for(var i = 0; i<options.ItemCount; i++)
{
var checkBoxOptions = jQuery.extend(
{
ID : options.ListID+"_c"+i,
EventTarget : options.ListName+"$c"+i
}, options);
new Prado.WebUI.TCheckBox(checkBoxOptions);
}
}
});
Prado.WebUI.TRadioButtonList = jQuery.klass(Prado.WebUI.Control,
{
constructor : function(options)
{
Prado.Registry.set(options.ListID, this);
for(var i = 0; i<options.ItemCount; i++)
{
var radioButtonOptions = jQuery.extend(
{
ID : options.ListID+"_c"+i,
EventTarget : options.ListName+"$c"+i
}, options);
new Prado.WebUI.TRadioButton(radioButtonOptions);
}
}
});
|