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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
|
/*
* Low Pro JQ
* ----------
*
* Author: Dan Webb (dan@danwebb.net)
* GIT: github.com:danwrong/low-pro-for-jquery.git
* Download: http://github.com/danwrong/low-pro-for-jquery/tree/master/src/lowpro.jquery.js?raw=true
*
* A jQuery port of the Low Pro behavior framework that was originally written for Prototype.
*
* Prado actually uses it as a base to emulate OOP subclassing, inheritance and contructor events.
* The "behaviour" and the "Remote" bits are not used and have been commented out.
*/
(function($) {
var addMethods = function(source) {
var ancestor = this.superclass && this.superclass.prototype;
var properties = $.keys(source);
if (!$.keys({ toString: true }).length) properties.push("toString", "valueOf");
for (var i = 0, length = properties.length; i < length; i++) {
var property = properties[i], value = source[property];
if (ancestor && $.isFunction(value) && $.argumentNames(value)[0] == "$super") {
var method = value, value = $.extend($.wrap((function(m) {
return function() { return ancestor[m].apply(this, arguments) };
})(property), method), {
valueOf: function() { return method },
toString: function() { return method.toString() }
});
}
this.prototype[property] = value;
}
return this;
};
$.extend({
keys: function(obj) {
var keys = [];
for (var key in obj) keys.push(key);
return keys;
},
argumentNames: function(func) {
var names = func.toString().match(/^[\s\(]*function[^(]*\((.*?)\)/)[1].split(/, ?/);
return names.length == 1 && !names[0] ? [] : names;
},
bind: function(func, scope) {
return function() {
return func.apply(scope, $.makeArray(arguments));
};
},
wrap: function(func, wrapper) {
var __method = func;
return function() {
return wrapper.apply(this, [$.bind(__method, this)].concat($.makeArray(arguments)));
};
},
klass: function() {
var parent = null, properties = $.makeArray(arguments);
if ($.isFunction(properties[0])) parent = properties.shift();
var klass = function() {
this.initialize.apply(this, arguments);
};
klass.superclass = parent;
klass.subclasses = [];
klass.addMethods = addMethods;
if (parent) {
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
parent.subclasses.push(klass);
}
for (var i = 0; i < properties.length; i++)
klass.addMethods(properties[i]);
if (!klass.prototype.initialize)
klass.prototype.initialize = function() {};
klass.prototype.constructor = klass;
return klass;
},
delegate: function(rules) {
return function(e) {
var target = $(e.target), parent = null;
for (var selector in rules) {
if (target.is(selector) || ((parent = target.parents(selector)) && parent.length > 0)) {
return rules[selector].apply(this, [parent || target].concat($.makeArray(arguments)));
}
parent = null;
}
};
}
});
/*
var bindEvents = function(instance) {
for (var member in instance) {
if (member.match(/^on(.+)/) && typeof instance[member] == 'function') {
instance.element.live(RegExp.$1, {'behavior': instance}, instance[member]);
}
}
};
var behaviorWrapper = function(behavior) {
return $.klass(behavior, {
initialize: function($super, element, args) {
this.element = element;
if ($super) $super.apply(this, args);
},
trigger: function(eventType, extraParameters) {
var parameters = [this].concat(extraParameters);
this.element.trigger(eventType, parameters);
}
});
};
var attachBehavior = function(el, behavior, args) {
var wrapper = behaviorWrapper(behavior);
var instance = new wrapper(el, args);
bindEvents(instance);
if (!behavior.instances) behavior.instances = [];
behavior.instances.push(instance);
return instance;
};
$.fn.extend({
attach: function() {
var args = $.makeArray(arguments), behavior = args.shift();
attachBehavior(this, behavior, args);
return this;
},
delegate: function(type, rules) {
return this.bind(type, $.delegate(rules));
},
attached: function(behavior) {
var instances = [];
if (!behavior.instances) return instances;
this.each(function(i, element) {
$.each(behavior.instances, function(i, instance) {
if (instance.element.get(0) == element) instances.push(instance);
});
});
return instances;
},
firstAttached: function(behavior) {
return this.attached(behavior)[0];
}
});
Remote = $.klass({
initialize: function(options) {
if (this.element.attr('nodeName') == 'FORM') this.element.attach(Remote.Form, options);
else this.element.attach(Remote.Link, options);
}
});
Remote.Base = $.klass({
initialize : function(options) {
this.options = $.extend(true, {}, options || {});
},
_makeRequest : function(options) {
$.ajax(options);
return false;
}
});
Remote.Link = $.klass(Remote.Base, {
onclick: function(e) {
var options = $.extend({
url: $(this).attr('href'),
type: 'GET'
}, this.options);
return e.data.behavior._makeRequest(e.data.behavior.options);
}
});
Remote.Form = $.klass(Remote.Base, {
onclick: function(e) {
var target = e.target;
if ($.inArray(target.nodeName.toLowerCase(), ['input', 'button']) >= 0 && target.type.match(/submit|image/))
e.data.behavior._submitButton = target;
},
onsubmit: function(e) {
var elm = $(this), data = elm.serializeArray();
if (e.data.behavior._submitButton) data.push({
name: e.data.behavior._submitButton.name,
value: e.data.behavior._submitButton.value
});
var options = $.extend({
url : elm.attr('action'),
type : elm.attr('method') || 'GET',
data : data
}, e.data.behavior.options);
e.data.behavior._makeRequest(options);
return false;
}
});
$.ajaxSetup({
beforeSend: function(xhr) {
if (!this.dataType)
xhr.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, *\/*");
}
});
*/
})(jQuery);
/**
* Prado base namespace
* @namespace Prado
*/
var Prado =
{
/**
* Version of Prado clientscripts
* @var Version
*/
Version: '3.2.2',
/**
* Registry for Prado components
* @var Registry
*/
Registry: {},
};
Prado.RequestManager =
{
FIELD_POSTBACK_TARGET : 'PRADO_POSTBACK_TARGET',
FIELD_POSTBACK_PARAMETER : 'PRADO_POSTBACK_PARAMETER',
};
/**
* Performs a PostBack using javascript.
* @function Prado.PostBack
* @param options - Postback options
* @param event - Event that triggered this postback
* @... {string} FormID - Form that should be posted back
* @... {optional boolean} CausesValidation - Validate before PostBack if true
* @... {optional string} ValidationGroup - Group to Validate
* @... {optional string} ID - Validation ID
* @... {optional string} PostBackUrl - Postback URL
* @... {optional boolean} TrackFocus - Keep track of focused element if true
* @... {string} EventTarget - Id of element that triggered PostBack
* @... {string} EventParameter - EventParameter for PostBack
*/
Prado.PostBack = jQuery.klass(
{
options : {},
initialize: function(options, event)
{
jQuery.extend(this.options, options || {});
this.doPostBack();
},
getForm : function()
{
return jQuery("#" + this.options['FormID']).get(0);
},
doPostBack : function()
{
var form = this.getForm();
if(this.options['CausesValidation'] && typeof(Prado.Validation) != "undefined")
{
if(!Prado.Validation.validate(this.options['FormID'], this.options['ValidationGroup'], $("#" + this.options['ID'])))
return event.preventDefault();
}
if(this.options['PostBackUrl'] && this.options['PostBackUrl'].length > 0)
form.action = this.options['PostBackUrl'];
if(this.options['TrackFocus'])
{
var lastFocus = $('PRADO_LASTFOCUS');
if(lastFocus)
{
var active = document.activeElement; //where did this come from
if(active)
lastFocus.value = active.id;
else
lastFocus.value = this.options['EventTarget'];
}
}
var input=null;
if(this.options.EventTarget)
{
input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", Prado.RequestManager.FIELD_POSTBACK_TARGET);
input.setAttribute("value", this.options.EventTarget);
form.appendChild(input);
}
if(this.options.EventParameter)
{
input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", Prado.RequestManager.FIELD_POSTBACK_PARAMETER);
input.setAttribute("value", this.options.EventParameter);
form.appendChild(input);
}
jQuery(form).trigger('submit');
}
});
/**
* Prado utilities to manipulate DOM elements.
* @object Prado.Element
*/
Prado.Element =
{
/**
* Executes a jQuery method on a particular element.
* @function ?
* @param {string} element - Element id
* @param {string} method - method name
* @param {array} value - method parameters
*/
j: function(element, method, params)
{
var obj=jQuery("#" + element);
obj[method].apply(obj, params);
},
/**
* Select options from a selectable element.
* @function ?
* @param {string} element - Element id
* @param {string} method - Name of any {@link Prado.Element.Selection} method
* @param {array|boolean|string} value - Values that should be selected
* @param {int} total - Number of elements
*/
select : function(element, method, value, total)
{
var el = jQuery("#" + element).get(0);
if(!el) return;
var selection = Prado.Element.Selection;
if(typeof(selection[method]) == "function")
{
var control = selection.isSelectable(el) ? [el] : selection.getListElements(element,total);
selection[method](control, value);
}
},
/**
* Sets an attribute of a DOM element.
* @function ?
* @param {string} element - Element id
* @param {string} attribute - Name of attribute
* @param {string} value - Value of attribute
*/
setAttribute : function(element, attribute, value)
{
var el = jQuery("#" + element);
if(!el) return;
if((attribute == "disabled" || attribute == "multiple" || attribute == "readonly" || attribute == "href") && value==false)
el.removeAttr(attribute);
else if(attribute.match(/^on/i)) //event methods
{
try
{
eval("(func = function(event){"+value+"})");
el.get(0)[attribute] = func;
}
catch(e)
{
debugger;
throw "Error in evaluating '"+value+"' for attribute "+attribute+" for element "+element;
}
}
else
el.attr(attribute, value);
},
scrollTo : function(element, options)
{
var op = {
duration : 500,
offset : 50
};
jQuery.extend(op, options || {});
$('html, body').animate({
scrollTop: $("#"+element).offset().top - op.offset
}, op.duration);
},
/**
* Sets the options for a select element.
* @function ?
* @param {string} element - Element id
* @param {array[]} options - Array of options, each an array of structure
* [ "optionText" , "optionValue" , "optionGroup" ]
*/
setOptions : function(element, options)
{
var el = jQuery("#" + element).get(0);
var previousGroup = null;
var optGroup=null;
if(el && el.tagName.toLowerCase() == "select")
{
while(el.childNodes.length > 0)
el.removeChild(el.lastChild);
var optDom = Prado.Element.createOptions(options);
for(var i = 0; i < optDom.length; i++)
el.appendChild(optDom[i]);
}
},
/**
* Create opt-group options from an array of options.
* @function {array} ?
* @param {array[]} options - Array of options, each an array of structure
* [ "optionText" , "optionValue" , "optionGroup" ]
* @returns Array of option DOM elements
*/
createOptions : function(options)
{
var previousGroup = null;
var optgroup=null;
var result = [];
for(var i = 0; i<options.length; i++)
{
var option = options[i];
if(option.length > 2)
{
var group = option[2];
if(group!=previousGroup)
{
if(previousGroup!=null && optgroup!=null)
{
result.push(optgroup);
previousGroup=null;
optgroup=null;
}
optgroup = document.createElement('optgroup');
optgroup.label = group;
previousGroup = group;
}
}
var opt = document.createElement('option');
opt.text = option[0];
opt.innerHTML = option[0];
opt.value = option[1];
if(optgroup!=null)
optgroup.appendChild(opt);
else
result.push(opt);
}
if(optgroup!=null)
result.push(optgroup);
return result;
},
/**
* Replace a DOM element either with given content or
* with content from a CallBack response boundary
* using a replacement method.
* @function ?
* @param {string|element} element - DOM element or element id
* @param {optional string} content - New content of element
* @param {optional string} boundary - Boundary of new content
*/
replace : function(element, content, boundary)
{
if(boundary)
{
var result = this.extractContent(boundary);
if(result != null)
content = result;
}
jQuery('#'+element).replaceWith(content);
},
/**
* Appends a javascript block to the document.
* @function ?
* @param {string} boundary - Boundary containing the javascript code
*/
appendScriptBlock : function(boundary)
{
var content = this.extractContent(boundary);
if(content == null)
return;
var el = document.createElement("script");
el.type = "text/javascript";
el.id = 'inline_' + boundary;
el.text = content;
(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(el);
el.parentNode.removeChild(el);
},
/**
* Evaluate a javascript snippet from a string.
* @function ?
* @param {string} content - String containing the script
* @param {string} boundary - Boundary containing the script
*/
evaluateScript : function(content, boundary)
{
if(boundary)
{
var result = this.extractContent(boundary);
if(result != null)
content = result;
}
try
{
jQuery.globalEval(content);
}
catch(e)
{
if(typeof(Logger) != "undefined")
Logger.error('Error during evaluation of script "'+content+'"');
else
debugger;
throw e;
}
},
};
/**
* Utilities for selections.
* @object Prado.Element.Selection
*/
Prado.Element.Selection =
{
/**
* Check if an DOM element can be selected.
* @function {boolean} ?
* @param {element} el - DOM elemet
* @returns true if element is selectable
*/
isSelectable : function(el)
{
if(el && el.type)
{
switch(el.type.toLowerCase())
{
case 'checkbox':
case 'radio':
case 'select':
case 'select-multiple':
case 'select-one':
return true;
}
}
return false;
},
/**
* Set checked attribute of a checkbox or radiobutton to value.
* @function {boolean} ?
* @param {element} el - DOM element
* @param {boolean} value - New value of checked attribute
* @returns New value of checked attribute
*/
inputValue : function(el, value)
{
switch(el.type.toLowerCase())
{
case 'checkbox':
case 'radio':
return el.checked = value;
}
},
/**
* Set selected attribute for elements options by value.
* If value is boolean, all elements options selected attribute will be set
* to value. Otherwhise all options that have the given value will be selected.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
* @param {boolean|string} value - Value of options that should be selected or boolean value of selection status
*/
selectValue : function(elements, value)
{
jQuery.each(elements, function(idx, el)
{
jQuery.each(el.options, function(idx, option)
{
if(typeof(value) == "boolean")
option.selected = value;
else if(option.value == value)
option.selected = true;
});
})
},
/**
* Set selected attribute for elements options by array of values.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
* @param {string[]} value - Array of values to select
*/
selectValues : function(elements, values)
{
var selection = this;
jQuery.each(values, function(idx, value)
{
selection.selectValue(elements,value);
})
},
/**
* Set selected attribute for elements options by option index.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
* @param {int} index - Index of option to select
*/
selectIndex : function(elements, index)
{
jQuery.each(elements, function(idx, el)
{
if(el.type.toLowerCase() == 'select-one')
el.selectedIndex = index;
else
{
for(var i = 0; i<el.length; i++)
{
if(i == index)
el.options[i].selected = true;
}
}
})
},
/**
* Set selected attribute to true for all elements options.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
*/
selectAll : function(elements)
{
jQuery.each(elements, function(idx, el)
{
if(el.type.toLowerCase() != 'select-one')
{
jQuery.each(el.options, function(idx, option)
{
option.selected = true;
})
}
})
},
/**
* Toggle the selected attribute for elements options.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
*/
selectInvert : function(elements)
{
jQuery.each(elements, function(idx, el)
{
if(el.type.toLowerCase() != 'select-one')
{
jQuery.each(el.options, function(idx, option)
{
option.selected = !option.selected;
})
}
})
},
/**
* Set selected attribute for elements options by array of option indices.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
* @param {int[]} indices - Array of option indices to select
*/
selectIndices : function(elements, indices)
{
var selection = this;
jQuery.each(indices, function(idx, index)
{
selection.selectIndex(elements,index);
})
},
/**
* Unselect elements.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
*/
selectClear : function(elements)
{
jQuery.each(elements, function(idx, el)
{
el.selectedIndex = -1;
})
},
/**
* Get list elements of an element.
* @function {element[]} ?
* @param {element[]} elements - Array of selectable DOM elements
* @param {int} total - Number of list elements to return
* @returns Array of list DOM elements
*/
getListElements : function(element, total)
{
var elements = new Array();
var el;
for(var i = 0; i < total; i++)
{
el = $("#"+element+"_c"+i).get(0);
if(el)
elements.push(el);
}
return elements;
},
/**
* Set checked attribute of elements by value.
* If value is boolean, checked attribute will be set to value.
* Otherwhise all elements that have the given value will be checked.
* @function ?
* @param {element[]} elements - Array of checkable DOM elements
* @param {boolean|String} value - Value that should be checked or boolean value of checked status
*
*/
checkValue : function(elements, value)
{
jQuery.each(elements, function(idx, el)
{
if(typeof(value) == "boolean")
el.checked = value;
else if(el.value == value)
el.checked = true;
});
},
/**
* Set checked attribute of elements by array of values.
* @function ?
* @param {element[]} elements - Array of checkable DOM elements
* @param {string[]} values - Values that should be checked
*
*/
checkValues : function(elements, values)
{
var selection = this;
jQuery(values).each(function(idx, value)
{
selection.checkValue(elements, value);
})
},
/**
* Set checked attribute of elements by list index.
* @function ?
* @param {element[]} elements - Array of checkable DOM elements
* @param {int} index - Index of element to set checked
*/
checkIndex : function(elements, index)
{
for(var i = 0; i<elements.length; i++)
{
if(i == index)
elements[i].checked = true;
}
},
/**
* Set checked attribute of elements by array of list indices.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
* @param {int[]} indices - Array of list indices to set checked
*/
checkIndices : function(elements, indices)
{
var selection = this;
jQuery.each(indices, function(idx, index)
{
selection.checkIndex(elements, index);
})
},
/**
* Uncheck elements.
* @function ?
* @param {element[]} elements - Array of checkable DOM elements
*/
checkClear : function(elements)
{
jQuery.each(elements, function(idx, el)
{
el.checked = false;
});
},
/**
* Set checked attribute of all elements to true.
* @function ?
* @param {element[]} elements - Array of checkable DOM elements
*/
checkAll : function(elements)
{
jQuery.each(elements, function(idx, el)
{
el.checked = true;
})
},
/**
* Toggle the checked attribute of elements.
* @function ?
* @param {element[]} elements - Array of selectable DOM elements
*/
checkInvert : function(elements)
{
jQuery.each(elements, function(idx, el)
{
el.checked = !el.checked;
})
}
};
jQuery.extend(String.prototype, {
/**
* Add padding to string
* @function {string} ?
* @param {string} side - "left" to pad the string on the left, "right" to pad right.
* @param {int} len - Minimum string length.
* @param {string} chr - Character(s) to pad
* @returns Padded string
*/
pad : function(side, len, chr) {
if (!chr) chr = ' ';
var s = this;
var left = side.toLowerCase()=='left';
while (s.length<len) s = left? chr + s : s + chr;
return s;
},
/**
* Add left padding to string
* @function {string} ?
* @param {int} len - Minimum string length.
* @param {string} chr - Character(s) to pad
* @returns Padded string
*/
padLeft : function(len, chr) {
return this.pad('left',len,chr);
},
/**
* Add right padding to string
* @function {string} ?
* @param {int} len - Minimum string length.
* @param {string} chr - Character(s) to pad
* @returns Padded string
*/
padRight : function(len, chr) {
return this.pad('right',len,chr);
},
/**
* Add zeros to the right of string
* @function {string} ?
* @param {int} len - Minimum string length.
* @returns Padded string
*/
zerofill : function(len) {
return this.padLeft(len,'0');
},
/**
* Remove white spaces from both ends of string.
* @function {string} ?
* @returns Trimmed string
*/
trim : function() {
return this.replace(/^\s+|\s+$/g,'');
},
/**
* Remove white spaces from the left side of string.
* @function {string} ?
* @returns Trimmed string
*/
trimLeft : function() {
return this.replace(/^\s+/,'');
},
/**
* Remove white spaces from the right side of string.
* @function {string} ?
* @returns Trimmed string
*/
trimRight : function() {
return this.replace(/\s+$/,'');
},
/**
* Convert period separated function names into a function reference.
* <br />Example:
* <pre>
* "Prado.AJAX.Callback.Action.setValue".toFunction()
* </pre>
* @function {function} ?
* @returns Reference to the corresponding function
*/
toFunction : function()
{
var commands = this.split(/\./);
var command = window;
jQuery(commands).each(function(idx, action)
{
if(command[new String(action)])
command=command[new String(action)];
});
if(typeof(command) == "function")
return command;
else
{
if(typeof Logger != "undefined")
Logger.error("Missing function", this);
throw new Error ("Missing function '"+this+"'");
}
},
/**
* Convert string into integer, returns null if not integer.
* @function {int} ?
* @returns Integer, null if string does not represent an integer.
*/
toInteger : function()
{
var exp = /^\s*[-\+]?\d+\s*$/;
if (this.match(exp) == null)
return null;
var num = parseInt(this, 10);
return (isNaN(num) ? null : num);
},
/**
* Convert string into a double/float value. <b>Internationalization
* is not supported</b>
* @function {double} ?
* @param {string} decimalchar - Decimal character, defaults to "."
* @returns Double, null if string does not represent a float value
*/
toDouble : function(decimalchar)
{
if(this.length <= 0) return null;
decimalchar = decimalchar || ".";
var exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + decimalchar + "(\\d+))?\\s*$");
var m = this.match(exp);
if (m == null)
return null;
m[1] = m[1] || "";
m[2] = m[2] || "0";
m[4] = m[4] || "0";
var cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
var num = parseFloat(cleanInput);
return (isNaN(num) ? null : num);
},
/**
* Convert strings that represent a currency value to float.
* E.g. "10,000.50" will become "10000.50". The number
* of dicimal digits, grouping and decimal characters can be specified.
* <i>The currency input format is <b>very</b> strict, null will be returned if
* the pattern does not match</i>.
* @function {double} ?
* @param {string} groupchar - Grouping character, defaults to ","
* @param {int} digits - Number of decimal digits
* @param {string} decimalchar - Decimal character, defaults to "."
* @returns Double, null if string does not represent a currency value
*/
toCurrency : function(groupchar, digits, decimalchar)
{
groupchar = groupchar || ",";
decimalchar = decimalchar || ".";
digits = typeof(digits) == "undefined" ? 2 : digits;
var exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + groupchar + ")*)(\\d+)"
+ ((digits > 0) ? "(\\" + decimalchar + "(\\d{1," + digits + "}))?" : "")
+ "\\s*$");
var m = this.match(exp);
if (m == null)
return null;
var intermed = m[2] + m[5] ;
var cleanInput = m[1] + intermed.replace(
new RegExp("(\\" + groupchar + ")", "g"), "")
+ ((digits > 0) ? "." + m[7] : "");
var num = parseFloat(cleanInput);
return (isNaN(num) ? null : num);
}
});
jQuery.extend(Date.prototype,
{
/**
* SimpleFormat
* @function ?
* @param {string} format - TODO
* @param {string} data - TODO
* @returns TODO
*/
SimpleFormat: function(format, data)
{
data = data || {};
var bits = new Array();
bits['d'] = this.getDate();
bits['dd'] = String(this.getDate()).zerofill(2);
bits['M'] = this.getMonth()+1;
bits['MM'] = String(this.getMonth()+1).zerofill(2);
if(data.AbbreviatedMonthNames)
bits['MMM'] = data.AbbreviatedMonthNames[this.getMonth()];
if(data.MonthNames)
bits['MMMM'] = data.MonthNames[this.getMonth()];
var yearStr = "" + this.getFullYear();
yearStr = (yearStr.length == 2) ? '19' + yearStr: yearStr;
bits['yyyy'] = yearStr;
bits['yy'] = bits['yyyy'].toString().substr(2,2);
// do some funky regexs to replace the format string
// with the real values
var frm = new String(format);
for (var sect in bits)
{
var reg = new RegExp("\\b"+sect+"\\b" ,"g");
frm = frm.replace(reg, bits[sect]);
}
return frm;
},
/**
* toISODate
* @function {string} ?
* @returns TODO
*/
toISODate : function()
{
var y = this.getFullYear();
var m = String(this.getMonth() + 1).zerofill(2);
var d = String(this.getDate()).zerofill(2);
return String(y) + String(m) + String(d);
}
});
jQuery.extend(Date,
{
/**
* SimpleParse
* @function ?
* @param {string} format - TODO
* @param {string} data - TODO
* @returns TODO
*/
SimpleParse: function(value, format)
{
var val=String(value);
format=String(format);
if(val.length <= 0) return null;
if(format.length <= 0) return new Date(value);
var isInteger = function (val)
{
var digits="1234567890";
for (var i=0; i < val.length; i++)
{
if (digits.indexOf(val.charAt(i))==-1) { return false; }
}
return true;
};
var getInt = function(str,i,minlength,maxlength)
{
for (var x=maxlength; x>=minlength; x--)
{
var token=str.substring(i,i+x);
if (token.length < minlength) { return null; }
if (isInteger(token)) { return token; }
}
return null;
};
var i_val=0;
var i_format=0;
var c="";
var token="";
var token2="";
var x,y;
var now=new Date();
var year=now.getFullYear();
var month=now.getMonth()+1;
var date=1;
while (i_format < format.length)
{
// Get next token from format string
c=format.charAt(i_format);
token="";
while ((format.charAt(i_format)==c) && (i_format < format.length))
{
token += format.charAt(i_format++);
}
// Extract contents of value based on format token
if (token=="yyyy" || token=="yy" || token=="y")
{
if (token=="yyyy") { x=4;y=4; }
if (token=="yy") { x=2;y=2; }
if (token=="y") { x=2;y=4; }
year=getInt(val,i_val,x,y);
if (year==null) { return null; }
i_val += year.length;
if (year.length==2)
{
if (year > 70) { year=1900+(year-0); }
else { year=2000+(year-0); }
}
}
else if (token=="MM"||token=="M")
{
month=getInt(val,i_val,token.length,2);
if(month==null||(month<1)||(month>12)){return null;}
i_val+=month.length;
}
else if (token=="dd"||token=="d")
{
date=getInt(val,i_val,token.length,2);
if(date==null||(date<1)||(date>31)){return null;}
i_val+=date.length;
}
else
{
if (val.substring(i_val,i_val+token.length)!=token) {return null;}
else {i_val+=token.length;}
}
}
// If there are any trailing characters left in the value, it doesn't match
if (i_val != val.length) { return null; }
// Is date valid for month?
if (month==2)
{
// Check for leap year
if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
if (date > 29){ return null; }
}
else { if (date > 28) { return null; } }
}
if ((month==4)||(month==6)||(month==9)||(month==11))
{
if (date > 30) { return null; }
}
var newdate=new Date(year,month-1,date, 0, 0, 0);
return newdate;
}
});
|